expensereport_rule.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 ExpenseReportRule extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element = 'expenserule';
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element = 'expensereport_rules';
  37. /**
  38. * @var string Fieldname with ID of parent key if this field has a parent
  39. */
  40. public $fk_element = 'fk_expense_rule';
  41. /**
  42. * date start
  43. * @var int|string
  44. */
  45. public $dates;
  46. /**
  47. * date end
  48. * @var int|string
  49. */
  50. public $datee;
  51. /**
  52. * amount
  53. * @var double
  54. */
  55. public $amount;
  56. /**
  57. * restrective
  58. * @var int
  59. */
  60. public $restrictive;
  61. /**
  62. * rule for user
  63. * @var int
  64. */
  65. public $fk_user;
  66. /**
  67. * rule for group
  68. * @var int
  69. */
  70. public $fk_usergroup;
  71. /**
  72. * c_type_fees id
  73. * @var int
  74. */
  75. public $fk_c_type_fees;
  76. /**
  77. * code type of expense report
  78. * @var string
  79. */
  80. public $code_expense_rules_type;
  81. /**
  82. * rule for all
  83. * @var int
  84. */
  85. public $is_for_all;
  86. /**
  87. * entity
  88. * @var int
  89. */
  90. public $entity;
  91. /**
  92. * Attribute object linked with database
  93. * @var array
  94. */
  95. public $fields = array(
  96. 'rowid'=>array('type'=>'integer', 'index'=>true)
  97. ,'dates'=>array('type'=>'date')
  98. ,'datee'=>array('type'=>'date')
  99. ,'amount'=>array('type'=>'double')
  100. ,'restrictive'=>array('type'=>'integer')
  101. ,'fk_user'=>array('type'=>'integer')
  102. ,'fk_usergroup'=>array('type'=>'integer')
  103. ,'fk_c_type_fees'=>array('type'=>'integer')
  104. ,'code_expense_rules_type'=>array('type'=>'string')
  105. ,'is_for_all'=>array('type'=>'integer')
  106. ,'entity'=>array('type'=>'integer')
  107. );
  108. /**
  109. * Constructor
  110. *
  111. * @param DoliDB $db Database handler
  112. */
  113. public function __construct(DoliDB $db)
  114. {
  115. $this->db = $db;
  116. }
  117. /**
  118. * Create object into database
  119. *
  120. * @param User $user User that creates
  121. * @param bool $notrigger false=launch triggers after, true=disable triggers
  122. * @return int <0 if KO, Id of created object if OK
  123. */
  124. public function create(User $user, $notrigger = false)
  125. {
  126. $resultcreate = $this->createCommon($user, $notrigger);
  127. //$resultvalidate = $this->validate($user, $notrigger);
  128. return $resultcreate;
  129. }
  130. /**
  131. * Load object in memory from the database
  132. *
  133. * @param int $id Id object
  134. * @param string $ref Ref
  135. * @return int <0 if KO, 0 if not found, >0 if OK
  136. */
  137. public function fetch($id, $ref = null)
  138. {
  139. $result = $this->fetchCommon($id, $ref);
  140. if ($result > 0 && !empty($this->table_element_line)) {
  141. $this->fetchLines();
  142. }
  143. return $result;
  144. }
  145. /**
  146. * Load object lines in memory from the database
  147. *
  148. * @return int <0 if KO, 0 if not found, >0 if OK
  149. */
  150. public function fetchLines()
  151. {
  152. $this->lines = array();
  153. $result = $this->fetchLinesCommon();
  154. return $result;
  155. }
  156. /**
  157. * Update object into database
  158. *
  159. * @param User $user User that modifies
  160. * @param bool $notrigger false=launch triggers after, true=disable triggers
  161. * @return int <0 if KO, >0 if OK
  162. */
  163. public function update(User $user, $notrigger = false)
  164. {
  165. return $this->updateCommon($user, $notrigger);
  166. }
  167. /**
  168. * Delete object in database
  169. *
  170. * @param User $user User that deletes
  171. * @param bool $notrigger false=launch triggers after, true=disable triggers
  172. * @return int <0 if KO, >0 if OK
  173. */
  174. public function delete(User $user, $notrigger = false)
  175. {
  176. return $this->deleteCommon($user, $notrigger);
  177. //return $this->deleteCommon($user, $notrigger, 1);
  178. }
  179. /**
  180. * Return all rules or filtered by something
  181. *
  182. * @param int $fk_c_type_fees type of expense
  183. * @param integer $date date of expense
  184. * @param int $fk_user user of expense
  185. * @return array Array with ExpenseReportRule
  186. */
  187. public function getAllRule($fk_c_type_fees = '', $date = '', $fk_user = '')
  188. {
  189. $rules = array();
  190. $sql = 'SELECT er.rowid';
  191. $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_rules er';
  192. $sql .= ' WHERE er.entity IN (0,'.getEntity($this->element).')';
  193. if (!empty($fk_c_type_fees)) {
  194. $sql .= ' AND er.fk_c_type_fees IN (-1, '.((int) $fk_c_type_fees).')';
  195. }
  196. if (!empty($date)) {
  197. $sql .= " AND er.dates <= '".$this->db->idate($date)."'";
  198. $sql .= " AND er.datee >= '".$this->db->idate($date)."'";
  199. }
  200. if ($fk_user > 0) {
  201. $sql .= ' AND (er.is_for_all = 1';
  202. $sql .= ' OR er.fk_user = '.((int) $fk_user);
  203. $sql .= ' OR er.fk_usergroup IN (SELECT ugu.fk_usergroup FROM '.MAIN_DB_PREFIX.'usergroup_user ugu WHERE ugu.fk_user = '.((int) $fk_user).') )';
  204. }
  205. $sql .= ' ORDER BY er.is_for_all, er.fk_usergroup, er.fk_user';
  206. dol_syslog("ExpenseReportRule::getAllRule");
  207. $resql = $this->db->query($sql);
  208. if ($resql) {
  209. while ($obj = $this->db->fetch_object($resql)) {
  210. $rule = new ExpenseReportRule($this->db);
  211. if ($rule->fetch($obj->rowid) > 0) {
  212. $rules[$rule->id] = $rule;
  213. } else {
  214. dol_print_error($this->db);
  215. }
  216. }
  217. } else {
  218. dol_print_error($this->db);
  219. }
  220. return $rules;
  221. }
  222. /**
  223. * Return the label of group for the current object
  224. *
  225. * @return string
  226. */
  227. public function getGroupLabel()
  228. {
  229. include_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  230. if ($this->fk_usergroup > 0) {
  231. $group = new UserGroup($this->db);
  232. if ($group->fetch($this->fk_usergroup) > 0) {
  233. return $group->name;
  234. } else {
  235. $this->error = $group->error;
  236. $this->errors[] = $this->error;
  237. }
  238. }
  239. return '';
  240. }
  241. /**
  242. * Return the name of user for the current object
  243. *
  244. * @return string
  245. */
  246. public function getUserName()
  247. {
  248. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  249. if ($this->fk_user > 0) {
  250. $u = new User($this->db);
  251. if ($u->fetch($this->fk_user) > 0) {
  252. return dolGetFirstLastname($u->firstname, $u->lastname);
  253. } else {
  254. $this->error = $u->error;
  255. $this->errors[] = $this->error;
  256. }
  257. }
  258. return '';
  259. }
  260. }