NEW Show total multicurrency on payment (#37070)

* Total multicurrency

* Total multicurrency paiment

* total result multicurrency

* show total multicurrency
This commit is contained in:
Lenin Rivas 2026-02-04 09:19:48 -05:00 committed by GitHub
parent 34f7bf1e68
commit 624407afcc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 183 additions and 69 deletions

View file

@ -2,6 +2,7 @@
/* Copyright (C) 2011 Auguria <anthony.poiret@auguria.net>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2026 Lenin Rivas <frederic.france@free.fr>
*
* 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

View file

@ -9,7 +9,7 @@
* Copyright (C) 2014 Teddy Andreotti <125155@supinfo.com>
* Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2023 Lenin Rivas <lenin.rivas777@gmail.com>
* Copyright (C) 2023-2026 Lenin Rivas <lenin.rivas777@gmail.com>
* Copyright (C) 2023 Sylvain Legrand <technique@infras.fr>
* Copyright (C) 2023 William Mead <william.mead@manchenumerique.fr>
* Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
@ -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 '<tbody>';
@ -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 '<td colspan="'.$colspan.'" class="left">'.$langs->trans('TotalTTC').'</td>';
if (isModEnabled('multicurrency')) {
print '<td></td>';
print '<td></td>';
print '<td></td>';
print '<td></td>';
print '<td class="right"><b>'.price($sign * $multicurrency_total_ttc).'</b></td>';
print '<td class="right"><b>'.price($sign * $multicurrency_totalrecu);
if ($multicurrency_totalrecucreditnote) {
print '+'.price($multicurrency_totalrecucreditnote);
}
if ($multicurrency_totalrecudeposits) {
print '+'.price($multicurrency_totalrecudeposits);
}
print '</b></td>';
print '<td class="right"><b>'.price($sign * (float) price2num($multicurrency_total_ttc - $multicurrency_totalrecu - $multicurrency_totalrecucreditnote - $multicurrency_totalrecudeposits, 'MT')).'</b></td>';
print '<td class="right" id="multicurrency_result" style="font-weight: bold;"></td>';
}
print '<td class="right"><b>'.price($sign * $total_ttc).'</b></td>';