diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index 2341f1fd406c9..090bdc067c850 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -891,78 +891,58 @@ 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')); + $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(); - - $stockmove = new MouvementStock($this->db); - $stockmove->setOrigin($this->element, $this->id); + $moline = new MoLine($this->db); + $moline->fetch($idline); - 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; + $this->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($this->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 +2202,63 @@ 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 $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($this->db); + $tmpmo->fetch($this->fk_mo); + + // load the (old) linked stockmovement and the associated product + $linkedMovement = new MouvementStock($this->db); + $linkedMovement->fetch($this->fk_stock_movement); + $productForLinkedMovement = new Product($this->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 '