<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	
	
	<NODE name="INIT" label="INITIALISATIONS">
	
		<NODE label="Corrections diverses">
			<sqlcmd><![CDATA[
				
				
					
		
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Identification des séjours">
			<sqlcmd><![CDATA[
			
					
				
			]]></sqlcmd>
		</NODE>
					
		
		
	</NODE>

	<NODE name="SEJOUR" label="RECUPERATION DES SEJOURS">
	
		<NODE label="Patients">
			<sqlcmd><![CDATA[

				INSERT INTO activite.p_patients(
					no_patient, 
					nom, 
					prenom, 
					nom_naissance, 
					date_naissance, 
					code_sexe)
				SELECT 
					TO_CHAR(NIPP,'FM9999999999990'), 
					TNOM, 
					TPRENOM, 
					TNOMJF, 
					date(DNAISDATE), 
					CASE WHEN TSEXE = 'F' THEN '2' ELSE '1' END
				FROM prod_sano.PATIPP_TD
				WHERE TO_CHAR(NIPP,'FM9999999999990')  NOT IN (SELECT no_patient FROM activite.p_patients)
				;

				UPDATE activite.p_patients SET
					nom = TNOM,
					prenom = TPRENOM,
					nom_naissance = TNOMJF,
					date_naissance = date(DNAISDATE),
					code_sexe = CASE WHEN TSEXE = 'F' THEN '2' ELSE '1' END
				FROM prod_sano.PATIPP_TD
				WHERE no_patient = TO_CHAR(NIPP,'FM9999999999990') AND
					(
					p_patients.nom <> TNOM OR
					p_patients.prenom <> TPRENOM OR
					p_patients.nom_naissance <> TNOMJF OR
					p_patients.date_naissance <> date(DNAISDATE) OR
					p_patients.code_sexe <> CASE WHEN TSEXE = 'F' THEN '2' ELSE '1' END
					)
				;
										
										
			]]></sqlcmd>
		</NODE>
		
		
				
		<NODE label="Séjours">
			<sqlcmd><![CDATA[

				DROP TABLE IF EXISTS w_PATHOSP;
				CREATE TEMP TABLE w_PATHOSP AS
				SELECT
					PATHOSP_TD.*,
					TO_CHAR(NIPP,'FM9999999999990') AS no_patient,
					date(DENTREE) AS DATE_ENTREE,
					to_char(DENTREE,'HH24MISS')::numeric AS HEURE_ENTREE,
					'80'::text AS MODE_ENTREE,
					COALESCE(date(DSORTIE),'20991231'::date) AS DAte_SORTIE,
					COALESCE(to_char(DSORTIE,'HH24MISS')::numeric,0) AS HEURE_SORTIE,
					CASE WHEN DSORTIE IS NULL OR DSORTIE > date(now()) THEN 0 ELSE 1 END::numeric AS CODE_SORTI,
					'80'::text AS MODE_SORTIE,
					''::text AS CODE_MEDECIN_SEJOUR,
					'1'::text AS type_sejour,

					TRESCODPOST,
						
					0::bigint AS medecin_sejour_id,
					0::bigint AS code_postal_id,
					0::bigint AS tiers_payant_0_id,
					0::bigint AS tiers_payant_1_id,
					0::bigint AS tiers_payant_2_id,
					0::bigint AS tiers_payant_22_id
					

				FROM prod_sano.PATHOSP_TD
				JOIN prod_sano.PATIPP_TD ON XNCLEPAT = NCLEPAT

				WHERE ABS(NSTATUTHOSP) NOT IN (4104, 257) AND  XNCLEPAT > 0
				ORDER BY DENTREE
				;

				UPDATE w_PATHOSP
				SET NHOSPI = w_PATHOSP.NHOSPI + 50000
				FROM
				(
				SELECT NHOSPI, MIN(NCLEHOSP) AS NCLEHOSP_min
				FROM w_PATHOSP
				GROUP BY 1
				HAVING count(*) > 1
				) subview
				WHERE w_PATHOSP.NCLEHOSP = NCLEHOSP_min
				;
				
				
				UPDATE w_PATHOSP
				SET code_postal_id = t_codes_postaux.oid
				FROM base.t_codes_postaux 
				WHERE TRESCODPOST = t_codes_postaux.code
				;
				
				UPDATE w_PATHOSP
				SET tiers_payant_0_id = t_tiers_payant.oid
				FROM activite[PX].t_tiers_payant
				WHERE code_original = 'PATIENT'
				;

				UPDATE w_PATHOSP
				SET tiers_payant_2_id = t_tiers_payant.oid
				FROM activite[PX].t_tiers_payant
				WHERE code_original = 'MUTDIV'
				;



				UPDATE w_PATHOSP
				SET tiers_payant_1_id = subview.tiers_payant_id
				FROM 
				(SELECT XNCLEHOSP, (MAX(Array[to_char(DDATEFACT,'YYYYMMDD'), t_tiers_payant.oid::text]))[2]::bigint AS tiers_payant_id
				FROM prod_sano.PATFACT_TD
				join activite[PX].t_tiers_payant ON code_original = TGDREGCACE
				GROUP BY 1
				) subview
				WHERE w_PATHOSP.NCLEHOSP = subview.XNCLEHOSP
				;
				
				
				UPDATE w_PATHOSP
				SET tiers_payant_2_id = subview.tiers_payant_id
				FROM 
				(SELECT XNCLEHOSP, (MAX(Array[to_char(DDATEFACT,'YYYYMMDD'), t_tiers_payant.oid::text]))[2]::bigint AS tiers_payant_id
				FROM prod_sano.PATFACT_TD
				join activite[PX].t_tiers_payant ON code_original = TSPECCODE AND TSPECCODE <> ''
				GROUP BY 1
				) subview
				WHERE w_PATHOSP.NCLEHOSP = subview.XNCLEHOSP
				;
				
				UPDATE w_PATHOSP SET
					XTMODENTPMSI = trim(translate(XTMODENTPMSI,' -','')),
					XTMODSORTPMSI = trim(translate(XTMODSORTPMSI,' -',''))
				;

				UPDATE w_PATHOSP
				SET XTMODENTPMSI = XTMODENTPMSI || '0'
				WHERE length(XTMODENTPMSI) = 1
				;

				
				UPDATE w_PATHOSP
				SET XTMODENTPMSI = '80'
				WHERE XTMODENTPMSI = ''
				;

				UPDATE w_PATHOSP
				SET XTMODSORTPMSI = XTMODSORTPMSI || '0'
				WHERE length(XTMODSORTPMSI) = 1
				;

				UPDATE w_PATHOSP
				SET XTMODSORTPMSI = '80'
				WHERE XTMODSORTPMSI = ''
				;

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

				INSERT INTO activite[PX].p_sejours (
					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,
					risque_id
				) 
				SELECT 
					w_PATHOSP.NHOSPI AS no_sejour, 
					w_PATHOSP.NCLEHOSP AS code_original,
					w_PATHOSP.no_patient AS no_patient, 
					w_PATHOSP.date_entree, 
					w_PATHOSP.heure_entree, 
					w_PATHOSP.date_sortie, 
					w_PATHOSP.heure_sortie, 
					p_patients.code_sexe, 
					0::numeric AS age, 
					w_PATHOSP.code_sorti, 
					'0' AS code_prevu, 
					w_PATHOSP.type_sejour, 
					0 AS lieu_sortie_id,
					w_PATHOSP.medecin_sejour_id,
					0 AS ghs_id,
					0 AS ghm_id,
					0 AS ghs_bebe1_id,
					0 AS ghs_bebe2_id,
					0 AS ghs_bebe3_id,
					'20991231' AS date_groupage,
					'0' AS code_cp_demandee,
					0 AS mode_traitement_id,
					substr(w_PATHOSP.XTMODENTPMSI,1,1) AS mode_entree,
					substr(w_PATHOSP.XTMODENTPMSI,2,1) AS provenance,
					substr(w_PATHOSP.XTMODSORTPMSI,1,1) AS mode_sortie,
					substr(w_PATHOSP.XTMODSORTPMSI,2,1) AS destination,
					w_PATHOSP.tiers_payant_0_id,
					w_PATHOSP.tiers_payant_1_id,
					w_PATHOSP.tiers_payant_2_id,
					0 AS tiers_payant_22_id,
					'0' AS est_budget_global,
					w_PATHOSP.code_postal_id,
					0 AS risque_id

				FROM w_PATHOSP
				JOIN activite.p_patients ON p_patients.no_patient = w_PATHOSP.no_patient
				;
				
				-- 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
					'[PX]' = '';

				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
					'[PX]' = '';
				
				-- 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
					'[PX]' = '';
			
			
				-- recréation index
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_2');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_3');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_4');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_5');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_6');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_7');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_8');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_9');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_10');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_11');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_12');
				SELECT base.cti_enable_index('activite[PX]', 'i_sejours_13');
				
				
				
				
					
				
				
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[

				UPDATE w_PATHOSP
				SET DSORTIE = DENTREE + interval '1 hour'
				WHERE DSORTIE = DENTREE
				;

				UPDATE w_PATHOSP
				SET DSORTIE = '20991231'
				WHERE DSORTIE IS NULL
				;

				DROP TABLE IF EXISTS w_PATHISTLIT_CORR;
				CREATE TEMP TABLE w_PATHISTLIT_CORR AS
				SELECT DENTREE, DSORTIE, PATHISTLIT_TD.*
				FROM prod_sano.PATHISTLIT_TD
				JOIN w_PATHOSP ON PATHISTLIT_TD.XNCLEHOSP = w_PATHOSP.NCLEHOSP
				;


				UPDATE w_PATHISTLIT_CORR
				SET DFIN = w_PATHISTLIT_CORR_next.DDEB - interval '1 second'
				FROM w_PATHISTLIT_CORR w_PATHISTLIT_CORR_next
				WHERE w_PATHISTLIT_CORR.XNCLEHOSP = w_PATHISTLIT_CORR_next.XNCLEHOSP AND
					w_PATHISTLIT_CORR.NORDMVT = w_PATHISTLIT_CORR_next.NORDMVT +1
				;

				DELETE
				FROM w_PATHISTLIT_CORR
				WHERE DFIN < DDEB
				;

				
				UPDATE w_PATHISTLIT_CORR
				SET DDEB = DENTREE
				WHERE DDEB < DENTREE
				;

				UPDATE w_PATHISTLIT_CORR
				SET DFIN = DSORTIE
				WHERE DFIN > DSORTIE
				;

				

				DELETE
				FROM w_PATHISTLIT_CORR
				WHERE DFIN <= DDEB
				;

				UPDATE w_PATHISTLIT_CORR
				SET DDEB = w_PATHISTLIT_CORR.DENTREE
				FROM
				(
				SELECT XNCLEHOSP, (MIN(Array[DDEB::text, to_char(NINDEX,'FM000000000000')]))[2]::bigint AS NINDEX
				FROM w_PATHISTLIT_CORR
				GROUP BY 1
				HAVING MIN(DDEB) <> MIN(DENTREE)
				) subview
				WHERE w_PATHISTLIT_CORR.XNCLEHOSP = subview.XNCLEHOSP AND
					w_PATHISTLIT_CORR.NINDEX = subview.NINDEX
				;

				UPDATE w_PATHISTLIT_CORR
				SET DFIN = w_PATHISTLIT_CORR.DSORTIE
				FROM
				(
				SELECT XNCLEHOSP, (MAX(Array[COALESCE(DFIN,'20991231')::text, to_char(NINDEX,'FM000000000000')]))[2]::bigint AS NINDEX
				FROM w_PATHISTLIT_CORR
				GROUP BY 1
				HAVING MAX(COALESCE(DFIN,'20991231')) <> MAX(DSORTIE)
				) subview
				WHERE w_PATHISTLIT_CORR.XNCLEHOSP = subview.XNCLEHOSP AND
					w_PATHISTLIT_CORR.NINDEX = subview.NINDEX
				;

				DELETE
				FROM w_PATHISTLIT_CORR
				WHERE DFIN <= DDEB
				;

				


				

				DROP SEQUENCE IF EXISTS w_PATHISTLIT_seq;
				CREATE TEMP SEQUENCE w_PATHISTLIT_seq;

				DROP TABLE IF EXISTS w_PATHISTLIT;
				CREATE TEMP TABLE w_PATHISTLIT AS
				SELECT *,
					nextval('w_PATHISTLIT_seq'::regclass) AS sequence,
					'1'::text AS entree_directe,
					'1'::text AS sortie_directe,
					date(MVT_DDEB) AS MVT_DDEB_date,
					to_char(MVT_DDEB,'HH24MISS')::numeric AS MVT_DDEB_heure,
					date(MVT_DFIN) AS MVT_DFIN_date,
					to_char(MVT_DFIN,'HH24MISS')::numeric AS MVT_DFIN_heure,
					SEJ_XNCLEUF AS service_code,
					SEJ_XNCLEUF AS uf_code,
					MVT_XNCLESECT AS etage_code,
					MVT_XNCLELIT AS lit_code,
					SEJ_XNCLEUF AS av_service_code,
					SEJ_XNCLEUF AS av_uf_code,
					MVT_XNCLESECT AS av_etage_code,
					SEJ_XNCLEUF AS ap_service_code,
					SEJ_XNCLEUF AS ap_uf_code,
					MVT_XNCLESECT AS ap_etage_code,
					'0'::text AS code_urgence,
					0::bigint AS lieu_id
				FROM
				(
					SELECT 	NHOSPI,
						w_PATHOSP.NCLEHOSP,
						w_PATHOSP.DENTREE AS SEJ_DDEB,
						w_PATHOSP.DSORTIE AS SEJ_DFIN,
						w_PATHOSP.XNCLEUF AS SEJ_XNCLEUF,
						w_PATHOSP.XNCLESECT AS SEJ_XNCLESECT,
						CASE WHEN w_PATHOSP.XNCLELIT <> 0 THEN w_PATHOSP.XNCLELIT ELSE w_PATHOSP.XNCLESECT END AS SEJ_XNCLELIT,
						COALESCE(PATHISTLIT_TD.DDEB,w_PATHOSP.DENTREE) AS MVT_DDEB,
						COALESCE(PATHISTLIT_TD.DFIN,w_PATHOSP.DSORTIE) AS MVT_DFIN,
						COALESCE(PATHISTLIT_TD.XNCLESECT,w_PATHOSP.XNCLESECT) AS MVT_XNCLESECT,
						COALESCE(CASE WHEN PATHISTLIT_TD.XNCLELIT <> 0 THEN PATHISTLIT_TD.XNCLELIT ELSE PATHISTLIT_TD.XNCLESECT END,CASE WHEN w_PATHOSP.XNCLELIT <> 0 THEN w_PATHOSP.XNCLELIT ELSE w_PATHOSP.XNCLESECT END) AS MVT_XNCLELIT
					FROM w_PATHOSP
					LEFT JOIN w_PATHISTLIT_CORR PATHISTLIT_TD ON
						PATHISTLIT_TD.XNCLEHOSP = w_PATHOSP.NCLEHOSP
					ORDER BY NHOSPI, w_PATHOSP.DENTREE, DDEB
				) subview
				; 



				UPDATE w_PATHISTLIT
				SET 
					sortie_directe = '0',
					ap_service_code = w_PATHISTLIT_next.service_code,
					ap_uf_code = w_PATHISTLIT_next.uf_code,
					ap_etage_code = w_PATHISTLIT_next.etage_code
				FROM w_PATHISTLIT w_PATHISTLIT_next
				WHERE w_PATHISTLIT.NHOSPI = w_PATHISTLIT_next.NHOSPI
					AND w_PATHISTLIT.sequence = w_PATHISTLIT_next.sequence - 1
				;


			

				UPDATE w_PATHISTLIT
				SET 
					entree_directe = '0',
					MVT_DDEB = w_PATHISTLIT_before.MVT_DFIN + interval '1 second',
					MVT_DDEB_date = date(w_PATHISTLIT_before.MVT_DFIN + interval '1 second'),
					MVT_DDEB_heure = to_char(w_PATHISTLIT_before.MVT_DFIN + interval '1 second','HH24MISS')::numeric,
					av_service_code = w_PATHISTLIT_before.service_code,
					av_uf_code = w_PATHISTLIT_before.uf_code,
					av_etage_code = w_PATHISTLIT_before.etage_code
				FROM w_PATHISTLIT w_PATHISTLIT_before
				WHERE w_PATHISTLIT.NHOSPI = w_PATHISTLIT_before.NHOSPI
					AND w_PATHISTLIT.sequence = w_PATHISTLIT_before.sequence + 1
				;



				INSERT INTO activite[PX].t_lieux(
							code_original_1, code_original_2, code_original_3, code_original_4, code_original_6)
				SELECT service_code, '', etage_code, lit_code, uf_code
				FROM w_PATHISTLIT
					LEFT JOIN activite[PX].t_lieux ON (
						code_original_1 = service_code AND
						code_original_2 = '' AND
						code_original_3 = etage_code AND
						code_original_4 = lit_code AND
						code_original_6 = uf_code
					)
				WHERE t_lieux.oid IS NULL
				GROUP BY 1,2,3,4,5;


				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
					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
					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_PATHISTLIT
				SET lieu_id = t_lieux.oid
				FROM activite[PX].t_lieux
				WHERE code_original_1 = service_code AND
					code_original_2 = '' AND
					code_original_3 = etage_code AND
					code_original_4 = lit_code AND
					code_original_6 = uf_code
				;


				DROP TABLE IF EXISTS w_mouvements_sejour;

				CREATE TEMP TABLE w_mouvements_sejour AS
				SELECT 
					w_PATHISTLIT.NHOSPI,
					w_PATHISTLIT.NHOSPI 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 = date(w_PATHISTLIT.MVT_DDEB_date) THEN w_PATHISTLIT.MVT_DDEB_heure ELSE 0 END AS heure_debut,
					CASE WHEN p_calendrier.date = date(w_PATHISTLIT.MVT_DFIN_date) THEN w_PATHISTLIT.MVT_DFIN_heure ELSE 240000 END AS heure_fin,
					CASE WHEN entree_directe = '1' AND p_calendrier.date = date(p_sejours.date_entree) AND p_sejours.type_sejour = '1' THEN 1 ELSE 0 END AS nb_entrees_directes,
					CASE WHEN SORTIE_DIRECTE = '1' AND p_calendrier.date = date(p_sejours.date_sortie) AND p_sejours.type_sejour = '1' THEN 1 ELSE 0 END AS nb_sorties_directes,
					CASE WHEN entree_directe = '1' AND p_calendrier.date = date(p_sejours.date_entree) AND p_sejours.type_sejour = '1' THEN '1' ELSE '0' END AS est_jour_entree,
					CASE WHEN SORTIE_DIRECTE = '1' AND p_calendrier.date = date(p_sejours.date_sortie) 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 entree_directe = '1' AND p_calendrier.date =  date(p_sejours.date_entree) AND p_sejours.type_sejour = '2' THEN 1 ELSE 0 END AS nb_ambulatoires,
					CASE WHEN entree_directe = '1' AND p_calendrier.date =  date(p_sejours.date_entree) AND p_sejours.type_sejour = '3' THEN 1 ELSE 0 END AS nb_externes,
					CASE WHEN code_urgence = '1' AND p_calendrier.date =  date(p_sejours.date_entree) AND entree_directe = '1' THEN 1 ELSE 0 END AS nb_urgences,
					CASE WHEN entree_directe = '1' AND p_calendrier.date =  date(p_sejours.date_entree) AND p_sejours.type_sejour = '5' THEN 1 ELSE 0 END AS nb_seances,

					CASE WHEN p_calendrier.date = date(w_PATHISTLIT.MVT_DDEB_date) AND p_sejours.type_sejour = '1' AND entree_directe <> '1' AND service_code <> av_service_code THEN 1 ELSE 0 END AS nb_entrees_mutation_service,
					CASE WHEN p_calendrier.date = date(w_PATHISTLIT.MVT_DFIN_date) AND p_sejours.type_sejour = '1' AND SORTIE_DIRECTE <> '1' AND service_code <> ap_service_code THEN 1 ELSE 0 END AS nb_sorties_mutation_service,
					CASE WHEN p_calendrier.date = date(w_PATHISTLIT.MVT_DDEB_date) AND p_sejours.type_sejour = '1' AND entree_directe <> '1' AND etage_code <> av_etage_code THEN 1 ELSE 0 END AS nb_entrees_mutation_etage,
					CASE WHEN p_calendrier.date = date(w_PATHISTLIT.MVT_DFIN_date) AND p_sejours.type_sejour = '1' AND SORTIE_DIRECTE <> '1' AND etage_code <> ap_etage_code 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 <> date(p_sejours.date_sortie) THEN 1 ELSE 0 END AS nb_jours_js_non_inclus,

					CASE WHEN p_sejours.type_sejour = '1' AND t_lits.chambre_particuliere = 'O' THEN '1' ELSE '0' END AS est_chambre_particuliere,
					CASE WHEN p_sejours.type_sejour = '1' AND t_lits.chambre_particuliere = 'O' 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()) THEN '1' ELSE '0' END AS est_mouvement_previsionnel
				FROM w_PATHISTLIT 
					JOIN activite[PX].p_sejours ON w_PATHISTLIT.NHOSPI = p_sejours.no_sejour AND p_sejours.etat = ''
					LEFT JOIN activite[PX].t_lits ON lit_code = t_lits.code_original
					JOIN base.p_calendrier ON (p_calendrier.date BETWEEN date(w_PATHISTLIT.MVT_DDEB_date) AND date(w_PATHISTLIT.MVT_DFIN_date) AND p_calendrier.date <= now() + interval '1 month' )
				ORDER BY sequence, p_calendrier.date;


				-- Génération
				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;

					DELETE
					FROM w_mouvements_sejour
					WHERE heure_debut = heure_fin
					;


					
				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');

				TRUNCATE activite[PX].p_mouvements_sejour;

				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>

	<NODE name="FACTURE" label="RECUPERATION DES FACTURES">
	
		<NODE label="Entêtes">

			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_PATFACT;
				CREATE TEMP TABLE w_PATFACT AS
				SELECT *
				FROM
				(
				SELECT NHOSPI,
					NIDFACT,
					NETATFACT,
					DDATEFACT,
					NNOFACT,
					NNOFACTORI,
					PATFACT_TD.DDATECR,
					XNCLEHOSP,
					DDATHOSPI,
					DDATENTRE,
					DDATSORTIE,
					date(DENTREE) AS DDATDEBFACT,
					date(DDATFINFACT) AS DDATFINFACT,
					DDATTELTRANS,
					TGDREGCACE
				FROM prod_sano.PATFACT_TD
				JOIN w_PATHOSP ON w_PATHOSP.NCLEHOSP = PATFACT_TD.XNCLEHOSP
				WHERE NNOFACT > 0
				ORDER BY NHOSPI, DDATFINFACT, DDATEFACT
				) subview
				;

				DROP TABLE IF EXISTS w_PATFACT_PER;
				DROP SEQUENCE IF EXISTS w_PATFACT_PER_seq;
				CREATE TEMP SEQUENCE w_PATFACT_PER_seq;
				CREATE TEMP TABLE w_PATFACT_PER AS
				SELECT nextval('w_PATFACT_PER_seq'::regclass) AS sequence,
					*
				FROM
				(
				SELECT XNCLEHOSP, DDATFINFACT, MIN(DDATDEBFACT) AS DDATDEBFACT
				FROM w_PATFACT
				GROUP BY 1,2
				ORDER BY 1,2
				) subview
				;


				UPDATE w_PATFACT_PER
				SET DDATDEBFACT = w_PATFACT_PER_prev.DDATFINFACT + interval '1 day'
				FROM w_PATFACT_PER w_PATFACT_PER_prev
				WHERE w_PATFACT_PER.XNCLEHOSP = w_PATFACT_PER_prev.XNCLEHOSP AND
					w_PATFACT_PER.sequence = w_PATFACT_PER_prev.sequence +1
				;

				UPDATE w_PATFACT
				SET DDATDEBFACT = w_PATFACT_PER.DDATDEBFACT
				FROM w_PATFACT_PER
				WHERE w_PATFACT_PER.XNCLEHOSP = w_PATFACT.XNCLEHOSP AND
					w_PATFACT_PER.DDATFINFACT = w_PATFACT.DDATFINFACT
				;

				


				-- Factures 'normales'
				DROP TABLE IF EXISTS w_factures;
				CREATE TEMP TABLE w_factures AS
				SELECT 
					nextval('activite.s_factures'::regclass) AS oid,
					p_sejours.oid AS sejour_id,
					w_PATFACT.NHOSPI AS no_sejour,
					w_PATFACT.NIDFACT,
					w_PATFACT.NNOFACT::text AS no_facture,
					w_PATFACT.NNOFACT::text AS no_facture_source,
					w_PATFACT.NNOFACT::text AS no_facture_reference,
					'0'::text AS zero_compta,
					0::bigint AS facture_reference_id,
					''::text AS no_facture_od_avoir,
					0::bigint AS facture_od_avoir_id,
					w_PATFACT.DDATDEBFACT AS date_debut,
					w_PATFACT.DDATFINFACT 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 AS code_cp_demandee,
					CASE WHEN w_PATFACT.NNOFACTORI < 1 THEN 'X' ELSE '0'::text END AS type_facture,
					0 AS nb_rejets,
					
					1::numeric AS code_facture,
					w_PATFACT.DDATEFACT AS date_facture,
					'0' AS code_vente,
					'20991231'::date AS date_vente,
					209912::numeric AS mois_vente,
					''::text AS code_cloture,

					CASE WHEN p_sejours.ghs_id > 0 THEN 'GHS' ELSE '' END AS particularite_t2a,
					
					p_sejours.tiers_payant_0_id,
					p_sejours.tiers_payant_1_id,
					p_sejours.tiers_payant_2_id,
					p_sejours.tiers_payant_22_id,

					0::numeric AS montant_facture_c,
					0::numeric AS montant_facture_h,
					0::numeric AS montant_facture_0_c,
					0::numeric AS montant_facture_0_h,
					0::numeric AS montant_facture_1_c,
					0::numeric AS montant_facture_1_h,
					0::numeric AS montant_facture_2_c,
					0::numeric AS montant_facture_2_h,
					0::numeric AS montant_facture_22_c,
					0::numeric AS montant_facture_22_h,
					0::numeric AS montant_comptabilise_c,
					0::numeric AS montant_comptabilise_h,
					0::numeric AS montant_comptabilise_0_c,
					0::numeric AS montant_comptabilise_0_h,
					0::numeric AS montant_comptabilise_1_c,
					0::numeric AS montant_comptabilise_1_h,
					0::numeric AS montant_comptabilise_2_c,
					0::numeric AS montant_comptabilise_2_h,
					0::numeric AS montant_comptabilise_22_c,
					0::numeric AS montant_comptabilise_22_h,
					0::numeric AS montant_regle_c,
					0::numeric AS montant_regle_h,
					0::numeric AS montant_regle_0_c,
					0::numeric AS montant_regle_0_h,
					0::numeric AS montant_regle_1_c,
					0::numeric AS montant_regle_1_h,
					0::numeric AS montant_regle_2_c,
					0::numeric AS montant_regle_2_h,
					0::numeric AS montant_regle_22_c,
					0::numeric AS montant_regle_22_h,
					'1'::text AS code_expedie_0,
					CASE WHEN DDATTELTRANS IS NOT NULL THEN '1' ELSE '0' END AS code_expedie_1,
					CASE WHEN DDATTELTRANS IS NOT NULL THEN '1' ELSE '0' END AS code_expedie_2,
					0::numeric AS code_expedie_22,
					CASE WHEN DDATTELTRANS > '00010101' THEN DDATTELTRANS ELSE '00010101' END AS date_expedition,
					w_PATFACT.DDATEFACT AS date_expedition_0,
					CASE WHEN DDATTELTRANS > '00010101' THEN DDATTELTRANS ELSE '00010101' END AS date_expedition_1,
					CASE WHEN DDATTELTRANS > '00010101' THEN DDATTELTRANS ELSE '00010101' END AS date_expedition_2,
					'00010101'::date AS date_expedition_22,
					0 AS no_bordereau_0,
					0 AS no_bordereau_1,
					0 AS no_bordereau_2,
					0::numeric AS no_bordereau_22,

					LEAST('20991231'::date) AS date_solde,
					LEAST('20991231'::date) AS date_solde_c,
					LEAST('20991231'::date) AS date_solde_h,
					LEAST('20991231'::date) AS date_solde_0_c,
					LEAST('20991231'::date) AS date_solde_0_h,
					LEAST('20991231'::date) AS date_solde_1_c,
					LEAST('20991231'::date) AS date_solde_1_h,
					LEAST('20991231'::date) AS date_solde_2_c,
					LEAST('20991231'::date) AS date_solde_2_h,
					'20991231'::date AS date_solde_22_c,
					'20991231'::date AS date_solde_22_h,


					0::numeric AS montant_encours_c,
					0::numeric AS montant_encours_0_c,
					0::numeric AS montant_encours_1_c,
					0::numeric AS montant_encours_2_c,
					0::numeric AS montant_encours_22_c,
					0::numeric AS montant_encours_h,
					0::numeric AS montant_encours_0_h,
					0::numeric AS montant_encours_1_h,
					0::numeric AS montant_encours_2_h,
					0::numeric AS montant_encours_22_h,

					'20991231'::date AS date_solde_reference,
					'20991231'::date AS date_solde_reference_c,
					'20991231'::date AS date_solde_reference_h,
					'20991231'::date AS date_solde_reference_0_c,
					'20991231'::date AS date_solde_reference_1_c,
					'20991231'::date AS date_solde_reference_2_c,
					'20991231'::date AS date_solde_reference_22_c,
					'20991231'::date AS date_solde_reference_0_h,
					'20991231'::date AS date_solde_reference_1_h,
					'20991231'::date AS date_solde_reference_2_h,
					'20991231'::date AS date_solde_reference_22_h,

					0::numeric AS delai_facture,
					0::numeric AS delai_solde,
					0::numeric AS delai_expedition,
					0::numeric AS delai_expedition_0,
					0::numeric AS delai_expedition_1,
					0::numeric AS delai_expedition_2,
					0::numeric AS delai_expedition_22,
					0::numeric AS delai_solde_0_c,
					0::numeric AS delai_solde_0_h,
					0::numeric AS delai_solde_1_c,
					0::numeric AS delai_solde_1_h,
					0::numeric AS delai_solde_2_c,
					0::numeric AS delai_solde_2_h,
					0::numeric AS delai_solde_22_c,
					0::numeric AS delai_solde_22_h,


					'20991231'::date AS date_encours,
					0::numeric AS montant_facture_c_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_h_actes_inclus_dans_sejour,
					0::numeric AS montant_comptabilise_budget_global_c

				FROM w_PATFACT
					JOIN activite[PX].p_sejours ON w_PATFACT.XNCLEHOSP = p_sejours.code_original AND p_sejours.etat = ''
				;

				
				
				-- Avoirs
				INSERT INTO w_factures
				SELECT 
					nextval('activite.s_factures'::regclass) AS oid,
					p_sejours.oid AS sejour_id,
					w_PATFACT.NHOSPI AS no_sejour,
					w_PATFACT.NIDFACT,
					'A'||w_PATFACT.NNOFACT AS no_facture,
					w_PATFACT.NNOFACT AS no_facture_source,
					w_PATFACT.NNOFACT AS no_facture_reference,
					'0'::text AS zero_compta,
					0::bigint AS facture_reference_id,
					COALESCE(w_PATFACT_refact.NNOFACT::text,'') AS no_facture_od_avoir,
					0::bigint AS facture_od_avoir_id,
					w_PATFACT.DDATDEBFACT AS date_debut,
					w_PATFACT.DDATFINFACT 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 AS code_cp_demandee,
					'1' AS type_facture,
					1 AS nb_rejets,
					
					1::numeric AS code_facture,
					COALESCE(w_PATFACT_refact.DDATEFACT,w_PATFACT.DDATEFACT) AS date_facture,
					'0' AS code_vente,
					'20991231'::date AS date_vente,
					209912::numeric AS mois_vente,
					''::text AS code_cloture,

					CASE WHEN p_sejours.ghs_id > 0 THEN 'GHS' ELSE '' END AS particularite_t2a,
					
					p_sejours.tiers_payant_0_id,
					p_sejours.tiers_payant_1_id,
					p_sejours.tiers_payant_2_id,
					p_sejours.tiers_payant_22_id,

					0::numeric AS montant_facture_c,
					0::numeric AS montant_facture_h,
					0::numeric AS montant_facture_0_c,
					0::numeric AS montant_facture_0_h,
					0::numeric AS montant_facture_1_c,
					0::numeric AS montant_facture_1_h,
					0::numeric AS montant_facture_2_c,
					0::numeric AS montant_facture_2_h,
					0::numeric AS montant_facture_22_c,
					0::numeric AS montant_facture_22_h,
					0::numeric AS montant_comptabilise_c,
					0::numeric AS montant_comptabilise_h,
					0::numeric AS montant_comptabilise_0_c,
					0::numeric AS montant_comptabilise_0_h,
					0::numeric AS montant_comptabilise_1_c,
					0::numeric AS montant_comptabilise_1_h,
					0::numeric AS montant_comptabilise_2_c,
					0::numeric AS montant_comptabilise_2_h,
					0::numeric AS montant_comptabilise_22_c,
					0::numeric AS montant_comptabilise_22_h,
					0::numeric AS montant_regle_c,
					0::numeric AS montant_regle_h,
					0::numeric AS montant_regle_0_c,
					0::numeric AS montant_regle_0_h,
					0::numeric AS montant_regle_1_c,
					0::numeric AS montant_regle_1_h,
					0::numeric AS montant_regle_2_c,
					0::numeric AS montant_regle_2_h,
					0::numeric AS montant_regle_22_c,
					0::numeric AS montant_regle_22_h,
					'1'::text AS code_expedie_0,
					CASE WHEN w_PATFACT.DDATTELTRANS IS NOT NULL THEN '1' ELSE '0' END AS code_expedie_1,
					CASE WHEN w_PATFACT.DDATTELTRANS IS NOT NULL THEN '1' ELSE '0' END AS code_expedie_2,
					0::numeric AS code_expedie_22,
					CASE WHEN w_PATFACT.DDATTELTRANS > '00010101' THEN w_PATFACT.DDATTELTRANS ELSE '00010101' END AS date_expedition,
					w_PATFACT.DDATEFACT AS date_expedition_0,
					CASE WHEN w_PATFACT.DDATTELTRANS > '00010101' THEN w_PATFACT.DDATTELTRANS ELSE '00010101' END AS date_expedition_1,
					CASE WHEN w_PATFACT.DDATTELTRANS > '00010101' THEN w_PATFACT.DDATTELTRANS ELSE '00010101' END AS date_expedition_2,
					'00010101'::date AS date_expedition_22,
					0 AS no_bordereau_0,
					0 AS no_bordereau_1,
					0 AS no_bordereau_2,
					0::numeric AS no_bordereau_22,

					LEAST('20991231'::date) AS date_solde,
					LEAST('20991231'::date) AS date_solde_c,
					LEAST('20991231'::date) AS date_solde_h,
					LEAST('20991231'::date) AS date_solde_0_c,
					LEAST('20991231'::date) AS date_solde_0_h,
					LEAST('20991231'::date) AS date_solde_1_c,
					LEAST('20991231'::date) AS date_solde_1_h,
					LEAST('20991231'::date) AS date_solde_2_c,
					LEAST('20991231'::date) AS date_solde_2_h,
					'20991231'::date AS date_solde_22_c,
					'20991231'::date AS date_solde_22_h,


					0::numeric AS montant_encours_c,
					0::numeric AS montant_encours_0_c,
					0::numeric AS montant_encours_1_c,
					0::numeric AS montant_encours_2_c,
					0::numeric AS montant_encours_22_c,
					0::numeric AS montant_encours_h,
					0::numeric AS montant_encours_0_h,
					0::numeric AS montant_encours_1_h,
					0::numeric AS montant_encours_2_h,
					0::numeric AS montant_encours_22_h,

					'20991231'::date AS date_solde_reference,
					'20991231'::date AS date_solde_reference_c,
					'20991231'::date AS date_solde_reference_h,
					'20991231'::date AS date_solde_reference_0_c,
					'20991231'::date AS date_solde_reference_1_c,
					'20991231'::date AS date_solde_reference_2_c,
					'20991231'::date AS date_solde_reference_22_c,
					'20991231'::date AS date_solde_reference_0_h,
					'20991231'::date AS date_solde_reference_1_h,
					'20991231'::date AS date_solde_reference_2_h,
					'20991231'::date AS date_solde_reference_22_h,

					0::numeric AS delai_facture,
					0::numeric AS delai_solde,
					0::numeric AS delai_expedition,
					0::numeric AS delai_expedition_0,
					0::numeric AS delai_expedition_1,
					0::numeric AS delai_expedition_2,
					0::numeric AS delai_expedition_22,
					0::numeric AS delai_solde_0_c,
					0::numeric AS delai_solde_0_h,
					0::numeric AS delai_solde_1_c,
					0::numeric AS delai_solde_1_h,
					0::numeric AS delai_solde_2_c,
					0::numeric AS delai_solde_2_h,
					0::numeric AS delai_solde_22_c,
					0::numeric AS delai_solde_22_h,


					'20991231'::date AS date_encours,
					0::numeric AS montant_facture_c_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_h_actes_inclus_dans_sejour,
					0::numeric AS montant_comptabilise_budget_global_c

				FROM w_PATFACT
					LEFT JOIN prod_sano.PATFACT_TD  w_PATFACT_refact ON w_PATFACT.NNOFACT = ABS(w_PATFACT_refact.NNOFACTORI)
					JOIN activite[PX].p_sejours ON w_PATFACT.XNCLEHOSP = p_sejours.code_original AND p_sejours.etat = ''
				WHERE w_PATFACT.NNOFACTORI < 0
				
				;
				
				DROP TABLE IF EXISTS w_PATFACTCPTA_TD;
				CREATE TEMP TABLE w_PATFACTCPTA_TD AS
				SELECT NNOFACT::text AS NNOFACT_CTI, ''::text AS type_tiers_payant, 
					'0'::text AS zero_compta,
					*
				FROM prod_sano.PATFACTCPTA_TD
				;
				
				UPDATE w_PATFACTCPTA_TD SET
					zero_compta = '1'
				FROM 
				(
				SELECT XNIDFACT, date(DDATECR) AS DDATECR, SUM(CSOMDUE)
				FROM  w_PATFACTCPTA_TD
				WHERE CSOMDUE <> 0
				GROUP BY 1,2
				HAVING SUM(CSOMDUE) = 0
				) subview
				WHERE w_PATFACTCPTA_TD.XNIDFACT = subview.XNIDFACT AND
					date(w_PATFACTCPTA_TD.DDATECR) = subview.DDATECR
					;
				

				UPDATE w_PATFACTCPTA_TD
				SET NNOFACT_CTI = 'A'||NNOFACT_CTI
				FROM
				(
				SELECT NNOFACT, date(DDATECR) AS DDATECR
				FROM w_PATFACTCPTA_TD
				JOIN w_factures ON w_PATFACTCPTA_TD.XNIDFACT = w_factures.NIDFACT AND 
					no_facture = 'A'||w_PATFACTCPTA_TD.NNOFACT
				WHERE w_PATFACTCPTA_TD.zero_compta <> '1' AND NTYPLIG < 0
				GROUP BY 1,2
				HAVING SUM(CSOMDUE) <> 0
				) subview
				WHERE w_PATFACTCPTA_TD.NNOFACT = subview.NNOFACT AND
					date(w_PATFACTCPTA_TD.DDATECR) = subview.DDATECR AND
					CSOMDUE <> 0
				;


				UPDATE w_PATFACTCPTA_TD
				SET type_tiers_payant =
					CASE 
					WHEN TREFCPTE LIKE 'T%' THEN '1'
					WHEN TREFCPTE LIKE 'C%' THEN '2'
					WHEN TREFCPTE LIKE 'P%' THEN '0'
					ELSE '' END
				WHERE substr(TREFCPTE,1,1) in ('T','C','P')
				;

				UPDATE w_PATFACTCPTA_TD
				SET type_tiers_payant = t_tiers_payant.type_tiers_payant
				FROM activite[PX].t_tiers_payant
				WHERE t_tiers_payant.code_original = TREFCPTE AND
					w_PATFACTCPTA_TD.type_tiers_payant = ''
				;

				
				UPDATE w_factures
				SET code_vente = '1',
					date_vente = date(DDATECR),
					mois_vente = to_char(date(DDATECR),'YYYYMM')::numeric
				FROM
				(
				SELECT NNOFACT_CTI, MIN(DDATECR) AS DDATECR
				FROM w_PATFACTCPTA_TD
				WHERE CSOMDUE <> 0 AND zero_compta <> '1'
				GROUP BY 1
				HAVING SUM(CSOMDUE) <> 0
				) subview
				WHERE w_factures.no_facture = NNOFACT_CTI AND
					date_vente <> date(DDATECR)
				;
				
				-- Sur les factures qui d'annulent le même jour, enlever montant comptabilisé
				UPDATE w_factures
				SET zero_compta = '1'::text
				WHERE NIDFACT IN (SELECT XNIDFACT FROM w_PATFACTCPTA_TD  WHERE zero_compta = '1')
				;

				drop table if exists temp.w_w_factures; create table temp.w_w_factures AS select * from w_factures;
				


				-- Ajout des factures
				
				
				UPDATE w_factures SET
					montant_facture_1_c = CASE WHEN type_facture <> '1' THEN subview.montant_facture_1 ELSE 0 - subview.montant_facture_1 END,
					montant_facture_2_c = CASE WHEN type_facture <> '1' THEN subview.montant_facture_2 ELSE 0 - subview.montant_facture_2 END,
					montant_facture_0_c = CASE WHEN type_facture <> '1' THEN subview.montant_facture_0 ELSE 0 - subview.montant_facture_0 END,
					montant_comptabilise_1_c = CASE WHEN zero_compta = '1' THEN 0 WHEN type_facture <> '1' THEN subview.montant_facture_1 ELSE 0 - subview.montant_facture_1 END,
					montant_comptabilise_2_c = CASE WHEN zero_compta = '1' THEN 0 WHEN type_facture <> '1' THEN subview.montant_facture_2 ELSE 0 - subview.montant_facture_2 END,
					montant_comptabilise_0_c = CASE WHEN zero_compta = '1' THEN 0 WHEN type_facture <> '1' THEN subview.montant_facture_0 ELSE 0 - subview.montant_facture_0 END
				FROM
				(
				SELECT XNIDFACT, 
					SUM(CPRIXACTSS) AS montant_facture_1, 
					SUM(CPRIXACTCMP) AS montant_facture_2, 
					SUM(CPRIXACTPAT) AS montant_facture_0
				FROm prod_sano.PATFACTACT_TD
				GROUP BY 1
				) subview
				WHERE w_factures.NIDFACT = subview.XNIDFACT
				;


				

				UPDATE w_factures SET
					montant_facture_c = montant_facture_1_c + montant_facture_2_c + montant_facture_0_c,
					montant_comptabilise_c = montant_comptabilise_1_c + montant_comptabilise_2_c + montant_comptabilise_0_c,
					montant_facture_h = montant_facture_1_h + montant_facture_2_h + montant_facture_0_h,
					montant_comptabilise_h = montant_comptabilise_1_h + montant_comptabilise_2_h + montant_comptabilise_0_h
				;		

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


				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,


					montant_encours_c,
					montant_encours_0_c,
					montant_encours_1_c,
					montant_encours_2_c,
					montant_encours_22_c,
					montant_encours_h,
					montant_encours_0_h,
					montant_encours_1_h,
					montant_encours_2_h,
					montant_encours_22_h,

					date_solde_reference,
					date_solde_reference_c,
					date_solde_reference_h,
					date_solde_reference_0_c,
					date_solde_reference_1_c,
					date_solde_reference_2_c,
					date_solde_reference_22_c,
					date_solde_reference_0_h,
					date_solde_reference_1_h,
					date_solde_reference_2_h,
					date_solde_reference_22_h,

					delai_facture,
					delai_solde,
					delai_expedition,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					delai_expedition_22,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2_c,
					delai_solde_2_h,
					delai_solde_22_c,
					delai_solde_22_h,


					date_encours,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					montant_comptabilise_budget_global_c

					)

				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,


					montant_encours_c,
					montant_encours_0_c,
					montant_encours_1_c,
					montant_encours_2_c,
					montant_encours_22_c,
					montant_encours_h,
					montant_encours_0_h,
					montant_encours_1_h,
					montant_encours_2_h,
					montant_encours_22_h,

					'20991231', 
					'20991231', 
					'20991231', 
					'20991231', 
					'20991231', 
					'20991231', 
					'20991231', 
					'20991231', 
					'20991231', 
					'20991231', 
					'20991231', 

					delai_facture,
					delai_solde,
					delai_expedition,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					delai_expedition_22,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2_c,
					delai_solde_2_h,
					delai_solde_22_c,
					delai_solde_22_h,

					date_encours,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					montant_comptabilise_budget_global_c

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

				UPDATE activite[PX].p_factures
				SET date_fin = p_sejours.date_sortie
				FROM activite[PX].p_sejours
				WHERE p_factures.no_sejour = p_sejours.no_sejour AND
					p_factures.date_fin > p_sejours.date_sortie
				;

				UPDATE activite[PX].p_factures
				SET date_debut = p_sejours.date_entree
				FROM activite[PX].p_sejours
				WHERE p_factures.no_sejour = p_sejours.no_sejour AND
					p_factures.date_debut < p_sejours.date_entree
					;
				
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_3');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_4');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_5');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_6');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_7');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_8');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_9');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_10');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_11');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_12');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_13');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_14');

			
				
				
				UPDATE activite[PX].p_factures
				SET facture_od_avoir_id = p_factures_od_avoir.oid
				FROM activite[PX].p_factures p_factures_od_avoir
				WHERE p_factures.no_facture_od_avoir = p_factures_od_avoir.no_facture AND
					p_factures.no_facture_od_avoir <> '';
					
					
					
				UPDATE activite[PX].p_sejours
				SET est_sans_facturation = '1' 
				WHERE lieu_sortie_id IN 
					(SELECT oid FROM activite[PX].t_lieux WHERE 
						service_facturation_id IN
						(SELECT oid FROM activite[PX].t_services_facturation WHERE est_sans_facturation = '1') OR
						unite_fonctionnelle_id IN
						(SELECT oid FROM activite[PX].t_unites_fonctionnelles WHERE est_sans_facturation = '1') 
					);
				
				
				-- 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'
				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, 
					'E' || no_sejour || CASE WHEN avec_facturation_intermediaire = '1' THEN '_' || p_calendrier.mois ELSE '' END, 
					'E' || no_sejour || 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
				HAVING MAX(LEAST(date_fin_encours, p_calendrier.date)) >= '[ENV_ADM_ANNEEDEBUT]0101' 
				;
				
									
										
			]]></sqlcmd>
		</NODE>
		
		<NODEx label="Rejets">
			<sqlcmd><![CDATA[

				
										
			]]></sqlcmd>
		</NODEx>
		
		<NODE label="Lignes non facturées Clinique">
			<sqlcmd><![CDATA[
			
				
				
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes facturées Clinique">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_lignes_c_1');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_lignes_c_2');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_lignes_c_3');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_lignes_c_4');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_lignes_c_5');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_lignes_c_6');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_lignes_c_7');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_lignes_c_8');
				
				INSERT INTO activite[PX].p_factures_lignes_c(
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					coefficient_mco,
					rubrique_facturation_id,
					prestation_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,
					nb_prestation,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					rubrique_comptabilisation_id,
					origine_facturation_id,
					facture_id,
					compte_produit_id
				    )
				SELECT
					w_factures.no_facture,
					DDATDACT AS date_debut,
					DDATFACT AS date_fin,
					CASE WHEN type_facture = '1' THEN -1 ELSE 1 END * PATFACTACT_TD.NQTACTE AS nb_rubrique,
					1::numeric AS coefficient,
					1::numeric AS coefficient_mco,
					COALESCE(t_rubriques_facturation.oid,0) AS rubrique_facturation_id,
					COALESCE(t_prestations.oid,0) AS prestation_id,
					CASE WHEN type_facture = '1' THEN -1 ELSE 1 END * (CPRIXACTSS + CPRIXACTCMP + CPRIXACTPAT) AS montant_facture,
					CASE WHEN type_facture = '1' THEN -1 ELSE 1 END * CPRIXACTPAT AS montant_facture_0,
					CASE WHEN type_facture = '1' THEN -1 ELSE 1 END * CPRIXACTSS AS montant_facture_1,
					CASE WHEN type_facture = '1' THEN -1 ELSE 1 END * CPRIXACTCMP AS montant_facture_2,
					0::numeric AS montant_facture_22,
					CASE WHEN CPRIXACTCMP = 0 AND CPRIXACTPAT <> 0 THEN 100-(NTAUXSS*100) ELSE 0 END AS taux_0,
					NTAUXSS*100 AS taux_1,
					CASE WHEN CPRIXACTCMP <> 0 THEN 100-(NTAUXSS*100) ELSE 0 END AS taux_2,
					0::numeric AS taux_22,
					PATFACTACT_TD.CPUACTHT,
					0::bigint AS lieu_id,
					CASE WHEN type_facture = '1' THEN -1 ELSE 1 END * PATFACTACT_TD.NQTACTE AS nb_prestation,
					CASE WHEN zero_compta = '1' THEN 0 WHEN type_facture = '1' THEN -1 ELSE 1 END * (CPRIXACTSS + CPRIXACTCMP + CPRIXACTPAT) AS montant_comptabilise,
					CASE WHEN zero_compta = '1' THEN 0 WHEN type_facture = '1' THEN -1 ELSE 1 END * CPRIXACTPAT AS montant_comptabilise_0,
					CASE WHEN zero_compta = '1' THEN 0 WHEN type_facture = '1' THEN -1 ELSE 1 END * CPRIXACTSS AS montant_comptabilise_1,
					CASE WHEN zero_compta = '1' THEN 0 WHEN type_facture = '1' THEN -1 ELSE 1 END * CPRIXACTCMP AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22,
					COALESCE(t_rubriques_facturation.oid,0) AS rubrique_comptabilisation_id,
					1::bigint AS origine_facturation_id,
					w_factures.oid AS facture_id,
					0::bigint AS compte_produit_id
				FROM prod_sano.PATFACTACT_TD
				JOIN w_factures ON w_factures.NIDFACT = PATFACTACT_TD.XNIDFACT
				LEFT JOIN activite[PX].t_rubriques_facturation ON PATFACTACT_TD.XNNUMACT = t_rubriques_facturation.code_original
				LEFT JOIN activite.t_prestations ON PATFACTACT_TD.TCODTELE = t_prestations.code
				;
				
				-- Regrouper Rubriques avec code en double
				DROP TABLE IF EXISTS w_rubriques;
				CREATE TEMP TABLE  w_rubriques AS
				SELECT code, (MIN(Array[CASE WHEN statut = 'P' THEN 0 ELSE 1 END::numeric,oid::numeric]))[2]::bigint AS oid, (MIN(Array[to_char(oid,'FM000000000000'),texte]))[2] AS texte, base.cti_array_accum(oid) AS oid_array
				FROM activite[PX].t_rubriques_facturation
				GROUP BY 1
				;

				UPDATE activite[PX].t_rubriques_facturation
				SET statut = CASE WHEN t_rubriques_facturation.oid = w_rubriques.oid THEN 'P' ELSE '' END
				FROM w_rubriques
				WHERE t_rubriques_facturation.oid = ANY(oid_array) AND
					statut IS DISTINCT FROM CASE WHEN t_rubriques_facturation.oid = w_rubriques.oid THEN 'P' ELSE '' END
				;
					

				UPDATE activite[PX].p_factures_lignes_c
				SET rubrique_facturation_id = w_rubriques.oid, rubrique_comptabilisation_id = w_rubriques.oid
				FROM w_rubriques
				WHERE p_factures_lignes_c.rubrique_facturation_id = ANY(oid_array) AND
					p_factures_lignes_c.rubrique_facturation_id <> w_rubriques.oid
				;



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

				CREATE TEMP TABLE w_factures_lignes_sup_c AS
				SELECT
					p_factures_lignes_c.CTID AS from_CTID, 
					t_prestations.code AS prestation_code,
					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 t_prestations.code = 'GHS' THEN date(date_entree + interval '1 day') ELSE date(p_factures_lignes_c.date_debut + interval '1 day') END AS date_debut_det,
					CASE WHEN t_prestations.code = 'GHS' THEN date(p_sejours.date_sortie - interval '1 day') ELSE date(p_factures_lignes_c.date_debut + ((ABS(nb_prestation)-1) || ' day')::interval)  END AS date_fin_det,
					ABS(CASE WHEN t_prestations.code = 'GHS' THEN date(date_sortie) - date(date_entree) ELSE nb_prestation END) AS nb_det,
					p_factures_lignes_c.*
				FROM activite[PX].p_factures_lignes_c
					JOIN activite[PX].p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture
					JOIN activite[PX].p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
					JOIN activite.t_prestations ON prestation_id = t_prestations.oid  AND 
						(
						code IN ('PJ', 'FJ', 'SSM', 'GHS', 'EXH', 'SRC', 'REA', 'STF', 'NN1', 'NN2', 'NN3', 'CP', 'CPC', 'SHO') OR
						t_prestations.type_ventilation_jour = '1'
						)
				WHERE (nb_prestation > 1 or nb_prestation < -1 OR (t_prestations.code = 'GHS' AND p_sejours.type_sejour != '5' 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 activite[PX].p_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
					from_CTID,
					no_facture,
					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,
					coefficient,
					coefficient_mco,
					rubrique_facturation_id,
					prestation_id,
					compte_produit_id,
					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,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					finess,
					prix_unitaire,
					lieu_id,
					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_comptabilisation_id,
					round(montant_encours / ABS(nb_det),2) AS montant_encours,
					round(montant_encours_0 / ABS(nb_det),2) AS montant_encours_0,
					round(montant_encours_1 / ABS(nb_det),2) AS montant_encours_1,
					round(montant_encours_2 / ABS(nb_det),2) AS montant_encours_2,
					round(montant_encours_22 / ABS(nb_det),2) AS montant_encours_22,
					round(montant_facture_actes_inclus_dans_sejour / ABS(nb_det),2) AS montant_facture_actes_inclus_dans_sejour,
					round(montant_facture_0_actes_inclus_dans_sejour / ABS(nb_det),2) AS montant_facture_0_actes_inclus_dans_sejour,
					round(montant_facture_1_actes_inclus_dans_sejour / ABS(nb_det),2) AS montant_facture_1_actes_inclus_dans_sejour,
					round(montant_facture_2_actes_inclus_dans_sejour / ABS(nb_det),2) AS montant_facture_2_actes_inclus_dans_sejour,
					round(montant_facture_22_actes_inclus_dans_sejour / ABS(nb_det),2) AS montant_facture_22_actes_inclus_dans_sejour,
					origine_facturation_id,
					round(montant_comptabilise_budget_global_1 / ABS(nb_det),2) AS montant_comptabilise_budget_global_1,
					facture_id
				FROM w_factures_lignes_sup_c
					JOIN base.p_calendrier ON date_debut BETWEEN p_calendrier.date - ABS(nb_det)::integer + 1 AND p_calendrier.date AND 
								p_calendrier.date <> date_debut ;
				;


				DROP TABLE IF EXISTS w_factures_lignes_sup_c_sup_tot;

				CREATE TEMP TABLE w_factures_lignes_sup_c_sup_tot AS

				SELECT from_CTID,
					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,
					SUM(montant_encours) AS montant_encours,
					SUM(montant_encours_0) AS montant_encours_0,
					SUM(montant_encours_1) AS montant_encours_1,
					SUM(montant_encours_2) AS montant_encours_2,
					SUM(montant_encours_22) AS montant_encours_22,
					SUM(montant_facture_actes_inclus_dans_sejour) AS montant_facture_actes_inclus_dans_sejour,
					SUM(montant_facture_0_actes_inclus_dans_sejour) AS montant_facture_0_actes_inclus_dans_sejour,
					SUM(montant_facture_1_actes_inclus_dans_sejour) AS montant_facture_1_actes_inclus_dans_sejour,
					SUM(montant_facture_2_actes_inclus_dans_sejour) AS montant_facture_2_actes_inclus_dans_sejour,
					SUM(montant_facture_22_actes_inclus_dans_sejour) AS montant_facture_22_actes_inclus_dans_sejour,
					SUM(montant_comptabilise_budget_global_1) AS montant_comptabilise_budget_global_1
				FROM w_factures_lignes_sup_c_sup
				GROUP BY 1;


				UPDATE activite[PX].p_factures_lignes_c
				SET
					date_fin = p_factures_lignes_c.date_debut,
					nb_rubrique = p_factures_lignes_c.nb_rubrique - w_factures_lignes_sup_c_sup_tot.nb_rubrique,
					nb_prestation = p_factures_lignes_c.nb_prestation - w_factures_lignes_sup_c_sup_tot.nb_prestation,
					montant_facture = p_factures_lignes_c.montant_facture - w_factures_lignes_sup_c_sup_tot.montant_facture,
					montant_facture_0 = p_factures_lignes_c.montant_facture_0 - w_factures_lignes_sup_c_sup_tot.montant_facture_0,
					montant_facture_1 = p_factures_lignes_c.montant_facture_1 - w_factures_lignes_sup_c_sup_tot.montant_facture_1,
					montant_facture_2 = p_factures_lignes_c.montant_facture_2 - w_factures_lignes_sup_c_sup_tot.montant_facture_2,
					montant_facture_22 = p_factures_lignes_c.montant_facture_22 - w_factures_lignes_sup_c_sup_tot.montant_facture_22,
					montant_comptabilise = p_factures_lignes_c.montant_comptabilise - w_factures_lignes_sup_c_sup_tot.montant_comptabilise,
					montant_comptabilise_0 = p_factures_lignes_c.montant_comptabilise_0 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_0,
					montant_comptabilise_1 = p_factures_lignes_c.montant_comptabilise_1 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_1,
					montant_comptabilise_2 = p_factures_lignes_c.montant_comptabilise_2 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_2,
					montant_comptabilise_22 = p_factures_lignes_c.montant_comptabilise_22 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_22,
					montant_encours = p_factures_lignes_c.montant_encours - w_factures_lignes_sup_c_sup_tot.montant_encours,
					montant_encours_0 = p_factures_lignes_c.montant_encours_0 - w_factures_lignes_sup_c_sup_tot.montant_encours_0,
					montant_encours_1 = p_factures_lignes_c.montant_encours_1 - w_factures_lignes_sup_c_sup_tot.montant_encours_1,
					montant_encours_2 = p_factures_lignes_c.montant_encours_2 - w_factures_lignes_sup_c_sup_tot.montant_encours_2,
					montant_encours_22 = p_factures_lignes_c.montant_encours_22 - w_factures_lignes_sup_c_sup_tot.montant_encours_22,
					montant_facture_actes_inclus_dans_sejour = p_factures_lignes_c.montant_facture_actes_inclus_dans_sejour - w_factures_lignes_sup_c_sup_tot.montant_facture_actes_inclus_dans_sejour,
					montant_facture_0_actes_inclus_dans_sejour = p_factures_lignes_c.montant_facture_0_actes_inclus_dans_sejour - w_factures_lignes_sup_c_sup_tot.montant_facture_0_actes_inclus_dans_sejour,
					montant_facture_1_actes_inclus_dans_sejour = p_factures_lignes_c.montant_facture_1_actes_inclus_dans_sejour - w_factures_lignes_sup_c_sup_tot.montant_facture_1_actes_inclus_dans_sejour,
					montant_facture_2_actes_inclus_dans_sejour = p_factures_lignes_c.montant_facture_2_actes_inclus_dans_sejour - w_factures_lignes_sup_c_sup_tot.montant_facture_2_actes_inclus_dans_sejour,
					montant_facture_22_actes_inclus_dans_sejour = p_factures_lignes_c.montant_facture_22_actes_inclus_dans_sejour - w_factures_lignes_sup_c_sup_tot.montant_facture_22_actes_inclus_dans_sejour,
					montant_comptabilise_budget_global_1 = p_factures_lignes_c.montant_comptabilise_budget_global_1 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_budget_global_1
				FROM w_factures_lignes_sup_c_sup_tot
				WHERE p_factures_lignes_c.CTID = from_CTID;
                    
                    

				INSERT INTO activite[PX].p_factures_lignes_c(
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					coefficient_mco,
					rubrique_facturation_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,
					finess,
					prix_unitaire,
					lieu_id,
					nb_prestation,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					rubrique_comptabilisation_id,
					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,
					origine_facturation_id,
					montant_comptabilise_budget_global_1,
					facture_id)
				SELECT 
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					coefficient_mco,
					rubrique_facturation_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,
					finess,
					prix_unitaire,
					lieu_id,
					nb_prestation,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					rubrique_comptabilisation_id,
					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,
					origine_facturation_id,
					montant_comptabilise_budget_global_1,
					facture_id
				FROM w_factures_lignes_sup_c_sup;


				-- Lieu exécution
				
				UPDATE activite[PX].p_factures_lignes_c
				SET lieu_id = p_mouvements_sejour.lieu_id
				FROM 
					activite[PX].p_factures , 
					activite[PX].p_mouvements_sejour
				WHERE
					p_factures_lignes_c.no_facture = p_factures.no_facture AND
					p_factures.no_sejour = p_mouvements_sejour.no_sejour AND
					p_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
					p_factures_lignes_c.lieu_id <> p_mouvements_sejour.lieu_id;


					UPDATE activite[PX].p_factures_lignes_c
				SET lieu_id = p_sejours.lieu_sortie_id
				FROM 
					activite[PX].p_factures 
				JOIN activite[PX].p_sejours ON p_factures.no_sejour = p_sejours.no_sejour 
				WHERE
					p_factures_lignes_c.no_facture = p_factures.no_facture AND
					p_factures_lignes_c.lieu_id = 0;
				
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_lignes_c_1');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_lignes_c_2');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_lignes_c_3');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_lignes_c_4');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_lignes_c_5');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_lignes_c_6');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_lignes_c_7');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_lignes_c_8');


                SELECT activite[PX].cti_reorganize_rubrique_facture_c('activite[PX].p_factures_lignes_c');	

				;
				
				-- Factures référence
				DROP TABLE IF EXISTS w_sejours_facture_reference;

				CREATE TEMP TABLE w_sejours_facture_reference AS
				SELECT 
					p_sejours.no_sejour,
					CASE WHEN avec_facturation_intermediaire IS DISTINCT FROM '1' THEN p_sejours.date_sortie ELSE p_factures.date_fin END AS date_fin,
					MIN(CASE WHEN avec_facturation_intermediaire IS DISTINCT FROM '1' THEN p_sejours.date_entree ELSE p_factures.date_debut END) AS date_debut,
					MAX(p_factures.no_facture) AS no_facture_last,
					MAX(CASE WHEN type_facture <> '1' AND type_facture <> 'X' AND type_facture <> 'P' THEN p_factures.no_facture ELSE null END) AS no_facture_reference_last,
					(MAX(ARRAY[to_char(p_factures_lignes_c.taux_1,'FM00000'),CASE WHEN type_facture <> '1' AND type_facture <> 'X' AND type_facture <> 'P' AND 
                        (t_prestations.code IN ('GHS','PJ') OR (p_factures.no_facture LIKE '%BG' AND p_sejours.type_sejour IN ('1','2','5'))) THEN p_factures.no_facture ELSE NULL END]))[2] AS no_facture_reference_sejour_last,
					COUNT(DISTINCT p_factures.no_facture) AS nb_factures,
					MAX(avec_facturation_intermediaire) AS avec_facturation_intermediaire
				FROM activite[PX].p_factures
					JOIN activite[PX].p_sejours ON p_factures.no_sejour = p_sejours.no_sejour
					JOIN activite[PX].t_lieux ON p_sejours.lieu_sortie_id = t_lieux.oid
					JOIN activite[PX].t_services_facturation ON service_facturation_id = t_services_facturation.oid
					LEFT JOIN activite[PX].p_factures_lignes_c ON p_factures.no_facture = p_factures_lignes_c.no_facture
					LEFT JOIN activite.t_prestations ON p_factures_lignes_c.prestation_id = t_prestations.oid AND t_prestations.code IN ('GHS','PJ')
				GROUP BY 1,2;


				CREATE INDEX w_sejours_facture_reference_i1
								  ON w_sejours_facture_reference
								  USING btree
								  (no_sejour);

				UPDATE activite[PX].p_factures
				SET no_facture_reference = COALESCE(no_facture_reference_sejour_last, no_facture_reference_last, no_facture_last)
				FROM w_sejours_facture_reference
				JOIN activite[PX].p_sejours ON p_sejours.no_sejour = w_sejours_facture_reference.no_sejour
				WHERE p_factures.no_sejour = w_sejours_facture_reference.no_sejour AND
					(
						p_factures.date_fin = w_sejours_facture_reference.date_fin OR
						p_factures.date_fin = p_sejours.date_sortie AND avec_facturation_intermediaire IS DISTINCT FROM '1'

					) AND
					p_factures.no_facture_reference <> COALESCE(no_facture_reference_sejour_last, no_facture_reference_last, no_facture_last);
						
				UPDATE activite[PX].p_factures
				SET facture_reference_id = p_factures_references.oid
				FROM activite[PX].p_factures p_factures_references
				WHERE p_factures.no_facture_reference = p_factures_references.no_facture
					AND p_factures.facture_reference_id <> p_factures_references.oid;

										
			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Compte client Clinique">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_soldes_c_1');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_soldes_c_2');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_soldes_c_3');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_soldes_c_4');
				SELECT base.cti_disable_index('activite[PX]', 'i_factures_soldes_c_8');
				
				INSERT INTO activite[PX].p_factures_soldes_c(
					no_facture,
					facture_id,
					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
					p_factures.no_facture,
					p_factures.oid AS facture_id,
					date_vente AS date_comptable,
					rubrique_comptabilisation_id AS rubrique_comptabilisee_id,
					prestation_id,
					SUM(montant_comptabilise),
					SUM(montant_comptabilise_0),
					SUM(montant_comptabilise_1),
					SUM(montant_comptabilise_2),
					SUM(montant_comptabilise_22),
					0::numeric AS montant_regle,
					0::numeric AS montant_regle_0,
					0::numeric AS montant_regle_1,
					0::numeric AS montant_regle_2,
					0::numeric AS montant_regle_22
				FROM activite[PX].p_factures_lignes_c
				JOIN activite[PX].p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture
				GROUP BY 1,2,3,4,5
				;
			
				INSERT INTO activite[PX].p_factures_soldes_c(
					no_facture,
					facture_id,
					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
					p_factures.no_facture,
					p_factures.oid AS facture_id,
					date(DDATENC) AS date_comptable,
					COALESCE(t_rubriques_facturation.oid,0) AS rubrique_comptabilisee_id,
					COALESCE(t_prestations.oid,0) AS prestation_id,
					0::numeric AS montant_comptabilise,
					0::numeric AS montant_comptabilise_0,
					0::numeric AS montant_comptabilise_1,
					0::numeric AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22,
					SUM(CSOMENC) AS montant_regle,
					SUM(CASE WHEN type_tiers_payant = '0' OR type_tiers_payant = '' THEN CSOMENC ELSE 0 END) AS montant_regle_0,
					SUM(CASE WHEN type_tiers_payant = '1' THEN CSOMENC ELSE 0 END) AS montant_regle_1,
					SUM(CASE WHEN type_tiers_payant = '2' THEN CSOMENC ELSE 0 END) AS montant_regle_2,
					0::numeric AS montant_regle_22
				FROM w_PATFACTCPTA_TD
				JOIN activite[PX].p_factures ON w_PATFACTCPTA_TD.NNOFACT_CTI = p_factures.no_facture
				LEFT JOIN activite[PX].t_rubriques_facturation ON XNNUMACT = t_rubriques_facturation.code_original
				LEFT JOIN prod_sano.LISTCLEACT_TP ON w_PATFACTCPTA_TD.XNNUMACT = LISTCLEACT_TP.NNUMACT
				LEFT JOIN activite.t_prestations ON LISTCLEACT_TP.TCODTELE = t_prestations.oid
				WHERE CSOMENC <> 0
				GROUP BY 1,2,3,4,5
				;
					  
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_soldes_c_1');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_soldes_c_2');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_soldes_c_3');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_soldes_c_4');
				SELECT base.cti_enable_index('activite[PX]', 'i_factures_soldes_c_8');
				
				
				-- Maj montants regles dans factures
				UPDATE activite[PX].p_factures SET
					montant_regle_c = subview.montant_regle,
					montant_regle_0_c = subview.montant_regle_0,
					montant_regle_1_c = subview.montant_regle_1,
					montant_regle_2_c = subview.montant_regle_2,
					date_solde_c = subview.date_solde,
					date_solde_0_c = subview.date_solde_0,
					date_solde_1_c = subview.date_solde_1,
					date_solde_2_c = subview.date_solde_2
				FROM
				(
				SELECT 
					no_facture,
					SUM(montant_regle) AS montant_regle,
					SUM(montant_regle_0) AS montant_regle_0,
					SUM(montant_regle_1) AS montant_regle_1,
					SUM(montant_regle_2) AS montant_regle_2,
					MAX(date_comptable) AS date_solde,
					MAX(CASE WHEN montant_regle_0 <> 0 THEN date_comptable ELSE '00010101' END) AS date_solde_0,
					MAX(CASE WHEN montant_regle_1 <> 0 THEN date_comptable ELSE '00010101' END) AS date_solde_1,
					MAX(CASE WHEN montant_regle_2 <> 0 THEN date_comptable ELSE '00010101' END) AS date_solde_2
				FROM activite[PX].p_factures_soldes_c
				WHERE montant_regle <> 0 OR
					montant_regle_0 <> 0 OR
					montant_regle_1 <> 0 OR
					montant_regle_2 <> 0
				GROUP BY 1
				) subview
				WHERE p_factures.no_facture = subview.no_facture
										
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Lignes non facturées Honoraires">
			<sqlcmd><![CDATA[

				
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures Honoraires">
			<sqlcmd><![CDATA[

						
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Compte client Honoraires">
			<sqlcmd><![CDATA[

				
										
										
			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	
	<NODE name="CHIFFRIER" label="CREATION DU CHIFFRIER">
	
		
	
	
		<NODE label="Récupération chiffrier comptable">
			<sqlcmd><![CDATA[
			
				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::numeric AS montant_solde_client_c,
					0::numeric AS montant_ventes_h,
					0::numeric AS montant_reglements_h,
					0::numeric AS montant_solde_client_h
				FROM
				(
					SELECT 
						date(DDATECR) AS date_comptable,
						SUM(CSOMDUE) AS montant_ventes_c,
						0::numeric AS montant_reglements_c
					FROM w_PATFACTCPTA_TD
					WHERE date(DDATECR) >= '[ENV_ADM_ANNEEDEBUT]0101' AND
						CSOMDUE <> 0
					GROUP BY 1
					UNION
					SELECT 
						date(DDATENC) AS date_comptable,
						0::numeric AS montant_ventes_c,
						SUM(CSOMENC) AS montant_reglements_c
					FROM w_PATFACTCPTA_TD
					WHERE date(DDATENC) >= '[ENV_ADM_ANNEEDEBUT]0101' AND
						CSOMENC <> 0
					GROUP BY 1
				) subview
				GROUP BY 1;
					
			

				TRUNCATE activite[PX].p_factures_comptables;

				INSERT INTO activite[PX].p_factures_comptables(
					no_facture,
					date_comptable,
					montant_ventes_c, 
					montant_reglements_c, 
					montant_ventes_h, 
					montant_reglements_h
				)
				SELECT 
					no_facture,
					date_comptable,
					SUM(montant_ventes_c),
					SUM(montant_reglements_c),
					0::numeric AS montant_ventes_h,
					0::numeric AS montant_reglements_h
				FROM
				(
					SELECT 
						NNOFACT_CTI AS no_facture,
						date(DDATECR) AS date_comptable,
						SUM(CSOMDUE) AS montant_ventes_c,
						0::numeric AS montant_reglements_c
					FROM 
						w_PATFACTCPTA_TD
					WHERE 
						date(DDATECR) >= '[ENV_ADM_ANNEEDEBUT]0101' AND
						CSOMDUE <> 0
					GROUP BY 1,2
					UNION
					SELECT 
						NNOFACT_CTI AS no_facture,
						date(DDATENC) AS date_comptable,
						0::numeric AS montant_ventes_c,
						SUM(CSOMENC) AS montant_reglements_c
					FROM
						w_PATFACTCPTA_TD
					WHERE 
						 date(DDATENC) >= '[ENV_ADM_ANNEEDEBUT]0101' AND
						 CSOMENC <> 0
					GROUP BY 1,2
				) subview
				GROUP BY 1,2
				;
										
			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">
	
		
		<NODE label="Etablissements">
			<sqlcmd><![CDATA[
			
				
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Tiers payant">
			<sqlcmd><![CDATA[

				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, code_original, type_tiers_payant, texte, texte_court) 
				SELECT 'MUTDIV', 'MUTDIV', '2', 'MUTUELLE DIVERSE', 'MUTUELLE DIVERSE'
				WHERE 'MUTDIV' 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 TCODDEST, TCODDEST, '1', MAX(TLIBELLE), MAX(TLIBELLE)
				FROM prod_sano.LISTFACTDEST_TP
				WHERE TCODDEST IN (SELECT TGDREGCACE FROM prod_sano.PATFACT_TD) AND
					TCODDEST NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant WHERE code_original IS NOT NULL) AND
					TCODDEST <> ''
				GROUP BY TCODDEST;
				
				INSERT INTO activite[PX].t_tiers_payant(code_original, code, type_tiers_payant, texte, texte_court) 
				SELECT TCODDEST, TCODDEST, '2', MAX(TLIBELLE), MAX(TLIBELLE)
				FROM prod_sano.LISTFACTDEST_TP
				WHERE TCODDEST IN (SELECT TSPECCODE FROM prod_sano.PATFACT_TD) AND
					TCODDEST NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant WHERE code_original IS NOT NULL) AND
					TCODDEST <> ''
				GROUP BY TCODDEST;

										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Médecins">
			<sqlcmd><![CDATA[
			
				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 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_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);
						
										
			]]></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 NINDEX, NINDEX, TLIBELLE, TLIBELLE, 0 AS nb_lits, '0' AS type_t2a
				FROM prod_sano.TOPOUF_TP
				WHERE NINDEX <> 0 AND
					NINDEX NOT IN (SELECT code_original FROM activite[PX].t_services_facturation WHERE code_original IS NOT NULL) AND
					NINDEX IN (SELECT XNCLEUF FROM prod_sano.PATHOSP_TD )
				ORDER BY 1;

				UPDATE activite[PX].t_services_facturation
				SET mode_traitement_id = COALESCE(t_modes_traitement.oid,0)
				FROM prod_sano.TOPOUF_TP
					LEFT JOIN base.t_modes_traitement ON substr(TCODSSUF,1,2) = t_modes_traitement.code
				WHERE t_services_facturation.code_original = NINDEX 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_sano.TOPOUF_TP
					LEFT JOIN base.t_dmt ON substr(TCODSSUF,3,3) = t_dmt.code
				WHERE t_services_facturation.code_original = NINDEX 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 NINDEX, NINDEX, TLIBELLE, TLIBELLE
				FROM prod_sano.TOPOUF_TP
				WHERE NINDEX <> 0 AND
					NINDEX NOT IN (SELECT code_original FROM activite[PX].t_unites_fonctionnelles WHERE code_original IS NOT NULL) AND
					NINDEX IN (SELECT XNCLEUF FROM prod_sano.PATHOSP_TD )
				ORDER BY 1;

				
					
				-- 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);


				-- 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);




				-- 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)
				SELECT to_char(NCLESECT,'FM999999999999'), to_char(NCLESECT,'FM999999999999'), to_char(NCLESECT,'FM999999999999'), to_char(NCLESECT,'FM999999999999')
				FROM prod_sano.TOPOSECT_TP
				WHERE NINDEX IN (SELECT XNCLELIT FROM prod_sano.PATHOSP_TD) AND
					to_char(NCLESECT,'FM999999999999') NOT IN (SELECT code_original FROM activite[PX].t_etages WHERE code_original IS NOT NULL)
				GROUP BY 1
				;
				
				INSERT INTO activite[PX].t_etages(code_original, code, texte, texte_court)
				SELECT to_char(NINDEX,'FM999999999999'), to_char(NINDEX,'FM999999999999'), to_char(NINDEX,'FM999999999999'), to_char(NINDEX,'FM999999999999')
				FROM prod_sano.TOPOSECT_TP
				WHERE NINDEX IN (SELECT NCLESECT FROM prod_sano.PATHOSP_TD) AND
					to_char(NINDEX,'FM999999999999') NOT IN (SELECT code_original FROM activite[PX].t_etages WHERE code_original IS NOT NULL)
				GROUP BY 1
				;


				UPDATE activite[PX].t_etages
				SET texte = TLIBELLE, texte_court = TLIBELLE
				FROM prod_sano.TOPOSECT_TP
				WHERE t_etages.code_original = to_char(NINDEX,'FM999999999999') AND
					t_etages.texte = t_etages.code_original
				;
					
				

				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);



				INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere) 
				SELECT to_char(NINDEX,'FM999999999999'), TLIBELLE, TLIBELLE, TLIBELLE, t_etages.oid, CASE WHEN TCODFAC = 'CPC' THEN 'O' ELSE 'N' END
				FROM prod_sano.TOPOSECT_TP
				JOIN activite[PX].t_etages ON t_etages.code_original = to_char(NCLESECT,'FM999999999999')
				WHERE NINDEX IN (SELECT XNCLELIT FROM prod_sano.PATHOSP_TD) AND
					to_char(NINDEX,'FM999999999999') NOT IN (SELECT code_original FROM activite[PX].t_lits WHERE code_original IS NOT NULL)
				;

				INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere) 
				SELECT to_char(NINDEX,'FM999999999999'), TLIBELLE, TLIBELLE, TLIBELLE, t_etages.oid, CASE WHEN TCODFAC = 'CPC' THEN 'O' ELSE 'N' END
				FROM prod_sano.TOPOSECT_TP
				JOIN activite[PX].t_etages ON t_etages.code_original = to_char(NCLESECT,'FM999999999999')
				WHERE NINDEX IN (SELECT XNCLELIT FROM prod_sano.PATHISTLIT_TD) AND
					to_char(NINDEX,'FM999999999999') NOT IN (SELECT code_original FROM activite[PX].t_lits WHERE code_original IS NOT NULL)
				;



				INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere) 
				SELECT code_original, code, texte || ' - sans lit', texte_court || ' - sans lit', oid, 'N'
				FROM activite[PX].t_etages
				WHERE oid <> 0 AND
					code_original 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 NNUMACT, TCLEACT, TLIBLETCLE, TLIBLETCLE
				FROM prod_sano.LISTCLEACT_TP
				WHERE NNUMACT NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation WHERE code_original IS NOT NULL)
				GROUP BY 1,2,3,4
				ORDER BY TCLEACT;
				
				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT XNNUMACT, TCLEACT, TCLEACT, TCLEACT
				FROM prod_sano.PATFACTACT_TD
				WHERE XNNUMACT NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation WHERE code_original IS NOT NULL)
				GROUP BY 1,2,3,4
				ORDER BY TCLEACT;

				INSERT INTO activite.t_prestations(oid, code_original, code, texte, texte_court) 
				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 UPPER(TCODTELE), UPPER(TCODTELE), UPPER(TCODTELE), UPPER(TCODTELE)
				FROM prod_sano.LISTCLEACT_TP
				WHERE UPPER(TCODTELE) NOT IN (SELECT code FROM activite.t_prestations WHERE code IS NOT NULL)
				GROUP BY 1,2
				ORDER BY UPPER(TCODTELE);

				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT UPPER(TCODTELE), UPPER(TCODTELE), UPPER(TCODTELE), UPPER(TCODTELE)
				FROM prod_sano.PATFACTACT_TD
				WHERE UPPER(TCODTELE) NOT IN (SELECT code FROM activite.t_prestations WHERE code IS NOT NULL)
				GROUP BY 1,2
				ORDER BY UPPER(TCODTELE);

										
			]]></sqlcmd>
		</NODE>
		
	</NODE>
		
	<NODE name="POST" label="TRAITEMENTS COMPLEMENTAIRES">
	
		<NODE name="Calcul -10% SSR">
			<sqlcmd><![CDATA[
				DROP TABLE IF EXISTS w_moins10;
				CREATE TEMP TABLE w_moins10 AS  
				SELECT p_factures_lignes_c.*, 
					p_factures_lignes_c.CTID AS CTID_orig,
					CASE 
						WHEN (t_prestations.code = 'PJ' OR type_valorisation_non_facture = 'PJ') AND
							montant_comptabilise_0=0 AND 
							montant_comptabilise_2=0 AND 
							montant_comptabilise_22=0 AND
							ABS(round(((prix_unitaire*nb_prestation*0.9)-montant_comptabilise_1)/nb_prestation,0)) BETWEEN 17 AND 37
						THEN round(((prix_unitaire*nb_prestation*0.9)-montant_comptabilise_1)/ABS(nb_prestation),2)
						ELSE 0::numeric END AS montant_fj,
						t_prestations.code AS prestation_code,
						type_valorisation_non_facture,
						'M'::text || CASE WHEN t_prestations.code <> 'PJ' THEN t_prestations.code ELSE 'PJS' END AS prestation_mcode
				FROM activite[PX].p_factures_lignes_c
				JOIN activite[PX].t_lieux ON lieu_id = t_lieux.oid
				JOIN activite[PX].t_services_facturation ON t_services_facturation.oid = service_facturation_id
				JOIN activite.t_prestations ON prestation_id = t_prestations.oid
				WHERE date_fin >= '20170701' AND
					date_debut >= '20170701' AND
					type_t2a = '2' AND
					prestation_id = ANY (SELECT to_id FROM activite.t_listes_contenu JOIN activite.t_listes ON liste_id = t_listes.oid AND t_listes.code = 'DMAP_MFS') AND
					montant_comptabilise <> 0 AND
					montant_comptabilise_1 <> 0 AND
					round(prix_unitaire * nb_prestation,2) <> montant_comptabilise AND
					abs(round(prix_unitaire * nb_prestation,2)) <> abs(montant_comptabilise)+(nb_prestation*18)
				ORDER BY no_facture, prestation_id
				;

				-- Calcul tarif plein
				UPDATE w_moins10 SET
					montant_facture_1 = 
						CASE WHEN prestation_code = 'PJ' OR type_valorisation_non_facture = 'PJ' THEN round(prix_unitaire * nb_prestation * taux_1 / 100,2) - montant_fj
						ELSE round(prix_unitaire * nb_prestation * taux_1 / 100,2) END,
					montant_facture = 
						CASE WHEN prestation_code = 'PJ' OR type_valorisation_non_facture = 'PJ' THEN round(prix_unitaire * nb_prestation * taux_1 / 100,2) - montant_fj
						ELSE round(prix_unitaire * nb_prestation * taux_1 / 100,2) END +
							(montant_facture_2+montant_facture_22+montant_facture_0)
				;

				-- Génération prestations spécifiques 
				INSERT INTO activite.t_prestations (code, texte, texte_court)
				SELECT prestation_mcode, t_prestations.texte || ' (-10%)', t_prestations.texte || ' (-10%)'
				FROM w_moins10
				JOIN activite.t_prestations on prestation_id = t_prestations.oid
				WHERE prestation_mcode NOT IN (SELECT code FROM activite.t_prestations WHERE code IS NOT NULL)
				GROUP BY 1,2,3
				;
			
				-- Maj tarif plein
				UPDATE activite[PX].p_factures_lignes_c
				SET montant_facture = w_moins10.montant_facture, montant_facture_1 = w_moins10.montant_facture_1
				FROM w_moins10
				WHERE p_factures_lignes_c.CTID = w_moins10.CTID_orig;
				;
				
				-- Génération -10%
				UPDATE w_moins10 SET
					montant_facture = montant_comptabilise - montant_facture,
					montant_facture_1 = montant_comptabilise_1 - montant_facture_1,
					montant_facture_2 = montant_comptabilise_2 - montant_facture_2,
					montant_facture_22 = montant_comptabilise_22 - montant_facture_22,
					montant_facture_0 = montant_comptabilise_0 - montant_facture_0,
					montant_comptabilise = 0,
					montant_comptabilise_1 = 0,
					montant_comptabilise_2 = 0,
					montant_comptabilise_22 = 0,
					montant_comptabilise_0 = 0,
					prestation_id = t_prestations.oid
				FROM activite.t_prestations
				WHERE t_prestations.code = prestation_mcode
				;
			
				ALTER TABLE w_moins10 DROP COLUMN CTID_orig;
				ALTER TABLE w_moins10 DROP COLUMN montant_fj;
				ALTER TABLE w_moins10 DROP COLUMN prestation_code;
				ALTER TABLE w_moins10 DROP COLUMN prestation_mcode;
				ALTER TABLE w_moins10 DROP COLUMN type_valorisation_non_facture;
			
				INSERT INTO activite[PX].p_factures_lignes_c
				SELECT *
				FROM w_moins10
				WHERE montant_facture <> 0
				;
			
			
				SELECT activite[PX].cti_reorganize_rubrique_facture_c('activite[PX].p_factures_lignes_c');
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compléments patients">
			<sqlcmd><![CDATA[
						
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compléments facture">
			<sqlcmd><![CDATA[
			
			UPDATE activite[PX].p_factures_lignes_c 
			SET lieu_id = lieu_sortie_id 
			FROM activite[PX].p_sejours 
				JOIN activite[PX].p_factures ON sejour_id = p_sejours.oid
			WHERE lieu_id = 0 and facture_id = p_factures.oid
	
				
			]]></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,  
                    
                    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
                
                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 p_sejours.no_sejour;

                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 = CASE WHEN w_sejours_total.date_expedition_0 <> '0001-01-01' THEN w_sejours_total.date_expedition_0 ELSE '2099-12-31' END,  
                    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 = CASE WHEN w_sejours_total.date_expedition_1 <> '0001-01-01' THEN w_sejours_total.date_expedition_1 ELSE '2099-12-31' END,   
                    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 = CASE WHEN w_sejours_total.date_expedition_2 <> '0001-01-01' THEN w_sejours_total.date_expedition_2 ELSE '2099-12-31' END, 
                    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 = CASE WHEN w_sejours_total.date_expedition_22 <> '0001-01-01' THEN w_sejours_total.date_expedition_22 ELSE '2099-12-31' END, 
                    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
                    
                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 CASE WHEN w_sejours_total.date_expedition_0 <> '0001-01-01' THEN w_sejours_total.date_expedition_0 ELSE '2099-12-31' END 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 CASE WHEN w_sejours_total.date_expedition_1 <> '0001-01-01' THEN w_sejours_total.date_expedition_1 ELSE '2099-12-31' END 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 CASE WHEN w_sejours_total.date_expedition_2 <> '0001-01-01' THEN w_sejours_total.date_expedition_2 ELSE '2099-12-31' END 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 CASE WHEN w_sejours_total.date_expedition_22 <> '0001-01-01' THEN w_sejours_total.date_expedition_22 ELSE '2099-12-31' END 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

                );
                
                UPDATE activite[PX].p_sejours set 
                    ghm_id = t_ghs.ghm_id::bigint
                FROM base.t_ghs
                WHERE 
                    p_sejours.ghs_id = t_ghs.oid
                    AND p_sejours.ghm_id = 0
                    AND p_sejours.ghs_id > 0;
                
                UPDATE activite[PX].p_sejours 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
                
                WHERE
                    
                    delai_groupage IS DISTINCT FROM 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 
                        OR
                    delai_facture IS DISTINCT FROM 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
                        OR
                    delai_expedition IS DISTINCT FROM 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
                        OR
                    delai_solde IS DISTINCT FROM 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
                        OR

                    delai_expedition_0 IS DISTINCT FROM 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
                        OR
                    delai_solde_0 IS DISTINCT FROM 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
                        OR

                    delai_expedition_1 IS DISTINCT FROM 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
                        OR
                    delai_solde_1 IS DISTINCT FROM 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
                        OR

                    delai_expedition_2 IS DISTINCT FROM 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
                        OR
                    delai_solde_2 IS DISTINCT FROM 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
                        OR

                    delai_expedition_22 IS DISTINCT FROM 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
                        OR
                    delai_solde_22 IS DISTINCT FROM 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;				
														
										
			]]></sqlcmd>
		</NODE>
		
	
		
	</NODE>
	
	
	

</ROOT>
