254 $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,';
255 $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,';
256 $sql .=
' fde.localtax1_tx, fde.localtax2_tx, fde.localtax1_type, fde.localtax2_type, fde.total_localtax1, fde.total_localtax2, fde.rule_warning_message,';
257 $sql .=
' ctf.code as type_fees_code, ctf.label as type_fees_libelle, fde.tcheck_file,';
258 $sql .=
' pjt.rowid as projet_id, pjt.title as projet_title, pjt.ref as projet_ref';
259 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expensereport_det as fde';
260 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id';
261 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'projet as pjt ON fde.fk_projet=pjt.rowid';
262 $sql .=
' WHERE fde.rowid = '.((int) $rowid);
264 $result = $this->db->query($sql);
267 $objp = $this->db->fetch_object($result);
269 $this->rowid = $objp->rowid;
270 $this->
id = $objp->rowid;
272 $this->fk_expensereport = $objp->fk_expensereport;
273 $this->comments = $objp->comments;
274 $this->qty = $objp->qty;
275 $this->
date = $objp->date;
276 $this->dates = $this->db->jdate($objp->date);
277 $this->value_unit = $objp->value_unit;
278 $this->fk_c_type_fees = (int) $objp->fk_c_type_fees;
279 $this->fk_c_exp_tax_cat = (int) $objp->fk_c_exp_tax_cat;
280 $this->fk_projet = $objp->fk_project;
281 $this->fk_project = $objp->fk_project;
282 $this->type_fees_code = $objp->type_fees_code;
283 $this->type_fees_libelle = $objp->type_fees_libelle;
284 $this->projet_ref = $objp->projet_ref;
285 $this->projet_title = $objp->projet_title;
287 $this->
vatrate = $objp->vatrate;
288 $this->vat_src_code = $objp->vat_src_code;
289 $this->localtax1_tx = $objp->localtax1_tx;
290 $this->localtax2_tx = $objp->localtax2_tx;
291 $this->localtax1_type = $objp->localtax1_type;
292 $this->localtax2_type = $objp->localtax2_type;
294 $this->total_ht = $objp->total_ht;
295 $this->total_tva = $objp->total_tva;
296 $this->total_ttc = $objp->total_ttc;
297 $this->total_localtax1 = $objp->total_localtax1;
298 $this->total_localtax2 = $objp->total_localtax2;
300 $this->fk_ecm_files = $objp->fk_ecm_files;
301 $this->tcheck_file = $objp->tcheck_file;
303 $this->rule_warning_message = $objp->rule_warning_message;
305 $this->db->free($result);
309 dol_print_error($this->db);
321 public function insert($notrigger = 0, $fromaddline =
false)
327 dol_syslog(
"ExpenseReportLine::Insert", LOG_DEBUG);
330 $this->comments = trim($this->comments);
331 if (empty($this->value_unit)) {
332 $this->value_unit = 0;
334 $this->qty = (float)
price2num($this->qty);
336 if (empty($this->fk_c_exp_tax_cat)) {
337 $this->fk_c_exp_tax_cat = 0;
342 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'expensereport_det';
343 $sql .=
' (fk_expensereport, fk_c_type_fees, fk_projet,';
344 $sql .=
' tva_tx, vat_src_code,';
345 $sql .=
' localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
346 $sql .=
' comments, qty, value_unit,';
347 $sql .=
' total_ht, total_tva, total_ttc,';
348 $sql .=
' total_localtax1, total_localtax2,';
349 $sql .=
' date, rule_warning_message, fk_c_exp_tax_cat, fk_ecm_files)';
350 $sql .=
" VALUES (".((int) $this->fk_expensereport).
",";
351 $sql .=
" ".((int) $this->fk_c_type_fees).
",";
352 $sql .=
" ".((int) (!empty($this->fk_project) && $this->fk_project > 0) ? ((
int) $this->fk_project) : ((!empty($this->fk_projet) && $this->fk_projet > 0) ? ((
int) $this->fk_projet) :
'null')).
",";
353 $sql .=
" ".((float) $this->
vatrate).
",";
354 $sql .=
" '".$this->db->escape(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"',";
355 $sql .=
" ".((float)
price2num($this->localtax1_tx)).
",";
356 $sql .=
" ".((float)
price2num($this->localtax2_tx)).
",";
357 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
358 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
359 $sql .=
" '".$this->db->escape($this->comments).
"',";
360 $sql .=
" ".((float) $this->qty).
",";
361 $sql .=
" ".((float) $this->value_unit).
",";
362 $sql .=
" ".((float)
price2num($this->total_ht)).
",";
363 $sql .=
" ".((float)
price2num($this->total_tva)).
",";
364 $sql .=
" ".((float)
price2num($this->total_ttc)).
",";
365 $sql .=
" ".((float)
price2num($this->total_localtax1)).
",";
366 $sql .=
" ".((float)
price2num($this->total_localtax2)).
",";
367 $sql .=
" '".$this->db->idate($this->
date).
"',";
368 $sql .=
" ".(empty($this->rule_warning_message) ?
'null' :
"'".$this->db->escape($this->rule_warning_message).
"'").
",";
369 $sql .=
" ".((int) $this->fk_c_exp_tax_cat).
",";
370 $sql .=
" ".($this->fk_ecm_files > 0 ? ((int) $this->fk_ecm_files) :
'null');
373 $resql = $this->db->query($sql);
375 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'expensereport_det');
379 $result = $this->call_trigger(
'EXPENSE_REPORT_DET_CREATE', $user);
388 $tmpparent->fetch($this->fk_expensereport);
389 $result = $tmpparent->update_price(1);
392 $this->error = $tmpparent->error;
393 $this->errors = $tmpparent->errors;
404 $this->error = $this->db->lasterror();
405 dol_syslog(
"ExpenseReportLine::insert Error ".$this->error, LOG_ERR);
406 $this->db->rollback();
423 $sql =
'SELECT SUM(d.total_ttc) as total_amount';
424 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expensereport_det d';
425 $sql .=
' INNER JOIN '.MAIN_DB_PREFIX.
'expensereport e ON (d.fk_expensereport = e.rowid)';
426 $sql .=
' WHERE e.fk_user_author = '.((int) $fk_user);
427 if (!empty($this->
id)) {
428 $sql .=
' AND d.rowid <> '.((int) $this->
id);
430 $sql .=
' AND d.fk_c_type_fees = '.((int) $rule->fk_c_type_fees);
431 if ($mode ==
'day' || $mode ==
'EX_DAY') {
432 $sql .=
" AND d.date = '".dol_print_date($this->
date,
'%Y-%m-%d').
"'";
433 } elseif ($mode ==
'mon' || $mode ==
'EX_MON') {
434 $sql .=
" AND DATE_FORMAT(d.date, '%Y-%m') = '".dol_print_date($this->
date,
'%Y-%m').
"'";
435 } elseif ($mode ==
'year' || $mode ==
'EX_YEA') {
436 $sql .=
" AND DATE_FORMAT(d.date, '%Y') = '".dol_print_date($this->
date,
'%Y').
"'";
439 dol_syslog(
'ExpenseReportLine::getExpAmount');
441 $resql = $this->db->query($sql);
443 $num = $this->db->num_rows($resql);
445 $obj = $this->db->fetch_object($resql);
446 $amount = (float) $obj->total_amount;
449 dol_print_error($this->db);
452 return $amount + $this->total_ttc;
467 $this->comments = trim($this->comments);
469 $this->value_unit =
price2num($this->value_unit);
470 if (empty($this->fk_c_exp_tax_cat)) {
471 $this->fk_c_exp_tax_cat = 0;
477 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expensereport_det SET";
478 $sql .=
" comments='".$this->db->escape($this->comments).
"'";
479 $sql .=
", value_unit = ".((float) $this->value_unit);
480 $sql .=
", qty=".((float) $this->qty);
481 $sql .=
", date='".$this->db->idate($this->
date).
"'";
482 $sql .=
", total_ht=".((float)
price2num($this->total_ht,
'MT'));
483 $sql .=
", total_tva=".((float)
price2num($this->total_tva,
'MT'));
484 $sql .=
", total_ttc=".((float)
price2num($this->total_ttc,
'MT'));
485 $sql .=
", total_localtax1=".((float)
price2num($this->total_localtax1,
'MT'));
486 $sql .=
", total_localtax2=".((float)
price2num($this->total_localtax2,
'MT'));
487 $sql .=
", tva_tx=".((float) $this->
vatrate);
488 $sql .=
", vat_src_code='".$this->db->escape($this->vat_src_code).
"'";
489 $sql .=
", localtax1_tx=".((float) $this->localtax1_tx);
490 $sql .=
", localtax2_tx=".((float) $this->localtax2_tx);
491 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
492 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
493 $sql .=
", rule_warning_message='".$this->db->escape($this->rule_warning_message).
"'";
494 $sql .=
", fk_c_exp_tax_cat = ".((int) $this->fk_c_exp_tax_cat);
495 $sql .=
", fk_ecm_files = ".($this->fk_ecm_files > 0 ? ((int) $this->fk_ecm_files) :
'null');
496 if ($this->fk_c_type_fees) {
497 $sql .=
", fk_c_type_fees = ".((int) $this->fk_c_type_fees);
499 $sql .=
", fk_c_type_fees=null";
501 if ($this->fk_project > 0) {
502 $sql .=
", fk_projet=".((int) $this->fk_project);
504 $sql .=
", fk_projet=null";
506 $sql .=
" WHERE rowid = ".((int) ($this->rowid ? $this->rowid : ((int) $this->
id)));
510 $resql = $this->db->query($sql);
513 $result = $tmpparent->fetch($this->fk_expensereport);
515 $result = $tmpparent->update_price(1);
518 $this->error = $tmpparent->error;
519 $this->errors = $tmpparent->errors;
523 $this->error = $tmpparent->error;
524 $this->errors = $tmpparent->errors;
528 dol_print_error($this->db);
531 if (!$error && !$notrigger) {
533 $result = $this->call_trigger(
'EXPENSE_REPORT_DET_MODIFY', $user);
544 $this->error = $this->db->lasterror();
545 dol_syslog(
"ExpenseReportLine::update Error ".$this->error, LOG_ERR);
546 $this->db->rollback();