dolibarr  9.0.0
expensereport_ik.class.php
Go to the documentation of this file.
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='expenseik';
36 
40  public $table_element='expensereport_ik';
41 
45  public $fk_element='fk_expense_ik';
46 
51  public $fk_c_exp_tax_cat;
52 
57  public $fk_range;
58 
63  public $coef;
64 
69  public $ikoffset;
70 
75  protected $fields=array(
76  'rowid'=>array('type'=>'integer','index'=>true)
77  ,'fk_c_exp_tax_cat'=>array('type'=>'integer','index'=>true)
78  ,'fk_range'=>array('type'=>'integer','index'=>true)
79  ,'coef'=>array('type'=>'double')
80  ,'ikoffset'=>array('type'=>'double')
81  );
82 
88  public function __construct(DoliDB &$db)
89  {
90  global $conf;
91 
92  parent::__construct($db);
93  parent::init();
94 
95  $this->errors = array();
96  }
97 
98 
105  public static function getTaxCategories($mode=1)
106  {
107  global $db;
108 
109  $categories = array();
110 
111  $sql = 'SELECT rowid, label, entity, active';
112  $sql.= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_cat';
113  $sql.= ' WHERE entity IN ('. getEntity('c_exp_tax_cat').')';
114  if ($mode == 1) $sql.= ' AND active = 1';
115  elseif ($mode == 2) $sql.= 'AND active = 0';
116 
117  dol_syslog(get_called_class().'::getTaxCategories sql='.$sql, LOG_DEBUG);
118  $resql = $db->query($sql);
119  if ($resql)
120  {
121  while ($obj = $db->fetch_object($resql))
122  {
123  $categories[$obj->rowid] = $obj;
124  }
125  }
126  else
127  {
128  dol_print_error($db);
129  }
130 
131  return $categories;
132  }
133 
141  public static function getRangeByUser(User $userauthor, $fk_c_exp_tax_cat)
142  {
143  $default_range = (int) $userauthor->default_range; // if not defined, then 0
144  $ranges = self::getRangesByCategory($fk_c_exp_tax_cat);
145 
146  // substract 1 because array start from 0
147  if (empty($ranges) || !isset($ranges[$default_range-1])) return false;
148  else return $ranges[$default_range-1];
149  }
150 
158  public static function getRangesByCategory($fk_c_exp_tax_cat, $active=1)
159  {
160  global $db;
161 
162  $ranges = array();
163 
164  $sql = 'SELECT r.rowid FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
165  if ($active) $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
166  $sql.= ' WHERE r.fk_c_exp_tax_cat = '.$fk_c_exp_tax_cat;
167  if ($active) $sql.= ' AND r.active = 1 AND c.active = 1';
168  $sql.= ' ORDER BY r.range_ik';
169 
170  dol_syslog(get_called_class().'::getRangesByCategory sql='.$sql, LOG_DEBUG);
171  $resql = $db->query($sql);
172  if ($resql)
173  {
174  $num = $db->num_rows($resql);
175  if ($num > 0)
176  {
177  while ($obj = $db->fetch_object($resql))
178  {
179  $object = new ExpenseReportIk($db);
180  $object->fetch($obj->rowid);
181 
182  $ranges[] = $object;
183  }
184  }
185  }
186  else
187  {
188  dol_print_error($db);
189  }
190 
191  return $ranges;
192  }
193 
199  public static function getAllRanges()
200  {
201  global $db;
202 
203  $ranges = array();
204 
205  $sql = ' SELECT r.rowid, r.fk_c_exp_tax_cat, r.range_ik, c.label, i.rowid as fk_expense_ik, r.active as range_active, c.active as cat_active';
206  $sql.= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
207  $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
208  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'expensereport_ik i ON (r.rowid = i.fk_range)';
209  $sql.= ' WHERE r.entity IN (0, '. getEntity('').')';
210  $sql.= ' ORDER BY r.fk_c_exp_tax_cat, r.range_ik';
211 
212  dol_syslog(get_called_class().'::getAllRanges sql='.$sql, LOG_DEBUG);
213  $resql = $db->query($sql);
214  if ($resql)
215  {
216  while ($obj = $db->fetch_object($resql))
217  {
218  $ik = new ExpenseReportIk($db);
219  if ($obj->fk_expense_ik > 0) $ik->fetch($obj->fk_expense_ik);
220  $obj->ik = $ik;
221 
222  if (!isset($ranges[$obj->fk_c_exp_tax_cat])) $ranges[$obj->fk_c_exp_tax_cat] = array('label' => $obj->label, 'active' => $obj->cat_active, 'ranges' => array());
223  $ranges[$obj->fk_c_exp_tax_cat]['ranges'][] = $obj;
224  }
225  }
226  else
227  {
228  dol_print_error($db);
229  }
230 
231  return $ranges;
232  }
233 
240  public static function getMaxRangeNumber($default_c_exp_tax_cat=0)
241  {
242  global $db,$conf;
243 
244  $sql = 'SELECT MAX(counted) as nbRange FROM (';
245  $sql.= ' SELECT COUNT(*) as counted';
246  $sql.= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
247  $sql.= ' WHERE r.entity IN (0, '.$conf->entity.')';
248  if ($default_c_exp_tax_cat > 0) $sql .= ' AND r.fk_c_exp_tax_cat = '.$default_c_exp_tax_cat;
249  $sql.= ' GROUP BY r.fk_c_exp_tax_cat';
250  $sql .= ') as counts';
251 
252  dol_syslog(get_called_class().'::getMaxRangeNumber sql='.$sql, LOG_DEBUG);
253  $resql = $db->query($sql);
254  if ($resql)
255  {
256  $obj = $db->fetch_object($resql);
257  return $obj->nbRange;
258  }
259  else
260  {
261  dol_print_error($db);
262  }
263 
264  return 0;
265  }
266 }
static getMaxRangeNumber($default_c_exp_tax_cat=0)
Return the max number of range by a category.
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
static getAllRanges()
Return an array of ranges grouped by category.
static getRangeByUser(User $userauthor, $fk_c_exp_tax_cat)
Return an array of ranges for a user.
Class to manage inventories.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class to manage Dolibarr database access.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
static getTaxCategories($mode=1)
Return expense categories in array.
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.
static getRangesByCategory($fk_c_exp_tax_cat, $active=1)
Return an array of ranges for a category.