From 45c5abea697bbcd65e8598b8e9002a3ac9980e1c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 20 Sep 2021 15:29:55 +0200 Subject: [PATCH] FIX calculateCosts of BOM must not be included into fetch --- htdocs/bom/bom_card.php | 5 ++-- htdocs/bom/class/bom.class.php | 51 +++++++++++++++++++--------------- htdocs/mrp/mo_list.php | 11 ++++---- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php index cd530994b9b..ab6112c0fed 100644 --- a/htdocs/bom/bom_card.php +++ b/htdocs/bom/bom_card.php @@ -71,6 +71,9 @@ if (empty($action) && empty($id) && empty($ref)) { // Load object include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once. +if ($object->id > 0) { + $object->calculateCosts(); +} // Security check - Protection if external user //if ($user->socid > 0) accessforbidden(); @@ -311,8 +314,6 @@ if (($id || $ref) && $action == 'edit') { // Part to show record if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) { - $res = $object->fetch_optionals(); - $head = bomPrepareHead($object); print dol_get_fiche_head($head, 'card', $langs->trans("BillOfMaterials"), -1, 'bom'); diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index b57aaf158fd..bd61243fbac 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -381,7 +381,7 @@ class BOM extends CommonObject if ($result > 0 && !empty($this->table_element_line)) { $this->fetchLines(); } - $this->calculateCosts(); + //$this->calculateCosts(); // This consume a high number of subrequests. Do not call it into fetch but when you need it. return $result; } @@ -1035,7 +1035,8 @@ class BOM extends CommonObject } /** - * BOM costs calculation based on cost_price or pmp of each BOM line + * BOM costs calculation based on cost_price or pmp of each BOM line. + * Set the property ->total_cost and ->unit_cost of BOM. * * @return void */ @@ -1045,30 +1046,36 @@ class BOM extends CommonObject $this->unit_cost = 0; $this->total_cost = 0; - require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; - $productFournisseur = new ProductFournisseur($this->db); - - foreach ($this->lines as &$line) { + if (is_array($this->lines) && count($this->lines)) { + require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; + $productFournisseur = new ProductFournisseur($this->db); $tmpproduct = new Product($this->db); - $result = $tmpproduct->fetch($line->fk_product); - 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; + + foreach ($this->lines as &$line) { + $tmpproduct->cost_price = 0; + $tmpproduct->pmp = 0; + + $result = $tmpproduct->fetch($line->fk_product, '', '', '', 0, 1, 1); + 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; } - $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) { - $this->unit_cost = price2num($this->total_cost / $this->qty, 'MU'); + $this->total_cost = price2num($this->total_cost, 'MT'); + if ($this->qty) { + $this->unit_cost = price2num($this->total_cost / $this->qty, 'MU'); + } } } } diff --git a/htdocs/mrp/mo_list.php b/htdocs/mrp/mo_list.php index 012b43ff61a..cfff1fb6181 100644 --- a/htdocs/mrp/mo_list.php +++ b/htdocs/mrp/mo_list.php @@ -84,7 +84,7 @@ if (!$sortorder) { } // Initialize array of search criterias -$search_all = GETPOST('search_all', 'alphanohtml') ? GETPOST('search_all', 'alphanohtml') : GETPOST('sall', 'alphanohtml'); +$search_all = GETPOST('search_all', 'alphanohtml'); $search = array(); foreach ($object->fields as $key => $val) { if (GETPOST('search_'.$key, 'alpha') !== '') { @@ -270,8 +270,7 @@ $sql .= $hookmanager->resPrint; /* If a group by is required $sql.= " GROUP BY "; -foreach($object->fields as $key => $val) -{ +foreach($object->fields as $key => $val) { $sql.='t.'.$key.', '; } // Add fields from extrafields @@ -343,9 +342,11 @@ if ($limit > 0 && $limit != $conf->liste_limit) { foreach ($search as $key => $val) { if (is_array($search[$key]) && count($search[$key])) { foreach ($search[$key] as $skey) { - $param .= '&search_'.$key.'[]='.urlencode($skey); + if ($skey != '') { + $param .= '&search_'.$key.'[]='.urlencode($skey); + } } - } else { + } elseif ($search[$key] != '') { $param .= '&search_'.$key.'='.urlencode($search[$key]); } }