From 624407afccc0b8a08ec1ae4570a85bb607bac7ca Mon Sep 17 00:00:00 2001 From: Lenin Rivas <53640168+leninrivas@users.noreply.github.com> Date: Wed, 4 Feb 2026 09:19:48 -0500 Subject: [PATCH] NEW Show total multicurrency on payment (#37070) * Total multicurrency * Total multicurrency paiment * total result multicurrency * show total multicurrency --- htdocs/compta/ajaxpayment.php | 199 ++++++++++++++++++++++++---------- htdocs/compta/paiement.php | 53 +++++++-- 2 files changed, 183 insertions(+), 69 deletions(-) diff --git a/htdocs/compta/ajaxpayment.php b/htdocs/compta/ajaxpayment.php index 4ec40478b98..369a0f1669b 100644 --- a/htdocs/compta/ajaxpayment.php +++ b/htdocs/compta/ajaxpayment.php @@ -2,6 +2,7 @@ /* Copyright (C) 2011 Auguria * Copyright (C) 2024 MDW * Copyright (C) 2024 Frédéric France + * Copyright (C) 2026 Lenin Rivas * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -65,72 +66,150 @@ $currentInvId = GETPOST('imgClicked'); // from DOM elements : imgId (equals invo // Getting the posted keys=>values, sanitize the ones who are from text inputs $amountPayment = $amountPayment != '' ? (is_numeric(price2num($amountPayment)) ? price2num($amountPayment) : '') : ''; // keep void if not a valid entry -// Clean checkamounts -if (is_array($amounts)) { - foreach ($amounts as $key => $value) { - $value = price2num($value); - $amounts[$key] = $value; - if (empty($value)) { - unset($amounts[$key]); +// Multicurrency LRR +$multicurrency = GETPOSTINT('multicurrency'); +$multicurrencyAmountPayment = GETPOST('multicurrency_amountPayment'); +$multicurrencyAmounts = GETPOST('multicurrency_amounts'); // from text inputs : invoice amount payment (check required) +$multicurrencyRemains = GETPOST('multicurrency_remains'); // from dolibarr's object (no need to check) + +// Getting the posted keys=>values, sanitize the ones who are from text inputs +$multicurrencyAmountPayment = $multicurrencyAmountPayment != '' ? (is_numeric(price2num($multicurrencyAmountPayment)) ? price2num($multicurrencyAmountPayment) : '') : ''; // keep void if not a valid entry + +if ($multicurrency) { + // Clean checkamounts multicurrency + if (is_array($multicurrencyAmounts)) { + foreach ($multicurrencyAmounts as $key => $value) { + $value = price2num($value); + $multicurrencyAmounts[$key] = $value; + if (empty($value)) { + unset($multicurrencyAmounts[$key]); + } } } -} -// Clean remains -if (is_array($remains)) { - foreach ($remains as $key => $value) { - $value = price2num($value); - $remains[$key] = ($invoice_type == 2 ? -1 : 1) * (float) $value; - if (empty($value)) { - unset($remains[$key]); - } - } -} elseif ($remains) { - $remains = array(price2num($remains)); -} else { - $remains = array(); -} - -// Treatment -$result = ($amountPayment != '') ? ((float) $amountPayment - array_sum($amounts)) : array_sum($amounts); // Remaining amountPayment -$toJsonArray = array(); -$totalRemaining = price2num(array_sum($remains)); -$toJsonArray['label'] = $amountPayment == '' ? '' : $langs->transnoentities('RemainingAmountPayment'); -if ($currentInvId) { // Here to breakdown - // Get the current amount (from form) and the corresponding remainToPay (from invoice) - $currentAmount = $amounts['amount_'.$currentInvId]; - $currentRemain = $remains['remain_'.$currentInvId]; - - // If amountPayment isn't filled, breakdown invoice amount, else breakdown from amountPayment - if ($amountPayment == '') { - // Check if current amount exists in amounts - $amountExists = array_key_exists('amount_'.$currentInvId, $amounts); - if ($amountExists) { - $remainAmount = $currentRemain - $currentAmount; // To keep value between curRemain and curAmount - $result += $remainAmount; // result must be deduced by - $currentAmount += $remainAmount; // curAmount put to curRemain - } else { - $currentAmount = $currentRemain; - $result += $currentRemain; + // Clean remains multicurrency + if (is_array($multicurrencyRemains)) { + foreach ($multicurrencyRemains as $key => $value) { + $value = price2num($value); + $multicurrencyRemains[$key] = ($invoice_type == 2 ? -1 : 1) * (float) $value; + if (empty($value)) { + unset($multicurrencyRemains[$key]); + } } + } elseif ($multicurrencyRemains) { + $multicurrencyRemains = array(price2num($multicurrencyRemains)); } else { - // Reset the subtraction for this amount - $result += price2num($currentAmount); - $currentAmount = 0; - - if ($result >= 0) { // then we need to calculate the amount to breakdown - $amountToBreakdown = ($result - $currentRemain >= 0 ? - $currentRemain : // Remain can be fully paid - $currentRemain + ($result - $currentRemain)); // Remain can only partially be paid - $currentAmount = $amountToBreakdown; // In both cases, amount will take breakdown value - $result -= $amountToBreakdown; // And canceled subtraction has been replaced by breakdown - } // else there's no need to calc anything, just reset the field (result is still < 0) + $multicurrencyRemains = array(); } - $toJsonArray['amount_'.$currentInvId] = price2num($currentAmount); // Param will exist only if an img has been clicked + + // Treatment + $result = ($multicurrencyAmountPayment != '') ? ((float) $multicurrencyAmountPayment - array_sum($multicurrencyAmounts)) : array_sum($multicurrencyAmounts); // Remaining amountPayment + $toJsonArray = array(); + $totalRemaining = price2num(array_sum($multicurrencyRemains)); + $toJsonArray['label'] = $multicurrencyAmountPayment == '' ? '' : $langs->transnoentities('RemainingAmountPayment'); + if ($currentInvId) { // Here to breakdown + // Get the current amount (from form) and the corresponding remainToPay (from invoice) + $currentAmount = $multicurrencyAmounts['amount_'.$currentInvId]; + $currentRemain = $multicurrencyRemains['remain_'.$currentInvId]; + + // If amountPayment isn't filled, breakdown invoice amount, else breakdown from amountPayment + if ($multicurrencyAmountPayment == '') { + // Check if current amount exists in amounts + $amountExists = array_key_exists('amount_'.$currentInvId, $multicurrencyAmounts); + if ($amountExists) { + $remainAmount = $currentRemain - $currentAmount; // To keep value between curRemain and curAmount + $result += $remainAmount; // result must be deduced by + $currentAmount += $remainAmount; // curAmount put to curRemain + } else { + $currentAmount = $currentRemain; + $result += $currentRemain; + } + } else { + // Reset the subtraction for this amount + $result += price2num($currentAmount); + $currentAmount = 0; + + if ($result >= 0) { // then we need to calculate the amount to breakdown + $amountToBreakdown = ($result - $currentRemain >= 0 ? + $currentRemain : // Remain can be fully paid + $currentRemain + ($result - $currentRemain)); // Remain can only partially be paid + $currentAmount = $amountToBreakdown; // In both cases, amount will take breakdown value + $result -= $amountToBreakdown; // And canceled subtraction has been replaced by breakdown + } // else there's no need to calc anything, just reset the field (result is still < 0) + } + $toJsonArray['multicurrency_amount_'.$currentInvId] = price2num($currentAmount); // Param will exist only if an img has been clicked + } + + $toJsonArray['multicurrency_makeRed'] = ($totalRemaining < price2num($result) || price2num($result) < 0); + $toJsonArray['multicurrency_result'] = price($result); // Return value to user format + $toJsonArray['multicurrency_resultnum'] = price2num($result); // Return value to numeric format +} else { + // Clean checkamounts + if (is_array($amounts)) { + foreach ($amounts as $key => $value) { + $value = price2num($value); + $amounts[$key] = $value; + if (empty($value)) { + unset($amounts[$key]); + } + } + } + // Clean remains + if (is_array($remains)) { + foreach ($remains as $key => $value) { + $value = price2num($value); + $remains[$key] = ($invoice_type == 2 ? -1 : 1) * (float) $value; + if (empty($value)) { + unset($remains[$key]); + } + } + } elseif ($remains) { + $remains = array(price2num($remains)); + } else { + $remains = array(); + } + + // Treatment + $result = ($amountPayment != '') ? ((float) $amountPayment - array_sum($amounts)) : array_sum($amounts); // Remaining amountPayment + $toJsonArray = array(); + $totalRemaining = price2num(array_sum($remains)); + $toJsonArray['label'] = $amountPayment == '' ? '' : $langs->transnoentities('RemainingAmountPayment'); + if ($currentInvId) { // Here to breakdown + // Get the current amount (from form) and the corresponding remainToPay (from invoice) + $currentAmount = $amounts['amount_'.$currentInvId]; + $currentRemain = $remains['remain_'.$currentInvId]; + + // If amountPayment isn't filled, breakdown invoice amount, else breakdown from amountPayment + if ($amountPayment == '') { + // Check if current amount exists in amounts + $amountExists = array_key_exists('amount_'.$currentInvId, $amounts); + if ($amountExists) { + $remainAmount = $currentRemain - $currentAmount; // To keep value between curRemain and curAmount + $result += $remainAmount; // result must be deduced by + $currentAmount += $remainAmount; // curAmount put to curRemain + } else { + $currentAmount = $currentRemain; + $result += $currentRemain; + } + } else { + // Reset the subtraction for this amount + $result += price2num($currentAmount); + $currentAmount = 0; + + if ($result >= 0) { // then we need to calculate the amount to breakdown + $amountToBreakdown = ($result - $currentRemain >= 0 ? + $currentRemain : // Remain can be fully paid + $currentRemain + ($result - $currentRemain)); // Remain can only partially be paid + $currentAmount = $amountToBreakdown; // In both cases, amount will take breakdown value + $result -= $amountToBreakdown; // And canceled subtraction has been replaced by breakdown + } // else there's no need to calc anything, just reset the field (result is still < 0) + } + $toJsonArray['amount_'.$currentInvId] = price2num($currentAmount); // Param will exist only if an img has been clicked + } + + $toJsonArray['makeRed'] = ($totalRemaining < price2num($result) || price2num($result) < 0); + $toJsonArray['result'] = price($result); // Return value to user format + $toJsonArray['resultnum'] = price2num($result); // Return value to numeric format } -$toJsonArray['makeRed'] = ($totalRemaining < price2num($result) || price2num($result) < 0); -$toJsonArray['result'] = price($result); // Return value to user format -$toJsonArray['resultnum'] = price2num($result); // Return value to numeric format - // Encode to JSON to return echo json_encode($toJsonArray); // Printing the call's result diff --git a/htdocs/compta/paiement.php b/htdocs/compta/paiement.php index 369470e05bb..3690b2b29d0 100644 --- a/htdocs/compta/paiement.php +++ b/htdocs/compta/paiement.php @@ -9,7 +9,7 @@ * Copyright (C) 2014 Teddy Andreotti <125155@supinfo.com> * Copyright (C) 2015 Juanjo Menent * Copyright (C) 2018-2024 Frédéric France - * Copyright (C) 2023 Lenin Rivas + * Copyright (C) 2023-2026 Lenin Rivas * Copyright (C) 2023 Sylvain Legrand * Copyright (C) 2023 William Mead * Copyright (C) 2024-2025 MDW @@ -431,15 +431,24 @@ if ($result >= 0) { return subJson; } - function callForResult(imgId) + function callForResult(imgId, multicurrency = 0) { var json = {}; var form = $("#payment_form"); + var keyresult = "result"; json["invoice_type"] = $("#invoice_type").val(); - json["amountPayment"] = $("#amountpayment").attr("value"); - json["amounts"] = _elemToJson(form.find("input.amount")); - json["remains"] = _elemToJson(form.find("input.remain")); + if (multicurrency) { + keyresult = "multicurrency_result"; + json["multicurrency"] = 1; + json["multicurrency_amountPayment"] = $("#multicurrency_amountpayment").attr("value"); + json["multicurrency_amounts"] = _elemToJson(form.find("input.multicurrency_amount")); + json["multicurrency_remains"] = _elemToJson(form.find("input.multicurrency_remain")); + } else { + json["amountPayment"] = $("#amountpayment").attr("value"); + json["amounts"] = _elemToJson(form.find("input.amount")); + json["remains"] = _elemToJson(form.find("input.remain")); + } json["token"] = "'.currentToken().'"; if (imgId != null) { json["imgClicked"] = imgId; @@ -453,7 +462,7 @@ if ($result >= 0) { for (var key in json) { - if (key == "result") { + if (key == keyresult) { if (json["makeRed"]) { $("#"+key).addClass("error"); } else { @@ -475,6 +484,13 @@ if ($result >= 0) { $("#payment_form").find("input.amount").keyup(function() { callForResult(); }); + // Multicurrency LRR + $("#payment_form").find("input.multicurrency_amount").change(function() { + callForResult(null, 1); + }); + $("#payment_form").find("input.multicurrency_amount").keyup(function() { + callForResult(null, 1); + }); '; print ' });'."\n"; @@ -694,6 +710,10 @@ if ($result >= 0) { $totalrecu = 0; $totalrecucreditnote = 0; $totalrecudeposits = 0; + $multicurrency_total_ttc = 0; + $multicurrency_totalrecu = 0; + $multicurrency_totalrecucreditnote = 0; + $multicurrency_totalrecudeposits = 0; $sign = 1; print ''; @@ -930,6 +950,14 @@ if ($result >= 0) { $totalrecu += $paiement; $totalrecucreditnote += $creditnotes; $totalrecudeposits += $deposits; + + if (isModEnabled('multicurrency')) { + $multicurrency_total_ttc += $objp->multicurrency_total_ttc; + $multicurrency_totalrecu += $multicurrency_payment; + $multicurrency_totalrecucreditnote += $multicurrency_creditnotes; + $multicurrency_totalrecudeposits += $multicurrency_deposits; + } + $i++; } @@ -947,9 +975,16 @@ if ($result >= 0) { print ''.$langs->trans('TotalTTC').''; if (isModEnabled('multicurrency')) { print ''; - print ''; - print ''; - print ''; + print ''.price($sign * $multicurrency_total_ttc).''; + print ''.price($sign * $multicurrency_totalrecu); + if ($multicurrency_totalrecucreditnote) { + print '+'.price($multicurrency_totalrecucreditnote); + } + if ($multicurrency_totalrecudeposits) { + print '+'.price($multicurrency_totalrecudeposits); + } + print ''; + print ''.price($sign * (float) price2num($multicurrency_total_ttc - $multicurrency_totalrecu - $multicurrency_totalrecucreditnote - $multicurrency_totalrecudeposits, 'MT')).''; print ''; } print ''.price($sign * $total_ttc).'';