expensereport_ik.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /* Copyright (C) 2017 ATM Consulting <support@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/class/expensereport_ik.class.php
  20. * \ingroup expenseik
  21. * \brief File of class to manage expense ik
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  24. /**
  25. * Class to manage inventories
  26. */
  27. class ExpenseReportIk extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element = 'expenseik';
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element = 'expensereport_ik';
  37. /**
  38. * @var string Field with ID of parent key if this field has a parent
  39. */
  40. public $fk_element = 'fk_expense_ik';
  41. /**
  42. * c_exp_tax_cat Id
  43. * @var int
  44. */
  45. public $fk_c_exp_tax_cat;
  46. /**
  47. * c_exp_tax_range id
  48. * @var int
  49. */
  50. public $fk_range;
  51. /**
  52. * Coef
  53. * @var double
  54. */
  55. public $coef;
  56. /**
  57. * Offset
  58. * @var double
  59. */
  60. public $ikoffset;
  61. /**
  62. * Attribute object linked with database
  63. * @var array
  64. */
  65. public $fields = array(
  66. 'rowid'=>array('type'=>'integer', 'index'=>true)
  67. ,'fk_c_exp_tax_cat'=>array('type'=>'integer', 'index'=>true)
  68. ,'fk_range'=>array('type'=>'integer', 'index'=>true)
  69. ,'coef'=>array('type'=>'double')
  70. ,'ikoffset'=>array('type'=>'double')
  71. );
  72. /**
  73. * Constructor
  74. *
  75. * @param DoliDB $db Database handler
  76. */
  77. public function __construct(DoliDB $db)
  78. {
  79. $this->db = $db;
  80. }
  81. /**
  82. * Create object into database
  83. *
  84. * @param User $user User that creates
  85. * @param bool $notrigger false=launch triggers after, true=disable triggers
  86. * @return int <0 if KO, Id of created object if OK
  87. */
  88. public function create(User $user, $notrigger = false)
  89. {
  90. $resultcreate = $this->createCommon($user, $notrigger);
  91. //$resultvalidate = $this->validate($user, $notrigger);
  92. return $resultcreate;
  93. }
  94. /**
  95. * Load object in memory from the database
  96. *
  97. * @param int $id Id object
  98. * @param string $ref Ref
  99. * @return int <0 if KO, 0 if not found, >0 if OK
  100. */
  101. public function fetch($id, $ref = null)
  102. {
  103. $result = $this->fetchCommon($id, $ref);
  104. return $result;
  105. }
  106. /**
  107. * Update object into database
  108. *
  109. * @param User $user User that modifies
  110. * @param bool $notrigger false=launch triggers after, true=disable triggers
  111. * @return int <0 if KO, >0 if OK
  112. */
  113. public function update(User $user, $notrigger = false)
  114. {
  115. return $this->updateCommon($user, $notrigger);
  116. }
  117. /**
  118. * Delete object in database
  119. *
  120. * @param User $user User that deletes
  121. * @param bool $notrigger false=launch triggers after, true=disable triggers
  122. * @return int <0 if KO, >0 if OK
  123. */
  124. public function delete(User $user, $notrigger = false)
  125. {
  126. return $this->deleteCommon($user, $notrigger);
  127. //return $this->deleteCommon($user, $notrigger, 1);
  128. }
  129. /**
  130. * Return expense categories in array
  131. *
  132. * @param int $mode 1=only active; 2=only inactive; other value return all
  133. * @return array of category
  134. */
  135. public function getTaxCategories($mode = 1)
  136. {
  137. $categories = array();
  138. $sql = 'SELECT rowid, label, entity, active';
  139. $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_cat';
  140. $sql .= ' WHERE entity IN (0, '.getEntity($this->element).')';
  141. if ($mode == 1) {
  142. $sql .= ' AND active = 1';
  143. } elseif ($mode == 2) {
  144. $sql .= 'AND active = 0';
  145. }
  146. dol_syslog(get_called_class().'::getTaxCategories', LOG_DEBUG);
  147. $resql = $this->db->query($sql);
  148. if ($resql) {
  149. while ($obj = $this->db->fetch_object($resql)) {
  150. $categories[$obj->rowid] = $obj;
  151. }
  152. } else {
  153. dol_print_error($this->db);
  154. }
  155. return $categories;
  156. }
  157. /**
  158. * Return an array of ranges for a user
  159. *
  160. * @param User $userauthor user author id
  161. * @param int $fk_c_exp_tax_cat category
  162. * @return boolean|array
  163. */
  164. public function getRangeByUser(User $userauthor, int $fk_c_exp_tax_cat)
  165. {
  166. $default_range = (int) $userauthor->default_range; // if not defined, then 0
  167. $ranges = $this->getRangesByCategory($fk_c_exp_tax_cat);
  168. // prevent out of range -1 indice
  169. $indice = $default_range - 1;
  170. // substract 1 because array start from 0
  171. if (empty($ranges) || $indice < 0 || !isset($ranges[$indice])) {
  172. return false;
  173. } else {
  174. return $ranges[$indice];
  175. }
  176. }
  177. /**
  178. * Return an array of ranges for a category
  179. *
  180. * @param int $fk_c_exp_tax_cat category id
  181. * @param int $active active
  182. * @return array
  183. */
  184. public function getRangesByCategory(int $fk_c_exp_tax_cat, $active = 1)
  185. {
  186. $ranges = array();
  187. dol_syslog(get_called_class().'::getRangesByCategory for fk_c_exp_tax_cat='.$fk_c_exp_tax_cat, LOG_DEBUG);
  188. $sql = 'SELECT r.rowid FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
  189. if ($active) {
  190. $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
  191. }
  192. $sql .= ' WHERE r.fk_c_exp_tax_cat = '.((int) $fk_c_exp_tax_cat);
  193. $sql .= " AND r.entity IN(0, ".getEntity($this->element).")";
  194. if ($active) {
  195. $sql .= ' AND r.active = 1 AND c.active = 1';
  196. }
  197. $sql .= ' ORDER BY r.range_ik';
  198. $resql = $this->db->query($sql);
  199. if ($resql) {
  200. $num = $this->db->num_rows($resql);
  201. if ($num > 0) {
  202. while ($obj = $this->db->fetch_object($resql)) {
  203. $object = new ExpenseReportIk($this->db);
  204. $object->fetch($obj->rowid);
  205. $ranges[] = $object;
  206. }
  207. }
  208. } else {
  209. dol_print_error($this->db);
  210. }
  211. return $ranges;
  212. }
  213. /**
  214. * Return an array of ranges grouped by category
  215. *
  216. * @return array
  217. */
  218. public function getAllRanges()
  219. {
  220. $ranges = array();
  221. $sql = ' SELECT r.rowid, r.fk_c_exp_tax_cat, r.range_ik, c.label, i.rowid as fk_expense_ik, r.active as range_active, c.active as cat_active';
  222. $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
  223. $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
  224. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'expensereport_ik i ON (r.rowid = i.fk_range)';
  225. $sql .= ' WHERE r.entity IN (0, '.getEntity($this->element).')';
  226. $sql .= ' ORDER BY r.fk_c_exp_tax_cat, r.range_ik';
  227. dol_syslog(get_called_class().'::getAllRanges', LOG_DEBUG);
  228. $resql = $this->db->query($sql);
  229. if ($resql) {
  230. while ($obj = $this->db->fetch_object($resql)) {
  231. $ik = new ExpenseReportIk($this->db);
  232. if ($obj->fk_expense_ik > 0) {
  233. $ik->fetch($obj->fk_expense_ik);
  234. }
  235. $obj->ik = $ik;
  236. if (!isset($ranges[$obj->fk_c_exp_tax_cat])) {
  237. $ranges[$obj->fk_c_exp_tax_cat] = array('label' => $obj->label, 'active' => $obj->cat_active, 'ranges' => array());
  238. }
  239. $ranges[$obj->fk_c_exp_tax_cat]['ranges'][] = $obj;
  240. }
  241. } else {
  242. dol_print_error($this->db);
  243. }
  244. return $ranges;
  245. }
  246. /**
  247. * Return the max number of range by a category
  248. *
  249. * @param int $default_c_exp_tax_cat id Default c_exp_tax_cat
  250. * @return int Max nb
  251. */
  252. public function getMaxRangeNumber($default_c_exp_tax_cat = 0)
  253. {
  254. $sql = 'SELECT MAX(counted) as nbRange FROM (';
  255. $sql .= ' SELECT COUNT(*) as counted';
  256. $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
  257. $sql .= ' WHERE r.entity IN (0, '.getEntity($this->element).')';
  258. if ($default_c_exp_tax_cat > 0) {
  259. $sql .= ' AND r.fk_c_exp_tax_cat = '.((int) $default_c_exp_tax_cat);
  260. }
  261. $sql .= ' GROUP BY r.fk_c_exp_tax_cat';
  262. $sql .= ') as counts';
  263. dol_syslog(get_called_class().'::getMaxRangeNumber', LOG_DEBUG);
  264. $resql = $this->db->query($sql);
  265. if ($resql) {
  266. $obj = $this->db->fetch_object($resql);
  267. return $obj->nbRange;
  268. } else {
  269. dol_print_error($this->db);
  270. }
  271. return 0;
  272. }
  273. }