From b6e7b36193dd2a5751ec2010b9c4d2717efaa760 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Mon, 10 Feb 2025 15:19:34 +0300 Subject: [PATCH] fix client group discount impact --- gestion/lib/Controller/PageController.php | 18 ++++- gestion/lib/Db/Bdd.php | 86 +++++++++++++++++++---- 2 files changed, 89 insertions(+), 15 deletions(-) diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index c3a9fd3..298346a 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -486,7 +486,14 @@ class PageController extends Controller { ); $produits = json_decode($this->getProduitsById($facture->id_devis)); foreach ($produits as $key => $produit) { - $facture_temp['montant_htc'] += $produit->prix_unitaire * $produit->quantite; + $htPrice = $produit->prix_unitaire; + if($facture->fk_client_group_id != null){ + $price = $this->myDb->getProductPriceByClientGroupId($facture->fk_client_group_id,$produit->id); + if($price != null){ + $htPrice = $price; + } + } + $facture_temp['montant_htc'] += $htPrice * $produit->quantite; }; $facture_temp['montant_tva'] = ($facture_temp['montant_htc'] * $facture_temp['tva'])/100; $facture_temp['montant_ttc'] = $facture_temp['montant_tva'] + $facture_temp['montant_htc']; @@ -737,7 +744,14 @@ class PageController extends Controller { ); $produits = json_decode($this->getProduitsById($facture->id_devis)); foreach ($produits as $key => $produit) { - $facture_temp['montant_htc'] += $produit->prix_unitaire * $produit->quantite; + $htPrice = $produit->prix_unitaire; + if($facture->fk_client_group_id != null){ + $price = $this->myDb->getProductPriceByClientGroupId($facture->fk_client_group_id,$produit->id); + if($price != null){ + $htPrice = $price; + } + } + $facture_temp['montant_htc'] += $htPrice * $produit->quantite; }; $facture_temp['montant_tva'] = ($facture_temp['montant_htc'] * $facture_temp['tva'])/100; $facture_temp['montant_ttc'] = $facture_temp['montant_tva'] + $facture_temp['montant_htc']; diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index 2891555..5a39902 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -357,7 +357,7 @@ class Bdd { .$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."client.mail as mail_client, ".$this->tableprefix."client.entreprise as client_entreprise, ".$this->tableprefix."client.fk_client_group_id as fk_client_group_id, " .$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," @@ -409,6 +409,7 @@ class Bdd { .$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.entreprise as client_entreprise, ".$this->tableprefix."client.fk_client_group_id as fk_client_group_id, " .$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," @@ -567,8 +568,25 @@ class Bdd { } public function getListProduit($numdevis, $idNextcloud){ - $sql = "SELECT ".$this->tableprefix."produit.id as pid,".$this->tableprefix."produit_devis.id as pdid, reference, description,".$this->tableprefix."produit_devis.comment, quantite, prix_unitaire FROM ".$this->tableprefix."produit, ".$this->tableprefix."devis, ".$this->tableprefix."produit_devis WHERE ".$this->tableprefix."produit.id = produit_id AND ".$this->tableprefix."devis.id = devis_id AND ".$this->tableprefix."devis.id = ?"; - return $this->execSQL($sql, array($numdevis)); + $sql = "SELECT ". + $this->tableprefix."produit.id as pid," + .$this->tableprefix."produit_devis.id as pdid, reference, description," + .$this->tableprefix."produit_devis.comment, quantite, prix_unitaire, " + .$this->tableprefix."devis.id_client + FROM ".$this->tableprefix."produit, ".$this->tableprefix."devis, ".$this->tableprefix."produit_devis + WHERE ".$this->tableprefix."produit.id = produit_id AND ".$this->tableprefix."devis.id = devis_id AND ".$this->tableprefix."devis.id = ?"; + + $produits = $this->execSQLNoJsonReturn($sql,[$numdevis]); + + if(!empty($produits)){ + $clientId = $produits[0]["id_client"]; + $client = $this->getClientById($clientId); + foreach($produits as &$produit){ + $productPrice = $this->getProductPriceByClientGroupId($client['fk_client_group_id'],$produit['pid']); + $produit['prix_unitaire'] = $productPrice ?? $produit['prix_unitaire']; + } + } + return json_encode($produits); } public function getListArticle($numdevis, $idNextcloud) { @@ -1486,15 +1504,33 @@ class Bdd { * Annual turnover per month without VAT */ public function getAnnualTurnoverPerMonthNoVat($idNextcloud){ - $sql = "SELECT EXTRACT(YEAR FROM ".$this->tableprefix."facture.date_paiement) as y, - EXTRACT(MONTH FROM ".$this->tableprefix."facture.date_paiement) as m, - sum(".$this->tableprefix."produit.prix_unitaire * ".$this->tableprefix."produit_devis.quantite) as total - FROM `".$this->tableprefix."facture`, `".$this->tableprefix."produit_devis`, `".$this->tableprefix."produit` - WHERE ".$this->tableprefix."facture.id_devis = ".$this->tableprefix."produit_devis.devis_id - AND ".$this->tableprefix."produit_devis.produit_id = ".$this->tableprefix."produit.id - GROUP BY EXTRACT(YEAR FROM ".$this->tableprefix."facture.date_paiement), EXTRACT(MONTH FROM ".$this->tableprefix."facture.date_paiement) - ORDER BY EXTRACT(YEAR FROM ".$this->tableprefix."facture.date_paiement) DESC, EXTRACT(MONTH FROM ".$this->tableprefix."facture.date_paiement);"; - // return $this->execSQL($sql, array($idNextcloud)); + $sql = "SELECT + EXTRACT(YEAR FROM facture.date_paiement) AS y, + EXTRACT(MONTH FROM facture.date_paiement) AS m, + SUM( + COALESCE(client_group_discount.ht_amount,produit.prix_unitaire) + * produit_devis.quantite + ) AS total + FROM ".$this->tableprefix."facture AS facture + JOIN ".$this->tableprefix."produit_devis AS produit_devis + ON facture.id_devis = produit_devis.devis_id + JOIN ".$this->tableprefix."produit AS produit + ON produit_devis.produit_id = produit.id + JOIN ".$this->tableprefix."devis AS devis + ON facture.id_devis = devis.id + JOIN ".$this->tableprefix."client AS client + ON devis.id_client = client.id + LEFT JOIN ".$this->tableprefix."client_group AS client_group + ON client.fk_client_group_id = client_group.id + LEFT JOIN ".$this->tableprefix."client_group_discount AS client_group_discount + ON client_group.id = client_group_discount.fk_client_group_id + AND produit.id = client_group_discount.fk_produit_id + GROUP BY + EXTRACT(YEAR FROM facture.date_paiement), + EXTRACT(MONTH FROM facture.date_paiement) + ORDER BY + EXTRACT(YEAR FROM facture.date_paiement) DESC, + EXTRACT(MONTH FROM facture.date_paiement);"; return $this->execSQL($sql, array()); } @@ -2285,6 +2321,15 @@ class Bdd { $produitList = $this->execSQLNoJsonReturn( $sql, [$devisId]); + + if(!empty($produitList)){ + $clientId = $produitList[0]["devis_client_id"]; + $client = $this->getClientById($clientId); + foreach($produitList as &$produit){ + $productPrice = $this->getProductPriceByClientGroupId($client['fk_client_group_id'],$produit['produit_id']); + $produit['produit_price'] = $productPrice ?? $produit['produit_price']; + } + } return $produitList; } @@ -2651,7 +2696,7 @@ class Bdd { .$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."client.mail as mail_client, ".$this->tableprefix."client.entreprise as client_entreprise, ".$this->tableprefix."client.fk_client_group_id as fk_client_group_id, " .$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," @@ -2878,4 +2923,19 @@ class Bdd { return $this->execSQL($sql, array()); } + public function getProductPriceByClientGroupId($clientGroupId,$productId){ + $sql = "SELECT * + FROM ".$this->tableprefix ."client_group_discount as client_group_discount + WHERE client_group_discount.fk_client_group_id = ? AND + client_group_discount.fk_produit_id = ?; + "; + $clientGroupDiscount = $this->execSQLNoJsonReturn( + $sql, + [$clientGroupId,$productId]); + if(!empty($clientGroupDiscount)){ + return $clientGroupDiscount[0]['ht_amount']; + } + return null; + } + }