23 if (!defined(
'NOREQUIRESOC')) define(
'NOREQUIRESOC',
'1');
24 if (!defined(
'NOCSRFCHECK')) define(
'NOCSRFCHECK',
'1');
25 if (!defined(
'NOTOKENRENEWAL')) define(
'NOTOKENRENEWAL',
'1');
26 if (!defined(
'NOREQUIREMENU')) define(
'NOREQUIREMENU',
'1');
27 if (!defined(
'NOREQUIREHTML')) define(
'NOREQUIREHTML',
'1');
29 require
'../main.inc.php';
31 $langs->load(
'compta');
39 $invoice_type =
GETPOST(
'invoice_type',
'int');
40 $amountPayment = $_POST[
'amountPayment'];
41 $amounts = $_POST[
'amounts'];
42 $remains = $_POST[
'remains'];
43 $currentInvId = $_POST[
'imgClicked'];
46 $amountPayment = $amountPayment !=
'' ? (is_numeric(
price2num($amountPayment)) ?
price2num($amountPayment) :
'') :
'';
49 foreach ($amounts as $key => $value)
52 $amounts[$key] = $value;
53 if (empty($value)) unset($amounts[$key]);
56 foreach ($remains as $key => $value)
59 $remains[$key] = (($invoice_type) == 2 ?-1 : 1) * $value;
60 if (empty($value)) unset($remains[$key]);
64 $result = ($amountPayment !=
'') ? ($amountPayment - array_sum($amounts)) : array_sum($amounts);
65 $toJsonArray = array();
66 $totalRemaining =
price2num(array_sum($remains));
67 $toJsonArray[
'label'] = $amountPayment ==
'' ?
'' : $langs->transnoentities(
'RemainingAmountPayment');
71 $currentAmount = $amounts[
'amount_'.$currentInvId];
72 $currentRemain = $remains[
'remain_'.$currentInvId];
75 if ($amountPayment ==
'')
78 $amountExists = array_key_exists(
'amount_'.$currentInvId, $amounts);
81 $remainAmount = $currentRemain - $currentAmount;
82 $result += $remainAmount;
83 $currentAmount += $remainAmount;
85 $currentAmount = $currentRemain;
86 $result += $currentRemain;
95 $amountToBreakdown = ($result - $currentRemain >= 0 ?
97 $currentRemain + ($result - $currentRemain));
98 $currentAmount = $amountToBreakdown;
99 $result -= $amountToBreakdown;
102 $toJsonArray[
'amount_'.$currentInvId] =
price2num($currentAmount).
"";
105 $toJsonArray[
'makeRed'] = ($totalRemaining <
price2num($result) ||
price2num($result) < 0) ?
true :
false;
106 $toJsonArray[
'result'] =
price($result);
107 $toJsonArray[
'resultnum'] =
price2num($result);
110 echo json_encode($toJsonArray);
GETPOST($paramname, $check= 'alphanohtml', $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= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...