return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  BEGIN
  
  	-- Réaffectation des types selon l'utilisation
  	UPDATE activite.t_tiers_payant
  	SET type_tiers_payant = '1'
  	FROM
  	(
  	SELECT t_tiers_payant.oid,t_tiers_payant.code,
  		SUM(CASE WHEN t_tiers_payant.oid = tiers_payant_1_id THEN 1 ELSE 0 END),
  		SUM(CASE WHEN t_tiers_payant.oid = tiers_payant_2_id AND tiers_payant_1_id <> tiers_payant_2_id THEN 1 ELSE 0 END)
  	FROM activite.t_tiers_payant
  	JOIN activite.p_factures ON
  		t_tiers_payant.oid = tiers_payant_1_id OR t_tiers_payant.oid = tiers_payant_2_id
  	WHERE type_tiers_payant = '2'
  	GROUP BY 1,2
  	HAVING SUM(CASE WHEN t_tiers_payant.oid = tiers_payant_1_id THEN 1 ELSE 0 END) > 0 AND
  		SUM(CASE WHEN t_tiers_payant.oid = tiers_payant_2_id AND tiers_payant_1_id <> tiers_payant_2_id THEN 1 ELSE 0 END) = 0
  	) subview
  	WHERE t_tiers_payant.oid = subview.oid
  	;
  
  
  
  	UPDATE activite.t_tiers_payant
  	SET type_tiers_payant = '1'
  	FROM
  	(
  	SELECT t_tiers_payant.oid,t_tiers_payant.code,
  		SUM(CASE WHEN t_tiers_payant.oid = tiers_payant_2_id THEN 1 ELSE 0 END),
  		SUM(CASE WHEN t_tiers_payant.oid = tiers_payant_1_id AND tiers_payant_1_id <> tiers_payant_2_id THEN 1 ELSE 0 END)
  	FROM activite.t_tiers_payant
  	JOIN activite.p_factures ON
  		t_tiers_payant.oid = tiers_payant_1_id OR t_tiers_payant.oid = tiers_payant_2_id
  	WHERE type_tiers_payant = '1'
  	GROUP BY 1,2
  	HAVING SUM(CASE WHEN t_tiers_payant.oid = tiers_payant_2_id THEN 1 ELSE 0 END) > 0 AND
  		SUM(CASE WHEN t_tiers_payant.oid = tiers_payant_1_id AND tiers_payant_1_id <> tiers_payant_2_id THEN 1 ELSE 0 END) = 0
  	) subview
  	WHERE t_tiers_payant.oid = subview.oid
  	;
  
  	-- Grands régimes
  	UPDATE activite.t_tiers_payant 
  	SET 
  		grand_regime_id = 0
  	WHERE grand_regime_id IS NULL;
  	UPDATE activite.t_tiers_payant
  	SET type_tiers_payant_texte = t_types_tiers_payant.texte_court,
  		type_tiers_payant_id = to_number('0' || type_tiers_payant,'00')
  	FROM activite.t_types_tiers_payant
  	WHERE type_tiers_payant = t_types_tiers_payant.code AND
  		(
  			type_tiers_payant_texte IS DISTINCT FROM t_types_tiers_payant.texte_court OR
  			type_tiers_payant_id IS DISTINCT FROM to_number('0' || type_tiers_payant,'00')
  		)
  		;
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '01' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%CPAM%' OR t_tiers_payant.texte ILIKE '%C.P.A.M%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '02' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%MSA%' OR t_tiers_payant.texte ILIKE '%M.S.A%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '02' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%GAMEX%' OR t_tiers_payant.texte ILIKE '%GROUPAMA%'  );
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '03' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%AMPI%' OR t_tiers_payant.texte ILIKE '%A.M.P.I%');
  	
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '03' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%AGF %' OR t_tiers_payant.texte ILIKE '%A.G.F.%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '03' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE 'RAM %' OR t_tiers_payant.texte ILIKE '%R.A.M.%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '04' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%SNCF%' OR t_tiers_payant.texte ILIKE '%S.N.C.F%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '05' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%RATP%' OR t_tiers_payant.texte ILIKE '%R.A.T.P%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '06' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%ENIM%' OR t_tiers_payant.texte ILIKE '%E.N.I.M%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '07' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%MINEUR%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '08' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%MILITAIRE%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '09' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%BANQUE%' AND t_tiers_payant.texte ILIKE '%FRANCE%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '10' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%NOTAIRE%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '14' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%ASSEMBLEE%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '15' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%SENAT%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '17' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%FRANCAIS%' AND t_tiers_payant.texte ILIKE '%ETRANGER%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '80' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%COMBATTANT%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '91' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%MGEN%' OR t_tiers_payant.texte ILIKE '%M.G.E.N%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '92' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%PTT%' OR t_tiers_payant.texte ILIKE '%P.T.T%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '93' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%MGP%' OR t_tiers_payant.texte ILIKE '%M.G.P%' OR t_tiers_payant.texte ILIKE '%POLICE%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '93' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%POLICE%' OR t_tiers_payant.texte ILIKE '%M.G.P%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '94' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%MFP%' OR t_tiers_payant.texte ILIKE '%M.F.P.%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '95' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%MNH%' OR t_tiers_payant.texte ILIKE '%M.N.H%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '96' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%AVIATION%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '99' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE 'MMI %' OR t_tiers_payant.texte ILIKE '%M.M.I%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '99' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%LMDE%');
  	UPDATE activite.t_tiers_payant
  	SET grand_regime_id = t_grands_regimes.oid
  	FROM base.t_grands_regimes
  	WHERE t_grands_regimes.code = '99' AND
  		(grand_regime_id IS NULL OR grand_regime_id = 0) AND
  		(t_tiers_payant.texte ILIKE '%VITTAVI%');
  	
  	
  	UPDATE activite.t_tiers_payant SET 
  			grand_regime_code = t_grands_regimes.code,
  			grand_regime_texte = t_grands_regimes.texte
  		FROM base.t_grands_regimes
  		WHERE grand_regime_id = t_grands_regimes.oid AND
  			(
  				grand_regime_code IS DISTINCT FROM t_grands_regimes.code OR
  				grand_regime_texte IS DISTINCT FROM t_grands_regimes.texte
  			);
  
  		
  		
  	RETURN 'OK';
  END;