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

FIX Supplier orders : delete dispatched lines on supplier order deletion #25292

Merged
merged 11 commits into from
Nov 12, 2024
97 changes: 90 additions & 7 deletions htdocs/fourn/commande/card.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
}

if (isModEnabled('stock')) {
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.dispatch.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
}

// Load translation files required by the page
$langs->loadLangs(array('admin', 'orders', 'sendings', 'companies', 'bills', 'propal', 'receptions', 'supplier_proposal', 'deliveries', 'products', 'stocks', 'productbatch'));
Expand All @@ -80,6 +84,8 @@
$backtopage = GETPOST('backtopage', 'alpha');
$backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');

$stockDelete = GETPOST('stockDelete', 'int');

$socid = GETPOST('socid', 'int');
$projectid = GETPOST('projectid', 'int');
$cancel = GETPOST('cancel', 'alpha');
Expand Down Expand Up @@ -1100,12 +1106,62 @@


if ($action == 'confirm_delete' && $confirm == 'yes' && $usercandelete) {
$result = $object->delete($user);
if ($result > 0) {
header("Location: ".DOL_URL_ROOT.'/fourn/commande/list.php?restore_lastsearch_values=1');
exit;
} else {
setEventMessages($object->error, $object->errors, 'errors');
// Delete existing dispatched lines
$errOnDelete = 0;
if($stockDelete){
$dispatchedLines = $object->getDispachedLines();
if (!empty($dispatchedLines)) {
foreach ($dispatchedLines as $dispatchedLine) {
$db->begin();
Copy link
Member

@eldy eldy Sep 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The begin end transaction must not be inside the loop. We must have 1 transaction for all the event so result in database will be everything done or nothing.
Commit must be after the delete if order

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified code to locate correctly begin, rollback and commit instructions for db.
I noticed that $commande->delete also have a begin, rollback and commit logic on $this->db in the commande.class.php file.
If two transactions have begun in parallel, I prefered to finish all the logic on $db in the card.php code.

$supplierorderdispatch = new CommandeFournisseurDispatch($db);
$result = $supplierorderdispatch->fetch($dispatchedLine['id']);
if ($result > 0) {
$qty = $supplierorderdispatch->qty;
$entrepot = $supplierorderdispatch->fk_entrepot;
$product = $supplierorderdispatch->fk_product;
$price = price2num(GETPOST('price', 'alpha'), 'MU');
$comment = $langs->trans('SupplierOrderDeletion', $object->ref);
$eatby = $supplierorderdispatch->eatby;
$sellby = $supplierorderdispatch->sellby;
$batch = $supplierorderdispatch->batch;

$result = $supplierorderdispatch->delete($user);
}
if ($result < 0) {
$errorsOnDelete = $object->errors;
$errOnDelete++;
} else {
// If module stock is enabled and the stock increase is done on purchase order dispatching
if ($entrepot > 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) && empty($supplierorderdispatch->fk_reception)) {
$mouv = new MouvementStock($db);
if ($product > 0) {
$mouv->origin = &$object;
$mouv->setOrigin($object->element, $object->id);
$result = $mouv->livraison($user, $product, $entrepot, $qty, $price, $comment, '', $eatby, $sellby, $batch);
if ($result < 0) {
$errorsOnDelete = $mouv->errors;
$errOnDelete++;
}
}
}
}
if ($errOnDelete > 0) {
$db->rollback();
setEventMessages('', $errorsOnDelete, 'errors');
} else {
$db->commit();
}
}
}
}
if (empty($errOnDelete)) {
$result = $object->delete($user);
if ($result > 0) {
header("Location: " . DOL_URL_ROOT . '/fourn/commande/list.php?restore_lastsearch_values=1');
exit;
} else {
setEventMessages($object->error, $object->errors, 'errors');
}
}
}

Expand Down Expand Up @@ -1903,7 +1959,34 @@

// Confirmation de la suppression de la commande
if ($action == 'delete') {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteOrder'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete', '', 0, 2);
$arrayAjouts = array();
$heightModal = 0;
$widthModal = 500;
if (!empty($object->getDispachedLines())) {
$arrayAjouts = array(
array(
'type' => 'other',
'value' => img_warning() . " " . $langs->trans('ExistingDipatchLines')
),
array('type' => 'separator'),
array(
'type' => 'select',
'id' => 'stockDeleteSelect',
'name' => 'stockDelete',
'label' => $langs->trans('ConfirmDeleteDispatchedLines'),
'values' => array(1 => $langs->trans('Yes'), 0 => $langs->trans('No')),
'select_show_empty' => false
),
array(
'type' => 'other',
'value' => img_warning() . " " . $langs->trans('WarningDispatchedLinesWillNotBeAccessibles')
),
array('type' => 'separator'),
);
$heightModal = 300;
$widthModal = "70%";
}
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteOrder'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete', $arrayAjouts, 0, 2, $heightModal, $widthModal);
}

// Clone confirmation
Expand Down
6 changes: 5 additions & 1 deletion htdocs/langs/en_US/orders.lang
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,8 @@ StatusSupplierOrderRefused=Refused
StatusSupplierOrderReceivedPartially=Partially received
StatusSupplierOrderReceivedAll=All products received
NeedAtLeastOneInvoice = There has to be at least one Invoice
LineAlreadyDispatched = The order line is already received.
LineAlreadyDispatched = The order line is already received.
ExistingDipatchLines = There are dispatched lines for this order.
WarningDispatchedLinesWillNotBeAccessibles = You will be unable to update/remove these dispatched lines if you decide to not delete them.
ConfirmDeleteDispatchedLines = Delete dispatched lines ?
SupplierOrderDeletion = Supplier order %s Deletion