diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index 60c264d..c2926de 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -717,7 +717,13 @@ class PageController extends Controller { try { try { $data_factures = array(); - $factures = json_decode($this->myDb->getCurrentMonthFactures_byClient($this->idNextcloud, $date, $idclient)); + if($idclient == 0){ + $clientIds = $this->myDb->getClientIdsByClientEntreprise(clientEntreprise: 'cogf'); + $factures = json_decode($this->myDb->getFactureByClientIdsListAndDate($clientIds,$date)); + } + else{ + $factures = json_decode($this->myDb->getCurrentMonthFactures_byClient($this->idNextcloud, $date, $idclient)); + } $factures = array_filter($factures, function($facture) {return $facture->id_client != NULL; }); foreach ($factures as $key => $facture) { $facture_temp = array( diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index c3a2c42..5105440 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -2272,6 +2272,49 @@ class Bdd { return $factureData; } + private function getInvoiceByClientIdsAndMonthYear($clientIds,$month,$year){ + $clientIdsSqlPlaceholder = implode(',', array_fill(0, count($clientIds), '?')); + $sql = "SELECT + facture.id, + facture.date, + facture.date_paiement, + facture.num, + devis.id as devis_id, + devis.date as devis_date, + devis.num as calendar_uuid, + devis.comment as devis_comment, + devis.id_client as devis_id_client, + client.nom as client_nom, + client.entreprise as client_entreprise, + client.adresse as client_adresse, + defunt.nom as defunt_nom, + defunt.sexe as defunt_sexe, + lieu.nom as lieu_nom, + lieu.adresse as lieu_adresse, + thanato.nom as thanato_nom, + thanato.prenom as thanato_prenom + FROM ".$this->tableprefix."facture as facture + LEFT JOIN ".$this->tableprefix."devis as devis on facture.id_devis = devis.id + LEFT JOIN ".$this->tableprefix."client as client on devis.id_client = client.id + LEFT JOIN ".$this->tableprefix."defunt as defunt on devis.id_defunt = defunt.id + LEFT JOIN ".$this->tableprefix."lieu as lieu on devis.id_lieu = lieu.id + LEFT JOIN ".$this->tableprefix."thanato as thanato on devis.id_thanato = thanato.id + WHERE devis.id_client IN ($clientIdsSqlPlaceholder) ? AND + YEAR(facture.date_paiement) = ?"; + + $conditions = array_merge($clientIds,[$year]); + if($month != 0){ + $conditions[] = $month; + $sql .= " AND MONTH(facture.date_paiement) = ?"; + } + $sql .= ";"; + $factures = $this->execSQLNoJsonReturn( + $sql, + $conditions); + + return $factures; + } + private function getInvoiceByClientAndMonthYear($clientId,$month,$year){ $sql = "SELECT facture.id, @@ -2315,7 +2358,13 @@ class Bdd { } public function getInvoicePdfDataByClientAndMonthYear($clientId,$month,$year,$configuration){ - $invoices = $this->getInvoiceByClientAndMonthYear($clientId,$month,$year); + if($clientId == 0){ + $clientIds = $this->getClientIdsByClientEntreprise(clientEntreprise: 'cogf'); + $invoices = $this->getInvoiceByClientIdsAndMonthYear($clientIds,$month,$year); + } + else{ + $invoices = $this->getInvoiceByClientAndMonthYear($clientId,$month,$year); + } $firstClient = $this->getFirstClient(); foreach($invoices as &$invoice){ $invoice["siret"] = $firstClient != null ? $firstClient['legal_one'] : ''; @@ -2525,4 +2574,45 @@ class Bdd { return $factureIdsGenerated; } + public function getClientIdsByClientEntreprise($clientEntreprise){ + $sql = "SELECT client.id + FROM ".$this->tableprefix."client as client + WHERE LOWER(client.entreprise) = ? ;"; + + $clientIds = []; + $clients = $this->execSQLNoJsonReturn( + $sql, + [strtolower($clientEntreprise)]); + + if(!empty($clients)){ + foreach($clients as $client){ + $clientIds[] = $client["id"]; + } + } + return $clientIds; + } + + public function getFactureByClientIdsListAndDate($clientIds,$date){ + $clientIdsSqlPlaceholder = $clientIdsSqlPlaceholder = implode(',', array_fill(0, count($clientIds), '?')); + $sql = "SELECT YEAR(".$this->tableprefix."facture.date_paiement) AS year, MONTH(".$this->tableprefix."facture.date_paiement) AS month," + .$this->tableprefix."facture.id, ".$this->tableprefix."facture.user_id, ".$this->tableprefix."facture.num, ".$this->tableprefix."facture.date, " + .$this->tableprefix."devis.num as dnum,".$this->tableprefix."devis.version as adresse_devis,".$this->tableprefix."devis.comment as numero_commande, " + .$this->tableprefix."client.adresse as adresse_client, ".$this->tableprefix."devis.date as date_soin, " + .$this->tableprefix."client.mail as mail_client, " + .$this->tableprefix."devis.id_client as id_client, date_paiement, type_paiement, id_devis, ".$this->tableprefix."client.nom as nom, prenom, entreprise, " + .$this->tableprefix."lieu.id as lid, ".$this->tableprefix."lieu.nom as lieu,".$this->tableprefix."lieu.adresse as adresse_soin," + .$this->tableprefix."defunt.id as id_defunt, ".$this->tableprefix."defunt.nom as nom_defunt," + .$this->tableprefix."facture.version, status_paiement + FROM (".$this->tableprefix."facture + LEFT JOIN ".$this->tableprefix."devis on ".$this->tableprefix."facture.id_devis = ".$this->tableprefix."devis.id) + LEFT JOIN ".$this->tableprefix."lieu on ".$this->tableprefix."devis.id_lieu = ".$this->tableprefix."lieu.id + LEFT JOIN ".$this->tableprefix."client on ".$this->tableprefix."devis.id_client = ".$this->tableprefix."client.id + LEFT JOIN ".$this->tableprefix."defunt on ".$this->tableprefix."devis.id_defunt = ".$this->tableprefix."defunt.id + WHERE YEAR(".$this->tableprefix."facture.date_paiement) = ".explode('-', $date)[2]." AND MONTH(".$this->tableprefix."facture.date_paiement) = ".explode('-', $date)[1]." + AND ".$this->tableprefix."client.id IN ($clientIdsSqlPlaceholder) ORDER BY year DESC, month DESC;"; + + $result = $this->execSQL($sql, $clientIds); + return $result; + } + }