diff --git a/htdocs/langs/en_US/mrp.lang b/htdocs/langs/en_US/mrp.lang index 1ee3cf313d2..935bcd93996 100644 --- a/htdocs/langs/en_US/mrp.lang +++ b/htdocs/langs/en_US/mrp.lang @@ -37,6 +37,8 @@ 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? +DeleteMoChild = Delete the child OFs linked to this OF %s +MoChildsDeleted = All child OFs have been deleted MenuMRP=Manufacturing Orders NewMO=New Manufacturing Order QtyToProduce=Qty to produce diff --git a/htdocs/langs/fr_FR/mrp.lang b/htdocs/langs/fr_FR/mrp.lang index 3d06398cbab..4e67ee09057 100644 --- a/htdocs/langs/fr_FR/mrp.lang +++ b/htdocs/langs/fr_FR/mrp.lang @@ -37,6 +37,8 @@ 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 ? +DeleteMoChild = Supprimer les OFs enfants liés à cet OF %s +MoChildsDeleted = Tous les OFs enfants ont été supprimés MenuMRP=Ordres de fabrication NewMO=Nouvel Ordre de fabrication QtyToProduce=Quantité à produire diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index e000b498589..af9c36c1af8 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -1816,6 +1816,46 @@ class Mo extends CommonObject } } + /** + * Function used to return all child MOs recursively + * + * @param int $depth Depth for recursing loop count + * @return Mo[]|int[] array of MOs if OK, -1 if KO + */ + public function getAllMoChilds($depth = 0) + { + if ($depth > 1000) return -1; + + $TMoChilds = array(); + $error = 0; + + $childMoList = $this->getMoChilds(); + + if ($childMoList == -1) return -1; + + foreach ($childMoList as $childMo) $TMoChilds[$childMo->id] = $childMo; + + foreach ($childMoList as $childMo) { + $childMoChildren = $childMo->getAllMoChilds($depth + 1); + + if ($childMoChildren == -1) { + $error++; + } else { + foreach ($childMoChildren as $child) { + $TMoChilds[$child->id] = $child; + } + } + } + + if ($error) { + return -1; + } else { + return $TMoChilds; + } + } + + + /** * Function used to return childs of Mo * diff --git a/htdocs/mrp/mo_card.php b/htdocs/mrp/mo_card.php index 4b0c6b65c1a..8696b83648a 100644 --- a/htdocs/mrp/mo_card.php +++ b/htdocs/mrp/mo_card.php @@ -196,6 +196,66 @@ if (empty($reshook)) { } } + if ($action == 'confirm_delete' && !empty($permissiontodelete)) { + if (!($object->id > 0)) { + dol_print_error('', 'Error, object must be fetched before being deleted'); + exit; + } + + $error = 0; + $deleteChilds = GETPOST('deletechilds', 'boolean'); + + // Start the database transaction + $db->begin(); + + if ($deleteChilds === 'on') { + $TMoChildren = $object->getAllMoChilds(); + + foreach ($TMoChildren as $id => $childObject) { + if ($childObject->delete($user) == -1) { + $error++; + if (!empty($childObject->errors)) { + setEventMessages(null, $childObject->errors, 'errors'); + } else { + setEventMessages($childObject->error, null, 'errors'); + } + } + } + } + + if (!$error) { + $result = $object->delete($user); + + if ($result > 0) { + setEventMessages("RecordDeleted", null, 'mesgs'); + + if ($deleteChilds === 'on') { + setEventMessages("MoChildsDeleted", null, 'mesgs'); + } + + if (empty($noback)) { + header("Location: " . $backurlforlist); + exit; + } + } else { + $error++; + if (!empty($object->errors)) { + setEventMessages(null, $object->errors, 'errors'); + } else { + setEventMessages($object->error, null, 'errors'); + } + } + } + + // Commit or rollback the database transaction based on whether there was an error + if ($error) { + $db->rollback(); + } else { + $db->commit(); + } + } + + // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php'; @@ -434,15 +494,21 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // Confirmation to delete if ($action == 'delete') { + $numberofmochilds = count($object->getAllMoChilds()); + + if ($numberofmochilds > 0)$label = $langs->trans("DeleteMoChild", '('.strval($numberofmochilds).')'); + else $label = $langs->trans("DeleteMoChild"); + $formquestion = array( + array('type' => 'checkbox', 'name' => 'deletechilds', 'label' => $label, 'value' => 0), 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); + $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') {