From 44ff30997161eedc7c9a95e77d883e8f5fcca36e Mon Sep 17 00:00:00 2001 From: ATM-Sami Date: Tue, 3 Oct 2023 11:52:36 +0200 Subject: [PATCH 1/5] added recursive deletion option for child MOs + added recursive recovery method for child MOs --- htdocs/langs/en_US/mrp.lang | 2 ++ htdocs/langs/fr_FR/mrp.lang | 2 ++ htdocs/mrp/class/mo.class.php | 37 ++++++++++++++++++++ htdocs/mrp/mo_card.php | 65 ++++++++++++++++++++++++++++++++++- 4 files changed, 105 insertions(+), 1 deletion(-) diff --git a/htdocs/langs/en_US/mrp.lang b/htdocs/langs/en_US/mrp.lang index 92105e3b63e16..2ebba9f37d4b7 100644 --- a/htdocs/langs/en_US/mrp.lang +++ b/htdocs/langs/en_US/mrp.lang @@ -34,6 +34,8 @@ DeleteBillOfMaterials=Delete Bill Of Materials 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 +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 04e1da9ea297a..ae70b1a7ea881 100644 --- a/htdocs/langs/fr_FR/mrp.lang +++ b/htdocs/langs/fr_FR/mrp.lang @@ -34,6 +34,8 @@ DeleteBillOfMaterials=Supprimer la nomenclature 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 +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 ae404fbf9ffdc..9c71493c1e328 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -1659,6 +1659,43 @@ public function getMoChilds() } } + /** + * Function used to return all child MOs recursively + * + * @return Mo[]|int[] array of MOs if OK, -1 if KO + */ + public function getAllMoChilds() + { + $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(); + + 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 8a2ebee5e6308..fe15fb36f11ae 100644 --- a/htdocs/mrp/mo_card.php +++ b/htdocs/mrp/mo_card.php @@ -176,6 +176,66 @@ exit; } + 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'); + + if ($deleteChilds === 'on') { + $TMoChildren = $object->getAllMoChilds(); + + // Supprimer les objets enfants en utilisant une transaction séparée + $db->begin(); + + 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) { + $db->rollback(); + } else { + $db->commit(); + } + } + + 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'); + } + } + + $action = ''; + } + } + + // 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'; @@ -414,7 +474,10 @@ // 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('type' => 'checkbox', 'name' => 'deletechilds', 'label' => $langs->trans("DeleteMoChild"), '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') { From dac3c291a1bdfd6b91bf362b4bd31f9c56e0a9b9 Mon Sep 17 00:00:00 2001 From: ATM-Sami Date: Tue, 3 Oct 2023 11:59:29 +0200 Subject: [PATCH 2/5] add number of MOs in form_confirm --- htdocs/langs/en_US/mrp.lang | 2 +- htdocs/langs/fr_FR/mrp.lang | 2 +- htdocs/mrp/mo_card.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/langs/en_US/mrp.lang b/htdocs/langs/en_US/mrp.lang index 2ebba9f37d4b7..a141356878d13 100644 --- a/htdocs/langs/en_US/mrp.lang +++ b/htdocs/langs/en_US/mrp.lang @@ -34,7 +34,7 @@ DeleteBillOfMaterials=Delete Bill Of Materials 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 +DeleteMoChild = Delete the child OFs linked to this OF (%s) MoChildsDeleted = All child OFs have been deleted MenuMRP=Manufacturing Orders NewMO=New Manufacturing Order diff --git a/htdocs/langs/fr_FR/mrp.lang b/htdocs/langs/fr_FR/mrp.lang index ae70b1a7ea881..568cd6b4da50d 100644 --- a/htdocs/langs/fr_FR/mrp.lang +++ b/htdocs/langs/fr_FR/mrp.lang @@ -34,7 +34,7 @@ DeleteBillOfMaterials=Supprimer la nomenclature 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 +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 diff --git a/htdocs/mrp/mo_card.php b/htdocs/mrp/mo_card.php index fe15fb36f11ae..cba2faa05755f 100644 --- a/htdocs/mrp/mo_card.php +++ b/htdocs/mrp/mo_card.php @@ -475,7 +475,7 @@ // Confirmation to delete if ($action == 'delete') { $formquestion = array( - array('type' => 'checkbox', 'name' => 'deletechilds', 'label' => $langs->trans("DeleteMoChild"), 'value' => 0), + array('type' => 'checkbox', 'name' => 'deletechilds', 'label' => $langs->trans("DeleteMoChild", strval(count($object->getAllMoChilds()))), 'value' => 0), ); $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteMo'), $langs->trans('ConfirmDeleteMo'), 'confirm_delete', $formquestion, 0, 1); } From f818fe9feff1693fc7e5798631f94daa2837ecaa Mon Sep 17 00:00:00 2001 From: ATM-Sami Date: Tue, 3 Oct 2023 12:24:25 +0200 Subject: [PATCH 3/5] update to delete number of mo childs if is null --- htdocs/langs/en_US/mrp.lang | 2 +- htdocs/langs/fr_FR/mrp.lang | 2 +- htdocs/mrp/mo_card.php | 7 ++++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/htdocs/langs/en_US/mrp.lang b/htdocs/langs/en_US/mrp.lang index a141356878d13..82c3813f757e2 100644 --- a/htdocs/langs/en_US/mrp.lang +++ b/htdocs/langs/en_US/mrp.lang @@ -34,7 +34,7 @@ DeleteBillOfMaterials=Delete Bill Of Materials 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) +DeleteMoChild = Delete the child OFs linked to this OF %s MoChildsDeleted = All child OFs have been deleted MenuMRP=Manufacturing Orders NewMO=New Manufacturing Order diff --git a/htdocs/langs/fr_FR/mrp.lang b/htdocs/langs/fr_FR/mrp.lang index 568cd6b4da50d..e8316048101f0 100644 --- a/htdocs/langs/fr_FR/mrp.lang +++ b/htdocs/langs/fr_FR/mrp.lang @@ -34,7 +34,7 @@ DeleteBillOfMaterials=Supprimer la nomenclature 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) +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 diff --git a/htdocs/mrp/mo_card.php b/htdocs/mrp/mo_card.php index cba2faa05755f..92b63fdfc712d 100644 --- a/htdocs/mrp/mo_card.php +++ b/htdocs/mrp/mo_card.php @@ -474,8 +474,13 @@ // 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' => $langs->trans("DeleteMoChild", strval(count($object->getAllMoChilds()))), 'value' => 0), + array('type' => 'checkbox', 'name' => 'deletechilds', 'label' => $label, 'value' => 0), ); $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteMo'), $langs->trans('ConfirmDeleteMo'), 'confirm_delete', $formquestion, 0, 1); } From 689b9bdc3d083dd15c5ed9b9fff32f1ca6cab637 Mon Sep 17 00:00:00 2001 From: atm-sami Date: Sun, 8 Oct 2023 19:57:16 +0200 Subject: [PATCH 4/5] added checking of an infinity loop --- htdocs/mrp/class/mo.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index 9c71493c1e328..7ea5bef6121e5 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -1664,8 +1664,10 @@ public function getMoChilds() * * @return Mo[]|int[] array of MOs if OK, -1 if KO */ - public function getAllMoChilds() + public function getAllMoChilds($depth = 0) { + if ($depth > 1000) return -1; + $TMoChilds = array(); $error = 0; @@ -1676,7 +1678,7 @@ public function getAllMoChilds() foreach ($childMoList as $childMo) $TMoChilds[$childMo->id] = $childMo; foreach ($childMoList as $childMo) { - $childMoChildren = $childMo->getAllMoChilds(); + $childMoChildren = $childMo->getAllMoChilds($depth + 1); if ($childMoChildren == -1) { $error++; From 44d2561d485c71bf6c835536bad8fcc7c17db075 Mon Sep 17 00:00:00 2001 From: ATM-Sami Date: Mon, 9 Oct 2023 11:42:00 +0200 Subject: [PATCH 5/5] review PR --- htdocs/mrp/class/mo.class.php | 1 + htdocs/mrp/mo_card.php | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index 7ea5bef6121e5..df5facfdad31c 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -1662,6 +1662,7 @@ public function getMoChilds() /** * 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) diff --git a/htdocs/mrp/mo_card.php b/htdocs/mrp/mo_card.php index 92b63fdfc712d..91df779235ab7 100644 --- a/htdocs/mrp/mo_card.php +++ b/htdocs/mrp/mo_card.php @@ -185,12 +185,12 @@ $error = 0; $deleteChilds = GETPOST('deletechilds', 'boolean'); + // Start the database transaction + $db->begin(); + if ($deleteChilds === 'on') { $TMoChildren = $object->getAllMoChilds(); - // Supprimer les objets enfants en utilisant une transaction séparée - $db->begin(); - foreach ($TMoChildren as $id => $childObject) { if ($childObject->delete($user) == -1) { $error++; @@ -201,11 +201,6 @@ } } } - if ($error) { - $db->rollback(); - } else { - $db->commit(); - } } if (!$error) { @@ -230,8 +225,13 @@ setEventMessages($object->error, null, 'errors'); } } + } - $action = ''; + // Commit or rollback the database transaction based on whether there was an error + if ($error) { + $db->rollback(); + } else { + $db->commit(); } }