ajaxik.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /* Copyright (C) 2017 ATM Consulting <contact@atm-consulting.fr>
  3. * Copyright (C) 2017 Pierre-Henry Favre <phf@atm-consulting.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/expensereport/ajax/ajaxik.php
  20. * \ingroup expensereport
  21. * \brief File to return Ajax response on third parties request
  22. */
  23. if (!defined('NOTOKENRENEWAL')) {
  24. define('NOTOKENRENEWAL', 1); // Disables token renewal
  25. }
  26. if (!defined('NOREQUIREMENU')) {
  27. define('NOREQUIREMENU', '1');
  28. }
  29. if (!defined('NOREQUIREHTML')) {
  30. define('NOREQUIREHTML', '1');
  31. }
  32. if (!defined('NOREQUIREAJAX')) {
  33. define('NOREQUIREAJAX', '1');
  34. }
  35. if (!defined('NOREQUIRESOC')) {
  36. define('NOREQUIRESOC', '1');
  37. }
  38. $res = 0;
  39. require '../../main.inc.php';
  40. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_ik.class.php';
  42. // Load translation files required by the page
  43. $langs->loadlangs(array('errors', 'trips'));
  44. $fk_expense = GETPOST('fk_expense', 'int');
  45. $fk_c_exp_tax_cat = GETPOST('fk_c_exp_tax_cat', 'int');
  46. $vatrate = GETPOST('vatrate', 'int');
  47. $qty = GETPOST('qty', 'int');
  48. // Security check
  49. $result = restrictedArea($user, 'expensereport', $fk_expense, 'expensereport');
  50. /*
  51. * View
  52. */
  53. top_httphead();
  54. $rep = new stdClass();
  55. $rep->response_status = 0;
  56. $rep->data = null;
  57. $rep->error = '';//@todo deprecated use error_message instead
  58. $rep->errorMessage = '';
  59. if (empty($fk_expense) || $fk_expense < 0) {
  60. $rep->errorMessage = $langs->transnoentitiesnoconv('ErrorBadValueForParameter', $fk_expense, 'fk_expense');
  61. } elseif (empty($fk_c_exp_tax_cat) || $fk_c_exp_tax_cat < 0) {
  62. $rep->errorMessage = $langs->transnoentitiesnoconv('ErrorBadValueForParameter', $fk_c_exp_tax_cat, 'fk_c_exp_tax_cat');
  63. $rep->response_status = 'error';
  64. } else {
  65. // @see ndfp.class.php:3576 (method: compute_total_km)
  66. $expense = new ExpenseReport($db);
  67. if ($expense->fetch($fk_expense) <= 0) {
  68. $rep->errorMessage = $langs->transnoentitiesnoconv('ErrorRecordNotFound');
  69. $rep->response_status = 'error';
  70. } else {
  71. $userauthor = new User($db);
  72. if ($userauthor->fetch($expense->fk_user_author) <= 0) {
  73. $rep->errorMessage = $langs->transnoentitiesnoconv('ErrorRecordNotFound');
  74. $rep->response_status = 'error';
  75. } else {
  76. $expense = new ExpenseReport($db);
  77. $result = $expense->fetch($fk_expense);
  78. if ($result) {
  79. $result = $expense->computeTotalKm($fk_c_exp_tax_cat, $qty, $vatrate);
  80. if ($result < 0) {
  81. $rep->error = $result;
  82. $rep->errorMessage = $langs->trans('errorComputeTtcOnMileageExpense');
  83. $rep->response_status = 'error';
  84. } else {
  85. $rep->data = $result;
  86. $rep->response_status = 'success';
  87. }
  88. }
  89. }
  90. }
  91. }
  92. echo json_encode($rep);