dolibarr  20.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  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28 
33 {
37  public $element = 'expenseik';
38 
42  public $table_element = 'expensereport_ik';
43 
47  public $fk_element = 'fk_expense_ik';
48 
53  public $fk_c_exp_tax_cat;
54 
59  public $fk_range;
60 
65  public $coef;
66 
71  public $ikoffset;
72 
73 
78  public $fields = array(
79  'rowid' => array('type' => 'integer', 'label' => 'ID', 'enabled' => 1, 'index' => 1, 'visible' => -1, 'position' => 10),
80  'fk_c_exp_tax_cat' => array('type' => 'integer', 'label' => 'Tax cat id', 'enabled' => 1, 'index' => 1, 'visible' => -1, 'position' => 20),
81  'fk_range' => array('type' => 'integer', 'label' => 'Tax range id', 'enabled' => 1, 'index' => 1, 'visible' => -1, 'position' => 30),
82  'coef' => array('type' => 'double', 'label' => 'Coef', 'enabled' => 1, 'visible' => -1, 'position' => 40),
83  'ikoffset' => array('type' => 'double', 'label' => 'Offset', 'enabled' => 1, 'visible' => -1, 'position' => 50),
84  );
85 
86 
92  public function __construct(DoliDB $db)
93  {
94  $this->db = $db;
95  }
96 
97 
105  public function create(User $user, $notrigger = 0)
106  {
107  $resultcreate = $this->createCommon($user, $notrigger);
108 
109  //$resultvalidate = $this->validate($user, $notrigger);
110 
111  return $resultcreate;
112  }
113 
114 
122  public function fetch($id, $ref = null)
123  {
124  return $this->fetchCommon($id, $ref);
125  }
126 
127 
128 
136  public function update(User $user, $notrigger = 0)
137  {
138  return $this->updateCommon($user, $notrigger);
139  }
140 
148  public function delete(User $user, $notrigger = 0)
149  {
150  return $this->deleteCommon($user, $notrigger);
151  //return $this->deleteCommon($user, $notrigger, 1);
152  }
153 
154 
161  public function getTaxCategories($mode = 1)
162  {
163  $categories = array();
164 
165  $sql = 'SELECT rowid, label, entity, active';
166  $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_cat';
167  $sql .= ' WHERE entity IN (0, '.getEntity($this->element).')';
168  if ($mode == 1) {
169  $sql .= ' AND active = 1';
170  } elseif ($mode == 2) {
171  $sql .= 'AND active = 0';
172  }
173 
174  dol_syslog(get_called_class().'::getTaxCategories', LOG_DEBUG);
175 
176  $resql = $this->db->query($sql);
177  if ($resql) {
178  while ($obj = $this->db->fetch_object($resql)) {
179  $categories[$obj->rowid] = $obj;
180  }
181  } else {
182  dol_print_error($this->db);
183  }
184 
185  return $categories;
186  }
187 
195  public function getRangeByUser(User $userauthor, int $fk_c_exp_tax_cat)
196  {
197  $default_range = (int) $userauthor->default_range; // if not defined, then 0
198  $ranges = $this->getRangesByCategory($fk_c_exp_tax_cat);
199  // prevent out of range -1 indice
200  $indice = $default_range - 1;
201  // subtract 1 because array start from 0
202  if (empty($ranges) || $indice < 0 || !isset($ranges[$indice])) {
203  return false;
204  } else {
205  return $ranges[$indice];
206  }
207  }
208 
216  public function getRangesByCategory(int $fk_c_exp_tax_cat, $active = 1)
217  {
218  $ranges = array();
219 
220  dol_syslog(get_called_class().'::getRangesByCategory for fk_c_exp_tax_cat='.$fk_c_exp_tax_cat, LOG_DEBUG);
221 
222  $sql = 'SELECT r.rowid FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
223  if ($active) {
224  $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
225  }
226  $sql .= ' WHERE r.fk_c_exp_tax_cat = '.((int) $fk_c_exp_tax_cat);
227  $sql .= " AND r.entity IN(0, ".getEntity($this->element).")";
228  if ($active) {
229  $sql .= ' AND r.active = 1 AND c.active = 1';
230  }
231  $sql .= ' ORDER BY r.range_ik';
232 
233  $resql = $this->db->query($sql);
234  if ($resql) {
235  $num = $this->db->num_rows($resql);
236  if ($num > 0) {
237  while ($obj = $this->db->fetch_object($resql)) {
238  $object = new ExpenseReportIk($this->db);
239  $object->fetch($obj->rowid);
240 
241  $ranges[] = $object;
242  }
243  }
244  } else {
245  dol_print_error($this->db);
246  }
247 
248  return $ranges;
249  }
250 
256  public function getAllRanges()
257  {
258  $ranges = array();
259 
260  $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';
261  $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
262  $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'c_exp_tax_cat c ON (r.fk_c_exp_tax_cat = c.rowid)';
263  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'expensereport_ik i ON (r.rowid = i.fk_range)';
264  $sql .= ' WHERE r.entity IN (0, '.getEntity($this->element).')';
265  $sql .= ' ORDER BY r.fk_c_exp_tax_cat, r.range_ik';
266 
267  dol_syslog(get_called_class().'::getAllRanges', LOG_DEBUG);
268 
269  $resql = $this->db->query($sql);
270  if ($resql) {
271  while ($obj = $this->db->fetch_object($resql)) {
272  $ik = new ExpenseReportIk($this->db);
273  if ($obj->fk_expense_ik > 0) {
274  $ik->fetch($obj->fk_expense_ik);
275  }
276 
277  // TODO Set a $tmparay = new stdObj(); and use it to fill $ranges array
278  $obj->ik = $ik;
279 
280  if (!isset($ranges[$obj->fk_c_exp_tax_cat])) {
281  $ranges[$obj->fk_c_exp_tax_cat] = array('label' => $obj->label, 'active' => $obj->cat_active, 'ranges' => array());
282  }
283  $ranges[$obj->fk_c_exp_tax_cat]['ranges'][] = $obj;
284  }
285  } else {
286  dol_print_error($this->db);
287  }
288 
289  return $ranges;
290  }
291 
298  public function getMaxRangeNumber($default_c_exp_tax_cat = 0)
299  {
300  $sql = 'SELECT MAX(counted) as nbRange FROM (';
301  $sql .= ' SELECT COUNT(*) as counted';
302  $sql .= ' FROM '.MAIN_DB_PREFIX.'c_exp_tax_range r';
303  $sql .= ' WHERE r.entity IN (0, '.getEntity($this->element).')';
304  if ($default_c_exp_tax_cat > 0) {
305  $sql .= ' AND r.fk_c_exp_tax_cat = '.((int) $default_c_exp_tax_cat);
306  }
307  $sql .= ' GROUP BY r.fk_c_exp_tax_cat';
308  $sql .= ') as counts';
309 
310  dol_syslog(get_called_class().'::getMaxRangeNumber', LOG_DEBUG);
311  $resql = $this->db->query($sql);
312  if ($resql) {
313  $obj = $this->db->fetch_object($resql);
314  return $obj->nbRange;
315  } else {
316  dol_print_error($this->db);
317  }
318 
319  return 0;
320  }
321 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Parent class of all other business classes (invoices, contracts, proposals, orders,...
createCommon(User $user, $notrigger=0)
Create object in the database.
updateCommon(User $user, $notrigger=0)
Update object into database.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Class to manage Dolibarr database access.
Class to manage inventories.
__construct(DoliDB $db)
Constructor.
getRangeByUser(User $userauthor, int $fk_c_exp_tax_cat)
Return an array of ranges for a user.
getTaxCategories($mode=1)
Return expense categories in array.
getAllRanges()
Return an array of ranges grouped by category.
create(User $user, $notrigger=0)
Create object into database.
update(User $user, $notrigger=0)
Update object into database.
getRangesByCategory(int $fk_c_exp_tax_cat, $active=1)
Return an array of ranges for a category.
fetch($id, $ref=null)
Load object in memory from the database.
getMaxRangeNumber($default_c_exp_tax_cat=0)
Return the max number of range by a category.
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.