dolibarr 18.0.6
expensereport_rule.class.php
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
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
31{
35 public $element = 'expenserule';
36
40 public $table_element = 'expensereport_rules';
41
45 public $fk_element = 'fk_expense_rule';
46
51 public $dates;
52
57 public $datee;
58
63 public $amount;
64
69 public $restrictive;
70
75 public $fk_user;
76
81 public $fk_usergroup;
82
87 public $fk_c_type_fees;
88
93 public $code_expense_rules_type;
94
95
100 public $is_for_all;
101
106 public $entity;
107
108
109
114 public $fields = array(
115 'rowid'=>array('type'=>'integer', 'index'=>true)
116 ,'dates'=>array('type'=>'date')
117 ,'datee'=>array('type'=>'date')
118 ,'amount'=>array('type'=>'double')
119 ,'restrictive'=>array('type'=>'integer')
120 ,'fk_user'=>array('type'=>'integer')
121 ,'fk_usergroup'=>array('type'=>'integer')
122 ,'fk_c_type_fees'=>array('type'=>'integer')
123 ,'code_expense_rules_type'=>array('type'=>'string')
124 ,'is_for_all'=>array('type'=>'integer')
125 ,'entity'=>array('type'=>'integer')
126 );
127
128
134 public function __construct(DoliDB $db)
135 {
136 $this->db = $db;
137 }
138
139
147 public function create(User $user, $notrigger = false)
148 {
149 $resultcreate = $this->createCommon($user, $notrigger);
150
151 //$resultvalidate = $this->validate($user, $notrigger);
152
153 return $resultcreate;
154 }
155
156
164 public function fetch($id, $ref = null)
165 {
166 $result = $this->fetchCommon($id, $ref);
167 if ($result > 0 && !empty($this->table_element_line)) {
168 $this->fetchLines();
169 }
170 return $result;
171 }
172
173
179 public function fetchLines()
180 {
181 $this->lines = array();
182
183 $result = $this->fetchLinesCommon();
184 return $result;
185 }
186
194 public function update(User $user, $notrigger = false)
195 {
196 return $this->updateCommon($user, $notrigger);
197 }
198
206 public function delete(User $user, $notrigger = false)
207 {
208 return $this->deleteCommon($user, $notrigger);
209 //return $this->deleteCommon($user, $notrigger, 1);
210 }
211
212
221 public function getAllRule($fk_c_type_fees = '', $date = '', $fk_user = '')
222 {
223 $rules = array();
224
225 $sql = 'SELECT er.rowid';
226 $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_rules er';
227 $sql .= ' WHERE er.entity IN (0,'.getEntity($this->element).')';
228 if (!empty($fk_c_type_fees)) {
229 $sql .= ' AND er.fk_c_type_fees IN (-1, '.((int) $fk_c_type_fees).')';
230 }
231 if (!empty($date)) {
232 $sql .= " AND er.dates <= '".$this->db->idate($date)."'";
233 $sql .= " AND er.datee >= '".$this->db->idate($date)."'";
234 }
235 if ($fk_user > 0) {
236 $sql .= ' AND (er.is_for_all = 1';
237 $sql .= ' OR er.fk_user = '.((int) $fk_user);
238 $sql .= ' OR er.fk_usergroup IN (SELECT ugu.fk_usergroup FROM '.MAIN_DB_PREFIX.'usergroup_user ugu WHERE ugu.fk_user = '.((int) $fk_user).') )';
239 }
240 $sql .= ' ORDER BY er.is_for_all, er.fk_usergroup, er.fk_user';
241
242 dol_syslog("ExpenseReportRule::getAllRule");
243
244 $resql = $this->db->query($sql);
245 if ($resql) {
246 while ($obj = $this->db->fetch_object($resql)) {
247 $rule = new ExpenseReportRule($this->db);
248 if ($rule->fetch($obj->rowid) > 0) {
249 $rules[$rule->id] = $rule;
250 } else {
251 dol_print_error($this->db);
252 }
253 }
254 } else {
255 dol_print_error($this->db);
256 }
257
258 return $rules;
259 }
260
266 public function getGroupLabel()
267 {
268 include_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
269
270 if ($this->fk_usergroup > 0) {
271 $group = new UserGroup($this->db);
272 if ($group->fetch($this->fk_usergroup) > 0) {
273 return $group->name;
274 } else {
275 $this->error = $group->error;
276 $this->errors[] = $this->error;
277 }
278 }
279
280 return '';
281 }
282
288 public function getUserName()
289 {
290 include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
291
292 if ($this->fk_user > 0) {
293 $u = new User($this->db);
294 if ($u->fetch($this->fk_user) > 0) {
295 return dolGetFirstLastname($u->firstname, $u->lastname);
296 } else {
297 $this->error = $u->error;
298 $this->errors[] = $this->error;
299 }
300 }
301
302 return '';
303 }
304}
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
createCommon(User $user, $notrigger=false)
Create object into database.
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
updateCommon(User $user, $notrigger=false)
Update object into database.
fetchLinesCommon($morewhere='')
Load object in memory from the database.
Class to manage Dolibarr database access.
Class to manage inventories.
getAllRule($fk_c_type_fees='', $date='', $fk_user='')
Return all rules or filtered by something.
getGroupLabel()
Return the label of group for the current object.
create(User $user, $notrigger=false)
Create object into database.
fetch($id, $ref=null)
Load object in memory from the database.
getUserName()
Return the name of user for the current object.
__construct(DoliDB $db)
Constructor.
fetchLines()
Load object lines in memory from the database.
update(User $user, $notrigger=false)
Update object into database.
Class to manage user groups.
Class to manage Dolibarr users.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.