Merge pull request #13440 from atm-maxime/new_bom_cost

New bom cost
This commit is contained in:
Laurent Destailleur 2020-03-30 19:16:09 +02:00 committed by GitHub
commit 89cbaa3391
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 7 deletions

View File

@ -523,6 +523,9 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
$keyforbreak = 'duration';
include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
print '<tr><td>'.$langs->trans("TotalCost").'</td><td>'.price($object->total_cost).'</td></tr>';
print '<tr><td>'.$langs->trans("UnitCost").'</td><td>'.price($object->unit_cost).'</td></tr>';
// Other attributes
include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
@ -542,9 +545,6 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
if (!empty($object->table_element_line))
{
// Show object lines
$result = $object->getLinesArray();
print ' <form name="addproduct" id="addproduct" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.(($action != 'editline') ? '#addline' : '#line_'.GETPOST('lineid', 'int')).'" method="POST">
<input type="hidden" name="token" value="' . newToken().'">
<input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateline').'">

View File

@ -166,6 +166,16 @@ class BOM extends CommonObject
*/
public $lines = array();
/**
* @var int Calculated cost for the BOM
*/
public $total_cost = 0;
/**
* @var int Calculated cost for 1 unit of the product in BOM
*/
public $unit_cost = 0;
/**
@ -324,6 +334,7 @@ class BOM extends CommonObject
{
$result = $this->fetchCommon($id, $ref);
if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
$this->calculateCosts();
return $result;
}
@ -991,6 +1002,29 @@ class BOM extends CommonObject
return $error;
}
/**
* BOM costs calculation based on cost_price or pmp of each BOM line
* @return void
*/
public function calculateCosts()
{
include_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
$this->unit_cost = 0;
$this->total_cost = 0;
foreach ($this->lines as &$line) {
$tmpproduct = new Product($this->db);
$tmpproduct->fetch($line->fk_product);
$line->unit_cost = (!empty($tmpproduct->cost_price)) ? $tmpproduct->cost_price : $tmpproduct->pmp; // TODO : add option to work with cost_price or pmp
$line->total_cost = price2num($line->qty * $line->unit_cost, 'MT');
$this->total_cost += $line->total_cost;
}
$this->total_cost = price2num($this->total_cost, 'MT');
$this->unit_cost = price2num($this->total_cost / $this->qty, 'MU');
}
}
@ -1072,6 +1106,16 @@ class BOMLine extends CommonObjectLine
public $import_key;
// END MODULEBUILDER PROPERTIES
/**
* @var int Calculated cost for the BOM line
*/
public $total_cost = 0;
/**
* @var int Line unit cost based on product cost price or pmp
*/
public $unit_cost = 0;
/**
* Constructor

View File

@ -133,6 +133,11 @@ print '<td class="bordertop nobottom nowrap linecollost right">';
print '<input type="text" size="1" name="efficiency" id="efficiency" class="flat right" value="'.(GETPOSTISSET("efficiency")?GETPOST("efficiency", 'alpha'):1).'">';
print '</td>';
$coldisplay++;
print '<td class="bordertop nobottom nowrap linecolcost right">';
print '&nbsp;';
print '</td>';
$coldisplay += $colspan;
print '<td class="bordertop nobottom linecoledit center valignmiddle" colspan="'.$colspan.'">';
print '<input type="submit" class="button" value="'.$langs->trans('Add').'" name="addline" id="addline">';

View File

@ -60,13 +60,16 @@ if ($conf->global->PRODUCT_USE_UNITS)
}
// Qty frozen
print '<td class="linecolqty right">'.$form->textwithpicto($langs->trans('QtyFrozen'), $langs->trans("QuantityConsumedInvariable")).'</td>';
print '<td class="linecolqtyfrozen right">'.$form->textwithpicto($langs->trans('QtyFrozen'), $langs->trans("QuantityConsumedInvariable")).'</td>';
// Disable stock change
print '<td class="linecolqty right">'.$form->textwithpicto($langs->trans('DisableStockChange'), $langs->trans('DisableStockChangeHelp')).'</td>';
print '<td class="linecoldisablestockchange right">'.$form->textwithpicto($langs->trans('DisableStockChange'), $langs->trans('DisableStockChangeHelp')).'</td>';
// Efficiency
print '<td class="linecollost right">'.$form->textwithpicto($langs->trans('ManufacturingEfficiency'), $langs->trans('ValueOfMeansLoss')).'</td>';
print '<td class="linecolefficiency right">'.$form->textwithpicto($langs->trans('ManufacturingEfficiency'), $langs->trans('ValueOfMeansLoss')).'</td>';
// Cost
print '<td class="linecolcost right">'.$langs->trans('CostPrice').'</td>';
print '<td class="linecoledit"></td>'; // No width to allow autodim

View File

@ -103,6 +103,11 @@ $coldisplay++;
echo $line->efficiency;
print '</td>';
print '<td class="linecolcost nowrap right">';
$coldisplay++;
echo price($line->total_cost);
print '</td>';
if ($this->status == 0 && ($object_rights->write) && $action != 'selectlines' ) {
print '<td class="linecoledit center">';
$coldisplay++;

View File

@ -70,4 +70,6 @@ ProductQtyToConsumeByMO=Product quantity still to consume by open MO
ProductQtyToProduceByMO=Product quentity still to produce by open MO
AddNewConsumeLines=Add new line to consume
ProductsToConsume=Products to consume
ProductsToProduce=Products to produce
ProductsToProduce=Products to produce
UnitCost=Unit cost
TotalCost=Total cost