	DROP TEMPORARY TABLE IF EXISTS temp_facture_leger;	 
	CREATE TEMPORARY TABLE temp_facture_leger
	( INDEX(idFacture) , INDEX(idBeneficiaire) , INDEX(idAdherent))
	SELECT
	LEFT(B.dateSysteme,7) AS DateComptable,idAdherent	NoAdherent	adherent	id	NoBeneficiaire	beneficiaire	datePrestation		Acte	numeroBonConsultation	numeroBonOrdonnance	numeroBonHospitalisation	numeroBonExamen	fraisReel	TM	Arembourser

		prt.id AS idFacture,
		e.idPolice,e.idCollege,e.idAdherent,
		e.idBeneficiaire,prt.datePrestation,prt.codeActe,
		IFNULL(prt.fraisReel,0)AS montantReclame,IFNULL(prt.montantTm,0) AS TM,IFNULL(prt.montantArembourser,0) AS Arembourser 
	FROM p_prestationactes prt 
	INNER JOIN beneficiaire e ON (e.id=prt.idBeneficiaire)
	INNER JOIN adherent a ON ((C.id=a.idPolice) AND (C.codeSociete=a.codeSociete) AND ( a.id=e.idAdherent))
	-- JOIN p_facture B ON (B.id=prt.idfacture)
	INNER JOIN  police C ON  (B.idPolice = C.id)
	INNER JOIN  gc_assureur G ON  (C.codeGcAssureur = G.codeGcAssureur)	
	LEFT JOIN p_feuillemaladie f ON (f.numeroFeuilleMaladie =B.numeroFeuilleMaladie )
	LEFT JOIN `p_affection` af ON (f.codeAffection =af.codeAffection )
	WHERE (LEFT(B.dateSysteme,10)  BETWEEN '2022-01-01' AND '2022-09-30' )
	AND G.id!='45' AND C.idClient!='49' 
	AND prt.codePrestataire='CE1376'
	AND prt.codeGarantie NOT IN ('CON','PHA')
	AND e.idPolice=156 -- AND f.codeAffection=990
	-- AND B.valide=1 AND B.okDecompte=1
	ORDER BY B.id,B.idPolice,e.idCollege,B.idAdherent;

	SELECT 
		B.idPolice,B.libellePolice,B.idCollege,
		B.idAdherent,B.numeroAdherent AS NoAdherent,B.adherent,		
		B.idBeneficiaire,B.numeroBeneficiaire AS NoBeneficiaire,B.beneficiaire,	
		SUM(A.montantReclame) AS Reclame,
		SUM(A.TM) TM,
		SUM(A.Arembourser) AS Rembourser,
		IFNULL(M.Total,0) AS Total 
		-- IFNULL(M.Decompte,0) AS Decompte 
	FROM temp_facture_leger A
	LEFT JOIN vw_infos_contrat_assures B ON (B.idBeneficiaire = A.idBeneficiaire) 
	INNER JOIN temp_facture_medu M ON (M.idAdherent = A.idAdherent)
	WHERE A.Arembourser>0
	GROUP BY B.idPolice,B.libellePolice,B.idCollege,B.idAdherent,B.numeroAdherent ,B.adherent,		
		 B.idBeneficiaire,B.numeroBeneficiaire ,B.beneficiaire,M.Total,M.Decompte
	ORDER BY Total DESC
	-- ORDER BY B.idPolice,B.idCollege,B.idAdherent,B.idBeneficiaire,A.Arembourser;
	
	-- DROP TEMPORARY TABLE  temp_facture_leger;
	-- DROP TEMPORARY TABLE  temp_facture_medu;