From 36068d4279375d8b813d097f9deecd998babecb3 Mon Sep 17 00:00:00 2001 From: Adrien Raze Date: Wed, 26 Jan 2022 14:41:36 +0100 Subject: [PATCH] FIX : Fixing total errors --- htdocs/bom/class/bom.class.php | 41 ++++++++++++++++++-------- htdocs/bom/tpl/objectline_view.tpl.php | 17 ++++------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 0520c76dde2..6f170fb066b 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1064,26 +1064,41 @@ class BOM extends CommonObject $tmpproduct->cost_price = 0; $tmpproduct->pmp = 0; - $result = $tmpproduct->fetch($line->fk_product, '', '', '', 0, 1, 1); // We discard selling price and language loading - if ($result < 0) { - $this->error = $tmpproduct->error; - return -1; - } - $line->unit_cost = price2num((!empty($tmpproduct->cost_price)) ? $tmpproduct->cost_price : $tmpproduct->pmp); - if (empty($line->unit_cost)) { - if ($productFournisseur->find_min_price_product_fournisseur($line->fk_product) > 0) { - $line->unit_cost = $productFournisseur->fourn_unitprice; + if(empty($line->fk_bom_child)){ + $result = $tmpproduct->fetch($line->fk_product, '', '', '', 0, 1, 1); // We discard selling price and language loading + if ($result < 0) { + $this->error = $tmpproduct->error; + return -1; + } + $line->unit_cost = price2num((!empty($tmpproduct->cost_price)) ? $tmpproduct->cost_price : $tmpproduct->pmp); + if (empty($line->unit_cost)) { + if ($productFournisseur->find_min_price_product_fournisseur($line->fk_product) > 0) { + $line->unit_cost = $productFournisseur->fourn_unitprice; + } + } + + $line->total_cost = price2num($line->qty * $line->unit_cost, 'MT'); + + $this->total_cost += $line->total_cost; + } else { + $bom_child= new BOM($this->db); + $res = $bom_child->fetch($line->fk_bom_child); + if($res>0){ + $bom_child->calculateCosts(); + $this->total_cost += $bom_child->total_cost; + } else { + $this->error = $bom_child->error; + return -2; } } - $line->total_cost = price2num($line->qty * $line->unit_cost, 'MT'); - - $this->total_cost += $line->total_cost; } $this->total_cost = price2num($this->total_cost, 'MT'); - if ($this->qty) { + if ($this->qty > 0) { $this->unit_cost = price2num($this->total_cost / $this->qty, 'MU'); + } elseif ($this->qty < 0) { + $this->unit_cost = price2num($this->total_cost * $this->qty, 'MU'); } } } diff --git a/htdocs/bom/tpl/objectline_view.tpl.php b/htdocs/bom/tpl/objectline_view.tpl.php index 0c74cee43ce..fc40c8a54f2 100644 --- a/htdocs/bom/tpl/objectline_view.tpl.php +++ b/htdocs/bom/tpl/objectline_view.tpl.php @@ -232,27 +232,22 @@ if ($resql) { print ''.$sub_bom_line->efficiency.''; // Cost price if it's defined - if (!empty($obj->fk_bom_child)) { - $bom = new BOM($object->db); - $bom->fetch($obj->fk_bom_child); - $bom->calculateCosts(); - $bom->total_cost * $line->qty; - print ''.price($bom->total_cost * $line->qty).''; - $total_cost+= $bom->total_cost * $line->qty; - } elseif ($sub_bom_product->cost_price > 0) { + if ($sub_bom_product->cost_price > 0) { print ''.price($sub_bom_product->cost_price * $line->qty).''; $total_cost+= $sub_bom_product->cost_price * $line->qty; } elseif ($sub_bom_product->pmp > 0) { // PMP if cost price isn't defined print ''.price($sub_bom_product->pmp * $line->qty).''; $total_cost.= $sub_bom_product->pmp * $line->qty; } else { // Minimum purchase price if cost price and PMP aren't defined - $sql_supplier_price = 'SELECT MIN(price) AS min_price FROM '.MAIN_DB_PREFIX.'product_fournisseur_price'; + $sql_supplier_price = 'SELECT MIN(price) AS min_price, quantity AS qty FROM '.MAIN_DB_PREFIX.'product_fournisseur_price'; $sql_supplier_price.= ' WHERE fk_product = '. (int) $sub_bom_product->id; $resql_supplier_price = $object->db->query($sql_supplier_price); if ($resql_supplier_price) { $obj = $object->db->fetch_object($resql_supplier_price); - print ''.price($obj->min_price * $line->qty).''; - $total_cost+= $obj->min_price * $line->qty; + $line_cost = $obj->min_price/$obj->qty * $sub_bom_line->qty; + + print ''.price($line_cost).''; + $total_cost+= $line_cost; } }