ajaxpayment.php 4.5 KB

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