expensereport_rule.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/coreobject.class.php';
  24. /**
  25. * Class to manage inventories
  26. */
  27. class ExpenseReportRule extends CoreObject
  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. global $conf;
  116. parent::__construct($db);
  117. parent::init();
  118. $this->errors = array();
  119. }
  120. /**
  121. * Return all rules or filtered by something
  122. *
  123. * @param int $fk_c_type_fees type of expense
  124. * @param integer $date date of expense
  125. * @param int $fk_user user of expense
  126. * @return array Array with ExpenseReportRule
  127. */
  128. public static function getAllRule($fk_c_type_fees = '', $date = '', $fk_user = '')
  129. {
  130. global $db;
  131. $rules = array();
  132. $sql = 'SELECT er.rowid';
  133. $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_rules er';
  134. $sql .= ' WHERE er.entity IN (0,'.getEntity('').')';
  135. if (!empty($fk_c_type_fees)) {
  136. $sql .= ' AND er.fk_c_type_fees IN (-1, '.((int) $fk_c_type_fees).')';
  137. }
  138. if (!empty($date)) {
  139. $sql .= " AND er.dates <= '".dol_print_date($date, '%Y-%m-%d')."'";
  140. $sql .= " AND er.datee >= '".dol_print_date($date, '%Y-%m-%d')."'";
  141. }
  142. if ($fk_user > 0) {
  143. $sql .= ' AND (er.is_for_all = 1';
  144. $sql .= ' OR er.fk_user = '.((int) $fk_user);
  145. $sql .= ' OR er.fk_usergroup IN (SELECT ugu.fk_usergroup FROM '.MAIN_DB_PREFIX.'usergroup_user ugu WHERE ugu.fk_user = '.((int) $fk_user).') )';
  146. }
  147. $sql .= ' ORDER BY er.is_for_all, er.fk_usergroup, er.fk_user';
  148. dol_syslog("ExpenseReportRule::getAllRule");
  149. $resql = $db->query($sql);
  150. if ($resql) {
  151. while ($obj = $db->fetch_object($resql)) {
  152. $rule = new ExpenseReportRule($db);
  153. if ($rule->fetch($obj->rowid) > 0) {
  154. $rules[$rule->id] = $rule;
  155. } else {
  156. dol_print_error($db);
  157. }
  158. }
  159. } else {
  160. dol_print_error($db);
  161. }
  162. return $rules;
  163. }
  164. /**
  165. * Return the label of group for the current object
  166. *
  167. * @return string
  168. */
  169. public function getGroupLabel()
  170. {
  171. include_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  172. if ($this->fk_usergroup > 0) {
  173. $group = new UserGroup($this->db);
  174. if ($group->fetch($this->fk_usergroup) > 0) {
  175. return $group->nom;
  176. } else {
  177. $this->error = $group->error;
  178. $this->errors[] = $this->error;
  179. }
  180. }
  181. return '';
  182. }
  183. /**
  184. * Return the name of user for the current object
  185. *
  186. * @return string
  187. */
  188. public function getUserName()
  189. {
  190. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  191. if ($this->fk_user > 0) {
  192. $u = new User($this->db);
  193. if ($u->fetch($this->fk_user) > 0) {
  194. return dolGetFirstLastname($u->firstname, $u->lastname);
  195. } else {
  196. $this->error = $u->error;
  197. $this->errors[] = $this->error;
  198. }
  199. }
  200. return '';
  201. }
  202. }