dolibarr 21.0.0-alpha
ajaxpayment.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011 Auguria <anthony.poiret@auguria.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24if (!defined('NOREQUIRESOC')) {
25 define('NOREQUIRESOC', '1');
26}
27if (!defined('NOTOKENRENEWAL')) {
28 define('NOTOKENRENEWAL', '1');
29}
30if (!defined('NOREQUIREMENU')) {
31 define('NOREQUIREMENU', '1'); // If there is no menu to show
32}
33if (!defined('NOREQUIREHTML')) {
34 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
35}
36
37// Load Dolibarr environment
38require '../main.inc.php';
39
40$langs->load('compta');
41
42// No permission check. This is just a formatting data service.
43
44
45/*
46 * View
47 */
48
49//init var
50$invoice_type = GETPOSTINT('invoice_type');
51$amountPayment = GETPOST('amountPayment');
52$amounts = GETPOST('amounts'); // from text inputs : invoice amount payment (check required)
53$remains = GETPOST('remains'); // from dolibarr's object (no need to check)
54$currentInvId = GETPOST('imgClicked'); // from DOM elements : imgId (equals invoice id)
55
56// Getting the posted keys=>values, sanitize the ones who are from text inputs
57$amountPayment = $amountPayment != '' ? (is_numeric(price2num($amountPayment)) ? price2num($amountPayment) : '') : ''; // keep void if not a valid entry
58
59// Clean checkamounts
60if (is_array($amounts)) {
61 foreach ($amounts as $key => $value) {
62 $value = price2num($value);
63 $amounts[$key] = $value;
64 if (empty($value)) {
65 unset($amounts[$key]);
66 }
67 }
68}
69// Clean remains
70if (is_array($remains)) {
71 foreach ($remains as $key => $value) {
72 $value = price2num($value);
73 $remains[$key] = ($invoice_type == 2 ? -1 : 1) * (float) $value;
74 if (empty($value)) {
75 unset($remains[$key]);
76 }
77 }
78} elseif ($remains) {
79 $remains = array(price2num($remains));
80} else {
81 $remains = array();
82}
83
84// Treatment
85$result = ($amountPayment != '') ? ((float) $amountPayment - array_sum($amounts)) : array_sum($amounts); // Remaining amountPayment
86$toJsonArray = array();
87$totalRemaining = price2num(array_sum($remains));
88$toJsonArray['label'] = $amountPayment == '' ? '' : $langs->transnoentities('RemainingAmountPayment');
89if ($currentInvId) { // Here to breakdown
90 // Get the current amount (from form) and the corresponding remainToPay (from invoice)
91 $currentAmount = $amounts['amount_'.$currentInvId];
92 $currentRemain = $remains['remain_'.$currentInvId];
93
94 // If amountPayment isn't filled, breakdown invoice amount, else breakdown from amountPayment
95 if ($amountPayment == '') {
96 // Check if current amount exists in amounts
97 $amountExists = array_key_exists('amount_'.$currentInvId, $amounts);
98 if ($amountExists) {
99 $remainAmount = $currentRemain - $currentAmount; // To keep value between curRemain and curAmount
100 $result += $remainAmount; // result must be deduced by
101 $currentAmount += $remainAmount; // curAmount put to curRemain
102 } else {
103 $currentAmount = $currentRemain;
104 $result += $currentRemain;
105 }
106 } else {
107 // Reset the subtraction for this amount
108 $result += price2num($currentAmount);
109 $currentAmount = 0;
110
111 if ($result >= 0) { // then we need to calculate the amount to breakdown
112 $amountToBreakdown = ($result - $currentRemain >= 0 ?
113 $currentRemain : // Remain can be fully paid
114 $currentRemain + ($result - $currentRemain)); // Remain can only partially be paid
115 $currentAmount = $amountToBreakdown; // In both cases, amount will take breakdown value
116 $result -= $amountToBreakdown; // And canceled subtraction has been replaced by breakdown
117 } // else there's no need to calc anything, just reset the field (result is still < 0)
118 }
119 $toJsonArray['amount_'.$currentInvId] = price2num($currentAmount); // Param will exist only if an img has been clicked
120}
121
122$toJsonArray['makeRed'] = ($totalRemaining < price2num($result) || price2num($result) < 0) ? true : false;
123$toJsonArray['result'] = price($result); // Return value to user format
124$toJsonArray['resultnum'] = price2num($result); // Return value to numeric format
125
126// Encode to JSON to return
127echo 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.