return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  BEGIN
  
  	
  	-- Recopie listes de FINESS de activité vers base
  	IF NOT EXISTS (
  		SELECT t_listes.code
  		FROM base.t_listes
  		) AND
  		EXISTS 
  		(SELECT t_listes.code
  		FROM activite.t_listes
  			JOIN activite.t_listes_tables ON t_listes.table_id = t_listes_tables.oid
  			WHERE t_listes_tables.code = 'FINESS'
  		)
  	THEN
  		DROP TABLE IF EXISTS w_listes_finess;
  		CREATE TEMP TABLE w_listes_finess AS
  		SELECT t_listes.*
  		FROM activite.t_listes
  		JOIN activite.t_listes_tables ON t_listes.table_id = t_listes_tables.oid
  		WHERE t_listes_tables.code = 'FINESS' AND 
  			(SELECT count(*) FROM base.t_listes) = 0
  			;
  			
  		INSERT INTO base.t_listes(
  			chapitre, 
  			code, 
  			contenu_by_code, 
  			contenu_non_replicable_ailleurs, 
  			est_partagee_modification, 
  			exclude_code, 
  			include_code, 
  			is_cti, 
  			oid, 
  			show_omit, 
  			table_id, 
  			texte, 
  			utilisateur_createur)
  		SELECT 
  			w_listes_finess.chapitre, 
  			w_listes_finess.code, 
  			w_listes_finess.contenu_by_code, 
  			w_listes_finess.contenu_non_replicable_ailleurs, 
  			w_listes_finess.est_partagee_modification, 
  			w_listes_finess.exclude_code, 
  			w_listes_finess.include_code, 
  			w_listes_finess.is_cti, 
  			w_listes_finess.oid, 
  			w_listes_finess.show_omit, 
  			t_listes_tables.oid AS table_id, 
  			w_listes_finess.texte, 
  			w_listes_finess.utilisateur_createur
  		FROM w_listes_finess
  		JOIN base.t_listes_tables ON t_listes_tables.code = 'FINESS'
  		;
  
  		INSERT INTO base.t_listes_contenu(
  			liste_id, 
  			to_id)
  		SELECT liste_id,
  			to_id
  		FROM activite.t_listes_contenu
  		WHERE liste_id IN (SELECT oid FROM w_listes_finess)
  		;
  
  	END IF;
  	
  	-- Recopie classes de FINESS de activité vers base
  	IF NOT EXISTS (
  		SELECT t_classes.code
  		FROM base.t_classes
  		) AND
  		EXISTS 
  		(SELECT t_classes.code
  		FROM activite.t_classes
  			JOIN activite.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
  			WHERE t_listes_tables.code = 'FINESS'
  		)
  	THEN
  		DROP TABLE IF EXISTS w_classes_finess;
  		CREATE TEMP TABLE w_classes_finess AS
  		SELECT t_classes.*
  		FROM activite.t_classes
  		JOIN activite.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
  		WHERE t_listes_tables.code = 'FINESS' AND 
  			(SELECT count(*) FROM base.t_classes) = 0
  			;
  
  
  		INSERT INTO base.t_classes(
  			oid, 
  			sequence, 
  			code, 
  			contenu_by_code, 
  			contenu_non_replicable_ailleurs, 
  			is_cti, 
  			table_id, 
  			texte)
  		SELECT 
  			w_classes_finess.oid, 
  			w_classes_finess.sequence, 
  			w_classes_finess.code, 
  			w_classes_finess.contenu_by_code, 
  			w_classes_finess.contenu_non_replicable_ailleurs, 
  			w_classes_finess.is_cti, 
  			t_listes_tables.oid AS table_id, 
  			w_classes_finess.texte
  		FROM w_classes_finess
  		JOIN base.t_listes_tables ON t_listes_tables.code = 'FINESS'
  		;
  
  		INSERT INTO base.t_classes_sections(
  			oid, 
  			classe_id, 
  			code, 
  			condition, 
  			exclude_code, 
  			include_code, 
  			texte)
  		SELECT oid, 
  			classe_id, 
  			code, 
  			condition, 
  			exclude_code, 
  			include_code, 
  			texte
  		FROM activite.t_classes_sections
  		WHERE classe_id IN (SELECT oid FROM w_classes_finess)
  		;
  
  		INSERT INTO base.t_classes_sections_elements(section_id, to_id)
  		SELECT t_classes_sections_elements.section_id, t_classes_sections_elements.to_id
  		FROM activite.t_classes_sections_elements
  		JOIN activite.t_classes_sections ON section_id = t_classes_sections.oid
  		WHERE t_classes_sections.classe_id IN (SELECT oid FROM w_classes_finess)
  		;
  
  		PERFORM base.cti_update_classes('base', 't_finess', 'FINESS','');
  	END IF;
  	
  	EXECUTE 'UPDATE activite.t_finess_c SET 
  			code = t_finess.code,
  			texte = t_finess.texte,
  			texte_court = t_finess.texte_court,
  			type_etablissement = t_finess.type_etablissement,
  			secondaire = t_finess.secondaire,
  			section_id = t_finess.section_id,
  			section_code = t_finess.section_code,
  			section_texte = t_finess.section_texte
  		FROM base.t_finess
  		WHERE t_finess.oid = t_finess_c.oid AND
  			(
  				t_finess_c.code IS DISTINCT FROM t_finess.code OR
  				t_finess_c.texte IS DISTINCT FROM t_finess.texte OR
  				t_finess_c.texte_court IS DISTINCT FROM t_finess.texte_court OR
  				t_finess_c.type_etablissement IS DISTINCT FROM t_finess.type_etablissement OR
  				t_finess_c.secondaire IS DISTINCT FROM t_finess.secondaire OR
  				t_finess_c.section_id IS DISTINCT FROM t_finess.section_id OR
  				t_finess_c.section_code IS DISTINCT FROM t_finess.section_code OR
  				t_finess_c.section_texte IS DISTINCT FROM t_finess.section_texte
  			)';
  	EXECUTE 'INSERT INTO activite.t_finess_c(oid, code, texte, texte_court, type_etablissement, section_id, section_code, section_texte) 
  		SELECT 0, ''000000000'', ''Non renseigné'', ''Non renseigné'', ''0'', NULL, NULL, NULL
  		WHERE 0 != ALL(ARRAY(SELECT oid FROM activite.t_finess_c)::bigint[])';
  	EXECUTE 'INSERT INTO activite.t_finess_c(oid, code, texte, texte_court, type_etablissement, secondaire, section_id, section_code, section_texte) 
  		SELECT oid, code, texte, texte_court, type_etablissement, secondaire, section_id, section_code, section_texte
  		FROM  base.t_finess
  		WHERE t_finess.oid NOT IN (SELECT oid FROM activite.t_finess_c)
  		ORDER BY t_finess.oid;';
  		
  	RETURN 'OK';
  END;