return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  	_module_pmsimco TEXT;
  BEGIN
  
  		RAISE NOTICE '%' , 'Initialisation Selon compte';  
  
  		DROP TABLE IF EXISTS w_compte_article;
  		CREATE TEMP TABLE w_compte_article AS
  		SELECT 
  			t_compte.code AS compte_code, 
  			t_compte.texte AS compte_texte,
  			CASE
  			WHEN t_compte.texte LIKE '%DMI%' THEN 'DMI'
  			WHEN t_compte.texte ILIKE '%prothese%' THEN 'DMI'
  			WHEN t_compte.texte ILIKE '%prothèse%' THEN 'DMI'
  			WHEN t_compte.texte ILIKE '%dispo%' AND t_compte.texte ILIKE '%implant%' THEN 'DMI'
  			WHEN t_compte.texte LIKE '%DM %' THEN 'DM'
  			WHEN t_compte.texte LIKE '% DM%' THEN 'DM'
  			WHEN t_compte.texte ILIKE '%medicament%' THEN 'MED'
  			WHEN t_compte.texte ILIKE '%médicament%' THEN 'MED'
  			WHEN t_compte.texte ILIKE '%pharmacie%' THEN 'MED'
  			WHEN t_compte.texte ILIKE '%produit%' AND t_compte.texte ILIKE '%entretien%' THEN 'ECO'
  			WHEN t_compte.texte ILIKE '%sang%' THEN 'MED'
  			WHEN 1<>1 THEN ''
  			ELSE '' END AS type_article_cti_code
  		FROM eco.t_articles
  		JOIN eco.t_compte ON compte_id = t_compte.oid
  		WHERE compte_id <> 0 AND
  			(
  			t_compte.code LIKE '467%' OR
  			t_compte.code LIKE '60%'
  			) AND
  			(
  			t_articles.oid IN (SELECT article_id FROM eco.p_lignes_commandes) OR
  			t_articles.oid IN (SELECT article_id FROM eco.p_mouvements_articles)
  			)
  		GROUP BY 1,2,3
  		ORDER BY 1
  		;
  
  		DROP TABLE IF EXISTS w_type_article_cti_rule;
  		CREATE TEMP TABLE w_type_article_cti_rule AS
  		SELECT type_article_cti_code,
  			'Défaut CTI selon compte ' || t_type_article_cti.texte AS type_article_cti_rule_texte,
  			t_type_article_cti.oid AS type_article_cti_id, 
  			CASE type_article_cti_code
  			WHEN 'DMI' THEN 70001
  			WHEN 'DM'  THEN 70002
  			WHEN 'MED' THEN 70003
  			WHEN 'ECO' THEN 70004
  			END AS priorite,
  			array_to_string(base.cti_array_accum(compte_code),',') AS liste_compte
  		FROM w_compte_article
  		JOIN eco.t_type_article_cti ON t_type_article_cti.code = type_article_cti_code
  		WHERE type_article_cti_code <> ''
  		GROUP BY 1,2,3,4
  		;
  		INSERT INTO w_type_article_cti_rule
  		SELECT 
  			t_type_article_cti.code,
  			CASE WHEN t_type_article_cti.code <> 'ZZZ' THEN 'Défaut CTI selon racine de compte ' || t_type_article_cti.texte ELSE 'Défaut CTI Autres' END AS type_article_cti_rule_texte,
  			t_type_article_cti.oid,
  			CASE t_type_article_cti.code
  			WHEN 'ECO' THEN 70009
  			WHEN 'ZZZ'  THEN 99999
  			END AS priorite,
  			CASE t_type_article_cti.code
  			WHEN 'ECO' THEN '2%,61%,62%'
  			WHEN 'ZZZ'  THEN ''
  			END AS liste_compte
  		FROM eco.t_type_article_cti
  		WHERE code IN ('ECO','ZZZ')
  		;
  
  		INSERT INTO eco.t_type_article_cti_rule(
  			texte,
  			priorite,
  			type_article_cti_id,
  			liste_compte
  			)
  		SELECT
  			type_article_cti_rule_texte AS texte,
  			priorite,
  			type_article_cti_id,
  			liste_compte
  		FROM w_type_article_cti_rule
  		WHERE w_type_article_cti_rule.priorite NOT IN (SELECT t_type_article_cti_rule.priorite FROM eco.t_type_article_cti_rule)
  		;
  
  		UPDATE eco.t_type_article_cti_rule SET
  			texte = type_article_cti_rule_texte,
  			type_article_cti_id = w_type_article_cti_rule.type_article_cti_id,
  			liste_compte = w_type_article_cti_rule.liste_compte
  		FROM w_type_article_cti_rule
  		WHERE t_type_article_cti_rule.priorite = w_type_article_cti_rule.priorite AND
  			(
  			t_type_article_cti_rule.texte IS DISTINCT FROM type_article_cti_rule_texte OR
  			t_type_article_cti_rule.type_article_cti_id IS DISTINCT FROM w_type_article_cti_rule.type_article_cti_id OR
  			t_type_article_cti_rule.liste_compte IS DISTINCT FROM w_type_article_cti_rule.liste_compte
  			)
  		;
  		
  
  			
  		-- Traduction des règles en oid
  		RAISE NOTICE '%' , 'Application des regles. Traduction en oid'; 
  		DROP TABLE IF EXISTS w_type_article_cti_rule;
  		CREATE TEMP TABLE w_type_article_cti_rule AS
  		SELECT
  			t_type_article_cti_rule.oid,
  			type_article_cti_id,
  			
  			priorite,
  			
  			CASE WHEN liste_article <> '' THEN '1' ELSE '0' END AS a_liste_article,
  			string_to_array(translate(liste_article,'*,','% '),' ') AS liste_article,
  			NULL::bigint[] AS liste_article_id ,
  			
  			CASE WHEN liste_type_article <> '' THEN '1' ELSE '0' END AS a_liste_type_article,
  			string_to_array(translate(liste_type_article,'*,','% '),' ') AS liste_type_article,
  			NULL::bigint[] AS liste_type_article_id ,
              
  			CASE WHEN liste_famille <> '' THEN '1' ELSE '0' END AS a_liste_famille,
  			string_to_array(translate(liste_famille,'*,','% '),' ') AS liste_famille,
  			NULL::bigint[] AS liste_famille_id,
  
  			CASE WHEN liste_sous_famille <> '' THEN '1' ELSE '0' END AS a_liste_sous_famille,
  			string_to_array(translate(liste_sous_famille,'*,','% '),' ') AS liste_sous_famille,
  			NULL::bigint[] AS liste_sous_famille_id,
  
  			CASE WHEN liste_categorie <> '' THEN '1' ELSE '0' END AS a_liste_categorie,
  			string_to_array(translate(liste_categorie,'*,','% '),' ') AS liste_categorie,
  			NULL::bigint[] AS liste_categorie_id,
  
  			CASE WHEN liste_sous_categorie <> '' THEN '1' ELSE '0' END AS a_liste_sous_categorie,
  			string_to_array(translate(liste_sous_categorie,'*,','% '),' ') AS liste_sous_categorie,
  			NULL::bigint[] AS liste_sous_categorie_id,
  
  			CASE WHEN liste_compte <> '' THEN '1' ELSE '0' END AS a_liste_compte,
  			string_to_array(translate(liste_compte,'*,','% '),' ') AS liste_compte,
  			NULL::bigint[] AS liste_compte_id
               
              
  			
  		FROM eco.t_type_article_cti_rule
  		JOIN eco.t_type_article_cti ON type_article_cti_id = t_type_article_cti.oid 
  		ORDER BY priorite, type_article_cti_id;
  
  
  		
  		UPDATE w_type_article_cti_rule 
  		SET liste_article_id = (SELECT base.cti_group_array3(oid) FROM eco.t_articles WHERE oid <> 0 AND code LIKE ANY (liste_article))
  		WHERE a_liste_article = '1';	
  
  		UPDATE w_type_article_cti_rule 
  		SET liste_type_article_id = (SELECT base.cti_group_array3(oid) FROM eco.t_types_articles WHERE oid <> 0 AND code LIKE ANY (liste_type_article))
  		WHERE a_liste_type_article = '1';	
  
  		UPDATE w_type_article_cti_rule 
  		SET liste_famille_id = (SELECT base.cti_group_array3(oid) FROM eco.t_familles_articles WHERE oid <> 0 AND code LIKE ANY (liste_famille))
  		WHERE a_liste_famille = '1';
  
  		UPDATE w_type_article_cti_rule 
  		SET liste_sous_famille_id = (SELECT base.cti_group_array3(oid) FROM eco.t_sous_familles_articles WHERE oid <> 0 AND code LIKE ANY (liste_sous_famille))
  		WHERE a_liste_sous_famille = '1';
  
  		UPDATE w_type_article_cti_rule 
  		SET liste_categorie_id = (SELECT base.cti_group_array3(oid) FROM eco.t_categories_articles WHERE oid <> 0 AND code LIKE ANY (liste_categorie))
  		WHERE a_liste_categorie = '1';
  
  		UPDATE w_type_article_cti_rule 
  		SET liste_sous_categorie_id = (SELECT base.cti_group_array3(oid) FROM eco.t_sous_categories_articles WHERE oid <> 0 AND code LIKE ANY (liste_sous_categorie))
  		WHERE a_liste_sous_categorie = '1';
  
  		UPDATE w_type_article_cti_rule 
  		SET liste_compte_id = (SELECT base.cti_group_array3(oid) FROM eco.t_compte WHERE oid <> 0 AND code LIKE ANY (liste_compte))
  		WHERE a_liste_compte = '1';
  
  		
  		
  		RAISE NOTICE '%' , 'Application des regles.'; 
  		DROP TABLE IF EXISTS w_articles;
  		CREATE TEMP TABLE w_articles AS
  		SELECT t_articles.oid, 
  			COALESCE(MIN(priorite),999999999) AS type_article_cti_priorite,
  			COALESCE((MIN(ARRAY[priorite,w_type_article_cti_rule.type_article_cti_id]))[2],0) AS type_article_cti_id
  		FROM eco.t_articles
  		JOIN w_type_article_cti_rule ON 
  			(a_liste_article = '0' OR t_articles.oid = ANY (liste_article_id)) AND
  			(a_liste_type_article = '0' OR type_id = ANY (liste_type_article_id)) AND
  			(a_liste_famille = '0' OR famille_id = ANY (liste_famille_id)) AND
  			(a_liste_sous_famille = '0' OR sous_famille_id = ANY (liste_sous_famille_id)) AND
  			(a_liste_categorie = '0' OR categorie_id = ANY (liste_categorie_id)) AND
  			(a_liste_sous_categorie = '0' OR sous_categorie_id = ANY (liste_sous_categorie_id)) AND
  			(a_liste_compte = '0' OR compte_id = ANY (liste_compte_id))
  		GROUP BY 1
  		ORDER BY 2,1
  		;
  
  		ANALYSE w_articles
  		;
  		
  
  
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des type_article_ctis'; 
  
  		UPDATE eco.t_articles
  		SET type_article_cti_id = w_articles.type_article_cti_id
  		FROM w_articles
  		WHERE t_articles.oid = w_articles.oid AND
  			t_articles.type_article_cti_id IS DISTINCT FROM w_articles.type_article_cti_id
  		;
  
  		
  
  
  		ANALYSE eco.t_articles
  		;
  		
  		INSERT INTO eco.p_oids (code_table, oid) 
                  SELECT 'type_article_cti', type_article_cti_id 
                  FROM eco.t_articles
                  WHERE type_article_cti_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'type_article_cti')
                  GROUP BY 2;
  
  
  	RETURN 'OK';
  END;