FIX calculateCosts of BOM must not be included into fetch

This commit is contained in:
Laurent Destailleur 2021-09-20 15:29:55 +02:00
parent b97ad33911
commit 45c5abea69
3 changed files with 38 additions and 29 deletions

View File

@ -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');

View File

@ -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');
}
}
}
}

View File

@ -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]);
}
}