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')) define('NOREQUIRESOC', '1'); 24if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1'); 25if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1'); 26if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no menu to show 27if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php 28 29require '../main.inc.php'; 30 31$langs->load('compta'); 32 33 34/* 35 * View 36 */ 37 38//init var 39$invoice_type = GETPOST('invoice_type', 'int'); 40$amountPayment = $_POST['amountPayment']; 41$amounts = $_POST['amounts']; // from text inputs : invoice amount payment (check required) 42$remains = $_POST['remains']; // from dolibarr's object (no need to check) 43$currentInvId = $_POST['imgClicked']; // from DOM elements : imgId (equals invoice id) 44 45// Getting the posted keys=>values, sanitize the ones who are from text inputs 46$amountPayment = $amountPayment != '' ? (is_numeric(price2num($amountPayment)) ? price2num($amountPayment) : '') : ''; // keep void if not a valid entry 47 48// Clean checkamounts 49foreach ($amounts as $key => $value) 50{ 51 $value = price2num($value); 52 $amounts[$key] = $value; 53 if (empty($value)) unset($amounts[$key]); 54} 55// Clean remains 56foreach ($remains as $key => $value) 57{ 58 $value = price2num($value); 59 $remains[$key] = (($invoice_type) == 2 ?-1 : 1) * $value; 60 if (empty($value)) unset($remains[$key]); 61} 62 63// Treatment 64$result = ($amountPayment != '') ? ($amountPayment - array_sum($amounts)) : array_sum($amounts); // Remaining amountPayment 65$toJsonArray = array(); 66$totalRemaining = price2num(array_sum($remains)); 67$toJsonArray['label'] = $amountPayment == '' ? '' : $langs->transnoentities('RemainingAmountPayment'); 68if ($currentInvId) // Here to breakdown 69{ 70 // Get the current amount (from form) and the corresponding remainToPay (from invoice) 71 $currentAmount = $amounts['amount_'.$currentInvId]; 72 $currentRemain = $remains['remain_'.$currentInvId]; 73 74 // If amountPayment isn't filled, breakdown invoice amount, else breakdown from amountPayment 75 if ($amountPayment == '') 76 { 77 // Check if current amount exists in amounts 78 $amountExists = array_key_exists('amount_'.$currentInvId, $amounts); 79 if ($amountExists) 80 { 81 $remainAmount = $currentRemain - $currentAmount; // To keep value between curRemain and curAmount 82 $result += $remainAmount; // result must be deduced by 83 $currentAmount += $remainAmount; // curAmount put to curRemain 84 } else { 85 $currentAmount = $currentRemain; 86 $result += $currentRemain; 87 } 88 } else { 89 // Reset the substraction for this amount 90 $result += price2num($currentAmount); 91 $currentAmount = 0; 92 93 if ($result >= 0) // then we need to calculate the amount to breakdown 94 { 95 $amountToBreakdown = ($result - $currentRemain >= 0 ? 96 $currentRemain : // Remain can be fully paid 97 $currentRemain + ($result - $currentRemain)); // Remain can only partially be paid 98 $currentAmount = $amountToBreakdown; // In both cases, amount will take breakdown value 99 $result -= $amountToBreakdown; // And canceled substraction has been replaced by breakdown 100 } // else there's no need to calc anything, just reset the field (result is still < 0) 101 } 102 $toJsonArray['amount_'.$currentInvId] = price2num($currentAmount).""; // Param will exist only if an img has been clicked 103} 104 105$toJsonArray['makeRed'] = ($totalRemaining < price2num($result) || price2num($result) < 0) ? true : false; 106$toJsonArray['result'] = price($result); // Return value to user format 107$toJsonArray['resultnum'] = price2num($result); // Return value to numeric format 108 109// Encode to JSON to return 110echo json_encode($toJsonArray); // Printing the call's result 111