diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php
index 9aa7d084ef1..64681ea098c 100644
--- a/htdocs/bom/bom_card.php
+++ b/htdocs/bom/bom_card.php
@@ -566,7 +566,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
if (!empty($object->table_element_line)) {
// Products
- $res = $object->fetchLinesbytypeproduct(0);
+ $res = $object->fetchLinesbytypeproduct(0); // Load all lines products into ->lines
$object->calculateCosts();
print ($res == 0 && $object->status >= $object::STATUS_VALIDATED) ? '' : load_fiche_titre($langs->trans('BOMProductsList'), '', 'product');
@@ -615,7 +615,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
// Services
$filtertype = 1;
- $res = $object->fetchLinesbytypeproduct(1);
+ $res = $object->fetchLinesbytypeproduct(1); // Load all lines services into ->lines
$object->calculateCosts();
print ($res == 0 && $object->status >= $object::STATUS_VALIDATED) ? '' : load_fiche_titre($langs->trans('BOMServicesList'), '', 'service');
diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php
index 1edf20c4472..5bc09528026 100644
--- a/htdocs/bom/class/bom.class.php
+++ b/htdocs/bom/class/bom.class.php
@@ -1391,21 +1391,32 @@ class BOM extends CommonObject
}
}
} else {
- //Convert qty to hour
- $unit = measuringUnitString($line->fk_unit, '', '', 1);
- $qty = convertDurationtoHour($line->qty, $unit);
+ // Convert qty of line into hours
+ $unitforline = measuringUnitString($line->fk_unit, '', '', 1);
+ $qtyhourforline = convertDurationtoHour($line->qty, $unitforline);
if (isModEnabled('workstation') && !empty($tmpproduct->fk_default_workstation)) {
$workstation = new Workstation($this->db);
$res = $workstation->fetch($tmpproduct->fk_default_workstation);
- if ($res > 0) $line->total_cost = price2num($qty * ($workstation->thm_operator_estimated + $workstation->thm_machine_estimated), 'MT');
+ if ($res > 0) $line->total_cost = price2num($qtyhourforline * ($workstation->thm_operator_estimated + $workstation->thm_machine_estimated), 'MT');
else {
$this->error = $workstation->error;
return -3;
}
} else {
- $line->total_cost = price2num($qty * $tmpproduct->cost_price, 'MT');
+ $defaultdurationofservice = $tmpproduct->duration;
+ $reg = array();
+ $qtyhourservice = 0;
+ if (preg_match('/^(\d+)([a-z]+)$/', $defaultdurationofservice, $reg)) {
+ $qtyhourservice = convertDurationtoHour($reg[1], $reg[2]);
+ }
+
+ if ($qtyhourservice) {
+ $line->total_cost = price2num($qtyhourforline / $qtyhourservice * $tmpproduct->cost_price, 'MT');
+ } else {
+ $line->total_cost = price2num($line->qty * $tmpproduct->cost_price, 'MT');
+ }
}
$this->total_cost += $line->total_cost;
diff --git a/htdocs/bom/tpl/objectline_view.tpl.php b/htdocs/bom/tpl/objectline_view.tpl.php
index 95bbed79784..b921e23d64d 100644
--- a/htdocs/bom/tpl/objectline_view.tpl.php
+++ b/htdocs/bom/tpl/objectline_view.tpl.php
@@ -78,10 +78,14 @@ $objectline = new BOMLine($object->db);
$coldisplay = 0;
print "\n";
print '
';
+
+// Line nb
if (!empty($conf->global->MAIN_VIEW_LINE_NUMBER)) {
print '| '.($i + 1).' | ';
$coldisplay++;
}
+
+// Product
print '';
print '';
$coldisplay++;
@@ -113,6 +117,7 @@ if (!empty($extrafields)) {
print ' | ';
+// Qty
print '';
$coldisplay++;
echo price($line->qty, 0, '', 0, 0); // Yes, it is a quantity, not a price, but we just want the formating role of function price
@@ -142,7 +147,7 @@ if ($filtertype != 1) {
echo $line->efficiency;
print ' | ';
} else {
- //Unité
+ // Unit
print '';
$coldisplay++;
@@ -166,6 +171,8 @@ if ($filtertype != 1) {
print ' | ';
}
}
+
+// Cost
$total_cost = 0;
$tmpbom->calculateCosts();
print '';
@@ -312,10 +319,12 @@ if ($resql) {
print ' | '.price(price2num($sub_bom_line->total_cost, 'MT')).' | ';
$this->total_cost += $line->total_cost;
} elseif ($sub_bom_product->cost_price > 0) {
- print ''.price(price2num($sub_bom_product->cost_price * $sub_bom_line->qty * $line->qty, 'MT')).' | ';
+ print '';
+ print ''.price(price2num($sub_bom_product->cost_price * $sub_bom_line->qty * $line->qty, 'MT')).' | ';
$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(price2num($sub_bom_product->pmp * $sub_bom_line->qty * $line->qty, 'MT')).' | ';
+ print '';
+ print ''.price(price2num($sub_bom_product->pmp * $sub_bom_line->qty * $line->qty, 'MT')).' | ';
$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';
diff --git a/htdocs/langs/en_US/mrp.lang b/htdocs/langs/en_US/mrp.lang
index fffb9e571da..f8ee50b341b 100644
--- a/htdocs/langs/en_US/mrp.lang
+++ b/htdocs/langs/en_US/mrp.lang
@@ -83,7 +83,7 @@ ProductsToProduce=Products to produce
UnitCost=Unit cost
TotalCost=Total cost
BOMTotalCost=The cost to produce this BOM based on cost of each quantity and product to consume (use Cost price if defined, else Average Weighted Price if defined, else the Best purchase price)
-BOMTotalCostService=If the "Workstation" module is activated and a workstation is defined by default on the line, then the calculation is "quantity (converted into hours) x workstation ahr", otherwise "quantity (converted into hours) x cost price of the service"
+BOMTotalCostService=If the "Workstation" module is activated and a workstation is defined by default on the line, then the calculation is "quantity (converted into hours) x workstation ahr", otherwise "quantity x cost price of the service"
GoOnTabProductionToProduceFirst=You must first have started the production to close a Manufacturing Order (See tab '%s'). But you can Cancel it.
ErrorAVirtualProductCantBeUsedIntoABomOrMo=A kit can't be used into a BOM or a MO
Workstation=Workstation
diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php
index ecb82507738..096a1afe906 100644
--- a/htdocs/mrp/class/mo.class.php
+++ b/htdocs/mrp/class/mo.class.php
@@ -101,7 +101,7 @@ class Mo extends CommonObject
'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'position'=>5, 'notnull'=>1, 'default'=>'1', 'index'=>1),
'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>4, 'position'=>10, 'notnull'=>1, 'default'=>'(PROV)', 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object", 'showoncombobox'=>'1', 'noteditable'=>1),
- 'fk_bom' => array('type'=>'integer:Bom:bom/class/bom.class.php:0:(t.status:=:1)', 'filter'=>'active=1', 'label'=>'BOM', 'enabled'=>'$conf->bom->enabled', 'visible'=>1, 'position'=>33, 'notnull'=>-1, 'index'=>1, 'comment'=>"Original BOM", 'css'=>'minwidth100 maxwidth300', 'csslist'=>'nowraponall', 'picto'=>'bom'),
+ 'fk_bom' => array('type'=>'integer:Bom:bom/class/bom.class.php:0:(t.status:=:1)', 'filter'=>'active=1', 'label'=>'BOM', 'enabled'=>'$conf->bom->enabled', 'visible'=>1, 'position'=>33, 'notnull'=>-1, 'index'=>1, 'comment'=>"Original BOM", 'css'=>'minwidth100 maxwidth500', 'csslist'=>'tdoverflowmax150', 'picto'=>'bom'),
'mrptype' => array('type'=>'integer', 'label'=>'Type', 'enabled'=>1, 'visible'=>1, 'position'=>34, 'notnull'=>1, 'default'=>'0', 'arrayofkeyval'=>array(0=>'Manufacturing', 1=>'Disassemble'), 'css'=>'minwidth150', 'csslist'=>'minwidth150 center'),
'fk_product' => array('type'=>'integer:Product:product/class/product.class.php:0', 'label'=>'Product', 'enabled'=>'$conf->product->enabled', 'visible'=>1, 'position'=>35, 'notnull'=>1, 'index'=>1, 'comment'=>"Product to produce", 'css'=>'maxwidth300', 'csslist'=>'tdoverflowmax100', 'picto'=>'product'),
'qty' => array('type'=>'real', 'label'=>'QtyToProduce', 'enabled'=>1, 'visible'=>1, 'position'=>40, 'notnull'=>1, 'comment'=>"Qty to produce", 'css'=>'width75', 'default'=>1, 'isameasure'=>1),
@@ -255,26 +255,23 @@ class Mo extends CommonObject
*/
public function create(User $user, $notrigger = false)
{
- global $conf;
-
$error = 0;
$idcreated = 0;
- $this->db->begin();
-
- // Check that product is not a kit/virtual product
- if (empty($conf->global->ALLOW_USE_KITS_INTO_BOM_AND_MO) && $this->fk_product > 0) {
+ // If kits feature is enabled and we don't allow kits into BOM and MO, we check that the product is not a kit/virtual product
+ if (getDolGlobalString('PRODUIT_SOUSPRODUITS') && !getDolGlobalString('ALLOW_USE_KITS_INTO_BOM_AND_MO') && $this->fk_product > 0) {
include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
$tmpproduct = new Product($this->db);
$tmpproduct->fetch($this->fk_product);
if ($tmpproduct->hasFatherOrChild(1) > 0) {
$this->error = 'ErrorAVirtualProductCantBeUsedIntoABomOrMo';
$this->errors[] = $this->error;
- $this->db->rollback();
return -1;
}
}
+ $this->db->begin();
+
if ($this->fk_bom > 0) {
// If there is a nown BOM, we force the type of MO to the type of BOM
include_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
@@ -292,7 +289,7 @@ class Mo extends CommonObject
}
if (!$error) {
- $result = $this->updateProduction($user, $notrigger); // Insert lines from BOM
+ $result = $this->createProduction($user, $notrigger); // Insert lines from BOM
if ($result <= 0) {
$error++;
}
@@ -612,6 +609,121 @@ class Mo extends CommonObject
$error++;
}
+ // Update the lines (the qty) to consume or to produce
+ $result = $this->updateProduction($user, $notrigger);
+ if ($result <= 0) {
+ $error++;
+ }
+
+ if (!$error) {
+ $this->db->commit();
+ return 1;
+ } else {
+ $this->db->rollback();
+ return -1;
+ }
+ }
+
+
+ /**
+ * Erase and update the line to consume and to produce.
+ *
+ * @param User $user User that modifies
+ * @param bool $notrigger false=launch triggers after, true=disable triggers
+ * @return int <0 if KO, >0 if OK
+ */
+ public function createProduction(User $user, $notrigger = true)
+ {
+ $error = 0;
+ $role = "";
+
+ if ($this->status != self::STATUS_DRAFT) {
+ return -1;
+ }
+
+ $this->db->begin();
+
+ // Insert lines in mrp_production table from BOM data
+ if (!$error) {
+ $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'mrp_production WHERE fk_mo = '.((int) $this->id);
+ $this->db->query($sql);
+
+ $moline = new MoLine($this->db);
+
+ // Line to produce
+ $moline->fk_mo = $this->id;
+ $moline->qty = $this->qty;
+ $moline->fk_product = $this->fk_product;
+ $moline->position = 1;
+
+ if ($this->fk_bom > 0) { // If a BOM is defined, we know what to produce.
+ include_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
+ $bom = new Bom($this->db);
+ $bom->fetch($this->fk_bom);
+ if ($bom->bomtype == 1) {
+ $role = 'toproduce';
+ $moline->role = 'toconsume';
+ } else {
+ $role = 'toconsume';
+ $moline->role = 'toproduce';
+ }
+ } else {
+ if ($this->mrptype == 1) {
+ $moline->role = 'toconsume';
+ } else {
+ $moline->role = 'toproduce';
+ }
+ }
+
+ $resultline = $moline->create($user, false); // Never use triggers here
+ if ($resultline <= 0) {
+ $error++;
+ $this->error = $moline->error;
+ $this->errors = $moline->errors;
+ dol_print_error($this->db, $moline->error, $moline->errors);
+ }
+
+ if ($this->fk_bom > 0) { // If a BOM is defined, we know what to consume.
+ if ($bom->id > 0) {
+ // Lines to consume
+ if (!$error) {
+ foreach ($bom->lines as $line) {
+ $moline = new MoLine($this->db);
+
+ $moline->fk_mo = $this->id;
+ $moline->origin_id = $line->id;
+ $moline->origin_type = 'bomline';
+ if ($line->qty_frozen) {
+ $moline->qty = $line->qty; // Qty to consume does not depends on quantity to produce
+ } else {
+ $moline->qty = price2num(($line->qty / ( !empty($bom->qty) ? $bom->qty : 1 ) ) * $this->qty / ( !empty($line->efficiency) ? $line->efficiency : 1 ), 'MS'); // Calculate with Qty to produce and more presition
+ }
+ if ($moline->qty <= 0) {
+ $error++;
+ $this->error = "BadValueForquantityToConsume";
+ break;
+ } else {
+ $moline->fk_product = $line->fk_product;
+ $moline->role = $role;
+ $moline->position = $line->position;
+ $moline->qty_frozen = $line->qty_frozen;
+ $moline->disable_stock_change = $line->disable_stock_change;
+
+ $resultline = $moline->create($user, false); // Never use triggers here
+ if ($resultline <= 0) {
+ $error++;
+ $this->error = $moline->error;
+ $this->errors = $moline->errors;
+ dol_print_error($this->db, $moline->error, $moline->errors);
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
if (!$error) {
$this->db->commit();
return 1;
@@ -622,7 +734,7 @@ class Mo extends CommonObject
}
/**
- * Erase and update the line to consume and to produce.
+ * Update quantities in lines to consume and to produce.
*
* @param User $user User that modifies
* @param bool $notrigger false=launch triggers after, true=disable triggers
diff --git a/htdocs/mrp/mo_production.php b/htdocs/mrp/mo_production.php
index 49bafee23c3..894a7156a13 100644
--- a/htdocs/mrp/mo_production.php
+++ b/htdocs/mrp/mo_production.php
@@ -168,6 +168,11 @@ if (empty($reshook)) {
$moline->origin_type = 'free'; // free consume line
$moline->position = 0;
+ // Is it a product or a service ?
+ $tmpproduct = new Product($db);
+ $tmpproduct->fetch($moline->fk_product);
+ $moline->disable_stock_change = ($tmpproduct->type == Product::TYPE_SERVICE ? 1 : 0);
+
$resultline = $moline->create($user, false); // Never use triggers here
if ($resultline <= 0) {
$error++;