ajaxpayment.php 4.5 KB

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