dolibarr  16.0.5
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 
25 require_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 
181  public function update(User $user, $notrigger = false)
182  {
183  return $this->updateCommon($user, $notrigger);
184  }
185 
193  public function delete(User $user, $notrigger = false)
194  {
195  return $this->deleteCommon($user, $notrigger);
196  //return $this->deleteCommon($user, $notrigger, 1);
197  }
198 
199 
208  public function getAllRule($fk_c_type_fees = '', $date = '', $fk_user = '')
209  {
210  $rules = array();
211 
212  $sql = 'SELECT er.rowid';
213  $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_rules er';
214  $sql .= ' WHERE er.entity IN (0,'.getEntity($this->element).')';
215  if (!empty($fk_c_type_fees)) {
216  $sql .= ' AND er.fk_c_type_fees IN (-1, '.((int) $fk_c_type_fees).')';
217  }
218  if (!empty($date)) {
219  $sql .= " AND er.dates <= '".$this->db->idate($date)."'";
220  $sql .= " AND er.datee >= '".$this->db->idate($date)."'";
221  }
222  if ($fk_user > 0) {
223  $sql .= ' AND (er.is_for_all = 1';
224  $sql .= ' OR er.fk_user = '.((int) $fk_user);
225  $sql .= ' OR er.fk_usergroup IN (SELECT ugu.fk_usergroup FROM '.MAIN_DB_PREFIX.'usergroup_user ugu WHERE ugu.fk_user = '.((int) $fk_user).') )';
226  }
227  $sql .= ' ORDER BY er.is_for_all, er.fk_usergroup, er.fk_user';
228 
229  dol_syslog("ExpenseReportRule::getAllRule");
230 
231  $resql = $this->db->query($sql);
232  if ($resql) {
233  while ($obj = $this->db->fetch_object($resql)) {
234  $rule = new ExpenseReportRule($this->db);
235  if ($rule->fetch($obj->rowid) > 0) {
236  $rules[$rule->id] = $rule;
237  } else {
238  dol_print_error($this->db);
239  }
240  }
241  } else {
242  dol_print_error($this->db);
243  }
244 
245  return $rules;
246  }
247 
253  public function getGroupLabel()
254  {
255  include_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
256 
257  if ($this->fk_usergroup > 0) {
258  $group = new UserGroup($this->db);
259  if ($group->fetch($this->fk_usergroup) > 0) {
260  return $group->name;
261  } else {
262  $this->error = $group->error;
263  $this->errors[] = $this->error;
264  }
265  }
266 
267  return '';
268  }
269 
275  public function getUserName()
276  {
277  include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
278 
279  if ($this->fk_user > 0) {
280  $u = new User($this->db);
281  if ($u->fetch($this->fk_user) > 0) {
282  return dolGetFirstLastname($u->firstname, $u->lastname);
283  } else {
284  $this->error = $u->error;
285  $this->errors[] = $this->error;
286  }
287  }
288 
289  return '';
290  }
291 }
CommonObject\deleteCommon
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
Definition: commonobject.class.php:9406
ExpenseReportRule\getAllRule
getAllRule($fk_c_type_fees='', $date='', $fk_user='')
Return all rules or filtered by something.
Definition: expensereport_rule.class.php:208
db
$conf db
API class for accounts.
Definition: inc.php:41
CommonObject\fetchCommon
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
Definition: commonobject.class.php:9202
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
ExpenseReportRule\fetch
fetch($id, $ref=null)
Load object in memory from the database.
Definition: expensereport_rule.class.php:164
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
UserGroup
Class to manage user groups.
Definition: usergroup.class.php:39
ExpenseReportRule\getUserName
getUserName()
Return the name of user for the current object.
Definition: expensereport_rule.class.php:275
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
ExpenseReportRule\create
create(User $user, $notrigger=false)
Create object into database.
Definition: expensereport_rule.class.php:147
CommonObject\createCommon
createCommon(User $user, $notrigger=false)
Create object into database.
Definition: commonobject.class.php:9035
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
CommonObject\updateCommon
updateCommon(User $user, $notrigger=false)
Update object into database.
Definition: commonobject.class.php:9308
ExpenseReportRule\update
update(User $user, $notrigger=false)
Update object into database.
Definition: expensereport_rule.class.php:181
dolGetFirstLastname
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
Definition: functions.lib.php:8062
User
Class to manage Dolibarr users.
Definition: user.class.php:44
ExpenseReportRule\__construct
__construct(DoliDB $db)
Constructor.
Definition: expensereport_rule.class.php:134
ExpenseReportRule
Class to manage inventories.
Definition: expensereport_rule.class.php:30
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
ExpenseReportRule\getGroupLabel
getGroupLabel()
Return the label of group for the current object.
Definition: expensereport_rule.class.php:253