From b0e7bd8a30e1c8f150bacf34f9e08e199348289d Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Mon, 30 Mar 2020 10:20:56 +0200 Subject: [PATCH] Finish BOM cost calculation --- htdocs/bom/bom_card.php | 7 ++----- htdocs/bom/class/bom.class.php | 34 +++++++++++++++++++++++++++++++--- htdocs/langs/en_US/mrp.lang | 4 +++- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php index 4a919d7bb2a..887a319aefe 100644 --- a/htdocs/bom/bom_card.php +++ b/htdocs/bom/bom_card.php @@ -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 'Coût total'.price($object->total_cost).''; + print ''.$langs->trans("TotalCost").''.price($object->total_cost).''; + print ''.$langs->trans("UnitCost").''.price($object->unit_cost).''; // 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 '
diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 5e25ef7fb14..ea47b39dad2 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -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 diff --git a/htdocs/langs/en_US/mrp.lang b/htdocs/langs/en_US/mrp.lang index 41be1d993b8..4e653e04409 100644 --- a/htdocs/langs/en_US/mrp.lang +++ b/htdocs/langs/en_US/mrp.lang @@ -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 \ No newline at end of file +ProductsToProduce=Products to produce +UnitCost=Unit cost +TotalCost=Total cost