diff --git a/htdocs/langs/en_US/mrp.lang b/htdocs/langs/en_US/mrp.lang
index 92105e3b63e..1ee3cf313d2 100644
--- a/htdocs/langs/en_US/mrp.lang
+++ b/htdocs/langs/en_US/mrp.lang
@@ -31,6 +31,9 @@ Consumption=Consumption
ValueOfMeansLoss=Value of 0.95 means an average of 5%% of loss during the manufacturing or the disassembly
ValueOfMeansLossForProductProduced=Value of 0.95 means an average of 5%% of loss of produced product
DeleteBillOfMaterials=Delete Bill Of Materials
+CancelMo=Cancel Manufacturing Order
+MoCancelConsumedAndProducedLines=Cancel also all the consumed and produced lines (delete lines and rollback stocks)
+ConfirmCancelMo=Are you sure you want to cancel this Manufacturing Order?
DeleteMo=Delete Manufacturing Order
ConfirmDeleteBillOfMaterials=Are you sure you want to delete this Bill Of Materials?
ConfirmDeleteMo=Are you sure you want to delete this Manufacturing Order?
diff --git a/htdocs/langs/fr_FR/mrp.lang b/htdocs/langs/fr_FR/mrp.lang
index 04e1da9ea29..3d06398cbab 100644
--- a/htdocs/langs/fr_FR/mrp.lang
+++ b/htdocs/langs/fr_FR/mrp.lang
@@ -31,6 +31,9 @@ Consumption=Consommation
ValueOfMeansLoss=Une valeur de 0,95 signifie une moyenne de 5%% de perte lors de la fabrication ou du désassemblage
ValueOfMeansLossForProductProduced=Une valeur de 0,95 signifie une moyenne de 5%% de perte de produit fabriqué
DeleteBillOfMaterials=Supprimer la nomenclature
+CancelMo=Annuler l'ordre de fabrication
+MoCancelConsumedAndProducedLines=Annule aussi les lignes de consommation et de production (supprime les lignes et rétablie le stock)
+ConfirmCancelMo=Êtes-vous sûr de vouloir annuler cet ordre de fabrication ?
DeleteMo=Supprimer l'ordre de fabrication
ConfirmDeleteBillOfMaterials=Êtes-vous sûr de vouloir supprimer cette nomenclature?
ConfirmDeleteMo=Êtes-vous sûr de vouloir supprimer cet ordre de fabrication ?
@@ -70,7 +73,7 @@ ForAQuantityToConsumeOf=Pour une quantité à désassembler de %s
ConfirmValidateMo=Voulez-vous vraiment valider cet ordre de fabrication?
ConfirmProductionDesc=En cliquant sur '%s', vous validerez la consommation et / ou la production pour les quantités définies. Cela mettra également à jour le stock et enregistrera les mouvements de stock.
ProductionForRef=Production de %s
-CancelProductionForRef=Annulation de la sortie de stock pour le produit
+CancelProductionForRef=Annulation de la sortie de stock pour le produit %s
TooltipDeleteAndRevertStockMovement=Supprimer la ligne et modifier le stock
AutoCloseMO=Fermer automatiquement l'Ordre de Fabrication si les quantités à consommer et à produire sont atteintes
NoStockChangeOnServices=Aucune variation de stock sur les services
diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php
index c7dc94e27fd..9502e51edaf 100644
--- a/htdocs/mrp/class/mo.class.php
+++ b/htdocs/mrp/class/mo.class.php
@@ -826,14 +826,37 @@ class Mo extends CommonObject
/**
* Delete object in database
*
- * @param User $user User that deletes
- * @param bool $notrigger false=launch triggers after, true=disable triggers
- * @return int <0 if KO, >0 if OK
+ * @param User $user User that deletes
+ * @param bool $notrigger false=launch triggers after, true=disable triggers
+ * @param bool $also_cancel_consumed_and_produced_lines true if the consumed and produced lines will be deleted (and stocks incremented/decremented back) (false by default)
+ * @return int <0 if KO, >0 if OK
*/
- public function delete(User $user, $notrigger = false)
+ public function delete(User $user, $notrigger = false, $also_cancel_consumed_and_produced_lines = false)
{
- return $this->deleteCommon($user, $notrigger);
- //return $this->deleteCommon($user, $notrigger, 1);
+ $error = 0;
+ $this->db->begin();
+
+ if ($also_cancel_consumed_and_produced_lines) {
+ $result = $this->cancelConsumedAndProducedLines($user, 0, false, $notrigger);
+ if ($result < 0) {
+ $error++;
+ }
+ }
+
+ if (!$error) {
+ $result = $this->deleteCommon($user, $notrigger);
+ if ($result < 0) {
+ $error++;
+ }
+ }
+
+ if ($error) {
+ $this->db->rollback();
+ return -1;
+ } else {
+ $this->db->commit();
+ return 1;
+ }
}
/**
@@ -1116,11 +1139,12 @@ class Mo extends CommonObject
/**
* Set cancel status
*
- * @param User $user Object user that modify
- * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
- * @return int <0 if KO, 0=Nothing done, >0 if OK
+ * @param User $user Object user that modify
+ * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
+ * @param bool $also_cancel_consumed_and_produced_lines true if the consumed and produced lines will be deleted (and stocks incremented/decremented back) (false by default)
+ * @return int <0 if KO, 0=Nothing done, >0 if OK
*/
- public function cancel($user, $notrigger = 0)
+ public function cancel($user, $notrigger = 0, $also_cancel_consumed_and_produced_lines = false)
{
// Protection
if ($this->status != self::STATUS_VALIDATED && $this->status != self::STATUS_INPROGRESS) {
@@ -1134,7 +1158,30 @@ class Mo extends CommonObject
return -1;
}*/
- return $this->setStatusCommon($user, self::STATUS_CANCELED, $notrigger, 'MRP_MO_CANCEL');
+ $error = 0;
+ $this->db->begin();
+
+ if ($also_cancel_consumed_and_produced_lines) {
+ $result = $this->cancelConsumedAndProducedLines($user, 0, true, $notrigger);
+ if ($result < 0) {
+ $error++;
+ }
+ }
+
+ if (!$error) {
+ $result = $this->setStatusCommon($user, self::STATUS_CANCELED, $notrigger, 'MRP_MO_CANCEL');
+ if ($result < 0) {
+ $error++;
+ }
+ }
+
+ if ($error) {
+ $this->db->rollback();
+ return -1;
+ } else {
+ $this->db->commit();
+ return 1;
+ }
}
/**
@@ -1161,6 +1208,115 @@ class Mo extends CommonObject
return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'MRP_MO_REOPEN');
}
+ /**
+ * Cancel consumed and produced lines (movement stocks)
+ *
+ * @param User $user Object user that modify
+ * @param bool $mode Type line supported (0 by default) (0: consumed and produced lines; 1: consumed lines; 2: produced lines)
+ * @param bool $also_delete_lines true if the consumed/produced lines is deleted (false by default)
+ * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
+ * @return int <0 if KO, 0=Nothing done, >0 if OK
+ */
+ public function cancelConsumedAndProducedLines($user, $mode = 0, $also_delete_lines = false, $notrigger = 0)
+ {
+ global $langs;
+
+ if (!isModEnabled('stock')) {
+ return 1;
+ }
+
+ require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
+ require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
+ $error = 0;
+ $langs->load('stocks');
+
+ $this->db->begin();
+
+ // Cancel consumed lines
+ if (empty($mode) || $mode == 1) {
+ $arrayoflines = $this->fetchLinesLinked('consumed');
+ if (!empty($arrayoflines)) {
+ foreach ($arrayoflines as $key => $lineDetails) {
+ $productstatic = new Product($this->db);
+ $productstatic->fetch($lineDetails['fk_product']);
+ $qtytoprocess = $lineDetails['qty'];
+
+ // Reverse stock movement
+ $labelmovementCancel = $langs->trans("CancelProductionForRef", $productstatic->ref);
+ $codemovementCancel = $langs->trans("StockIncrease");
+
+ $stockmove = new MouvementStock($this->db);
+ $stockmove->setOrigin($this->element, $this->id);
+ 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 = $stockmove->error;
+ $this->errors = $stockmove->errors;
+ $error++;
+ break;
+ }
+
+ if ($also_delete_lines) {
+ $result = $this->deleteLineCommon($user, $lineDetails['rowid'], $notrigger);
+ if ($result < 0) {
+ $error++;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ // Cancel produced lines
+ if (empty($mode) || $mode == 2) {
+ $arrayoflines = $this->fetchLinesLinked('produced');
+ if (!empty($arrayoflines)) {
+ foreach ($arrayoflines as $key => $lineDetails) {
+ $productstatic = new Product($this->db);
+ $productstatic->fetch($lineDetails['fk_product']);
+ $qtytoprocess = $lineDetails['qty'];
+
+ // Reverse stock movement
+ $labelmovementCancel = $langs->trans("CancelProductionForRef", $productstatic->ref);
+ $codemovementCancel = $langs->trans("StockDecrease");
+
+ $stockmove = new MouvementStock($this->db);
+ $stockmove->setOrigin($this->element, $this->id);
+ if ($qtytoprocess >= 0) {
+ $idstockmove = $stockmove->livraison($user, $lineDetails['fk_product'], $lineDetails['fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel, dol_now(), '', '', $lineDetails['batch'], 0, $codemovementCancel);
+ } else {
+ $idstockmove = $stockmove->reception($user, $lineDetails['fk_product'], $lineDetails['fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel, '', '', $lineDetails['batch'], dol_now(), 0, $codemovementCancel);
+ }
+ if ($idstockmove < 0) {
+ $this->error = $stockmove->error;
+ $this->errors = $stockmove->errors;
+ $error++;
+ break;
+ }
+
+ if ($also_delete_lines) {
+ $result = $this->deleteLineCommon($user, $lineDetails['rowid'], $notrigger);
+ if ($result < 0) {
+ $error++;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ if ($error) {
+ $this->db->rollback();
+ return -1;
+ } else {
+ $this->db->commit();
+ return 1;
+ }
+ }
+
/**
* getTooltipContentArray
*
diff --git a/htdocs/mrp/mo_card.php b/htdocs/mrp/mo_card.php
index 8a2ebee5e63..4b0c6b65c1a 100644
--- a/htdocs/mrp/mo_card.php
+++ b/htdocs/mrp/mo_card.php
@@ -174,6 +174,26 @@ if (empty($reshook)) {
header("Location: ".dol_buildpath('/mrp/mo_card.php?id='.((int) $moline->fk_mo), 1));
exit;
+ } elseif ($action == 'confirm_cancel' && $confirm == 'yes' && !empty($permissiontoadd)) {
+ $also_cancel_consumed_and_produced_lines = (GETPOST('alsoCancelConsumedAndProducedLines', 'alpha') ? 1 : 0);
+ $result = $object->cancel($user, 0, $also_cancel_consumed_and_produced_lines);
+ if ($result > 0) {
+ header("Location: " . dol_buildpath('/mrp/mo_card.php?id=' . $object->id, 1));
+ exit;
+ } else {
+ $action = '';
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ } elseif ($action == 'confirm_delete' && $confirm == 'yes' && !empty($permissiontodelete)) {
+ $also_cancel_consumed_and_produced_lines = (GETPOST('alsoCancelConsumedAndProducedLines', 'alpha') ? 1 : 0);
+ $result = $object->delete($user, 0, $also_cancel_consumed_and_produced_lines);
+ if ($result > 0) {
+ header("Location: " . $backurlforlist);
+ exit;
+ } else {
+ $action = '';
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
}
// Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
@@ -414,7 +434,15 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
// Confirmation to delete
if ($action == 'delete') {
- $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteMo'), $langs->trans('ConfirmDeleteMo'), 'confirm_delete', '', 0, 1);
+ $formquestion = array(
+ array(
+ 'label' => $langs->trans('MoCancelConsumedAndProducedLines'),
+ 'name' => 'alsoCancelConsumedAndProducedLines',
+ 'type' => 'checkbox',
+ 'value' => empty($conf->global->MO_ALSO_CANCEL_CONSUMED_AND_PRODUCED_LINES_BY_DEFAULT) ? 0 : 1
+ ),
+ );
+ $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteMo'), $langs->trans('ConfirmDeleteMo'), 'confirm_delete', $formquestion, 0, 1);
}
// Confirmation to delete line
if ($action == 'deleteline') {
@@ -460,6 +488,19 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('Validate'), $text, 'confirm_validate', $formquestion, 0, 1, 220);
}
+ // Confirmation to cancel
+ if ($action == 'cancel') {
+ $formquestion = array(
+ array(
+ 'label' => $langs->trans('MoCancelConsumedAndProducedLines'),
+ 'name' => 'alsoCancelConsumedAndProducedLines',
+ 'type' => 'checkbox',
+ 'value' => 0
+ ),
+ );
+ $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('CancelMo'), $langs->trans('ConfirmCancelMo'), 'confirm_cancel', $formquestion, 0, 1);
+ }
+
// Clone confirmation
if ($action == 'clone') {
// Create an array for form
@@ -699,7 +740,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print 'transnoentitiesnoconv("Production")).'">'.$langs->trans("Close").''."\n";
}
- print 'id.'&action=confirm_close&confirm=yes&token='.newToken().'">'.$langs->trans("Cancel").''."\n";
+ print 'id.'&action=cancel&token='.newToken().'">'.$langs->trans("Cancel").''."\n";
}
if ($object->status == $object::STATUS_PRODUCED || $object->status == $object::STATUS_CANCELED) {
diff --git a/htdocs/mrp/mo_production.php b/htdocs/mrp/mo_production.php
index c01866efca7..07e87f1004b 100644
--- a/htdocs/mrp/mo_production.php
+++ b/htdocs/mrp/mo_production.php
@@ -128,6 +128,28 @@ if (empty($reshook)) {
}
$triggermodname = 'MO_MODIFY'; // Name of trigger action code to execute when we modify record
+ if ($action == 'confirm_cancel' && $confirm == 'yes' && !empty($permissiontoadd)) {
+ $also_cancel_consumed_and_produced_lines = (GETPOST('alsoCancelConsumedAndProducedLines', 'alpha') ? 1 : 0);
+ $result = $object->cancel($user, 0, $also_cancel_consumed_and_produced_lines);
+ if ($result > 0) {
+ header("Location: " . dol_buildpath('/mrp/mo_card.php?id=' . $object->id, 1));
+ exit;
+ } else {
+ $action = '';
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ } elseif ($action == 'confirm_delete' && $confirm == 'yes' && !empty($permissiontodelete)) {
+ $also_cancel_consumed_and_produced_lines = (GETPOST('alsoCancelConsumedAndProducedLines', 'alpha') ? 1 : 0);
+ $result = $object->delete($user, 0, $also_cancel_consumed_and_produced_lines);
+ if ($result > 0) {
+ header("Location: " . $backurlforlist);
+ exit;
+ } else {
+ $action = '';
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+
// Actions cancel, add, update, delete or clone
include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
@@ -473,7 +495,15 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
// Confirmation to delete
if ($action == 'delete') {
- $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteMo'), $langs->trans('ConfirmDeleteMo'), 'confirm_delete', '', 0, 1);
+ $formquestion = array(
+ array(
+ 'label' => $langs->trans('MoCancelConsumedAndProducedLines'),
+ 'name' => 'alsoCancelConsumedAndProducedLines',
+ 'type' => 'checkbox',
+ 'value' => 0
+ ),
+ );
+ $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteMo'), $langs->trans('ConfirmDeleteMo'), 'confirm_delete', $formquestion, 0, 1);
}
// Confirmation to delete line
if ($action == 'deleteline') {
@@ -525,6 +555,19 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('Validate'), $text, 'confirm_validate', $formquestion, 0, 1, 220);
}
+ // Confirmation to cancel
+ if ($action == 'cancel') {
+ $formquestion = array(
+ array(
+ 'label' => $langs->trans('MoCancelConsumedAndProducedLines'),
+ 'name' => 'alsoCancelConsumedAndProducedLines',
+ 'type' => 'checkbox',
+ 'value' => empty($conf->global->MO_ALSO_CANCEL_CONSUMED_AND_PRODUCED_LINES_BY_DEFAULT) ? 0 : 1
+ ),
+ );
+ $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('CancelMo'), $langs->trans('ConfirmCancelMo'), 'confirm_cancel', $formquestion, 0, 1);
+ }
+
// Call Hook formConfirm
$parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
$reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
@@ -665,11 +708,11 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print 'transnoentitiesnoconv("Production")).'">'.$langs->trans("Close").''."\n";
}
- print 'id.'&action=confirm_close&confirm=yes&token='.$newToken.'">'.$langs->trans("Cancel").''."\n";
+ print 'id.'&action=cancel&token='.$newToken.'">'.$langs->trans("Cancel").''."\n";
}
if ($object->status == $object::STATUS_CANCELED) {
- print 'id.'&action=confirm_reopen&confirm=yes&token='.$newToken.'">'.$langs->trans("Re-Open").''."\n";
+ print 'id.'&action=confirm_reopen&confirm=yes&token='.$newToken.'">'.$langs->trans("ReOpen").''."\n";
}
if ($object->status == $object::STATUS_PRODUCED) {