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