diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php
index cbd730e5d23..f6e40b511f1 100644
--- a/htdocs/bom/bom_card.php
+++ b/htdocs/bom/bom_card.php
@@ -518,7 +518,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
// Common attributes
$keyforbreak = 'duration';
include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
-
+ $object->calculateCosts();
print '
| '.$form->textwithpicto($langs->trans("TotalCost"), $langs->trans("BOMTotalCost")).' | '.price($object->total_cost).' |
';
print '| '.$langs->trans("UnitCost").' | '.price($object->unit_cost).' |
';
diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php
index 58ef900c31d..aa8c7b0d04d 100644
--- a/htdocs/bom/class/bom.class.php
+++ b/htdocs/bom/class/bom.class.php
@@ -1085,7 +1085,7 @@ class BOM extends CommonObject
$res = $bom_child->fetch($line->fk_bom_child);
if ($res>0) {
$bom_child->calculateCosts();
- $this->total_cost += $bom_child->total_cost;
+ $this->total_cost += $bom_child->total_cost * $line->qty;
} else {
$this->error = $bom_child->error;
return -2;
diff --git a/htdocs/bom/tpl/objectline_view.tpl.php b/htdocs/bom/tpl/objectline_view.tpl.php
index b72ed043581..8040310ea53 100644
--- a/htdocs/bom/tpl/objectline_view.tpl.php
+++ b/htdocs/bom/tpl/objectline_view.tpl.php
@@ -180,6 +180,7 @@ if ($action == 'selectlines') {
print '';
// Select of all the sub-BOM lines
+// From this pont to the end of the file, we only take care of sub-BOM lines
$sql = 'SELECT rowid, fk_bom_child, fk_product, qty FROM '.MAIN_DB_PREFIX.'bom_bomline AS bl';
$sql.= ' WHERE fk_bom ='. (int) $tmpbom->id;
$resql = $object->db->query($sql);
@@ -191,7 +192,9 @@ if ($resql) {
$sub_bom_product->fetch($obj->fk_product);
$sub_bom = new BOM($object->db);
- $sub_bom->fetch($obj->fk_bom_child);
+ if (!empty($obj->fk_bom_child)) {
+ $sub_bom->fetch($obj->fk_bom_child);
+ }
$sub_bom_line = new BOMLine($object->db);
$sub_bom_line->fetch($obj->rowid);
@@ -233,20 +236,23 @@ if ($resql) {
// Efficiency
print ''.$sub_bom_line->efficiency.' | ';
- // Cost price if it's defined
- 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;
+ if (!empty($sub_bom->id)) {
+ $sub_bom->calculateCosts();
+ print ''.price($sub_bom->total_cost * $sub_bom_line->qty * $line->qty).' | ';
+ $total_cost+= $sub_bom->total_cost * $sub_bom_line->qty * $line->qty;
+ } elseif ($sub_bom_product->cost_price > 0) {
+ print ''.price($sub_bom_product->cost_price * $sub_bom_line->qty * $line->qty).' | ';
+ $total_cost+= $sub_bom_product->cost_price * $sub_bom_line->qty * $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;
+ print ''.price($sub_bom_product->pmp * $sub_bom_line->qty * $line->qty).' | ';
+ $total_cost.= $sub_bom_product->pmp * $sub_bom_line->qty * $line->qty;
} else { // Minimum purchase price if cost price and PMP aren't defined
$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);
- $line_cost = $obj->min_price/$obj->qty * $sub_bom_line->qty;
+ $line_cost = $obj->min_price/$obj->qty * $sub_bom_line->qty * $line->qty;
print ''.price($line_cost).' | ';
$total_cost+= $line_cost;