Finish BOM cost calculation

This commit is contained in:
Maxime Kohlhaas 2020-03-30 10:20:56 +02:00
parent 436fcd39d4
commit b0e7bd8a30
3 changed files with 36 additions and 9 deletions

View File

@ -68,7 +68,6 @@ if (empty($action) && empty($id) && empty($ref)) $action = 'view';
// Load object
include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
$object->calculateCosts();
// Security check - Protection if external user
//if ($user->socid > 0) accessforbidden();
@ -524,7 +523,8 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
$keyforbreak = 'duration';
include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
print '<td>Coût total</td><td>'.price($object->total_cost).'</td>';
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';
@ -545,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;
}
@ -912,7 +923,6 @@ class BOM extends CommonObject
else
{
$this->lines = $result;
$this->calculateCosts();
return $this->lines;
}
}
@ -993,17 +1003,25 @@ class BOM extends CommonObject
return $error;
}
/**
* BOM costs calculation based on cost_price or pmp of each BOM line
*/
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 = $tmpproduct->cost_price; // TODO : add option to work with cost_price or pmp
$line->total_cost = price2num($line->qty * $line->unit_cost);
$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');
}
}
@ -1086,6 +1104,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

@ -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