From 161552e6fbe3f1605410b596d3e3adf73ae0c848 Mon Sep 17 00:00:00 2001 From: "christian.humpel" Date: Thu, 29 Feb 2024 22:27:19 +0100 Subject: [PATCH 1/3] fix #28533 --- htdocs/mrp/class/mo.class.php | 155 +++++++++++++++++++++------------- htdocs/mrp/mo_production.php | 2 +- 2 files changed, 98 insertions(+), 59 deletions(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index 2341f1fd406c9..e705d1f01c9df 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -891,78 +891,60 @@ public function delete(User $user, $notrigger = false, $also_cancel_consumed_and */ public function deleteLine(User $user, $idline, $notrigger = false) { - global $langs; - $langs->loadLangs(array('stocks', 'mrp')); + global $db; + + $error = 0; if ($this->status < 0) { $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus'; return -2; } - $productstatic = new Product($this->db); - $fk_movement = GETPOST('fk_movement', 'int'); - $arrayoflines = $this->fetchLinesLinked('consumed', $idline); - if (!empty($arrayoflines)) { - $this->db->begin(); + $moline = new MoLine($db); + $moline->fetch($idline); - $stockmove = new MouvementStock($this->db); - $stockmove->setOrigin($this->element, $this->id); - - if (!empty($fk_movement)) { - $moline = new MoLine($this->db); - $TArrayMoLine = $moline->fetchAll('', '', 1, 0, array('customsql' => 'fk_stock_movement ='.$fk_movement)); - $moline = array_shift($TArrayMoLine); + $affectedLinkedMoLines; + // Check if it's linked or a main line + if (empty($moline->fk_mrp_production)) { + // Check if the main line has linked lines + $affectedLinkedMoLines = $this->fetchLinesLinked('consumed', $moline->id); + } - $movement = new MouvementStock($this->db); - $movement->fetch($fk_movement); - $productstatic->fetch($movement->product_id); - $qtytoprocess = $movement->qty; + $db->begin(); - // Reverse stock movement - $labelmovementCancel = $langs->trans("CancelProductionForRef", $productstatic->ref); - $codemovementCancel = $langs->trans("StockIncrease"); + // undo stockmovements and remove linked lines + if (!empty($affectedLinkedMoLines)) { + foreach ($affectedLinkedMoLines as $linkedLine) { + if(!$error) { + $linkedMoline = new MoLine($db); + $linkedMoline->fetch($linkedLine['rowid']); + $result = $linkedMoline->delete($user, $notrigger); - if (($qtytoprocess >= 0)) { - $idstockmove = $stockmove->reception($user, $movement->product_id, $movement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel, '', '', $movement->batch, dol_now(), 0, $codemovementCancel); - } else { - $idstockmove = $stockmove->livraison($user, $movement->product_id, $movement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel, dol_now(), '', '', $movement->batch, 0, $codemovementCancel); - } - if ($idstockmove < 0) { - $this->error++; - $this->db->rollback(); - setEventMessages($stockmove->error, $stockmove->errors, 'errors'); - } else { - $this->db->commit(); + if ($result < 0) { + $error++; + setEventMessages($linkedMoline->error, $linkedMoline->errors, 'errors'); + } } - return $moline->delete($user, $notrigger); - } else { - foreach ($arrayoflines as $key => $arrayofline) { - $lineDetails = $arrayoflines[$key]; - $productstatic->fetch($lineDetails['fk_product']); - $qtytoprocess = $lineDetails['qty']; + } + } - // Reverse stock movement - $labelmovementCancel = $langs->trans("CancelProductionForRef", $productstatic->ref); - $codemovementCancel = $langs->trans("StockIncrease"); + if (!$error) { + $result = $moline->delete($user, $notrigger); - if ($qtytoprocess >= 0) { - $idstockmove = $stockmove->reception($user, $lineDetails['fk_product'], $lineDetails['fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel, '', '', $lineDetails['batch'], dol_now(), 0, $codemovementCancel); - } else { - $idstockmove = $stockmove->livraison($user, $lineDetails['fk_product'], $lineDetails['fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel, dol_now(), '', '', $lineDetails['batch'], 0, $codemovementCancel); - } - if ($idstockmove < 0) { - $this->error++; - $this->db->rollback(); - setEventMessages($stockmove->error, $stockmove->errors, 'errors'); - } else { - $this->db->commit(); - } - } - return $this->deleteLineCommon($user, $idline, $notrigger); + if ($result < 0) { + $error++; + setEventMessages($moline->error, $moline->errors, 'errors'); } + } + + if (!$error) { + $this->db->commit(); + $result = 1; } else { - return $this->deleteLineCommon($user, $idline, $notrigger); + $this->db->rollback(); + $result = -1; } + return $result; } @@ -2222,7 +2204,64 @@ public function update(User $user, $notrigger = false) */ public function delete(User $user, $notrigger = false) { - return $this->deleteCommon($user, $notrigger); - //return $this->deleteCommon($user, $notrigger, 1); + global $db, $langs; + + $error = 0; + $result = -1; + + // the stockmovements it was created for this line revise + if (!empty($this->fk_warehouse)) + { + $langs->loadLangs(array('stocks', 'mrp')); + + // load the mo for this line + $tmpmo = new Mo($db); + $tmpmo->fetch($this->fk_mo); + + // load the (old) linked stockmovement and the associated product + $linkedMovement = new MouvementStock($db); + $linkedMovement->fetch($this->fk_stock_movement); + $productForLinkedMovement = new Product($db); + $productForLinkedMovement->fetch($linkedMovement->product_id); + + // create new stockmovement to revise the linked stockmovement + $stockmove = new MouvementStock($this->db); + $stockmove->setOrigin($tmpmo->element, $tmpmo->id); + + // Reverse stock movement + $labelmovementCancel = $langs->trans("CancelProductionForRef", $productForLinkedMovement->ref); + $codemovementCancel = dol_print_date(dol_now(), 'dayhourlog'); + + $idstockmove = -1; + if (($linkedMovement->qty < 0)) { + $qtytoprocess = $linkedMovement->qty * -1; + $idstockmove = $stockmove->reception($user, $linkedMovement->product_id, $linkedMovement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel, '', '', $linkedMovement->batch, dol_now(), 0, $codemovementCancel); + } else { + $idstockmove = $stockmove->livraison($user, $linkedMovement->product_id, $linkedMovement->warehouse_id, $linkedMovement->qty, 0, $labelmovementCancel, dol_now(), '', '', $linkedMovement->batch, 0, $codemovementCancel); + } + + if ($idstockmove < 0) { + $this->error++; + setEventMessages($stockmove->error, $stockmove->errors, 'errors'); + } + } + + if (!$error) { + $result = $this->deleteCommon($user, $notrigger); + if ($result < 0) { + $this->error++; + setEventMessages($stockmove->error, $stockmove->errors, 'errors'); + } + } + + if (!$error) { + $this->db->commit(); + $result = 1; + } else { + $this->db->rollback(); + $result = -1; + } + + return $result; } } diff --git a/htdocs/mrp/mo_production.php b/htdocs/mrp/mo_production.php index 0caba7cb77739..1dafabc87a77c 100644 --- a/htdocs/mrp/mo_production.php +++ b/htdocs/mrp/mo_production.php @@ -1288,7 +1288,7 @@ // Action delete line if ($permissiontodelete) { - $href = $_SERVER["PHP_SELF"].'?id='.((int) $object->id).'&action=deleteline&token='.newToken().'&lineid='.((int) $line->id).'&fk_movement='.((int) $line2['fk_stock_movement']); + $href = $_SERVER["PHP_SELF"].'?id='.((int) $object->id).'&action=deleteline&token='.newToken().'&lineid='.((int) $line2['rowid']); print ''; print ''; print img_picto($langs->trans('TooltipDeleteAndRevertStockMovement'), 'delete'); From 5dbb11c59b0819afd2ff7f55fbf3c53093169877 Mon Sep 17 00:00:00 2001 From: "christian.humpel" Date: Thu, 29 Feb 2024 22:37:29 +0100 Subject: [PATCH 2/3] qual for github actions --- htdocs/mrp/class/mo.class.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index e705d1f01c9df..a494f1f79ca6a 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -915,7 +915,7 @@ public function deleteLine(User $user, $idline, $notrigger = false) // undo stockmovements and remove linked lines if (!empty($affectedLinkedMoLines)) { foreach ($affectedLinkedMoLines as $linkedLine) { - if(!$error) { + if (!$error) { $linkedMoline = new MoLine($db); $linkedMoline->fetch($linkedLine['rowid']); $result = $linkedMoline->delete($user, $notrigger); @@ -2210,8 +2210,7 @@ public function delete(User $user, $notrigger = false) $result = -1; // the stockmovements it was created for this line revise - if (!empty($this->fk_warehouse)) - { + if (!empty($this->fk_warehouse)) { $langs->loadLangs(array('stocks', 'mrp')); // load the mo for this line From 930d596ff6afd53a689ddfb895144f956ff26cc3 Mon Sep 17 00:00:00 2001 From: "christian.humpel" Date: Fri, 1 Mar 2024 12:30:48 +0100 Subject: [PATCH 3/3] fix use $this->db in classes --- htdocs/mrp/class/mo.class.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index a494f1f79ca6a..090bdc067c850 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -891,8 +891,6 @@ public function delete(User $user, $notrigger = false, $also_cancel_consumed_and */ public function deleteLine(User $user, $idline, $notrigger = false) { - global $db; - $error = 0; if ($this->status < 0) { @@ -900,7 +898,7 @@ public function deleteLine(User $user, $idline, $notrigger = false) return -2; } - $moline = new MoLine($db); + $moline = new MoLine($this->db); $moline->fetch($idline); $affectedLinkedMoLines; @@ -910,13 +908,13 @@ public function deleteLine(User $user, $idline, $notrigger = false) $affectedLinkedMoLines = $this->fetchLinesLinked('consumed', $moline->id); } - $db->begin(); + $this->db->begin(); // undo stockmovements and remove linked lines if (!empty($affectedLinkedMoLines)) { foreach ($affectedLinkedMoLines as $linkedLine) { if (!$error) { - $linkedMoline = new MoLine($db); + $linkedMoline = new MoLine($this->db); $linkedMoline->fetch($linkedLine['rowid']); $result = $linkedMoline->delete($user, $notrigger); @@ -2204,7 +2202,7 @@ public function update(User $user, $notrigger = false) */ public function delete(User $user, $notrigger = false) { - global $db, $langs; + global $langs; $error = 0; $result = -1; @@ -2214,13 +2212,13 @@ public function delete(User $user, $notrigger = false) $langs->loadLangs(array('stocks', 'mrp')); // load the mo for this line - $tmpmo = new Mo($db); + $tmpmo = new Mo($this->db); $tmpmo->fetch($this->fk_mo); // load the (old) linked stockmovement and the associated product - $linkedMovement = new MouvementStock($db); + $linkedMovement = new MouvementStock($this->db); $linkedMovement->fetch($this->fk_stock_movement); - $productForLinkedMovement = new Product($db); + $productForLinkedMovement = new Product($this->db); $productForLinkedMovement->fetch($linkedMovement->product_id); // create new stockmovement to revise the linked stockmovement