Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW add recursive deletion option for child m os #26102

Merged
2 changes: 2 additions & 0 deletions htdocs/langs/en_US/mrp.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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 %s
MoChildsDeleted = All child OFs have been deleted
MenuMRP=Manufacturing Orders
NewMO=New Manufacturing Order
QtyToProduce=Qty to produce
Expand Down
2 changes: 2 additions & 0 deletions htdocs/langs/fr_FR/mrp.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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 %s
MoChildsDeleted = Tous les OFs enfants ont été supprimés
MenuMRP=Ordres de fabrication
NewMO=Nouvel Ordre de fabrication
QtyToProduce=Quantité à produire
Expand Down
39 changes: 39 additions & 0 deletions htdocs/mrp/class/mo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,45 @@ 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($depth = 0)
samifilali marked this conversation as resolved.
Show resolved Hide resolved
{
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
*
Expand Down
70 changes: 69 additions & 1 deletion htdocs/mrp/mo_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
samifilali marked this conversation as resolved.
Show resolved Hide resolved

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';

Expand Down Expand Up @@ -414,7 +474,15 @@

// 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);
$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),
);
$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') {
Expand Down