diff --git a/htdocs/fourn/commande/card.php b/htdocs/fourn/commande/card.php index 4e648b3271122..d53802c4b428b 100644 --- a/htdocs/fourn/commande/card.php +++ b/htdocs/fourn/commande/card.php @@ -65,6 +65,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'; +} /** * @var Conf $conf @@ -83,21 +87,22 @@ // Get Parameters -$id = GETPOSTINT('id'); -$ref = GETPOST('ref', 'alpha'); $action = GETPOST('action', 'alpha'); $confirm = GETPOST('confirm', 'alpha'); $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'purchaseordercard'; // To manage different context of search +$cancel = GETPOST('cancel', 'alpha'); $backtopage = GETPOST('backtopage', 'alpha'); $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha'); +$id = GETPOSTINT('id'); +$ref = GETPOST('ref', 'alpha'); $socid = GETPOSTINT('socid'); $projectid = GETPOSTINT('projectid'); -$cancel = GETPOST('cancel', 'alpha'); $lineid = GETPOSTINT('lineid'); $origin = GETPOST('origin', 'alpha'); $originid = (GETPOSTINT('originid') ? GETPOSTINT('originid') : GETPOSTINT('origin_id')); // For backward compatibility $rank = (GETPOSTINT('rank') > 0) ? GETPOSTINT('rank') : -1; +$stockDelete = GETPOST('stockDelete', 'int'); // PDF $hidedetails = (GETPOSTINT('hidedetails') ? GETPOSTINT('hidedetails') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0)); @@ -1128,12 +1133,63 @@ 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; + // Delete existing dispatched lines + $errOnDelete = 0; + + $db->begin(); + + if ($stockDelete) { + $dispatchedLines = $object->getDispachedLines(); + if (!empty($dispatchedLines)) { + foreach ($dispatchedLines as $dispatchedLine) { + $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 (empty($errOnDelete)) { + $result = $object->delete($user); + if ($result > 0) { + $db->commit(); + header("Location: " . DOL_URL_ROOT . '/fourn/commande/list.php?restore_lastsearch_values=1'); + exit; + } else { + $db->rollback(); + setEventMessages($object->error, $object->errors, 'errors'); + } } else { - setEventMessages($object->error, $object->errors, 'errors'); + $db->rollback(); + setEventMessages('', $errorsOnDelete, 'errors'); } } @@ -1902,7 +1958,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 diff --git a/htdocs/langs/en_US/orders.lang b/htdocs/langs/en_US/orders.lang index 33ec8c22c7d62..692bd2c77140b 100644 --- a/htdocs/langs/en_US/orders.lang +++ b/htdocs/langs/en_US/orders.lang @@ -206,4 +206,8 @@ StatusSupplierOrderReceivedPartially=Partially received StatusSupplierOrderReceivedAll=All products received NeedAtLeastOneInvoice = There has to be at least one Invoice 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 OrderStatusMakeOperationForbidden = Order status forbidden for this operation.