return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  BEGIN
  	
  	
  		-- Géographie
  		INSERT INTO base.t_regions(oid, code, texte, texte_court)
  		SELECT 0, chr(127) || '**', 'Non renseigné', 'Non renseigné' 
  		WHERE 0 NOT IN (SELECT oid FROM base.t_regions);
  		
  		INSERT INTO base.t_departements(oid, code, texte, texte_court)
  		SELECT 0, chr(127) || '**', 'Non renseigné', 'Non renseigné' 
  		WHERE 0 NOT IN (SELECT oid FROM base.t_departements);
  		
  		INSERT INTO base.t_territoires_sante(oid, code, texte, texte_court)
  		SELECT 0, chr(127) || '*******', 'Non renseigné', 'Non renseigné' 
  		WHERE 0 NOT IN (SELECT oid FROM base.t_territoires_sante);
  		
  		INSERT INTO base.t_codes_geographiques_pmsi(oid, code, texte, texte_court, population, departement_id, territoire_sante_id)
  		SELECT 0, chr(127) || '*****', 'Non renseigné', 'Non renseigné', 0, 0, 0 
  		WHERE 0 NOT IN (SELECT oid FROM base.t_codes_geographiques_pmsi WHERE oid = 0);
  		
  		INSERT INTO base.t_codes_postaux(oid, code, texte, texte_court, secteur_geographique_id, population,
  			code_geographique_pmsi, longitude, latitude, departement_id, territoire_sante_id)
  		SELECT 0, chr(127) || '*****', 'Non renseigné', 'Non renseigné', 0, 0, '', 0, 0, 0, 0
  		WHERE 0 NOT IN (SELECT oid FROM base.t_codes_postaux);
  		
  		-- Médecins
  		INSERT INTO base.t_specialites_medecin (oid, code, texte) 
  		SELECT 0, chr(127) || '**', 'Non saisie'
  		WHERE 0 NOT IN (SELECT oid FROM base.t_specialites_medecin);
  		
  		INSERT INTO base.t_medecins (oid, code, nom, specialite_id) 
  		SELECT 0, chr(127) || '******', 'Non saisi', 0
  		WHERE 0 NOT IN (SELECT oid FROM base.t_medecins);
  		
  		-- GHM
  		INSERT INTO base.t_cmd(oid, code, texte, texte_court)
  		SELECT 0, chr(127) || '*', 'Hors PMSI', 'Hors PMSI' 
  		WHERE 0 NOT IN (SELECT oid FROM base.t_cmd);
  		
  		INSERT INTO base.t_poles_oap(oid, code, texte, texte_court)
  		SELECT 0, chr(127) || '**', 'Hors PMSI', 'Hors PMSI' 
  		WHERE 0 NOT IN (SELECT oid FROM base.t_poles_oap);
  		
  		INSERT INTO base.t_lignes_oap(oid, code, texte, pole_oap_id, mco)
  		SELECT 0, chr(127) || '****', 'Hors PMSI', 0, '*'
  		WHERE 0 NOT IN (SELECT oid FROM base.t_lignes_oap);
  		
  		INSERT INTO base.t_cas(oid, code, texte, texte_court)
  		SELECT 0, chr(127), 'Non renseigné', 'Non renseigné' 
  		WHERE 0 NOT IN (SELECT oid FROM base.t_cas);
  		
  		INSERT INTO base.t_ipa(oid, code, texte, texte_court, categorie_oid, categorie_code, categorie_texte, categorie_texte_court)
  		SELECT 0, chr(127) || '**', 'Non renseigné', 'Non renseigné', 0, chr(127), 'Non renseigné', 'Non renseigné' 
  		WHERE 0 NOT IN (SELECT oid FROM base.t_ipa);
  		
  		INSERT INTO base.t_ghm5 (oid, code, texte)
  		SELECT 0, chr(127) || '****', 'Hors PMSI' 
  		WHERE 0 NOT IN (SELECT oid FROM base.t_ghm5);
  		
  		INSERT INTO base.t_ghm (oid, code, texte, ghm3, mco, aso, severite, statut, cmd_id, groupe_activite_id, ghm5_id, ligne_oap_id, cas_id)
  		SELECT 0, chr(127) || '*****', 'Hors PMSI', '*', '*', '*', '*', 'I', 0, 0, 0, 0, (SELECT oid FROM base.t_cas WHERE code = '*')
  		WHERE 0 NOT IN (SELECT oid FROM base.t_ghm);
  		
  		INSERT INTO base.t_ghs (oid, code, texte, ghm_id, borne_basse, borne_haute, dms_nationale)
  		SELECT 0, 0, 'Sans GHS', 0, 0, 0, 0
  		WHERE 0 NOT IN (SELECT oid FROM base.t_ghs);
          
          -- GME
  		INSERT INTO base.t_gn (oid, code, texte)
  		SELECT 0, chr(127) || '****'::text, 'Non renseigné'::text
  		WHERE 0 NOT IN (SELECT oid FROM base.t_gn);
          
  		INSERT INTO base.t_cm (oid, code, texte)
  		SELECT 0, chr(127) || '**'::text, 'Non renseigné'::text
  		WHERE 0 NOT IN (SELECT oid FROM base.t_cm);
          
  		INSERT INTO base.t_rgme (oid, code, texte)
  		SELECT 0, chr(127) || '*****'::text, 'Non renseigné'::text
  		WHERE 0 NOT IN (SELECT oid FROM base.t_rgme);
          
  		INSERT INTO base.t_gme (oid, code, texte, cm_id, gn_id, rgme_id)
  		SELECT 0, chr(127) || '******'::text, 'Non renseigné'::text, 0, 0, 0
  		WHERE 0 NOT IN (SELECT oid FROM base.t_gme);
  		
  		-- Actes
  		INSERT INTO base.t_actes (oid, code, texte, texte_court)
  		SELECT 0, '*******', 'Non renseigné', 'Non renseigné'
  		WHERE 0 NOT IN (SELECT oid FROM base.t_actes);
  		
  		-- Regroupements CCAM
  		INSERT INTO base.t_ccam_regroupements (oid)
  		SELECT 0
  		WHERE 0 NOT IN (SELECT oid FROM base.t_ccam_regroupements);
  		
  		-- Modes de traitement
  		INSERT INTO base.t_modes_traitement (oid, code, code_original, texte, texte_court)
  		SELECT 0, '**', '', 'Non renseigné', 'Non renseigné'
  		WHERE 0 NOT IN (SELECT oid FROM base.t_modes_traitement);
  		
  		
  		
  		
  		-- Activités
  		INSERT INTO activite.t_activites(oid, code_original, texte, texte_court, code, etage_par_defaut_id, etage_force_id)
  		SELECT 0, '0', 'Non renseigné', 'Non renseigné', '', 0, 0
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_activites);
  		
  		-- Etages
  		INSERT INTO activite.t_etages(oid, code_original, texte, texte_court, nb_lits, code, 
  			section_id, section_code, section_texte)
  		SELECT 0, '0', 'Non renseigné', 'Non renseigné', 0, '', 
  			array[0::bigint, 0::bigint, 0::bigint],
  			array[chr(127) || '***'::text, chr(127) || '***'::text, chr(127) || '***'::text],
  			array['Autres'::text, 'Autres'::text, 'Autres'::text]
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_etages);
  		
  		-- Lieux
  		INSERT INTO activite.t_lieux(oid)
  		SELECT 0
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_lieux);
  		
  		-- Lits
  		INSERT INTO activite.t_lits (oid, texte, texte_court, etage_id, code, chambre_particuliere)
  		SELECT 0, 'Non renseigné', 'Non renseigné', 0, '*', 'N' 
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_lits);
  		
  		-- Médecins administratifs
        INSERT INTO activite.t_medecins_administratifs (oid, code, code_original, nom, prenom, specialite_id, medecin_id, est_medecin_salarie)
        SELECT 0, '****', '0', 'Non renseigné', '', 0, 0, 0
        WHERE 0 NOT IN (SELECT oid FROM activite.t_medecins_administratifs);
  		
  		-- Places
  		INSERT INTO activite.t_places_parents (oid, parent_code, parent_texte, parent_type)
  		SELECT 0, '', 'Non renseigné', 'ETA'
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_places_parents);
  		
  		-- Prestations
  		INSERT INTO activite.t_prestations (oid, code_original, texte, texte_court, code)
  		SELECT 0, '0', 'Non renseigné', 'Non renseigné', '***'
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_prestations);
  		UPDATE activite.t_prestations 
  		SET code = '***' 
  		WHERE oid = 0 AND 
  			'***' NOT IN (SELECT code FROM activite.t_prestations WHERE code = '***');
  		
  		-- Protocoles
  		INSERT INTO activite.t_protocoles (oid, code_original, texte, texte_court, code)
  		SELECT 0, '0', 'Non renseigné', 'Non renseigné', '***'
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_protocoles);
  		
  		-- Rubriques facturation
  		INSERT INTO activite.t_rubriques_facturation (oid, code_original, texte, texte_court, code, actes_inclus_public)
  		SELECT 0, '0', 'Non renseigné', 'Non renseigné', '', '0'
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_rubriques_facturation);
  		
  		-- Services de facturation
  		INSERT INTO activite.t_services_facturation (oid, code_original, texte, texte_court, code, nb_lits)
  		SELECT 0, '0', 'Non renseigné', 'Non renseigné', '', 0
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_services_facturation);
  		
  		-- Spécialités médecin
  		INSERT INTO activite.t_specialites_medecin (oid, code_original, texte, texte_court, code)
  		SELECT 0, '0', 'Non saisie', 'Non saisie', ''
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_specialites_medecin);
  		
  		-- Tiers payant
  		INSERT INTO activite.t_tiers_payant (oid, code, code_original, texte, texte_court, type_tiers_payant)
  		SELECT 0, '', '', 'Non saisi', 'Non saisi', '0'
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_tiers_payant);
  		
  		-- Unités fonctionnelles
  		INSERT INTO activite.t_unites_fonctionnelles (oid, code, code_original, texte, texte_court)
  		SELECT 0, '', '0', 'Non renseignée', 'Non renseignée'
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_unites_fonctionnelles);
  		
  		-- Unités médicales
  		INSERT INTO activite.t_unites_medicales (oid, code, code_original, texte, texte_court)
  		SELECT 0, '', '0', 'Non renseignée', 'Non renseignée'
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_unites_medicales);
  
  
  		-- Formes d'activité
  		INSERT INTO activite.t_forme_activite (oid, code, texte, texte_court)
  		SELECT 0, '**', 'Non renseignée', 'Non renseignée'
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_forme_activite);
  		
  		-- Codes risque
  		INSERT INTO activite.t_risque (oid, code, texte)
  		SELECT 0, '**', 'Non renseigné'
  		WHERE 0 NOT IN (SELECT oid FROM activite.t_risque);
  
  		-- Motifs de non facturation
  		INSERT INTO activite.t_motif_non_facturation(oid, code_original, code, texte, texte_court) 
  		SELECT 0, 0, '', 'Non renseigné', 'Non renseigné' WHERE 0 NOT IN (SELECt oid FROM activite.t_motif_non_facturation)
  		;
                  INSERT INTO activite.t_motif_non_facturation(code_original, code, texte, texte_court) 
                  SELECT '*CTIZZZ', '*AUTRES', 'Autres', 'Autres' WHERE '*CTIZZZ' NOT IN (SELECT code_original FROM activite.t_motif_non_facturation)
                  ;
                  INSERT INTO activite.t_motif_non_facturation(code_original, code, texte, texte_court) 
                  SELECT '*CTING', '*NG', 'Non groupé', 'Non groupé' WHERE '*CTING' NOT IN (SELECT code_original FROM activite.t_motif_non_facturation)
                  ;
  
  		
  		-- Liste des tables pour installation après version
  		INSERT INTO activite.t_listes_tables (code, name, texte, select_cmd)
  		SELECT
  			'FORMEACTIVITE',
  			't_forme_activite',
  			'Formes d''activité',
  			'SELECT oid, code, texte, ''''::text as complement, 
  			CASE WHEN oid in (SELECT oid FROM activite.p_oids WHERE code_table IN (''forme_activite'')) THEN ''O'' ELSE ''N'' END as utilise 
  			FROM activite.t_forme_activite
  			ORDER BY 2'
  		WHERE 'FORMEACTIVITE' NOT IN (SELECT code FROM activite.t_listes_tables WHERE code IS NOT NULL)
  		;
  		WITH subview AS 
  			(SELECT
  				'FORMEACTIVITE'::text AS code,
  				't_forme_activite'::text AS name,
  				'Formes d''activité'::text AS texte,
  				'SELECT oid, code, texte, ''''::text as complement, 
  				CASE WHEN oid in (SELECT oid FROM activite.p_oids WHERE code_table IN (''forme_activite'')) THEN ''O'' ELSE ''N'' END as utilise 
  				FROM activite.t_forme_activite
  				ORDER BY 2'::text AS select_cmd
  			)
  		UPDATE activite.t_listes_tables SET
  			name = subview.name,
  			texte = subview.texte,
  			select_cmd = subview.select_cmd
  		FROM subview
  		WHERE t_listes_tables.code = subview.code AND
  			(
  				t_listes_tables.name IS DISTINCT FROM subview.name OR
  				t_listes_tables.texte IS DISTINCT FROM subview.texte OR
  				t_listes_tables.select_cmd IS DISTINCT FROM subview.select_cmd
  			)
  		;
  		
  		
  	RETURN 'OK';
  END;