From 5b95397100f5219e08a9bc22a217a2af92f989f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20de=20Grandpr=C3=A9?= Date: Sun, 14 Jun 2026 22:16:42 -0400 Subject: [PATCH] 37423 DiscountAbsolute model refactor (#38808) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * opérations CRUD du modèle DiscountAbsolute * taux de taxes locales et types ajoutés au modèle * canvas et méthode paramétrées pour test unitaires * test création et lecture OK * #37423 - DiscountAbsolute model refactor to have localtaxes on a remise - unit tests completed * phpcs * remove comment * phpcs local * remove attributes in favor of parent * phpstan - type de données et correction tests unitaires * typage des retours * . * exceptions Phan sur test unitaire * commentaire de fonction bien formatté * directives phan depuis CommonClassTest * avertissements phan * removing localtaxes from multicurrency attributes as not supported by data model * interface testing from ThirdParty comm/remx.php with added multicurrency ttc support * phpcs OK * phan typage * merci phan * buyer / seller roles of thirdparty integrity * attribute typo in fetch * setabsolutediscount and converttoreduc controller actions take into account localtaxes * usage of localtaxes for discounts in objects * phpcs * stan typing * integrity of multicurrency for generated credit note from supplier invoice --- htdocs/comm/propal/class/propal.class.php | 8 +- htdocs/comm/remx.php | 64 ++---- htdocs/commande/class/commande.class.php | 7 + htdocs/compta/facture/card.php | 36 +++- htdocs/compta/facture/class/facture.class.php | 8 +- htdocs/core/class/discount.class.php | 191 +++++++++++++++++- .../fourn/class/fournisseur.facture.class.php | 7 + htdocs/fourn/facture/card.php | 4 + htdocs/societe/class/societe.class.php | 26 +-- .../class/supplier_proposal.class.php | 7 + test/phpunit/DiscountTest.php | 151 ++++++++++++++ 11 files changed, 432 insertions(+), 77 deletions(-) diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index 4e1f482c2dd..8b3bfe00233 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -20,7 +20,7 @@ * Copyright (C) 2022 Gauthier VERDOL * Copyright (C) 2024-2026 MDW * Copyright (C) 2025 William Mead - * + * Copyright (C) 2026 Vincent de Grandpré * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -568,6 +568,10 @@ class Propal extends CommonObject $line->desc = $remise->description; // Line Description $line->vat_src_code = $remise->vat_src_code; $line->tva_tx = $remise->tva_tx; + $line->localtax1_tx = $remise->localtax1_tx; + $line->localtax1_type = $remise->localtax1_type; + $line->localtax2_tx = $remise->localtax1_tx; + $line->localtax2_type = $remise->localtax1_type; $line->subprice = -(float) $remise->amount_ht; $line->fk_product = 0; // Product Id predefined $line->qty = 1; @@ -578,6 +582,8 @@ class Propal extends CommonObject $line->total_ht = -(float) $remise->amount_ht; $line->total_tva = -(float) $remise->amount_tva; $line->total_ttc = -(float) $remise->amount_ttc; + $line->total_localtax1 = -(float) $remise->total_localtax1; + $line->total_localtax2 = -(float) $remise->total_localtax2; $result = $line->insert(); if ($result > 0) { diff --git a/htdocs/comm/remx.php b/htdocs/comm/remx.php index 84492827d8f..cb8a77d902f 100644 --- a/htdocs/comm/remx.php +++ b/htdocs/comm/remx.php @@ -5,6 +5,7 @@ * Copyright (C) 2019-2025 Frédéric France * Copyright (C) 2024-2026 MDW * Copyright (C) 2025 Anthony Damhet + * Copyright (C) 2026 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -129,10 +130,16 @@ if ($action == 'confirm_split_more' && $permissiontocreate) { $newdiscount->discount_type = $discount->discount_type; $newdiscount->datec = $discount->datec; $newdiscount->tva_tx = $discount->tva_tx; + $newdiscount->localtax1_tx = $discount->localtax1_tx; + $newdiscount->localtax1_type = $discount->localtax1_type; + $newdiscount->localtax2_tx = $discount->localtax2_tx; + $newdiscount->localtax2_type = $discount->localtax2_type; $newdiscount->vat_src_code = $discount->vat_src_code; $newdiscount->amount_ttc = price2num($value); - $newdiscount->amount_ht = price2num($newdiscount->amount_ttc / (1 + $newdiscount->tva_tx / 100), 'MT'); - $newdiscount->amount_tva = price2num($newdiscount->amount_ttc - $newdiscount->amount_ht); + + // Calculate localtaxes and VAT amounts + $newdiscount->generateFromAmount($newdiscount->amount_ttc, 1, $newdiscount->tva_tx, $newdiscount->localtax1_tx, $newdiscount->localtax2_tx, $newdiscount->localtax1_type, $newdiscount->localtax2_type); + $newdiscount->multicurrency_amount_ttc = (float) $value * ($discount->multicurrency_amount_ttc / $discount->amount_ttc); $newdiscount->multicurrency_amount_ht = price2num($newdiscount->multicurrency_amount_ttc / (1 + $newdiscount->tva_tx / 100), 'MT'); $newdiscount->multicurrency_amount_tva = price2num($newdiscount->multicurrency_amount_ttc - $newdiscount->multicurrency_amount_ht); @@ -184,55 +191,10 @@ if ($action == 'confirm_split' && GETPOST("confirm", "alpha") == 'yes' && $permi setEventMessages($langs->trans("ErrorCantSplitAUsedDiscount"), null, 'errors'); } if (!$error) { - $newdiscount1 = new DiscountAbsolute($db); - $newdiscount2 = new DiscountAbsolute($db); - $newdiscount1->fk_facture_source = $discount->fk_facture_source; - $newdiscount2->fk_facture_source = $discount->fk_facture_source; - $newdiscount1->fk_facture = $discount->fk_facture; - $newdiscount2->fk_facture = $discount->fk_facture; - $newdiscount1->fk_facture_line = $discount->fk_facture_line; - $newdiscount2->fk_facture_line = $discount->fk_facture_line; - $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source; - $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source; - $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier; - $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier; - $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line; - $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line; - if ($discount->description == '(CREDIT_NOTE)' || $discount->description == '(DEPOSIT)') { - $newdiscount1->description = $discount->description; - $newdiscount2->description = $discount->description; - } else { - $newdiscount1->description = $discount->description.' (1)'; - $newdiscount2->description = $discount->description.' (2)'; - } - - $newdiscount1->fk_user = $discount->fk_user; - $newdiscount2->fk_user = $discount->fk_user; - $newdiscount1->fk_soc = $discount->fk_soc; - $newdiscount1->socid = $discount->socid; - $newdiscount2->fk_soc = $discount->fk_soc; - $newdiscount2->socid = $discount->socid; - $newdiscount1->discount_type = $discount->discount_type; - $newdiscount2->discount_type = $discount->discount_type; - $newdiscount1->datec = $discount->datec; - $newdiscount2->datec = $discount->datec; - $newdiscount1->tva_tx = $discount->tva_tx; - $newdiscount2->tva_tx = $discount->tva_tx; - $newdiscount1->vat_src_code = $discount->vat_src_code; - $newdiscount2->vat_src_code = $discount->vat_src_code; - $newdiscount1->amount_ttc = $amount_ttc_1; - $newdiscount2->amount_ttc = price2num($discount->amount_ttc - $newdiscount1->amount_ttc); - $newdiscount1->amount_ht = price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100), 'MT'); - $newdiscount2->amount_ht = price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100), 'MT'); - $newdiscount1->amount_tva = price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht); - $newdiscount2->amount_tva = price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht); - - $newdiscount1->multicurrency_amount_ttc = (float) $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc); - $newdiscount2->multicurrency_amount_ttc = price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc); - $newdiscount1->multicurrency_amount_ht = price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100), 'MT'); - $newdiscount2->multicurrency_amount_ht = price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100), 'MT'); - $newdiscount1->multicurrency_amount_tva = price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht); - $newdiscount2->multicurrency_amount_tva = price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht); + // Split a discount in two + $newDiscounts = $discount->splitAmount((float) $amount_ttc_1, (float) $amount_ttc_2); + $newdiscount1 = $newDiscounts[0]; + $newdiscount2 = $newDiscounts[1]; $db->begin(); diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index ce12be8d3cf..0da11bd8d21 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -15,6 +15,7 @@ * Copyright (C) 2022 Gauthier VERDOL * Copyright (C) 2024-2026 MDW * Copyright (C) 2024 William Mead + * Copyright (C) 2026 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -2183,6 +2184,10 @@ class Commande extends CommonOrder $line->desc = $remise->description; // Description for the order line $line->vat_src_code = $remise->vat_src_code; $line->tva_tx = $remise->tva_tx; + $line->localtax1_tx = $remise->localtax1_tx; + $line->localtax1_type = $remise->localtax1_type; + $line->localtax2_tx = $remise->localtax1_tx; + $line->localtax2_type = $remise->localtax1_type; $line->subprice = -(float) $remise->amount_ht; $line->price = -(float) $remise->amount_ht; $line->fk_product = 0; // Predefined Product ID @@ -2194,6 +2199,8 @@ class Commande extends CommonOrder $line->total_ht = -(float) $remise->amount_ht; $line->total_tva = -(float) $remise->amount_tva; $line->total_ttc = -(float) $remise->amount_ttc; + $line->total_localtax1 = -(float) $remise->total_localtax1; + $line->total_localtax2 = -(float) $remise->total_localtax2; $result = $line->insert(); if ($result > 0) { diff --git a/htdocs/compta/facture/card.php b/htdocs/compta/facture/card.php index 5c01e8e20c5..20553b7987d 100644 --- a/htdocs/compta/facture/card.php +++ b/htdocs/compta/facture/card.php @@ -20,6 +20,7 @@ * Copyright (C) 2024-2026 MDW * Copyright (C) 2024-2025 Alexandre Spangaro * Copyright (C) 2025 Lenin Rivas + * Copyright (C) 2026 Vincent de Grandpré * Copyright (C) 2026 Joachim Küter * * This program is free software; you can redistribute it and/or modify @@ -1018,7 +1019,7 @@ if (empty($reshook)) { if ($canconvert) { $db->begin(); - $amount_ht = $amount_tva = $amount_ttc = array(); + $amount_ht = $amount_tva = $amount_ttc = $amount_localtax1 = $amount_localtax2 = array(); $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array(); // Loop on each vat rate @@ -1035,6 +1036,14 @@ if (empty($reshook)) { $amount_tva[$keyforvatrate] = 0; } $amount_tva[$keyforvatrate] += $line->total_tva; + if (!isset($amount_localtax1[$keyforvatrate])) { + $amount_localtax1[$keyforvatrate] = 0; + } + $amount_localtax1[$keyforvatrate] += $line->total_localtax1; + if (!isset($amount_localtax2[$keyforvatrate])) { + $amount_localtax2[$keyforvatrate] = 0; + } + $amount_localtax2[$keyforvatrate] += $line->total_localtax2; if (!isset($amount_ttc[$keyforvatrate])) { $amount_ttc[$keyforvatrate] = 0; } @@ -1057,6 +1066,8 @@ if (empty($reshook)) { '@phan-var-force array $amount_ht @phan-var-force array $amount_tva @phan-var-force array $amount_ttc + @phan-var-force array $amount_localtax1 + @phan-var-force array $amount_localtax2 @phan-var-force array $multicurrency_amount_ht @phan-var-force array $multicurrency_amount_tva @phan-var-force array $multicurrency_amount_ttc'; @@ -1070,6 +1081,8 @@ if (empty($reshook)) { $amount_ht[$vatrate] = price2num($amount_ht[$vatrate] * $ratio, 'MU'); $amount_tva[$vatrate] = price2num($amount_tva[$vatrate] * $ratio, 'MU'); $amount_ttc[$vatrate] = price2num($amount_ttc[$vatrate] * $ratio, 'MU'); + $amount_localtax1[$vatrate] = price2num($amount_localtax1[$vatrate] * $ratio, 'MU'); + $amount_localtax2[$vatrate] = price2num($amount_localtax2[$vatrate] * $ratio, 'MU'); $multicurrency_amount_ht[$vatrate] = price2num($multicurrency_amount_ht[$vatrate] * $ratio, 'MU'); $multicurrency_amount_tva[$vatrate] = price2num($multicurrency_amount_tva[$vatrate] * $ratio, 'MU'); $multicurrency_amount_ttc[$vatrate] = price2num($multicurrency_amount_ttc[$vatrate] * $ratio, 'MU'); @@ -1136,6 +1149,10 @@ if (empty($reshook)) { $discount->amount_tva = 0; $discount->amount_ht = $discount->amount_ttc; $discount->tva_tx = 0; + $discount->localtax1_tx = 0; + $discount->localtax1_type = 0; + $discount->localtax2_tx = 0; + $discount->localtax2_type = 0; $discount->vat_src_code = ''; if ($discount->amount_ttc > 0) { @@ -1155,12 +1172,17 @@ if (empty($reshook)) { } foreach ($amount_ht as $tva_tx => $xxx) { + // Get localtaxes from TVA tx + $taxes = getTaxesFromId($tva_tx, $object->thirdparty, $mysoc, 0); + if ($object->type == Facture::TYPE_CREDIT_NOTE) { $discount->amount_ht = -((float) $amount_ht[$tva_tx]); $discount->amount_tva = -((float) $amount_tva[$tva_tx]); $discount->amount_ttc = -((float) $amount_ttc[$tva_tx]); $discount->total_ht = -((float) $amount_ht[$tva_tx]); $discount->total_tva = -((float) $amount_tva[$tva_tx]); + $discount->total_localtax1 = -((float) $amount_localtax1[$tva_tx]); + $discount->total_localtax2 = -((float) $amount_localtax2[$tva_tx]); $discount->total_ttc = -((float) $amount_ttc[$tva_tx]); $discount->multicurrency_amount_ht = -((float) $multicurrency_amount_ht[$tva_tx]); $discount->multicurrency_amount_tva = -((float) $multicurrency_amount_tva[$tva_tx]); @@ -1175,6 +1197,8 @@ if (empty($reshook)) { $discount->amount_ttc = abs((float) $amount_ttc[$tva_tx]); $discount->total_ht = abs((float) $amount_ht[$tva_tx]); $discount->total_tva = abs((float) $amount_tva[$tva_tx]); + $discount->total_localtax1 = abs((float) $amount_localtax1[$tva_tx]); + $discount->total_localtax2 = abs((float) $amount_localtax2[$tva_tx]); $discount->total_ttc = abs((float) $amount_ttc[$tva_tx]); $discount->multicurrency_amount_ht = abs((float) $multicurrency_amount_ht[$tva_tx]); $discount->multicurrency_amount_tva = abs((float) $multicurrency_amount_tva[$tva_tx]); @@ -1193,6 +1217,10 @@ if (empty($reshook)) { } $discount->tva_tx = abs((float) $tva_tx); + $discount->localtax1_tx = $taxes['localtax1']; + $discount->localtax1_type = $taxes['localtax1_type']; + $discount->localtax2_tx = $taxes['localtax2']; + $discount->localtax2_type = $taxes['localtax2_type']; $discount->vat_src_code = $vat_src_code; $result = $discount->create($user); @@ -1992,6 +2020,12 @@ if (empty($reshook)) { $discount->total_tva = abs($lines[$i]->total_tva); $discount->total_ttc = abs($lines[$i]->total_ttc); $discount->tva_tx = $lines[$i]->tva_tx; + $discount->localtax1_tx = $lines[$i]->localtax1_tx; + $discount->localtax2_tx = $lines[$i]->localtax2_tx; + $discount->localtax1_type = $lines[$i]->localtax1_type; + $discount->localtax2_type = $lines[$i]->localtax2_type; + $discount->total_localtax1 = abs($lines[$i]->total_localtax1); + $discount->total_localtax2 = abs($lines[$i]->total_localtax2); $discount->fk_user = $user->id; $discount->description = $desc; $discount->multicurrency_subprice = abs($lines[$i]->multicurrency_subprice); diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 3508bcec9e4..495df373c63 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -24,6 +24,7 @@ * Copyright (C) 2024-2026 MDW * Copyright (C) 2024-2025 Frédéric France * Copyright (C) 2025 Lenin Rivas + * Copyright (C) 2026 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -2914,7 +2915,10 @@ class Facture extends CommonInvoice $facligne->desc = $remise->description; // Description of the invoice line $facligne->vat_src_code = $remise->vat_src_code; $facligne->tva_tx = $remise->tva_tx; - + $facligne->localtax1_tx = $remise->localtax1_tx; + $facligne->localtax1_type = (int) $remise->localtax1_type; + $facligne->localtax2_tx = $remise->localtax1_tx; + $facligne->localtax2_type = (int) $remise->localtax1_type; $facligne->subprice = -(float) $remise->total_ht; $facligne->fk_product = 0; // Predefined Product ID $facligne->qty = 1; @@ -2943,6 +2947,8 @@ class Facture extends CommonInvoice $facligne->total_ht = -(float) $remise->total_ht; $facligne->total_tva = -(float) $remise->total_tva; $facligne->total_ttc = -(float) $remise->total_ttc; + $facligne->total_localtax1 = -(float) $remise->total_localtax1; + $facligne->total_localtax2 = -(float) $remise->total_localtax2; $facligne->multicurrency_subprice = -(float) $remise->multicurrency_subprice; $facligne->multicurrency_total_ht = -(float) $remise->multicurrency_total_ht; diff --git a/htdocs/core/class/discount.class.php b/htdocs/core/class/discount.class.php index 64c8522fbb1..50ea7e69440 100644 --- a/htdocs/core/class/discount.class.php +++ b/htdocs/core/class/discount.class.php @@ -5,6 +5,7 @@ * Copyright (C) 2024 Frédéric France * Copyright (C) 2024-2026 MDW * Copyright (C) 2024 Noé Cendrier + * Copyright (C) 2026 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -123,6 +124,22 @@ class DiscountAbsolute extends CommonObject * @var string|float Vat rate */ public $tva_tx; + /** + * @var string|float Localtax1 rate + */ + public $localtax1_tx; + /** + * @var string|float Localtax2 rate + */ + public $localtax2_tx; + /** + * @var string|float Localtax1 type + */ + public $localtax1_type; + /** + * @var string|float Localtax2 type + */ + public $localtax2_type; /** * @var string */ @@ -216,7 +233,7 @@ class DiscountAbsolute extends CommonObject $sql = "SELECT sr.rowid, sr.fk_soc, sr.discount_type,"; $sql .= " sr.fk_user,"; - $sql .= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,"; + $sql .= " sr.amount_ht, sr.amount_tva, sr.amount_localtax1, sr.amount_localtax2, sr.amount_ttc, sr.tva_tx, sr.localtax1_tx, sr.localtax1_type, sr.localtax2_tx, sr.localtax2_type, sr.vat_src_code,"; $sql .= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,"; $sql .= " sr.fk_facture_line, sr.fk_facture, sr.fk_facture_source, sr.fk_invoice_supplier_line, sr.fk_invoice_supplier, sr.fk_invoice_supplier_source, sr.description,"; $sql .= " sr.datec,"; @@ -249,6 +266,8 @@ class DiscountAbsolute extends CommonObject $this->total_ht = $obj->amount_ht; $this->total_tva = $obj->amount_tva; + $this->total_localtax1 = $obj->amount_localtax1; + $this->total_localtax2 = $obj->amount_localtax2; $this->total_ttc = $obj->amount_ttc; // For backward compatibility $this->amount_ht = $this->total_ht; @@ -264,6 +283,10 @@ class DiscountAbsolute extends CommonObject $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc; $this->tva_tx = $obj->tva_tx; + $this->localtax1_tx = $obj->localtax1_tx; + $this->localtax1_type = $obj->localtax1_type; + $this->localtax2_tx = $obj->localtax2_tx; + $this->localtax2_type = $obj->localtax2_type; $this->vat_src_code = $obj->vat_src_code; $this->fk_user = $obj->fk_user; @@ -309,6 +332,10 @@ class DiscountAbsolute extends CommonObject $this->amount_ttc = price2num($this->amount_ttc); $this->tva_tx = price2num($this->tva_tx); + $this->localtax1_tx = price2num($this->localtax1_tx); + $this->localtax1_type = price2num($this->localtax1_type); + $this->localtax2_tx = price2num($this->localtax2_tx); + $this->localtax2_type = price2num($this->localtax2_type); $this->multicurrency_amount_ht = price2num($this->multicurrency_amount_ht); $this->multicurrency_amount_tva = price2num($this->multicurrency_amount_tva); @@ -326,6 +353,14 @@ class DiscountAbsolute extends CommonObject if (empty($this->tva_tx)) { $this->tva_tx = 0; } + if (empty($this->localtax1_tx)) { + $this->localtax1_tx = 0; + $this->localtax1_type = 0; + } + if (empty($this->localtax2_tx)) { + $this->localtax2_tx = 0; + $this->localtax2_type = 0; + } // Check parameters if (empty($this->description)) { @@ -345,13 +380,17 @@ class DiscountAbsolute extends CommonObject // Insert request $sql = "INSERT INTO ".$this->db->prefix()."societe_remise_except"; $sql .= " (entity, datec, fk_soc, discount_type, fk_user, description,"; - $sql .= " amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,"; + $sql .= " amount_ht, amount_tva, amount_localtax1, amount_localtax2, amount_ttc, tva_tx, localtax1_tx, localtax1_type, localtax2_tx, localtax2_type, vat_src_code,"; $sql .= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,"; $sql .= " fk_facture_source, fk_invoice_supplier_source, multicurrency_code, multicurrency_tx"; $sql .= ")"; $sql .= " VALUES (".$conf->entity.", '".$this->db->idate($this->datec != '' ? $this->datec : dol_now())."', ".((int) $this->socid).", ".(empty($this->discount_type) ? 0 : intval($this->discount_type)).", ".((int) $userid).", '".$this->db->escape($this->description)."',"; - $sql .= " ".price2num($this->amount_ht).", ".price2num($this->amount_tva).", ".price2num($this->amount_ttc).", ".price2num($this->tva_tx).", '".$this->db->escape($this->vat_src_code)."',"; - $sql .= " ".price2num($this->multicurrency_amount_ht).", ".price2num($this->multicurrency_amount_tva).", ".price2num($this->multicurrency_amount_ttc).", "; + $sql .= " ".price2num($this->amount_ht).", ".price2num($this->amount_tva).", "; + $sql .= " ".($this->total_localtax1 ? price2num($this->total_localtax1) : 0).", ".($this->total_localtax2 ? price2num($this->total_localtax2) : 0).", ".price2num($this->amount_ttc).", ".price2num($this->tva_tx).","; + $sql .= " ".price2num($this->localtax1_tx).", ".price2num($this->localtax1_type).", "; + $sql .= " ".($this->localtax2_tx ? price2num($this->localtax2_tx) : 0).", ".($this->localtax2_type ? price2num($this->localtax2_type) : 0).", '".$this->db->escape($this->vat_src_code)."',"; + $sql .= " ".price2num($this->multicurrency_amount_ht).", ".price2num($this->multicurrency_amount_tva).", "; + $sql .= " ".price2num($this->multicurrency_amount_ttc).", "; $sql .= " ".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null").","; $sql .= " ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) : "null").","; $sql .= " ".($this->multicurrency_code ? "'".$this->db->escape($this->multicurrency_code)."'" : "null").","; @@ -891,4 +930,148 @@ class DiscountAbsolute extends CommonObject return 1; } + + /** + * Helper method to create a discount from an amount. + * Used by Service.class:set_remise_except() + * @param float $amount Currency amount to process + * @param int<0,1> $amount_type 0 = Amount includes no taxes, 1 = Amount includes all the taxes + * @param float $tva_tx TVA (main tax) percentage (>1) + * @param float $localtax1_tx localtax1 percentage (>1) + * @param float $localtax2_tx localtax2 percentage (>1) + * @param int<0,1> $localtax1_type localtax1 type + * @param int<0,1> $localtax2_type localtax2 type + * @return int OK = >= 0, KO = < 0 + */ + public function generateFromAmount($amount, $amount_type, $tva_tx, $localtax1_tx, $localtax2_tx, $localtax1_type = 1, $localtax2_type = 1) + { + $localtax1_tx_pct = (float) ($localtax1_tx / 100); + $localtax2_tx_pct = (float) ($localtax2_tx / 100); + $tva_tx_pct = (float) ($tva_tx / 100); + $err = 0; + + // Test if localtax type is 2, 4 or 6 (compond tax calculation) + $localtax1_type2 = ((int) $localtax1_type > 0 && (int) $localtax1_type % 2 == 0 ? 1 : 0); + $localtax2_type2 = ((int) $localtax2_type > 0 && (int) $localtax2_type % 2 == 0 ? 1 : 0); + + if ($amount_type == 1) { + // TTC + + $this->amount_ttc = price2num($amount, 'MT'); + + if (!$localtax1_type2 && !$localtax2_type2) { + $diviseur = 1 + $tva_tx_pct + $localtax1_tx_pct + $localtax2_tx_pct; + $this->amount_ht = price2num((float) $amount / $diviseur, 'MT'); + $this->amount_tva = price2num((float) $this->amount_ht * $tva_tx_pct, 'MT'); + } else { + $diviseur = 1 + $tva_tx_pct * ($localtax1_tx_pct > 0 ? $localtax1_tx_pct : 1) * ($localtax2_tx_pct > 0 ? $localtax2_tx_pct : 1); + $lt1 = 0; $lt2 = 0; $ttc = (float) $this->amount_ttc; + if ($localtax2_tx_pct > 0) { + $lt2 = $ttc - $ttc / (1 + $localtax2_tx_pct); + } + if ($localtax1_tx_pct > 0) { + $lt1 = $ttc - $lt2 - ($ttc - $lt2)/ (1 + $localtax1_tx_pct); + } + $tva = ($ttc - $lt2 - $lt1) - ($ttc - $lt2 - $lt1) / ( 1 + $tva_tx_pct); + $this->amount_tva = price2num($tva, 'MT'); + $this->total_localtax1 = $lt1; + $this->total_localtax2 = $lt2; + $this->amount_ht = price2num((float) $this->amount_ttc- $this->total_localtax1 - $this->total_localtax2 - (float) $this->amount_tva, 'MT'); + } + + $this->multicurrency_amount_ttc = price2num($amount * (float) $this->multicurrency_tx, 'MT'); + $this->multicurrency_amount_ht = $this->amount_ht; + $this->multicurrency_amount_tva = price2num((float) $this->amount_ht * $tva_tx_pct * (float) $this->multicurrency_tx, 'MT'); + } elseif ($amount_type == 0) { + // HT + $this->amount_ht = price2num($amount, 'MT'); + $this->amount_tva = price2num((float) $this->amount_ht * $tva_tx_pct, 'MT'); + + $this->multicurrency_amount_ht = price2num($amount * (float) $this->multicurrency_tx, 'MT'); + $this->multicurrency_amount_tva = price2num(((float) $amount * $tva_tx_pct) * (float) $this->multicurrency_tx, 'MT'); + } + + $this->tva_tx = $tva_tx; + $this->localtax1_tx = $localtax1_tx; + $this->localtax1_type = $localtax1_type; + $this->localtax2_tx = $localtax2_tx; + $this->localtax2_type = $localtax2_type; + + if ($localtax1_type2 == 0) { + $this->total_localtax1 = (float) $this->amount_ht * $localtax1_tx_pct; + } elseif ($localtax1_type2 == 1) { + $this->total_localtax1 = ((float) $this->amount_ht + (float) $this->amount_tva) * $localtax1_tx_pct; + } + + if ($localtax2_type2 == 0) { + $this->total_localtax2 = (float) $this->amount_ht * $localtax2_tx_pct; + } elseif ($localtax2_type2 == 1) { + $this->total_localtax2 = ((float) $this->amount_ht + (float) $this->amount_tva + $this->total_localtax1) * $localtax2_tx_pct; + } + + if (empty($this->amount_ttc)) { + // Dans le cas où le montant ttc vient d'une remise scindée on le prend tel quel + // car de recalculer le montant ttc à partir du montant ht créé des erreurs de précision. + $this->amount_ttc = price2num((float) $this->amount_ht + (float) $this->amount_tva + $this->total_localtax1 + $this->total_localtax2, 'MT'); + $this->multicurrency_amount_ttc = price2num(((float) $this->amount_ht + (float) $this->amount_tva + $this->total_localtax1 + $this->total_localtax2) * (float) $this->multicurrency_tx, 'MT'); + } + + return $err; + } + + /** + * Helper method to create two new discounts from split amount. + * + * @param float $amount_ttc1 Currency amount 1 + * @param float $amount_ttc2 Currency amount 2 + * @return array Array of 2 DiscountAbsolute, representing the splits + */ + public function splitAmount($amount_ttc1, $amount_ttc2) + { + // Moved from remx.php :: domain-related operations within action confirm_split + $newdiscount1 = new DiscountAbsolute($this->db); + $newdiscount2 = new DiscountAbsolute($this->db); + $newdiscount1->fk_facture_source = $this->fk_facture_source; + $newdiscount2->fk_facture_source = $this->fk_facture_source; + $newdiscount1->fk_facture = $this->fk_facture; + $newdiscount2->fk_facture = $this->fk_facture; + $newdiscount1->fk_facture_line = $this->fk_facture_line; + $newdiscount2->fk_facture_line = $this->fk_facture_line; + $newdiscount1->fk_invoice_supplier_source = $this->fk_invoice_supplier_source; + $newdiscount2->fk_invoice_supplier_source = $this->fk_invoice_supplier_source; + $newdiscount1->fk_invoice_supplier = $this->fk_invoice_supplier; + $newdiscount2->fk_invoice_supplier = $this->fk_invoice_supplier; + $newdiscount1->fk_invoice_supplier_line = $this->fk_invoice_supplier_line; + $newdiscount2->fk_invoice_supplier_line = $this->fk_invoice_supplier_line; + if ($this->description == '(CREDIT_NOTE)' || $this->description == '(DEPOSIT)') { + $newdiscount1->description = $this->description; + $newdiscount2->description = $this->description; + } else { + $newdiscount1->description = $this->description.' (1)'; + $newdiscount2->description = $this->description.' (2)'; + } + $newdiscount1->fk_user = $this->fk_user; + $newdiscount2->fk_user = $this->fk_user; + $newdiscount1->fk_soc = $this->fk_soc; + $newdiscount1->socid = $this->socid; + $newdiscount2->fk_soc = $this->fk_soc; + $newdiscount2->socid = $this->socid; + $newdiscount1->discount_type = $this->discount_type; + $newdiscount2->discount_type = $this->discount_type; + $newdiscount1->datec = $this->datec; + $newdiscount2->datec = $this->datec; + $newdiscount1->tva_tx = $this->tva_tx; + $newdiscount2->tva_tx = $this->tva_tx; + $newdiscount1->localtax1_tx = $this->localtax1_tx; + $newdiscount2->localtax1_tx = $this->localtax1_tx; + $newdiscount1->localtax2_tx = $this->localtax2_tx; + $newdiscount2->localtax2_tx = $this->localtax2_tx; + $newdiscount1->vat_src_code = $this->vat_src_code; + $newdiscount2->vat_src_code = $this->vat_src_code; + + $newdiscount1->generateFromAmount($amount_ttc1, 1, $newdiscount1->tva_tx, $newdiscount1->localtax1_tx, $newdiscount1->localtax2_tx, $this->localtax1_type, $this->localtax2_type); + $newdiscount2->generateFromAmount($amount_ttc2, 1, $newdiscount1->tva_tx, $newdiscount1->localtax1_tx, $newdiscount1->localtax2_tx, $this->localtax1_type, $this->localtax2_type); + + return array($newdiscount1, $newdiscount2); + } } diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index 5f41250663d..3faba6c38e0 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -16,6 +16,7 @@ * Copyright (C) 2022 Gauthier VERDOL * Copyright (C) 2023 Nick Fragoulis * Copyright (C) 2024-2026 MDW + * Copyright (C) 2026 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -1386,6 +1387,10 @@ class FactureFournisseur extends CommonInvoice $facligne->desc = $remise->description; // Line description $facligne->vat_src_code = $remise->vat_src_code; $facligne->tva_tx = $remise->tva_tx; + $facligne->localtax1_tx = $remise->localtax1_tx; + $facligne->localtax1_type = $remise->localtax1_type; + $facligne->localtax2_tx = $remise->localtax1_tx; + $facligne->localtax2_type = $remise->localtax1_type; $facligne->subprice = -(float) $remise->amount_ht; $facligne->fk_product = 0; // Predefined Product ID $facligne->product_type = 0; @@ -1416,6 +1421,8 @@ class FactureFournisseur extends CommonInvoice $facligne->total_ht = -(float) $remise->amount_ht; $facligne->total_tva = -(float) $remise->amount_tva; $facligne->total_ttc = -(float) $remise->amount_ttc; + $facligne->total_localtax1 = -(float) $remise->total_localtax1; + $facligne->total_localtax2 = -(float) $remise->total_localtax2; $facligne->multicurrency_subprice = -$remise->multicurrency_subprice; $facligne->multicurrency_total_ht = -$remise->multicurrency_total_ht; diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php index 97e2b354f11..1e289038572 100644 --- a/htdocs/fourn/facture/card.php +++ b/htdocs/fourn/facture/card.php @@ -14,6 +14,7 @@ * Copyright (C) 2022 Gauthier VERDOL * Copyright (C) 2023 Nick Fragoulis * Copyright (C) 2024-2026 MDW + * Copyright (C) 2026 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -969,6 +970,9 @@ if (empty($reshook)) { $line->total_ttc = -$line->total_ttc; $line->total_localtax1 = -$line->total_localtax1; $line->total_localtax2 = -$line->total_localtax2; + $line->multicurrency_total_ht = -$line->multicurrency_total_ht; + $line->multicurrency_total_tva = -$line->multicurrency_total_tva; + $line->multicurrency_total_ttc = -$line->multicurrency_total_ttc; $result = $line->insert(); diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 8d7b6387752..4d36bb31b74 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -21,6 +21,7 @@ * Copyright (C) 2023-2024 Alexandre Janniaux * Copyright (C) 2024 William Mead * Copyright (C) 2024-2026 MDW + * Copyright (C) 2026 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -2728,7 +2729,7 @@ class Societe extends CommonObject public function set_remise_except($remise, User $user, $desc, $vatrate = '', $discount_type = 0, $price_base_type = 'HT') { // phpcs:enable - global $langs; + global $langs, $mysoc; // Clean parameters $remise = (float) price2num($remise); @@ -2745,7 +2746,9 @@ class Societe extends CommonObject } if ($this->id > 0) { - // Clean vat code + // Separate VAT code from VAT rate string + // Discount type determines who is buyer and seller + $taxes = getTaxesFromId($vatrate, ($discount_type == 0 ? $this : $mysoc), ($discount_type == 0 ? $mysoc : $this), 0); $reg = array(); $vat_src_code = ''; if (preg_match('/\((.*)\)/', $vatrate, $reg)) { @@ -2764,25 +2767,10 @@ class Societe extends CommonObject list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code); $discount->multicurrency_tx = $this->multicurrency_tx; - if ($price_base_type == 'TTC') { - $discount->multicurrency_amount_ttc = price2num($remise * (float) $discount->multicurrency_tx, 'MT'); - $discount->multicurrency_amount_ht = price2num(((float) $remise / (1 + (float) $vatrate / 100)) * (float) $discount->multicurrency_tx, 'MT'); - $discount->multicurrency_amount_tva = price2num(((float) $discount->amount_ttc - (float) $discount->amount_ht) * (float) $discount->multicurrency_tx, 'MT'); + $vat_tx = (float) price2num($vatrate); - $discount->amount_ttc = price2num($remise, 'MT'); - $discount->amount_ht = price2num((float) $remise / (1 + (float) $vatrate / 100), 'MT'); - $discount->amount_tva = price2num((float) $discount->amount_ttc - (float) $discount->amount_ht, 'MT'); - } else { - $discount->amount_ht = price2num($remise, 'MT'); - $discount->amount_tva = price2num((float) $remise * (float) $vatrate / 100, 'MT'); - $discount->amount_ttc = price2num((float) $discount->amount_ht + (float) $discount->amount_tva, 'MT'); + $discount->generateFromAmount($remise, ($price_base_type == 'TTC' ? 1 : 0), $vat_tx, $taxes['localtax1'], $taxes['localtax2'], (int) $taxes['localtax1_type'], (int) $taxes['localtax2_type']); - $discount->multicurrency_amount_ht = price2num($remise * (float) $discount->multicurrency_tx, 'MT'); - $discount->multicurrency_amount_tva = price2num(((float) $remise * (float) $vatrate / 100) * (float) $discount->multicurrency_tx, 'MT'); - $discount->multicurrency_amount_ttc = price2num(((float) $discount->amount_ht + (float) $discount->amount_tva) * (float) $discount->multicurrency_tx, 'MT'); - } - - $discount->tva_tx = (float) price2num($vatrate); $discount->vat_src_code = $vat_src_code; $discount->description = $desc; diff --git a/htdocs/supplier_proposal/class/supplier_proposal.class.php b/htdocs/supplier_proposal/class/supplier_proposal.class.php index 645e27ac730..1bb0a45ce0b 100644 --- a/htdocs/supplier_proposal/class/supplier_proposal.class.php +++ b/htdocs/supplier_proposal/class/supplier_proposal.class.php @@ -17,6 +17,7 @@ * Copyright (C) 2020 Tobias Sekan * Copyright (C) 2022 Gauthier VERDOL * Copyright (C) 2024-2026 MDW + * Copyright (C) 2026 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -388,6 +389,10 @@ class SupplierProposal extends CommonObject $supplier_proposalligne->fk_remise_except = $remise->id; $supplier_proposalligne->desc = $remise->description; // Description of the proposal line $supplier_proposalligne->tva_tx = $remise->tva_tx; + $supplier_proposalligne->localtax1_tx = $remise->localtax1_tx; + $supplier_proposalligne->localtax1_type = $remise->localtax1_type; + $supplier_proposalligne->localtax2_tx = $remise->localtax1_tx; + $supplier_proposalligne->localtax2_type = $remise->localtax1_type; $supplier_proposalligne->subprice = -(float) $remise->amount_ht; $supplier_proposalligne->fk_product = 0; // Predefined Product ID $supplier_proposalligne->qty = 1; @@ -398,6 +403,8 @@ class SupplierProposal extends CommonObject $supplier_proposalligne->total_ht = -(float) $remise->amount_ht; $supplier_proposalligne->total_tva = -(float) $remise->amount_tva; $supplier_proposalligne->total_ttc = -(float) $remise->amount_ttc; + $supplier_proposalligne->total_localtax1 = -(float) $remise->total_localtax1; + $supplier_proposalligne->total_localtax2 = -(float) $remise->total_localtax2; $result = $supplier_proposalligne->insert(); if ($result > 0) { diff --git a/test/phpunit/DiscountTest.php b/test/phpunit/DiscountTest.php index 530d6f8eff2..e6a88432ed3 100644 --- a/test/phpunit/DiscountTest.php +++ b/test/phpunit/DiscountTest.php @@ -2,6 +2,7 @@ /* Copyright (C) 2010 Laurent Destailleur * Copyright (C) 2023 Alexandre Janniaux * Copyright (C) 2024 Frédéric France + * Copyright (C) 2026 Vincent de Grandpré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,6 +47,10 @@ $conf->global->MAIN_DISABLE_ALL_MAILS = 1; * @backupGlobals disabled * @backupStaticAttributes enabled * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased. + * @phan-file-suppress PhanUndeclaredClass + * @phan-file-suppress PhanUndeclaredExtendedClass + * @phan-file-suppress PhanUndeclaredMethod + * @phan-file-suppress PhanUndeclaredProperty */ class DiscountTest extends CommonClassTest { @@ -65,6 +70,8 @@ class DiscountTest extends CommonClassTest $soc = new Societe($db); $soc->name = "CommandeTest Unittest"; $socid = $soc->create($user); + + // @phan-suppress-next-line PhanUndeclaredMethod $this->assertLessThan($socid, 0, $soc->errorsToString()); $localobject = new DiscountAbsolute($db); @@ -72,6 +79,7 @@ class DiscountTest extends CommonClassTest $localobject->socid = $socid; $result = $localobject->create($user); + // @phan-suppress-next-line PhanUndeclaredMethod $this->assertLessThan($result, 0); print __METHOD__." result=".$result."\n"; return $result; @@ -97,6 +105,7 @@ class DiscountTest extends CommonClassTest $localobject = new DiscountAbsolute($db); $result = $localobject->fetch($id); + // @phan-suppress-next-line PhanUndeclaredMethod $this->assertLessThan($result, 0); print __METHOD__." id=".$id." result=".$result."\n"; return $id; @@ -124,7 +133,149 @@ class DiscountTest extends CommonClassTest $result = $localobject->delete($user); print __METHOD__." id=".$id." result=".$result."\n"; + // @phan-suppress-next-line PhanUndeclaredMethod $this->assertLessThan($result, 0); return $result; } + + /** + * testDiscountScenarioConfirmSplit + * + * test the scenario of action 'confirm_split' in remx.php + * also support 'confirm_split_more' + * @param float $total_amount initial amount of discount to split + * @param float $splitamount_1 first half of split discount + * @param float $tva_tx vat rate + * @param float $localtax1_tx localtax1 rate + * @param int $localtax1_type localtax1 type + * @param float $localtax2_tx localtax2 rate + * @param int $localtax2_type localtax1 type + * @param float $ex_ht_amount1 expected amount 1 HT + * @param float $ex_total_amount1 expected amount 1 TTC + * @param float $ex_ht_amount2 expected amount 2 HT + * @param float $ex_total_amount2 expected amount 2 TTC + * @return int + * @dataProvider providerSplitRemiseData + */ + public function testDiscountSplitScenarioConfirmSplit($total_amount, $splitamount_1, $tva_tx, $localtax1_tx, $localtax1_type, $localtax2_tx, $localtax2_type, $ex_ht_amount1, $ex_total_amount1, $ex_ht_amount2, $ex_total_amount2) + { + global $conf,$user,$langs,$db; + $conf = $this->savconf; + $user = $this->savuser; + $langs = $this->savlangs; + $db = $this->savdb; + + /** + * Create a DiscountAbsolute object from spec, split it and + * test results with expected. + */ + $localobject = new DiscountAbsolute($db); + $localobject->tva_tx = $tva_tx; + $localobject->localtax1_tx = $localtax1_tx; + $localobject->localtax1_type = $localtax1_type; + $localobject->localtax2_tx = $localtax2_tx; + $localobject->localtax2_type = $localtax2_type; + $newDiscounts = $localobject->splitAmount($splitamount_1, $total_amount - $splitamount_1); + $newdiscount1 = $newDiscounts[0]; + $newdiscount2 = $newDiscounts[1]; + // @phan-suppress-next-line PhanUndeclaredMethod + $this->assertEquals($ex_ht_amount1, $newdiscount1->amount_ht); + // @phan-suppress-next-line PhanUndeclaredMethod + $this->assertEquals($ex_total_amount1, $newdiscount1->amount_ttc); + // @phan-suppress-next-line PhanUndeclaredMethod + $this->assertEquals($ex_ht_amount2, $newdiscount2->amount_ht); + // @phan-suppress-next-line PhanUndeclaredMethod + $this->assertEquals($ex_total_amount2, $newdiscount2->amount_ttc); + $result = 1; + + print __METHOD__." total_amount=".$total_amount." splitamount_1=".$splitamount_1." tva_tx=".$tva_tx." localtax1_tx=".$localtax1_tx." localtax1_type=".$localtax1_type." localtax2_tx=".$localtax2_tx." localtax2_type=".$localtax2_type." result=".$result."\n"; + + return $result; + } + + /** + * testDiscountScenarioSetRemise + * + * test the scenario of function set_remise_exept of class Societe + * + * @param float $amount amount of discount + * @param float $vat_tx vat rate + * @param float $localtax1_tx localtax1 rate + * @param int $localtax1_type localtax1 type + * @param float $localtax2_tx localtax2 rate + * @param int $localtax2_type localtax1 type + * @param string $price_base ('HT' or something else) + * @param float $ex_total_tva expected discount vat amount + * @param float $ex_total_localtax1 expected discount localtax1 amount + * @param float $ex_total_localtax2 expected discount localtax2 amount + * @param float $ex_total_ttc expected discount total TTC amount + * @param float $ex_total_ht expected discount HT amount + * @return int + * @dataProvider providerRemiseData + */ + public function testDiscountScenarioSetRemise($amount, $vat_tx, $localtax1_tx, $localtax1_type, $localtax2_tx, $localtax2_type, $price_base, $ex_total_tva, $ex_total_localtax1, $ex_total_localtax2, $ex_total_ttc, $ex_total_ht) + { + global $conf,$user,$langs,$db; + $conf = $this->savconf; + $user = $this->savuser; + $langs = $this->savlangs; + $db = $this->savdb; + + /** + * Create a DiscountAbsolute object with spec and test with expected result + */ + $localobject = new DiscountAbsolute($db); + + $localobject->generateFromAmount($amount, ($price_base == 'HT' ? 0 : 1), $vat_tx, $localtax1_tx, $localtax2_tx, $localtax1_type, $localtax2_type); + + // @phan-suppress-next-line PhanUndeclaredMethod + $this->assertEquals($ex_total_ht, $localobject->amount_ht); + // @phan-suppress-next-line PhanUndeclaredMethod + $this->assertEquals($ex_total_ttc, $localobject->amount_ttc); + // @phan-suppress-next-line PhanUndeclaredMethod + $this->assertEquals($ex_total_tva, $localobject->amount_tva); + // @phan-suppress-next-line PhanUndeclaredMethod + $this->assertEquals($ex_total_localtax1, price2num($localobject->total_localtax1, 'MT')); + // @phan-suppress-next-line PhanUndeclaredMethod + $this->assertEquals($ex_total_localtax2, price2num($localobject->total_localtax2, 'MT')); + $result = 1; + + print __METHOD__." amount=".$amount." vat_tx=".$vat_tx." localtax1_tx=".$localtax1_tx." localtax1_type=".$localtax1_type." localtax2_tx=".$localtax2_tx." localtax2_type=".$localtax2_type." price_base=".$price_base." result=".$result."\n"; + + return $result; + } + + /** + * Provide test data for AbsoluteDiscount + * + * @return array values and expectations data + */ + public function providerRemiseData() + { + // array(amount, vat_tx, localtax1_tx, localtax1_type, localtax2_tx, localtax2_type, price_base, ex_total_tva, ex_total_localtax1, ex_total_localtax2, ex_total_ttc, ex_total_ht), + return array( + array(1234,5,9.975,1,0,0,'HT',61.7,123.09,0,1418.79,1234), + array(1418.79,5,9.975,1,0,0,'',61.7,123.09,0,1418.79,1234), + array(1234,5,9.975,1,4,1,'HT',61.7,123.09,49.36,1468.15,1234), + array(1468.15,5,9.975,1,4,1,'',61.7,123.09,49.36,1468.15,1234), + array(1234,5,9.975,2,0,0,'HT',61.7,129.25,0,1424.95,1234), + array(1424.95,5,9.975,2,0,0,'',61.7,129.25,0,1424.95,1234), + array(1234,5,9.975,2,4,2,'HT',61.7,129.25,57,1481.94,1234), + array(1481.94,5,9.975,2,4,2,'',61.7,129.25,57,1481.94,1234) + ); + } + + /** + * Provide test data for AbsoluteDiscount splitting + * + * @return array values and expectations data + */ + public function providerSplitRemiseData() + { + // A fixed discount with a taxed total + // array(total_amount, splitamount_1, tva_tx, localtax1_tx, localtax1_type, localtax2_tx, localtax2_type, ex_ht_amount1, ex_total_amount1, ex_ht_amount2, ex_total_amount2), + return array( + array(1468.15,1000,5,9.975,1,0,0,869.75,1000,407.18,468.15) + ); + } }