<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	
	
	<NODE name="INIT" label="INITIALISATIONS">

		<NODE label="Identification des séjours">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_PERIODE;
				CREATE TEMP TABLE w_PERIODE AS 
				SELECT MAX(GREATEST(DTMOD,NODTPH)) AS now_chataigneraie
				FROM prod_chataigneraie_as400.NXXXFAC_NDOADM;
			
				DROP TABLE IF EXISTS w_NDOADM_SELECT;

				CREATE TEMP TABLE w_NDOADM_SELECT AS
				SELECT NDOADM.IDETAB,
					NDOADM.NONUMA,
					NDOADM.STID,
					NDOADM.DPERNU,
					to_char(NDOADM.NOCDPH,'FM00000') AS NOCDPH,
					NDOADM.NODTEN,
					NDOADM.NOHREN,
					NDOADM.NODTSO,
					NDOADM.NOHRSO,
					NDOADM.NODTPR,
					NDOADM.NOHRPR,
					NOSEXH,
					NDOADM.PERNUTTR,
					NDOADM.NOURGA,
					NDOADM.NOCAIS,
					NDOADM.NOCAIE,
					NDOADM.NOLSOR,
					NDOADM.NOCMUT,
					NDOADM.CATCHB,
					NDOADM.DPERAD,
					NDOADM.NOCHIL,
					NDOADM.NOFACT,
					NDOADM.LIEUS,
					NDOADM.SENU1,
					DIDSEJ.*,
					NDOADM.NOTAR::text || CASE WHEN NDOADM.NOFACT = 'N' THEN '-NF' ELSE '' END AS TARID,
					''::text AS mode_traitement_code,
					''::text AS dmt_code,
					''::text AS lit_code,
					0::bigint AS mode_traitement_id,
					0::bigint AS tiers_payant_1_id,
					0::bigint AS tiers_payant_2_id,
					0::bigint AS tiers_payant_22_id,
					0::bigint AS tiers_payant_0_id,
					0::bigint AS code_postal_id,
					0::bigint AS medecin_sejour_id
				FROM prod_chataigneraie_as400.NXXXFAC_NDOADM NDOADM
				JOIN prod_chataigneraie_as400.NXXXFIC_DIDSEJ DIDSEJ ON
					SENUMD = NONUMA
				WHERE 
					(NDOADM.STID <> 3 AND NODTSO >= '20120101' OR
					NDOADM.STID <> 3 AND NODTEN >= '20120101' OR
					NDOADM.STID <> 3 AND NODTSO = '00010101' OR
					NDOADM.STID <> 3 AND NODTSO IS NULL OR
					NONUMA IN (
						SELECT FACTURES.NONUMA 
						FROM prod_chataigneraie_as400.NXXXFAC_FACTURES FACTURES 
						WHERE FADAT >= '20120101') OR
					NONUMA IN (	
						SELECT FACTURES.NONUMA 
						FROM prod_chataigneraie_as400.NXXXFAC_FACTURES FACTURES 
						JOIN prod_chataigneraie_as400.XXXFICH_REGLI REGLI ON FACTURES.FAID = REGLI.ACNFAC
						WHERE (ACSDTF*100+ACADTF) >= 2012
					)
					); 

				CREATE INDEX w_NDOADM_SELECT_i1
					ON w_NDOADM_SELECT
					USING btree
					(NONUMA);
	
					
			]]></sqlcmd>
		</NODE>
					
		
	</NODE>

	<NODE name="SEJOUR" label="RECUPERATION DES SEJOURS">
	
		<NODE label="Patients">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_PATIENT;
				CREATE TEMP TABLE w_PATIENT AS
				SELECT 
					DUUNIQ, 
					to_char(DUUNIQ,'FM000000000') AS no_patient,
					MAX(DUNOMH) AS DUNOMH, 
					MAX(DUPNMH) AS DUPNMH, 
					MAX(DUNJFH) AS DUNJFH, 
					MAX(DUDTNA) AS DUDTNA,
					MAX(DUSEXH) AS DUSEXH
				FROM prod_chataigneraie_as400.NXXXFIC_DPATIENT
				WHERE DUUNIQ IN (SELECT DUUNIQ FROM w_NDOADM_SELECT)
				GROUP BY 1,2;
				
				CREATE INDEX w_PATIENT_i1
				  ON w_PATIENT 
				  USING btree
				  (DUUNIQ);

				CREATE INDEX w_PATIENT_i2
				  ON w_PATIENT 
				  USING btree
				  (no_patient);
				  
				UPDATE activite[PX].p_patients
				SET nom = DUNOMH, 
					nom_naissance = DUNJFH, 
					prenom = DUPNMH, 
					date_naissance = DUDTNA, 
					code_sexe = DUSEXH
				FROM w_PATIENT 
				WHERE p_patients.no_patient = w_PATIENT.no_patient
					AND (
						p_patients.nom IS DISTINCT FROM DUNOMH OR 
						p_patients.prenom IS DISTINCT FROM DUPNMH OR
						nom_naissance IS DISTINCT FROM DUNJFH OR
						p_patients.date_naissance IS DISTINCT FROM DUDTNA OR
						p_patients.code_sexe IS DISTINCT FROM DUSEXH
					);

				INSERT INTO activite[PX].p_patients(finess, no_patient, nom, prenom, nom_naissance, date_naissance, code_sexe)
				SELECT '', w_PATIENT.no_patient, DUNOMH, DUPNMH, DUNJFH, DUDTNA, DUSEXH
				FROM w_PATIENT 
					LEFT JOIN activite[PX].p_patients ON (w_PATIENT.no_patient = p_patients.no_patient)
				WHERE p_patients.no_patient IS NULL;
				
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Séjours">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_NDOADM_GHM;
				CREATE TEMP TABLE w_NDOADM_GHM AS
				SELECT NONUMA,
					COALESCE(MAX(CASE WHEN STDEV_SEJBEBE.BENU = 0 THEN CCAM4_GHS.ghs ELSE 0 END),0) AS GHS_code,
					MAX(COALESCE(CASE WHEN STDEV_SEJBEBE.BENU = 0 THEN t_ghs.oid ELSE 0 END,0)) AS ghs_id,
					COALESCE(MAX(CASE WHEN STDEV_SEJBEBE.BENU = 0 THEN CCAM4_GHM.code ELSE '' END),'') AS GHM_code,
					MAX(COALESCE(CASE WHEN STDEV_SEJBEBE.BENU = 0 THEN t_ghm.oid ELSE 0 END,0)) AS ghm_id,
					COALESCE(MAX(CASE WHEN STDEV_SEJBEBE.BENU > 0 THEN CCAM4_GHS.ghs ELSE 0 END),0) AS GHS_bebe_code,
					MAX(COALESCE(CASE WHEN STDEV_SEJBEBE.BENU > 0 THEN t_ghs.oid ELSE 0 END,0)) AS ghs_bebe_id,
					COALESCE(MAX(CASE WHEN STDEV_SEJBEBE.BENU > 0 THEN CCAM4_GHM.code ELSE '' END),'') AS GHM_bebe_code,
					MAX(COALESCE(CASE WHEN STDEV_SEJBEBE.BENU > 0 THEN t_ghm.oid ELSE 0 END,0)) AS ghm_bebe_id,
					CASE WHEN MAX(GHMOK) = 1 THEN date(COALESCE(MAX(GREATEST(DATEPRVAL,DATEDRVAL)),'20991231')) ELSE '20991231' END AS DATEDRVAL,
					COALESCE(MAX(to_char(MVTLIEU_DEB.MDEID,'FM9')),'8') AS MDEID,
					COALESCE(MAX(CASE WHEN MVTLIEU_DEB.PREID <> 0 THEN to_char(MVTLIEU_DEB.PREID,'FM0') ELSE '' END),'') AS PREID,
					COALESCE(MAX(to_char(MVTLIEU_FIN.MDSID,'FM9')),'8') AS MDSID,
					COALESCE(MAX(CASE WHEN MVTLIEU_DEB.DEEID <> 0 THEN to_char(MVTLIEU_DEB.DEEID,'FM0') ELSE '' END),'') AS DEEID
				FROM w_NDOADM_SELECT
				LEFT JOIN prod_chataigneraie_mysql.STDEV_SEJBEBE ON STDEV_SEJBEBE.SENUM = NONUMA AND STDEV_SEJBEBE.IDETAB = w_NDOADM_SELECT.IDETAB
				LEFT JOIN prod_chataigneraie_mysql.STDEV_RUM ON 
						STDEV_RUM.SENUMD = STDEV_SEJBEBE.SENUM AND 
						STDEV_RUM.IDETAB = STDEV_SEJBEBE.IDETAB AND 
						STDEV_RUM.DUUNIQ = STDEV_SEJBEBE.DUUNIQ
				LEFT JOIN prod_chataigneraie_mysql.STDEV_MVTLIEU MVTLIEU_DEB ON 
					STDEV_RUM.IDETAB = MVTLIEU_DEB.IDETAB AND 
					IDMVTDEB = MVTLIEU_DEB.IDMVTL
				LEFT JOIN prod_chataigneraie_mysql.STDEV_MVTLIEU MVTLIEU_FIN ON 
					STDEV_RUM.IDETAB = MVTLIEU_FIN.IDETAB AND 
					IDMVTFIN = MVTLIEU_FIN.IDMVTL
				LEFT JOIN prod_chataigneraie_mysql.CCAM4_GHM ON CCAM4_GHM.ID = STDEV_RUM.IDGHM
				LEFT JOIN prod_chataigneraie_mysql.CCAM4_GHS ON CCAM4_GHS.ID = STDEV_RUM.IDGHS
				LEFT JOIN base.t_ghm ON CCAM4_GHM.code = t_ghm.code
				LEFT JOIN base.t_ghs ON CCAM4_GHS.ghs = t_ghs.code 
				GROUP BY 1
				;

				UPDATE w_NDOADM_GHM
				SET DATEDRVAL = '20991231'
				WHERE DATEDRVAL = '00010101'
				;

				UPDATE w_NDOADM_GHM SET
					ghm_id = ghm_bebe_id,	
					ghm_code = ghm_bebe_code,
					ghs_id = ghs_bebe_id,	
					ghs_code = ghs_bebe_code,
					ghm_bebe_id = 0,
					ghm_bebe_code = '',
					ghs_bebe_id = 0,
					ghs_bebe_code = 0
				WHERE ghm_bebe_id <> 0 AND 
					ghm_id = 0;	

				CREATE INDEX w_NDOADM_GHM_i1
					ON w_NDOADM_GHM
					USING btree
					(NONUMA);

				-- Médecin
				UPDATE w_NDOADM_SELECT
				SET medecin_sejour_id = t_medecins_administratifs.oid
				FROM activite[PX].t_medecins_administratifs
				WHERE t_medecins_administratifs.code_original = DPERNU
				;

				-- Id code postal
				UPDATE w_NDOADM_SELECT
				SET code_postal_id = t_codes_postaux.oid
				FROM base.t_codes_postaux
				WHERE t_codes_postaux.code = NOCDPH
				;

				-- Id des Tiers payant
				UPDATE w_NDOADM_SELECT
				SET tiers_payant_1_id = t_tiers_payant.oid
				FROM activite[PX].t_tiers_payant
				WHERE t_tiers_payant.code_original = ('C'||NOCAIS)
				;

				UPDATE w_NDOADM_SELECT
				SET tiers_payant_2_id = t_tiers_payant.oid
				FROM activite[PX].t_tiers_payant
				WHERE t_tiers_payant.code_original = ('M'||NOCMUT)
				;

				UPDATE w_NDOADM_SELECT
				SET tiers_payant_0_id = t_tiers_payant.oid
				FROM activite[PX].t_tiers_payant
				WHERE t_tiers_payant.code_original = 'PATIENT'
				;



				-- Code service de sortie
				UPDATE w_NDOADM_SELECT SET
					mode_traitement_id = t_services_facturation.mode_traitement_id,
					mode_traitement_code = t_modes_traitement.code,
					dmt_code = t_dmt.code
				FROM activite[PX].t_services_facturation
				JOIN base.t_modes_traitement ON t_services_facturation.mode_traitement_id = t_modes_traitement.oid
				JOIN base.t_dmt ON t_services_facturation.dmt_id = t_dmt.oid
				WHERE w_NDOADM_SELECT.TARID = t_services_facturation.code_original
				;

				-- Date sortie repoussée sur présents
				UPDATE w_NDOADM_SELECT SET
					NODTSO = date(now())
				WHERE NODTSO < date(now()) AND
					SEPRE = 'O'
				;


				DROP TABLE IF EXISTS w_NDOADM_LIEU;
				CREATE TEMP TABLE w_NDOADM_LIEU AS
				SELECT 
					w_NDOADM_SELECT.NONUMA,
					0::bigint AS lieu_id,
					MAX(TARID) AS lieu_sortie_service_facturation_code_original,
					COALESCE((MAX(Array[(MVDTEN+MVHREN)::text,SPESAE::text]))[2],'0') AS lieu_sortie_activite_code_original,
					COALESCE((MAX(Array[(MVDTEN+MVHREN)::text,NMVTCHB.SENU::text]))[2],'0') AS lieu_sortie_etage_code_original,
					COALESCE((MAX(Array[(MVDTEN+MVHREN)::text,(NMVTCHB.SENU||'|'||NMVTCHB.LITNU)]))[2],'0') AS lieu_sortie_lit_code_original,
					COALESCE((MAX(Array[(MVDTEN+MVHREN)::text,NMVTCHB.SENU::text]))[2],'0') AS lieu_sortie_unite_fonctionnelle_code_original,
					'0'::text AS lieu_sortie_unite_medicale_code_original,
					'0'::text AS type_sejour_force
				FROM w_NDOADM_SELECT
				LEFT JOIN prod_chataigneraie_as400.NXXXFAC_NMVTCHB NMVTCHB ON w_NDOADM_SELECT.NONUMA = NMVTCHB.SENUMD AND 
							MVANNU = ''
				GROUP BY 1
				ORDER BY 1
				;


				INSERT INTO activite[PX].t_lieux(
							code_original_1, code_original_2, code_original_3, code_original_4, code_original_5, code_original_6)
				SELECT lieu_sortie_service_facturation_code_original, 
					lieu_sortie_activite_code_original, 
					lieu_sortie_etage_code_original, 
					lieu_sortie_lit_code_original,
					lieu_sortie_unite_fonctionnelle_code_original,
					lieu_sortie_unite_medicale_code_original
				FROM w_NDOADM_LIEU
					LEFT JOIN activite[PX].t_lieux ON (
						code_original_1 = lieu_sortie_service_facturation_code_original AND
						code_original_2 = lieu_sortie_activite_code_original AND
						code_original_3 = lieu_sortie_etage_code_original AND
						code_original_4 = lieu_sortie_lit_code_original AND
						code_original_5 = lieu_sortie_unite_fonctionnelle_code_original AND
						code_original_6 = lieu_sortie_unite_medicale_code_original
					)
				WHERE t_lieux.oid IS NULL
				GROUP BY 1,2,3,4,5,6;

				

				UPDATE activite[PX].t_lieux SET
					service_facturation_id = COALESCE(t_services_facturation.oid,0),
					mode_traitement_id = COALESCE(t_services_facturation.mode_traitement_id,0)
				FROM
					activite[PX].t_services_facturation 
				WHERE code_original_1 = t_services_facturation.code_original AND
					(t_lieux.service_facturation_id <> COALESCE(t_services_facturation.oid,0) OR
						t_lieux.mode_traitement_id <> COALESCE(t_services_facturation.mode_traitement_id,0)
					)
					;

				UPDATE activite[PX].t_lieux SET
					activite_id = COALESCE(t_activites.oid,0)
				FROM
					activite[PX].t_activites
				WHERE code_original_6 = t_activites.code_original
					AND activite_id <> COALESCE(t_activites.oid,0);


				UPDATE activite[PX].t_lieux SET
					unite_fonctionnelle_id = COALESCE(t_unites_fonctionnelles.oid,0)
				FROM
					activite[PX].t_unites_fonctionnelles
				WHERE code_original_6 = t_unites_fonctionnelles.code_original
					AND unite_fonctionnelle_id <> COALESCE(t_unites_fonctionnelles.oid,0);

				UPDATE activite[PX].t_lieux SET
					unite_medicale_id = COALESCE(t_unites_medicales.oid,0)
				FROM
					activite[PX].t_unites_medicales
				WHERE code_original_5 = t_unites_medicales.code_original
					AND unite_medicale_id <> COALESCE(t_unites_medicales.oid,0);
				

				UPDATE activite[PX].t_lieux SET
					lit_id = COALESCE(t_lits.oid,0)
				FROM
					activite[PX].t_lits
				WHERE code_original_4 = t_lits.code_original AND
					t_lieux.lit_id <> COALESCE(t_lits.oid,0)
					;

				UPDATE w_NDOADM_LIEU SET
					lieu_id = t_lieux.oid,
					type_sejour_force = CASE WHEN t_services_facturation.type_sejour IN ('1', '2', '3', '4', '5') THEN t_services_facturation.type_sejour ELSE '0' END
				FROM activite[PX].t_lieux
				JOIN activite[PX].t_services_facturation ON service_facturation_id = t_services_facturation.oid
				WHERE 
					code_original_1 = lieu_sortie_service_facturation_code_original AND
					code_original_2 = lieu_sortie_activite_code_original AND
					code_original_3 = lieu_sortie_etage_code_original AND
					code_original_4 = lieu_sortie_lit_code_original AND
					code_original_5 = lieu_sortie_unite_fonctionnelle_code_original AND
					code_original_6 = lieu_sortie_unite_medicale_code_original
				;




				DROP TABLE IF EXISTS w_sejours;
				CREATE TEMP TABLE w_sejours AS
				SELECT 
					'000000000'::text AS finess,
					w_NDOADM_SELECT.NONUMA as no_sejour, 
					w_NDOADM_SELECT.NONUMA as code_original, 
					to_char(DUUNIQ,'FM000000000') as no_patient, 
					NODTEN as date_entree, 
					to_char(NOHREN,'HH24MISS')::numeric as heure_entree, 
					NODTSO as date_sortie, 
					to_char(NOHRSO,'HH24MISS')::numeric as heure_sortie, 
					NOSEXH as code_sexe, 
					0 AS age, 
					CASE WHEN SEPRE = 'O' THEN 0 ELSE 1 END::numeric as code_sorti, 
					CASE WHEN NODTEN > now() THEN 1::numeric ELSE 0::numeric END AS code_prevu,
					CASE 
						WHEN w_NDOADM_SELECT.STID = 3 THEN '9'
						WHEN type_sejour_force <> '0' THEN type_sejour_force
						WHEN w_NDOADM_GHM.ghm_code LIKE '28%' THEN '5'
						WHEN mode_traitement_code IN ('07') OR dmt_code IN ('958') THEN '3' 
						WHEN mode_traitement_code IN ('03') AND NODTSO = NODTEN THEN '2' 
						WHEN mode_traitement_code IN ('04') AND NODTSO = NODTEN THEN '2' 
						WHEN mode_traitement_code IN ('19', '23') AND NODTSO = NODTEN THEN '2' 
						WHEN mode_traitement_code IN ('03') THEN '1' 
						WHEN NODTSO = NODTEN THEN '2' 
						ELSE '1' END as type_sejour,
					w_NDOADM_LIEU.lieu_id AS lieu_sortie_id,
					w_NDOADM_SELECT.medecin_sejour_id,
					w_NDOADM_GHM.ghs_id,
					w_NDOADM_GHM.ghm_id,
					w_NDOADM_GHM.ghs_bebe_id AS ghs_bebe1_id,
					0::numeric AS ghs_bebe2_id,
					0::numeric AS ghs_bebe3_id,
					w_NDOADM_GHM.DATEDRVAL AS DATE_GROUPAGE,
					'0'::text AS code_cp_demandee,
					w_NDOADM_SELECT.mode_traitement_id,
					w_NDOADM_GHM.MDEID AS mode_entree,
					w_NDOADM_GHM.PREID AS provenance,
					w_NDOADM_GHM.MDSID AS mode_sortie,
					w_NDOADM_GHM.DEEID AS destination,
					w_NDOADM_SELECT.tiers_payant_0_id,
					w_NDOADM_SELECT.tiers_payant_1_id,
					w_NDOADM_SELECT.tiers_payant_2_id,
					w_NDOADM_SELECT.tiers_payant_22_id,
					0 AS est_budget_global,
					w_NDOADM_SELECT.code_postal_id
				FROM w_NDOADM_SELECT
					JOIN w_NDOADM_GHM ON w_NDOADM_SELECT.NONUMA = w_NDOADM_GHM.NONUMA
					JOIN w_NDOADM_LIEU ON w_NDOADM_SELECT.NONUMA = w_NDOADM_LIEU.NONUMA
				;



				-- Forcage type selon séjour
				UPDATE w_sejours
				SET type_sejour = t_sejour.type_sejour
				FROM activite.t_sejour
				WHERE t_sejour.no_sejour = w_sejours.no_sejour AND
					t_sejour.type_sejour IN ('1','2','3','4','5','6','9');
					
				UPDATE w_sejours
				SET type_sejour = t_sejour.type_sejour
				FROM activite.t_sejour
				WHERE t_sejour.no_sejour = ('SP_'||w_sejours.no_sejour) AND
					t_sejour.type_sejour IN ('1','2','3','4','5','6','9');
					
				-- FINESS
				UPDATE w_sejours
				SET finess = t_finess.code
				FROM activite[PX].t_lieux 
				JOIN activite[PX].t_services_facturation on  service_facturation_id = t_services_facturation.oid
				JOIN base.t_finess ON finess_id = t_finess.oid
				WHERE lieu_sortie_id = t_lieux.oid;



				-- Validation des séjours

				SELECT base.cti_disable_index('activite', 'i_sejours_2');
				SELECT base.cti_disable_index('activite', 'i_sejours_3');
				SELECT base.cti_disable_index('activite', 'i_sejours_4');
				SELECT base.cti_disable_index('activite', 'i_sejours_5');
				SELECT base.cti_disable_index('activite', 'i_sejours_6');
				SELECT base.cti_disable_index('activite', 'i_sejours_7');
				SELECT base.cti_disable_index('activite', 'i_sejours_8');
				SELECT base.cti_disable_index('activite', 'i_sejours_9');
				SELECT base.cti_disable_index('activite', 'i_sejours_10');
				SELECT base.cti_disable_index('activite', 'i_sejours_11');
				SELECT base.cti_disable_index('activite', 'i_sejours_12');
				SELECT base.cti_disable_index('activite', 'i_sejours_13');

				UPDATE activite[PX].p_sejours SET
					finess = w_sejours.finess, 
					code_original = w_sejours.code_original, 
					no_patient = w_sejours.no_patient, 
					date_entree = w_sejours.date_entree, 
					heure_entree = w_sejours.heure_entree, 
					date_sortie = w_sejours.date_sortie, 
					heure_sortie = w_sejours.heure_sortie, 
					code_sexe = w_sejours.code_sexe, 
					age = w_sejours.age, 
					code_sorti = w_sejours.code_sorti, 
					code_prevu = w_sejours.code_prevu, 
					type_sejour = w_sejours.type_sejour, 
					lieu_sortie_id = w_sejours.lieu_sortie_id,
					medecin_sejour_id = w_sejours.medecin_sejour_id,
					code_cp_demandee = w_sejours.code_cp_demandee,
					mode_traitement_id = w_sejours.mode_traitement_id,
					mode_entree = w_sejours.mode_entree,
					provenance = w_sejours.provenance,
					mode_sortie = w_sejours.mode_sortie,
					destination = w_sejours.destination,
					tiers_payant_0_id = w_sejours.tiers_payant_0_id,
					tiers_payant_1_id = w_sejours.tiers_payant_1_id,
					tiers_payant_2_id = w_sejours.tiers_payant_2_id,
					tiers_payant_22_id = w_sejours.tiers_payant_22_id,
					est_budget_global = w_sejours.est_budget_global,
					date_groupage = w_sejours.date_groupage,
					ghs_id = w_sejours.ghs_id,
					ghm_id = w_sejours.ghm_id,
					ghs_bebe1_id = w_sejours.ghs_bebe1_id,
					ghs_bebe2_id = w_sejours.ghs_bebe2_id,
					ghs_bebe3_id = w_sejours.ghs_bebe3_id,
					code_postal_id = w_sejours.code_postal_id
				FROM w_sejours
				WHERE w_sejours.no_sejour = p_sejours.no_sejour
				AND (
					w_sejours.finess IS DISTINCT FROM p_sejours.finess OR
					w_sejours.code_original IS DISTINCT FROM p_sejours.code_original OR
					w_sejours.no_patient IS DISTINCT FROM p_sejours.no_patient OR
					w_sejours.date_entree IS DISTINCT FROM p_sejours.date_entree OR
					w_sejours.heure_entree IS DISTINCT FROM p_sejours.heure_entree OR
					w_sejours.date_sortie IS DISTINCT FROM p_sejours.date_sortie OR
					w_sejours.heure_sortie IS DISTINCT FROM p_sejours.heure_sortie OR
					w_sejours.code_sexe IS DISTINCT FROM p_sejours.code_sexe OR
					w_sejours.age IS DISTINCT FROM p_sejours.age OR
					w_sejours.code_sorti IS DISTINCT FROM p_sejours.code_sorti OR
					w_sejours.code_prevu IS DISTINCT FROM p_sejours.code_prevu OR
					w_sejours.type_sejour IS DISTINCT FROM p_sejours.type_sejour OR
					w_sejours.lieu_sortie_id IS DISTINCT FROM p_sejours.lieu_sortie_id OR
					w_sejours.medecin_sejour_id IS DISTINCT FROM p_sejours.medecin_sejour_id OR
					w_sejours.code_cp_demandee IS DISTINCT FROM p_sejours.code_cp_demandee OR
					w_sejours.mode_traitement_id IS DISTINCT FROM p_sejours.mode_traitement_id OR
					w_sejours.mode_entree IS DISTINCT FROM p_sejours.mode_entree OR
					w_sejours.provenance IS DISTINCT FROM p_sejours.provenance OR 
					w_sejours.mode_sortie IS DISTINCT FROM p_sejours.mode_sortie OR 
					w_sejours.destination IS DISTINCT FROM p_sejours.destination OR 
					w_sejours.tiers_payant_0_id IS DISTINCT FROM p_sejours.tiers_payant_0_id OR
					w_sejours.tiers_payant_1_id IS DISTINCT FROM p_sejours.tiers_payant_1_id OR
					w_sejours.tiers_payant_2_id IS DISTINCT FROM p_sejours.tiers_payant_2_id OR
					w_sejours.tiers_payant_22_id IS DISTINCT FROM p_sejours.tiers_payant_22_id OR
					w_sejours.est_budget_global IS DISTINCT FROM p_sejours.est_budget_global OR
					w_sejours.date_groupage IS DISTINCT FROM p_sejours.date_groupage OR
					w_sejours.ghs_id IS DISTINCT FROM p_sejours.ghs_id OR
					w_sejours.ghm_id IS DISTINCT FROM p_sejours.ghm_id OR
					w_sejours.ghs_bebe1_id IS DISTINCT FROM p_sejours.ghs_bebe1_id OR
					w_sejours.ghs_bebe2_id IS DISTINCT FROM p_sejours.ghs_bebe2_id OR
					w_sejours.ghs_bebe3_id IS DISTINCT FROM p_sejours.ghs_bebe3_id OR
					w_sejours.code_postal_id IS DISTINCT FROM p_sejours.code_postal_id
				);		


				INSERT INTO activite[PX].p_sejours (
					finess,
					no_sejour, 
					code_original, 
					no_patient, 
					date_entree, 
					heure_entree, 
					date_sortie, 
					heure_sortie, 
					code_sexe, 
					age, 
					code_sorti, 
					code_prevu, 
					type_sejour, 
					lieu_sortie_id,
					medecin_sejour_id,
					ghs_id,
					ghm_id,
					ghs_bebe1_id,
					ghs_bebe2_id,
					ghs_bebe3_id,
					date_groupage,
					code_cp_demandee,
					mode_traitement_id,
					mode_entree,
					provenance,
					mode_sortie,
					destination,
					tiers_payant_0_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					est_budget_global,
					code_postal_id
				) 
				SELECT 
					w_sejours.finess, 
					w_sejours.no_sejour, 
					w_sejours.code_original,
					w_sejours.no_patient, 
					w_sejours.date_entree, 
					w_sejours.heure_entree, 
					w_sejours.date_sortie, 
					w_sejours.heure_sortie, 
					w_sejours.code_sexe, 
					w_sejours.age, 
					w_sejours.code_sorti, 
					w_sejours.code_prevu, 
					w_sejours.type_sejour, 
					w_sejours.lieu_sortie_id,
					w_sejours.medecin_sejour_id,
					w_sejours.ghs_id,
					w_sejours.ghm_id,
					w_sejours.ghs_bebe1_id,
					w_sejours.ghs_bebe2_id,
					w_sejours.ghs_bebe3_id,
					w_sejours.date_groupage,
					w_sejours.code_cp_demandee,
					w_sejours.mode_traitement_id,
					w_sejours.mode_entree,
					w_sejours.provenance,
					w_sejours.mode_sortie,
					w_sejours.destination,
					w_sejours.tiers_payant_0_id,
					w_sejours.tiers_payant_1_id,
					w_sejours.tiers_payant_2_id,
					w_sejours.tiers_payant_22_id,
					w_sejours.est_budget_global,
					w_sejours.code_postal_id
				FROM w_sejours 
				LEFT JOIN activite[PX].p_sejours ON (w_sejours.no_sejour = p_sejours.no_sejour)
				WHERE p_sejours.no_sejour IS NULL;

				DELETE FROM activite[PX].p_sejours WHERE no_sejour NOT IN (SELECT no_sejour FROM w_sejours );
				
				DELETE FROM activite[PX].p_sejours USING
				(
				SELECT no_sejour, min(CTID) AS koctid 
				FROM activite[PX].p_sejours
				GROUP BY 1
				HAVING count(*) > 1
				) subview
				WHERE CTID = koctid;
				
				-- Séjours ignorés
				UPDATE activite[PX].p_sejours
				SET etat = 'I'
				FROM activite.t_sejour
				WHERE t_sejour.no_sejour = p_sejours.no_sejour AND
					est_ignore = '1' AND
					etat <> 'I' AND
					'' = '';

				UPDATE activite[PX].p_sejours
				SET etat = ''
				WHERE etat = 'I' AND 
					no_sejour NOT IN (SELECT no_sejour FROM activite.t_sejour WHERE est_ignore = '1') AND
					'' = '';
				
				-- Médecin séjour forcé
				UPDATE activite[PX].p_sejours
				SET medecin_sejour_id = t_sejour.medecin_sejour_id
				FROM activite.t_sejour
				WHERE t_sejour.no_sejour = p_sejours.no_sejour AND
					t_sejour.medecin_sejour_id <> 0 AND
					p_sejours.medecin_sejour_id IS DISTINCT FROM t_sejour.medecin_sejour_id AND
					'' = '';
			
			
				-- recréation index
				
				SELECT base.cti_enable_index('activite', 'i_sejours_2');
				SELECT base.cti_enable_index('activite', 'i_sejours_3');
				SELECT base.cti_enable_index('activite', 'i_sejours_4');
				SELECT base.cti_enable_index('activite', 'i_sejours_5');
				SELECT base.cti_enable_index('activite', 'i_sejours_6');
				SELECT base.cti_enable_index('activite', 'i_sejours_7');
				SELECT base.cti_enable_index('activite', 'i_sejours_8');
				SELECT base.cti_enable_index('activite', 'i_sejours_9');
				SELECT base.cti_enable_index('activite', 'i_sejours_10');
				SELECT base.cti_enable_index('activite', 'i_sejours_11');
				SELECT base.cti_enable_index('activite', 'i_sejours_12');
				SELECT base.cti_enable_index('activite', 'i_sejours_13');
									
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[

				DROP SEQUENCE IF EXISTS w_NMVTCHB_seq;
				CREATE TEMP SEQUENCE w_NMVTCHB_seq;

				DROP TABLE IF EXISTS w_NMVTCHB;
				CREATE TEMP TABLE w_NMVTCHB AS
				SELECT 
					nextval('w_NMVTCHB_seq'::regclass) AS sequence, 
					subview.*
				FROM
					(
					SELECT
						w_NDOADM_SELECT.NONUMA,
						NODTEN,
						NOHREN,
						NODTEN + NOHREN AS NODHEN,
						NODTSO,
						NOHRSO,
						NODTSO + NOHRSO AS NODHSO,
						NOURGA,
						COALESCE(MVDTEN,NODTEN) AS MVDTEN,
						COALESCE(MVHREN,NOHREN) AS MVHREN,
						COALESCE(MVDTEN,NODTEN) + COALESCE(MVHREN,NOHREN) AS MVDHEN,
						COALESCE(MVDTSO,NODTSO) AS MVDTSO,
						COALESCE(MVHRSO,NOHRSO) AS MVHRSO,
						COALESCE(MVDTSO,NODTSO) + COALESCE(MVHRSO,NOHRSO) AS MVDHSO,
						0::bigint AS lieu_id,
						'0'::text AS est_chambre_particuliere,
						
						TARID::text lieu_service_facturation_code_original,
						COALESCE(SPESAE,'0') AS lieu_activite_code_original,
						COALESCE(NMVTCHB.SENU,'0') AS lieu_etage_code_original,
						COALESCE((NMVTCHB.SENU||'|'||NMVTCHB.LITNU),'0') AS lieu_lit_code_original,
						COALESCE(NMVTCHB.SENU,'0') AS lieu_unite_fonctionnelle_code_original,
						'0'::text AS lieu_unite_medicale_code_original,

						''::text AS lieu_service_facturation_before,
						''::text AS lieu_activite_before,
						''::text AS lieu_etage_before,
						''::text AS lieu_lit_before,
						''::text AS lieu_unite_fonctionnelle_before,
						''::text AS lieu_unite_medicale_before,
						''::text AS lieu_service_facturation_after,
						''::text AS lieu_activite_after,
						''::text AS lieu_etage_after,
						''::text AS lieu_lit_after,
						''::text AS lieu_unite_fonctionnelle_after,
						''::text AS lieu_unite_medicale_after
					FROM w_NDOADM_SELECT
					LEFT JOIN prod_chataigneraie_as400.NXXXFAC_NMVTCHB NMVTCHB ON w_NDOADM_SELECT.NONUMA = NMVTCHB.SENUMD AND 
						MVANNU = ''
					ORDER BY 1,9,10
					) subview
				;


				CREATE INDEX w_NMVTCHB_i1
					ON w_NMVTCHB
					USING btree
					(NONUMA);

				CREATE INDEX w_NMVTCHB_i2
					ON w_NMVTCHB
					USING btree
					(sequence);

				-- Correction des mouvements en désaccord avec séjour (date entrée)
				UPDATE w_NMVTCHB SET
					MVDTEN = w_NMVTCHB.NODTEN,
					MVHREN = w_NMVTCHB.NOHREN,
					MVDHEN = w_NMVTCHB.NODHEN
				FROM
					(
					SELECT NONUMA, 
						MIN(sequence) AS sequence_first,
						MAX(NODTEN) AS NODTEN,
						MAX(NOHREN) AS NOHREN,
						MAX(NODHEN) AS NODHEN,
						(MIN(ARRAY[to_char(sequence,'FM00000000'),MVDTEN::text]))[2]::date AS MVDTEN_first,
						(MIN(ARRAY[to_char(sequence,'FM00000000'),MVHREN::text]))[2]::time without time zone AS MVHREN_first,
						(MIN(ARRAY[to_char(sequence,'FM00000000'),MVDHEN::text]))[2]::timestamp without time zone AS MVDHEN_first
					FROM w_NMVTCHB
					GROUP BY 1
					HAVING MAX(NODHEN) <>
						(MIN(ARRAY[to_char(sequence,'FM00000000'),MVDHEN::text]))[2]::timestamp without time zone
					) subview
				WHERE w_NMVTCHB.NONUMA = subview.NONUMA AND
					w_NMVTCHB.sequence = subview.sequence_first
				;

				-- Correction des mouvements en désaccord avec séjour (date sortie)
				UPDATE w_NMVTCHB SET
					MVDTSO = w_NMVTCHB.NODTSO,
					MVHRSO = w_NMVTCHB.NOHRSO,
					MVDHSO = w_NMVTCHB.NODHSO
				FROM
					(
					SELECT NONUMA, 
						MAX(sequence) AS sequence_last,
						MAX(NODTSO) AS NODTSO,
						MAX(NOHRSO) AS NOHRSO,
						MAX(NODHSO) AS NODHSO,
						(MAX(ARRAY[to_char(sequence,'FM00000000'),MVDTSO::text]))[2]::date AS MVDTSO_last,
						(MAX(ARRAY[to_char(sequence,'FM00000000'),MVHRSO::text]))[2]::time without time zone AS MVHRSO_last,
						(MAX(ARRAY[to_char(sequence,'FM00000000'),MVDHSO::text]))[2]::timestamp without time zone AS MVDHSO_last
					FROM w_NMVTCHB
					GROUP BY 1
					HAVING MAX(NODHSO) <>
						(MAX(ARRAY[to_char(sequence,'FM00000000'),MVDHSO::text]))[2]::timestamp without time zone
					) subview
				WHERE w_NMVTCHB.NONUMA = subview.NONUMA AND
					w_NMVTCHB.sequence = subview.sequence_last
				;

				UPDATE w_NMVTCHB SET
					MVDHEN = MVDHSO - interval '1 second',
					MVDTEN = date(MVDHSO - interval '1 second'),
					MVHREN = (MVDHSO - interval '1 second')::time without time zone
				WHERE MVDHEN > MVDHSO
				;


				UPDATE w_NMVTCHB SET
					MVDHSO = w_NMVTCHB_after.MVDHEN - interval '1 second',
					MVDTSO = date(w_NMVTCHB_after.MVDHEN - interval '1 second'),
					MVHRSO = (w_NMVTCHB_after.MVDHEN - interval '1 second')::time without time zone
				FROM w_NMVTCHB w_NMVTCHB_after 
				WHERE w_NMVTCHB.NONUMA = w_NMVTCHB_after.NONUMA AND
					w_NMVTCHB.sequence = w_NMVTCHB_after.sequence -1
				;

				DELETE FROM w_NMVTCHB
				WHERE MVDHSO < NODHEN
				;

				DELETE FROM w_NMVTCHB
				WHERE MVDTEN < date('1900-12-31')
				;


				INSERT INTO activite[PX].t_lieux(
							code_original_1, code_original_2, code_original_3, code_original_4, code_original_5, code_original_6)
				SELECT lieu_service_facturation_code_original, 
					lieu_activite_code_original, 
					lieu_etage_code_original, 
					lieu_lit_code_original,
					lieu_unite_medicale_code_original,
					lieu_unite_fonctionnelle_code_original
				FROM w_NMVTCHB
					LEFT JOIN activite[PX].t_lieux ON (
						code_original_1 = lieu_service_facturation_code_original AND
						code_original_2 = lieu_activite_code_original AND
						code_original_3 = lieu_etage_code_original AND
						code_original_4 = lieu_lit_code_original AND
						code_original_5 = lieu_unite_medicale_code_original AND
						code_original_6 = lieu_unite_fonctionnelle_code_original
					)
				WHERE t_lieux.oid IS NULL
				GROUP BY 1,2,3,4,5,6;

				

				UPDATE activite[PX].t_lieux SET
					service_facturation_id = COALESCE(t_services_facturation.oid,0),
					mode_traitement_id = COALESCE(t_services_facturation.mode_traitement_id,0)
				FROM
					activite[PX].t_services_facturation 
				WHERE code_original_1 = t_services_facturation.code_original AND
					(t_lieux.service_facturation_id <> COALESCE(t_services_facturation.oid,0) OR
						t_lieux.mode_traitement_id <> COALESCE(t_services_facturation.mode_traitement_id,0)
					)
					;

				UPDATE activite[PX].t_lieux SET
					activite_id = COALESCE(t_activites.oid,0)
				FROM
					activite[PX].t_activites
				WHERE code_original_6 = t_activites.code_original
					AND activite_id <> COALESCE(t_activites.oid,0);


				UPDATE activite[PX].t_lieux SET
					unite_fonctionnelle_id = COALESCE(t_unites_fonctionnelles.oid,0)
				FROM
					activite[PX].t_unites_fonctionnelles
				WHERE code_original_6 = t_unites_fonctionnelles.code_original
					AND unite_fonctionnelle_id <> COALESCE(t_unites_fonctionnelles.oid,0);

				UPDATE activite[PX].t_lieux SET
					unite_medicale_id = COALESCE(t_unites_medicales.oid,0)
				FROM
					activite[PX].t_unites_medicales
				WHERE code_original_5 = t_unites_medicales.code_original
					AND unite_medicale_id <> COALESCE(t_unites_medicales.oid,0);
				

				UPDATE activite[PX].t_lieux SET
					lit_id = COALESCE(t_lits.oid,0)
				FROM
					activite[PX].t_lits
				WHERE code_original_4 = t_lits.code_original AND
					t_lieux.lit_id <> COALESCE(t_lits.oid,0)
					;

				UPDATE w_NMVTCHB SET
					lieu_id = t_lieux.oid,
					est_chambre_particuliere = CASE WHEN t_lits.chambre_particuliere = 'O' THEN '1' ELSE '0' END
				FROM activite[PX].t_lieux
				JOIN activite[PX].t_lits ON lit_id = t_lits.oid
				WHERE 
					code_original_1 = lieu_service_facturation_code_original AND
					code_original_2 = lieu_activite_code_original AND
					code_original_3 = lieu_etage_code_original AND
					code_original_4 = lieu_lit_code_original AND
					code_original_5 = lieu_unite_medicale_code_original AND
					code_original_6 = lieu_unite_fonctionnelle_code_original
				;



				-- Etat avant
				UPDATE w_NMVTCHB SET
					lieu_service_facturation_before = w_NMVTCHB_before.lieu_service_facturation_code_original,
					lieu_activite_before = w_NMVTCHB_before.lieu_activite_code_original,
					lieu_etage_before = w_NMVTCHB_before.lieu_etage_code_original,
					lieu_lit_before = w_NMVTCHB_before.lieu_lit_code_original,
					lieu_unite_fonctionnelle_before = w_NMVTCHB_before.lieu_unite_fonctionnelle_code_original,
					lieu_unite_medicale_before = w_NMVTCHB_before.lieu_unite_medicale_code_original
				FROM w_NMVTCHB w_NMVTCHB_before 
				WHERE w_NMVTCHB.NONUMA = w_NMVTCHB_before.NONUMA AND
					w_NMVTCHB.sequence = w_NMVTCHB_before.sequence +1
				;

				-- Etat apres
				UPDATE w_NMVTCHB SET
					lieu_service_facturation_after = w_NMVTCHB_after.lieu_service_facturation_code_original,
					lieu_activite_after = w_NMVTCHB_after.lieu_activite_code_original,
					lieu_etage_after = w_NMVTCHB_after.lieu_etage_code_original,
					lieu_lit_after = w_NMVTCHB_after.lieu_lit_code_original,
					lieu_unite_fonctionnelle_after = w_NMVTCHB_after.lieu_unite_fonctionnelle_code_original,
					lieu_unite_medicale_after = w_NMVTCHB_after.lieu_unite_medicale_code_original
				FROM w_NMVTCHB w_NMVTCHB_after 
				WHERE w_NMVTCHB.NONUMA = w_NMVTCHB_after.NONUMA AND
					w_NMVTCHB.sequence = w_NMVTCHB_after.sequence -1
				;

				


				DROP TABLE IF EXISTS w_mouvements_sejour;

				CREATE TEMP TABLE w_mouvements_sejour AS
				SELECT 
					w_NMVTCHB.NONUMA,
					w_NMVTCHB.NONUMA AS no_sejour,
					p_sejours.oid AS sejour_id,
					sequence,
					p_calendrier.date, 
					p_calendrier.jour_semaine,
					p_calendrier.is_weekend,
					CASE WHEN p_calendrier.date = MVDTEN THEN to_char(MVHREN,'HH24MISS')::numeric ELSE 0 END AS heure_debut,
					CASE WHEN p_calendrier.date = MVDTSO THEN to_char(MVHRSO,'HH24MISS')::numeric ELSE 240000 END AS heure_fin,
					CASE WHEN MVDHEN = NODHEN AND p_calendrier.date = MVDTEN AND p_sejours.type_sejour = '1' THEN 1 ELSE 0 END AS nb_entrees_directes,
					CASE WHEN MVDHSO = NODHSO AND p_calendrier.date = MVDTSO AND p_sejours.type_sejour = '1' THEN 1 ELSE 0 END AS nb_sorties_directes,
					CASE WHEN MVDHEN = NODHEN AND p_calendrier.date = MVDTEN AND p_sejours.type_sejour = '1' THEN '1' ELSE '0' END AS est_jour_entree,
					CASE WHEN MVDHSO = NODHSO AND p_calendrier.date = MVDTSO AND p_sejours.type_sejour = '1' THEN '1' ELSE '0' END AS est_jour_sortie,
					CASE WHEN p_sejours.type_sejour = '1' THEN '1' ELSE '0' END AS est_jour_hospitalisation,
					CASE WHEN MVDHEN = NODHEN AND p_calendrier.date = MVDTEN AND p_sejours.type_sejour = '2' THEN 1 ELSE 0 END AS nb_ambulatoires,
					CASE WHEN MVDHEN = NODHEN AND p_calendrier.date = MVDTEN AND p_sejours.type_sejour = '3' THEN 1 ELSE 0 END AS nb_externes,
					CASE WHEN NOURGA = '1' AND p_calendrier.date = MVDTEN AND MVDHEN = NODHEN THEN 1 ELSE 0 END AS nb_urgences,
					CASE WHEN MVDHEN = NODHEN AND p_calendrier.date = MVDTEN AND p_sejours.type_sejour = '5' THEN 1 ELSE 0 END AS nb_seances,

					CASE WHEN p_calendrier.date = MVDTEN AND p_sejours.type_sejour = '1' AND MVDHEN <> NODHEN AND lieu_service_facturation_code_original <> lieu_service_facturation_before THEN 1 ELSE 0 END AS nb_entrees_mutation_service,
					CASE WHEN p_calendrier.date = MVDTSO AND p_sejours.type_sejour = '1' AND MVDHSO <> NODHSO AND lieu_service_facturation_code_original <> lieu_service_facturation_after THEN 1 ELSE 0 END AS nb_sorties_mutation_service,
					CASE WHEN p_calendrier.date = MVDTEN AND p_sejours.type_sejour = '1' AND MVDHEN <> NODHEN AND lieu_etage_code_original <> lieu_etage_before THEN 1 ELSE 0 END AS nb_entrees_mutation_etage,
					CASE WHEN p_calendrier.date = MVDTSO AND p_sejours.type_sejour = '1' AND MVDHSO <> NODHSO AND lieu_etage_code_original <> lieu_etage_after THEN 1 ELSE 0 END AS nb_sorties_mutation_etage,
					0 AS nb_entrees_mutation_activite,
					0 AS nb_sorties_mutation_activite,
					0 AS nb_entrees_mutation_unite_medicale,
					0 AS nb_sorties_mutation_unite_medicale,

					CASE WHEN p_sejours.type_sejour = '1' THEN 1 ELSE 0 END AS nb_jours_js_inclus,
					CASE WHEN p_sejours.type_sejour = '1' AND p_calendrier.date <> MVDTSO THEN 1 ELSE 0 END AS nb_jours_js_non_inclus,

					CASE WHEN p_sejours.type_sejour IN ('1','2') THEN est_chambre_particuliere ELSE '0' END AS est_chambre_particuliere,
					CASE WHEN p_sejours.type_sejour IN ('1','2') AND est_chambre_particuliere = 1 THEN 1 ELSE 0 END AS nb_chambres_particulieres,

					0 AS nb_bebes,

					lieu_id,

					p_sejours.medecin_sejour_id as medecin_sejour_id,
					
					CASE WHEN p_sejours.code_prevu = '1' OR p_calendrier.date > date(now_chataigneraie) THEN '1' ELSE '0' END AS est_mouvement_previsionnel
				FROM w_NMVTCHB 
					JOIN w_PERIODE ON 1=1
					JOIN activite[PX].p_sejours ON w_NMVTCHB.NONUMA = p_sejours.no_sejour AND p_sejours.etat = ''
					JOIN base.p_calendrier ON (p_calendrier.date BETWEEN MVDTEN AND MVDTSO AND p_calendrier.date <= now() + interval '1 month' )
				ORDER BY sequence, p_calendrier.date;



				-- Bébés A faire
				-- Séances A Faire



				DELETE FROM w_mouvements_sejour 
				WHERE 
					nb_entrees_directes = 0 AND
					nb_sorties_directes = 0 AND
					est_jour_hospitalisation <> '1' AND
					nb_ambulatoires = 0 AND
					nb_externes = 0 AND
					nb_urgences = 0 AND
					nb_seances = 0 AND
					nb_entrees_mutation_service = 0 AND
					nb_sorties_mutation_service = 0 AND
					nb_entrees_mutation_etage = 0 AND
					nb_sorties_mutation_etage = 0 AND
					nb_entrees_mutation_activite = 0 AND
					nb_sorties_mutation_activite = 0 AND
					nb_entrees_mutation_unite_medicale = 0 AND
					nb_sorties_mutation_unite_medicale = 0 AND
					nb_jours_js_inclus = 0 AND
					nb_jours_js_non_inclus = 0 AND
					est_chambre_particuliere <> '1' AND
					nb_chambres_particulieres = 0 AND
					nb_bebes = 0;
					
					
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_1');
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_2');
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_3');
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_4');
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_5');

				INSERT INTO activite[PX].p_mouvements_sejour(
							sejour_id, no_sejour, date, nb_jours_js_non_inclus, 
							lieu_id, nb_entrees_directes, nb_sorties_directes, 
							nb_urgences, nb_externes, nb_ambulatoires, medecin_sejour_id, 
							nb_jours_js_inclus, nb_seances, nb_entrees_mutation_service, 
							nb_sorties_mutation_service, nb_entrees_mutation_activite, nb_sorties_mutation_activite, 
							nb_entrees_mutation_etage, nb_sorties_mutation_etage, nb_chambres_particulieres, 
							heure_debut, heure_fin, est_jour_entree, est_jour_sortie, est_jour_hospitalisation, 
							est_chambre_particuliere, 
							nb_entrees_mutation_unite_medicale, nb_sorties_mutation_unite_medicale,
							nb_bebes,est_mouvement_previsionnel, jour_semaine, is_weekend)
				SELECT sejour_id, no_sejour, date,nb_jours_js_non_inclus, 
							lieu_id, nb_entrees_directes, nb_sorties_directes, 
							nb_urgences, nb_externes, nb_ambulatoires, medecin_sejour_id, 
							nb_jours_js_inclus, nb_seances, nb_entrees_mutation_service, 
							nb_sorties_mutation_service, nb_entrees_mutation_activite, nb_sorties_mutation_activite, 
							nb_entrees_mutation_etage, nb_sorties_mutation_etage, nb_chambres_particulieres, 
							heure_debut, heure_fin, est_jour_entree, est_jour_sortie, est_jour_hospitalisation, 
							est_chambre_particuliere, 
							nb_entrees_mutation_unite_medicale, nb_sorties_mutation_unite_medicale,
							nb_bebes,est_mouvement_previsionnel, jour_semaine, is_weekend
				FROM w_mouvements_sejour 
				ORDER BY no_sejour, sequence;
				
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_1');
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_2');
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_3');
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_4');
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_5');
				
				
				
				UPDATE activite[PX].p_sejours SET lieu_sortie_id = p_mouvements_sejour.lieu_id  
					FROM activite[PX].p_mouvements_sejour 
					WHERE p_sejours.no_sejour = p_mouvements_sejour.no_sejour  
						AND p_mouvements_sejour.date = p_sejours.date_sortie  
						AND (lieu_sortie_id IS DISTINCT FROM p_mouvements_sejour.lieu_id);
						
				UPDATE activite[PX].p_sejours SET lieu_sortie_id = p_mouvements_sejour.lieu_id  
					FROM activite[PX].p_mouvements_sejour 
					WHERE p_sejours.no_sejour = p_mouvements_sejour.no_sejour  
						AND p_mouvements_sejour.date = p_sejours.date_sortie  AND p_mouvements_sejour.nb_sorties_directes = 1
						AND (lieu_sortie_id <> p_mouvements_sejour.lieu_id OR lieu_sortie_id IS NULL);  

				

				UPDATE activite[PX].p_sejours SET lieu_sortie_id = p_mouvements_sejour.lieu_id  
					FROM activite[PX].p_mouvements_sejour 
					WHERE p_sejours.no_sejour = p_mouvements_sejour.no_sejour 
						AND (p_sejours.lieu_sortie_id = 0 OR lieu_sortie_id IS NULL);
						
						
				
				UPDATE activite[PX].p_sejours
				SET type_sejour = '5'
				WHERE type_sejour <> '5' AND 
					type_sejour <> '1' AND 
					no_sejour IN (
						SELECT no_sejour 
						FROM activite[PX].p_mouvements_sejour 
						WHERE nb_seances > 0);
						
				
				UPDATE activite[PX].p_mouvements_sejour
				SET nb_externes = 0, nb_ambulatoires = 0
				FROM activite[PX].p_sejours
				WHERE p_mouvements_sejour.no_sejour = p_sejours.no_sejour AND
					p_sejours.type_sejour = '5' AND
					(
						nb_externes <> 0 OR
						nb_ambulatoires <> 0
					)

				;
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lien PMSI">
		
			<condition><![CDATA[
				"[TYPE_TRAITEMENT]" == "E"
			]]></condition>
			
			<sqlcmd><![CDATA[
			
					SELECT activite.cti_reorganize_sejour_pmsi()

			]]></sqlcmd>
		</NODE>
		
	</NODE>

	<NODE name="FACTURE" label="RECUPERATION DES FACTURES">
	
	
		<NODE label="Correction mouvements">
			<sqlcmd><![CDATA[
						
				
		]]></sqlcmd>
		</NODE>
	
		<NODE label="Entêtes">
			<sqlcmd><![CDATA[
			
			
				DROP TABLE IF EXISTS w_FACPAR;
				CREATE TEMP TABLE w_FACPAR AS
				SELECT
					FACTURES.NONUMA,
					FACTURES.FAID,
					MAX(FAAVFA) AS FAAVFA,
					MAX(FATTCL) AS FATTCL,
					SUM(CASE WHEN BFIDPA = 1 THEN FACPCT ELSE 0 END) AS FACPCT_1,
					SUM(CASE WHEN BFIDPA = 2 THEN FACPCT ELSE 0 END) AS FACPCT_2,
					SUM(CASE WHEN BFIDPA = 3 THEN FACPCT ELSE 0 END) AS FACPCT_3,
					MAX(FAHOTT) AS FAHOTT,
					SUM(CASE WHEN BFIDPA = 1 THEN FACPHO ELSE 0 END) AS FACPHO_1,
					SUM(CASE WHEN BFIDPA = 2 THEN FACPHO ELSE 0 END) AS FACPHO_2,
					SUM(CASE WHEN BFIDPA = 3 THEN FACPHO ELSE 0 END) AS FACPHO_3,
					MAX(CASE WHEN BFIDPA = 1 THEN FACPDE ELSE '00010101' END) AS FACPDE_1,
					MAX(CASE WHEN BFIDPA = 2 THEN FACPDE ELSE '00010101' END) AS FACPDE_2,
					MAX(CASE WHEN BFIDPA = 3 THEN FACPDE ELSE '00010101' END) AS FACPDE_3,
					MAX(CASE WHEN BFIDPA = 1 THEN FACNBO ELSE 0 END) AS FACNBO_1,
					MAX(CASE WHEN BFIDPA = 2 THEN FACNBO ELSE 0 END) AS FACNBO_2,
					MAX(CASE WHEN BFIDPA = 3 THEN FACNBO ELSE 0 END) AS FACNBO_3
				FROM prod_chataigneraie_as400.NXXXFAC_FACTURES FACTURES
				JOIN w_NDOADM_SELECT ON w_NDOADM_SELECT.NONUMA = FACTURES.NONUMA
				LEFT JOIN prod_chataigneraie_as400.NXXXFAC_FACPAR FACPAR ON FACTURES.FAID = FACPAR.FAID
				GROUP BY 1,2;
				
				UPDATE w_FACPAR SET
					FATTCL = 0 - FATTCL,
					FACPCT_1 = 0 - FACPCT_1,
					FACPCT_2 = 0 - FACPCT_2,
					FACPCT_3 = 0 - FACPCT_3,
					FAHOTT = 0 - FAHOTT,
					FACPHO_1 = 0 - FACPHO_1,
					FACPHO_2 = 0 - FACPHO_2,
					FACPHO_3 = 0 - FACPHO_3
				WHERE FAAVFA = 'A';

				
				CREATE INDEX w_FACPAR_i1
					ON w_FACPAR
					USING btree
					(FAID);





				DROP TABLE IF EXISTS w_REGLI;
				CREATE TEMP TABLE w_REGLI AS
				SELECT
					FACTURES.NONUMA,
					FACTURES.FAID,
					MAX(FAAVFA) AS FAAVFA,

					MAX(FATTCL) AS FATTCL,
					COALESCE(SUM(CASE WHEN ACID < 100 THEN 0-ACMTFC ELSE 0 END),0) AS ACMTFC,
					COALESCE(SUM(CASE WHEN ACINDI = 1 AND ACID < 100 THEN 0-ACMTFC ELSE 0 END),0) AS ACMTFC_1,
					COALESCE(SUM(CASE WHEN ACINDI = 2 AND ACID < 100 THEN 0-ACMTFC ELSE 0 END),0) AS ACMTFC_2,
					COALESCE(SUM(CASE WHEN ACINDI = 3 AND ACID < 100 THEN 0-ACMTFC ELSE 0 END),0) AS ACMTFC_3,
					COALESCE(MAX(CASE WHEN ACID < 100 THEN (to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||to_char(ACJDTF,'FM00'))::date ELSE '00010101' END),'00010101') AS ACDDTF,
					COALESCE(MAX(CASE WHEN ACINDI = 1 AND ACID < 100 THEN (to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||to_char(ACJDTF,'FM00'))::date ELSE '00010101' END),'00010101') AS ACDDTF_1,
					COALESCE(MAX(CASE WHEN ACINDI = 2 AND ACID < 100 THEN (to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||to_char(ACJDTF,'FM00'))::date ELSE '00010101' END),'00010101') AS ACDDTF_2,
					COALESCE(MAX(CASE WHEN ACINDI = 3 AND ACID < 100 THEN (to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||to_char(ACJDTF,'FM00'))::date ELSE '00010101' END),'00010101') AS ACDDTF_3,

					MAX(FAHOTT) AS FAHOTT,
					COALESCE(SUM(CASE WHEN ACID > 100 THEN 0-ACMTFC ELSE 0 END),0) AS ACMTFH,
					COALESCE(SUM(CASE WHEN ACINDI = 1 AND ACID > 100 THEN 0-ACMTFC ELSE 0 END),0) AS ACMTFH_1,
					COALESCE(SUM(CASE WHEN ACINDI = 2 AND ACID > 100 THEN 0-ACMTFC ELSE 0 END),0) AS ACMTFH_2,
					COALESCE(SUM(CASE WHEN ACINDI = 3 AND ACID > 100 THEN 0-ACMTFC ELSE 0 END),0) AS ACMTFH_3,
					COALESCE(MAX(CASE WHEN ACID > 100 THEN (to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||to_char(ACJDTF,'FM00'))::date ELSE '00010101' END),'00010101') AS ACDDTH,
					COALESCE(MAX(CASE WHEN ACINDI = 1 AND ACID > 100 THEN (to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||to_char(ACJDTF,'FM00'))::date ELSE '00010101' END),'00010101') AS ACDDTH_1,
					COALESCE(MAX(CASE WHEN ACINDI = 2 AND ACID > 100 THEN (to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||to_char(ACJDTF,'FM00'))::date ELSE '00010101' END),'00010101') AS ACDDTH_2,
					COALESCE(MAX(CASE WHEN ACINDI = 3 AND ACID > 100 THEN (to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||to_char(ACJDTF,'FM00'))::date ELSE '00010101' END),'00010101') AS ACDDTH_3,

					COALESCE(MAX(to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||to_char(ACJDTF,'FM00'))::date,'00010101') AS ACDDTCH

				FROM prod_chataigneraie_as400.NXXXFAC_FACTURES FACTURES
				JOIN w_NDOADM_SELECT ON w_NDOADM_SELECT.NONUMA = FACTURES.NONUMA
				LEFT JOIN prod_chataigneraie_as400.XXXFICH_REGLI REGLI ON FACTURES.FAID = REGLI.ACNFAC
				GROUP BY 1,2;
				
				UPDATE w_REGLI SET
					FATTCL = 0 - FATTCL,
					FAHOTT = 0 - FAHOTT
				WHERE FAAVFA = 'A';

				
				CREATE INDEX w_REGLI_i1
					ON w_REGLI
					USING btree
					(FAID);



				
				
				DROP TABLE IF EXISTS w_factures;
				CREATE TEMP TABLE w_factures AS 
				SELECT
					FACTURES.FAID,
					nextval('activite[PX].s_factures'::regclass) AS oid,
					p_sejours.oid AS sejour_id,
					p_sejours.no_sejour,
					FACTURES.FAID::text AS no_facture,
					FACTURES.FAID::text AS no_facture_reference,
					0::bigint AS facture_reference_id,
					FAC_FAID::text AS no_facture_od_avoir,
					0::bigint AS facture_od_avoir_id,
					FADTE AS date_debut,
					FADTS AS date_fin,
					p_sejours.ghs_id,
					p_sejours.ghs_bebe1_id,
					p_sejours.ghs_bebe2_id,
					p_sejours.ghs_bebe3_id,
					p_sejours.code_cp_demandee,
					FACTURES.FAAVFA,
					CASE WHEN FACTURES.FAAVFA = 'A' THEN '1' ELSE '0' END AS type_facture,
					CASE WHEN FACTURES.FAAVFA = 'A' THEN 1 ELSE 0 END AS nb_rejets,
					'1' AS code_facture,
					FADAT AS date_facture,
					'1' AS code_vente,
					to_char(FADAT,'YYYYMM')::numeric AS mois_vente,
					FADAT AS date_vente,
					'' AS code_cloture,

					'' AS particularite_t2a,

					
					p_sejours.tiers_payant_0_id,
					FACCAI,
					p_sejours.tiers_payant_1_id,
					FACMUT,
					p_sejours.tiers_payant_2_id,
					p_sejours.tiers_payant_22_id,
					w_FACPAR.FATTCL AS montant_facture_c,
					w_FACPAR.FAHOTT AS montant_facture_h,
					COALESCE(FACPCT_3,0) AS montant_facture_0_c,
					COALESCE(FACPHO_3,0) AS montant_facture_0_h,
					COALESCE(FACPCT_1,0) AS montant_facture_1_c,
					COALESCE(FACPHO_1,0) AS montant_facture_1_h,
					COALESCE(FACPCT_2,0) AS montant_facture_2_c,
					COALESCE(FACPHO_3,0) AS montant_facture_2_h,
					0::numeric AS montant_facture_22_c,
					0::numeric AS montant_facture_22_h,
					w_FACPAR.FATTCL AS montant_comptabilise_c,
					w_FACPAR.FAHOTT AS montant_comptabilise_h,
					FACPCT_3 AS montant_comptabilise_0_c,
					FACPHO_3 AS montant_comptabilise_0_h,
					FACPCT_1 AS montant_comptabilise_1_c,
					FACPHO_1 AS montant_comptabilise_1_h,
					FACPCT_2 AS montant_comptabilise_2_c,
					FACPHO_3 AS montant_comptabilise_2_h,
					0::numeric AS montant_comptabilise_22_c,
					0::numeric AS montant_comptabilise_22_h,
					ACMTFC AS montant_regle_c,
					ACMTFH AS montant_regle_h,
					ACMTFC_3 AS montant_regle_0_c,
					ACMTFH_3 AS montant_regle_0_h,
					ACMTFC_1 AS montant_regle_1_c,
					ACMTFH_1 AS montant_regle_1_h,
					ACMTFC_2 AS montant_regle_2_c,
					ACMTFH_2 AS montant_regle_2_h,
					0::numeric AS montant_regle_22_c,
					0::numeric AS montant_regle_22_h,
					CASE WHEN FACPDE_3 > '00010101' THEN '1' ELSE '0' END AS code_expedie_0,
					CASE WHEN FACPDE_1 > '00010101' THEN '1' ELSE '0' END AS code_expedie_1,
					CASE WHEN FACPDE_2 > '00010101' THEN '1' ELSE '0' END AS code_expedie_2,
					'0' AS code_expedie_22,
					GREATEST(FACPDE_1,FACPDE_2,FACPDE_3) AS date_expedition,
					FACPDE_3 AS date_expedition_0,
					FACPDE_1 AS date_expedition_1,
					FACPDE_2 AS date_expedition_2,
					'20991231'::date AS date_expedition_22,
					FACNBO_3::text AS no_bordereau_0,
					FACNBO_1::text AS no_bordereau_1,
					FACNBO_2::text AS no_bordereau_2,
					''::text AS no_bordereau_22,
					ACDDTCH AS date_solde,
					ACDDTF AS date_solde_c,
					ACDDTH AS date_solde_h,
					ACDDTF_3 AS date_solde_0_c,
					ACDDTH_3 AS date_solde_0_h,
					ACDDTF_1 AS date_solde_1_c,
					ACDDTH_1 AS date_solde_1_h,
					ACDDTF_2 AS date_solde_2_c,
					ACDDTH_2 AS date_solde_2_h,
					'0001-01-01'::date AS date_solde_22_c,
					'0001-01-01'::date AS date_solde_22_h

				FROM prod_chataigneraie_as400.NXXXFAC_FACTURES FACTURES
				JOIN w_NDOADM_SELECT ON w_NDOADM_SELECT.NONUMA = FACTURES.NONUMA
				JOIN activite[PX].p_sejours ON FACTURES.NONUMA = p_sejours.code_original
				JOIN w_FACPAR ON FACTURES.FAID = w_FACPAR.FAID
				JOIN w_REGLI ON FACTURES.FAID = w_REGLI.FAID
				WHERE FACTURES.FAID <> 0
				;
				
				CREATE INDEX w_factures_i1
					ON w_factures
					USING btree
					(FAID);

				CREATE INDEX w_factures_i2
					ON w_factures
					USING btree
					(sejour_id);

				UPDATE w_factures
				SET facture_od_avoir_id = w_factures_od_avoir.oid
				FROM w_factures w_factures_od_avoir
				WHERE w_factures.no_facture_od_avoir = w_factures_od_avoir.no_facture
				;

				UPDATE w_factures
				SET type_facture = 'X'
				WHERE w_factures.no_facture_od_avoir <> '0' AND
					w_factures.type_facture = '0'
				;

				UPDATE w_factures
				SET no_facture_reference = subview.no_facture_reference
				FROM
					(SELECT sejour_id,
						(MAX(ARRAY[date_facture::text,no_facture]))[2] AS no_facture_reference
						FROM w_factures
						WHERE type_facture = '0'
						GROUP BY 1
					) subview
				WHERE w_factures.sejour_id = subview.sejour_id AND
					w_factures.no_facture_reference <> subview.no_facture_reference
				;

				UPDATE w_factures
				SET facture_reference_id = w_factures_reference.oid
				FROM w_factures w_factures_reference
				WHERE w_factures.no_facture_reference = w_factures_reference.no_facture
				;


				SELECT base.cti_disable_index('activite', 'i_factures_3');
				SELECT base.cti_disable_index('activite', 'i_factures_4');
				SELECT base.cti_disable_index('activite', 'i_factures_5');
				SELECT base.cti_disable_index('activite', 'i_factures_6');
				SELECT base.cti_disable_index('activite', 'i_factures_7');
				SELECT base.cti_disable_index('activite', 'i_factures_8');
				SELECT base.cti_disable_index('activite', 'i_factures_9');
				SELECT base.cti_disable_index('activite', 'i_factures_10');
				SELECT base.cti_disable_index('activite', 'i_factures_11');
				SELECT base.cti_disable_index('activite', 'i_factures_12');
				SELECT base.cti_disable_index('activite', 'i_factures_13');
				SELECT base.cti_disable_index('activite', 'i_factures_14');
				

				INSERT INTO activite[PX].p_factures (
					oid,
					sejour_id,
					no_sejour,
					no_facture,
					no_facture_reference,
					facture_reference_id,
					no_facture_od_avoir,
					facture_od_avoir_id,
					date_debut,
					date_fin,
					ghs_id,
					ghs_bebe1_id,
					ghs_bebe2_id,
					ghs_bebe3_id,
					code_cp_demandee,
					type_facture,
					nb_rejets,
					code_facture,
					date_facture,
					code_vente,
					mois_vente,
					date_vente,
					code_cloture,

					particularite_t2a,
					
					tiers_payant_0_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_22,
					date_expedition,
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					date_expedition_22,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					no_bordereau_22,

					date_solde,
					date_solde_c,
					date_solde_h,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2_c,
					date_solde_2_h,
					date_solde_22_c,
					date_solde_22_h


					)

				SELECT
					oid,
					sejour_id,
					no_sejour,
					no_facture,
					w_factures.no_facture_reference,
					facture_reference_id,
					no_facture_od_avoir,
					facture_od_avoir_id,
					date_debut,
					date_fin,
					ghs_id,
					ghs_bebe1_id,
					ghs_bebe2_id,
					ghs_bebe3_id,
					code_cp_demandee,
					type_facture,
					nb_rejets,
					code_facture,
					date_facture,
					code_vente,
					mois_vente,
					date_vente,
					code_cloture,

					particularite_t2a,
					
					tiers_payant_0_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_22,
					CASE WHEN date_expedition <> '00010101' THEN date_expedition ELSE '20991231' END,
					CASE WHEN date_expedition_0 <> '00010101' THEN date_expedition_0 ELSE '20991231' END,
					CASE WHEN date_expedition_1 <> '00010101' THEN date_expedition_1 ELSE '20991231' END,
					CASE WHEN date_expedition_2 <> '00010101' THEN date_expedition_2 ELSE '20991231' END,
					CASE WHEN date_expedition_22 <> '00010101' THEN date_expedition_22 ELSE '20991231' END,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					no_bordereau_22,

					CASE WHEN date_solde <> '00010101' THEN date_solde ELSE '20991231' END,
					CASE WHEN date_solde_c <> '00010101' THEN date_solde_c ELSE '20991231' END,
					CASE WHEN date_solde_h <> '00010101' THEN date_solde_h ELSE '20991231' END,
					CASE WHEN date_solde_0_c <> '00010101' THEN date_solde_0_c ELSE '20991231' END,
					CASE WHEN date_solde_0_h <> '00010101' THEN date_solde_0_h ELSE '20991231' END,
					CASE WHEN date_solde_1_c <> '00010101' THEN date_solde_1_c ELSE '20991231' END,
					CASE WHEN date_solde_1_h <> '00010101' THEN date_solde_1_h ELSE '20991231' END,
					CASE WHEN date_solde_2_c <> '00010101' THEN date_solde_2_c ELSE '20991231' END,
					CASE WHEN date_solde_2_h <> '00010101' THEN date_solde_2_h ELSE '20991231' END,
					CASE WHEN date_solde_22_c <> '00010101' THEN date_solde_22_c ELSE '20991231' END,
					CASE WHEN date_solde_22_h <> '00010101' THEN date_solde_22_h ELSE '20991231' END

				FROM w_factures 
				WHERE oid NOT IN (SELECT oid FROM activite[PX].p_factures);

				SELECT base.cti_enable_index('activite', 'i_factures_3');
				SELECT base.cti_enable_index('activite', 'i_factures_4');
				SELECT base.cti_enable_index('activite', 'i_factures_5');
				SELECT base.cti_enable_index('activite', 'i_factures_6');
				SELECT base.cti_enable_index('activite', 'i_factures_7');
				SELECT base.cti_enable_index('activite', 'i_factures_8');
				SELECT base.cti_enable_index('activite', 'i_factures_9');
				SELECT base.cti_enable_index('activite', 'i_factures_10');
				SELECT base.cti_enable_index('activite', 'i_factures_11');
				SELECT base.cti_enable_index('activite', 'i_factures_12');
				SELECT base.cti_enable_index('activite', 'i_factures_13');
				SELECT base.cti_enable_index('activite', 'i_factures_14');





				
				
				
				-- Séjours sans facture
				
				DROP TABLE IF EXISTS w_factures_trous;
				CREATE TEMP TABLE w_factures_trous AS
				SELECT
					p_sejours.finess,
					p_sejours.no_sejour,
					p_sejours.date_entree,
					p_sejours.tiers_payant_0_id, 
					p_sejours.tiers_payant_1_id, 
					p_sejours.tiers_payant_2_id, 
					p_sejours.tiers_payant_22_id,
					p_sejours.code_cp_demandee,
					COALESCE(t_services_facturation.avec_facturation_intermediaire,'') AS avec_facturation_intermediaire,
					p_sejours.code_sorti,
					CASE WHEN code_sorti = '1' THEN date_sortie ELSE date(now()) END AS date_sortie,
					MAX(CASE WHEN p_factures.date_fin IS NOT NULL AND date_sortie - date_entree < 30 THEN date_sortie ELSE p_factures.date_fin END) AS date_fin_facture,
					date(MAX(COALESCE(CASE WHEN p_factures.date_fin IS NOT NULL AND date_sortie - date_entree < 30 THEN date_sortie ELSE p_factures.date_fin END + interval '1 day', p_sejours.date_entree))) AS date_debut_encours,
					CASE WHEN code_sorti = '1' THEN date_sortie ELSE date(now()) END AS date_fin_encours
				FROM activite[PX].p_sejours
					LEFT JOIN activite[PX].p_factures ON p_sejours.no_sejour = p_factures.no_sejour AND no_facture NOT LIKE 'E%'
					JOIN activite[PX].t_lieux ON lieu_sortie_id = t_lieux.oid
					JOIN activite[PX].t_services_facturation ON t_lieux.service_facturation_id = t_services_facturation.oid
				WHERE p_sejours.etat = '' AND p_sejours.est_sans_facturation IS DISTINCT FROM '1' AND date_entree <= now() AND p_sejours.type_sejour <> '6' AND p_sejours.type_sejour <> '9' AND code_prevu <> 1
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11;
				
				INSERT INTO activite[PX].p_factures(
					finess, 
					no_sejour, 
					no_facture, 
					no_facture_reference, 
					type_facture, 
					code_facture, 
					date_facture, 
					tiers_payant_0_id, tiers_payant_1_id, tiers_payant_2_id, tiers_payant_22_id,
					code_cp_demandee, 
					date_debut, 
					date_fin)
				SELECT finess, 
					no_sejour, 
					no_sejour || '.00' || CASE WHEN avec_facturation_intermediaire = '1' THEN '_' || p_calendrier.mois ELSE '' END, 
					no_sejour || '.00' || CASE WHEN avec_facturation_intermediaire = '1' THEN '_' || p_calendrier.mois ELSE '' END, 
					'E', 
					'0', 
					'20991231'::date,
					tiers_payant_0_id, tiers_payant_1_id, tiers_payant_2_id, tiers_payant_22_id, 
					code_cp_demandee , 
					MIN(GREATEST(date_debut_encours, p_calendrier.date)), 
					MAX(LEAST(date_fin_encours, p_calendrier.date))
				FROM w_factures_trous 
					JOIN base.p_calendrier ON p_calendrier.date  BETWEEN date_debut_encours AND date_fin_encours
				WHERE (date_debut_encours <= date_fin_encours or date_fin_facture is null) 
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12;

					
				UPDATE activite[PX].p_factures SET
					date_debut = date_entree,
					date_fin = CASE WHEN code_sorti = '1' THEN date_sortie ELSE date_fin END
				FROM activite[PX].p_sejours 
					JOIN activite[PX].t_lieux ON lieu_sortie_id = t_lieux.oid
					JOIN activite[PX].t_services_facturation ON t_lieux.service_facturation_id = t_services_facturation.oid AND 
						avec_facturation_intermediaire IS DISTINCT FROM '1'
				WHERE p_factures.no_sejour = p_sejours.no_sejour AND
					CASE WHEN code_sorti = '1' THEN date_sortie ELSE date(now()) END - date_entree < 30 AND
					(
					p_factures.date_debut <> date_entree OR
					p_factures.date_fin <> CASE WHEN code_sorti = '1' THEN date_sortie ELSE date_fin END
					);
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes non facturées Clinique">
			<sqlcmd><![CDATA[

				
				
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures Clinique">
			<sqlcmd><![CDATA[
			
				DROP SEQUENCE IF EXISTS w_factures_lignes_c_seq;
				CREATE TEMP SEQUENCE w_factures_lignes_c_seq;
			
				DROP TABLE IF EXISTS w_factures_lignes_c;
				CREATE TEMP TABLE w_factures_lignes_c AS 
				SELECT
					nextval('w_factures_lignes_c_seq'::regclass) AS sequence, 
					w_factures.no_sejour,
					w_factures.oid AS facture_id,
					w_factures.no_facture AS no_facture,
					w_factures.date_vente,

					w_factures.FAAVFA,
					
					1::numeric AS coefficient,
					CASE WHEN MCO > 0 THEN MCO ELSE 1 END AS coefficient_mco,
					t_rubriques_facturation.oid AS rubrique_facturation_id,
					t_rubriques_facturation.oid AS rubrique_comptabilisation_id,
					t_prestations.oid AS prestation_id,
					t_prestations.code AS prestation_code,
					t_prestations.type_ventilation_jour AS prestation_type_ventilation_jour,
					COALESCE(t_compte.oid,0) AS compte_produit_id,
					p_sejours.lieu_sortie_id AS lieu_id,
					1::bigint AS origine_facturation_id,

					SFTTC AS prix_unitaire,
					0::numeric AS taux_0,
					SFTXSS AS taux_1,
					0::numeric  AS taux_2,
					0::numeric AS taux_22,

					SFDTEA AS date_debut,
					SFDTSA AS date_fin,
					CASE WHEN SFCOEF <> 0 THEN SFCOEF ELSE SFQTE END AS nb_rubrique,
					CASE WHEN SFCOEF <> 0 THEN SFCOEF ELSE SFQTE END AS nb_prestation,
					SFTTTO AS montant_facture,
					SFTTHO AS montant_facture_0,
					SFTTSS AS montant_facture_1,
					SFTTMU AS montant_facture_2,
					0::numeric AS montant_facture_22,
					SFTTTO AS montant_comptabilise,
					SFTTHO AS montant_comptabilise_0,
					SFTTSS AS montant_comptabilise_1,
					SFTTMU AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22,
					0::numeric AS rubrique_facture_id
				FROM prod_chataigneraie_as400.NXXXFAC_NELFTARC NELFTARC
					JOIN w_factures ON w_factures.FAID = NELFTARC.FAID
					JOIN activite[PX].p_sejours ON w_factures.no_sejour = p_sejours.no_sejour
					JOIN prod_chataigneraie_as400.NXXXFAC_BFARCLI BFARCLI ON BFARCLI.ARIID = NELFTARC.ARIID
					JOIN prod_chataigneraie_as400.NXXXFAC_BFAB2 BFAB2 ON BFAB2.AB2ID = BFARCLI.AB2ID
					LEFT JOIN prod_chataigneraie_as400.NXXXFAC_BFCENT BFCENT ON
						BFCENT.ARIID = NELFTARC.ARIID AND
						BFCENT.CATID = NELFTARC.SFCATID AND
						BFCENT.SCAID = NELFTARC.SFSCAID AND
						BFCENT.BFTXID = NELFTARC.BFTXID
					JOIN activite[PX].t_rubriques_facturation ON NELFTARC.ARIID = t_rubriques_facturation.code_original 
					JOIN activite.t_prestations ON BFAB2.AB2OF = t_prestations.code
					LEFT JOIN activite[PX].t_compte ON BFCENT.CPTID = t_compte.code
				;


				DROP TABLE IF EXISTS w_BFTYMED;
				CREATE TEMP TABLE w_BFTYMED AS
				SELECT TYLFMED,
					TYLFMED||'M' AS rubrique_code_original,
					0::bigint AS rubrique_facturation_id,
					''::text AS rubrique_facturation_code,
					COALESCE(AB2OF,
						CASE 
						WHEN TYLFLI ILIKE '%proth%' THEN 'PII' 
						WHEN TYLFLI ILIKE '%cament%' THEN 'PH8' 
						ELSE 'PII' END) AS prestation_code_original,
					0::bigint AS prestation_id,
					''::text AS prestation_code
				FROM prod_chataigneraie_as400.NXXXFAC_BFTYMED BFTYMED
					LEFT JOIN prod_chataigneraie_as400.NXXXFAC_BFAB2 BFAB2 ON BFAB2.AB2ID = BFTYMED.AB2ID
				;

				UPDATE w_BFTYMED
				SET prestation_id = t_prestations.oid, 
					prestation_code = t_prestations.code
				FROM activite.t_prestations
				WHERE prestation_code_original = code;

				UPDATE w_BFTYMED
				SET rubrique_facturation_id = t_rubriques_facturation.oid,
					rubrique_facturation_code = t_rubriques_facturation.code
				FROM activite[PX].t_rubriques_facturation
				WHERE rubrique_code_original = code_original;


				INSERT INTO w_factures_lignes_c
				SELECT
					nextval('w_factures_lignes_c_seq'::regclass) AS sequence, 
					w_factures.no_sejour,
					w_factures.oid AS facture_id,
					w_factures.no_facture AS no_facture,
					w_factures.date_vente,

					w_factures.FAAVFA,
					
					1::numeric AS coefficient,
					1::numeric AS coefficient_mco,
					w_BFTYMED.rubrique_facturation_id,
					w_BFTYMED.rubrique_facturation_id AS rubrique_comptabilisation_id,
					w_BFTYMED.prestation_id,
					w_BFTYMED.prestation_code,
					'0'::text AS prestation_type_ventilation_jour,
					0::bigint AS compte_produit_id,
					p_sejours.lieu_sortie_id AS lieu_id,
					1::bigint AS origine_facturation_id,

					SMPUTC AS prix_unitaire,
					0::numeric AS taux_0,
					SMTXSS AS taux_1,
					0::numeric  AS taux_2,
					0::numeric AS taux_22,

					SMDINA AS date_debut,
					SMDINA AS date_fin,
					CASE WHEN SMCOEF <> 0 THEN SMCOEF ELSE SMQTE END AS nb_rubrique,
					CASE WHEN SMCOEF <> 0 THEN SMCOEF ELSE SMQTE END AS nb_prestation,
					SMTTTO AS montant_facture,
					SMTTHO AS montant_facture_0,
					SMTTSS AS montant_facture_1,
					SMTTMU AS montant_facture_2,
					0::numeric AS montant_facture_22,
					CASE WHEN NELFMEDI.TYLFMED NOT IN (20,40) THEN SMTTTO ELSE 0 END AS montant_comptabilise,
					CASE WHEN NELFMEDI.TYLFMED NOT IN (20,40) THEN SMTTHO ELSE 0 END AS montant_comptabilise_0,
					CASE WHEN NELFMEDI.TYLFMED NOT IN (20,40) THEN SMTTSS ELSE 0 END AS montant_comptabilise_1,
					CASE WHEN NELFMEDI.TYLFMED NOT IN (20,40) THEN SMTTMU ELSE 0 END AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22,
					0::numeric AS rubrique_facture_id
				FROM prod_chataigneraie_as400.NXXXFAC_NELFMEDI NELFMEDI
					JOIN w_factures ON w_factures.FAID = NELFMEDI.FAID
					JOIN activite[PX].p_sejours ON w_factures.no_sejour = p_sejours.no_sejour
					JOIN w_BFTYMED ON w_BFTYMED.TYLFMED = NELFMEDI.TYLFMED
				;
				  
				CREATE INDEX w_factures_lignes_c_i1
				  ON w_factures_lignes_c
				  USING btree
				  (sequence);
				  
				CREATE INDEX w_factures_lignes_c_i2
				  ON w_factures_lignes_c
				  USING btree
				  (prestation_id);


				-- Eclatement des supplements par jour
				DROP TABLE IF EXISTS w_factures_lignes_sup_c;

				CREATE TEMP TABLE w_factures_lignes_sup_c AS
				SELECT
					date(p_sejours.date_entree) AS date_debut_ghs,
					date(p_sejours.date_sortie - interval '1 day') AS date_fin_ghs,
					(date(date_sortie) - date(date_entree)) * CASE WHEN nb_prestation < 0 THEN -1 ELSE 1 END AS nb_prestation_ghs,
					CASE WHEN prestation_code = 'GHS' THEN date(date_entree + interval '1 day') ELSE date(date_debut + interval '1 day') END AS date_debut_det,
					CASE WHEN prestation_code = 'GHS' THEN date(p_sejours.date_sortie - interval '1 day') ELSE date(date_debut + ((ABS(nb_prestation)-1)|| ' days')::interval)  END AS date_fin_det,
					ABS(CASE WHEN prestation_code = 'GHS' THEN date(date_sortie) - date(date_entree) ELSE nb_prestation END) AS nb_det,
					w_factures_lignes_c.*
				FROM w_factures_lignes_c
					JOIN activite[PX].p_sejours ON w_factures_lignes_c.no_sejour = p_sejours.no_sejour
				WHERE 
					(
						prestation_code IN ('PJ', 'FJ', 'SSM', 'GHS', 'EXH', 'SRC', 'REA', 'STF', 'NN1', 'NN2', 'NN3', 'CP', 'CPC', 'SHO') OR
						prestation_type_ventilation_jour = '1'
					)
						AND
					(
					(nb_prestation > 1 or nb_prestation < -1 OR 
					(prestation_code = 'GHS' AND date(date_sortie) - date(date_entree) > 1))
					) ;

				
				UPDATE w_factures_lignes_sup_c SET 
					date_fin_ghs = date(date_debut_exh - interval '1 day'),
					date_fin_det = date(date_debut_exh - interval '1 day'),
					nb_prestation_ghs = date(date_debut_exh - interval '1 day') - date_debut_ghs + 1,
					nb_det = date(date_debut_exh - interval '1 day') - date_debut_ghs + 1
				FROM (
					SELECT no_facture, date(MIN(date_debut)) AS date_debut_exh
					FROM w_factures_lignes_c
						JOIN activite.t_prestations ON prestation_id = t_prestations.oid AND code = 'EXH'
					GROUP BY 1) subview
				WHERE w_factures_lignes_sup_c.no_facture = subview.no_facture AND
					w_factures_lignes_sup_c.prestation_code = 'GHS';

				CREATE INDEX w_factures_lignes_sup_c_i1
				  ON w_factures_lignes_sup_c
				  USING btree
				  (date_debut);

				DROP TABLE IF EXISTS w_factures_lignes_sup_c_sup;

				CREATE TEMP TABLE w_factures_lignes_sup_c_sup AS
				SELECT
					sequence, 
					no_sejour,
					facture_id,
					no_facture,
					date_vente,

					FAAVFA,
					
					coefficient,
					coefficient_mco,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					prestation_code,
					prestation_type_ventilation_jour,
					compte_produit_id,
					lieu_id,
					origine_facturation_id,

					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,

					date AS date_debut,
					date AS date_fin,
					CASE WHEN prestation_code = 'GHS' AND date <> date_fin_ghs THEN 0 WHEN nb_prestation < 0 THEN -1 ELSE 1 END AS nb_rubrique,
					CASE WHEN prestation_code = 'GHS' AND date <> date_fin_ghs THEN 0 WHEN nb_prestation < 0 THEN -1 ELSE 1 END AS nb_prestation,
					round(montant_facture / ABS(nb_det),2) AS montant_facture,
					round(montant_facture_0 / ABS(nb_det),2) AS montant_facture_0,
					round(montant_facture_1 / ABS(nb_det),2) AS montant_facture_1,
					round(montant_facture_2 / ABS(nb_det),2) AS montant_facture_2,
					round(montant_facture_22 / ABS(nb_det),2) AS montant_facture_22,
					round(montant_comptabilise / ABS(nb_det),2) AS montant_comptabilise,
					round(montant_comptabilise_0 / ABS(nb_det),2) AS montant_comptabilise_0,
					round(montant_comptabilise_1 / ABS(nb_det),2) AS montant_comptabilise_1,
					round(montant_comptabilise_2 / ABS(nb_det),2) AS montant_comptabilise_2,
					round(montant_comptabilise_22 / ABS(nb_det),2) AS montant_comptabilise_22,
					rubrique_facture_id
				FROM w_factures_lignes_sup_c
					JOIN base.p_calendrier ON p_calendrier.date BETWEEN date_debut_det AND date_fin_det 
				;

				DROP TABLE IF EXISTS w_factures_lignes_sup_c_sup_tot;

				CREATE TEMP TABLE w_factures_lignes_sup_c_sup_tot AS

				SELECT sequence,
					SUM(nb_rubrique) AS nb_rubrique,
					SUM(nb_prestation) AS nb_prestation,
					SUM(montant_facture) AS montant_facture,
					SUM(montant_facture_0) AS montant_facture_0,
					SUM(montant_facture_1) AS montant_facture_1,
					SUM(montant_facture_2) AS montant_facture_2,
					SUM(montant_facture_22) AS montant_facture_22,
					SUM(montant_comptabilise) AS montant_comptabilise,
					SUM(montant_comptabilise_0) AS montant_comptabilise_0,
					SUM(montant_comptabilise_1) AS montant_comptabilise_1,
					SUM(montant_comptabilise_2) AS montant_comptabilise_2,
					SUM(montant_comptabilise_22) AS montant_comptabilise_22
				FROM w_factures_lignes_sup_c_sup
				GROUP BY 1;


				UPDATE w_factures_lignes_c
				SET
					date_fin = w_factures_lignes_c.date_debut,
					nb_rubrique = w_factures_lignes_c.nb_rubrique - w_factures_lignes_sup_c_sup_tot.nb_rubrique,
					nb_prestation = w_factures_lignes_c.nb_prestation - w_factures_lignes_sup_c_sup_tot.nb_prestation,
					montant_facture = w_factures_lignes_c.montant_facture - w_factures_lignes_sup_c_sup_tot.montant_facture,
					montant_facture_0 = w_factures_lignes_c.montant_facture_0 - w_factures_lignes_sup_c_sup_tot.montant_facture_0,
					montant_facture_1 = w_factures_lignes_c.montant_facture_1 - w_factures_lignes_sup_c_sup_tot.montant_facture_1,
					montant_facture_2 = w_factures_lignes_c.montant_facture_2 - w_factures_lignes_sup_c_sup_tot.montant_facture_2,
					montant_facture_22 = w_factures_lignes_c.montant_facture_22 - w_factures_lignes_sup_c_sup_tot.montant_facture_22,
					montant_comptabilise = w_factures_lignes_c.montant_comptabilise - w_factures_lignes_sup_c_sup_tot.montant_comptabilise,
					montant_comptabilise_0 = w_factures_lignes_c.montant_comptabilise_0 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_0,
					montant_comptabilise_1 = w_factures_lignes_c.montant_comptabilise_1 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_1,
					montant_comptabilise_2 = w_factures_lignes_c.montant_comptabilise_2 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_2,
					montant_comptabilise_22 = w_factures_lignes_c.montant_comptabilise_22 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_22
				FROM w_factures_lignes_sup_c_sup_tot
				WHERE w_factures_lignes_c.sequence = w_factures_lignes_sup_c_sup_tot.sequence;
				
				
		
				-- Ajout des lignes
				INSERT INTO w_factures_lignes_c(
					sequence, 
					no_sejour,
					facture_id,
					no_facture,
					date_vente,

					FAAVFA,
					
					coefficient,
					coefficient_mco,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					prestation_code,
					prestation_type_ventilation_jour,
					compte_produit_id,
					lieu_id,
					origine_facturation_id,

					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,

					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					rubrique_facture_id)
				SELECT 
					sequence, 
					no_sejour,
					facture_id,
					no_facture,
					date_vente,

					FAAVFA,
					
					coefficient,
					coefficient_mco,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					prestation_code,
					prestation_type_ventilation_jour,
					compte_produit_id,
					lieu_id,
					origine_facturation_id,

					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,

					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					rubrique_facture_id
				FROM w_factures_lignes_sup_c_sup;
				
				-- Inversion signe pour avoirs
				UPDATE w_factures_lignes_c SET
					nb_rubrique = 0-nb_rubrique,
					nb_prestation = 0-nb_prestation,
					montant_facture = 0-montant_facture,
					montant_facture_0 = 0-montant_facture_0,
					montant_facture_1 = 0-montant_facture_1,
					montant_facture_2 = 0-montant_facture_2,
					montant_facture_22 = 0-montant_facture_22,
					montant_comptabilise = 0-montant_comptabilise,
					montant_comptabilise_0 = 0-montant_comptabilise_0,
					montant_comptabilise_1 = 0-montant_comptabilise_1,
					montant_comptabilise_2 = 0-montant_comptabilise_2,
					montant_comptabilise_22 = 0-montant_comptabilise_22
				WHERE FAAVFA = 'A'
				;
				
				
				-- Lieu exécution
				
				
				UPDATE w_factures_lignes_c
				SET lieu_id = p_mouvements_sejour.lieu_id
				FROM 
					activite[PX].p_mouvements_sejour
				WHERE
					w_factures_lignes_c.no_sejour = p_mouvements_sejour.no_sejour AND
					w_factures_lignes_c.date_fin = p_mouvements_sejour.date AND
					(230000 BETWEEN p_mouvements_sejour.heure_debut AND p_mouvements_sejour.heure_fin OR 
					nb_entrees_directes = 1 AND heure_fin = 240000 OR 
					nb_sorties_directes = 1 ) AND
					w_factures_lignes_c.lieu_id <> p_mouvements_sejour.lieu_id;
					
				-- Rubriques internes cti
				SELECT activite[PX].cti_reorganize_rubrique_facture_c('w_factures_lignes_c');	
				  



				-- Validation des lignes de facture  
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_1');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_2');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_3');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_4');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_5');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_6');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_7');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_8');

				INSERT INTO activite[PX].p_factures_lignes_c (
					facture_id,
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					rubrique_facture_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					compte_produit_id,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					lieu_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					montant_encours,
					montant_encours_0,
					montant_encours_1,
					montant_encours_2,
					montant_encours_22,
					montant_facture_actes_inclus_dans_sejour,
					montant_facture_0_actes_inclus_dans_sejour,
					montant_facture_1_actes_inclus_dans_sejour,
					montant_facture_2_actes_inclus_dans_sejour,
					montant_facture_22_actes_inclus_dans_sejour,
					montant_comptabilise_budget_global_1,
					origine_facturation_id
				)
				SELECT
					facture_id,
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					rubrique_facture_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					compte_produit_id,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					lieu_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					0::numeric AS montant_encours,
					0::numeric AS montant_encours_0,
					0::numeric AS montant_encours_1,
					0::numeric AS montant_encours_2,
					0::numeric AS montant_encours_22,
					0::numeric AS montant_facture_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_0_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_1_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_2_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_22_actes_inclus_dans_sejour,
					0::numeric AS montant_comptabilise_budget_global_1,
					origine_facturation_id
				FROM w_factures_lignes_c
				WHERE montant_facture <> 0;
				
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_1');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_2');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_3');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_4');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_5');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_6');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_7');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_8');
										
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Compte client Clinique">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_1');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_2');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_3');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_4');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_8');
				
				
				INSERT INTO activite[PX].p_factures_soldes_c(
					facture_id,
					no_facture, 
					date_comptable, 
					rubrique_comptabilisee_id, 
					prestation_id, 
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2, 
					montant_comptabilise_22, 
					montant_regle, 
					montant_regle_0, 
					montant_regle_1, 
					montant_regle_2, 
					montant_regle_22
				)
				SELECT
					facture_id,
					no_facture,
					date_trunc('month',date_vente) + interval '1 month' - interval '1 day',
					rubrique_comptabilisation_id,
					prestation_id,
					SUM(montant_comptabilise),
					SUM(montant_comptabilise_0),
					SUM(montant_comptabilise_1),
					SUM(montant_comptabilise_2),
					SUM(montant_comptabilise_22),
					0,
					0,
					0,
					0,
					0
				FROM w_factures_lignes_c
				WHERE date_vente >= '20120101' AND
					date_vente IS NOT NULL  AND
					(montant_comptabilise <> 0 OR 
					montant_comptabilise_0 <> 0 OR 
					montant_comptabilise_1 <> 0 OR 
					montant_comptabilise_2 <> 0 OR 
					montant_comptabilise_22 <> 0 
					)
				GROUP BY 1,2,3,4,5;


				INSERT INTO activite[PX].p_factures_soldes_c(
					facture_id,
					no_facture, 
					date_comptable, 
					rubrique_comptabilisee_id, 
					prestation_id, 
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2, 
					montant_comptabilise_22, 
					montant_regle, 
					montant_regle_0, 
					montant_regle_1, 
					montant_regle_2, 
					montant_regle_22
				)
				SELECT
					w_factures.oid AS facture_id,
					no_facture,
					(to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||'01')::date,
					0::bigint AS rubrique_comptabilisation_id,
					0::bigint AS prestation_id,
					0,
					0,
					0,
					0,
					0,
					SUM(0-ACMTFC) AS montant_regle,
					SUM(CASE WHEN ACINDI = 3 THEN 0-ACMTFC ELSE 0 END) AS montant_regle_0,
					SUM(CASE WHEN ACINDI = 1 THEN 0-ACMTFC ELSE 0 END) AS montant_regle_1,
					SUM(CASE WHEN ACINDI = 2 THEN 0-ACMTFC ELSE 0 END) AS montant_regle_2,
					0 AS montant_regle_22
				FROM prod_chataigneraie_as400.NXXXFAC_FACTURES FACTURES
				JOIN w_NDOADM_SELECT ON w_NDOADM_SELECT.NONUMA = FACTURES.NONUMA
				JOIN prod_chataigneraie_as400.XXXFICH_REGLI REGLI ON FACTURES.FAID = REGLI.ACNFAC
				JOIN w_factures ON REGLI.ACNFAC = w_factures.FAID
				WHERE ACSDTF*100+ACADTF >= 2012 AND
					ACID < 100
				GROUP BY 1,2,3,4,5
				;
				
				
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_1');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_2');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_3');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_4');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_8');
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Lignes non facturées Honoraires">
			<sqlcmd><![CDATA[

						
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures Honoraires">
			<sqlcmd><![CDATA[
		
				DROP TABLE IF EXISTS w_factures_lignes_h;
				CREATE TEMP TABLE w_factures_lignes_h AS 
				SELECT
					w_factures.no_sejour,
					w_factures.oid AS facture_id,
					w_factures.no_facture AS no_facture,
					w_factures.date_vente,

					w_factures.FAAVFA,
					
					SHCOEF AS coefficient,
					1::numeric AS coefficient_mco,
					t_rubriques_facturation.oid AS rubrique_facturation_id,
					t_rubriques_facturation.oid AS rubrique_comptabilisation_id,
					t_prestations.oid AS prestation_id,
					t_prestations.code AS prestation_code,
					t_medecins_administratifs.oid AS medecin_executant_id,
					t_medecins_administratifs.oid AS medecin_facture_id,
					t_medecins_administratifs.oid AS medecin_comptabilise_id,
					p_sejours.lieu_sortie_id AS lieu_id,
					1::bigint AS origine_facturation_id,

					SHTTTO AS prix_unitaire,
					0::numeric AS taux_0,
					SHTXSS AS taux_1,
					0::numeric  AS taux_2,
					0::numeric AS taux_22,

					SHDINA AS date_debut,
					SHDINA AS date_fin,
					SHQTAC AS nb_rubrique,
					SHQTAC AS nb_prestation,
					SHTTTO AS montant_facture,
					SHTTHO AS montant_facture_0,
					SHTTSS AS montant_facture_1,
					SHTTMU AS montant_facture_2,
					0::numeric AS montant_facture_22,
					SHTTTO AS montant_comptabilise,
					SHTTHO AS montant_comptabilise_0,
					SHTTSS AS montant_comptabilise_1,
					SHTTMU AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22,
					SHDPTO AS montant_depassement,
					0::bigint AS acte_id,	
					''::text AS activite_ccam,
					''::text AS extension_ccam,
					''::text AS phase_ccam,
					''::text AS modificateur_ccam_1,
					''::text AS modificateur_ccam_2,
					''::text AS modificateur_ccam_3,
					''::text AS modificateur_ccam_4
				FROM prod_chataigneraie_as400.NXXXFAC_NELFHONO NELFHONO
					JOIN w_factures ON w_factures.FAID = NELFHONO.FAID
					JOIN activite[PX].p_sejours ON w_factures.no_sejour = p_sejours.no_sejour
					JOIN prod_chataigneraie_as400.NXXXFAC_BFHONO BFHONO ON BFHONO.AHID = NELFHONO.AHID
					JOIN activite[PX].t_rubriques_facturation ON (BFHONO.AHID||'H') = t_rubriques_facturation.code_original 
					JOIN activite.t_prestations ON BFHONO.AHLOF = t_prestations.code
					LEFT JOIN activite[PX].t_medecins_administratifs ON SHPNUAX = t_medecins_administratifs.code_original
					LEFT JOIN base.t_actes ON BFHONO.AHLOF = t_actes.code
				;


				INSERT INTO w_factures_lignes_h
				SELECT
					w_factures.no_sejour,
					w_factures.oid AS facture_id,
					w_factures.no_facture AS no_facture,
					w_factures.date_vente,

					w_factures.FAAVFA,
					
					1::numeric AS coefficient,
					1::numeric AS coefficient_mco,
					t_rubriques_facturation.oid AS rubrique_facturation_id,
					t_rubriques_facturation.oid AS rubrique_comptabilisation_id,
					t_prestations.oid AS prestation_id,
					t_prestations.code AS prestation_code,
					t_medecins_administratifs.oid AS medecin_executant_id,
					t_medecins_administratifs.oid AS medecin_facture_id,
					t_medecins_administratifs.oid AS medecin_comptabilise_id,
					p_sejours.lieu_sortie_id AS lieu_id,
					1::bigint AS origine_facturation_id,

					SATTTO AS prix_unitaire,
					0::numeric AS taux_0,
					SATXSS AS taux_1,
					0::numeric  AS taux_2,
					0::numeric AS taux_22,

					SADINA AS date_debut,
					SADINA AS date_fin,
					BAQTAC AS nb_rubrique,
					BAQTAC AS nb_prestation,
					SATTTO AS montant_facture,
					SATTHO AS montant_facture_0,
					SATTSS AS montant_facture_1,
					SATTMU AS montant_facture_2,
					0::numeric AS montant_facture_22,
					SATTTO AS montant_comptabilise,
					SATTHO AS montant_comptabilise_0,
					SATTSS AS montant_comptabilise_1,
					SATTMU AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22,
					SADPTO AS montant_depassement,
					COALESCE(t_actes.oid,0) AS acte_id,	
					BACACTIV AS activite_ccam,
					BACEXTEN AS extension_ccam,
					BACPHASE AS phase_ccam,
					''::text AS modificateur_ccam_1,
					''::text AS modificateur_ccam_2,
					''::text AS modificateur_ccam_3,
					''::text AS modificateur_ccam_4
				FROM prod_chataigneraie_mysql.STDEV_NELFHONOC NELFHONOC
					JOIN w_NDOADM_SELECT ON NELFHONOC.SENUM = w_NDOADM_SELECT.NONUMA AND NELFHONOC.IDETAB = w_NDOADM_SELECT.IDETAB
					JOIN w_factures ON w_factures.FAID = NELFHONOC.FAID
					JOIN activite[PX].p_sejours ON w_factures.no_sejour = p_sejours.no_sejour
					JOIN activite[PX].t_rubriques_facturation ON (BAREGROU||'-C') = t_rubriques_facturation.code_original 
					JOIN activite.t_prestations ON BAREGROU = t_prestations.code
					LEFT JOIN activite[PX].t_medecins_administratifs ON BAPNUAXD = t_medecins_administratifs.code_original
					LEFT JOIN base.t_actes ON BACATA = t_actes.code
				;


				-- Inversion signe pour avoirs
				UPDATE w_factures_lignes_h SET
					nb_rubrique = 0-nb_rubrique,
					nb_prestation = 0-nb_prestation,
					montant_facture = 0-montant_facture,
					montant_facture_0 = 0-montant_facture_0,
					montant_facture_1 = 0-montant_facture_1,
					montant_facture_2 = 0-montant_facture_2,
					montant_facture_22 = 0-montant_facture_22,
					montant_comptabilise = 0-montant_comptabilise,
					montant_comptabilise_0 = 0-montant_comptabilise_0,
					montant_comptabilise_1 = 0-montant_comptabilise_1,
					montant_comptabilise_2 = 0-montant_comptabilise_2,
					montant_comptabilise_22 = 0-montant_comptabilise_22
				WHERE FAAVFA = 'A'
				;
				

				CREATE INDEX w_factures_lignes_h_i1
				  ON w_factures_lignes_h
				  USING btree
				  (no_sejour);

				UPDATE w_factures_lignes_h
				SET lieu_id = p_mouvements_sejour.lieu_id
				FROM 
					activite[PX].p_mouvements_sejour
				WHERE
					w_factures_lignes_h.no_sejour = p_mouvements_sejour.no_sejour AND
					w_factures_lignes_h.date_fin = p_mouvements_sejour.date AND
					(230000 BETWEEN p_mouvements_sejour.heure_debut AND p_mouvements_sejour.heure_fin OR 
					nb_entrees_directes = 1 AND heure_fin = 240000 OR 
					nb_sorties_directes = 1 ) AND
					w_factures_lignes_h.lieu_id <> p_mouvements_sejour.lieu_id;



				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_1');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_2');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_3');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_4');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_5');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_6');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_7');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_8');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_9');
				

				
				INSERT INTO activite[PX].p_factures_lignes_h (
					facture_id,
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					medecin_facture_id,
					medecin_comptabilise_id,
					prestation_id,
					rubrique_facturation_id,
					acte_id,	
					activite_ccam,
					extension_ccam,
					phase_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					montant_depassement,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					lieu_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22
					)
				SELECT
					facture_id,
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					medecin_facture_id,
					medecin_comptabilise_id,
					prestation_id,
					rubrique_facturation_id,
					acte_id,	
					activite_ccam,
					extension_ccam,
					phase_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					montant_depassement,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					lieu_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22
					
				FROM w_factures_lignes_h;
				
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_1');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_2');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_3');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_4');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_5');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_6');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_7');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_8');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_9');
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Compte client Honoraires">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_h_1');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_h_3');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_h_4');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_h_8');
			
				INSERT INTO activite[PX].p_factures_soldes_h(
					facture_id,
					no_facture, 
					date_comptable, 
					medecin_comptabilise_id, 
					prestation_id,
					rubrique_comptabilisee_id,
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2, 
					montant_comptabilise_22, 
					montant_regle, 
					montant_regle_0, 
					montant_regle_1, 
					montant_regle_2, 
					montant_regle_22
				)
				SELECT
					facture_id,
					no_facture,
					date_trunc('month',date_vente) + interval '1 month' - interval '1 day',
					medecin_comptabilise_id,
					prestation_id,
					rubrique_facturation_id,
					SUM(montant_comptabilise),
					SUM(montant_comptabilise_0),
					SUM(montant_comptabilise_1),
					SUM(montant_comptabilise_2),
					SUM(montant_comptabilise_22),
					0,
					0,
					0,
					0,
					0
				FROM w_factures_lignes_h
				WHERE date_vente >= '20120101' AND
					(montant_comptabilise <> 0 OR 
					montant_comptabilise_0 <> 0 OR 
					montant_comptabilise_1 <> 0 OR 
					montant_comptabilise_2 <> 0 OR 
					montant_comptabilise_22 <> 0 
					)
				GROUP BY 1,2,3,4,5,6;

				DROP TABLE IF EXISTS w_medecins_3;
				CREATE TEMP TABLE w_medecins_3 AS
				SELECT
					base.cti_to_number(right(code_original,3)) AS code_3,
					MAX(oid) AS medecin_administratif_id
				FROM activite[PX].t_medecins_administratifs
				GROUP BY 1;


				INSERT INTO activite[PX].p_factures_soldes_h(
					facture_id,
					no_facture, 
					date_comptable, 
					medecin_comptabilise_id, 
					prestation_id,
					rubrique_comptabilisee_id,
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2, 
					montant_comptabilise_22, 
					montant_regle, 
					montant_regle_0, 
					montant_regle_1, 
					montant_regle_2, 
					montant_regle_22
				)
				SELECT
					w_factures.oid AS facture_id,
					no_facture,
					(to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||'01')::date,
					COALESCE(w_medecins_3.medecin_administratif_id,0) AS medecin_comptabilise_id, 
					0::bigint AS rubrique_comptabilisation_id,
					0::bigint AS prestation_id,
					0,
					0,
					0,
					0,
					0,
					SUM(0-ACMTFC) AS montant_regle,
					SUM(CASE WHEN ACINDI = 3 THEN 0-ACMTFC ELSE 0 END) AS montant_regle_0,
					SUM(CASE WHEN ACINDI = 1 THEN 0-ACMTFC ELSE 0 END) AS montant_regle_1,
					SUM(CASE WHEN ACINDI = 2 THEN 0-ACMTFC ELSE 0 END) AS montant_regle_2,
					0 AS montant_regle_22
				FROM prod_chataigneraie_as400.NXXXFAC_FACTURES FACTURES
				JOIN w_NDOADM_SELECT ON w_NDOADM_SELECT.NONUMA = FACTURES.NONUMA
				JOIN prod_chataigneraie_as400.XXXFICH_REGLI REGLI ON FACTURES.FAID = REGLI.ACNFAC
				JOIN w_factures ON REGLI.ACNFAC = w_factures.FAID
				LEFT JOIN w_medecins_3 ON w_medecins_3.code_3 = ACMED
				WHERE ACSDTF*100+ACADTF >= 2012 AND
					ACID > 100
				GROUP BY 1,2,3,4,5,6
				;
				
				
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_h_1');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_h_3');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_h_4');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_h_8');
						
										
			]]></sqlcmd>
		</NODE>
	
	</NODE>
	
	<NODE name="CHIFFRIER" label="CREATION DU CHIFFRIER">
		
		<NODE label="Récupération chiffrier">
			<sqlcmd><![CDATA[

				DROP TABLE IF EXISTS w_factures_comptables;
				
				CREATE TEMP TABLE w_factures_comptables AS
				SELECT 
					FAID::text AS no_facture,
					date_trunc('month', FADAT) + interval '1 month' - interval '1 day' AS date_comptable,
					SUM(CASE WHEN FAAVFA <> 'A' THEN FATTCL ELSE 0-FATTCL END) AS montant_ventes_c, 
					0::numeric AS montant_reglements_c, 
					SUM(CASE WHEN FAAVFA <> 'A' THEN FAHOTT ELSE 0-FAHOTT END) AS montant_ventes_h, 
					0::numeric AS montant_reglements_h
				FROM prod_chataigneraie_as400.NXXXFAC_FACTURES FACTURES
				WHERE FADAT >= '20120101' 
				GROUP BY 1,2;
				
				
				INSERT INTO w_factures_comptables
				SELECT 
					ACNFAC::text AS no_facture,
					(to_char(ACSDTF,'FM00')||to_char(ACADTF,'FM00')||to_char(ACMDTF,'FM00')||'01')::date AS date_comptable,
					0::numeric AS montant_ventes_c, 
					SUM(CASE WHEN ACID < 100 THEN 0-ACMTFC ELSE 0 END) AS montant_reglements_c, 
					0::numeric AS montant_ventes_h, 
					SUM(CASE WHEN ACID > 100 THEN 0-ACMTFC ELSE 0 END) AS montant_reglements_h
				FROM prod_chataigneraie_as400.XXXFICH_REGLI REGLI
				WHERE ACSDTF*100+ACADTF >= 2012
				GROUP BY 1,2
				;

				
				TRUNCATE activite[PX].p_chiffrier_comptable;

				INSERT INTO activite[PX].p_chiffrier_comptable(
					date_comptable, 
					montant_ventes_c, 
					montant_reglements_c, 
					montant_solde_client_c, 
					montant_ventes_h, 
					montant_reglements_h, 
					montant_solde_client_h)
				SELECT 
					date_comptable, 
					SUM(montant_ventes_c), 
					SUM(montant_reglements_c), 
					0 AS montant_solde_client_c,
					SUM(montant_ventes_h), 
					SUM(montant_reglements_h),
					0 AS montant_solde_client_h
				FROM w_factures_comptables
				WHERE date_comptable >= '20120101'
				GROUP BY 1;
				
				TRUNCATE activite[PX].p_factures_comptables;

				INSERT INTO activite[PX].p_factures_comptables(
					facture_id,
					no_facture,
					date_comptable, 
					montant_ventes_c, 
					montant_reglements_c, 
					montant_ventes_h, 
					montant_reglements_h)

				SELECT 
					0::bigint AS facture_id,
					no_facture,
					date_comptable, 
					SUM(montant_ventes_c), 
					SUM(montant_reglements_c), 
					SUM(montant_ventes_h), 
					SUM(montant_reglements_h)
				FROM w_factures_comptables
				WHERE date_comptable >= '20120101' AND
					no_facture IS NOT NULL 
				GROUP BY 1,2,3;

			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">
	
		<NODE label="Type d'environnement">
			<sqlcmd><![CDATA[
				CTISELECT_PROPERTY_READ 'ENV_WITHPMSI', MAX(CASE WHEN schemaname = 'pmsi' AND tablename = 'p_rss' THEN 1 ELSE 0 END) 
				FROM pg_tables 
				WHERE schemaname = 'pmsi' AND tablename = 'p_rss'
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Tiers payant">
			<sqlcmd><![CDATA[

				INSERT INTO activite[PX].t_types_tiers_payant(code, texte, texte_court)
				SELECT code, texte, texte FROM
				(
				SELECT '0' AS code, 'Patients' AS texte
				UNION ALL
				SELECT '1','Régime général'
				UNION ALL
				SELECT '2','Mutuelles'
				) subview
				WHERE code NOT IN (SELECT code FROM activite[PX].t_types_tiers_payant);

				INSERT INTO activite[PX].t_tiers_payant(oid, code, code_original, type_tiers_payant, texte, texte_court) 
				SELECT 0, '', '0', '0', 'Non saisi', 'Non saisi'
				WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_tiers_payant);

				INSERT INTO activite[PX].t_tiers_payant(code, code_original, type_tiers_payant, texte, texte_court) 
				SELECT 'PATIENT', 'PATIENT', '0', 'PATIENTS', 'PATIENTS'
				WHERE 'PATIENT' NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant WHERE code_original IS NOT NULL);

				INSERT INTO activite[PX].t_tiers_payant(code_original, code, type_tiers_payant, texte, texte_court) 
				SELECT 'C'||NOCAIS, 'C'||NOCAIS, 
					'1', 
					MAX(SSCAI), MAX(SSCAI)
				FROM prod_chataigneraie_as400.NXXXFAC_BFCAIS
				WHERE ('C'||NOCAIS) NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant WHERE code_original IS NOT NULL)
				GROUP BY 1,2;

				UPDATE activite[PX].t_tiers_payant
				SET grand_regime_id = t_grands_regimes.oid
				FROM prod_chataigneraie_as400.NXXXFAC_BFCAIS BFCAIS
				JOIN prod_chataigneraie_as400.NXXXFAC_BFREG BFREG ON BFCAIS.RGID = BFREG.RGID
				JOIN base.t_grands_regimes ON RGCDSS = t_grands_regimes.code AND
					t_grands_regimes.oid <> 0
				WHERE t_tiers_payant.code_original = 'C'||NOCAIS AND
					(grand_regime_id = 0 OR grand_regime_id IS NULL);


				INSERT INTO activite[PX].t_tiers_payant(code_original, code, type_tiers_payant, texte, texte_court) 
				SELECT 'M'||MUNUM, 'M'||MUNUM, 
					'1', 
					MAX(MUNMMU), MAX(MUNMMU)
				FROM prod_chataigneraie_as400.NXXXFAC_BFMUTU
				WHERE ('M'||MUNUM) NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant WHERE code_original IS NOT NULL)
				GROUP BY 1,2;
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Médecins">
			<sqlcmd><![CDATA[

				INSERT INTO base.t_specialites_medecin(code_original, code, texte, texte_court)
				SELECT 
					to_char(SC1NU,'FM00'),
					to_char(SC1NU,'FM00'),
					SC1LI, 
					SC1LI
				FROM prod_chataigneraie_as400.NXXXFIC_DSPECIA DSPECIA 
					LEFT JOIN base.t_specialites_medecin on t_specialites_medecin.code = to_char(SC1NU,'FM00')
				WHERE t_specialites_medecin.code IS NULL AND
					SC1NU <= 99
				ORDER BY 1;

				INSERT INTO activite[PX].t_specialites_medecin(oid, code, code_original, texte, texte_court) 
				SELECT 0, '**', '0', 'Non saisie', 'Non saisie'
				WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_specialites_medecin);
				
				INSERT INTO base.t_specialites_medecin(oid, code, code_original, texte, texte_court) 
				SELECT 0, '', '0', 'Non saisie', 'Non saisie'
				WHERE 0 NOT IN (SELECT oid FROM base.t_specialites_medecin);


				INSERT INTO base.t_medecins(oid, nom, prenom, numero_ordre, specialite_id ) 
				SELECT 0, 'Non renseigné', '', '', 0 WHERE 0 NOT IN (SELECT oid FROM base.t_medecins);

				INSERT INTO activite[PX].t_medecins_administratifs(oid, code_original, code, nom, prenom, specialite_id, medecin_id) 
				SELECT 0, '0', '****', 'Non renseigné', '', 0, 0 WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_medecins_administratifs);

				DROP TABLE IF EXISTS w_medecins;
				CREATE TEMP TABLE w_medecins AS
				SELECT 
					PERNU AS PERNU,
					base.cti_soundex_nom(PERNOM) AS PERNOM_SOUNDEX, 
					base.cti_soundex_nom(PERPNM) AS PERPNM_SOUNDEX, 
					PERNOM, 
					PERPNM AS PERPNM, 
					SUBSTR(COALESCE(PERNSE,''),1,9) AS PERNSE, 
					CASE WHEN SC1NU <= 99 THEN to_char(SC1NU,'FM00') ELSE '' END AS SC1NU, 
					COALESCE(t_medecins_administratifs.medecin_id,0) as medecin_id
				FROM prod_chataigneraie_as400.NXXXFIC_DPERSON DPERSON
					LEFT JOIN activite[PX].t_medecins_administratifs ON PERNU = t_medecins_administratifs.code_original
				WHERE PERNU IN (SELECT DPERNU FROM prod_chataigneraie_as400.NXXXFAC_NDOADM NDOADM)
				;
				
				INSERT INTO w_medecins
				SELECT 
					PERNU AS PERNU,
					base.cti_soundex_nom(PERNOM) AS PERNOM_SOUNDEX, 
					base.cti_soundex_nom(PERPNM) AS PERPNM_SOUNDEX, 
					PERNOM, 
					PERPNM AS PERPNM, 
					SUBSTR(COALESCE(PERNSE,''),1,9) AS PERNSE, 
					CASE WHEN SC1NU <= 99 THEN to_char(SC1NU,'FM00') ELSE '' END AS SC1NU, 
					COALESCE(t_medecins_administratifs.medecin_id,0) as medecin_id
				FROM prod_chataigneraie_as400.NXXXFIC_DPERSON DPERSON
					LEFT JOIN activite[PX].t_medecins_administratifs ON PERNU = t_medecins_administratifs.code_original
				WHERE PERNU IN (SELECT SHPNUAX FROM prod_chataigneraie_as400.NXXXFAC_NELFHONO) AND
					PERNU NOT IN (SELECT PERNU FROM w_medecins)
				;
				
				
				INSERT INTO w_medecins
				SELECT 
					PERNU AS PERNU,
					base.cti_soundex_nom(PERNOM) AS PERNOM_SOUNDEX, 
					base.cti_soundex_nom(PERPNM) AS PERPNM_SOUNDEX, 
					PERNOM, 
					PERPNM AS PERPNM, 
					SUBSTR(COALESCE(PERNSE,''),1,9) AS PERNSE, 
					CASE WHEN SC1NU <= 99 THEN to_char(SC1NU,'FM00') ELSE '' END AS SC1NU, 
					COALESCE(t_medecins_administratifs.medecin_id,0) as medecin_id
				FROM prod_chataigneraie_as400.NXXXFIC_DPERSON DPERSON
					LEFT JOIN activite[PX].t_medecins_administratifs ON PERNU = t_medecins_administratifs.code_original
				WHERE PERNU IN (SELECT BAPNUAXD FROM prod_chataigneraie_mysql.STDEV_NELFHONOC) AND
					PERNU NOT IN (SELECT PERNU FROM w_medecins)
				;


				INSERT INTO base.t_medecins(nom, prenom, numero_ordre, specialite_id)
				SELECT w_medecins.PERNOM, w_medecins.PERPNM, w_medecins.PERNSE, 0 FROM
				(SELECT SUBSTR(PERNOM_SOUNDEX, 1, 8) AS PERNOM_SOUNDEX, SUBSTR(PERPNM_SOUNDEX, 1, 8) AS PERPNM_SOUNDEX, PERNSE, MIN(PERNU) AS PERNU
				FROM w_medecins
				GROUP BY 1,2,3) subview, w_medecins
				WHERE w_medecins.PERNU = subview.PERNU
					AND subview.PERNOM_SOUNDEX || ',' || subview.PERPNM_SOUNDEX || ',' ||  subview.PERNSE NOT IN 
					(SELECT substr(base.cti_soundex_nom(nom),1,8)  || ',' || substr(base.cti_soundex_nom(prenom),1,8) || ',' || numero_ordre FROM base.t_medecins);

				INSERT INTO base.t_medecins(nom, prenom, numero_ordre, specialite_id)
				SELECT w_medecins.PERNOM, w_medecins.PERPNM, w_medecins.PERNSE, 0 FROM
				(SELECT PERNOM_SOUNDEX, PERPNM_SOUNDEX, MIN(PERNU) AS PERNU
				FROM w_medecins
				GROUP BY 1,2) subview, w_medecins
				WHERE w_medecins.PERNU = subview.PERNU
					AND subview.PERNOM_SOUNDEX || ',' || subview.PERPNM_SOUNDEX NOT IN 
					(SELECT base.cti_soundex_nom(nom)  || ',' || base.cti_soundex_nom(prenom) FROM base.t_medecins);


				UPDATE w_medecins SET medecin_id = subview.medecin_id
				FROM 
				(SELECT substr(base.cti_soundex_nom(nom),1,8) as nom , substr(base.cti_soundex_nom(prenom),1,8) as prenom, numero_ordre, MIN(oid) AS medecin_id FROM base.t_medecins GROUP BY 1,2,3) subview
				WHERE SUBSTR(PERNOM_SOUNDEX, 1, 8) = subview.nom
				AND SUBSTR(PERPNM_SOUNDEX, 1, 8) = subview.prenom
				AND PERNSE = subview.numero_ordre
				AND w_medecins.medecin_id = 0;

				UPDATE w_medecins SET medecin_id = subview.medecin_id
				FROM 
				(SELECT base.cti_soundex_nom(nom) as nom , base.cti_soundex_nom(prenom) as prenom, MIN(oid) AS medecin_id FROM base.t_medecins GROUP BY 1,2) subview
				WHERE SUBSTR(PERNOM_SOUNDEX, 1, 8) = subview.nom
				AND SUBSTR(PERPNM_SOUNDEX, 1, 8) = subview.prenom
				AND w_medecins.medecin_id = 0;


				INSERT INTO activite[PX].t_medecins_administratifs(code_original, code, nom, prenom, specialite_id,  medecin_id)
				SELECT DISTINCT PERNU, substr(translate(PERNOM,' -=()''',''),1,5) || substr(translate(PERPNM,' -=()''',''),1,2), PERNOM, PERPNM, COALESCE(t_specialites_medecin.oid,0), medecin_id
				FROM w_medecins
					LEFT JOIN base.t_specialites_medecin ON SC1NU = t_specialites_medecin.code_original
				WHERE PERNU NOT IN (SELECT code_original FROM activite[PX].t_medecins_administratifs);

				UPDATE activite[PX].t_medecins_administratifs
				SET nom = PERNOM,
					prenom = PERPNM,
					specialite_id = COALESCE(t_specialites_medecin.oid,0),
					medecin_id = w_medecins.medecin_id
				FROM w_medecins LEFT JOIN base.t_specialites_medecin ON SC1NU = t_specialites_medecin.code_original
				WHERE w_medecins.PERNU = t_medecins_administratifs.code_original
					AND (
					t_medecins_administratifs.nom <> PERNOM OR
					t_medecins_administratifs.prenom <> PERPNM OR
					t_medecins_administratifs.specialite_id <> COALESCE(t_specialites_medecin.oid,0) OR
					t_medecins_administratifs.medecin_id <> w_medecins.medecin_id
				);

				UPDATE base.t_medecins SET specialite_id = subview.specialite_id 
				FROM 
					(SELECT t_medecins.oid, MIN(t_medecins_administratifs.specialite_id) as specialite_id 
					FROM base.t_medecins, activite[PX].t_medecins_administratifs , base.t_specialites_medecin 
					WHERE t_medecins.oid = t_medecins_administratifs.medecin_id 
					AND t_medecins_administratifs.specialite_id = t_specialites_medecin.oid 
					GROUP BY t_medecins.oid) subview 
				WHERE t_medecins.oid = subview.oid AND t_medecins.oid <> 0 AND t_medecins.specialite_id = 0;
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Hébergement">
			<sqlcmd><![CDATA[
				
				-- Services
				
				INSERT INTO activite[PX].t_services_facturation(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '****', 'Non renseigné', 'Non renseigné' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_services_facturation);

				
				
				INSERT INTO activite[PX].t_services_facturation(code_original, code, texte, texte_court, nb_lits, type_t2a)
				SELECT TARID, TARID, TARLI, TARLI, 0 AS nb_lits, '0' AS type_t2a
				FROM prod_chataigneraie_as400.NXXXFAC_BFGRTA
				WHERE TARID NOT IN (SELECT code_original FROM activite[PX].t_services_facturation WHERE code_original IS NOT NULL)
				ORDER BY TARID;
				

				UPDATE activite[PX].t_services_facturation SET
					texte = TARLI,
					texte_court = CASE WHEN texte_court = texte THEN TARLI ELSE texte_court END
				FROM prod_chataigneraie_as400.NXXXFAC_BFGRTA
				WHERE TARID = code_original AND
					(
						texte IS DISTINCT FROM TARLI OR
						texte_court IS DISTINCT FROM CASE WHEN texte_court = texte THEN TARLI ELSE texte_court END
					);
				
				UPDATE activite[PX].t_services_facturation
				SET mode_traitement_id = COALESCE(t_modes_traitement.oid,0)
				FROM prod_chataigneraie_as400.NXXXFAC_BFGRTA BFGRTA
					JOIN prod_chataigneraie_as400.NXXXFAC_BFMT BFMT ON BFGRTA.MTID = BFMT.MTID
					LEFT JOIN base.t_modes_traitement ON BFMT.MTLISS = t_modes_traitement.code
				WHERE t_services_facturation.code_original = TARID AND 
					COALESCE(mode_traitement_id,0) = 0 AND
					COALESCE(mode_traitement_id,0) <> COALESCE(t_modes_traitement.oid,0);
				
				UPDATE activite[PX].t_services_facturation
				SET dmt_id = COALESCE(t_dmt.oid,0)
				FROM prod_chataigneraie_as400.NXXXFAC_BFGRTA BFGRTA
					JOIN prod_chataigneraie_as400.NXXXFAC_BFDMT BFDMT ON BFGRTA.DISID = BFDMT.DISID
					LEFT JOIN base.t_dmt ON BFDMT.DISLISS = t_dmt.code
				WHERE t_services_facturation.code_original = TARID AND 
					COALESCE(dmt_id,0) = 0 AND
					COALESCE(dmt_id,0) <> COALESCE(t_dmt.oid,0);
					
				-- Services non facturables
				INSERT INTO activite[PX].t_services_facturation(code_original, code, texte, texte_court, nb_lits, type_t2a)
				SELECT TARID||'-NF', TARID||'-NF', TARLI || ' (Non facturable)', TARLI || ' (Non facturable)', 0 AS nb_lits, '0' AS type_t2a
				FROM prod_chataigneraie_as400.NXXXFAC_BFGRTA
				WHERE TARID IN (SELECT NOTAR FROM prod_chataigneraie_as400.NXXXFAC_NDOADM WHERE NOFACT = 'N') AND
					(TARID||'-NF') NOT IN (SELECT code_original FROM activite[PX].t_services_facturation WHERE code_original IS NOT NULL)
				ORDER BY TARID;
	
				UPDATE activite[PX].t_services_facturation SET
					texte = TARLI || ' (Non facturable)',
					texte_court = CASE WHEN texte_court = texte THEN TARLI ELSE texte_court END || ' (Non facturable)'
				FROM prod_chataigneraie_as400.NXXXFAC_BFGRTA
				WHERE (TARID||'-NF') = code_original AND
					(
						texte IS DISTINCT FROM (TARLI || ' (Non facturable)') OR
						texte_court IS DISTINCT FROM (CASE WHEN texte_court = texte THEN TARLI ELSE texte_court END || ' (Non facturable)')
					);
				
				UPDATE activite[PX].t_services_facturation
				SET mode_traitement_id = COALESCE(t_modes_traitement.oid,0)
				FROM prod_chataigneraie_as400.NXXXFAC_BFGRTA BFGRTA
					JOIN prod_chataigneraie_as400.NXXXFAC_BFMT BFMT ON BFGRTA.MTID = BFMT.MTID
					LEFT JOIN base.t_modes_traitement ON BFMT.MTLISS = t_modes_traitement.code
				WHERE t_services_facturation.code_original = (TARID||'-NF') AND 
					COALESCE(mode_traitement_id,0) = 0 AND
					COALESCE(mode_traitement_id,0) <> COALESCE(t_modes_traitement.oid,0);
				
				UPDATE activite[PX].t_services_facturation
				SET dmt_id = COALESCE(t_dmt.oid,0)
				FROM prod_chataigneraie_as400.NXXXFAC_BFGRTA BFGRTA
					JOIN prod_chataigneraie_as400.NXXXFAC_BFDMT BFDMT ON BFGRTA.DISID = BFDMT.DISID
					LEFT JOIN base.t_dmt ON BFDMT.DISLISS = t_dmt.code
				WHERE t_services_facturation.code_original = (TARID||'-NF') AND 
					COALESCE(dmt_id,0) = 0 AND
					COALESCE(dmt_id,0) <> COALESCE(t_dmt.oid,0);

				-- Unités fonctionnelles

				INSERT INTO activite[PX].t_unites_fonctionnelles(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_unites_fonctionnelles);

				INSERT INTO activite[PX].t_unites_fonctionnelles(code_original, code, texte, texte_court)
				SELECT SENU, SELIC, SELI, SELI
				FROM prod_chataigneraie_as400.NXXXFIC_NSERSO
				WHERE SENU NOT IN (SELECT code_original FROM activite[PX].t_unites_fonctionnelles WHERE code_original IS NOT NULL)
				ORDER BY SELIC;
				
				UPDATE activite[PX].t_unites_fonctionnelles SET
					code = SELIC,
					texte = SELI,
					texte_court = CASE WHEN texte_court = texte THEN SELI ELSE texte_court END
				FROM prod_chataigneraie_as400.NXXXFIC_NSERSO
				WHERE SENU = code_original AND
					(
						code IS DISTINCT FROM SELIC OR
						texte IS DISTINCT FROM SELI OR
						texte_court IS DISTINCT FROM CASE WHEN texte_court = texte THEN SELI ELSE texte_court END
					);
					
				-- Unités médicales

				INSERT INTO activite[PX].t_unites_medicales(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_unites_medicales);

				INSERT INTO activite[PX].t_unites_medicales(code_original, code, texte, texte_court)
				SELECT 
					CHROUM,
					to_char(VALUM,'FM0000')||'-'||CHROUM,
					LUM AS texte,
					LUM AS texte_court
				FROM prod_chataigneraie_mysql.STDEV_UNIMED
				WHERE IDETAB = 10 AND
					CHROUM NOT IN (SELECT code_original FROM activite[PX].t_unites_medicales WHERE code_original IS NOT NULL)
				ORDER BY 1,2,3
				;

				-- Activités

				INSERT INTO activite[PX].t_activites(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_activites);

				INSERT INTO activite[PX].t_activites(code_original, code, texte, texte_court)
				SELECT SPESAE, SPESAE, LSPESA, LSPESA
				FROM prod_chataigneraie_as400.NXXXFAC_BFSPESAE
				WHERE SPESAE NOT IN (SELECT code_original FROM activite[PX].t_activites WHERE code_original IS NOT NULL)
				ORDER BY SPESAE;
				
				UPDATE activite[PX].t_activites SET
					texte = LSPESA,
					texte_court = CASE WHEN texte_court = texte THEN LSPESA ELSE texte_court END
				FROM prod_chataigneraie_as400.NXXXFAC_BFSPESAE
				WHERE SPESAE = code_original AND
					(
						texte IS DISTINCT FROM LSPESA OR
						texte_court IS DISTINCT FROM CASE WHEN texte_court = texte THEN LSPESA ELSE texte_court END
					);

				

				-- Etages et lits
				
				INSERT INTO activite[PX].t_etages(oid, code_original, code, texte, texte_court, nb_lits) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné', 0 WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_etages);
				
				INSERT INTO activite[PX].t_etages(code_original, code, texte, texte_court, nb_lits)
				SELECT SENU, SELIC, SELI, SELI, 0
				FROM prod_chataigneraie_as400.NXXXFIC_NSERSO
				WHERE SENU NOT IN (SELECT code_original FROM activite[PX].t_etages WHERE code_original IS NOT NULL)
				ORDER BY SELIC;
				
				UPDATE activite[PX].t_etages SET
					code = SELIC,
					texte = SELI,
					texte_court = CASE WHEN texte_court = texte THEN SELI ELSE texte_court END
				FROM prod_chataigneraie_as400.NXXXFIC_NSERSO
				WHERE SENU = code_original AND
					(
						code IS DISTINCT FROM SELIC OR
						texte IS DISTINCT FROM SELI OR
						texte_court IS DISTINCT FROM CASE WHEN texte_court = texte THEN SELI ELSE texte_court END
					);



				INSERT INTO activite[PX].t_lits(oid, code_original, code, texte, texte_court, etage_id, chambre_particuliere) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné', 0, 'N' WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_lits);

				DROP TABLE IF EXISTS w_lits;
				CREATE TEMP TABLE w_lits AS 
				SELECT 
					NMVTCHB.SENU, 
					NMVTCHB.LITNU, 
					NMVTCHB.SENU||'|'||NMVTCHB.LITNU AS lit_code_original,
					CHLIC||LITLOC AS lit_code,
					MAX(CASE WHEN base.cti_to_number(CHLIC) > 0 AND LITLOC = '' AND length(CHLIC) = 4 THEN 'O' ELSE 'N' END) AS chambre_particuliere,
					count(*) AS nb,
					NMVTCHB.SENU||'|'||NMVTCHB.LITNU AS lit_code_original_cible,
					0::bigint AS lit_id
				FROM prod_chataigneraie_as400.NXXXFAC_NMVTCHB NMVTCHB
				JOIN prod_chataigneraie_as400.NXXXFIC_DLIT DLIT ON NMVTCHB.LITNU = DLIT.LITNU
				JOIN prod_chataigneraie_as400.NXXXFIC_NCHAMBR NCHAMBR ON DLIT.CHNU1 = NCHAMBR.CHNU1
				WHERE NMVTCHB.SENU <> 0 AND NMVTCHB.LITNU <> 0
				GROUP BY 1,2,3,4
				ORDER BY 1,2,3,4;

				
				UPDATE w_lits
				SET lit_code_original_cible = subview.lit_code_original_cible
				FROM
				(
					SELECT SENU,lit_code,(MAX(Array[to_char(nb,'FM0000000000'),lit_code_original]))[2] AS lit_code_original_cible
					FROM w_lits
					GROUP BY 1,2
					HAVING COUNT(*) > 1
				) subview
				WHERE w_lits.SENU = subview.SENU AND
					w_lits.lit_code = subview.lit_code AND
					w_lits.lit_code_original <> subview.lit_code_original_cible
				;
			

				

				INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere)
				SELECT lit_code_original, lit_code, lit_code, lit_code,
					t_etages.oid, chambre_particuliere
				FROM w_lits
					JOIN activite[PX].t_etages ON SENU = t_etages.code_original
				WHERE lit_code_original_cible = lit_code_original AND
					lit_code_original <> '' AND
					lit_code_original NOT IN (SELECT code_original FROM activite[PX].t_lits WHERE code_original IS NOT NULL)
				ORDER BY 2;

				UPDATE w_lits
				SET lit_id = t_lits.oid
				FROM activite[PX].t_lits 
				WHERE lit_code_original_cible = t_lits.code_original
				;


				INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere)
				SELECT code_original || '|0' , '', 'Non renseigné', 'Non renseigné', oid, 'O' 
				FROM activite[PX].t_etages
				WHERE oid > 0 AND
					code_original || '|0' NOT IN (SELECT code_original FROM activite[PX].t_lits WHERE code_original IS NOT NULL );


									
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Facturation">
			<sqlcmd><![CDATA[
				
				-- Rubriques et prestations

				INSERT INTO activite[PX].t_rubriques_facturation(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_rubriques_facturation);

				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT ARIID, upper(substr(split_part(ARILI,' ',1),1,3))|| '-' || ARIID, MAX(ARILI), MAX(ARILI)
				FROM prod_chataigneraie_as400.NXXXFAC_BFARCLI BFARCLI
				WHERE ARIID NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation WHERE code_original IS NOT NULL)
				GROUP BY 1,2
				ORDER BY 2;
				
				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT AHID||'H' ,AHLB2|| '-' || AHID, MAX(AHLIB), MAX(AHLIB)
				FROM prod_chataigneraie_as400.NXXXFAC_BFHONO
				WHERE (AHID||'H') NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation WHERE code IS NOT NULL)
				GROUP BY 1,2
				ORDER BY 2;
				
				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT TYLFMED||'M' , 
					COALESCE(AB2OF,
						CASE 
						WHEN TYLFLI ILIKE '%proth%' THEN 'PII' 
						WHEN TYLFLI ILIKE '%cament%' THEN 'PH8' 
						ELSE 'XXX' END)
						|| '-' || TYLFMED, MAX(TYLFLI), MAX(TYLFLI)
				FROM prod_chataigneraie_as400.NXXXFAC_BFTYMED BFTYMED
					LEFT JOIN prod_chataigneraie_as400.NXXXFAC_BFAB2 BFAB2 ON BFAB2.AB2ID = BFTYMED.AB2ID
				WHERE (TYLFMED||'M') NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation WHERE code IS NOT NULL)
				GROUP BY 1,2
				ORDER BY 2;
				

				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT BAREGROU||'-C' ,BAREGROU, BAREGROU, BAREGROU
				FROM 
					(
					SELECT BAREGROU
					FROM prod_chataigneraie_mysql.STDEV_NELFHONOC 
					WHERE BAREGROU IS NOT NULL AND BAREGROU <> '' 
					GROUP BY 1
					) subview
				WHERE BAREGROU||'-C' NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation WHERE code IS NOT NULL)
				GROUP BY 1,2
				ORDER BY BAREGROU;
				

				INSERT INTO activite.t_prestations(oid, code_original, code, texte, texte_court, clinique_honoraire) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée', '' WHERE 0 NOT IN (SELECT oid FROM activite.t_prestations);

				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT AB2OF, AB2OF, MAX(AB2LC), MAX(AB2LC)
				FROM prod_chataigneraie_as400.NXXXFAC_BFAB2
				WHERE AB2OF NOT IN (SELECT code FROM activite.t_prestations WHERE code IS NOT NULL)
				GROUP BY 1,2
				ORDER BY AB2OF;
				
				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT AHLOF ,AHLOF, MAX(AHLIB), MAX(AHLIB)
				FROM prod_chataigneraie_as400.NXXXFAC_BFHONO
				WHERE AHLOF NOT IN (SELECT code FROM activite.t_prestations WHERE code IS NOT NULL)
				GROUP BY 1,2
				ORDER BY AHLOF;
	
				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT BAREGROU ,BAREGROU, BAREGROU, BAREGROU
				FROM 
					(
					SELECT BAREGROU
					FROM prod_chataigneraie_mysql.STDEV_NELFHONOC 
					WHERE BAREGROU IS NOT NULL AND BAREGROU <> '' 
					GROUP BY 1
					) subview
				WHERE BAREGROU NOT IN (SELECT code FROM activite.t_prestations WHERE code IS NOT NULL)
				GROUP BY 1
				ORDER BY BAREGROU;

				
				
				INSERT INTO activite[PX].t_compte(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_compte);
				
				INSERT INTO activite[PX].t_compte(code_original, code, texte, texte_court) 
				SELECT CPTID, CPTID, CPTID, CPTID
				FROM prod_chataigneraie_as400.NXXXFAC_BFCENT
				WHERE CPTID NOT IN (SELECT code FROM activite[PX].t_compte WHERE code IS NOT NULL)
				GROUP BY 1
				;
				
				INSERT INTO activite[PX].t_compte(code_original, code, texte, texte_court) 
				SELECT CPTID, CPTID, CPTID, CPTID
				FROM prod_chataigneraie_as400.NXXXFAC_BFCENT
				WHERE CPTID NOT IN (SELECT code FROM activite[PX].t_compte WHERE code IS NOT NULL)
				GROUP BY 1
				;
										
			]]></sqlcmd>
		</NODE>
		
	</NODE>
		
	<NODE name="POST" label="TRAITEMENTS COMPLEMENTAIRES">
	
		
	
		<NODE label="Compléments facture">
			<sqlcmd><![CDATA[
							
				
			
				UPDATE activite[PX].p_factures SET date_expedition_0 = date_facture WHERE date_expedition_0 < date_facture AND date_facture <> '2099-12-31';

				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compléments séjours">
			<sqlcmd><![CDATA[

				UPDATE activite[PX].p_sejours 
				SET date_groupage = date_facture 
				WHERE (date_groupage IS null OR date_groupage = '20991231') AND 
					date_facture <> '2099-12-31';
				
				
				DROP TABLE IF EXISTS w_sejours_total;
				CREATE TEMP TABLE w_sejours_total AS
				SELECT   
					p_sejours.no_sejour AS no_sejour, 
					p_sejours.code_sorti,
					p_sejours.date_sortie,
					p_sejours.date_groupage,   
					p_sejours.ghs_id,
	
					MAX(COALESCE(p_factures.date_facture,'20991231')) AS date_facture,     
					MAX(COALESCE(p_factures.date_expedition,'20991231')) AS date_expedition,  
					MAX(COALESCE(p_factures.date_solde,'20991231')) AS date_solde,  
					SUM(COALESCE(p_factures.montant_facture_c,0)) AS montant_facture_c,  
					SUM(COALESCE(p_factures.montant_facture_h,0)) AS montant_facture_h,  
					SUM(COALESCE(p_factures.montant_regle_c,0)) AS montant_regle_c,  
					SUM(COALESCE(p_factures.montant_regle_h,0)) AS montant_regle_h,  
					SUM(COALESCE(CASE WHEN p_factures.type_facture <> 'P' AND p_factures.type_facture <> 'E' AND p_factures.type_facture <> 'G' THEN 1 ELSE 0 END,0)) AS nb_factures,  
					SUM(COALESCE(CASE WHEN p_factures.type_facture = '0' THEN 0 ELSE 1 END,0)) AS nb_factures_regularisation,  
					SUM(COALESCE(p_factures.nb_rejets,0)) AS nb_rejets,  

					SUM(COALESCE(p_factures.montant_facture_0_c,0)) AS montant_facture_0_c,  
					SUM(COALESCE(p_factures.montant_facture_0_h,0)) AS montant_facture_0_h,  
					SUM(COALESCE(p_factures.montant_regle_0_c,0)) AS montant_regle_0_c,  
					SUM(COALESCE(p_factures.montant_regle_0_h,0)) AS montant_regle_0_h,  
					MAX(COALESCE(CASE WHEN (p_factures.montant_facture_0_c <> 0 OR p_factures.montant_facture_0_h <> 0)  THEN p_factures.date_expedition_0 ELSE '0001-01-01' END,'20991231' )) AS date_expedition_0,   
					MAX(COALESCE(CASE WHEN p_factures.date_solde_0_c < '2099-12-31' AND p_factures.date_solde_0_c > p_factures.date_solde_0_h THEN p_factures.date_solde_0_c ELSE p_factures.date_solde_0_h END,'20991231' )) AS date_solde_0,  
					

					SUM(COALESCE(p_factures.montant_facture_1_c,0)) AS montant_facture_1_c,  
					SUM(COALESCE(p_factures.montant_facture_1_h,0)) AS montant_facture_1_h,  
					SUM(COALESCE(p_factures.montant_regle_1_c,0)) AS montant_regle_1_c,  
					SUM(COALESCE(p_factures.montant_regle_1_h,0)) AS montant_regle_1_h,  
					MAX(COALESCE(CASE WHEN (p_factures.montant_facture_1_c <> 0 OR p_factures.montant_facture_1_h <> 0)  THEN p_factures.date_expedition_1 ELSE '0001-01-01' END,'20991231' )) AS date_expedition_1,  
					MAX(COALESCE(CASE WHEN p_factures.date_solde_1_c < '2099-12-31' AND p_factures.date_solde_1_c > p_factures.date_solde_1_h THEN p_factures.date_solde_1_c ELSE p_factures.date_solde_1_h END,'20991231' )) AS date_solde_1,  

					SUM(COALESCE(p_factures.montant_facture_2_c,0)) AS montant_facture_2_c,  
					SUM(COALESCE(p_factures.montant_facture_2_h,0)) AS montant_facture_2_h,  
					SUM(COALESCE(p_factures.montant_regle_2_c,0)) AS montant_regle_2_c,  
					SUM(COALESCE(p_factures.montant_regle_2_h,0)) AS montant_regle_2_h,  
					MAX(COALESCE(CASE WHEN (p_factures.montant_facture_2_c <> 0 OR p_factures.montant_facture_2_h <> 0)  THEN p_factures.date_expedition_2 ELSE '0001-01-01' END,'20991231' )) AS date_expedition_2,  
					MAX(COALESCE(CASE WHEN p_factures.date_solde_2_c < '2099-12-31' AND p_factures.date_solde_2_c > p_factures.date_solde_2_h THEN p_factures.date_solde_2_c ELSE p_factures.date_solde_2_h END,'20991231' )) AS date_solde_2,  

					SUM(COALESCE(p_factures.montant_facture_22_c,0)) AS montant_facture_22_c,  
					SUM(COALESCE(p_factures.montant_facture_22_h,0)) AS montant_facture_22_h,  
					SUM(COALESCE(p_factures.montant_regle_22_c,0)) AS montant_regle_22_c,  
					SUM(COALESCE(p_factures.montant_regle_22_h,0)) AS montant_regle_22_h,  
					MAX(COALESCE(CASE WHEN (p_factures.montant_facture_22_c <> 0 OR p_factures.montant_facture_22_h <> 0)  THEN p_factures.date_expedition_22 ELSE '0001-01-01' END,'20991231' )) AS date_expedition_22,  
					MAX(COALESCE(CASE WHEN p_factures.date_solde_22_c < '2099-12-31' AND p_factures.date_solde_22_c > p_factures.date_solde_22_h THEN p_factures.date_solde_22_c ELSE p_factures.date_solde_22_h END,'20991231' )) AS date_solde_22 , 

					
					SUM(COALESCE(p_factures.montant_comptabilise_c,0)) AS montant_comptabilise_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_h,0)) AS montant_comptabilise_h,  
					SUM(COALESCE(p_factures.montant_comptabilise_0_c,0)) AS montant_comptabilise_0_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_0_h,0)) AS montant_comptabilise_0_h,  
					SUM(COALESCE(p_factures.montant_comptabilise_1_c,0)) AS montant_comptabilise_1_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_1_h,0)) AS montant_comptabilise_1_h,  
					SUM(COALESCE(p_factures.montant_comptabilise_2_c,0)) AS montant_comptabilise_2_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_2_h,0)) AS montant_comptabilise_2_h,  
					SUM(COALESCE(p_factures.montant_comptabilise_22_c,0)) AS montant_comptabilise_22_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_22_h,0)) AS montant_comptabilise_22_h,

					SUM(COALESCE(p_factures.montant_comptabilise_budget_global_c,0)) AS montant_comptabilise_budget_global_c,  

					SUM(COALESCE(p_factures.montant_facture_c_actes_inclus_dans_sejour,0)) AS montant_facture_c_actes_inclus_dans_sejour,  
					SUM(COALESCE(p_factures.montant_facture_h_actes_inclus_dans_sejour,0)) AS montant_facture_h_actes_inclus_dans_sejour,

					0::numeric AS delai_groupage, 
					0::numeric AS delai_facture, 
					0::numeric AS delai_expedition, 
					0::numeric AS delai_solde, 
					0::numeric AS delai_expedition_0, 
					0::numeric AS delai_solde_0, 
					0::numeric AS delai_expedition_1, 
					0::numeric AS delai_solde_1, 
					0::numeric AS delai_expedition_2, 
					0::numeric AS delai_solde_2, 
					0::numeric AS delai_expedition_22, 
					0::numeric AS delai_solde_22
				
				FROM activite[PX].p_sejours LEFT JOIN activite[PX].p_factures ON p_sejours.no_sejour = p_factures.no_sejour AND type_facture <> 'P'
				GROUP BY 1,2,3,4,5;
				
				
				UPDATE w_sejours_total
				SET date_groupage = date_facture 
				WHERE (date_groupage IS null OR date_groupage = '20991231' OR date_groupage = '00010101') AND 
					date_facture <> '2099-12-31';


				UPDATE w_sejours_total SET  
					date_groupage = CASE WHEN date_groupage > '00010101' THEN date_groupage ELSE '20991231' END,
					date_facture = CASE WHEN date_facture> '00010101' THEN date_facture ELSE '20991231' END,
					date_expedition = CASE WHEN date_expedition > '00010101' THEN date_expedition ELSE '20991231' END,
					date_solde = CASE WHEN date_solde > '00010101' THEN date_solde ELSE '20991231' END,
					date_expedition_0 = CASE WHEN date_expedition_0 > '00010101' THEN date_expedition_0 ELSE '20991231' END,
					date_solde_0 = CASE WHEN date_solde_0 > '00010101' THEN date_solde_0 ELSE '20991231' END,
					date_expedition_1 = CASE WHEN date_expedition_1 > '00010101' THEN date_expedition_1 ELSE '20991231' END,
					date_solde_1 = CASE WHEN date_solde_1 > '00010101' THEN date_solde_1 ELSE '20991231' END,
					date_expedition_2 = CASE WHEN date_expedition_2 > '00010101' THEN date_expedition_2 ELSE '20991231' END,
					date_solde_2 = CASE WHEN date_solde_2 > '00010101' THEN date_solde_2 ELSE '20991231' END,
					date_expedition_22 = CASE WHEN date_expedition_22 > '00010101' THEN date_expedition_22 ELSE '20991231' END,
					date_solde_22 = CASE WHEN date_solde_22 > '00010101' THEN date_solde_22 ELSE '20991231' END;

					
				UPDATE w_sejours_total SET  
					delai_groupage = CASE WHEN code_sorti = '1' AND date_groupage > date_sortie AND date_groupage < '2099-12-31' AND ghs_id <> 0
								THEN date_groupage  - date_sortie ELSE 0 END, 
					delai_facture = CASE WHEN code_sorti = '1' AND date_facture  > date_sortie AND date_facture < '2099-12-31' AND date_facture > date_sortie  
								THEN date_facture  - date_sortie ELSE 0 END, 
					delai_expedition = CASE WHEN code_sorti = '1' AND date_expedition  > date_sortie AND date_facture < '2099-12-31' AND date_expedition < '2099-12-31'  
								THEN date_expedition  - date_sortie ELSE 0 END, 
					delai_solde = CASE WHEN code_sorti = '1' AND date_solde > date_sortie AND date_facture < '2099-12-31' AND date_solde < '2099-12-31'  
								THEN date_solde - date_sortie ELSE 0 END, 
					delai_expedition_0 = CASE WHEN code_sorti = '1' AND date_expedition_0 > date_sortie AND date_facture < '2099-12-31' AND date_expedition_0 < '2099-12-31'  
								THEN date_expedition_0  - date_sortie ELSE 0 END, 
					delai_solde_0 = CASE WHEN code_sorti = '1'AND date_solde_0 > date_sortie AND date_facture < '2099-12-31' AND date_solde_0 < '2099-12-31'  
								THEN date_solde_0 - date_sortie ELSE 0 END, 
					delai_expedition_1 = CASE WHEN code_sorti = '1' AND date_expedition_1 > date_sortie AND date_facture < '2099-12-31' AND date_expedition_1 < '2099-12-31'  
								THEN date_expedition_1  - date_sortie ELSE 0 END, 
					delai_solde_1 = CASE WHEN code_sorti = '1' AND date_solde_1 > date_sortie AND date_facture < '2099-12-31' AND date_solde_1 < '2099-12-31'  
								THEN date_solde_1 - date_sortie ELSE 0 END, 
					delai_expedition_2 = CASE WHEN code_sorti = '1' AND date_expedition_2 > date_sortie AND date_facture < '2099-12-31' AND date_expedition_2 < '2099-12-31'  
								THEN date_expedition_2  - date_sortie ELSE 0 END, 
					delai_solde_2 = CASE WHEN code_sorti = '1' AND date_solde_2 > date_sortie AND date_facture < '2099-12-31' AND date_solde_2 < '2099-12-31'  
								THEN date_solde_2 - date_sortie ELSE 0 END, 
					delai_expedition_22 = CASE WHEN code_sorti = '1' AND date_expedition_22 > date_sortie AND date_facture < '2099-12-31' AND date_expedition_22 < '2099-12-31'  
								THEN date_expedition_22  - date_sortie ELSE 0 END, 
					delai_solde_22 = CASE WHEN code_sorti = '1' AND date_solde_22 > date_sortie AND date_facture < '2099-12-31' AND date_solde_22 < '2099-12-31'  
								THEN date_solde_22 - date_sortie ELSE 0 END;

				ALTER TABLE w_sejours_total  ADD CONSTRAINT w_sejours_total_pkey PRIMARY KEY(no_sejour);



				UPDATE activite[PX].p_sejours SET

					date_facture = w_sejours_total.date_facture,  
					date_expedition = w_sejours_total.date_expedition,  
					date_solde = w_sejours_total.date_solde,  
					montant_facture_c = w_sejours_total.montant_facture_c,  
					montant_facture_h = w_sejours_total.montant_facture_h,  
					montant_regle_c = w_sejours_total.montant_regle_c,  
					montant_regle_h = w_sejours_total.montant_regle_h,  
					nb_factures = w_sejours_total.nb_factures,  
					nb_factures_regularisation = w_sejours_total.nb_factures_regularisation,  
					nb_factures_rejet = w_sejours_total.nb_rejets,  

					montant_facture_0_c = w_sejours_total.montant_facture_0_c,  
					montant_facture_0_h = w_sejours_total.montant_facture_0_h,  
					montant_regle_0_c = w_sejours_total.montant_regle_0_c,  
					montant_regle_0_h = w_sejours_total.montant_regle_0_h,  
					date_expedition_0 = w_sejours_total.date_expedition_0,  
					date_solde_0 = w_sejours_total.date_solde_0,  

					montant_facture_1_c = w_sejours_total.montant_facture_1_c,  
					montant_facture_1_h = w_sejours_total.montant_facture_1_h,  
					montant_regle_1_c = w_sejours_total.montant_regle_1_c,  
					montant_regle_1_h = w_sejours_total.montant_regle_1_h,  
					date_expedition_1 = w_sejours_total.date_expedition_1,   
					date_solde_1 = w_sejours_total.date_solde_1,  
		 
					montant_facture_2_c = w_sejours_total.montant_facture_2_c,  
					montant_facture_2_h = w_sejours_total.montant_facture_2_h,  
					montant_regle_2_c = w_sejours_total.montant_regle_2_c,  
					montant_regle_2_h = w_sejours_total.montant_regle_2_h,  
					date_expedition_2 = w_sejours_total.date_expedition_2, 
					date_solde_2 = w_sejours_total.date_solde_2,  
		 
					montant_facture_22_c = w_sejours_total.montant_facture_22_c,  
					montant_facture_22_h = w_sejours_total.montant_facture_22_h,  
					montant_regle_22_c = w_sejours_total.montant_regle_22_c,  
					montant_regle_22_h = w_sejours_total.montant_regle_22_h,  
					date_expedition_22 = w_sejours_total.date_expedition_22, 
					date_solde_22 = w_sejours_total.date_solde_22,
					
					montant_comptabilise_c = w_sejours_total.montant_comptabilise_c,  
					montant_comptabilise_h = w_sejours_total.montant_comptabilise_h,  
					montant_comptabilise_0_c = w_sejours_total.montant_comptabilise_0_c,  
					montant_comptabilise_0_h = w_sejours_total.montant_comptabilise_0_h,  
					montant_comptabilise_1_c = w_sejours_total.montant_comptabilise_1_c,  
					montant_comptabilise_1_h = w_sejours_total.montant_comptabilise_1_h,  
					montant_comptabilise_2_c = w_sejours_total.montant_comptabilise_2_c,  
					montant_comptabilise_2_h = w_sejours_total.montant_comptabilise_2_h,  
					montant_comptabilise_22_c = w_sejours_total.montant_comptabilise_22_c,  
					montant_comptabilise_22_h = w_sejours_total.montant_comptabilise_22_h,

					montant_comptabilise_budget_global_c = w_sejours_total.montant_comptabilise_budget_global_c,

					montant_facture_c_actes_inclus_dans_sejour = w_sejours_total.montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour = w_sejours_total.montant_facture_h_actes_inclus_dans_sejour,


					delai_groupage = w_sejours_total.delai_groupage, 
					delai_facture = w_sejours_total.delai_facture, 
					delai_expedition = w_sejours_total.delai_expedition, 
					delai_solde = w_sejours_total.delai_solde, 
					delai_expedition_0 = w_sejours_total.delai_expedition_0, 
					delai_solde_0 = w_sejours_total.delai_solde_0, 
					delai_expedition_1 = w_sejours_total.delai_expedition_1, 
					delai_solde_1 = w_sejours_total.delai_solde_1, 
					delai_expedition_2 = w_sejours_total.delai_expedition_2, 
					delai_solde_2 = w_sejours_total.delai_solde_2, 
					delai_expedition_22 = w_sejours_total.delai_expedition_22, 
					delai_solde_22 = w_sejours_total.delai_solde_22
					
				FROM w_sejours_total
				WHERE w_sejours_total.no_sejour =  p_sejours.no_sejour

				AND (
					p_sejours.date_facture IS DISTINCT FROM w_sejours_total.date_facture OR   
					p_sejours.date_expedition IS DISTINCT FROM w_sejours_total.date_expedition OR   
					p_sejours.date_solde IS DISTINCT FROM w_sejours_total.date_solde OR   
					p_sejours.montant_facture_c IS DISTINCT FROM w_sejours_total.montant_facture_c OR   
					p_sejours.montant_facture_h IS DISTINCT FROM w_sejours_total.montant_facture_h OR   
					p_sejours.montant_regle_c IS DISTINCT FROM w_sejours_total.montant_regle_c OR   
					p_sejours.montant_regle_h IS DISTINCT FROM w_sejours_total.montant_regle_h OR   
					p_sejours.nb_factures IS DISTINCT FROM w_sejours_total.nb_factures OR   
					p_sejours.nb_factures_regularisation IS DISTINCT FROM w_sejours_total.nb_factures_regularisation OR   
					p_sejours.nb_factures_rejet IS DISTINCT FROM w_sejours_total.nb_rejets OR   

					p_sejours.montant_facture_0_c IS DISTINCT FROM w_sejours_total.montant_facture_0_c OR   
					p_sejours.montant_facture_0_h IS DISTINCT FROM w_sejours_total.montant_facture_0_h OR   
					p_sejours.montant_regle_0_c IS DISTINCT FROM w_sejours_total.montant_regle_0_c OR   
					p_sejours.montant_regle_0_h IS DISTINCT FROM w_sejours_total.montant_regle_0_h OR   
					p_sejours.date_expedition_0 IS DISTINCT FROM w_sejours_total.date_expedition_0 OR   
					p_sejours.date_solde_0 IS DISTINCT FROM w_sejours_total.date_solde_0 OR   

					p_sejours.montant_facture_1_c IS DISTINCT FROM w_sejours_total.montant_facture_1_c OR   
					p_sejours.montant_facture_1_h IS DISTINCT FROM w_sejours_total.montant_facture_1_h OR   
					p_sejours.montant_regle_1_c IS DISTINCT FROM w_sejours_total.montant_regle_1_c OR   
					p_sejours.montant_regle_1_h IS DISTINCT FROM w_sejours_total.montant_regle_1_h OR   
					p_sejours.date_expedition_1 IS DISTINCT FROM w_sejours_total.date_expedition_1 OR    
					p_sejours.date_solde_1 IS DISTINCT FROM w_sejours_total.date_solde_1 OR   
		 
					p_sejours.montant_facture_2_c IS DISTINCT FROM w_sejours_total.montant_facture_2_c OR   
					p_sejours.montant_facture_2_h IS DISTINCT FROM w_sejours_total.montant_facture_2_h OR   
					p_sejours.montant_regle_2_c IS DISTINCT FROM w_sejours_total.montant_regle_2_c OR   
					p_sejours.montant_regle_2_h IS DISTINCT FROM w_sejours_total.montant_regle_2_h OR   
					p_sejours.date_expedition_2 IS DISTINCT FROM w_sejours_total.date_expedition_2 OR  
					p_sejours.date_solde_2 IS DISTINCT FROM w_sejours_total.date_solde_2 OR   
		 
					p_sejours.montant_facture_22_c IS DISTINCT FROM w_sejours_total.montant_facture_22_c OR   
					p_sejours.montant_facture_22_h IS DISTINCT FROM w_sejours_total.montant_facture_22_h OR   
					p_sejours.montant_regle_22_c IS DISTINCT FROM w_sejours_total.montant_regle_22_c OR   
					p_sejours.montant_regle_22_h IS DISTINCT FROM w_sejours_total.montant_regle_22_h OR   
					p_sejours.date_expedition_22 IS DISTINCT FROM w_sejours_total.date_expedition_22 OR  
					p_sejours.date_solde_22 IS DISTINCT FROM w_sejours_total.date_solde_22 OR 
					
					p_sejours.montant_comptabilise_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_c OR   
					p_sejours.montant_comptabilise_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_h OR   
					p_sejours.montant_comptabilise_0_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_0_c OR   
					p_sejours.montant_comptabilise_0_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_0_h OR   
					p_sejours.montant_comptabilise_1_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_1_c OR   
					p_sejours.montant_comptabilise_1_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_1_h OR   
					p_sejours.montant_comptabilise_2_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_2_c OR   
					p_sejours.montant_comptabilise_2_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_2_h OR   
					p_sejours.montant_comptabilise_22_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_22_c OR   
					p_sejours.montant_comptabilise_22_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_22_h OR   

					p_sejours.montant_comptabilise_budget_global_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_budget_global_c OR   

					p_sejours.montant_facture_c_actes_inclus_dans_sejour IS DISTINCT FROM w_sejours_total.montant_facture_c_actes_inclus_dans_sejour OR   
					p_sejours.montant_facture_h_actes_inclus_dans_sejour IS DISTINCT FROM w_sejours_total.montant_facture_h_actes_inclus_dans_sejour OR

					p_sejours.delai_groupage IS DISTINCT FROM w_sejours_total.delai_groupage OR   
					p_sejours.delai_facture IS DISTINCT FROM w_sejours_total.delai_facture OR   
					p_sejours.delai_expedition IS DISTINCT FROM w_sejours_total.delai_expedition OR   
					p_sejours.delai_solde IS DISTINCT FROM w_sejours_total.delai_solde OR   
					p_sejours.delai_expedition_0 IS DISTINCT FROM w_sejours_total.delai_expedition_0 OR   
					p_sejours.delai_solde_0 IS DISTINCT FROM w_sejours_total.delai_solde_0 OR   
					p_sejours.delai_expedition_1 IS DISTINCT FROM w_sejours_total.delai_expedition_1 OR   
					p_sejours.delai_solde_1 IS DISTINCT FROM w_sejours_total.delai_solde_1 OR   
					p_sejours.delai_expedition_2 IS DISTINCT FROM w_sejours_total.delai_expedition_2 OR   
					p_sejours.delai_solde_2 IS DISTINCT FROM w_sejours_total.delai_solde_2 OR   
					p_sejours.delai_expedition_22 IS DISTINCT FROM w_sejours_total.delai_expedition_22 OR   
					p_sejours.delai_solde_22 IS DISTINCT FROM w_sejours_total.delai_solde_22

				);
														
										
			]]></sqlcmd>
		</NODE>
		
	
	</NODE>
	
	
</ROOT>
