SELECT  
		fn_libelle_familleacte(prt.codeActe) AS Prestations, 
		prt.codeActe AS Actes, 
		`fn_libelle_medecin_rp`(F.codeMedecin) AS Medecin,
		DATE_FORMAT(prt.datePrestation, '%d/%m/%Y') AS DatedeSoins,
		ta.codeTypeAffection AS CodeTypeAffection,  
		ta.libelle AS TypeAffection,  
		af.codeAffection AS CodeAffection,
		CASE WHEN prt.tiersPayant='1' THEN 'TP' ELSE 'RD' END AS TypeD,
		IFNULL(prt.fraisReel,0) AS Reclamé,
		IFNULL(prt.fraisRetenu,0) AS BaseDecomptée,		
		IFNULL(prt.tm,0) AS TM,
		IFNULL(prt.fraisExclu,0) AS Exclusion,	
		IFNULL(prt.depassement,0) AS Depassement,	-- A METTRE
		IFNULL(prt.montantApayer,0) AS Remboursé,	
		IFNULL(prt.montantTM,0) AS Part_Patient		
	FROM p_prestationactes prt 
	JOIN p_facture B ON (B.id=prt.idfacture)
	JOIN police P ON  (B.idPolice = P.id)
	JOIN gc_assureur G ON  (P.codeGcAssureur = G.codeGcAssureur)
	JOIN p_feuillemaladie F ON (F.numeroFeuilleMaladie=B.numeroFeuilleMaladie)
	JOIN p_affection af ON (F.codeAffection=af.codeAffection)
	JOIN p_typeaffection ta ON (ta.CodeTypeAffection=af.CodeTypeAffection)	
	WHERE (LEFT(B.dateSysteme,10) BETWEEN '2023-02-01' AND '2023-02-28')
	AND (CASE WHEN 'GNA' IS NULL THEN G.codeGcAssureur IS NOT NULL ELSE G.codeGcAssureur='GNA' END) 
	AND G.id!='45'  AND (B.codeSociete="vitalis")
	AND prt.montantApayer!=0
	AND B.valide=1 AND B.okDecompte=1 AND prt.ententePrealable NOT IN ('2','9')
	AND B.codePrestataire NOT IN ('CE0416','PR1176','CE1215','CE1232','CE1287','PR1175','CE1276') 
	ORDER BY  DatedeSoins DESC;
	
	
	-- select * from p_typeaffection