diff --git a/htdocs/langs/en_US/mrp.lang b/htdocs/langs/en_US/mrp.lang index 4dc74122ea9..224273d84e6 100644 --- a/htdocs/langs/en_US/mrp.lang +++ b/htdocs/langs/en_US/mrp.lang @@ -69,6 +69,8 @@ ForAQuantityToConsumeOf=For a quantity to disassemble of %s ConfirmValidateMo=Are you sure you want to validate this Manufacturing Order? ConfirmProductionDesc=By clicking on '%s', you will validate the consumption and/or production for the quantities set. This will also update the stock and record stock movements. ProductionForRef=Production of %s +CancelProductionForRef=Cancellation of product stock decrementation for product %s +TooltipDeleteAndRevertStockMovement=Delete line and revert stock movement AutoCloseMO=Close automatically the Manufacturing Order if quantities to consume and to produce are reached NoStockChangeOnServices=No stock change on services ProductQtyToConsumeByMO=Product quantity still to consume by open MO diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index de9913fdb9f..2c0773dd9be 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -750,12 +750,79 @@ class Mo extends CommonObject */ public function deleteLine(User $user, $idline, $notrigger = false) { + global $langs; + $langs->load('stocks'); + if ($this->status < 0) { $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus'; return -2; } - return $this->deleteLineCommon($user, $idline, $notrigger); + $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); + + 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); + + $movement = new MouvementStock($this->db); + $movement->fetch($fk_movement); + $productstatic->fetch($movement->product_id); + $qtytoprocess = $movement->qty; + + // Reverse stock movement + $labelmovementCancel = $langs->trans("CancelProductionForRef", $productstatic->ref); + $codemovementCancel = $langs->trans("StockIncrease"); + + 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(); + } + 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 ($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); + } + } else { + return $this->deleteLineCommon($user, $idline, $notrigger); + } } diff --git a/htdocs/mrp/mo_production.php b/htdocs/mrp/mo_production.php index 545a7aaf202..ad33a43b17f 100644 --- a/htdocs/mrp/mo_production.php +++ b/htdocs/mrp/mo_production.php @@ -49,6 +49,7 @@ $cancel = GETPOST('cancel', 'aZ09'); $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'mocard'; // To manage different context of search $backtopage = GETPOST('backtopage', 'alpha'); $lineid = GETPOST('lineid', 'int'); +$fk_movement = GETPOST('fk_movement', 'int'); $collapse = GETPOST('collapse', 'aZ09comma'); @@ -445,7 +446,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea } // Confirmation to delete line if ($action == 'deleteline') { - $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_deleteline', '', 0, 1); + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid.'&fk_movement='.$fk_movement, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_deleteline', '', 0, 1); } // Clone confirmation if ($action == 'clone') { @@ -914,7 +915,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea $href = $_SERVER["PHP_SELF"].'?id='.((int) $object->id).'&action=deleteline&token='.newToken().'&lineid='.((int) $line->id); print ''; print ''; - print img_picto('', 'delete'); + print img_picto($langs->trans('TooltipDeleteAndRevertStockMovement'), 'delete'); print ''; print ''; } @@ -969,7 +970,12 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // Action delete line if ($permissiontodelete) { - print ''; + $href = $_SERVER["PHP_SELF"].'?id='.((int) $object->id).'&action=deleteline&token='.newToken().'&lineid='.((int) $line->id).'&fk_movement='.((int) $line2['fk_stock_movement']); + print ''; + print ''; + print img_picto($langs->trans('TooltipDeleteAndRevertStockMovement'), 'delete'); + print ''; + print ''; } print ''; @@ -1220,7 +1226,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea $href .= '&lineid='.$line->id; print ''; print ''; - print img_picto('', "delete"); + print img_picto($langs->trans('TooltipDeleteAndRevertStockMovement'), "delete"); print ''; print ''; }