diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php
index 7d83a88ce55..baf6e3e1a91 100644
--- a/htdocs/bom/bom_card.php
+++ b/htdocs/bom/bom_card.php
@@ -148,8 +148,17 @@ if (empty($reshook)) {
$error = 0;
// Set if we used free entry or predefined product
- $idprod = (int) GETPOST('idprod', 'int');
- $bom_child = (int) GETPOST('bom_select', 'int');
+ $bom_child_id = (int) GETPOST('bom_id', 'int');
+ if ($bom_child_id > 0) {
+ $bom_child = new BOM($db);
+ $res = $bom_child->fetch($bom_child_id);
+ if ($res) {
+ $idprod = $bom_child->fk_product;
+ }
+ } else {
+ $idprod = (int) GETPOST('idprod', 'int');
+ }
+
$qty = price2num(GETPOST('qty', 'alpha'), 'MS');
$qty_frozen = price2num(GETPOST('qty_frozen', 'alpha'), 'MS');
$disable_stock_change = GETPOST('disable_stock_change', 'int');
@@ -173,7 +182,7 @@ if (empty($reshook)) {
$bomline = new BOMLine($db);
$bomline->fk_bom = $id;
$bomline->fk_product = $idprod;
- $bomline->fk_bom_child = $bom_child;
+ $bomline->fk_bom_child = $bom_child_id;
$bomline->qty = $qty;
$bomline->qty_frozen = (int) $qty_frozen;
$bomline->disable_stock_change = (int) $disable_stock_change;
diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php
index 0520c76dde2..4923e9d0ca2 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_create.tpl.php b/htdocs/bom/tpl/objectline_create.tpl.php
index 7419c4618e0..0a3a3b34e7a 100644
--- a/htdocs/bom/tpl/objectline_create.tpl.php
+++ b/htdocs/bom/tpl/objectline_create.tpl.php
@@ -109,7 +109,7 @@ if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) {
if (!empty($conf->global->BOM_SUB_BOM)) {
print '
'.$langs->trans("or").'
'.$langs->trans("BOM");
// TODO Add component to select a BOM
- print '';
+ $form->select_bom();
}
print '';
diff --git a/htdocs/bom/tpl/objectline_view.tpl.php b/htdocs/bom/tpl/objectline_view.tpl.php
index 82626c12fec..4360d521036 100644
--- a/htdocs/bom/tpl/objectline_view.tpl.php
+++ b/htdocs/bom/tpl/objectline_view.tpl.php
@@ -41,7 +41,7 @@ if (empty($object) || !is_object($object)) {
}
-global $forceall, $senderissupplier, $inputalsopricewithtax, $outputalsopricetotalwithtax;
+global $forceall, $senderissupplier, $inputalsopricewithtax, $outputalsopricetotalwithtax, $langs;
if (empty($dateSelector)) {
$dateSelector = 0;
@@ -83,8 +83,10 @@ $tmpproduct->fetch($line->fk_product);
$tmpbom = new BOM($object->db);
$res = $tmpbom->fetch($line->fk_bom_child);
if ($tmpbom->id > 0) {
- print $tmpbom->getNomUrl(1);
print '' . (empty($conf->global->BOM_SHOW_ALL_BOM_BY_DEFAULT) ? '(+)' : '(-)') . ' ';
+ print $tmpproduct->getNomUrl(1);
+ print ' '.$langs->trans('or').' ';
+ print $tmpbom->getNomUrl(1);
} else {
print $tmpproduct->getNomUrl(1);
print ' - '.$tmpproduct->label;
@@ -176,7 +178,7 @@ if ($action == 'selectlines') {
print '';
// Select of all the sub-BOM lines
-$sql = 'SELECT rowid, fk_bom_child, fk_product FROM '.MAIN_DB_PREFIX.'bom_bomline AS bl';
+$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);
@@ -199,21 +201,23 @@ if ($resql) {
print '