dolibarr  13.0.2
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 <https://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  public $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  } else {
126  dol_print_error($db);
127  }
128 
129  return $categories;
130  }
131 
139  public static function getRangeByUser(User $userauthor, $fk_c_exp_tax_cat)
140  {
141  $default_range = (int) $userauthor->default_range; // if not defined, then 0
142  $ranges = self::getRangesByCategory($fk_c_exp_tax_cat);
143 
144  // substract 1 because array start from 0
145  if (empty($ranges) || !isset($ranges[$default_range - 1])) return false;
146  else return $ranges[$default_range - 1];
147  }
148 
156  public static function getRangesByCategory($fk_c_exp_tax_cat, $active = 1)
157  {
158  global $db;
159 
160  $ranges = array();
161 
162  $sql = 'SELECT r.rowid FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
163  if ($active) $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
164  $sql .= ' WHERE r.fk_c_exp_tax_cat = '.$fk_c_exp_tax_cat;
165  if ($active) $sql .= ' AND r.active = 1 AND c.active = 1';
166  $sql .= ' ORDER BY r.range_ik';
167 
168  dol_syslog(get_called_class().'::getRangesByCategory sql='.$sql, LOG_DEBUG);
169  $resql = $db->query($sql);
170  if ($resql)
171  {
172  $num = $db->num_rows($resql);
173  if ($num > 0)
174  {
175  while ($obj = $db->fetch_object($resql))
176  {
177  $object = new ExpenseReportIk($db);
178  $object->fetch($obj->rowid);
179 
180  $ranges[] = $object;
181  }
182  }
183  } else {
184  dol_print_error($db);
185  }
186 
187  return $ranges;
188  }
189 
195  public static function getAllRanges()
196  {
197  global $db;
198 
199  $ranges = array();
200 
201  $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';
202  $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
203  $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
204  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'expensereport_ik i ON (r.rowid = i.fk_range)';
205  $sql .= ' WHERE r.entity IN (0, '.getEntity('').')';
206  $sql .= ' ORDER BY r.fk_c_exp_tax_cat, r.range_ik';
207 
208  dol_syslog(get_called_class().'::getAllRanges sql='.$sql, LOG_DEBUG);
209  $resql = $db->query($sql);
210  if ($resql)
211  {
212  while ($obj = $db->fetch_object($resql))
213  {
214  $ik = new ExpenseReportIk($db);
215  if ($obj->fk_expense_ik > 0) $ik->fetch($obj->fk_expense_ik);
216  $obj->ik = $ik;
217 
218  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());
219  $ranges[$obj->fk_c_exp_tax_cat]['ranges'][] = $obj;
220  }
221  } else {
222  dol_print_error($db);
223  }
224 
225  return $ranges;
226  }
227 
234  public static function getMaxRangeNumber($default_c_exp_tax_cat = 0)
235  {
236  global $db, $conf;
237 
238  $sql = 'SELECT MAX(counted) as nbRange FROM (';
239  $sql .= ' SELECT COUNT(*) as counted';
240  $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
241  $sql .= ' WHERE r.entity IN (0, '.$conf->entity.')';
242  if ($default_c_exp_tax_cat > 0) $sql .= ' AND r.fk_c_exp_tax_cat = '.$default_c_exp_tax_cat;
243  $sql .= ' GROUP BY r.fk_c_exp_tax_cat';
244  $sql .= ') as counts';
245 
246  dol_syslog(get_called_class().'::getMaxRangeNumber sql='.$sql, LOG_DEBUG);
247  $resql = $db->query($sql);
248  if ($resql)
249  {
250  $obj = $db->fetch_object($resql);
251  return $obj->nbRange;
252  } else {
253  dol_print_error($db);
254  }
255 
256  return 0;
257  }
258 }
static getMaxRangeNumber($default_c_exp_tax_cat=0)
Return the max number of range by a category.
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:44
Class to manage Dolibarr database access.
static getTaxCategories($mode=1)
Return expense categories in array.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
__construct(DoliDB &$db)
Constructor.
static getRangesByCategory($fk_c_exp_tax_cat, $active=1)
Return an array of ranges for a category.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
CoreObject.