From cdf9d045d693290cdc72de88ef39992b5ce15177 Mon Sep 17 00:00:00 2001 From: Sylvain Legrand Date: Wed, 6 Dec 2023 06:41:08 +0100 Subject: [PATCH] Update card.php When using a discount or credit equal to the invoice amount, set that invoice as paid. --- htdocs/fourn/facture/card.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php index 1f338317902..90446c20584 100644 --- a/htdocs/fourn/facture/card.php +++ b/htdocs/fourn/facture/card.php @@ -510,6 +510,7 @@ if (empty($reshook)) { dol_print_error($db, $object->error); } } elseif ($action == "setabsolutediscount" && $usercancreate) { + $db->begin(); // We use the credit to reduce amount of invoice if (GETPOST("remise_id", "int")) { $ret = $object->fetch($id); @@ -530,7 +531,8 @@ if (empty($reshook)) { //var_dump($object->getRemainToPay(0)); //var_dump($discount->amount_ttc);exit; - if (price2num($discount->amount_ttc) > price2num($object->getRemainToPay(0))) { + $remaintopay = $object->getRemainToPay(0); + if (price2num($discount->amount_ttc) > price2num($remaintopay)) { // TODO Split the discount in 2 automatically $error++; setEventMessages($langs->trans("ErrorDiscountLargerThanRemainToPaySplitItBefore"), null, 'errors'); @@ -539,12 +541,23 @@ if (empty($reshook)) { if (!$error) { $result = $discount->link_to_invoice(0, $id); if ($result < 0) { + $error++; setEventMessages($discount->error, $discount->errors, 'errors'); } } + if (!$error) { + $newremaintopay = $object->getRemainToPay(0); + if ($newremaintopay == 0) { + $object->setPaid($user); + } + } } - - if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) { + if (!$error) { + $db->commit(); + } else { + $db->rollback(); + } + if (empty($error) && !getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) { $outputlangs = $langs; $newlang = ''; if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {