return: text
lang: plpgsql
src: |
  DECLARE	
  	_module_pmsimco TEXT;
  	_module_activite TEXT;
  	_module_pmsissr TEXT;
  	result TEXT;
  BEGIN
  	IF EXISTS (SELECT * FROM pg_tables WHERE schemaname = 'pmsi' AND tablename = 'p_rss') THEN
  		_module_pmsimco = '1';
  	ELSE
  		_module_pmsimco = '0';
  	END IF;
  	
  	IF EXISTS (SELECT * FROM pg_tables WHERE schemaname = 'activite' AND tablename = 'p_sejours') THEN
  		_module_activite = '1';
  	ELSE
  		_module_activite = '0';
  	END IF;
  	IF EXISTS (SELECT * FROM pg_tables WHERE schemaname = 'pmsissr' AND tablename = 'p_ssr') THEN
  		_module_pmsissr = '1';
  	ELSE
  		_module_pmsissr = '0';
  	END IF;
  
  	PERFORM base.cti_execute('INSERT INTO eco.t_divers(code,texte,valeur,description)
  	    VALUES (''UPDATE_PMSI'',''Mise à jour des LPP et UCD dans le module Pmsi Mco à partir du module Gestion Economique'',''0'',''0: Non 1: Oui'')',1)
  	   WHERE (SELECT count(*) FROM eco.t_divers WHERE code = 'UPDATE_PMSI') = 0
  	;
  
  
  					DROP TABLE IF EXISTS w_mvt_sejours;
  					CREATE TABLE w_mvt_sejours AS
  					SELECT 
  						sejour_id
  						,rss_id
  						,COALESCE(t_lpp_c.code,t_ucd_c.code,'') AS code
  						,SUM(sortie_montant-entree_montant) AS sortie_montant
  					FROM eco.p_mouvements_articles
  						JOIN eco.p_sejours ON mouvement_id = p_mouvements_articles.oid
  						LEFT JOIN eco.t_lpp_c ON lpp_id = t_lpp_c.oid
  						LEFT JOIN eco.t_ucd_c ON ucd_id = t_ucd_c.oid
  					WHERE (lpp_id != 0 OR ucd_id != 0)
  						AND type_mouvement_id IN (SELECT to_id from eco.t_classes JOIN eco.t_classes_sections ON t_classes.oid = classe_id
  						JOIN eco.t_classes_sections_elements ON section_id = t_classes_sections.oid
  						WHERE t_classes.code = 'CTI_TYPMVT' AND t_classes_sections.code = '01')
  					GROUP BY 1,2,3
  					;
  
  					CREATE INDEX w_mvt_sejours_i1 on w_mvt_sejours USING btree (sejour_id);
  					CREATE INDEX w_mvt_sejours_i2 on w_mvt_sejours USING btree (rss_id);
  					CREATE INDEX w_mvt_sejours_i3 on w_mvt_sejours USING btree (code);
  	-- Mise à jour des ucd et lpp
  
  			DROP TABLE IF EXISTS w_fac;
  					CREATE TEMP TABLE w_fac AS 
  					SELECT 
  						0::bigint AS mco_rss_id,
  						0::bigint AS act_sejour_id,
  						''::text AS fac_code,
  						''::text AS fac_code_orig,
  						0 AS from_eco,
  						0 AS nombre,
  						0 AS montant_facture
  					;
  
  		
  					DROP TABLE IF EXISTS w_ghs;
  					CREATE TEMP TABLE w_ghs AS 
  					SELECT 
  						0::bigint AS mco_rss_id,
  						0::bigint AS act_sejour_id,
  						0::int AS encours,
  						0::numeric AS ca_ghs_theorique
  					;
  	IF _module_pmsimco = '1' THEN		
  								
  						INSERT INTO w_fac
  						SELECT 
  							rss_id AS mco_rss_id,
  							0::bigint AS act_sejour_id,
  							lpp_code AS fac_code,
  							'',
  							COALESCE(from_eco,0),
  							sum(nombre) AS nombre,
  							sum(montant_facture) AS montant_facture 
  						FROM pmsi.v_rss_lpp_1
  						WHERE rss_id = ANY(ARRAY(SELECT rss_id FROM w_mvt_sejours))
  						GROUP BY 1,2,3,4,5
  						;
  
  						INSERT INTO w_fac
  						SELECT 
  							rss_id AS mco_rss_id,
  							0::bigint AS act_sejour_id,
  							ucd_code AS fac_code,
  							'',
  							COALESCE(from_eco,0),
  							sum(nombre) AS nombre,
  							sum(montant_facture) AS montant_facture 
  						FROM pmsi.v_rss_ucd_1
  						WHERE rss_id = ANY(ARRAY(SELECT rss_id FROM w_mvt_sejours))
  						GROUP BY 1,2,3,4,5
  						;
  
  						INSERT INTO w_ghs
  						SELECT 
  							v_rss_1.rss_id AS mco_rss_id,
  							0,
  							0::bigint AS act_sejour_id,
  							SUM(ca_ghs_theorique) AS ca_ghs_theorique
  						FROM pmsi.v_rss_1
  						WHERE ca_ghs_theorique != 0 
  							AND rss_id = ANY(ARRAY(SELECT rss_id FROM w_mvt_sejours))
  						GROUP BY 1,2,3
  						;
  END IF;
  						
  IF _module_activite = '1' THEN	
  						INSERT INTO w_fac
  						SELECT
  							p_sejour_pmsi.rss_id,
  							p_sejours.oid AS act_sejour_id,
  							CASE WHEN lpp_id != 0 THEN t_lpp.code 
  							 WHEN ucd_id != 0 THEN t_ucd.code 
  							 WHEN t_prestations.code IN ('PME','PII') THEN 'LPP' || (montant_facture+montant_encours)::text
  							 ELSE 'UCD' || (montant_facture+montant_encours)::text END,
  							'',
  							0 AS from_eco,
  							sum(nb_prestation),
  							sum(montant_facture + montant_encours)
  						FROM 
  							activite.v_factures_lignes_c_5
  							JOIN activite.p_sejours ON v_factures_lignes_c_5.no_sejour = p_sejours.no_sejour
  							JOIN activite.p_sejour_pmsi ON v_factures_lignes_c_5.no_sejour = p_sejour_pmsi.no_sejour
  							LEFT JOIN base.t_ucd on ucd_id = t_ucd.oid
  							LEFT JOIN base.t_lpp on lpp_id = t_lpp.oid
  							JOIN activite.t_prestations ON prestation_id = t_prestations.oid AND t_prestations.code IN ('PII','PME','LPP','PH8','PHX','UCD')
  						WHERE
  							p_sejours.oid = ANY(ARRAY(SELECT sejour_id FROM w_mvt_sejours)) AND
  							(p_sejour_pmsi.rss_id = 0 OR (p_sejour_pmsi.rss_id,COALESCE(t_ucd.code,t_lpp.code)) NOT IN (SELECT mco_rss_id, fac_code FROM w_fac))
  						GROUP BY 1,2,3,4,5						
  ;
  
  					INSERT INTO w_ghs
  						SELECT
  							p_sejour_pmsi.rss_id,
  							p_sejours.oid AS act_sejour_id,
  							CASE WHEN montant_encours != 0 THEN 1 ELSE 0 END as encours,
  							sum(montant_facture + montant_encours) as ca_ghs_theorique
  						FROM 
  							activite.v_factures_lignes_c_1
  							JOIN activite.p_sejours ON v_factures_lignes_c_1.no_sejour = p_sejours.no_sejour
  							LEFT JOIN activite.p_sejour_pmsi ON v_factures_lignes_c_1.no_sejour = p_sejour_pmsi.no_sejour
  						WHERE
  							prestation_code = 'GHS' AND
  							p_sejours.oid = ANY(ARRAY(SELECT sejour_id FROM w_mvt_sejours)) AND
  							(p_sejour_pmsi.rss_id = 0 OR p_sejour_pmsi.rss_id NOT IN (SELECT mco_rss_id FROM w_ghs))
  						GROUP BY 1,2,3
  					;
  
  						UPDATE w_ghs
  						SET encours = 1
  						FROM 
  							activite.v_factures_lignes_c_1
  							JOIN activite.p_sejours ON v_factures_lignes_c_1.no_sejour = p_sejours.no_sejour
  							LEFT JOIN activite.p_sejour_pmsi ON v_factures_lignes_c_1.no_sejour = p_sejour_pmsi.no_sejour
  						WHERE
  							prestation_code = 'GHS' AND
  							montant_encours != 0 AND
  							p_sejour_pmsi.rss_id = mco_rss_id
  							;
  					
  END IF;
  
  
  CREATE INDEX w_fac_i1 on w_fac USING btree (mco_rss_id);
  CREATE INDEX w_fac_i2 on w_fac USING btree (act_sejour_id);
  CREATE INDEX w_ghs_i1 on w_ghs USING btree (mco_rss_id);
  CREATE INDEX w_ghs_i2 on w_ghs USING btree (act_sejour_id);
  
  ANALYSE w_fac;
  ANALYSe w_ghs;
    
  					UPDATE w_fac
  					SET fac_code_orig = w_fac.fac_code
  						,fac_code = sub.code
  					FROM (
  						SELECT w_fac.*
  							,sub.*
  						FROM w_fac
  						JOIN (
  							SELECT sejour_id
  								,rss_id
  								,code
  								,sortie_montant
  							FROM w_mvt_sejours
  							LEFT JOIN w_fac ON (
  									rss_id = mco_rss_id
  									AND rss_id != 0
  									OR sejour_id = act_sejour_id
  									AND sejour_id != 0
  									)
  								AND code = fac_code
  							WHERE 
  								COALESCE(from_eco,1) != 0
  							) sub ON (
  								rss_id = mco_rss_id
  								AND rss_id != 0
  								OR sejour_id = act_sejour_id
  								AND sejour_id != 0
  								)
  						LEFT JOIN w_mvt_sejours ON (
  								w_mvt_sejours.rss_id = mco_rss_id
  								AND w_mvt_sejours.rss_id != 0
  								OR w_mvt_sejours.sejour_id = act_sejour_id
  								AND w_mvt_sejours.sejour_id != 0
  								)
  							AND w_mvt_sejours.code = fac_code
  						WHERE w_mvt_sejours.code IS NULL
  							AND base.cti_division(abs(sub.sortie_montant - montant_facture), sub.sortie_montant) < 0.01
  						) sub
  					WHERE (
  							sub.mco_rss_id = w_fac.mco_rss_id
  							AND sub.act_sejour_id = w_fac.act_sejour_id
  							AND sub.fac_code = w_fac.fac_code
  							);
  
  
  						UPDATE eco.p_sejours
  						SET ca_ghs = ca_ghs_theorique,
  						encours = w_ghs.encours
  						FROM w_ghs
  						WHERE p_sejours.sejour_id = w_ghs.act_sejour_id AND w_ghs.act_sejour_id != 0
  						
  						;	
  
  						UPDATE eco.p_sejours
  						SET ca_ghs = ca_ghs_theorique,
  						encours = w_ghs.encours
  						FROM w_ghs
  						WHERE p_sejours.rss_id = w_ghs.mco_rss_id AND w_ghs.mco_rss_id != 0 AND
  							ca_ghs = 0
  						;
  
  UPDATE w_fac
  					SET fac_code_orig = w_fac.fac_code
  						,fac_code = sub.code
  					FROM (
  						SELECT w_fac.*
  							,sub.*
  						FROM w_fac
  						JOIN (
  							SELECT sejour_id
  								,rss_id
  								,code
  								,sortie_montant
  							FROM w_mvt_sejours
  							LEFT JOIN w_fac ON (
  									rss_id = mco_rss_id
  									AND rss_id != 0
  									OR sejour_id = act_sejour_id
  									AND sejour_id != 0
  									)
  								AND code = fac_code
  							WHERE 
  								COALESCE(from_eco,1) != 0
  							) sub ON (
  								rss_id = mco_rss_id
  								AND rss_id != 0
  								OR sejour_id = act_sejour_id
  								AND sejour_id != 0
  								)
  						LEFT JOIN w_mvt_sejours ON (
  								w_mvt_sejours.rss_id = mco_rss_id
  								AND w_mvt_sejours.rss_id != 0
  								OR w_mvt_sejours.sejour_id = act_sejour_id
  								AND w_mvt_sejours.sejour_id != 0
  								)
  							AND w_mvt_sejours.code = fac_code
  						WHERE w_mvt_sejours.code IS NULL
  							AND base.cti_division(abs(sub.sortie_montant - montant_facture), sub.sortie_montant) < 0.1
  						) sub
  					WHERE (
  							sub.mco_rss_id = w_fac.mco_rss_id
  							AND sub.act_sejour_id = w_fac.act_sejour_id
  							AND sub.fac_code = w_fac.fac_code
  							);
  
  						UPDATE eco.p_sejours
  						SET ca_ghs = ca_ghs_theorique,
  						encours = w_ghs.encours
  						FROM w_ghs
  						WHERE p_sejours.sejour_id = w_ghs.act_sejour_id AND w_ghs.act_sejour_id != 0
  						;	
  
  						UPDATE eco.p_sejours
  						SET ca_ghs = ca_ghs_theorique,
  						encours = w_ghs.encours
  						FROM w_ghs
  						WHERE p_sejours.rss_id = w_ghs.mco_rss_id AND w_ghs.mco_rss_id != 0 AND ca_ghs = 0
  						;
  	DROP TABLE IF EXISTS w_fac_sej;
  	CREATE TEMP TABLE w_fac_sej AS 
  	SELECT w_fac.*, mouvement_id
  	FROM w_fac JOIN eco.p_sejours ON (
    			rss_id != 0 AND mco_rss_id != 0 AND rss_id = mco_rss_id)
    	;
  
  	INSERT INTO w_fac_sej
  	SELECT w_fac.*, p_sejours.mouvement_id
  	FROM w_fac JOIN eco.p_sejours ON (
    			sejour_id != 0 
    								AND act_sejour_id != 0 
    								AND sejour_id = act_sejour_id)
    	LEFT JOIN (SELECT DISTINCT mouvement_id FROM w_fac_sej) sub ON sub.mouvement_id = p_sejours.mouvement_id
    	WHERE sub.mouvement_id IS NULL
    	;
  
  	
  	UPDATE eco.p_mouvements_articles
    				SET quantite_ligne_facture = nombre,
    				montant_ligne_facture = montant_facture
    	FROM
    					w_fac_sej
    					LEFT JOIN eco.t_lpp_c ON w_fac_sej.fac_code = t_lpp_c.code
    					LEFT JOIN eco.t_ucd_c ON w_fac_sej.fac_code = t_ucd_c.code
    				WHERE
    					mouvement_id = p_mouvements_articles.oid 
    					AND (lpp_id = t_lpp_c.oid
    						OR ucd_id = t_ucd_c.oid)
    				;
  				
  					
  	IF _module_pmsimco = '1' AND (SELECT count(*) FROM eco.t_divers WHERE code = 'UPDATE_PMSI' AND valeur = '1') = 1 THEN 
  		BEGIN
  		-- maj et ajout des ucd
  			DELETE FROM pmsi.p_rss_ucd
  			WHERE from_eco = 1
  			;
  
  			UPDATE pmsi.p_rss_ucd
  			SET
  				montant_consommation = sortie_montant
  			FROM eco.p_sejours
  				JOIN pmsi.p_rss  ON p_rss.oid = p_sejours.rss_id
  				JOIN eco.p_mouvements_articles ON p_mouvements_articles.oid = mouvement_id
  				JOIN eco.t_ucd_c ON t_ucd_c.oid = p_mouvements_articles.ucd_id AND p_mouvements_articles.ucd_id != 0
  				JOIN pmsi.t_ucd ON t_ucd.code = t_ucd_c.code
  				JOIN pmsi.t_ucd_tarifs ON t_ucd.oid = t_ucd_tarifs.ucd_id AND date BETWEEN t_ucd_tarifs.date_debut AND t_ucd_tarifs.date_fin
  			WHERE p_rss_ucd.rss_id = p_rss.oid AND t_ucd.oid = p_rss_ucd.ucd_id AND sortie_quantite = nombre
  			;
  
  			INSERT INTO pmsi.p_rss_ucd (
  				finess, 
  				no_rss, 
  				date_ucd, 
  				ucd_id, 
  				nombre, 
  				prix_unitaire_achat,
  				montant_consommation, 
  				rss_id, 
  				type_ucd_id, 
  				from_eco)
  			SELECT 
  				p_rss.finess, 
  				p_rss.no_rss, 
  				date, 
  				t_ucd.oid, 
  				sortie_quantite, 
  				COALESCE(prix_unitaire_ttc,prix_unitaire), 
  				sortie_montant, 
  				p_rss.oid,
  				CASE WHEN t_ucd_tarifs.ucd_id IS NULL OR top_taa = 0 THEN 0 ELSE 1 END ,
  				1
  			FROM eco.p_sejours
  				JOIN pmsi.p_rss  ON p_rss.oid = p_sejours.rss_id
  				JOIN eco.p_mouvements_articles ON p_mouvements_articles.oid = mouvement_id
  				JOIN eco.t_ucd_c ON t_ucd_c.oid = p_mouvements_articles.ucd_id AND p_mouvements_articles.ucd_id != 0
  				JOIN pmsi.t_ucd ON t_ucd.code = t_ucd_c.code
  				LEFT JOIN pmsi.t_ucd_tarifs ON t_ucd.oid = t_ucd_tarifs.ucd_id AND date BETWEEN t_ucd_tarifs.date_debut AND t_ucd_tarifs.date_fin
  				LEFT JOIN pmsi.p_rss_ucd ON p_rss_ucd.rss_id = p_rss.oid AND t_ucd.oid = p_rss_ucd.ucd_id
  			WHERE p_rss_ucd.ucd_id IS NULL AND type_mouvement_id IN 
  			(SELECT to_id FROM eco.t_classes JOIN eco.t_classes_sections on classe_id = t_classes.oid
  			JOIN eco.t_classes_sections_elements ON section_id = t_classes_sections.oid
  			WHERE t_classes.code = 'CTI_TYPMVT' AND t_classes_sections.code = '01')
  			;
  
  			-- maj et ajout des lpp
  			DELETE FROM pmsi.p_rss_lpp
  			WHERE from_eco = 1
  			;
  			
  			UPDATE pmsi.p_rss_lpp
  			SET
  				montant_consommation = sortie_montant
  			FROM eco.p_sejours
  				JOIN pmsi.p_rss  ON p_rss.oid = p_sejours.rss_id
  				JOIN eco.p_mouvements_articles ON p_mouvements_articles.oid = mouvement_id
  				JOIN eco.t_lpp_c ON t_lpp_c.oid = p_mouvements_articles.lpp_id AND p_mouvements_articles.lpp_id != 0
  				JOIN pmsi.t_lpp ON t_lpp.code = t_lpp_c.code
  				JOIN pmsi.t_lpp_tarifs ON t_lpp.oid = t_lpp_tarifs.lpp_id AND date BETWEEN t_lpp_tarifs.date_debut AND t_lpp_tarifs.date_fin
  			WHERE p_rss_lpp.rss_id = p_rss.oid AND t_lpp.oid = p_rss_lpp.lpp_id AND sortie_quantite = nombre
  			;
  
  			INSERT INTO pmsi.p_rss_lpp (
  				finess, 
  				no_rss, 
  				date_lpp, 
  				lpp_id, 
  				nombre, 
  				prix_unitaire,
  				montant_consommation, 
  				rss_id, 
  				type_lpp_id, 
  				from_eco)
  			SELECT 
  				p_rss.finess, 
  				p_rss.no_rss, 
  				date, 
  				t_lpp.oid, 
  				sortie_quantite, 
  				COALESCE(t_lpp_tarifs.prix_unitaire, p_mouvements_articles.prix_unitaire), 
  				sortie_montant, 
  				p_rss.oid,
  				CASE WHEN annexe = 1 OR (annexe = 2 AND type_etablissement = 1) THEN 1 ELSE 0 END ,
  				1
  			FROM eco.p_sejours
  				JOIN pmsi.p_rss  ON p_rss.oid = p_sejours.rss_id
  				JOIN base.t_finess ON finess = t_finess.code
  				JOIN eco.p_mouvements_articles ON p_mouvements_articles.oid = mouvement_id
  				JOIN eco.t_lpp_c ON t_lpp_c.oid = p_mouvements_articles.lpp_id AND p_mouvements_articles.lpp_id != 0
  				JOIN pmsi.t_lpp ON t_lpp.code = t_lpp_c.code
  				LEFT JOIN base.t_lpp_sus ON t_lpp.oid = t_lpp_sus.lpp_id AND date BETWEEN t_lpp_sus.date_debut AND t_lpp_sus.date_fin
  				LEFT JOIN pmsi.t_lpp_tarifs ON t_lpp.oid = t_lpp_tarifs.lpp_id AND date BETWEEN t_lpp_tarifs.date_debut AND t_lpp_tarifs.date_fin
  				LEFT JOIN pmsi.p_rss_lpp ON p_rss_lpp.rss_id = p_rss.oid AND t_lpp.oid = p_rss_lpp.lpp_id
  			WHERE p_rss_lpp.lpp_id IS NULL AND type_mouvement_id IN 
  			(SELECT to_id FROM eco.t_classes JOIN eco.t_classes_sections on classe_id = t_classes.oid
  			JOIN eco.t_classes_sections_elements ON section_id = t_classes_sections.oid
  			WHERE t_classes.code = 'CTI_TYPMVT' AND t_classes_sections.code = 'CO')
  			;
  		END;
  
  	END IF;
  	-- Mise à jour des prestation PII et PH8
  	IF _module_activite = '1' AND _module_pmsimco = '1' AND false THEN 
  		BEGIN
  		-- maj et ajout des lpp
  			DELETE FROM activite.p_factures_lignes_non_facturees_c
  			WHERE from_eco = 1
  			;
  
  			DROP TABLE IF EXISTS w_factures_lignes_non_facturees_c;
  			CREATE TEMP TABLE w_factures_lignes_non_facturees_c AS
  			SELECT
  				finess,
  				sejour_id,
  				sejour_act.no_sejour,
  				ghs_id,
  				date,
  				lieu_sortie_id,
  				t_prestations.code,
  				t_lpp_tarifs.prix_unitaire,
  				t_lpp_tarifs.prix_unitaire * sortie_quantite AS montant_non_facture,
  				sortie_quantite,
  				sejour_act.date_entree,
  				sejour_act.date_sortie
  			FROM eco.p_sejours
  				JOIN eco.p_mouvements_articles ON mouvement_id = p_mouvements_articles.oid
  				JOIN activite.p_sejours sejour_act ON p_sejours.sejour_id = sejour_act.oid
  				LEFT JOIN base.t_finess ON sejour_act.finess = t_finess.code
  				JOIN eco.t_lpp_c on lpp_id = t_lpp_c.oid
  				JOIN pmsi.t_lpp on t_lpp_c.code = t_lpp.code
  				JOIN pmsi.t_prestations on t_lpp.prestation_defaut_id = t_prestations.oid
  				JOIN base.t_lpp_sus on t_lpp_sus.lpp_id = t_lpp.oid AND date BETWEEN t_lpp_sus.date_debut AND t_lpp_sus.date_fin
  				JOIN pmsi.t_lpp_tarifs on t_lpp_tarifs.lpp_id = t_lpp.oid AND date BETWEEN t_lpp_tarifs.date_debut AND t_lpp_tarifs.date_fin
  			WHERE p_mouvements_articles.lpp_id != 0 
  				AND (annexe = 1 OR annexe = 2 AND COALESCE(type_etablissement,'1') = '1')
  				AND type_sejour = '1'
  			;
  
  			DELETE FROM w_factures_lignes_non_facturees_c
  			USING activite.p_factures 
  				JOIN activite.p_factures_lignes_c ON facture_id = p_factures.oid
  				JOIN activite.t_prestations ON p_factures_lignes_c.prestation_id = t_prestations.oid
  			WHERE 
  				p_factures.sejour_id = w_factures_lignes_non_facturees_c.sejour_id AND
  				(t_prestations.code = w_factures_lignes_non_facturees_c.code OR code_cloture = 'H')
  			;
  
  			INSERT INTO activite.p_factures (
  				sejour_id,
  				finess, 
  				no_sejour, 
  				no_facture, 
  				no_facture_reference, 
  				type_facture, 
  				code_facture, 
  				date_facture, 
  				ghs_id,
  				date_debut, 
  				date_fin,
  				montant_encours_c)
  			SELECT 
  				w_factures_lignes_non_facturees_c.sejour_id,
  				w_factures_lignes_non_facturees_c.finess, 
  				w_factures_lignes_non_facturees_c.no_sejour, 
  				'E' || w_factures_lignes_non_facturees_c.no_sejour, 
  				'E' || w_factures_lignes_non_facturees_c.no_sejour, 
  				'E', 
  				'0', 
  				'20991231'::date,
  				w_factures_lignes_non_facturees_c.ghs_id,
  				date_entree AS date_debut, 
  				date_sortie AS date_fin,
  				sum(prix_unitaire * sortie_quantite)
  			FROM w_factures_lignes_non_facturees_c
  				LEFT JOIN activite.p_factures ON p_factures.sejour_id = w_factures_lignes_non_facturees_c.sejour_id AND montant_encours_c != 0
  			WHERE p_factures.no_facture IS NULL
  				GROUP BY 1,2,3,4,5,6,7,8,9,10,11;
  				
  				
  			INSERT INTO activite.p_factures_lignes_non_facturees_c(
  				no_facture, 
  				date_debut, 
  				date_fin, 
  				nb_rubrique, 
  				coefficient, 
  				rubrique_facturation_id, 
  				prestation_id, 
  				prix_unitaire, 
  				lieu_id,
  				from_eco)
  			SELECT
  				no_facture,
  				date,
  				date,
  				sortie_quantite,
  				1,
  				0,
  				t_prestations.oid,
  				prix_unitaire,
  				lieu_sortie_id,
  				1
  			FROM w_factures_lignes_non_facturees_c
  				JOIN activite.p_factures ON w_factures_lignes_non_facturees_c.sejour_id = p_factures.sejour_id 
  					AND montant_encours_c != 0
  				LEFT JOIN activite.t_prestations ON w_factures_lignes_non_facturees_c.code = t_prestations.code
  				--LEFT JOIN activite.t_rubriques_facturation ON w_factures_lignes_non_facturees_c.code = t_prestations.code
  			;
  
  			-- maj et ajout des ucd
  			DROP TABLE IF EXISTS w_factures_lignes_non_facturees_c;
  			CREATE TEMP TABLE w_factures_lignes_non_facturees_c AS
  			SELECT
  				finess,
  				sejour_id,
  				sejour_act.no_sejour,
  				ghs_id,
  				date,
  				lieu_sortie_id,
  				t_prestations.code,
  				t_ucd_tarifs.prix_unitaire_ttc,
  				sortie_montant,
  				sortie_quantite,
  				sejour_act.date_entree,
  				sejour_act.date_sortie
  			FROM eco.p_sejours
  				JOIN eco.p_mouvements_articles ON mouvement_id = p_mouvements_articles.oid
  				JOIN activite.p_sejours sejour_act ON p_sejours.sejour_id = sejour_act.oid
  				LEFT JOIN base.t_finess ON sejour_act.finess = t_finess.code
  				JOIN eco.t_ucd_c on ucd_id = t_ucd_c.oid
  				JOIN pmsi.t_ucd on t_ucd_c.code = t_ucd.code
  				JOIN pmsi.t_prestations on t_ucd.prestation_defaut_id = t_prestations.oid
  				JOIN pmsi.t_ucd_tarifs on t_ucd_tarifs.ucd_id = t_ucd.oid AND date BETWEEN t_ucd_tarifs.date_debut AND t_ucd_tarifs.date_fin
  			WHERE p_mouvements_articles.ucd_id != 0
  				AND type_sejour = '1'
  			;
  
  			DELETE FROM w_factures_lignes_non_facturees_c
  			USING activite.p_factures 
  				JOIN activite.p_factures_lignes_c ON facture_id = p_factures.oid
  				JOIN activite.t_prestations ON p_factures_lignes_c.prestation_id = t_prestations.oid
  			WHERE 
  				p_factures.sejour_id = w_factures_lignes_non_facturees_c.sejour_id AND
  				(t_prestations.code = w_factures_lignes_non_facturees_c.code OR code_cloture = 'H')
  			;
  
  			INSERT INTO activite.p_factures (
  				sejour_id,
  				finess, 
  				no_sejour, 
  				no_facture, 
  				no_facture_reference, 
  				type_facture, 
  				code_facture, 
  				date_facture, 
  				ghs_id,
  				date_debut, 
  				date_fin,
  				montant_encours_c)
  			SELECT 
  				w_factures_lignes_non_facturees_c.sejour_id,
  				w_factures_lignes_non_facturees_c.finess, 
  				w_factures_lignes_non_facturees_c.no_sejour, 
  				'E' || w_factures_lignes_non_facturees_c.no_sejour, 
  				'E' || w_factures_lignes_non_facturees_c.no_sejour, 
  				'E', 
  				'0', 
  				'20991231'::date,
  				w_factures_lignes_non_facturees_c.ghs_id,
  				date_entree AS date_debut, 
  				date_sortie AS date_fin,
  				sum(prix_unitaire_ttc * sortie_quantite)
  			FROM w_factures_lignes_non_facturees_c
  				LEFT JOIN activite.p_factures ON p_factures.sejour_id = w_factures_lignes_non_facturees_c.sejour_id AND montant_encours_c != 0
  			WHERE p_factures.no_facture IS NULL
  				GROUP BY 1,2,3,4,5,6,7,8,9,10,11;
  				
  				
  			INSERT INTO activite.p_factures_lignes_non_facturees_c(
  				no_facture, 
  				date_debut, 
  				date_fin, 
  				nb_rubrique, 
  				coefficient, 
  				rubrique_facturation_id, 
  				prestation_id, 
  				prix_unitaire, 
  				lieu_id)
  			SELECT
  				no_facture,
  				date,
  				date,
  				prix_unitaire_ttc * sortie_quantite,
  				1,
  				0,
  				t_prestations.oid,
  				prix_unitaire_ttc,
  				lieu_sortie_id
  			FROM w_factures_lignes_non_facturees_c
  				JOIN activite.p_factures ON w_factures_lignes_non_facturees_c.sejour_id = p_factures.sejour_id 
  					AND montant_encours_c != 0
  				LEFT JOIN activite.t_prestations ON w_factures_lignes_non_facturees_c.code = t_prestations.code
  				--LEFT JOIN activite.t_rubriques_facturation ON w_factures_lignes_non_facturees_c.code = t_prestations.code
  			;
  		END;
  	END IF;
  
  	REINDEX TABLE eco.p_sejours;
  
  	RETURN 'OK';
  END;