From 574606c3a7275fb8d8c20e4f8d7f5878bb368284 Mon Sep 17 00:00:00 2001 From: Narindra ezway Date: Mon, 14 Apr 2025 16:01:19 +0300 Subject: [PATCH] add-can-factured-in-15-days --- gestion/lib/Controller/PageController.php | 57 +++-- gestion/lib/Db/Bdd.php | 234 +++++++++++++++--- gestion/lib/Service/InvoicePdfService.php | 67 +++-- .../Sql/20250414-ADD_FACTURE_ID_IN_DEVIS.sql | 2 + 4 files changed, 282 insertions(+), 78 deletions(-) create mode 100644 gestion/lib/Sql/20250414-ADD_FACTURE_ID_IN_DEVIS.sql diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index f9b8894..f313f51 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -1802,33 +1802,48 @@ class PageController extends Controller { $facture_temp['montant_ttc'] = $facture_temp['montant_tva'] + $facture_temp['montant_htc']; } else{ + $devis = $this->myDb->getDevisByFkFactureId($facture->id); + $factureGroupIsRelatedToAnyDevis = $devis != null; + $mentionFilters = [ + DevisMentionConstant::FACTURED, + DevisMentionConstant::FACTURED_FORMATTED + ]; + + $isFactureGroupForSingleClient = $facture->facture_client_id != null && $facture->facture_client_id != 0; if($isFactureGroupForSingleClient){ $facture_temp["client"] = $facture->facture_client_entreprise; $facture_temp["nom_client"] = $facture->facture_client_name; - $devisList = $this->myDb->getDevisByClientIdAndMonthYear( - $facture->facture_client_id, - $facture->facture_month, - $facture->facture_year, - [ - DevisMentionConstant::FACTURED, - DevisMentionConstant::FACTURED_FORMATTED - ] - ); + + if(!$factureGroupIsRelatedToAnyDevis ){ + $devisList = $this->myDb->getDevisByClientIdAndMonthYear( + $facture->facture_client_id, + $facture->facture_month, + $facture->facture_year, + $mentionFilters + ); + } } else{ $facture_temp["client"] = $facture->group_code_comptable ??'-'; $facture_temp["nom_client"] = $facture->facture_group_name; - $devisList = $this->myDb->getDevisByClientGroupFacturationIdAndMonthYear( - $facture->facture_client_group_facturation_id, - $facture->facture_month, - $facture->facture_year, - [ - DevisMentionConstant::FACTURED, - DevisMentionConstant::FACTURED_FORMATTED - ] - ); + + if(!$factureGroupIsRelatedToAnyDevis ){ + $devisList = $this->myDb->getDevisByClientGroupFacturationIdAndMonthYear( + $facture->facture_client_group_facturation_id, + $facture->facture_month, + $facture->facture_year, + [ + DevisMentionConstant::FACTURED, + DevisMentionConstant::FACTURED_FORMATTED + ] + ); + } } + if($factureGroupIsRelatedToAnyDevis){ + $devisList = $this->myDb->getDevisByClientIdByFactureId($facture->id, $mentionFilters ); + } + foreach($devisList as $currentDevis){ $produits = json_decode($this->getProduitsById($currentDevis['id'])); foreach ($produits as $key => $produit) { @@ -3103,7 +3118,11 @@ class PageController extends Controller { $facturationDate, $this->idNextcloud ); - return json_encode($result); + if ($result) { + + return json_encode($result); + } + return json_encode([]); } catch(Exception $e) { return json_encode([]); diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index 874e658..9d260b8 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -3070,20 +3070,26 @@ class Bdd { DevisMentionConstant::FACTURED_FORMATTED, DevisMentionConstant::FACTURED ]; - if($isFactureForSingleClient){ - $factureDevisList = $this->getDevisDataByClientIdAndMonthYear( - $factureData['fk_client_id'], + $devis = $this->getDevisByFkFactureId($factureId); + $factureGroupIsRelatedToAnyDevis = $devis != null; + if (!$factureGroupIsRelatedToAnyDevis) { + if($isFactureForSingleClient){ + $factureDevisList = $this->getDevisDataByClientIdAndMonthYear( + $factureData['fk_client_id'], + $factureData['month'], + $factureData['year'],$devisMentionFilters + ); + }else{ + $factureDevisList = $this->getDevisDataByClientGroupFacturationIdAndMonthYear( + $factureData['fk_client_group_facturation_id'], $factureData['month'], $factureData['year'],$devisMentionFilters - ); - } - else{ - $factureDevisList = $this->getDevisDataByClientGroupFacturationIdAndMonthYear( - $factureData['fk_client_group_facturation_id'], - $factureData['month'], - $factureData['year'],$devisMentionFilters - ); + ); + } + }else{ + $factureDevisList = $this->getDevisDataGroupByFactureId($factureId, $devisMentionFilters ); } + $factureIncrement = 0; $productsCount = 0; $totalHt = 0; @@ -4582,6 +4588,38 @@ COMMENTAIRES: ".$comment; return $result; } + public function getDevisIdsByClientGroupFacturationIdAnDate($clientGroupFacturationId,$facturationDate , $mentionFilters = []){ + + $dateTime = Datetime::createFromFormat('Y-m-d',$facturationDate); + $month = $dateTime->format('m'); + $year = $dateTime->format('Y'); + + $sql = "SELECT devis.id + FROM ".$this->tableprefix."devis as devis + LEFT JOIN ".$this->tableprefix."client as client on devis.id_client = client.id + WHERE + client.fk_client_group_facturation_id = ? AND + YEAR(devis.date) = ? AND + MONTH(devis.date) = ? AND + devis.date <= ? " ; + + $conditions = [$clientGroupFacturationId,$year,$month ,$facturationDate]; + + if(!empty($mentionFilters)){ + $mentionsFilterPlaceholders = implode(',', array_fill(0, count($mentionFilters), '?')); + $sql .= " AND devis.mentions IN ($mentionsFilterPlaceholders)"; + $conditions = array_merge($conditions, $mentionFilters); + } + $sql.= ";"; + $result = $this->execSQLNoJsonReturn($sql,$conditions); + + $devisIds = []; + foreach($result as $currentResult){ + $devisIds[] = $currentResult['id']; + } + return $devisIds; + } + //OLD concept public function getDevisIdsByClientGroupFacturationIdAndMonthYear($clientGroupFacturationId,$month,$year,$mentionFilters = []){ $sql = "SELECT devis.id FROM ".$this->tableprefix."devis as devis @@ -4628,7 +4666,54 @@ COMMENTAIRES: ".$comment; return $result; } + public function getDevisByClientIdByFactureId($factureId , $mentionFilters = []){ + $sql = "SELECT devis.id,devis.id_client,client.fk_client_group_id + FROM ".$this->tableprefix."devis as devis + LEFT JOIN ".$this->tableprefix."client as client on devis.id_client = client.id + WHERE devis.fk_facture_id = ? "; + $conditions = [$factureId]; + if(!empty($mentionFilters)){ + $mentionsFilterPlaceholders = implode(',', array_fill(0, count($mentionFilters), '?')); + $sql .= " AND devis.mentions IN ($mentionsFilterPlaceholders)"; + $conditions = array_merge($conditions, $mentionFilters); + } + $sql.= ";"; + $result = $this->execSQLNoJsonReturn($sql,$conditions); + + return $result; + } + + public function getDevisIdsByClientIdAndDate($clientId,$date,$mentionFilters = [] ){ + $dateTime = Datetime::createFromFormat('Y-m-d',$date); + $month = $dateTime->format('m'); + $year = $dateTime->format('Y'); + + $sql = "SELECT devis.id,devis.id_client,client.fk_client_group_id + FROM ".$this->tableprefix."devis as devis + LEFT JOIN ".$this->tableprefix."client as client on devis.id_client = client.id + WHERE + client.id = ? AND + YEAR(devis.date) = ? AND + MONTH(devis.date) = ? AND + devis.date <= ? "; + + $conditions = [$clientId,$year,$month ,$date]; + if(!empty($mentionFilters)){ + $mentionsFilterPlaceholders = implode(',', array_fill(0, count($mentionFilters), '?')); + $sql .= " AND devis.mentions IN ($mentionsFilterPlaceholders)"; + $conditions = array_merge($conditions, $mentionFilters); + } + $sql.= ";"; + $result = $this->execSQLNoJsonReturn($sql,$conditions); + + $devisIds = []; + foreach($result as $currentResult){ + $devisIds[] = $currentResult['id']; + } + return $devisIds; + + } public function getDevisIdsByClientIdAndMonthYear($clientId,$month,$year,$mentionFilters = []){ $sql = "SELECT devis.id,devis.id_client,client.fk_client_group_id FROM ".$this->tableprefix."devis as devis @@ -4655,21 +4740,20 @@ COMMENTAIRES: ".$comment; } - public function getFactureIdByClientIdAndMonthYear($clientId,$month,$year){ + public function getFactureIdByClientIdAndDate($clientId,$facturationDate){ + $sql = "SELECT facture.id as facture_id FROM ".$this->tableprefix."facture as facture WHERE facture.fk_client_id = ? AND - facture.year = ? AND - facture.month = ? AND - facture.facture_type = ?"; + facture.facture_type = ? AND + facture.date_paiement = ? "; $result = $this->execSQLNoJsonReturn($sql,[ $clientId, - $year, - $month, - FactureTypeConstant::TYPE_GROUP + FactureTypeConstant::TYPE_GROUP, + $facturationDate, ]); if(!empty($result)){ @@ -4678,21 +4762,20 @@ COMMENTAIRES: ".$comment; return null; } - public function getFactureIdByClientGroupFacturationIdAndMonthYear($clientId,$month,$year){ + public function getFactureIdByClientGroupFacturationIdAndDate($clientId,$facturationDate){ $sql = "SELECT facture.id as facture_id FROM ".$this->tableprefix."facture as facture WHERE facture.fk_client_group_facturation_id = ? AND - facture.year = ? AND - facture.month = ? AND - facture.facture_type = ?"; + facture.facture_type = ? AND + facture.date_paiement = ? "; + $result = $this->execSQLNoJsonReturn($sql,[ $clientId, - $year, - $month, - FactureTypeConstant::TYPE_GROUP + FactureTypeConstant::TYPE_GROUP, + $facturationDate ]); if(!empty($result)){ @@ -4701,14 +4784,14 @@ COMMENTAIRES: ".$comment; return null; } - public function invoiceListOfDevisIds($devisIds){ + public function invoiceListOfDevisIds($devisIds ,$factureId){ $sqlConditionsPlaceholder = implode(',', array_fill(0, count($devisIds), '?')); $sql = "UPDATE ".$this->tableprefix."devis as devis - SET devis.mentions = ? + SET devis.mentions = ? , devis.fk_facture_id = ? WHERE devis.id IN ($sqlConditionsPlaceholder);"; $conditions = array_merge( - [DevisMentionConstant::FACTURED_FORMATTED], + [DevisMentionConstant::FACTURED_FORMATTED ,$factureId ], $devisIds ); @@ -4770,6 +4853,57 @@ COMMENTAIRES: ".$comment; return $devisList; } + public function getDevisDataGroupByFactureId($factureId ,$mentionFilters = []){ + $sql = "SELECT + devis.id as devis_id, + devis.date as devis_date, + devis.num as calendar_uuid, + devis.comment as devis_comment, + devis.order_number as order_number, + devis.case_number as case_number, + devis.devis_full_number as devis_full_number, + devis.id_client as client_id, + client.nom as client_nom, + client.entreprise as client_entreprise, + client.adresse as client_adresse, + client.mail as client_mail, + client.legal_one as client_legal_one, + 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, + client_group_facturation.id as group_id, + client_group_facturation.group_facturation_name as group_name, + client_group_facturation.phone_number as group_phone_number, + client_group_facturation.address as group_address, + client_group_facturation.postal_code as group_postal_code, + client_group_facturation.city as group_city, + client_group_facturation.email as group_email, + client_group_facturation.siret_number as group_siret_number, + client_group_facturation.tva_intracommu as group_tva_intracommu, + client_group_facturation.fk_template_type_key as fk_template_type_key, + client_group_facturation.code_comptable as code_comptable + FROM ".$this->tableprefix."devis as devis + 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 + LEFT JOIN ".$this->tableprefix."client_group_facturation as client_group_facturation on client.fk_client_group_facturation_id = client_group_facturation.id + WHERE devis.fk_facture_id = ? "; + + $conditions = [$factureId]; + if(!empty($mentionFilters)){ + $mentionsFilterPlaceholders = implode(',', array_fill(0, count($mentionFilters), '?')); + $sql .= " AND devis.mentions IN ($mentionsFilterPlaceholders)"; + $conditions = array_merge($conditions, $mentionFilters); + } + $sql.= " ORDER BY devis.date ASC;"; + $devisList = $this->execSQLNoJsonReturn($sql,$conditions); + + return $devisList; + } private function getDevisDataByClientIdAndMonthYear($clientId,$month,$year,$mentionFilters = []){ $sql = "SELECT devis.id as devis_id, @@ -4828,6 +4962,14 @@ COMMENTAIRES: ".$comment; $sql = "SELECT * FROM ".$this->tableprefix."client_template_type as client_template_type"; return $this->execSQL($sql,[]); } + public function getDevisByFkFactureId($factureId){ + $sql = "SELECT * FROM ".$this->tableprefix."devis as devis WHERE devis.fk_facture_id = ? LIMIT 1;"; + $result = $this->execSQLNoJsonReturn($sql,[$factureId]); + if(!empty($result)){ + return $result[0]; + } + return null; + } public function getFactureGroupById($factureId){ $sql = "SELECT @@ -4866,6 +5008,9 @@ COMMENTAIRES: ".$comment; DevisMentionConstant::FACTURED, DevisMentionConstant::FACTURED_FORMATTED ]; + $devis = $this->getDevisByFkFactureId($factureId); + $factureGroupIsRelatedToAnyDevis = $devis != null; + if($isFactureSingleClient){ $client = $this->getClientById($facture['fk_client_id']); @@ -4874,12 +5019,17 @@ COMMENTAIRES: ".$comment; $facture['client_address'] = $client["client_address"]; $facture['siret'] = $client["client_legal_one"]; $facture['mail'] = $client["client_mail"]; - $devisList = $this->getDevisDataByClientIdAndMonthYear( - $facture['fk_client_id'], - $facture['month'], - $facture['year'], - $devisMentionFilters - ); + if (!$factureGroupIsRelatedToAnyDevis) { + $devisList = $this->getDevisDataByClientIdAndMonthYear( + $facture['fk_client_id'], + $facture['month'], + $facture['year'], + $devisMentionFilters + ); + + }else{ + $devisList = $this->getDevisDataGroupByFactureId( $factureId,$devisMentionFilters); + } } else{ $clientGroupFacturation = $this->getClientGroupFacturationById($facture['fk_client_group_facturation_id']); @@ -4888,12 +5038,16 @@ COMMENTAIRES: ".$comment; $facture['client_address'] = $clientGroupFacturation["address"] . ' - ' .$clientGroupFacturation["postal_code"] . ' ' . $clientGroupFacturation['city']; $facture['siret'] = $clientGroupFacturation["siret_number"]; $facture['mail'] = $clientGroupFacturation["email"]; - $devisList = $this->getDevisDataByClientGroupFacturationIdAndMonthYear( - $facture['fk_client_group_facturation_id'], - $facture['month'], - $facture['year'], - $devisMentionFilters - ); + if (!$factureGroupIsRelatedToAnyDevis) { + $devisList = $this->getDevisDataByClientGroupFacturationIdAndMonthYear( + $facture['fk_client_group_facturation_id'], + $facture['month'], + $facture['year'], + $devisMentionFilters + ); + }else{ + $devisList = $this->getDevisDataGroupByFactureId( $factureId,$devisMentionFilters); + } } $factureTotalHt= 0; $factureTotalTva = 0; diff --git a/gestion/lib/Service/InvoicePdfService.php b/gestion/lib/Service/InvoicePdfService.php index 81822ce..640117f 100644 --- a/gestion/lib/Service/InvoicePdfService.php +++ b/gestion/lib/Service/InvoicePdfService.php @@ -273,30 +273,59 @@ class InvoicePdfService { DevisMentionConstant::NEW, DevisMentionConstant::MENTION ]; + // Recuperer les devis non facturés du client avant la date de facturation du mois + //Si il a devis qui n est pas encore facturés + //Cree un facture, atttaché l ID du facture au devis et generer le pdf + if($clientType == MultipleFactureTypeConstant::CLIENT_FILTER_TYPE){ - $factureId = $this->gestionBdd->getFactureIdByClientIdAndMonthYear($clientId,$month,$year); - $devisIds = $this->gestionBdd->getDevisIdsByClientIdAndMonthYear($clientId,$month,$year,$devisMentionFiltersToBeInvoiced); + $devisIds = $this->gestionBdd->getDevisIdsByClientIdAndDate($clientId,$facturationDate,$devisMentionFiltersToBeInvoiced); $fkClientId = $clientId; - } - else{ - $factureId = $this->gestionBdd->getFactureIdByClientGroupFacturationIdAndMonthYear($clientId,$month,$year); - $devisIds = $this->gestionBdd->getDevisIdsByClientGroupFacturationIdAndMonthYear($clientId,$month,$year); + $factureId = $this->gestionBdd->getFactureIdByClientIdAndDate($clientId,$facturationDate); + + }else{ + $devisIds = $this->gestionBdd->getDevisIdsByClientGroupFacturationIdAnDate($clientId , $facturationDate , $devisMentionFiltersToBeInvoiced ); $fkClientGroupFacturationId = $clientId; + $factureId = $this->gestionBdd->getFactureIdByClientGroupFacturationIdAndDate($clientId,$facturationDate); + } - $clientIsAlreadyFacturedForThisMonthAndYear = $factureId != null && $factureId != 0; - if($clientIsAlreadyFacturedForThisMonthAndYear == false){ - $factureId = $this->gestionBdd->createFactureAndReturnFactureId( - $facturationDate, - FactureTypeConstant::TYPE_GROUP, - $month, - $year, - $fkClientId, - $fkClientGroupFacturationId); + // if($clientType == MultipleFactureTypeConstant::CLIENT_FILTER_TYPE){ + // $devisIds = $this->gestionBdd->getDevisIdsByClientIdAndMonthYear($clientId,$month,$year,$devisMentionFiltersToBeInvoiced); + // $fkClientId = $clientId; + // } + // else{ + // $factureId = $this->gestionBdd->getFactureIdByClientGroupFacturationIdAndMonthYear($clientId,$month,$year); + // $devisIds = $this->gestionBdd->getDevisIdsByClientGroupFacturationIdAndMonthYear($clientId,$month,$year); + // $fkClientGroupFacturationId = $clientId; + // } + // $clientIsAlreadyFacturedForThisMonthAndYear = $factureId != null && $factureId != 0; + // if($clientIsAlreadyFacturedForThisMonthAndYear == false){ + // $factureId = $this->gestionBdd->createFactureAndReturnFactureId( + // $facturationDate, + // FactureTypeConstant::TYPE_GROUP, + // $month, + // $year, + // $fkClientId, + // $fkClientGroupFacturationId); + // } + + if (!empty($devisIds)) { + //Get facture by date and client + $clientIsAlreadyFacturedForThisDate = $factureId != null && $factureId != 0; + if (!$clientIsAlreadyFacturedForThisDate) { + $factureId = $this->gestionBdd->createFactureAndReturnFactureId( + $facturationDate, + FactureTypeConstant::TYPE_GROUP, + $month, + $year, + $fkClientId, + $fkClientGroupFacturationId + ); + } + $this->gestionBdd->invoiceListOfDevisIds($devisIds , $factureId); + $factureGeneratedResponse = $this->generateFactureGroupPdfByFactureId($factureId,$idNextcloud); + return $factureGeneratedResponse["filenames"]; } - - $this->gestionBdd->invoiceListOfDevisIds($devisIds); - $factureGeneratedResponse = $this->generateFactureGroupPdfByFactureId($factureId,$idNextcloud); - return $factureGeneratedResponse["filenames"]; + return null; } catch(Exception){ return null; diff --git a/gestion/lib/Sql/20250414-ADD_FACTURE_ID_IN_DEVIS.sql b/gestion/lib/Sql/20250414-ADD_FACTURE_ID_IN_DEVIS.sql new file mode 100644 index 0000000..5ab905b --- /dev/null +++ b/gestion/lib/Sql/20250414-ADD_FACTURE_ID_IN_DEVIS.sql @@ -0,0 +1,2 @@ +alter table oc_gestion_devis +add column fk_facture_id VARCHAR(100) DEFAULT NULL; \ No newline at end of file