dolibarr 24.0.0-beta
ajaxpayment.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011 Auguria <anthony.poiret@auguria.net>
3 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2026 Lenin Rivas <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
26if (!defined('NOREQUIRESOC')) {
27 define('NOREQUIRESOC', '1');
28}
29if (!defined('NOTOKENRENEWAL')) {
30 define('NOTOKENRENEWAL', '1');
31}
32if (!defined('NOREQUIREMENU')) {
33 define('NOREQUIREMENU', '1'); // If there is no menu to show
34}
35if (!defined('NOREQUIREHTML')) {
36 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
37}
38
39// Load Dolibarr environment
40require '../main.inc.php';
41
50$langs->load('compta');
51
52// No permission check. This is just a formatting data service.
53
54
55/*
56 * View
57 */
58
59//init var
60$invoice_type = GETPOSTINT('invoice_type');
61$amountPayment = GETPOST('amountPayment');
62$amounts = GETPOST('amounts', 'array'); // from text inputs : invoice amount payment (check required)
63$remains = GETPOST('remains'); // from dolibarr's object (no need to check)
64$currentInvId = GETPOST('imgClicked'); // from DOM elements : imgId (equals invoice id)
65
66// Getting the posted keys=>values, sanitize the ones who are from text inputs
67$amountPayment = $amountPayment != '' ? (is_numeric(price2num($amountPayment)) ? price2num($amountPayment) : '') : ''; // keep void if not a valid entry
68
69// Multicurrency LRR
70$multicurrency = GETPOSTINT('multicurrency');
71$multicurrencyAmountPayment = GETPOST('multicurrency_amountPayment');
72$multicurrencyAmounts = GETPOST('multicurrency_amounts', 'array'); // from text inputs : invoice amount payment (check required)
73$multicurrencyRemains = GETPOST('multicurrency_remains'); // from dolibarr's object (no need to check)
74
75// Getting the posted keys=>values, sanitize the ones who are from text inputs
76$multicurrencyAmountPayment = $multicurrencyAmountPayment != '' ? (is_numeric(price2num($multicurrencyAmountPayment)) ? price2num($multicurrencyAmountPayment) : '') : ''; // keep void if not a valid entry
77
78if ($multicurrency) {
79 // Clean checkamounts multicurrency
80 if (is_array($multicurrencyAmounts)) {
81 foreach ($multicurrencyAmounts as $key => $value) {
82 $value = price2num($value);
83 $multicurrencyAmounts[$key] = $value;
84 if (empty($value)) {
85 unset($multicurrencyAmounts[$key]);
86 }
87 }
88 }
89 // Clean remains multicurrency
90 if (is_array($multicurrencyRemains)) {
91 foreach ($multicurrencyRemains as $key => $value) {
92 $value = price2num($value);
93 $multicurrencyRemains[$key] = ($invoice_type == 2 ? -1 : 1) * (float) $value;
94 if (empty($value)) {
95 unset($multicurrencyRemains[$key]);
96 }
97 }
98 } elseif ($multicurrencyRemains) {
99 $multicurrencyRemains = array(price2num($multicurrencyRemains));
100 } else {
101 $multicurrencyRemains = array();
102 }
103
104 // Treatment
105 $result = ($multicurrencyAmountPayment != '') ? ((float) $multicurrencyAmountPayment - array_sum($multicurrencyAmounts)) : array_sum($multicurrencyAmounts); // Remaining amountPayment
106 $toJsonArray = array();
107 $totalRemaining = price2num(array_sum($multicurrencyRemains));
108 $toJsonArray['label'] = $multicurrencyAmountPayment == '' ? '' : $langs->transnoentities('RemainingAmountPayment');
109 if ($currentInvId) { // Here to breakdown
110 // Get the current amount (from form) and the corresponding remainToPay (from invoice)
111 $currentAmount = $multicurrencyAmounts['amount_'.$currentInvId];
112 $currentRemain = $multicurrencyRemains['remain_'.$currentInvId];
113
114 // If amountPayment isn't filled, breakdown invoice amount, else breakdown from amountPayment
115 if ($multicurrencyAmountPayment == '') {
116 // Check if current amount exists in amounts
117 $amountExists = array_key_exists('amount_'.$currentInvId, $multicurrencyAmounts);
118 if ($amountExists) {
119 $remainAmount = $currentRemain - $currentAmount; // To keep value between curRemain and curAmount
120 $result += $remainAmount; // result must be deduced by
121 $currentAmount += $remainAmount; // curAmount put to curRemain
122 } else {
123 $currentAmount = $currentRemain;
124 $result += $currentRemain;
125 }
126 } else {
127 // Reset the subtraction for this amount
128 $result += price2num($currentAmount);
129 $currentAmount = 0;
130
131 if ($result >= 0) { // then we need to calculate the amount to breakdown
132 $amountToBreakdown = ($result - $currentRemain >= 0 ?
133 $currentRemain : // Remain can be fully paid
134 $currentRemain + ($result - $currentRemain)); // Remain can only partially be paid
135 $currentAmount = $amountToBreakdown; // In both cases, amount will take breakdown value
136 $result -= $amountToBreakdown; // And canceled subtraction has been replaced by breakdown
137 } // else there's no need to calc anything, just reset the field (result is still < 0)
138 }
139 $toJsonArray['multicurrency_amount_'.$currentInvId] = price2num($currentAmount); // Param will exist only if an img has been clicked
140 }
141
142 $toJsonArray['multicurrency_makeRed'] = ($totalRemaining < price2num($result) || price2num($result) < 0);
143 $toJsonArray['multicurrency_result'] = price($result); // Return value to user format
144 $toJsonArray['multicurrency_resultnum'] = price2num($result); // Return value to numeric format
145} else {
146 // Clean checkamounts
147 if (is_array($amounts)) {
148 foreach ($amounts as $key => $value) {
149 $value = price2num($value);
150 $amounts[$key] = $value;
151 if (empty($value)) {
152 unset($amounts[$key]);
153 }
154 }
155 }
156 // Clean remains
157 if (is_array($remains)) {
158 foreach ($remains as $key => $value) {
159 $value = price2num($value);
160 $remains[$key] = ($invoice_type == 2 ? -1 : 1) * (float) $value;
161 if (empty($value)) {
162 unset($remains[$key]);
163 }
164 }
165 } elseif ($remains) {
166 $remains = array(price2num($remains));
167 } else {
168 $remains = array();
169 }
170
171 // Treatment
172 $result = ($amountPayment != '') ? ((float) $amountPayment - array_sum($amounts)) : array_sum($amounts); // Remaining amountPayment
173 $toJsonArray = array();
174 $totalRemaining = price2num(array_sum($remains));
175 $toJsonArray['label'] = $amountPayment == '' ? '' : $langs->transnoentities('RemainingAmountPayment');
176 if ($currentInvId) { // Here to breakdown
177 // Get the current amount (from form) and the corresponding remainToPay (from invoice)
178 $currentAmount = $amounts['amount_'.$currentInvId];
179 $currentRemain = $remains['remain_'.$currentInvId];
180
181 // If amountPayment isn't filled, breakdown invoice amount, else breakdown from amountPayment
182 if ($amountPayment == '') {
183 // Check if current amount exists in amounts
184 $amountExists = array_key_exists('amount_'.$currentInvId, $amounts);
185 if ($amountExists) {
186 $remainAmount = $currentRemain - $currentAmount; // To keep value between curRemain and curAmount
187 $result += $remainAmount; // result must be deduced by
188 $currentAmount += $remainAmount; // curAmount put to curRemain
189 } else {
190 $currentAmount = $currentRemain;
191 $result += $currentRemain;
192 }
193 } else {
194 // Reset the subtraction for this amount
195 $result += price2num($currentAmount);
196 $currentAmount = 0;
197
198 if ($result >= 0) { // then we need to calculate the amount to breakdown
199 $amountToBreakdown = ($result - $currentRemain >= 0 ?
200 $currentRemain : // Remain can be fully paid
201 $currentRemain + ($result - $currentRemain)); // Remain can only partially be paid
202 $currentAmount = $amountToBreakdown; // In both cases, amount will take breakdown value
203 $result -= $amountToBreakdown; // And canceled subtraction has been replaced by breakdown
204 } // else there's no need to calc anything, just reset the field (result is still < 0)
205 }
206 $toJsonArray['amount_'.$currentInvId] = price2num($currentAmount); // Param will exist only if an img has been clicked
207 }
208
209 $toJsonArray['makeRed'] = ($totalRemaining < price2num($result) || price2num($result) < 0);
210 $toJsonArray['result'] = price($result); // Return value to user format
211 $toJsonArray['resultnum'] = price2num($result); // Return value to numeric format
212}
213
214// Encode to JSON to return
215echo json_encode($toJsonArray); // Printing the call's result
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.