From b1cd6c5ecf3c745e4953f312decc9602ba5f3f87 Mon Sep 17 00:00:00 2001 From: Francis Appels Date: Tue, 26 Jan 2021 16:18:50 +0100 Subject: [PATCH 01/12] mo line is line so no fetchLines --- htdocs/mrp/class/mo.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index 274782a14ea..1d04852efea 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -1487,7 +1487,7 @@ class MoLine extends CommonObjectLine public function fetch($id, $ref = null) { $result = $this->fetchCommon($id, $ref); - if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines(); + return $result; } From 060ddff88389a8ecfa2264afb47cb5383fe7e009 Mon Sep 17 00:00:00 2001 From: Francis Appels Date: Sat, 30 Jan 2021 17:27:16 +0100 Subject: [PATCH 02/12] Add missing pmp valorisation to MO production. (Issue #16072) --- htdocs/mrp/mo_movements.php | 6 ++- htdocs/mrp/mo_production.php | 76 +++++++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/htdocs/mrp/mo_movements.php b/htdocs/mrp/mo_movements.php index b4dbed059bc..8a54a8b0cbd 100644 --- a/htdocs/mrp/mo_movements.php +++ b/htdocs/mrp/mo_movements.php @@ -119,7 +119,7 @@ $arrayfields = array( 'm.type_mouvement'=>array('label'=>$langs->trans("TypeMovement"), 'checked'=>1, 'position'=>48), 'origin'=>array('label'=>$langs->trans("Origin"), 'enabled'=>0, 'checked'=>0, 'position'=>50), 'm.value'=>array('label'=>$langs->trans("Qty"), 'checked'=>1, 'position'=>60), - 'm.price'=>array('label'=>$langs->trans("UnitPurchaseValue"), 'enabled'=>0, 'checked'=>0, 'position'=>62), + 'm.price'=>array('label'=>$langs->trans("UnitCost"), 'enabled'=>0, 'checked'=>0, 'position'=>62), //'m.datec'=>array('label'=>$langs->trans("DateCreation"), 'checked'=>0, 'position'=>500), //'m.tms'=>array('label'=>$langs->trans("DateModificationShort"), 'checked'=>0, 'position'=>500) ); @@ -139,7 +139,11 @@ $permissiontodelete = $user->rights->mrp->delete || ($permissiontoadd && isset($ $upload_dir = $conf->mrp->multidir_output[isset($object->entity) ? $object->entity : 1]; $permissiontoproduce = $permissiontoadd; +$permissiontoupdatecost = $user->rights->bom->write; // User who can define cost must have knowledge of pricing +if ($permissiontoupdatecost) { + $arrayfields['m.price']['enabled'] = 1; +} /* * Actions diff --git a/htdocs/mrp/mo_production.php b/htdocs/mrp/mo_production.php index c1b6fa64ec8..eb40b1d6f8b 100644 --- a/htdocs/mrp/mo_production.php +++ b/htdocs/mrp/mo_production.php @@ -34,6 +34,7 @@ require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php'; dol_include_once('/mrp/class/mo.class.php'); +dol_include_once('/bom/class/bom.class.php'); dol_include_once('/mrp/lib/mrp_mo.lib.php'); // Load translation files required by the page @@ -88,6 +89,7 @@ $permissiontodelete = $user->rights->mrp->delete || ($permissiontoadd && isset($ $upload_dir = $conf->mrp->multidir_output[isset($object->entity) ? $object->entity : 1]; $permissiontoproduce = $permissiontoadd; +$permissiontoupdatecost = $user->rights->bom->write; // User who can define cost must have knowledge of pricing /* @@ -149,7 +151,7 @@ if (empty($reshook)) $moline->fk_mo = $object->id; $moline->qty = GETPOST('qtytoadd', 'int'); ; $moline->fk_product = GETPOST('productidtoadd', 'int'); - $moline->role = 'toconsume'; + $moline->role = 'toconsumef'; // free consume line $moline->position = 0; $resultline = $moline->create($user, false); // Never use triggers here @@ -168,10 +170,10 @@ if (empty($reshook)) $codemovement = GETPOST('inventorycode', 'alphanohtml'); $db->begin(); - + $pos = 0; // Process line to consume foreach ($object->lines as $line) { - if ($line->role == 'toconsume') { + if (preg_match('/toconsume/', $line->role)) { $tmpproduct = new Product($db); $tmpproduct->fetch($line->fk_product); @@ -207,7 +209,6 @@ if (empty($reshook)) } if (!$error) { - $pos = 0; // Record consumption $moline = new MoLine($db); $moline->fk_mo = $object->id; @@ -237,6 +238,7 @@ if (empty($reshook)) } // Process line to produce + $pos = 0; foreach ($object->lines as $line) { if ($line->role == 'toproduce') { $tmpproduct = new Product($db); @@ -245,6 +247,7 @@ if (empty($reshook)) $i = 1; while (GETPOSTISSET('qtytoproduce-'.$line->id.'-'.$i)) { $qtytoprocess = price2num(GETPOST('qtytoproduce-'.$line->id.'-'.$i)); + $pricetoprocess = GETPOST('pricetoproduce-'.$line->id.'-'.$i) ? price2num(GETPOST('pricetoproduce-'.$line->id.'-'.$i)) : 0; if ($qtytoprocess != 0) { // Check warehouse is set if we should have to @@ -266,7 +269,7 @@ if (empty($reshook)) // Record stock movement $id_product_batch = 0; $stockmove->origin = $object; - $idstockmove = $stockmove->reception($user, $line->fk_product, GETPOST('idwarehousetoproduce-'.$line->id.'-'.$i), $qtytoprocess, 0, $labelmovement, '', '', GETPOST('batchtoproduce-'.$line->id.'-'.$i), dol_now(), $id_product_batch, $codemovement); + $idstockmove = $stockmove->reception($user, $line->fk_product, GETPOST('idwarehousetoproduce-'.$line->id.'-'.$i), $qtytoprocess, $pricetoprocess, $labelmovement, '', '', GETPOST('batchtoproduce-'.$line->id.'-'.$i), dol_now(), $id_product_batch, $codemovement); if ($idstockmove < 0) { $error++; setEventMessages($stockmove->error, $stockmove->errors, 'errors'); @@ -274,7 +277,6 @@ if (empty($reshook)) } if (!$error) { - $pos = 0; // Record production $moline = new MoLine($db); $moline->fk_mo = $object->id; @@ -309,7 +311,7 @@ if (empty($reshook)) if (GETPOST('autoclose', 'int')) { foreach ($object->lines as $line) { - if ($line->role == 'toconsume') { + if (preg_match('/toconsume/', $line->role)) { $arrayoflines = $object->fetchLinesLinked('consumed', $line->id); $alreadyconsumed = 0; foreach ($arrayoflines as $line2) { @@ -677,12 +679,23 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // Show object lines $object->fetchLines(); + $bomcost = 0; + if ($object->fk_bom > 0) { + $bom = new Bom($db); + $res = $bom->fetch($object->fk_bom); + if ($res > 0) { + $bomcost = $bom->unit_cost; + } + } + + // consumtion + print '
'; print '
'; print '
'; $newlinetext = ''; - if ($action != 'consumeorproduce' && $action != 'consumeandproduceall') { + if (($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) && $action != 'consumeorproduce' && $action != 'consumeandproduceall') { $newlinetext = ''.$langs->trans("AddNewConsumeLines").''; } print load_fiche_titre($langs->trans('Consumption'), '', '', 0, '', '', $newlinetext); @@ -693,6 +706,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea print ''; print ''.$langs->trans("Product").''; print ''.$langs->trans("Qty").''; + if ($permissiontoupdatecost) print ''.$langs->trans("PMPValue").''; print ''.$langs->trans("QtyAlreadyConsumed").''; print ''; if ($collapse || in_array($action, array('consumeorproduce', 'consumeandproduceall'))) print $langs->trans("Warehouse"); @@ -711,6 +725,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea print $form->select_produits('', 'productidtoadd', '', 0, 0, -1, 2, '', 0, array(), 0, '1', 0, 'maxwidth300'); print ''; print ''; + if ($permissiontoupdatecost) print ''; print ''; print ''; print ''; @@ -734,12 +749,25 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea $nblinetoconsumecursor = 0; foreach ($object->lines as $line) { - if ($line->role == 'toconsume') { + if (preg_match('/toconsume/', $line->role)) { $nblinetoconsumecursor++; $tmpproduct = new Product($db); $tmpproduct->fetch($line->fk_product); + if (!empty($bomcost) && $line->role == 'toconsumef' && $object->qty > 0) { + // add free consume line cost to bomcost + require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; + $productFournisseur = new ProductFournisseur($db); + $linecost = price2num((!empty($tmpproduct->cost_price)) ? $tmpproduct->cost_price : $tmpproduct->pmp); + if (empty($linecost)) { + if ($productFournisseur->find_min_price_product_fournisseur($line->fk_product) > 0){ + $linecost = $productFournisseur->fourn_unitprice; + } + } + $bomcost += price2num(($line->qty * $linecost) / $object->qty, 'MT'); + } + $arrayoflines = $object->fetchLinesLinked('consumed', $line->id); $alreadyconsumed = 0; foreach ($arrayoflines as $line2) { @@ -760,6 +788,11 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea print $line->qty; } print ''; + if ($permissiontoupdatecost) { + print ''; + print price($tmpproduct->pmp); + print ''; + } print ''; if ($alreadyconsumed) { print '