dolibarr 20.0.0
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28
33{
37 public $element = 'expenserule';
38
42 public $table_element = 'expensereport_rules';
43
47 public $fk_element = 'fk_expense_rule';
48
53 public $dates;
54
59 public $datee;
60
65 public $amount;
66
71 public $restrictive;
72
77 public $fk_user;
78
83 public $fk_usergroup;
84
89 public $fk_c_type_fees;
90
95 public $code_expense_rules_type;
96
101 public $is_for_all;
102
107 public $entity;
108
109
110
115 public $fields = array(
116 'rowid' => array('type' => 'integer', 'index' => 1, 'label' => 'ID', 'enabled' => 1, 'visible' => -1, 'position' => 10),
117 'dates' => array('type' => 'date', 'label' => 'Dates', 'enabled' => 1, 'visible' => -1, 'position' => 20),
118 'datee' => array('type' => 'date', 'label' => 'Datee', 'enabled' => 1, 'visible' => -1, 'position' => 30),
119 'amount' => array('type' => 'double', 'label' => 'Amount', 'enabled' => 1, 'visible' => -1, 'position' => 40),
120 'restrictive' => array('type' => 'integer', 'label' => 'Restrictive', 'enabled' => 1, 'visible' => -1, 'position' => 50),
121 'fk_user' => array('type' => 'integer', 'label' => 'User', 'enabled' => 1, 'visible' => -1, 'position' => 60),
122 'fk_usergroup' => array('type' => 'integer', 'label' => 'Usergroup', 'enabled' => 1, 'visible' => -1, 'position' => 70),
123 'fk_c_type_fees' => array('type' => 'integer', 'label' => 'Type fees', 'enabled' => 1, 'visible' => -1, 'position' => 80),
124 'code_expense_rules_type' => array('type' => 'string', 'label' => 'Expense rule code', 'enabled' => 1, 'visible' => -1, 'position' => 90),
125 'is_for_all' => array('type' => 'integer', 'label' => 'IsForAll', 'enabled' => 1, 'visible' => -1, 'position' => 100),
126 'entity' => array('type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'visible' => -2, 'position' => 110),
127 );
128
129
135 public function __construct(DoliDB $db)
136 {
137 $this->db = $db;
138 }
139
140
148 public function create(User $user, $notrigger = 0)
149 {
150 $resultcreate = $this->createCommon($user, $notrigger);
151
152 //$resultvalidate = $this->validate($user, $notrigger);
153
154 return $resultcreate;
155 }
156
157
165 public function fetch($id, $ref = null)
166 {
167 return $this->fetchCommon($id, $ref);
168 }
169
177 public function update(User $user, $notrigger = 0)
178 {
179 return $this->updateCommon($user, $notrigger);
180 }
181
189 public function delete(User $user, $notrigger = 0)
190 {
191 return $this->deleteCommon($user, $notrigger);
192 //return $this->deleteCommon($user, $notrigger, 1);
193 }
194
195
204 public function getAllRule($fk_c_type_fees = 0, $date = '', $fk_user = 0)
205 {
206 $rules = array();
207
208 $sql = 'SELECT er.rowid';
209 $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_rules er';
210 $sql .= ' WHERE er.entity IN (0,'.getEntity($this->element).')';
211 if (!empty($fk_c_type_fees)) {
212 $sql .= ' AND er.fk_c_type_fees IN (-1, '.((int) $fk_c_type_fees).')';
213 }
214 if (!empty($date)) {
215 $sql .= " AND er.dates <= '".$this->db->idate($date)."'";
216 $sql .= " AND er.datee >= '".$this->db->idate($date)."'";
217 }
218 if ($fk_user > 0) {
219 $sql .= ' AND (er.is_for_all = 1';
220 $sql .= ' OR er.fk_user = '.((int) $fk_user);
221 $sql .= ' OR er.fk_usergroup IN (SELECT ugu.fk_usergroup FROM '.MAIN_DB_PREFIX.'usergroup_user ugu WHERE ugu.fk_user = '.((int) $fk_user).') )';
222 }
223 $sql .= ' ORDER BY er.is_for_all, er.fk_usergroup, er.fk_user';
224
225 dol_syslog("ExpenseReportRule::getAllRule");
226
227 $resql = $this->db->query($sql);
228 if ($resql) {
229 while ($obj = $this->db->fetch_object($resql)) {
230 $rule = new ExpenseReportRule($this->db);
231 if ($rule->fetch($obj->rowid) > 0) {
232 $rules[$rule->id] = $rule;
233 } else {
234 dol_print_error($this->db);
235 }
236 }
237 } else {
238 dol_print_error($this->db);
239 }
240
241 return $rules;
242 }
243
249 public function getGroupLabel()
250 {
251 include_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
252
253 if ($this->fk_usergroup > 0) {
254 $group = new UserGroup($this->db);
255 if ($group->fetch($this->fk_usergroup) > 0) {
256 return $group->name;
257 } else {
258 $this->error = $group->error;
259 $this->errors[] = $this->error;
260 }
261 }
262
263 return '';
264 }
265
271 public function getUserName()
272 {
273 include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
274
275 if ($this->fk_user > 0) {
276 $u = new User($this->db);
277 if ($u->fetch($this->fk_user) > 0) {
278 return dolGetFirstLastname($u->firstname, $u->lastname);
279 } else {
280 $this->error = $u->error;
281 $this->errors[] = $this->error;
282 }
283 }
284
285 return '';
286 }
287}
Parent class of all other business classes (invoices, contracts, proposals, orders,...
createCommon(User $user, $notrigger=0)
Create object in the database.
updateCommon(User $user, $notrigger=0)
Update object into database.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Class to manage Dolibarr database access.
Class to manage inventories.
create(User $user, $notrigger=0)
Create object into database.
getGroupLabel()
Return the label of group for the current object.
getAllRule($fk_c_type_fees=0, $date='', $fk_user=0)
Return all rules or filtered by something.
fetch($id, $ref=null)
Load object in memory from the database.
getUserName()
Return the name of user for the current object.
update(User $user, $notrigger=0)
Update object into database.
__construct(DoliDB $db)
Constructor.
Class to manage user groups.
Class to manage Dolibarr users.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.