dolibarr 19.0.3
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
25require_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
124 return $result;
125 }
126
127
128
136 public function update(User $user, $notrigger = false)
137 {
138 return $this->updateCommon($user, $notrigger);
139 }
140
148 public function delete(User $user, $notrigger = false)
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 // substract 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}
Parent class of all other business classes (invoices, contracts, proposals, orders,...
createCommon(User $user, $notrigger=false)
Create object into database.
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
updateCommon(User $user, $notrigger=false)
Update object into database.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
Class to manage Dolibarr database access.
Class to manage inventories.
__construct(DoliDB $db)
Constructor.
create(User $user, $notrigger=false)
Create object into database.
update(User $user, $notrigger=false)
Update object into database.
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.
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.
dol_print_error($db='', $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.