accountancyimport.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  7. * Copyright (C) 2016 Pierre-Henry Favre <phf@atm-consulting.fr>
  8. * Copyright (C) 2016-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
  9. * Copyright (C) 2013-2017 Olivier Geffroy <jeff@jeffinfo.com>
  10. * Copyright (C) 2017 Elarifr. Ari Elbaz <github@accedinfo.com>
  11. * Copyright (C) 2017-2019 Frédéric France <frederic.france@netlogic.fr>
  12. * Copyright (C) 2017 André Schild <a.schild@aarboard.ch>
  13. * Copyright (C) 2020 Guillaume Alexandre <guillaume@tag-info.fr>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  27. */
  28. /**
  29. * \file htdocs/accountancy/class/accountancyimport.class.php
  30. * \ingroup Accountancy (Double entries)
  31. * \brief Class with methods for accountancy import
  32. */
  33. /**
  34. * Manage the different format accountancy import
  35. */
  36. class AccountancyImport
  37. {
  38. /**
  39. * @var DoliDB Database handler
  40. */
  41. public $db;
  42. /**
  43. * Constructor
  44. *
  45. * @param DoliDb $db Database handler
  46. */
  47. public function __construct(DoliDB $db)
  48. {
  49. $this->db = $db;
  50. }
  51. /**
  52. * Clean amount
  53. *
  54. * @param array $arrayrecord Array of read values: [fieldpos] => (['val']=>val, ['type']=>-1=null,0=blank,1=string), [fieldpos+1]...
  55. * @param array $listfields Fields list to add
  56. * @param int $record_key Record key
  57. * @return mixed Value
  58. */
  59. public function cleanAmount(&$arrayrecord, $listfields, $record_key)
  60. {
  61. $value_trim = trim($arrayrecord[$record_key]['val']);
  62. return floatval($value_trim);
  63. }
  64. /**
  65. * Clean value with trim
  66. *
  67. * @param array $arrayrecord Array of read values: [fieldpos] => (['val']=>val, ['type']=>-1=null,0=blank,1=string), [fieldpos+1]...
  68. * @param array $listfields Fields list to add
  69. * @param int $record_key Record key
  70. * @return mixed Value
  71. */
  72. public function cleanValue(&$arrayrecord, $listfields, $record_key)
  73. {
  74. return trim($arrayrecord[$record_key]['val']);
  75. }
  76. /**
  77. * Compute amount
  78. *
  79. * @param array $arrayrecord Array of read values: [fieldpos] => (['val']=>val, ['type']=>-1=null,0=blank,1=string), [fieldpos+1]...
  80. * @param array $listfields Fields list to add
  81. * @param int $record_key Record key
  82. * @return mixed Value
  83. */
  84. public function computeAmount(&$arrayrecord, $listfields, $record_key)
  85. {
  86. // get fields indexes
  87. $field_index_list = array_flip($listfields);
  88. if (isset($field_index_list['debit']) && isset($field_index_list['credit'])) {
  89. $debit_index = $field_index_list['debit'];
  90. $credit_index = $field_index_list['credit'];
  91. $debit = floatval($arrayrecord[$debit_index]['val']);
  92. $credit = floatval($arrayrecord[$credit_index]['val']);
  93. if (!empty($debit)) {
  94. $amount = $debit;
  95. } else {
  96. $amount = $credit;
  97. }
  98. return "'" . $this->db->escape(abs($amount)) . "'";
  99. }
  100. return "''";
  101. }
  102. /**
  103. * Compute direction
  104. *
  105. * @param array $arrayrecord Array of read values: [fieldpos] => (['val']=>val, ['type']=>-1=null,0=blank,1=string), [fieldpos+1]...
  106. * @param array $listfields Fields list to add
  107. * @param int $record_key Record key
  108. * @return mixed Value
  109. */
  110. public function computeDirection(&$arrayrecord, $listfields, $record_key)
  111. {
  112. $field_index_list = array_flip($listfields);
  113. if (isset($field_index_list['debit'])) {
  114. $debit_index = $field_index_list['debit'];
  115. $debit = floatval($arrayrecord[$debit_index]['val']);
  116. if (!empty($debit)) {
  117. $sens = 'D';
  118. } else {
  119. $sens = 'C';
  120. }
  121. return "'" . $this->db->escape($sens) . "'";
  122. }
  123. return "''";
  124. }
  125. }