From bcbccf0c83f05d123054d47e69a1a5bb8beeaa76 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Tue, 24 Mar 2020 10:32:31 +0100 Subject: [PATCH 1/4] Start working on bom cost --- htdocs/bom/bom_card.php | 3 +++ htdocs/bom/class/bom.class.php | 14 ++++++++++++++ htdocs/bom/tpl/objectline_create.tpl.php | 5 +++++ htdocs/bom/tpl/objectline_title.tpl.php | 9 ++++++--- htdocs/bom/tpl/objectline_view.tpl.php | 5 +++++ 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php index 81b5f858c9b..c4146660053 100644 --- a/htdocs/bom/bom_card.php +++ b/htdocs/bom/bom_card.php @@ -68,6 +68,7 @@ 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(); @@ -523,6 +524,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).''; + // Other attributes include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php'; diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index b53e2a7bc88..5e25ef7fb14 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -912,6 +912,7 @@ class BOM extends CommonObject else { $this->lines = $result; + $this->calculateCosts(); return $this->lines; } } @@ -991,6 +992,19 @@ class BOM extends CommonObject return $error; } + + public function calculateCosts() { + include_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; + $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); + $this->total_cost += $line->total_cost; + } + } } diff --git a/htdocs/bom/tpl/objectline_create.tpl.php b/htdocs/bom/tpl/objectline_create.tpl.php index 445466a27a1..66bcf0cece5 100644 --- a/htdocs/bom/tpl/objectline_create.tpl.php +++ b/htdocs/bom/tpl/objectline_create.tpl.php @@ -133,6 +133,11 @@ print ''; print ''; print ''; +$coldisplay++; +print ''; +print ' '; +print ''; + $coldisplay += $colspan; print ''; print ''; diff --git a/htdocs/bom/tpl/objectline_title.tpl.php b/htdocs/bom/tpl/objectline_title.tpl.php index 138a0b9aa58..ce440dbaab6 100644 --- a/htdocs/bom/tpl/objectline_title.tpl.php +++ b/htdocs/bom/tpl/objectline_title.tpl.php @@ -60,13 +60,16 @@ if ($conf->global->PRODUCT_USE_UNITS) } // Qty frozen -print ''.$form->textwithpicto($langs->trans('QtyFrozen'), $langs->trans("QuantityConsumedInvariable")).''; +print ''.$form->textwithpicto($langs->trans('QtyFrozen'), $langs->trans("QuantityConsumedInvariable")).''; // Disable stock change -print ''.$form->textwithpicto($langs->trans('DisableStockChange'), $langs->trans('DisableStockChangeHelp')).''; +print ''.$form->textwithpicto($langs->trans('DisableStockChange'), $langs->trans('DisableStockChangeHelp')).''; // Efficiency -print ''.$form->textwithpicto($langs->trans('ManufacturingEfficiency'), $langs->trans('ValueOfMeansLoss')).''; +print ''.$form->textwithpicto($langs->trans('ManufacturingEfficiency'), $langs->trans('ValueOfMeansLoss')).''; + +// Cost +print ''.$langs->trans('CostPrice').''; print ''; // No width to allow autodim diff --git a/htdocs/bom/tpl/objectline_view.tpl.php b/htdocs/bom/tpl/objectline_view.tpl.php index 16aa5382f53..ad2c2d3e4c6 100644 --- a/htdocs/bom/tpl/objectline_view.tpl.php +++ b/htdocs/bom/tpl/objectline_view.tpl.php @@ -103,6 +103,11 @@ $coldisplay++; echo $line->efficiency; print ''; +print ''; +$coldisplay++; +echo price($line->total_cost); +print ''; + if ($this->status == 0 && ($object_rights->write) && $action != 'selectlines' ) { print ''; $coldisplay++; From b0e7bd8a30e1c8f150bacf34f9e08e199348289d Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Mon, 30 Mar 2020 10:20:56 +0200 Subject: [PATCH 2/4] 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 From 3a349e653340e3d7a2bc4c4491891b48573828a1 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Mon, 30 Mar 2020 08:25:57 +0000 Subject: [PATCH 3/4] Fixing style errors. --- htdocs/bom/class/bom.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index ea47b39dad2..e65a56a1e72 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1006,7 +1006,8 @@ class BOM extends CommonObject /** * BOM costs calculation based on cost_price or pmp of each BOM line */ - public function calculateCosts() { + public function calculateCosts() + { include_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; $this->unit_cost = 0; $this->total_cost = 0; From 8e4e1002f939a541cf9152da47b0f879f8e89687 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Mon, 30 Mar 2020 10:33:48 +0200 Subject: [PATCH 4/4] Missing return comment --- htdocs/bom/class/bom.class.php | 1 + 1 file changed, 1 insertion(+) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index ea47b39dad2..54411f2da0f 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1005,6 +1005,7 @@ class BOM extends CommonObject /** * 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';