- //récupération des informations nécessaires
- /**********************************************************/
- //Sélect
- /**********************************************************/
- //date ecriture
- $query="SELECT Ecriture.dte, ";
- //numéro dossier d'écriture
- $query.="Ecriture.dossier, ";
- //nom du dossier
- $query.="Dossier.titre, ";
- //date d'ouverture du dossier
- $query.="Dossier.date_ouv, ";
- //datediff
- $query.="datediff(day, date_ouv , ".$debut." ), ";
- //avocat responsable
- $query.="Dossier.avocat_resp, ";
- //avocat d'origine
- $query.="Dossier.avocat_origine, ";
- //avocat responsable du contact
- $query.="(select max(avocat_resp) FROM cic_contact WHERE numero_client = round(numero_ouv/10000,0)) AS 'max_avocat_resp', ";
- //i don't know
- $query.="(SELECT max(valeur) from cic_dyn_val where cic_dyn_def in
- (SELECT cic_dyn_def from cic_dyn_def where libelle_court = 'NVCLT')
- and rang in
- (SELECT cic_contact_fiche from cic_contact_fiche where type_fiche = -1 and contact in
- (SELECT contact from cic_contact where numero_client = round(numero_ouv/10000,0))
- )
- ) ,";
- //here too :(
- $query.="(SELECT convert(datetime,max(valeur)) from cic_dyn_val where cic_dyn_def in
- (SELECT cic_dyn_def from cic_dyn_def where libelle_court = 'DTCLT')
- and rang in (SELECT cic_contact_fiche from cic_contact_fiche where type_fiche = -1 and contact in
- (SELECT contact from cic_contact where numero_client = round(numero_ouv/10000,0))
- )
- ),";
- //date de la facture
- $query.="(SELECT max(e3.dte) from ecriture e3 where e3.facture = ecriture.facture and e3.type_operation = 'FC' and e3.type_prestation = 'H') as date_facture, ";
- //numéro facture
- $query.="facture, ";
- //montant facture
- $query.="(SELECT -coalesce(sum(e3.montant+e3.montant_tva),0) from ecriture e3 where e3.facture = ecriture.facture and e3.type_operation = 'FC' and type_prestation = 'H' and e3.montant < 0) as 'montant facture', ";
- //règlement facture
- $query.="(SELECT sum(e3.montant+e3.montant_tva) from ecriture e3 where e3.facture = ecriture.facture and e3.dte >= ".$debut." and e3.dte <= ".$fin." and e3.type_operation = 'RC' and type_prestation = 'H') as 'montant reglt',";
- //travail avr
- $query.="(SELECT sum(duree) from evaluation where initiale = '".$avr."' and dossier = dossier.numero_ouv and evaluation.facture = ecriture.facture and evaluation.tpe in (3,4,5,12) and etat = 1) as 'travail avr', ";
- //travail autres assoc
- $query.="(SELECT sum(duree) from evaluation where initiale <> '".$avr."' and initiale in (SELECT initial_utils from groupe where numero_groupe = 4) and dossier = dossier.numero_ouv and evaluation.facture = ecriture.facture and evaluation.tpe in (3,4,5,12) and etat = 1) as 'travail autres assoc', ";
- //travail coll
- $query.="(SELECT sum(duree) from evaluation where initiale not in (SELECT initial_utils from groupe where numero_groupe = 4) and dossier = dossier.numero_ouv and evaluation.facture = ecriture.facture and evaluation.tpe in (3,4,5,12) and etat = 1) as 'travail coll', ";
- //montant avr
- $query.="(SELECT sum(montant) from evaluation where initiale = '".$avr."' and dossier = dossier.numero_ouv and evaluation.facture = ecriture.facture and evaluation.tpe in (3,4,5,12) and etat = 1) as 'montant avr',";
- //montant autres assoc
- $query.="(SELECT sum(montant) from evaluation where initiale <> '".$avr."' and initiale in (SELECT initial_utils from groupe where numero_groupe = 4) and dossier = dossier.numero_ouv and evaluation.facture = ecriture.facture and evaluation.tpe in (3,4,5,12) and etat = 1) as 'montant autres assoc',";
- //montant coll
- $query.="(SELECT sum(montant) from evaluation where initiale not in (SELECT initial_utils from groupe where numero_groupe = 4) and dossier = dossier.numero_ouv and evaluation.facture = ecriture.facture and evaluation.tpe in (3,4,5,12) and etat = 1) as 'montant coll',";
- //montant initial
- $query.="(SELECT sum(montant) from evaluation where dossier = dossier.numero_ouv and evaluation.facture = ecriture.facture and evaluation.tpe in (3,4,5,12) and etat = 1) as 'montant initial', ";
- //montant final
- $query.="(SELECT sum(montant_final) from evaluation where dossier = dossier.numero_ouv and evaluation.facture = ecriture.facture and evaluation.tpe in (3,4,5,12) and etat = 1) as 'montant final'";
- /**********************************************************/
- //FROM
- /**********************************************************/
- $query.=" FROM Ecriture, Dossier ";
- /**********************************************************/
- //WHERE
- /**********************************************************/
- //jointure écriture -> dossier
- $query.="WHERE ecriture.dossier = dossier.numero_ouv ";
- //définition de la période et du type de prestation et d'opération et facture NOT NULL
- $where="Ecriture.dte >= '".$debut."' and Ecriture.dte <= '".$fin."' and Ecriture.type_operation = 'RC' and Ecriture.type_prestation = 'H' and facture <> 0 ";
- $whereE2="facture <> 0 and facture in (SELECT e2.facture from ecriture e2 where e2.dte >= '".$debut."' and e2.dte <= '".$fin."' and e2.type_operation = 'RC' and e2.type_prestation = 'H')";
- //multi conditions avec des "OR"
- $query.="and (";
- //Dossiers ou l'avocat est responsable !
- //numéro de client du dossier où l'avocat est responsable
- $query.="(".$where." and round(numero_ouv/10000,0) in (SELECT numero_client from cic_contact where avocat_resp = '".$avr."' ))";
- //numéro de client du dossier où l'avocat est responsable
- $query.="OR (".$where." and dossier in (SELECT dossier from evaluation where initiale = '".$avr."' and tpe in (3,4,5,12) and ".$whereE2."))";
- //i don't know
- $query.="OR (".$where." and (avocat_resp = '".$avr."' or avocat_origine = '".$avr."' )and dossier in (SELECT dossier from evaluation where ".$whereE2.")))";
- //Dossier
- /**********************************************************/
- //group by
- /**********************************************************/
- $query.=" group by Ecriture.dte,Ecriture.dossier,dossier.numero_ouv,Dossier.titre,Dossier.date_ouv,Dossier.avocat_resp,Dossier.avocat_origine, Dossier.desthono,facture ";
- /**********************************************************/
- //order by
- /**********************************************************/
- $query.=" order by dossier.titre,facture";