dolibarr 23.0.3
expensereportline.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011 Dimitri Mouillard <dmouillard@teclib.com>
3 * Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
5 * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
6 * Copyright (c) 2018-2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2016-2020 Ferran Marcet <fmarcet@2byte.es>
8 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
32require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_rule.class.php';
33
38{
42 public $db;
43
47 public $table_element = 'expensereport_det';
48
52 public $error = '';
53
57 public $rowid;
58
62 public $comments;
63
67 public $qty;
68
72 public $value_unit;
73
77 public $date;
78
82 public $dates;
83
87 public $fk_c_type_fees;
88
92 public $fk_c_exp_tax_cat;
93
97 public $fk_expensereport;
98
102 public $type_fees_code;
103
107 public $type_fees_libelle;
108
112 public $type_fees_accountancy_code;
113
117 public $projet_ref;
118
122 public $projet_title;
123
127 public $rang;
128
132 public $vatrate;
133
137 public $vat_src_code;
138
142 public $tva_tx;
143
147 public $localtax1_tx;
148
152 public $localtax2_tx;
153
157 public $localtax1_type;
158
162 public $localtax2_type;
163
167 public $total_ht;
168
172 public $total_tva;
173
177 public $total_ttc;
178
182 public $total_localtax1;
183
187 public $total_localtax2;
188
189 // Multicurrency
193 public $fk_multicurrency;
194
198 public $multicurrency_code;
199
203 public $multicurrency_tx;
204
208 public $multicurrency_total_ht;
209
213 public $multicurrency_total_tva;
214
218 public $multicurrency_total_ttc;
219
223 public $fk_ecm_files;
224
228 public $rule_warning_message;
229
230
236 public function __construct($db)
237 {
238 $this->db = $db;
239 }
240
247 public function fetch($rowid)
248 {
249 $sql = 'SELECT fde.rowid, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_c_exp_tax_cat, fde.fk_projet as fk_project, fde.date,';
250 $sql .= ' fde.tva_tx as vatrate, fde.vat_src_code, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc, fde.fk_ecm_files,';
251 $sql .= ' fde.localtax1_tx, fde.localtax2_tx, fde.localtax1_type, fde.localtax2_type, fde.total_localtax1, fde.total_localtax2, fde.rule_warning_message,';
252 $sql .= ' ctf.code as type_fees_code, ctf.label as type_fees_libelle,';
253 $sql .= ' pjt.rowid as projet_id, pjt.title as projet_title, pjt.ref as projet_ref';
254 $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_det as fde';
255 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id'; // Sometimes type of expense report has been removed, so we use a left join here.
256 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pjt ON fde.fk_projet=pjt.rowid';
257 $sql .= ' WHERE fde.rowid = '.((int) $rowid);
258
259 $result = $this->db->query($sql);
260
261 if ($result) {
262 $objp = $this->db->fetch_object($result);
263
264 $this->rowid = $objp->rowid;
265 $this->id = $objp->rowid;
266 //$this->ref = $objp->ref; Undefined property
267 $this->fk_expensereport = $objp->fk_expensereport;
268 $this->comments = $objp->comments;
269 $this->qty = $objp->qty;
270 $this->date = $objp->date;
271 $this->dates = $this->db->jdate($objp->date);
272 $this->value_unit = $objp->value_unit;
273 $this->fk_c_type_fees = $objp->fk_c_type_fees;
274 $this->fk_c_exp_tax_cat = $objp->fk_c_exp_tax_cat;
275 $this->fk_projet = $objp->fk_project; // deprecated
276 $this->fk_project = $objp->fk_project;
277 $this->type_fees_code = $objp->type_fees_code;
278 $this->type_fees_libelle = $objp->type_fees_libelle;
279 $this->projet_ref = $objp->projet_ref;
280 $this->projet_title = $objp->projet_title;
281
282 $this->vatrate = $objp->vatrate;
283 $this->vat_src_code = $objp->vat_src_code;
284 $this->localtax1_tx = $objp->localtax1_tx;
285 $this->localtax2_tx = $objp->localtax2_tx;
286 $this->localtax1_type = $objp->localtax1_type;
287 $this->localtax2_type = $objp->localtax2_type;
288
289 $this->total_ht = $objp->total_ht;
290 $this->total_tva = $objp->total_tva;
291 $this->total_ttc = $objp->total_ttc;
292 $this->total_localtax1 = $objp->total_localtax1;
293 $this->total_localtax2 = $objp->total_localtax2;
294
295 $this->fk_ecm_files = $objp->fk_ecm_files;
296
297 $this->rule_warning_message = $objp->rule_warning_message;
298
299 $this->db->free($result);
300
301 return $this->id;
302 } else {
303 dol_print_error($this->db);
304 return -1;
305 }
306 }
307
315 public function insert($notrigger = 0, $fromaddline = false)
316 {
317 global $user;
318
319 $error = 0;
320
321 dol_syslog("ExpenseReportLine::Insert", LOG_DEBUG);
322
323 // Clean parameters
324 $this->comments = trim($this->comments);
325 if (empty($this->value_unit)) {
326 $this->value_unit = 0;
327 }
328 $this->qty = (float) price2num($this->qty);
329 $this->vatrate = price2num($this->vatrate);
330 if (empty($this->fk_c_exp_tax_cat)) {
331 $this->fk_c_exp_tax_cat = 0;
332 }
333
334 $this->db->begin();
335
336 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'expensereport_det';
337 $sql .= ' (fk_expensereport, fk_c_type_fees, fk_projet,';
338 $sql .= ' tva_tx, vat_src_code,';
339 $sql .= ' localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
340 $sql .= ' comments, qty, value_unit,';
341 $sql .= ' total_ht, total_tva, total_ttc,';
342 $sql .= ' total_localtax1, total_localtax2,';
343 $sql .= ' date, rule_warning_message, fk_c_exp_tax_cat, fk_ecm_files)';
344 $sql .= " VALUES (".$this->db->escape((string) $this->fk_expensereport).",";
345 $sql .= " ".((int) $this->fk_c_type_fees).",";
346 $sql .= " ".((int) (!empty($this->fk_project) && $this->fk_project > 0) ? $this->fk_project : ((!empty($this->fk_projet) && $this->fk_projet > 0) ? $this->fk_projet : 'null')).",";
347 $sql .= " ".((float) $this->vatrate).",";
348 $sql .= " '".$this->db->escape(empty($this->vat_src_code) ? '' : $this->vat_src_code)."',";
349 $sql .= " ".((float) price2num($this->localtax1_tx)).",";
350 $sql .= " ".((float) price2num($this->localtax2_tx)).",";
351 $sql .= " '".$this->db->escape($this->localtax1_type)."',";
352 $sql .= " '".$this->db->escape($this->localtax2_type)."',";
353 $sql .= " '".$this->db->escape($this->comments)."',";
354 $sql .= " ".((float) $this->qty).",";
355 $sql .= " ".((float) $this->value_unit).",";
356 $sql .= " ".((float) price2num($this->total_ht)).",";
357 $sql .= " ".((float) price2num($this->total_tva)).",";
358 $sql .= " ".((float) price2num($this->total_ttc)).",";
359 $sql .= " ".((float) price2num($this->total_localtax1)).",";
360 $sql .= " ".((float) price2num($this->total_localtax2)).",";
361 $sql .= " '".$this->db->idate($this->date)."',";
362 $sql .= " ".(empty($this->rule_warning_message) ? 'null' : "'".$this->db->escape($this->rule_warning_message)."'").",";
363 $sql .= " ".((int) $this->fk_c_exp_tax_cat).",";
364 $sql .= " ".($this->fk_ecm_files > 0 ? ((int) $this->fk_ecm_files) : 'null');
365 $sql .= ")";
366
367 $resql = $this->db->query($sql);
368 if ($resql) {
369 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'expensereport_det');
370
371 if (!$notrigger) {
372 // Call triggers
373 $result = $this->call_trigger('EXPENSE_REPORT_DET_CREATE', $user);
374 if ($result < 0) {
375 $error++;
376 }
377 // End call triggers
378 }
379
380 if (!$fromaddline) {
381 $tmpparent = new ExpenseReport($this->db);
382 $tmpparent->fetch($this->fk_expensereport);
383 $result = $tmpparent->update_price(1);
384 if ($result < 0) {
385 $error++;
386 $this->error = $tmpparent->error;
387 $this->errors = $tmpparent->errors;
388 }
389 }
390 } else {
391 $error++;
392 }
393
394 if (!$error) {
395 $this->db->commit();
396 return $this->id;
397 } else {
398 $this->error = $this->db->lasterror();
399 dol_syslog("ExpenseReportLine::insert Error ".$this->error, LOG_ERR);
400 $this->db->rollback();
401 return -2;
402 }
403 }
404
413 public function getExpAmount(ExpenseReportRule $rule, $fk_user, $mode = 'day')
414 {
415 $amount = 0;
416
417 $sql = 'SELECT SUM(d.total_ttc) as total_amount';
418 $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_det d';
419 $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'expensereport e ON (d.fk_expensereport = e.rowid)';
420 $sql .= ' WHERE e.fk_user_author = '.((int) $fk_user);
421 if (!empty($this->id)) {
422 $sql .= ' AND d.rowid <> '.((int) $this->id);
423 }
424 $sql .= ' AND d.fk_c_type_fees = '.((int) $rule->fk_c_type_fees);
425 if ($mode == 'day' || $mode == 'EX_DAY') {
426 $sql .= " AND d.date = '".dol_print_date($this->date, '%Y-%m-%d')."'";
427 } elseif ($mode == 'mon' || $mode == 'EX_MON') {
428 $sql .= " AND DATE_FORMAT(d.date, '%Y-%m') = '".dol_print_date($this->date, '%Y-%m')."'"; // @todo DATE_FORMAT is forbidden
429 } elseif ($mode == 'year' || $mode == 'EX_YEA') {
430 $sql .= " AND DATE_FORMAT(d.date, '%Y') = '".dol_print_date($this->date, '%Y')."'"; // @todo DATE_FORMAT is forbidden
431 }
432
433 dol_syslog('ExpenseReportLine::getExpAmount');
434
435 $resql = $this->db->query($sql);
436 if ($resql) {
437 $num = $this->db->num_rows($resql);
438 if ($num > 0) {
439 $obj = $this->db->fetch_object($resql);
440 $amount = (float) $obj->total_amount;
441 }
442 } else {
443 dol_print_error($this->db);
444 }
445
446 return $amount + $this->total_ttc;
447 }
448
456 public function update(User $user, $notrigger = 0)
457 {
458 $error = 0;
459
460 // Clean parameters
461 $this->comments = trim($this->comments);
462 $this->vatrate = price2num($this->vatrate);
463 $this->value_unit = price2num($this->value_unit);
464 if (empty($this->fk_c_exp_tax_cat)) {
465 $this->fk_c_exp_tax_cat = 0;
466 }
467
468 $this->db->begin();
469
470 // Update line in database
471 $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport_det SET";
472 $sql .= " comments='".$this->db->escape($this->comments)."'";
473 $sql .= ", value_unit = ".((float) $this->value_unit);
474 $sql .= ", qty=".((float) $this->qty);
475 $sql .= ", date='".$this->db->idate($this->date)."'";
476 $sql .= ", total_ht=".((float) price2num($this->total_ht, 'MT'));
477 $sql .= ", total_tva=".((float) price2num($this->total_tva, 'MT'));
478 $sql .= ", total_ttc=".((float) price2num($this->total_ttc, 'MT'));
479 $sql .= ", total_localtax1=".((float) price2num($this->total_localtax1, 'MT'));
480 $sql .= ", total_localtax2=".((float) price2num($this->total_localtax2, 'MT'));
481 $sql .= ", tva_tx=".((float) $this->vatrate);
482 $sql .= ", vat_src_code='".$this->db->escape($this->vat_src_code)."'";
483 $sql .= ", localtax1_tx=".((float) $this->localtax1_tx);
484 $sql .= ", localtax2_tx=".((float) $this->localtax2_tx);
485 $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
486 $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
487 $sql .= ", rule_warning_message='".$this->db->escape($this->rule_warning_message)."'";
488 $sql .= ", fk_c_exp_tax_cat=".$this->db->escape((string) $this->fk_c_exp_tax_cat);
489 $sql .= ", fk_ecm_files=".($this->fk_ecm_files > 0 ? ((int) $this->fk_ecm_files) : 'null');
490 if ($this->fk_c_type_fees) {
491 $sql .= ", fk_c_type_fees = ".((int) $this->fk_c_type_fees);
492 } else {
493 $sql .= ", fk_c_type_fees=null";
494 }
495 if ($this->fk_project > 0) {
496 $sql .= ", fk_projet=".((int) $this->fk_project);
497 } else {
498 $sql .= ", fk_projet=null";
499 }
500 $sql .= " WHERE rowid = ".((int) ($this->rowid ? $this->rowid : $this->id));
501
502 dol_syslog("ExpenseReportLine::update");
503
504 $resql = $this->db->query($sql);
505 if ($resql) {
506 $tmpparent = new ExpenseReport($this->db);
507 $result = $tmpparent->fetch($this->fk_expensereport);
508 if ($result > 0) {
509 $result = $tmpparent->update_price(1);
510 if ($result < 0) {
511 $error++;
512 $this->error = $tmpparent->error;
513 $this->errors = $tmpparent->errors;
514 }
515 } else {
516 $error++;
517 $this->error = $tmpparent->error;
518 $this->errors = $tmpparent->errors;
519 }
520 } else {
521 $error++;
522 dol_print_error($this->db);
523 }
524
525 if (!$error && !$notrigger) {
526 // Call triggers
527 $result = $this->call_trigger('EXPENSE_REPORT_DET_MODIFY', $user);
528 if ($result < 0) {
529 $error++;
530 }
531 // End call triggers
532 }
533
534 if (!$error) {
535 $this->db->commit();
536 return 1;
537 } else {
538 $this->error = $this->db->lasterror();
539 dol_syslog("ExpenseReportLine::update Error ".$this->error, LOG_ERR);
540 $this->db->rollback();
541 return -2;
542 }
543 }
544}
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage Trips and Expenses.
Class of expense report details lines.
fetch($rowid)
Fetch record for expense report detailed line.
update(User $user, $notrigger=0)
Update line.
getExpAmount(ExpenseReportRule $rule, $fk_user, $mode='day')
Function to get total amount in expense reports for a same rule.
insert($notrigger=0, $fromaddline=false)
Insert a line of expense report.
Class to manage inventories.
Class to manage Dolibarr users.
vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0, $html=0)
Return a string with VAT rate label formatted for view output Used into pdf and HTML pages.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:464