dolibarr  7.0.0-beta
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 {
32  public $element='expenserule';
33  public $table_element='expensereport_rules';
34  public $fk_element='fk_expense_rule';
35 
40  public $dates;
41 
46  public $datee;
47 
52  public $amount;
53 
58  public $restrictive;
59 
64  public $fk_user;
65 
70  public $fk_usergroup;
71 
76  public $fk_c_type_fees;
77 
82  public $code_expense_rules_type;
83 
84 
89  public $is_for_all;
90 
95  public $entity;
96 
97 
98 
103  protected $fields=array(
104  'rowid'=>array('type'=>'integer','index'=>true)
105  ,'dates'=>array('type'=>'date')
106  ,'datee'=>array('type'=>'date')
107  ,'amount'=>array('type'=>'double')
108  ,'restrictive'=>array('type'=>'integer')
109  ,'fk_user'=>array('type'=>'integer')
110  ,'fk_usergroup'=>array('type'=>'integer')
111  ,'fk_c_type_fees'=>array('type'=>'integer')
112  ,'code_expense_rules_type'=>array('type'=>'string')
113  ,'is_for_all'=>array('type'=>'integer')
114  ,'entity'=>array('type'=>'integer')
115  );
116 
122  public function __construct(DoliDB &$db)
123  {
124  global $conf;
125 
126  parent::__construct($db);
127  parent::init();
128 
129  $this->errors = array();
130  }
131 
140  public static function getAllRule($fk_c_type_fees='', $date='', $fk_user='')
141  {
142  global $db;
143 
144  $rules = array();
145  $sql = 'SELECT er.rowid';
146  $sql.= ' FROM '.MAIN_DB_PREFIX.'expensereport_rules er';
147  $sql.= ' WHERE er.entity IN (0,'. getEntity('').')';
148  if (!empty($fk_c_type_fees))
149  {
150  $sql.= ' AND er.fk_c_type_fees IN (-1, '.$fk_c_type_fees.')';
151  }
152  if (!empty($date))
153  {
154  $date = dol_print_date($date, '%Y-%m-%d');
155  $sql.= ' AND er.dates <= \''.$date.'\'';
156  $sql.= ' AND er.datee >= \''.$date.'\'';
157  }
158  if ($fk_user > 0)
159  {
160  $sql.= ' AND (er.is_for_all = 1';
161  $sql.= ' OR er.fk_user = '.$fk_user;
162  $sql.= ' OR er.fk_usergroup IN (SELECT ugu.fk_usergroup FROM '.MAIN_DB_PREFIX.'usergroup_user ugu WHERE ugu.fk_user = '.$fk_user.') )';
163  }
164  $sql.= ' ORDER BY er.is_for_all, er.fk_usergroup, er.fk_user';
165 
166  dol_syslog("ExpenseReportRule::getAllRule sql=".$sql);
167 
168  $resql = $db->query($sql);
169  if ($resql)
170  {
171  while ($obj = $db->fetch_object($resql))
172  {
173  $rule = new ExpenseReportRule($db);
174  if ($rule->fetch($obj->rowid) > 0) $rules[$rule->id] = $rule;
175  else dol_print_error($db);
176  }
177  }
178  else
179  {
180  dol_print_error($db);
181  }
182 
183  return $rules;
184  }
185 
191  public function getGroupLabel()
192  {
193  include_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
194 
195  if ($this->fk_usergroup > 0)
196  {
197  $group = new UserGroup($this->db);
198  if ($group->fetch($this->fk_usergroup) > 0)
199  {
200  return $group->nom;
201  }
202  else
203  {
204  $this->error = $group->error;
205  $this->errors[] = $this->error;
206  }
207  }
208 
209  return '';
210  }
211 
217  public function getUserName()
218  {
219  include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
220 
221  if ($this->fk_user > 0)
222  {
223  $u = new User($this->db);
224  if ($u->fetch($this->fk_user) > 0)
225  {
226  return dolGetFirstLastname($u->firstname, $u->lastname);
227  }
228  else
229  {
230  $this->error = $u->error;
231  $this->errors[] = $this->error;
232  }
233  }
234 
235  return '';
236  }
237 }
Class to manage inventories.
Class to manage Dolibarr users.
Definition: user.class.php:39
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.
__construct(DoliDB &$db)
Constructor.
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
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.
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:1013
getUserName()
Return the name of user for the current object.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.