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