dolibarr  9.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  *
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 <http://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT.'/core/class/coreobject.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  protected $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 
133  public function __construct(DoliDB &$db)
134  {
135  global $conf;
136 
137  parent::__construct($db);
138  parent::init();
139 
140  $this->errors = array();
141  }
142 
151  public static function getAllRule($fk_c_type_fees='', $date='', $fk_user='')
152  {
153  global $db;
154 
155  $rules = array();
156  $sql = 'SELECT er.rowid';
157  $sql.= ' FROM '.MAIN_DB_PREFIX.'expensereport_rules er';
158  $sql.= ' WHERE er.entity IN (0,'. getEntity('').')';
159  if (!empty($fk_c_type_fees))
160  {
161  $sql.= ' AND er.fk_c_type_fees IN (-1, '.$fk_c_type_fees.')';
162  }
163  if (!empty($date))
164  {
165  $date = dol_print_date($date, '%Y-%m-%d');
166  $sql.= ' AND er.dates <= \''.$date.'\'';
167  $sql.= ' AND er.datee >= \''.$date.'\'';
168  }
169  if ($fk_user > 0)
170  {
171  $sql.= ' AND (er.is_for_all = 1';
172  $sql.= ' OR er.fk_user = '.$fk_user;
173  $sql.= ' OR er.fk_usergroup IN (SELECT ugu.fk_usergroup FROM '.MAIN_DB_PREFIX.'usergroup_user ugu WHERE ugu.fk_user = '.$fk_user.') )';
174  }
175  $sql.= ' ORDER BY er.is_for_all, er.fk_usergroup, er.fk_user';
176 
177  dol_syslog("ExpenseReportRule::getAllRule sql=".$sql);
178 
179  $resql = $db->query($sql);
180  if ($resql)
181  {
182  while ($obj = $db->fetch_object($resql))
183  {
184  $rule = new ExpenseReportRule($db);
185  if ($rule->fetch($obj->rowid) > 0) $rules[$rule->id] = $rule;
186  else dol_print_error($db);
187  }
188  }
189  else
190  {
191  dol_print_error($db);
192  }
193 
194  return $rules;
195  }
196 
202  public function getGroupLabel()
203  {
204  include_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
205 
206  if ($this->fk_usergroup > 0)
207  {
208  $group = new UserGroup($this->db);
209  if ($group->fetch($this->fk_usergroup) > 0)
210  {
211  return $group->nom;
212  }
213  else
214  {
215  $this->error = $group->error;
216  $this->errors[] = $this->error;
217  }
218  }
219 
220  return '';
221  }
222 
228  public function getUserName()
229  {
230  include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
231 
232  if ($this->fk_user > 0)
233  {
234  $u = new User($this->db);
235  if ($u->fetch($this->fk_user) > 0)
236  {
237  return dolGetFirstLastname($u->firstname, $u->lastname);
238  }
239  else
240  {
241  $this->error = $u->error;
242  $this->errors[] = $this->error;
243  }
244  }
245 
246  return '';
247  }
248 }
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
Class to manage inventories.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class to manage Dolibarr database access.
Class to manage user groups.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
getGroupLabel()
Return the label of group for the current object.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
__construct(DoliDB &$db)
Constructor.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
static getAllRule($fk_c_type_fees='', $date='', $fk_user='')
Return all rules or filtered by something.
getUserName()
Return the name of user for the current object.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.