dolibarr  16.0.1
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/commonobject.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 
71 
76  public $fields = array(
77  'rowid'=>array('type'=>'integer', 'index'=>true)
78  ,'fk_c_exp_tax_cat'=>array('type'=>'integer', 'index'=>true)
79  ,'fk_range'=>array('type'=>'integer', 'index'=>true)
80  ,'coef'=>array('type'=>'double')
81  ,'ikoffset'=>array('type'=>'double')
82  );
83 
84 
90  public function __construct(DoliDB $db)
91  {
92  $this->db = $db;
93  }
94 
95 
103  public function create(User $user, $notrigger = false)
104  {
105  $resultcreate = $this->createCommon($user, $notrigger);
106 
107  //$resultvalidate = $this->validate($user, $notrigger);
108 
109  return $resultcreate;
110  }
111 
112 
120  public function fetch($id, $ref = null)
121  {
122  $result = $this->fetchCommon($id, $ref);
123  if ($result > 0 && !empty($this->table_element_line)) {
124  $this->fetchLines();
125  }
126  return $result;
127  }
128 
129 
137  public function update(User $user, $notrigger = false)
138  {
139  return $this->updateCommon($user, $notrigger);
140  }
141 
149  public function delete(User $user, $notrigger = false)
150  {
151  return $this->deleteCommon($user, $notrigger);
152  //return $this->deleteCommon($user, $notrigger, 1);
153  }
154 
155 
162  public function getTaxCategories($mode = 1)
163  {
164  $categories = array();
165 
166  $sql = 'SELECT rowid, label, entity, active';
167  $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_cat';
168  $sql .= ' WHERE entity IN (0, '.getEntity($this->element).')';
169  if ($mode == 1) {
170  $sql .= ' AND active = 1';
171  } elseif ($mode == 2) {
172  $sql .= 'AND active = 0';
173  }
174 
175  dol_syslog(get_called_class().'::getTaxCategories', LOG_DEBUG);
176 
177  $resql = $this->db->query($sql);
178  if ($resql) {
179  while ($obj = $this->db->fetch_object($resql)) {
180  $categories[$obj->rowid] = $obj;
181  }
182  } else {
183  dol_print_error($this->db);
184  }
185 
186  return $categories;
187  }
188 
196  public function getRangeByUser(User $userauthor, int $fk_c_exp_tax_cat)
197  {
198  $default_range = (int) $userauthor->default_range; // if not defined, then 0
199  $ranges = $this->getRangesByCategory($fk_c_exp_tax_cat);
200  // prevent out of range -1 indice
201  $indice = $default_range > 0 ? $default_range - 1 : 0;
202  // substract 1 because array start from 0
203  if (empty($ranges) || !isset($ranges[$indice])) {
204  return false;
205  } else {
206  return $ranges[$indice];
207  }
208  }
209 
217  public function getRangesByCategory(int $fk_c_exp_tax_cat, $active = 1)
218  {
219  $ranges = array();
220 
221  dol_syslog(get_called_class().'::getRangesByCategory for fk_c_exp_tax_cat='.$fk_c_exp_tax_cat, LOG_DEBUG);
222 
223  $sql = 'SELECT r.rowid FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
224  if ($active) {
225  $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
226  }
227  $sql .= ' WHERE r.fk_c_exp_tax_cat = '.((int) $fk_c_exp_tax_cat);
228  $sql .= " AND r.entity IN(0, ".getEntity($this->element).")";
229  if ($active) {
230  $sql .= ' AND r.active = 1 AND c.active = 1';
231  }
232  $sql .= ' ORDER BY r.range_ik';
233 
234  $resql = $this->db->query($sql);
235  if ($resql) {
236  $num = $this->db->num_rows($resql);
237  if ($num > 0) {
238  while ($obj = $this->db->fetch_object($resql)) {
239  $object = new ExpenseReportIk($this->db);
240  $object->fetch($obj->rowid);
241 
242  $ranges[] = $object;
243  }
244  }
245  } else {
246  dol_print_error($this->db);
247  }
248 
249  return $ranges;
250  }
251 
257  public function getAllRanges()
258  {
259  $ranges = array();
260 
261  $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';
262  $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
263  $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
264  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'expensereport_ik i ON (r.rowid = i.fk_range)';
265  $sql .= ' WHERE r.entity IN (0, '.getEntity($this->element).')';
266  $sql .= ' ORDER BY r.fk_c_exp_tax_cat, r.range_ik';
267 
268  dol_syslog(get_called_class().'::getAllRanges', LOG_DEBUG);
269 
270  $resql = $this->db->query($sql);
271  if ($resql) {
272  while ($obj = $this->db->fetch_object($resql)) {
273  $ik = new ExpenseReportIk($this->db);
274  if ($obj->fk_expense_ik > 0) {
275  $ik->fetch($obj->fk_expense_ik);
276  }
277  $obj->ik = $ik;
278 
279  if (!isset($ranges[$obj->fk_c_exp_tax_cat])) {
280  $ranges[$obj->fk_c_exp_tax_cat] = array('label' => $obj->label, 'active' => $obj->cat_active, 'ranges' => array());
281  }
282  $ranges[$obj->fk_c_exp_tax_cat]['ranges'][] = $obj;
283  }
284  } else {
285  dol_print_error($this->db);
286  }
287 
288  return $ranges;
289  }
290 
297  public function getMaxRangeNumber($default_c_exp_tax_cat = 0)
298  {
299  $sql = 'SELECT MAX(counted) as nbRange FROM (';
300  $sql .= ' SELECT COUNT(*) as counted';
301  $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
302  $sql .= ' WHERE r.entity IN (0, '.getEntity($this->element).')';
303  if ($default_c_exp_tax_cat > 0) {
304  $sql .= ' AND r.fk_c_exp_tax_cat = '.((int) $default_c_exp_tax_cat);
305  }
306  $sql .= ' GROUP BY r.fk_c_exp_tax_cat';
307  $sql .= ') as counts';
308 
309  dol_syslog(get_called_class().'::getMaxRangeNumber', LOG_DEBUG);
310  $resql = $this->db->query($sql);
311  if ($resql) {
312  $obj = $this->db->fetch_object($resql);
313  return $obj->nbRange;
314  } else {
315  dol_print_error($this->db);
316  }
317 
318  return 0;
319  }
320 }
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
getTaxCategories($mode=1)
Return expense categories in array.
$conf db
API class for accounts.
Definition: inc.php:41
update(User $user, $notrigger=false)
Update object into database.
Class to manage inventories.
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
createCommon(User $user, $notrigger=false)
Create object into database.
getMaxRangeNumber($default_c_exp_tax_cat=0)
Return the max number of range by a category.
__construct(DoliDB $db)
Constructor.
create(User $user, $notrigger=false)
Create object into database.
fetch($id, $ref=null)
Load object in memory from the database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getRangesByCategory(int $fk_c_exp_tax_cat, $active=1)
Return an array of ranges for a category.
updateCommon(User $user, $notrigger=false)
Update object into database.
if(isModEnabled('facture')&&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur')&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)&&$user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice')&&$user->rights->supplier_invoice->lire)) if(isModEnabled('don')&&!empty($user->rights->don->lire)) if(isModEnabled('tax')&&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture')&&isModEnabled('commande')&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
getRangeByUser(User $userauthor, int $fk_c_exp_tax_cat)
Return an array of ranges for a user.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getAllRanges()
Return an array of ranges grouped by category.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
fetchCommon($id, $ref=null, $morewhere= '')
Load object in memory from the database.