Skip to content

Commit

Permalink
NEW add recursive deletion option for child m os (#26102)
Browse files Browse the repository at this point in the history
* added recursive deletion option for child MOs + added recursive recovery method for child MOs

* add number of MOs in form_confirm

* update to delete number of mo childs if is null

* added checking of an infinity loop

* review PR
  • Loading branch information
samifilali authored Oct 31, 2023
1 parent e308e00 commit cfdc3d1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
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)
{
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

0 comments on commit cfdc3d1

Please sign in to comment.