dolibarr  7.0.0-beta
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 {
32  public $element='expenseik';
33  public $table_element='expensereport_ik';
34  public $fk_element='fk_expense_ik';
35 
40  public $fk_c_exp_tax_cat;
41 
46  public $fk_range;
47 
52  public $coef;
53 
58  public $ikoffset;
59 
64  protected $fields=array(
65  'rowid'=>array('type'=>'integer','index'=>true)
66  ,'fk_c_exp_tax_cat'=>array('type'=>'integer','index'=>true)
67  ,'fk_range'=>array('type'=>'integer','index'=>true)
68  ,'coef'=>array('type'=>'double')
69  ,'ikoffset'=>array('type'=>'double')
70  );
71 
77  public function __construct(DoliDB &$db)
78  {
79  global $conf;
80 
81  parent::__construct($db);
82  parent::init();
83 
84  $this->errors = array();
85  }
86 
87 
94  public static function getTaxCategories($mode=1)
95  {
96  global $db;
97 
98  $categories = array();
99 
100  $sql = 'SELECT rowid, label, entity, active';
101  $sql.= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_cat';
102  $sql.= ' WHERE entity IN ('. getEntity('c_exp_tax_cat').')';
103  if ($mode == 1) $sql.= ' AND active = 1';
104  elseif ($mode == 2) $sql.= 'AND active = 0';
105 
106  dol_syslog(get_called_class().'::getTaxCategories sql='.$sql, LOG_DEBUG);
107  $resql = $db->query($sql);
108  if ($resql)
109  {
110  while ($obj = $db->fetch_object($resql))
111  {
112  $categories[$obj->rowid] = $obj;
113  }
114  }
115  else
116  {
117  dol_print_error($db);
118  }
119 
120  return $categories;
121  }
122 
123  public static function getRangeByUser(User $userauthor, $fk_c_exp_tax_cat)
124  {
125  $default_range = (int) $userauthor->default_range; // if not defined, then 0
126  $ranges = self::getRangesByCategory($fk_c_exp_tax_cat);
127 
128  // substract 1 because array start from 0
129  if (empty($ranges) || !isset($ranges[$default_range-1])) return false;
130  else return $ranges[$default_range-1];
131  }
132 
140  public static function getRangesByCategory($fk_c_exp_tax_cat, $active=1)
141  {
142  global $db;
143 
144  $ranges = array();
145 
146  $sql = 'SELECT r.rowid FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
147  if ($active) $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
148  $sql.= ' WHERE r.fk_c_exp_tax_cat = '.$fk_c_exp_tax_cat;
149  if ($active) $sql.= ' AND r.active = 1 AND c.active = 1';
150  $sql.= ' ORDER BY r.range_ik';
151 
152  dol_syslog(get_called_class().'::getRangesByCategory sql='.$sql, LOG_DEBUG);
153  $resql = $db->query($sql);
154  if ($resql)
155  {
156  $num = $db->num_rows($resql);
157  if ($num > 0)
158  {
159  while ($obj = $db->fetch_object($resql))
160  {
161  $object = new ExpenseReportIk($db);
162  $object->fetch($obj->rowid);
163 
164  $ranges[] = $object;
165  }
166  }
167  }
168  else
169  {
170  dol_print_error($db);
171  }
172 
173  return $ranges;
174  }
175 
181  public static function getAllRanges()
182  {
183  global $db;
184 
185  $ranges = array();
186 
187  $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';
188  $sql.= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
189  $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
190  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'expensereport_ik i ON (r.rowid = i.fk_range)';
191  $sql.= ' WHERE r.entity IN (0, '. getEntity('').')';
192  $sql.= ' ORDER BY r.fk_c_exp_tax_cat, r.range_ik';
193 
194  dol_syslog(get_called_class().'::getAllRanges sql='.$sql, LOG_DEBUG);
195  $resql = $db->query($sql);
196  if ($resql)
197  {
198  while ($obj = $db->fetch_object($resql))
199  {
200  $ik = new ExpenseReportIk($db);
201  if ($obj->fk_expense_ik > 0) $ik->fetch($obj->fk_expense_ik);
202  $obj->ik = $ik;
203 
204  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());
205  $ranges[$obj->fk_c_exp_tax_cat]['ranges'][] = $obj;
206  }
207  }
208  else
209  {
210  dol_print_error($db);
211  }
212 
213  return $ranges;
214  }
215 
222  public static function getMaxRangeNumber($default_c_exp_tax_cat=0)
223  {
224  global $db,$conf;
225 
226  $sql = 'SELECT MAX(counted) as nbRange FROM (';
227  $sql.= ' SELECT COUNT(*) as counted';
228  $sql.= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
229  $sql.= ' WHERE r.entity IN (0, '.$conf->entity.')';
230  if ($default_c_exp_tax_cat > 0) $sql .= ' AND r.fk_c_exp_tax_cat = '.$default_c_exp_tax_cat;
231  $sql.= ' GROUP BY r.fk_c_exp_tax_cat';
232  $sql .= ') as counts';
233 
234  dol_syslog(get_called_class().'::getMaxRangeNumber sql='.$sql, LOG_DEBUG);
235  $resql = $db->query($sql);
236  if ($resql)
237  {
238  $obj = $db->fetch_object($resql);
239  return $obj->nbRange;
240  }
241  else
242  {
243  dol_print_error($db);
244  }
245 
246  return 0;
247  }
248 }
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.
Class to manage inventories.
Class to manage Dolibarr users.
Definition: user.class.php:39
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, $forceentity=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.
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