return: text
lang: plpgsql
parameters:
  p0:
    type: text
    name: i_table
src: |
  DECLARE	
  	w_table TEXT;
  	result TEXT;
  BEGIN
  	-- i_table =
  	-- Table à mettre à jour, si '*NONE', ne met à jour que les codes et libellés
  	
  	w_table = i_table;
  	IF w_table = '' THEN
  		w_table = 'activite.p_factures_lignes_c';
  	END IF;
  	
  	IF w_table <> '*NONE' THEN
  		BEGIN
  			RAISE NOTICE '%' , 'Creation nouvelles rubriques';  
  			EXECUTE '
  			INSERT INTO activite.t_rubrique_facture_c
  				(rubrique_facturation_id,rubrique_comptabilisation_id, prestation_id, compte_produit_id)
  			SELECT
  				p_factures_lignes_c.rubrique_facturation_id, 
  				p_factures_lignes_c.rubrique_comptabilisation_id, 
  				p_factures_lignes_c.prestation_id, 
  				p_factures_lignes_c.compte_produit_id
  			FROM ' || w_table || ' p_factures_lignes_c
  			LEFT JOIN activite.t_rubrique_facture_c ON 
  				(p_factures_lignes_c.rubrique_facturation_id = t_rubrique_facture_c.rubrique_facturation_id AND
  				p_factures_lignes_c.rubrique_comptabilisation_id = t_rubrique_facture_c.rubrique_comptabilisation_id AND
  				p_factures_lignes_c.prestation_id = t_rubrique_facture_c.prestation_id AND
  				p_factures_lignes_c.compte_produit_id = t_rubrique_facture_c.compte_produit_id)
  			WHERE t_rubrique_facture_c.oid IS NULL
  			GROUP BY 1,2,3,4
  			ORDER BY 1,2,3,4';
  		EXCEPTION 
  				WHEN others THEN RAISE NOTICE 'Erreur %' ,'1'; 
  		END;
  		RAISE NOTICE '%' , 'Mise a jour lignes de factures'; 
  		
  		-- Création colonne
  		BEGIN
  			IF NOT EXISTS (SELECT CASE WHEN table_schema NOT LIKE 'pg_temp%' THEN table_schema||'.' ELSE '' END ||table_name 
  				FROM information_schema.columns
  				WHERE CASE WHEN table_schema NOT LIKE 'pg_temp%' THEN table_schema||'.' ELSE '' END ||table_name ILIKE w_table AND
  					column_name = 'rubrique_facture_id') THEN
  			
  				EXECUTE '
  				ALTER TABLE ' || w_table || ' ADD COLUMN rubrique_facture_id bigint;
  				ALTER TABLE ' || w_table || ' ALTER COLUMN rubrique_facture_id SET DEFAULT 0';
  			END IF;
  		EXCEPTION 
  			WHEN others THEN RAISE NOTICE 'Erreur %' ,'2'; 
  		END;
  		
  		-- Création index
  		-- (REINDEX Désactivé) EXECUTE ' REINDEX TABLE ' || w_table;
  		BEGIN
  			IF NOT EXISTS (SELECT CASE WHEN schemaname NOT LIKE 'pg_temp%' THEN schemaname||'.' ELSE '' END ||tablename from pg_indexes
  				WHERE (CASE WHEN schemaname NOT LIKE 'pg_temp%' THEN schemaname||'.' ELSE '' END ||tablename) ILIKE w_table AND
  					indexdef ILIKE '%rubrique_facturation_id%') THEN
  					EXECUTE '
  					CREATE INDEX i_' || w_table || '_rubrique_facturation_id
  					  ON ' || w_table || '
  					  USING btree
  					  (rubrique_facturation_id)';
  				
  			END IF;
  		EXCEPTION 
  			WHEN others THEN RAISE NOTICE 'Erreur %' ,'3'; 
  		END;
  		
  		-- Mise à jour nouvelle zone
  		BEGIN
  			EXECUTE '
  			UPDATE ' || w_table || ' p_factures_lignes_c
  			SET rubrique_facture_id = t_rubrique_facture_c.oid
  			FROM activite.t_rubrique_facture_c
  			WHERE
  				p_factures_lignes_c.rubrique_facturation_id = t_rubrique_facture_c.rubrique_facturation_id AND
  				p_factures_lignes_c.rubrique_comptabilisation_id = t_rubrique_facture_c.rubrique_comptabilisation_id AND
  				p_factures_lignes_c.prestation_id = t_rubrique_facture_c.prestation_id AND
  				p_factures_lignes_c.compte_produit_id = t_rubrique_facture_c.compte_produit_id AND
  				rubrique_facture_id IS DISTINCT FROM t_rubrique_facture_c.oid';
  		EXCEPTION 
  				WHEN others THEN RAISE NOTICE 'Erreur %' ,'4'; 
  		END;
  			
  		EXECUTE '
  		ANALYSE ' || w_table;
  	END IF;
  	RAISE NOTICE '%' , 'Mise a jour libelles'; 
  	BEGIN
  		UPDATE activite.t_rubrique_facture_c SET
  			rubrique_facturation_code = t_rubriques_facturation.code,
  			rubrique_facturation_texte = t_rubriques_facturation.texte_court,
  			rubrique_facturation_section_id = t_rubriques_facturation.section_id,
  			rubrique_facturation_section_code = t_rubriques_facturation.section_code,
  			rubrique_facturation_section_texte = t_rubriques_facturation.section_texte
  		FROM activite.t_rubriques_facturation 
  		WHERE rubrique_facturation_id = t_rubriques_facturation.oid AND
  			(
  			rubrique_facturation_code::text IS DISTINCT FROM t_rubriques_facturation.code::text OR
  			rubrique_facturation_texte::text IS DISTINCT FROM t_rubriques_facturation.texte_court::text OR
  			rubrique_facturation_section_id::bigint[] IS DISTINCT FROM t_rubriques_facturation.section_id::bigint[]  OR
  			rubrique_facturation_section_code::text[] IS DISTINCT FROM t_rubriques_facturation.section_code::text[] OR
  			rubrique_facturation_section_texte::text[] IS DISTINCT FROM t_rubriques_facturation.section_texte::text[]
  			);
  	EXCEPTION 
  			WHEN others THEN RAISE NOTICE 'Erreur %' ,'5'; 
  	END;
  	BEGIN
  		UPDATE activite.t_rubrique_facture_c SET
  			rubrique_comptabilisation_code = t_rubriques_facturation.code,
  			rubrique_comptabilisation_texte = t_rubriques_facturation.texte_court,
  			rubrique_comptabilisation_section_id = t_rubriques_facturation.section_id,
  			rubrique_comptabilisation_section_code = t_rubriques_facturation.section_code,
  			rubrique_comptabilisation_section_texte = t_rubriques_facturation.section_texte
  		FROM activite.t_rubriques_facturation 
  		WHERE rubrique_comptabilisation_id = t_rubriques_facturation.oid AND
  			(
  			rubrique_comptabilisation_code::text IS DISTINCT FROM t_rubriques_facturation.code::text OR
  			rubrique_comptabilisation_texte::text IS DISTINCT FROM t_rubriques_facturation.texte_court::text OR
  			rubrique_comptabilisation_section_id::bigint[] IS DISTINCT FROM t_rubriques_facturation.section_id::bigint[] OR
  			rubrique_comptabilisation_section_code::text[] IS DISTINCT FROM t_rubriques_facturation.section_code::text[] OR
  			rubrique_comptabilisation_section_texte::text[] IS DISTINCT FROM t_rubriques_facturation.section_texte::text[]
  			);
  	EXCEPTION 
  			WHEN others THEN RAISE NOTICE 'Erreur %' ,'6'; 
  	END;
  	BEGIN		
  		UPDATE activite.t_rubrique_facture_c SET
  			prestation_code = t_prestations.code,
  			prestation_texte = t_prestations.texte_court,
  			prestation_section_id = t_prestations.section_id,
  			prestation_section_code = t_prestations.section_code,
  			prestation_section_texte = t_prestations.section_texte
  		FROM activite.t_prestations
  		WHERE prestation_id = t_prestations.oid AND
  			(
  			prestation_code::text IS DISTINCT FROM t_prestations.code::text OR
  			prestation_texte::text IS DISTINCT FROM t_prestations.texte_court::text OR
  			prestation_section_id::bigint[] IS DISTINCT FROM t_prestations.section_id::bigint[] OR
  			prestation_section_code::text[] IS DISTINCT FROM t_prestations.section_code::text[] OR
  			prestation_section_texte::text[] IS DISTINCT FROM t_prestations.section_texte::text[]
  			);
  	EXCEPTION 
  			WHEN others THEN RAISE NOTICE 'Erreur %' ,'7'; 
  	END;
  	BEGIN
  		UPDATE activite.t_rubrique_facture_c SET
  			compte_produit_code = t_compte.code,
  			compte_produit_texte = t_compte.texte_court,
  			compte_produit_section_id = t_compte.section_id,
  			compte_produit_section_code = t_compte.section_code,
  			compte_produit_section_texte = t_compte.section_texte
  		FROM activite.t_compte
  		WHERE compte_produit_id = t_compte.oid AND
  			(
  			compte_produit_code::text IS DISTINCT FROM t_compte.code::text OR
  			compte_produit_texte::text IS DISTINCT FROM t_compte.texte_court::text OR
  			compte_produit_section_id::bigint[] IS DISTINCT FROM t_compte.section_id::bigint[] OR
  			compte_produit_section_code::text[] IS DISTINCT FROM t_compte.section_code::text[] OR
  			compte_produit_section_texte::text[] IS DISTINCT FROM t_compte.section_texte::text[]
  			);
  	EXCEPTION 
  			WHEN others THEN RAISE NOTICE 'Erreur %' ,'8'; 
  	END;
  		
  	RETURN 'OK';
  END;