From d4f20df3ea174a49a53056c8d1c00131eb6c6e78 Mon Sep 17 00:00:00 2001 From: Adrien Raze Date: Tue, 28 Mar 2023 13:52:20 +0200 Subject: [PATCH 1/5] FIX : no usage of the function updateProduction in the update function --- htdocs/mrp/class/mo.class.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index 9c27a21b1f5..43b6adac644 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -612,11 +612,6 @@ class Mo extends CommonObject $error++; } - $result = $this->updateProduction($user, $notrigger); - if ($result <= 0) { - $error++; - } - if (!$error) { $this->db->commit(); return 1; From 805d457be11d32e62043bf67118f751902d71a19 Mon Sep 17 00:00:00 2001 From: Adrien Raze Date: Mon, 3 Apr 2023 16:54:37 +0200 Subject: [PATCH 2/5] FIX : redesign of the function : updateProduction --- htdocs/mrp/class/mo.class.php | 99 ++++++----------------------------- htdocs/mrp/mo_card.php | 2 + 2 files changed, 19 insertions(+), 82 deletions(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index 43b6adac644..7745d05d71a 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -631,93 +631,28 @@ class Mo extends CommonObject public function updateProduction(User $user, $notrigger = true) { $error = 0; - $role = ""; - if ($this->status != self::STATUS_DRAFT) { - //$this->error = 'BadStatusForUpdateProduction'; - //return -1; - return 1; - } + if ($this->status != self::STATUS_DRAFT) return 1; $this->db->begin(); - // Insert lines in mrp_production table from BOM data - if (!$error) { - // TODO Check that production has not started. If yes, we stop here. + $oldQty = $this->oldQty; + $newQty = $this->qty; + if($newQty != $oldQty) { + $sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'mrp_production WHERE fk_mo = ' . $this->id; + $resql = $this->db->query($sql); + if($resql) { + while ($obj = $this->db->fetch_object($resql)) { + $moLine = new MoLine($this->db); + $res = $moLine->fetch($obj->rowid); + if(!$res) $error++; - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'mrp_production WHERE fk_mo = '.((int) $this->id); - $this->db->query($sql); - - $moline = new MoLine($this->db); - - // Line to produce - $moline->fk_mo = $this->id; - $moline->qty = $this->qty; - $moline->fk_product = $this->fk_product; - $moline->position = 1; - - if ($this->fk_bom > 0) { // If a BOM is defined, we know what to produce. - include_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php'; - $bom = new Bom($this->db); - $bom->fetch($this->fk_bom); - if ($bom->bomtype == 1) { - $role = 'toproduce'; - $moline->role = 'toconsume'; - } else { - $role = 'toconsume'; - $moline->role = 'toproduce'; - } - } else { - if ($this->mrptype == 1) { - $moline->role = 'toconsume'; - } else { - $moline->role = 'toproduce'; - } - } - - $resultline = $moline->create($user, false); // Never use triggers here - if ($resultline <= 0) { - $error++; - $this->error = $moline->error; - $this->errors = $moline->errors; - dol_print_error($this->db, $moline->error, $moline->errors); - } - - if ($this->fk_bom > 0) { // If a BOM is defined, we know what to consume. - if ($bom->id > 0) { - // Lines to consume - if (!$error) { - foreach ($bom->lines as $line) { - $moline = new MoLine($this->db); - - $moline->fk_mo = $this->id; - $moline->origin_id = $line->id; - $moline->origin_type = 'bomline'; - if ($line->qty_frozen) { - $moline->qty = $line->qty; // Qty to consume does not depends on quantity to produce - } else { - $moline->qty = price2num(($line->qty / ( !empty($bom->qty) ? $bom->qty : 1 ) ) * $this->qty / ( !empty($line->efficiency) ? $line->efficiency : 1 ), 'MS'); // Calculate with Qty to produce and more presition - } - if ($moline->qty <= 0) { - $error++; - $this->error = "BadValueForquantityToConsume"; - break; - } else { - $moline->fk_product = $line->fk_product; - $moline->role = $role; - $moline->position = $line->position; - $moline->qty_frozen = $line->qty_frozen; - $moline->disable_stock_change = $line->disable_stock_change; - - $resultline = $moline->create($user, false); // Never use triggers here - if ($resultline <= 0) { - $error++; - $this->error = $moline->error; - $this->errors = $moline->errors; - dol_print_error($this->db, $moline->error, $moline->errors); - break; - } - } + if($moLine->role == 'toconsume' || $moLine->role == 'toproduce') { + if (empty($moLine->qty_frozen)) { + $qty = $newQty * $moLine->qty / $oldQty; + $moLine->qty = price2num($qty * (!empty($line->efficiency) ? $line->efficiency : 1 ), 'MS'); // Calculate with Qty to produce and more presition + $res = $moLine->update($user); + if(!$res) $error++; } } } diff --git a/htdocs/mrp/mo_card.php b/htdocs/mrp/mo_card.php index b3a4b3aced8..8a460056a13 100644 --- a/htdocs/mrp/mo_card.php +++ b/htdocs/mrp/mo_card.php @@ -123,6 +123,8 @@ if (empty($reshook)) { $backurlforlist = dol_buildpath('/mrp/mo_list.php', 1); + $object->oldQty = $object->qty; + if (empty($backtopage) || ($cancel && empty($id))) { if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) { if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) { From 700efad2355a6a2ecabc5a1d710c3477a165a8a9 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Mon, 3 Apr 2023 15:00:40 +0000 Subject: [PATCH 3/5] Fixing style errors. --- htdocs/mrp/class/mo.class.php | 10 +++++----- htdocs/mrp/mo_card.php | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index 7745d05d71a..8e0b94ccb5b 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -638,21 +638,21 @@ class Mo extends CommonObject $oldQty = $this->oldQty; $newQty = $this->qty; - if($newQty != $oldQty) { + if ($newQty != $oldQty) { $sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'mrp_production WHERE fk_mo = ' . $this->id; $resql = $this->db->query($sql); - if($resql) { + if ($resql) { while ($obj = $this->db->fetch_object($resql)) { $moLine = new MoLine($this->db); $res = $moLine->fetch($obj->rowid); - if(!$res) $error++; + if (!$res) $error++; - if($moLine->role == 'toconsume' || $moLine->role == 'toproduce') { + if ($moLine->role == 'toconsume' || $moLine->role == 'toproduce') { if (empty($moLine->qty_frozen)) { $qty = $newQty * $moLine->qty / $oldQty; $moLine->qty = price2num($qty * (!empty($line->efficiency) ? $line->efficiency : 1 ), 'MS'); // Calculate with Qty to produce and more presition $res = $moLine->update($user); - if(!$res) $error++; + if (!$res) $error++; } } } diff --git a/htdocs/mrp/mo_card.php b/htdocs/mrp/mo_card.php index 8a460056a13..974e6eae4df 100644 --- a/htdocs/mrp/mo_card.php +++ b/htdocs/mrp/mo_card.php @@ -123,7 +123,7 @@ if (empty($reshook)) { $backurlforlist = dol_buildpath('/mrp/mo_list.php', 1); - $object->oldQty = $object->qty; + $object->oldQty = $object->qty; if (empty($backtopage) || ($cancel && empty($id))) { if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) { From 2c90f7335ea26149688d21977372e76f87b52649 Mon Sep 17 00:00:00 2001 From: Adrien Raze Date: Wed, 5 Apr 2023 11:42:50 +0200 Subject: [PATCH 4/5] FIX : Travis --- htdocs/mrp/class/mo.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index 7745d05d71a..97075ddd440 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -638,8 +638,8 @@ class Mo extends CommonObject $oldQty = $this->oldQty; $newQty = $this->qty; - if($newQty != $oldQty) { - $sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'mrp_production WHERE fk_mo = ' . $this->id; + if($newQty != $oldQty && !empty($this->oldQty)) { + $sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'mrp_production WHERE fk_mo = ' . (int) $this->id; $resql = $this->db->query($sql); if($resql) { while ($obj = $this->db->fetch_object($resql)) { From 14560c1de818f45c86f82baac72aff2eb5931ba2 Mon Sep 17 00:00:00 2001 From: Adrien Raze Date: Wed, 5 Apr 2023 15:37:35 +0200 Subject: [PATCH 5/5] FIX : Travis --- 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 5d971c0e440..ecb82507738 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -639,7 +639,7 @@ class Mo extends CommonObject $oldQty = $this->oldQty; $newQty = $this->qty; if ($newQty != $oldQty && !empty($this->oldQty)) { - $sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'mrp_production WHERE fk_mo = ' . (int) $this->id; + $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "mrp_production WHERE fk_mo = " . (int) $this->id; $resql = $this->db->query($sql); if ($resql) { while ($obj = $this->db->fetch_object($resql)) {