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 @@ -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
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 @@ -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
Expand Down
40 changes: 40 additions & 0 deletions htdocs/mrp/class/mo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,46 @@ 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)
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: 68 additions & 2 deletions htdocs/mrp/mo_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,66 @@
}
}

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

Expand Down Expand Up @@ -434,15 +494,21 @@

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