dolibarr/htdocs/compta/ajaxpayment.php

123 lines
4.5 KiB
PHP
Raw Normal View History

2011-09-21 18:56:23 +02:00
<?php
2011-09-21 15:05:32 +02:00
/* Copyright (C) 2011 Auguria <anthony.poiret@auguria.net>
*
* 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
* the Free Software Foundation; either version 3 of the License, or
2011-09-21 15:05:32 +02:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2011-09-21 15:05:32 +02:00
*/
/**
* \file htdocs/compta/ajaxpayment.php
* \brief File to return Ajax response on payment breakdown process
*/
2021-02-23 21:09:01 +01:00
if (!defined('NOREQUIRESOC')) {
define('NOREQUIRESOC', '1');
}
if (!defined('NOCSRFCHECK')) {
define('NOCSRFCHECK', '1');
}
if (!defined('NOTOKENRENEWAL')) {
define('NOTOKENRENEWAL', '1');
}
if (!defined('NOREQUIREMENU')) {
define('NOREQUIREMENU', '1'); // If there is no menu to show
}
if (!defined('NOREQUIREHTML')) {
define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
}
2011-09-21 15:05:32 +02:00
2012-11-20 12:11:19 +01:00
require '../main.inc.php';
2011-09-21 15:05:32 +02:00
2012-11-20 12:11:19 +01:00
$langs->load('compta');
/*
* View
*/
2011-09-21 15:05:32 +02:00
//init var
$invoice_type = GETPOST('invoice_type', 'int');
2022-02-22 23:44:56 +01:00
$amountPayment = GETPOST('amountPayment');
$amounts = GETPOST('amounts'); // from text inputs : invoice amount payment (check required)
$remains = GETPOST('remains'); // from dolibarr's object (no need to check)
$currentInvId = GETPOST('imgClicked'); // from DOM elements : imgId (equals invoice id)
2011-09-21 15:05:32 +02:00
// 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
2012-11-20 12:11:19 +01:00
// Clean checkamounts
2022-02-10 13:16:51 +01:00
if (is_array($amounts)) {
foreach ($amounts as $key => $value) {
$value = price2num($value);
$amounts[$key] = $value;
if (empty($value)) {
unset($amounts[$key]);
}
2021-02-23 21:09:01 +01:00
}
2012-11-20 12:11:19 +01:00
}
// Clean remains
2022-02-10 13:16:51 +01:00
if (is_array($remains)) {
foreach ($remains as $key => $value) {
$value = price2num($value);
$remains[$key] = (($invoice_type) == 2 ?-1 : 1) * $value;
if (empty($value)) {
unset($remains[$key]);
}
2021-02-23 21:09:01 +01:00
}
2012-11-20 12:11:19 +01:00
}
2011-09-21 15:05:32 +02:00
// Treatment
$result = ($amountPayment != '') ? ($amountPayment - array_sum($amounts)) : array_sum($amounts); // Remaining amountPayment
$toJsonArray = array();
2011-09-21 15:05:32 +02:00
$totalRemaining = price2num(array_sum($remains));
$toJsonArray['label'] = $amountPayment == '' ? '' : $langs->transnoentities('RemainingAmountPayment');
2021-02-23 21:09:01 +01:00
if ($currentInvId) { // Here to breakdown
2011-09-21 15:05:32 +02:00
// 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
2021-02-23 21:09:01 +01:00
if ($amountPayment == '') {
2011-09-21 15:05:32 +02:00
// Check if current amount exists in amounts
$amountExists = array_key_exists('amount_'.$currentInvId, $amounts);
2021-02-23 21:09:01 +01:00
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
2020-05-21 01:52:25 +02:00
} else {
2011-09-21 15:05:32 +02:00
$currentAmount = $currentRemain;
$result += $currentRemain;
}
2020-05-21 01:52:25 +02:00
} else {
2011-09-21 15:05:32 +02:00
// Reset the substraction for this amount
$result += price2num($currentAmount);
$currentAmount = 0;
2021-02-23 21:09:01 +01:00
if ($result >= 0) { // then we need to calculate the amount to breakdown
2011-09-21 15:05:32 +02:00
$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 substraction has been replaced by breakdown
2011-09-21 15:05:32 +02:00
} // 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
2011-09-21 15:05:32 +02:00
}
$toJsonArray['makeRed'] = ($totalRemaining < price2num($result) || price2num($result) < 0) ? true : false;
$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