From efd3c4d86354a4e881965c526f9289af6b8dfbd7 Mon Sep 17 00:00:00 2001 From: alainabbas Date: Thu, 25 Jun 2015 20:57:01 +0200 Subject: [PATCH 01/17] Correct bug #3050 when a piece is in draft and odt2pdf.sh is used, the filename is not corrected escaped for the shell --- htdocs/includes/odtphp/odf.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/includes/odtphp/odf.php b/htdocs/includes/odtphp/odf.php index a13a3923eab..26d1c057219 100644 --- a/htdocs/includes/odtphp/odf.php +++ b/htdocs/includes/odtphp/odf.php @@ -489,11 +489,11 @@ IMG; $name=preg_replace('/\.odt/i', '', $name); if (!empty($conf->global->MAIN_DOL_SCRIPTS_ROOT)) { - $command = $conf->global->MAIN_DOL_SCRIPTS_ROOT.'/scripts/odt2pdf/odt2pdf.sh '.$name.' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF); + $command = $conf->global->MAIN_DOL_SCRIPTS_ROOT.'/scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($name).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF); } else { - $command = '../../scripts/odt2pdf/odt2pdf.sh '.$name.' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF); + $command = '../../scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($name).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF); } From cb36a919b540ea895fa33d0d3d99c3da0cebd908 Mon Sep 17 00:00:00 2001 From: aspangaro Date: Fri, 26 Jun 2015 05:34:12 +0200 Subject: [PATCH 02/17] HRM area : Show Ref instead of id for expense report --- htdocs/compta/hrm.php | 6 ++---- htdocs/langs/en_US/trips.lang | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/htdocs/compta/hrm.php b/htdocs/compta/hrm.php index 4df30c023fc..f33e8bd3dd7 100644 --- a/htdocs/compta/hrm.php +++ b/htdocs/compta/hrm.php @@ -196,7 +196,7 @@ if (! empty($conf->deplacement->enabled) && $user->rights->deplacement->lire) if (! empty($conf->expensereport->enabled) && $user->rights->expensereport->lire) { - $sql = "SELECT u.rowid as uid, u.lastname, u.firstname, x.rowid, x.date_debut as date, x.tms as dm, x.total_ttc, x.fk_statut as status"; + $sql = "SELECT u.rowid as uid, u.lastname, u.firstname, x.rowid, x.ref, x.date_debut as date, x.tms as dm, x.total_ttc, x.fk_statut as status"; $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as x, ".MAIN_DB_PREFIX."user as u"; if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE u.rowid = x.fk_user_author"; @@ -231,13 +231,11 @@ if (! empty($conf->expensereport->enabled) && $user->rights->expensereport->lire while ($i < $num && $i < $max) { $obj = $db->fetch_object($result); - $expensereportstatic->ref=$obj->rowid; - $expensereportstatic->id=$obj->rowid; $userstatic->id=$obj->uid; $userstatic->lastname=$obj->lastname; $userstatic->firstname=$obj->firstname; print ''; - print ''.$expensereportstatic->getNomUrl(1).''; + print ''.img_object($langs->trans("ShowTrip"),"trip").' '.$obj->ref.''; print ''.$userstatic->getNomUrl(1).''; print ''.price($obj->total_ttc).''; print ''.dol_print_date($db->jdate($obj->dm),'day').''; diff --git a/htdocs/langs/en_US/trips.lang b/htdocs/langs/en_US/trips.lang index 969ceaa01d6..cc14a66aea5 100644 --- a/htdocs/langs/en_US/trips.lang +++ b/htdocs/langs/en_US/trips.lang @@ -9,6 +9,7 @@ TripCard=Expense report card AddTrip=Create expense report ListOfTrips=List of expense reports ListOfFees=List of fees +ShowTrip=Show expense report NewTrip=New expense report CompanyVisited=Company/foundation visited Kilometers=Kilometers From cff82a52266395e793505fcec8cf8ad9317da87c Mon Sep 17 00:00:00 2001 From: aspangaro Date: Mon, 29 Jun 2015 06:41:24 +0200 Subject: [PATCH 03/17] Use ref instead of rowid for expense report --- htdocs/compta/hrm.php | 4 +- .../class/expensereport.class.php | 41 ++++++++++++++----- htdocs/expensereport/list.php | 7 +++- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/htdocs/compta/hrm.php b/htdocs/compta/hrm.php index f33e8bd3dd7..3ef716ca548 100644 --- a/htdocs/compta/hrm.php +++ b/htdocs/compta/hrm.php @@ -231,11 +231,13 @@ if (! empty($conf->expensereport->enabled) && $user->rights->expensereport->lire while ($i < $num && $i < $max) { $obj = $db->fetch_object($result); + $expensereportstatic->id=$obj->rowid; + $expensereportstatic->ref=$obj->ref; $userstatic->id=$obj->uid; $userstatic->lastname=$obj->lastname; $userstatic->firstname=$obj->firstname; print ''; - print ''.img_object($langs->trans("ShowTrip"),"trip").' '.$obj->ref.''; + print ''.$expensereportstatic->getNomUrl(1).''; print ''.$userstatic->getNomUrl(1).''; print ''.price($obj->total_ttc).''; print ''.dol_print_date($db->jdate($obj->dm),'day').''; diff --git a/htdocs/expensereport/class/expensereport.class.php b/htdocs/expensereport/class/expensereport.class.php index 0aa45386a85..9858df308ce 100644 --- a/htdocs/expensereport/class/expensereport.class.php +++ b/htdocs/expensereport/class/expensereport.class.php @@ -371,7 +371,7 @@ class ExpenseReport extends CommonObject function set_paid($id, $fuser) { $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport"; - $sql.= " SET fk_statut = 6; + $sql.= " SET fk_statut = 6"; $sql.= " WHERE rowid = $id AND fk_statut = 5"; dol_syslog(get_class($this)."::set_paid sql=".$sql, LOG_DEBUG); @@ -1074,6 +1074,11 @@ class ExpenseReport extends CommonObject } } + /** + * Return next reference of expense report not already used + * + * @return string free ref + */ function getNextNumRef() { global $conf; @@ -1109,12 +1114,11 @@ class ExpenseReport extends CommonObject endif; } - /** * Return clicable name (with picto eventually) * * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto - * @return string Chaine avec URL + * @return string String with URL */ function getNomUrl($withpicto=0) { @@ -1135,8 +1139,15 @@ class ExpenseReport extends CommonObject return $result; } - - function update_totaux_add($ligne_total_ht,$ligne_total_tva){ + /** + * Update total of an expense report when you add a line. + * + * @param string $ligne_total_ht + * @param string $ligne_total_tva + * @return void + */ + function update_totaux_add($ligne_total_ht,$ligne_total_tva) + { $this->total_ht = $this->total_ht + $ligne_total_ht; $this->total_tva = $this->total_tva + $ligne_total_tva; $this->total_ttc = $this->total_ht + $this->total_tva; @@ -1156,7 +1167,15 @@ class ExpenseReport extends CommonObject endif; } - function update_totaux_del($ligne_total_ht,$ligne_total_tva){ + /** + * Update total of an expense report when you delete a line. + * + * @param string $ligne_total_ht + * @param string $ligne_total_tva + * @return void + */ + function update_totaux_del($ligne_total_ht,$ligne_total_tva) + { $this->total_ht = $this->total_ht - $ligne_total_ht; $this->total_tva = $this->total_tva - $ligne_total_tva; $this->total_ttc = $this->total_ht + $this->total_tva; @@ -1490,12 +1509,12 @@ class ExpenseReportLine /** * fetch record * - * @param int $rowid Row id to fetch + * @param int $rowid Id of object to load * @return int <0 if KO, >0 if OK */ - function fetch($rowid) + function fetch($rowid, $ref='') { - $sql = 'SELECT fde.rowid, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_projet, fde.date,'; + $sql = 'SELECT fde.rowid, fde.ref, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_projet, fde.date,'; $sql.= ' fde.tva_tx as vatrate, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc,'; $sql.= ' ctf.code as type_fees_code, ctf.label as type_fees_libelle,'; $sql.= ' pjt.rowid as projet_id, pjt.title as projet_title, pjt.ref as projet_ref'; @@ -1503,7 +1522,7 @@ class ExpenseReportLine $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id'; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pjt ON fde.fk_projet=pjt.rowid'; $sql.= ' WHERE fde.rowid = '.$rowid; - + $result = $this->db->query($sql); if($result) @@ -1511,6 +1530,8 @@ class ExpenseReportLine $objp = $this->db->fetch_object($result); $this->rowid = $objp->rowid; + $this->id = $obj->rowid; + $this->ref = $obj->ref; $this->fk_expensereport = $objp->fk_expensereport; $this->comments = $objp->comments; $this->qty = $objp->qty; diff --git a/htdocs/expensereport/list.php b/htdocs/expensereport/list.php index c0b30b6c873..61032f268cf 100644 --- a/htdocs/expensereport/list.php +++ b/htdocs/expensereport/list.php @@ -245,16 +245,21 @@ if ($resql) $total_total_ht = 0; $total_total_ttc = 0; $total_total_tva = 0; + + $expensereportstatic=new ExpenseReport($db); if($num > 0) { while ($i < min($num,$limit)) { $objp = $db->fetch_object($resql); + + $expensereportstatic->id=$objp->rowid; + $expensereportstatic->ref=$objp->ref; $var=!$var; print ""; - print ''.img_object($langs->trans("ShowTrip"),"trip").' '.$objp->ref.''; + print ''.$expensereportstatic->getNomUrl(1).''; print ''.($objp->date_debut > 0 ? dol_print_date($objp->date_debut, 'day') : '').''; print ''.($objp->date_fin > 0 ? dol_print_date($objp->date_fin, 'day') : '').''; print ''.img_object($langs->trans("ShowUser"),"user").' '.dolGetFirstLastname($objp->firstname, $objp->lastname).''; From cb0aa4138615c6d22dba759fe8ca675827f03bd0 Mon Sep 17 00:00:00 2001 From: aspangaro Date: Mon, 29 Jun 2015 06:48:39 +0200 Subject: [PATCH 04/17] Debug --- htdocs/expensereport/class/expensereport.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/expensereport/class/expensereport.class.php b/htdocs/expensereport/class/expensereport.class.php index 3538770a111..dc9c533eb9b 100644 --- a/htdocs/expensereport/class/expensereport.class.php +++ b/htdocs/expensereport/class/expensereport.class.php @@ -1512,7 +1512,7 @@ class ExpenseReportLine * @param int $rowid Id of object to load * @return int <0 if KO, >0 if OK */ - function fetch($rowid, $ref='') + function fetch($rowid) { $sql = 'SELECT fde.rowid, fde.ref, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_projet, fde.date,'; $sql.= ' fde.tva_tx as vatrate, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc,'; @@ -1522,7 +1522,7 @@ class ExpenseReportLine $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id'; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pjt ON fde.fk_projet=pjt.rowid'; $sql.= ' WHERE fde.rowid = '.$rowid; - + $result = $this->db->query($sql); if($result) From 279d1d4320d425f2a227cb57aea1028ab8c8868d Mon Sep 17 00:00:00 2001 From: aspangaro Date: Mon, 29 Jun 2015 12:59:55 +0200 Subject: [PATCH 05/17] Correct travis --- htdocs/expensereport/class/expensereport.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/htdocs/expensereport/class/expensereport.class.php b/htdocs/expensereport/class/expensereport.class.php index dc9c533eb9b..be338cad0d3 100644 --- a/htdocs/expensereport/class/expensereport.class.php +++ b/htdocs/expensereport/class/expensereport.class.php @@ -1142,9 +1142,9 @@ class ExpenseReport extends CommonObject /** * Update total of an expense report when you add a line. * - * @param string $ligne_total_ht - * @param string $ligne_total_tva - * @return void + * @param string $ligne_total_ht Amount without taxes + * @param string $ligne_total_tva Amount of all taxes + * @return void */ function update_totaux_add($ligne_total_ht,$ligne_total_tva) { @@ -1170,9 +1170,9 @@ class ExpenseReport extends CommonObject /** * Update total of an expense report when you delete a line. * - * @param string $ligne_total_ht - * @param string $ligne_total_tva - * @return void + * @param string $ligne_total_ht Amount without taxes + * @param string $ligne_total_tva Amount of all taxes + * @return void */ function update_totaux_del($ligne_total_ht,$ligne_total_tva) { From 7e54840cb6fa122a07756d57f41d696358996b38 Mon Sep 17 00:00:00 2001 From: aspangaro Date: Mon, 29 Jun 2015 13:12:37 +0200 Subject: [PATCH 06/17] Replace tabs with spaces --- .../class/expensereport.class.php | 3226 ++++++++--------- 1 file changed, 1613 insertions(+), 1613 deletions(-) diff --git a/htdocs/expensereport/class/expensereport.class.php b/htdocs/expensereport/class/expensereport.class.php index be338cad0d3..e36040743ad 100644 --- a/htdocs/expensereport/class/expensereport.class.php +++ b/htdocs/expensereport/class/expensereport.class.php @@ -29,352 +29,352 @@ require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php'; */ class ExpenseReport extends CommonObject { - var $db; - var $error; - var $element='expensereport'; - var $table_element='expensereport'; - var $table_element_line = 'expensereport_det'; - var $fk_element = 'fk_expensereport'; + var $db; + var $error; + var $element='expensereport'; + var $table_element='expensereport'; + var $table_element_line = 'expensereport_det'; + var $fk_element = 'fk_expensereport'; - var $id; - var $ref; - var $lignes=array(); - var $total_ht; - var $total_tva; - var $total_ttc; - var $note_public; - var $note_private; - var $date_debut; - var $date_fin; + var $id; + var $ref; + var $lignes=array(); + var $total_ht; + var $total_tva; + var $total_ttc; + var $note_public; + var $note_private; + var $date_debut; + var $date_fin; - var $fk_user_validator; - var $status; - var $fk_statut; // -- 1=draft, 2=validated (attente approb), 4=canceled, 5=approved, 6=payed, 99=denied - var $fk_c_paiement; - var $paid; + var $fk_user_validator; + var $status; + var $fk_statut; // -- 1=draft, 2=validated (attente approb), 4=canceled, 5=approved, 6=payed, 99=denied + var $fk_c_paiement; + var $paid; - var $user_author_infos; - var $user_validator_infos; + var $user_author_infos; + var $user_validator_infos; var $modepayment; var $modepaymentid; - var $code_paiement; - var $code_statut; + var $code_paiement; + var $code_statut; - /* - ACTIONS - */ + /* + ACTIONS + */ - // Enregistrement - var $date_create; - var $fk_user_author; + // Enregistrement + var $date_create; + var $fk_user_author; - // Refus - var $date_refuse; - var $detail_refuse; - var $fk_user_refuse; + // Refus + var $date_refuse; + var $detail_refuse; + var $fk_user_refuse; - // Annulation - var $date_cancel; - var $detail_cancel; - var $fk_user_cancel; + // Annulation + var $date_cancel; + var $detail_cancel; + var $fk_user_cancel; - // Validation - var $date_valid; - var $fk_user_valid; - var $user_valid_infos; + // Validation + var $date_valid; + var $fk_user_valid; + var $user_valid_infos; - // Approve - var $date_approve; - var $fk_user_approve; + // Approve + var $date_approve; + var $fk_user_approve; - // Paiement - var $user_paid_infos; + // Paiement + var $user_paid_infos; - /* - END ACTIONS - */ + /* + END ACTIONS + */ - /** - * Constructor - * - * @param DoliDB $db Handler acces base de donnees - */ - function __construct($db) - { - $this->db = $db; - $this->total_ht = 0; - $this->total_ttc = 0; - $this->total_tva = 0; - $this->modepaymentid = 0; + /** + * Constructor + * + * @param DoliDB $db Handler acces base de donnees + */ + function __construct($db) + { + $this->db = $db; + $this->total_ht = 0; + $this->total_ttc = 0; + $this->total_tva = 0; + $this->modepaymentid = 0; - // List of language codes for status + // List of language codes for status $this->statuts_short = array(0 => 'Draft', 2 => 'Validated', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused'); $this->statuts = array(0 => 'Draft', 2 => 'ValidatedWaitingApproval', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused'); $this->statuts_logo = array(0 => 'statut0', 2 => 'statut1', 4 => 'statut5', 5 => 'statut3', 6 => 'statut6', 99 => 'statut8'); - return 1; - } + return 1; + } - /** - * Create object in database - * - * @param User $user User that create - * @return int <0 if KO, >0 if OK - */ - function create($user) - { - global $conf; + /** + * Create object in database + * + * @param User $user User that create + * @return int <0 if KO, >0 if OK + */ + function create($user) + { + global $conf; - $now = dol_now(); + $now = dol_now(); - $this->db->begin(); + $this->db->begin(); - $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." ("; - $sql.= "ref"; - $sql.= ",total_ht"; - $sql.= ",total_ttc"; - $sql.= ",total_tva"; - $sql.= ",date_debut"; - $sql.= ",date_fin"; - $sql.= ",date_create"; - $sql.= ",fk_user_author"; - $sql.= ",fk_user_validator"; - $sql.= ",fk_statut"; - $sql.= ",fk_c_paiement"; - $sql.= ",paid"; - $sql.= ",note_public"; - $sql.= ",note_private"; - $sql.= ") VALUES("; - $sql.= "'(PROV)'"; - $sql.= ", ".$this->total_ht; - $sql.= ", ".$this->total_ttc; - $sql.= ", ".$this->total_tva; - $sql.= ", '".$this->db->idate($this->date_debut)."'"; - $sql.= ", '".$this->db->idate($this->date_fin)."'"; - $sql.= ", '".$this->db->idate($now)."'"; - $sql.= ", ".($user->id > 0 ? $user->id:"null"); - $sql.= ", ".($this->fk_user_validator > 0 ? $this->fk_user_validator:"null"); - $sql.= ", ".($this->fk_statut > 1 ? $this->fk_statut:0); - $sql.= ", ".($this->modepaymentid?$this->modepaymentid:"null"); - $sql.= ", 0"; - $sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null"); - $sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null"); - $sql.= ")"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." ("; + $sql.= "ref"; + $sql.= ",total_ht"; + $sql.= ",total_ttc"; + $sql.= ",total_tva"; + $sql.= ",date_debut"; + $sql.= ",date_fin"; + $sql.= ",date_create"; + $sql.= ",fk_user_author"; + $sql.= ",fk_user_validator"; + $sql.= ",fk_statut"; + $sql.= ",fk_c_paiement"; + $sql.= ",paid"; + $sql.= ",note_public"; + $sql.= ",note_private"; + $sql.= ") VALUES("; + $sql.= "'(PROV)'"; + $sql.= ", ".$this->total_ht; + $sql.= ", ".$this->total_ttc; + $sql.= ", ".$this->total_tva; + $sql.= ", '".$this->db->idate($this->date_debut)."'"; + $sql.= ", '".$this->db->idate($this->date_fin)."'"; + $sql.= ", '".$this->db->idate($now)."'"; + $sql.= ", ".($user->id > 0 ? $user->id:"null"); + $sql.= ", ".($this->fk_user_validator > 0 ? $this->fk_user_validator:"null"); + $sql.= ", ".($this->fk_statut > 1 ? $this->fk_statut:0); + $sql.= ", ".($this->modepaymentid?$this->modepaymentid:"null"); + $sql.= ", 0"; + $sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null"); + $sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null"); + $sql.= ")"; - dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG); - $result = $this->db->query($sql); - if ($result) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); - $this->ref='(PROV'.$this->id.')'; + dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); + $this->ref='(PROV'.$this->id.')'; - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element." SET ref='".$this->ref."' WHERE rowid=".$this->id; - dol_syslog(get_class($this)."::create sql=".$sql); - $resql=$this->db->query($sql); - if (!$resql) $error++; + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element." SET ref='".$this->ref."' WHERE rowid=".$this->id; + dol_syslog(get_class($this)."::create sql=".$sql); + $resql=$this->db->query($sql); + if (!$resql) $error++; - foreach ($this->lignes as $i => $val) - { - $newndfline=new ExpenseReportLine($this->db); - $newndfline=$this->lignes[$i]; - $newndfline->fk_expensereport=$this->id; - if ($result >= 0) - { - $result=$newndfline->insert(); - } - if ($result < 0) - { - $error++; - break; - } - } + foreach ($this->lignes as $i => $val) + { + $newndfline=new ExpenseReportLine($this->db); + $newndfline=$this->lignes[$i]; + $newndfline->fk_expensereport=$this->id; + if ($result >= 0) + { + $result=$newndfline->insert(); + } + if ($result < 0) + { + $error++; + break; + } + } - if (! $error) - { - $result=$this->update_price(); - if ($result > 0) - { - $this->db->commit(); - return $this->id; - } - else - { - $this->db->rollback(); - return -3; - } - } - else - { - dol_syslog(get_class($this)."::create error ".$this->error, LOG_ERR); - $this->db->rollback(); - return -2; - } - } - else - { - $this->error=$this->db->error()." sql=".$sql; - $this->db->rollback(); - return -1; - } + if (! $error) + { + $result=$this->update_price(); + if ($result > 0) + { + $this->db->commit(); + return $this->id; + } + else + { + $this->db->rollback(); + return -3; + } + } + else + { + dol_syslog(get_class($this)."::create error ".$this->error, LOG_ERR); + $this->db->rollback(); + return -2; + } + } + else + { + $this->error=$this->db->error()." sql=".$sql; + $this->db->rollback(); + return -1; + } - } + } - /** - * update - * - * @param User $user User making change - * @return int <0 if KO, >0 if OK - */ - function update($user) - { - global $langs; + /** + * update + * + * @param User $user User making change + * @return int <0 if KO, >0 if OK + */ + function update($user) + { + global $langs; - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql.= " total_ht = ".$this->total_ht; - $sql.= " , total_ttc = ".$this->total_ttc; - $sql.= " , total_tva = ".$this->total_tva; - $sql.= " , date_debut = '".$this->db->idate($this->date_debut)."'"; - $sql.= " , date_fin = '".$this->db->idate($this->date_fin)."'"; - $sql.= " , fk_user_author = ".($user->id > 0 ? "'".$user->id."'":"null"); - $sql.= " , fk_user_validator = ".($this->fk_user_validator > 0 ? $this->fk_user_validator:"null"); - $sql.= " , fk_user_valid = ".($this->fk_user_valid > 0 ? $this->fk_user_valid:"null"); - $sql.= " , fk_statut = ".($this->fk_statut >= 0 ? $this->fk_statut:'0'); - $sql.= " , fk_c_paiement = ".($this->fk_c_paiement > 0 ? $this->fk_c_paiement:"null"); - $sql.= " , note_public = ".(!empty($this->note_public)?"'".$this->db->escape($this->note_public)."'":"''"); - $sql.= " , note_private = ".(!empty($this->note_private)?"'".$this->db->escape($this->note_private)."'":"''"); - $sql.= " , detail_refuse = ".(!empty($this->detail_refuse)?"'".$this->db->escape($this->detail_refuse)."'":"''"); - $sql.= " WHERE rowid = ".$this->id; + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql.= " total_ht = ".$this->total_ht; + $sql.= " , total_ttc = ".$this->total_ttc; + $sql.= " , total_tva = ".$this->total_tva; + $sql.= " , date_debut = '".$this->db->idate($this->date_debut)."'"; + $sql.= " , date_fin = '".$this->db->idate($this->date_fin)."'"; + $sql.= " , fk_user_author = ".($user->id > 0 ? "'".$user->id."'":"null"); + $sql.= " , fk_user_validator = ".($this->fk_user_validator > 0 ? $this->fk_user_validator:"null"); + $sql.= " , fk_user_valid = ".($this->fk_user_valid > 0 ? $this->fk_user_valid:"null"); + $sql.= " , fk_statut = ".($this->fk_statut >= 0 ? $this->fk_statut:'0'); + $sql.= " , fk_c_paiement = ".($this->fk_c_paiement > 0 ? $this->fk_c_paiement:"null"); + $sql.= " , note_public = ".(!empty($this->note_public)?"'".$this->db->escape($this->note_public)."'":"''"); + $sql.= " , note_private = ".(!empty($this->note_private)?"'".$this->db->escape($this->note_private)."'":"''"); + $sql.= " , detail_refuse = ".(!empty($this->detail_refuse)?"'".$this->db->escape($this->detail_refuse)."'":"''"); + $sql.= " WHERE rowid = ".$this->id; - dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG); - $result = $this->db->query($sql); - if ($result) - { - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } + dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) + { + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } - /** - * Load an object from database - * - * @param int $id Id - * @param string $ref Ref - * @return int <0 if KO, >0 if OK - */ - function fetch($id, $ref='') - { - global $conf; + /** + * Load an object from database + * + * @param int $id Id + * @param string $ref Ref + * @return int <0 if KO, >0 if OK + */ + function fetch($id, $ref='') + { + global $conf; - $sql = "SELECT d.rowid, d.ref, d.note_public, d.note_private,"; // DEFAULT - $sql.= " d.detail_refuse, d.detail_cancel, d.fk_user_refuse, d.fk_user_cancel,"; // ACTIONS - $sql.= " d.date_refuse, d.date_cancel,"; // ACTIONS - $sql.= " d.total_ht, d.total_ttc, d.total_tva,"; // TOTAUX (int) - $sql.= " d.date_debut, d.date_fin, d.date_create, d.date_valid, d.date_approve,"; // DATES (datetime) - $sql.= " d.fk_user_author, d.fk_user_validator, d.fk_statut as status, d.fk_c_paiement,"; - $sql.= " d.fk_user_valid, d.fk_user_approve,"; - $sql.= " dp.libelle as libelle_paiement, dp.code as code_paiement"; // INNER JOIN paiement - $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as d LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as dp ON d.fk_c_paiement = dp.id"; - if ($ref) $sql.= " WHERE d.ref = '".$this->db->escape($ref)."'"; - else $sql.= " WHERE d.rowid = ".$id; - $sql.= $restrict; + $sql = "SELECT d.rowid, d.ref, d.note_public, d.note_private,"; // DEFAULT + $sql.= " d.detail_refuse, d.detail_cancel, d.fk_user_refuse, d.fk_user_cancel,"; // ACTIONS + $sql.= " d.date_refuse, d.date_cancel,"; // ACTIONS + $sql.= " d.total_ht, d.total_ttc, d.total_tva,"; // TOTAUX (int) + $sql.= " d.date_debut, d.date_fin, d.date_create, d.date_valid, d.date_approve,"; // DATES (datetime) + $sql.= " d.fk_user_author, d.fk_user_validator, d.fk_statut as status, d.fk_c_paiement,"; + $sql.= " d.fk_user_valid, d.fk_user_approve,"; + $sql.= " dp.libelle as libelle_paiement, dp.code as code_paiement"; // INNER JOIN paiement + $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as d LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as dp ON d.fk_c_paiement = dp.id"; + if ($ref) $sql.= " WHERE d.ref = '".$this->db->escape($ref)."'"; + else $sql.= " WHERE d.rowid = ".$id; + $sql.= $restrict; - dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG); - $resql = $this->db->query($sql) ; - if ($resql) - { - $obj = $this->db->fetch_object($resql); - if ($obj) - { - $this->id = $obj->rowid; - $this->ref = $obj->ref; - $this->total_ht = $obj->total_ht; - $this->total_tva = $obj->total_tva; - $this->total_ttc = $obj->total_ttc; - $this->note_public = $obj->note_public; - $this->note_private = $obj->note_private; - $this->detail_refuse = $obj->detail_refuse; - $this->detail_cancel = $obj->detail_cancel; + dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG); + $resql = $this->db->query($sql) ; + if ($resql) + { + $obj = $this->db->fetch_object($resql); + if ($obj) + { + $this->id = $obj->rowid; + $this->ref = $obj->ref; + $this->total_ht = $obj->total_ht; + $this->total_tva = $obj->total_tva; + $this->total_ttc = $obj->total_ttc; + $this->note_public = $obj->note_public; + $this->note_private = $obj->note_private; + $this->detail_refuse = $obj->detail_refuse; + $this->detail_cancel = $obj->detail_cancel; - $this->date_debut = $this->db->jdate($obj->date_debut); - $this->date_fin = $this->db->jdate($obj->date_fin); - $this->date_valid = $this->db->jdate($obj->date_valid); - $this->date_approve = $this->db->jdate($obj->date_approve); - $this->date_create = $this->db->jdate($obj->date_create); - $this->date_refuse = $this->db->jdate($obj->date_refuse); - $this->date_cancel = $this->db->jdate($obj->date_cancel); + $this->date_debut = $this->db->jdate($obj->date_debut); + $this->date_fin = $this->db->jdate($obj->date_fin); + $this->date_valid = $this->db->jdate($obj->date_valid); + $this->date_approve = $this->db->jdate($obj->date_approve); + $this->date_create = $this->db->jdate($obj->date_create); + $this->date_refuse = $this->db->jdate($obj->date_refuse); + $this->date_cancel = $this->db->jdate($obj->date_cancel); - $this->fk_user_author = $obj->fk_user_author; - $this->fk_user_validator = $obj->fk_user_validator; - $this->fk_user_valid = $obj->fk_user_valid; - $this->fk_user_refuse = $obj->fk_user_refuse; - $this->fk_user_cancel = $obj->fk_user_cancel; - $this->fk_user_approve = $obj->fk_user_approve; + $this->fk_user_author = $obj->fk_user_author; + $this->fk_user_validator = $obj->fk_user_validator; + $this->fk_user_valid = $obj->fk_user_valid; + $this->fk_user_refuse = $obj->fk_user_refuse; + $this->fk_user_cancel = $obj->fk_user_cancel; + $this->fk_user_approve = $obj->fk_user_approve; - $user_author = new User($this->db); - if ($this->fk_user_author > 0) $user_author->fetch($this->fk_user_author); + $user_author = new User($this->db); + if ($this->fk_user_author > 0) $user_author->fetch($this->fk_user_author); - $this->user_author_infos = dolGetFirstLastname($user_author->firstname, $user_author->lastname); + $this->user_author_infos = dolGetFirstLastname($user_author->firstname, $user_author->lastname); - $user_approver = new User($this->db); - if ($this->fk_user_validator > 0) $user_approver->fetch($this->fk_user_validator); - $this->user_validator_infos = dolGetFirstLastname($user_approver->firstname, $user_approver->lastname); + $user_approver = new User($this->db); + if ($this->fk_user_validator > 0) $user_approver->fetch($this->fk_user_validator); + $this->user_validator_infos = dolGetFirstLastname($user_approver->firstname, $user_approver->lastname); - $this->fk_statut = $obj->status; - $this->status = $obj->status; - $this->fk_c_paiement = $obj->fk_c_paiement; - $this->paid = $obj->paid; + $this->fk_statut = $obj->status; + $this->status = $obj->status; + $this->fk_c_paiement = $obj->fk_c_paiement; + $this->paid = $obj->paid; - if ($this->fk_statut==5 || $this->fk_statut==6) - { - $user_valid = new User($this->db); - if ($this->fk_user_valid > 0) $user_valid->fetch($this->fk_user_valid); - $this->user_valid_infos = dolGetFirstLastname($user_valid->firstname, $user_valid->lastname); - } + if ($this->fk_statut==5 || $this->fk_statut==6) + { + $user_valid = new User($this->db); + if ($this->fk_user_valid > 0) $user_valid->fetch($this->fk_user_valid); + $this->user_valid_infos = dolGetFirstLastname($user_valid->firstname, $user_valid->lastname); + } - $this->libelle_statut = $obj->libelle_statut; - $this->libelle_paiement = $obj->libelle_paiement; - $this->code_statut = $obj->code_statut; - $this->code_paiement = $obj->code_paiement; + $this->libelle_statut = $obj->libelle_statut; + $this->libelle_paiement = $obj->libelle_paiement; + $this->code_statut = $obj->code_statut; + $this->code_paiement = $obj->code_paiement; - $this->lignes = array(); // deprecated - $this->lines = array(); + $this->lignes = array(); // deprecated + $this->lines = array(); - $result=$this->fetch_lines(); + $result=$this->fetch_lines(); - return $result; - } - else - { - return 0; - } - } - else - { - $this->error=$this->db->lasterror(); - return -1; - } - } + return $result; + } + else + { + return 0; + } + } + else + { + $this->error=$this->db->lasterror(); + return -1; + } + } /** * Classify the expense report as paid * - * @param int $id Id of expense report - * @param user $fuser User making change - * @return int <0 if KO, >0 if OK + * @param int $id Id of expense report + * @param user $fuser User making change + * @return int <0 if KO, >0 if OK */ function set_paid($id, $fuser) { $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport"; - $sql.= " SET fk_statut = 6"; + $sql.= " SET fk_statut = 6"; $sql.= " WHERE rowid = ".$id." AND fk_statut = 5"; - dol_syslog(get_class($this)."::set_paid sql=".$sql, LOG_DEBUG); + dol_syslog(get_class($this)."::set_paid sql=".$sql, LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) { @@ -394,1075 +394,1075 @@ class ExpenseReport extends CommonObject } } - /** - * Returns the label status - * - * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto - * @return string Label - */ - function getLibStatut($mode=0) - { - return $this->LibStatut($this->status,$mode); - } - - /** - * Returns the label of a statut - * - * @param int $status id statut - * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto - * @return string Label - */ - function LibStatut($status,$mode=0) - { - global $langs; - - if ($mode == 0) - return $langs->trans($this->statuts[$status]); - - if ($mode == 1) - return $langs->trans($this->statuts_short[$status]); - - if ($mode == 2) - return img_picto($langs->trans($this->statuts_short[$status]), $this->statuts_logo[$status]).' '.$langs->trans($this->statuts_short[$status]); - - if ($mode == 3) - return img_picto($langs->trans($this->statuts_short[$status]), $this->statuts_logo[$status]); - - if ($mode == 4) - return img_picto($langs->trans($this->statuts_short[$status]),$this->statuts_logo[$status]).' '.$langs->trans($this->statuts[$status]); - - if ($mode == 5) - return ''.$langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),$this->statuts_logo[$status]); - - } - - - /** - * Load information on object - * - * @param int $id Id of object - * @return void - */ - function info($id) - { - global $conf; - - $sql = "SELECT f.rowid,"; - $sql.= " f.date_create as datec,"; - $sql.= " f.tms as date_modification,"; - $sql.= " f.date_valid as datev,"; - $sql.= " f.date_approve as datea,"; - $sql.= " f.fk_user_author as fk_user_creation,"; - $sql.= " f.fk_user_modif as fk_user_modification,"; - $sql.= " f.fk_user_valid,"; - $sql.= " f.fk_user_approve"; - $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as f"; - $sql.= " WHERE f.rowid = ".$id; - $sql.= " AND f.entity = ".$conf->entity; - - $resql = $this->db->query($sql); - if ($resql) - { - if ($this->db->num_rows($resql)) - { - $obj = $this->db->fetch_object($resql); - - $this->id = $obj->rowid; - - $this->date_creation = $this->db->jdate($obj->datec); - $this->date_modification = $this->db->jdate($obj->date_modification); - $this->date_validation = $this->db->jdate($obj->datev); - $this->date_approbation = $this->db->jdate($obj->datea); - - $cuser = new User($this->db); - $cuser->fetch($obj->fk_user_author); - $this->user_creation = $cuser; - - if ($obj->fk_user_creation) - { - $cuser = new User($this->db); - $cuser->fetch($obj->fk_user_creation); - $this->user_creation = $cuser; - } - if ($obj->fk_user_valid) - { - $vuser = new User($this->db); - $vuser->fetch($obj->fk_user_valid); - $this->user_validation = $vuser; - } - if ($obj->fk_user_modification) - { - $muser = new User($this->db); - $muser->fetch($obj->fk_user_modification); - $this->user_modification = $muser; - } - - } - $this->db->free($resql); - } - else - { - dol_print_error($this->db); - } - } - - - - /** - * Initialise an instance with random values. - * Used to build previews or test instances. - * id must be 0 if object instance is a specimen. - * - * @return void - */ - function initAsSpecimen() - { - global $user,$langs,$conf; - - $now=dol_now(); - - // Initialise parametres - $this->id=0; - $this->ref = 'SPECIMEN'; - $this->specimen=1; - $this->date_create = $now; - $this->date_debut = $now; - $this->date_fin = $now; - $this->date_approve = $now; - - $this->status = 5; - $this->fk_statut = 5; - - $this->fk_user_author = $user->id; - $this->fk_user_valid = $user->id; - $this->fk_user_approve = $user->id; - $this->fk_user_validator = $user->id; - - $this->note_private='Private note'; - $this->note_public='SPECIMEN'; - $nbp = 5; - $xnbp = 0; - while ($xnbp < $nbp) - { - $line=new ExpenseReportLine($this->db); - $line->comments=$langs->trans("Comment")." ".$xnbp; - $line->date=($now-3600*(1+$xnbp)); - $line->total_ht=100; - $line->total_tva=20; - $line->total_ttc=120; - $line->qty=1; - $line->vatrate=20; - $line->value_unit=120; - $line->fk_expensereport=0; - $line->type_fees_code='TRA'; - - $line->projet_ref = 'ABC'; - - $this->lines[$xnbp]=$line; - $xnbp++; - - $this->total_ht+=$line->total_ht; - $this->total_tva+=$line->total_tva; - $this->total_ttc+=$line->total_ttc; - } - } - - /** - * fetch_line_by_project - * - * @param int $projectid Project id - * @param User $user User - * @return int <0 if KO, >0 if OK - */ - function fetch_line_by_project($projectid,$user='') - { - global $conf,$db,$langs; - - $langs->load('trips'); - - if($user->rights->expensereport->lire) { - - $sql = "SELECT de.fk_expensereport, de.date, de.comments, de.total_ht, de.total_ttc"; - $sql.= " FROM ".MAIN_DB_PREFIX."expensereport_det as de"; - $sql.= " WHERE de.fk_projet = ".$projectid; - - dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG); - $result = $db->query($sql) ; - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - $total_HT = 0; - $total_TTC = 0; - - while ($i < $num) - { - - $objp = $db->fetch_object($result); - - $sql2 = "SELECT d.rowid, d.fk_user_author, d.ref, d.fk_statut"; - $sql2.= " FROM ".MAIN_DB_PREFIX."expensereport as d"; - $sql2.= " WHERE d.rowid = '".$objp->fk_expensereport."'"; - - $result2 = $db->query($sql2); - $obj = $db->fetch_object($result2); - - $objp->fk_user_author = $obj->fk_user_author; - $objp->ref = $obj->ref; - $objp->fk_c_expensereport_status = $obj->fk_statut; - $objp->rowid = $obj->rowid; - - $total_HT = $total_HT + $objp->total_ht; - $total_TTC = $total_TTC + $objp->total_ttc; - $author = new User($db); - $author->fetch($objp->fk_user_author); - - print ''; - print ''.$objp->ref_num.''; - print ''.dol_print_date($objp->date,'day').''; - print ''.$author->getNomUrl().''; - print ''.$objp->comments.''; - print ''.price($objp->total_ht).''; - print ''.price($objp->total_ttc).''; - print ''; - - switch($objp->fk_c_expensereport_status) { - case 4: - print img_picto($langs->trans('StatusOrderCanceled'),'statut5'); - break; - case 1: - print $langs->trans('Draft').' '.img_picto($langs->trans('Draft'),'statut0'); - break; - case 2: - print $langs->trans('TripForValid').' '.img_picto($langs->trans('TripForValid'),'statut3'); - break; - case 5: - print $langs->trans('TripForPaid').' '.img_picto($langs->trans('TripForPaid'),'statut3'); - break; - case 6: - print $langs->trans('TripPaid').' '.img_picto($langs->trans('TripPaid'),'statut4'); - break; - } - /* - if ($status==4) return img_picto($langs->trans('StatusOrderCanceled'),'statut5'); - if ($status==1) return img_picto($langs->trans('StatusOrderDraft'),'statut0'); - if ($status==2) return img_picto($langs->trans('StatusOrderValidated'),'statut1'); - if ($status==2) return img_picto($langs->trans('StatusOrderOnProcess'),'statut3'); - if ($status==5) return img_picto($langs->trans('StatusOrderToBill'),'statut4'); - if ($status==6) return img_picto($langs->trans('StatusOrderOnProcess'),'statut6'); - */ - print ''; - print ''; - - $i++; - } - - print ''.$langs->trans("Number").': '.$i.''; - print ''.$langs->trans("TotalHT").' : '.price($total_HT).''; - print ''.$langs->trans("TotalTTC").' : '.price($total_TTC).''; - print ' '; - print ''; - - } - else - { - $this->error=$db->error(); - return -1; - } - } - - } - - /** - * recalculer - * TODO Replace this with call to update_price if not already done - * - * @param int $id Id of expense report - * @return int <0 if KO, >0 if OK - */ - function recalculer($id) - { - $sql = 'SELECT tt.total_ht, tt.total_ttc, tt.total_tva'; - $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as tt'; - $sql.= ' WHERE tt.'.$this->fk_element.' = '.$id; - - $total_ht = 0; $total_tva = 0; $total_ttc = 0; - - dol_syslog('ExpenseReport::recalculer sql='.$sql,LOG_DEBUG); - - $result = $this->db->query($sql); - if($result) - { - $num = $this->db->num_rows($result); - $i = 0; - while ($i < $num): - $objp = $this->db->fetch_object($result); - $total_ht+=$objp->total_ht; - $total_tva+=$objp->total_tva; - $i++; - endwhile; - - $total_ttc = $total_ht + $total_tva; - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql.= " total_ht = ".$total_ht; - $sql.= " , total_ttc = ".$total_ttc; - $sql.= " , total_tva = ".$total_tva; - $sql.= " WHERE rowid = ".$id; - $result = $this->db->query($sql); - if($result): - $this->db->free($result); - return 1; - else: - $this->error=$this->db->error(); - dol_syslog('ExpenseReport::recalculer: Error '.$this->error,LOG_ERR); - return -3; - endif; - } - else - { - $this->error=$this->db->error(); - dol_syslog('ExpenseReport::recalculer: Error '.$this->error,LOG_ERR); - return -3; - } - } - - /** - * fetch_lines - * - * @return int <0 if OK, >0 if KO - */ - function fetch_lines() - { - $this->lines=array(); - - $sql = ' SELECT de.rowid, de.comments, de.qty, de.value_unit, de.date,'; - $sql.= ' de.'.$this->fk_element.', de.fk_c_type_fees, de.fk_projet, de.tva_tx as vatrate,'; - $sql.= ' de.total_ht, de.total_tva, de.total_ttc,'; - $sql.= ' ctf.code as code_type_fees, ctf.label as libelle_type_fees,'; - $sql.= ' p.ref as ref_projet, p.title as title_projet'; - $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as de'; - $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON de.fk_c_type_fees = ctf.id'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as p ON de.fk_projet = p.rowid'; - $sql.= ' WHERE de.'.$this->fk_element.' = '.$this->id; - - dol_syslog('ExpenseReport::fetch_lines sql='.$sql, LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) - { - $num = $this->db->num_rows($resql); - $i = 0; - while ($i < $num) - { - $objp = $this->db->fetch_object($resql); - - $deplig = new ExpenseReportLine($this->db); - - $deplig->rowid = $objp->rowid; - $deplig->comments = $objp->comments; - $deplig->qty = $objp->qty; - $deplig->value_unit = $objp->value_unit; - $deplig->date = $objp->date; - - $deplig->fk_expensereport = $objp->fk_expensereport; - $deplig->fk_c_type_fees = $objp->fk_c_type_fees; - $deplig->fk_projet = $objp->fk_projet; - - $deplig->total_ht = $objp->total_ht; - $deplig->total_tva = $objp->total_tva; - $deplig->total_ttc = $objp->total_ttc; - - $deplig->type_fees_code = $objp->code_type_fees; - $deplig->type_fees_libelle = $objp->libelle_type_fees; - $deplig->vatrate = $objp->vatrate; - $deplig->projet_ref = $objp->ref_projet; - $deplig->projet_title = $objp->title_projet; - - $this->lignes[$i] = $deplig; - $this->lines[$i] = $deplig; - - $i++; - } - $this->db->free($resql); - return 1; - } - else - { - $this->error=$this->db->lasterror(); - dol_syslog('ExpenseReport::fetch_lines: Error '.$this->error, LOG_ERR); - return -3; - } - } - - - /** - * delete - * - * @param int $rowid Id to delete (optional) - * @param User $fuser User that delete - * @return int <0 if KO, >0 if OK - */ - function delete($rowid=0, User $fuser=null) - { - global $user,$langs,$conf; - - if (! $rowid) $rowid=$this->id; - - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line.' WHERE '.$this->fk_element.' = '.$rowid; - if ($this->db->query($sql)) - { - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid = '.$rowid; - $resql=$this->db->query($sql); - if ($resql) - { - $this->db->commit(); - return 1; - } - else - { - $this->error=$this->db->error()." sql=".$sql; - dol_syslog("ExpenseReport.class::delete ".$this->error, LOG_ERR); - $this->db->rollback(); - return -6; - } - } - else - { - $this->error=$this->db->error()." sql=".$sql; - dol_syslog("ExpenseReport.class::delete ".$this->error, LOG_ERR); - $this->db->rollback(); - return -4; - } - } - - /** - * Set to status validate - * - * @param User $fuser User - * @return int <0 if KO, >0 if OK - */ - function setValidate($fuser) - { - global $conf,$langs; - - $expld_car = (empty($conf->global->NDF_EXPLODE_CHAR))?"-":$conf->global->NDF_EXPLODE_CHAR; - - // Sélection du numéro de ref suivant - $ref_next = $this->getNextNumRef(); - $ref_number_int = ($this->ref+1)-1; - - // Sélection de la date de début de la NDF - $sql = 'SELECT date_debut'; - $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element; - $sql.= ' WHERE rowid = '.$this->id; - $result = $this->db->query($sql); - $objp = $this->db->fetch_object($result); - $this->date_debut = $this->db->jdate($objp->date_debut); - - // Création du ref_number suivant - if($ref_next) - { - $prefix="ER"; - if (! empty($conf->global->EXPENSE_REPORT_PREFIX)) $prefix=$conf->global->EXPENSE_REPORT_PREFIX; - $this->ref = strtoupper($fuser->login).$expld_car.$prefix.$this->ref.$expld_car.dol_print_date($this->date_debut,'%y%m%d'); - } - - if ($this->fk_statut != 2) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql.= " SET ref = '".$this->ref."', fk_statut = 2, fk_user_valid = ".$fuser->id.","; - $sql.= " ref_number_int = ".$ref_number_int; - $sql.= ' WHERE rowid = '.$this->id; - - dol_syslog(get_class($this)."::set_save sql=".$sql, LOG_DEBUG); - - if ($this->db->query($sql)) - { - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - else - { - dol_syslog(get_class($this)."::set_save expensereport already with save status", LOG_WARNING); - } - } - - /** - * set_save_from_refuse - * - * @param User $fuser User - * @return int <0 if KO, >0 if OK - */ - function set_save_from_refuse($fuser) - { - global $conf,$langs; - - // Sélection de la date de début de la NDF - $sql = 'SELECT date_debut'; - $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element; - $sql.= ' WHERE rowid = '.$this->id; - - $result = $this->db->query($sql); - - $objp = $this->db->fetch_object($result); - - $this->date_debut = $this->db->jdate($objp->date_debut); - - if ($this->fk_statut != 2) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql.= " SET fk_statut = 2"; - $sql.= ' WHERE rowid = '.$this->id; - - dol_syslog(get_class($this)."::set_save_from_refuse sql=".$sql, LOG_DEBUG); - - if ($this->db->query($sql)) - { - return 1; - } - else - { - $this->error=$this->db->lasterror(); - return -1; - } - } - else - { - dol_syslog(get_class($this)."::set_save_from_refuse expensereport already with save status", LOG_WARNING); - } - } - - /** - * Set status to approved - * - * @param User $fuser User - * @return int <0 if KO, >0 if OK - */ - function setApproved($fuser) - { - $now=dol_now(); - - // date approval - $this->date_approve = $this->db->idate($now); - if ($this->fk_statut != 5) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql.= " SET ref = '".$this->ref."', fk_statut = 5, fk_user_approve = ".$fuser->id.","; - $sql.= " date_approve='".$this->date_approve."'"; - $sql.= ' WHERE rowid = '.$this->id; - if ($this->db->query($sql)) - { - return 1; - } - else - { - $this->error=$this->db->lasterror(); - return -1; - } - } - else - { - dol_syslog(get_class($this)."::set_valide expensereport already with valide status", LOG_WARNING); - } - } - - /** - * setDeny - * - * @param User $fuser User - * @param Details $details Details - */ - function setDeny($fuser,$details) - { - $now = dol_now(); - - // date de refus - if ($this->fk_statut != 99) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql.= " SET ref = '".$this->ref."', fk_statut = 99, fk_user_refuse = ".$fuser->id.","; - $sql.= " date_refuse='".$this->db->idate($now)."',"; - $sql.= " detail_refuse='".$this->db->escape($details)."'"; - $sql.= " fk_user_approve=NULL,"; - $sql.= ' WHERE rowid = '.$this->id; - if ($this->db->query($sql)) - { - $this->fk_statut = 99; - $this->fk_user_refuse = $fuser->id; - $this->detail_refuse = $details; - $this->date_refuse = $now; - return 1; - } - else - { - $this->error=$this->db->lasterror(); - return -1; - } - } - else - { - dol_syslog(get_class($this)."::setDeny expensereport already with refuse status", LOG_WARNING); - } - } - - /** - * set_unpaid - * - * @param User $fuser User - * @return int <0 if KO, >0 if OK - */ - function set_unpaid($fuser) - { - if ($this->fk_c_deplacement_statuts != 5) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql.= " SET fk_statut = 5"; - $sql.= ' WHERE rowid = '.$this->id; - - dol_syslog(get_class($this)."::set_unpaid sql=".$sql, LOG_DEBUG); - - if ($this->db->query($sql)): - return 1; - else: - $this->error=$this->db->error(); - return -1; - endif; - } - else - { - dol_syslog(get_class($this)."::set_unpaid expensereport already with unpaid status", LOG_WARNING); - } - } - - /** - * set_cancel - * - * @param User $fuser User - * @param string $detail Detail - * @return int <0 if KO, >0 if OK - */ - function set_cancel($fuser,$detail) - { - $this->date_cancel = $this->db->idate(gmmktime()); - if ($this->fk_statut != 4) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql.= " SET fk_statut = 4, fk_user_cancel = ".$fuser->id; - $sql.= ", date_cancel='".$this->date_cancel."'"; - $sql.= " ,detail_cancel='".$this->db->escape($detail)."'"; - $sql.= ' WHERE rowid = '.$this->id; - - dol_syslog(get_class($this)."::set_cancel sql=".$sql, LOG_DEBUG); - - if ($this->db->query($sql)) - { - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - else - { - dol_syslog(get_class($this)."::set_cancel expensereport already with cancel status", LOG_WARNING); - } - } - - /** - * Return next reference of expense report not already used - * - * @return string free ref - */ - function getNextNumRef() - { - global $conf; - - $expld_car = (empty($conf->global->NDF_EXPLODE_CHAR))?"-":$conf->global->NDF_EXPLODE_CHAR; - $num_car = (empty($conf->global->NDF_NUM_CAR_REF))?"5":$conf->global->NDF_NUM_CAR_REF; - - $sql = 'SELECT de.ref_number_int'; - $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' de'; - $sql.= ' ORDER BY de.ref_number_int DESC'; - - $result = $this->db->query($sql); - - if($this->db->num_rows($result) > 0): - $objp = $this->db->fetch_object($result); - $this->ref = $objp->ref_number_int; - $this->ref++; - while(strlen($this->ref) < $num_car): - $this->ref = "0".$this->ref; - endwhile; - else: - $this->ref = 1; - while(strlen($this->ref) < $num_car): - $this->ref = "0".$this->ref; - endwhile; - endif; - - if ($result): - return 1; - else: - $this->error=$this->db->error(); - return -1; - endif; - } - - /** - * Return clicable name (with picto eventually) - * - * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto - * @return string String with URL - */ - function getNomUrl($withpicto=0) - { - global $langs; - - $result=''; - - $link = ''; - $linkend=''; - - $picto='trip'; - - $label=$langs->trans("Show").': '.$this->ref; - - if ($withpicto) $result.=($link.img_object($label,$picto).$linkend); - if ($withpicto && $withpicto != 2) $result.=' '; - if ($withpicto != 2) $result.=$link.$this->ref.$linkend; - return $result; - } - - /** - * Update total of an expense report when you add a line. - * - * @param string $ligne_total_ht Amount without taxes - * @param string $ligne_total_tva Amount of all taxes - * @return void - */ - function update_totaux_add($ligne_total_ht,$ligne_total_tva) - { - $this->total_ht = $this->total_ht + $ligne_total_ht; - $this->total_tva = $this->total_tva + $ligne_total_tva; - $this->total_ttc = $this->total_ht + $this->total_tva; - - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql.= " total_ht = ".$this->total_ht; - $sql.= " , total_ttc = ".$this->total_ttc; - $sql.= " , total_tva = ".$this->total_tva; - $sql.= " WHERE rowid = ".$this->id; - - $result = $this->db->query($sql); - if ($result): - return 1; - else: - $this->error=$this->db->error(); - return -1; - endif; - } - - /** - * Update total of an expense report when you delete a line. - * - * @param string $ligne_total_ht Amount without taxes - * @param string $ligne_total_tva Amount of all taxes - * @return void - */ - function update_totaux_del($ligne_total_ht,$ligne_total_tva) - { - $this->total_ht = $this->total_ht - $ligne_total_ht; - $this->total_tva = $this->total_tva - $ligne_total_tva; - $this->total_ttc = $this->total_ht + $this->total_tva; - - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql.= " total_ht = ".$this->total_ht; - $sql.= " , total_ttc = ".$this->total_ttc; - $sql.= " , total_tva = ".$this->total_tva; - $sql.= " WHERE rowid = ".$this->id; - - $result = $this->db->query($sql); - if ($result): - return 1; - else: - $this->error=$this->db->error(); - return -1; - endif; - } - - - /** - * updateline - * - * @param int $rowid Line to edit - * @param int $type_fees_id Type payment - * @param int $projet_id Project id - * @param double $vatrate Vat rate - * @param string $comments Description - * @param real $qty Qty - * @param double $value_unit Value init - * @param int $date Date - * @param int $expensereport_id Expense report id - * @return int <0 if KO, >0 if OK - */ - function updateline($rowid, $type_fees_id, $projet_id, $vatrate, $comments, $qty, $value_unit, $date, $expensereport_id) - { - global $user; - - if ($this->fk_statut==0 || $this->fk_statut==99) - { - $this->db->begin(); - - // calcul de tous les totaux de la ligne - $total_ttc = price2num($qty*$value_unit, 'MT'); - - $tx_tva = $vatrate / 100; - $tx_tva = $tx_tva + 1; - $total_ht = price2num($total_ttc/$tx_tva, 'MT'); - - $total_tva = price2num($total_ttc - $total_ht, 'MT'); - // fin calculs - - $ligne = new ExpenseReportLine($this->db); - $ligne->comments = $comments; - $ligne->qty = $qty; - $ligne->value_unit = $value_unit; - $ligne->date = $date; - - $ligne->fk_expensereport= $expensereport_id; - $ligne->fk_c_type_fees = $type_fees_id; - $ligne->fk_projet = $projet_id; - - $ligne->total_ht = $total_ht; - $ligne->total_tva = $total_tva; - $ligne->total_ttc = $total_ttc; - $ligne->vatrate = price2num($vatrate); - $ligne->rowid = $rowid; - - // Select des infos sur le type fees - $sql = "SELECT c.code as code_type_fees, c.label as libelle_type_fees"; - $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees as c"; - $sql.= " WHERE c.id = ".$type_fees_id; - $result = $this->db->query($sql); - $objp_fees = $this->db->fetch_object($result); - $ligne->type_fees_code = $objp_fees->code_type_fees; - $ligne->type_fees_libelle = $objp_fees->libelle_type_fees; - - // Select des informations du projet - $sql = "SELECT p.ref as ref_projet, p.title as title_projet"; - $sql.= " FROM ".MAIN_DB_PREFIX."projet as p"; - $sql.= " WHERE p.rowid = ".$projet_id; - $result = $this->db->query($sql); - $objp_projet = $this->db->fetch_object($result); - $ligne->projet_ref = $objp_projet->ref_projet; - $ligne->projet_title = $objp_projet->title_projet; - - $result = $ligne->update($user); - if ($result > 0) - { - $this->db->commit(); - return 1; - } - else - { - $this->error=$ligne->error; - $this->errors=$ligne->errors; - $this->db->rollback(); - return -2; - } - } - } - - /** - * deleteline - * - * @param int $rowid Row id - * @param User $fuser User - * @return int <0 if KO, >0 if OK - */ - function deleteline($rowid, $fuser='') - { - $this->db->begin(); - - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line; - $sql.= ' WHERE rowid = '.$rowid; - - dol_syslog(get_class($this)."::deleteline sql=".$sql); - $result = $this->db->query($sql); - if (!$result) - { - $this->error=$this->db->error(); - dol_syslog(get_class($this)."::deleteline Error ".$this->error, LOG_ERR); - $this->db->rollback(); - return -1; - } - - $this->db->commit(); - - return 1; - } - - /** - * periode_existe - * - * @param User $fuser User - * @param Date $date_debut Start date - * @param Date $date_fin End date - * @return int <0 if KO, >0 if OK - */ - function periode_existe($fuser, $date_debut, $date_fin) - { - $sql = "SELECT rowid, date_debut, date_fin"; - $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql.= " WHERE fk_user_author = '{$fuser->id}'"; - - dol_syslog(get_class($this)."::periode_existe sql=".$sql); - $result = $this->db->query($sql); - if($result) - { - $num_lignes = $this->db->num_rows($result); $i = 0; - - if ($num_lignes>0) - { - $date_d_form = $date_debut; - $date_f_form = $date_fin; - - $existe = false; - - while ($i < $num_lignes) - { - $objp = $this->db->fetch_object($result); - - $date_d_req = $this->db->jdate($objp->date_debut); // 3 - $date_f_req = $this->db->jdate($objp->date_fin); // 4 - - if (!($date_f_form < $date_d_req || $date_d_form > $date_f_req)) $existe = true; - - $i++; - } - - if($existe) return 1; - else return 0; - } - else - { - return 0; - } - } - else - { - $this->error=$this->db->lasterror(); - dol_syslog(get_class($this)."::periode_existe Error ".$this->error, LOG_ERR); - return -1; - } - } - - - /** - * Return list of people with permission to validate trips and expenses - * - * @return array Array of user ids - */ - function fetch_users_approver_expensereport() - { - $users_validator=array(); - - $sql = "SELECT fk_user"; - $sql.= " FROM ".MAIN_DB_PREFIX."user_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd"; - $sql.= " WHERE ur.fk_id = rd.id and module = 'expensereport' AND perms = 'approve'"; // Permission 'Approve'; - - dol_syslog(get_class($this)."::fetch_users_approver_expensereport sql=".$sql); - $result = $this->db->query($sql); - if($result) - { - $num_lignes = $this->db->num_rows($result); $i = 0; - while ($i < $num_lignes) - { - $objp = $this->db->fetch_object($result); - array_push($users_validator,$objp->fk_user); - $i++; - } - return $users_validator; - } - else - { - $this->error=$this->db->lasterror(); - dol_syslog(get_class($this)."::fetch_users_approver_expensereport Error ".$this->error, LOG_ERR); - return -1; - } - } - - /** - * Create a document onto disk accordign to template module. - * - * @param string $modele Force le mnodele a utiliser ('' to not force) - * @param Translate $outputlangs objet lang a utiliser pour traduction - * @param int $hidedetails Hide details of lines - * @param int $hidedesc Hide description - * @param int $hideref Hide ref - * @return int 0 if KO, 1 if OK - */ - public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0) - { - global $conf,$langs; - - $langs->load("trips"); - - // Positionne le modele sur le nom du modele a utiliser - if (! dol_strlen($modele)) - { - if (! empty($conf->global->EXPENSEREPORT_ADDON_PDF)) - { - $modele = $conf->global->EXPENSEREPORT_ADDON_PDF; - } - else - { - $modele = 'standard'; - } - } - - $modelpath = "core/modules/expensereport/doc/"; - - return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref); - } - - /** - * List of types - * - * @param int $active Active or not - * @return array - */ - function listOfTypes($active=1) - { - global $langs; - $ret=array(); - $sql = "SELECT id, code, label"; - $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees"; - $sql.= " WHERE active = ".$active; - dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG); - $result = $this->db->query($sql); - if ( $result ) - { - $num = $this->db->num_rows($result); - $i=0; - while ($i < $num) - { - $obj = $this->db->fetch_object($result); - $ret[$obj->code]=(($langs->trans($obj->code)!=$obj->code)?$langs->trans($obj->code):$obj->label); - $i++; - } - } - else - { - dol_print_error($this->db); - } - return $ret; - } + /** + * Returns the label status + * + * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto + * @return string Label + */ + function getLibStatut($mode=0) + { + return $this->LibStatut($this->status,$mode); + } + + /** + * Returns the label of a statut + * + * @param int $status id statut + * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto + * @return string Label + */ + function LibStatut($status,$mode=0) + { + global $langs; + + if ($mode == 0) + return $langs->trans($this->statuts[$status]); + + if ($mode == 1) + return $langs->trans($this->statuts_short[$status]); + + if ($mode == 2) + return img_picto($langs->trans($this->statuts_short[$status]), $this->statuts_logo[$status]).' '.$langs->trans($this->statuts_short[$status]); + + if ($mode == 3) + return img_picto($langs->trans($this->statuts_short[$status]), $this->statuts_logo[$status]); + + if ($mode == 4) + return img_picto($langs->trans($this->statuts_short[$status]),$this->statuts_logo[$status]).' '.$langs->trans($this->statuts[$status]); + + if ($mode == 5) + return ''.$langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),$this->statuts_logo[$status]); + + } + + + /** + * Load information on object + * + * @param int $id Id of object + * @return void + */ + function info($id) + { + global $conf; + + $sql = "SELECT f.rowid,"; + $sql.= " f.date_create as datec,"; + $sql.= " f.tms as date_modification,"; + $sql.= " f.date_valid as datev,"; + $sql.= " f.date_approve as datea,"; + $sql.= " f.fk_user_author as fk_user_creation,"; + $sql.= " f.fk_user_modif as fk_user_modification,"; + $sql.= " f.fk_user_valid,"; + $sql.= " f.fk_user_approve"; + $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as f"; + $sql.= " WHERE f.rowid = ".$id; + $sql.= " AND f.entity = ".$conf->entity; + + $resql = $this->db->query($sql); + if ($resql) + { + if ($this->db->num_rows($resql)) + { + $obj = $this->db->fetch_object($resql); + + $this->id = $obj->rowid; + + $this->date_creation = $this->db->jdate($obj->datec); + $this->date_modification = $this->db->jdate($obj->date_modification); + $this->date_validation = $this->db->jdate($obj->datev); + $this->date_approbation = $this->db->jdate($obj->datea); + + $cuser = new User($this->db); + $cuser->fetch($obj->fk_user_author); + $this->user_creation = $cuser; + + if ($obj->fk_user_creation) + { + $cuser = new User($this->db); + $cuser->fetch($obj->fk_user_creation); + $this->user_creation = $cuser; + } + if ($obj->fk_user_valid) + { + $vuser = new User($this->db); + $vuser->fetch($obj->fk_user_valid); + $this->user_validation = $vuser; + } + if ($obj->fk_user_modification) + { + $muser = new User($this->db); + $muser->fetch($obj->fk_user_modification); + $this->user_modification = $muser; + } + + } + $this->db->free($resql); + } + else + { + dol_print_error($this->db); + } + } + + + + /** + * Initialise an instance with random values. + * Used to build previews or test instances. + * id must be 0 if object instance is a specimen. + * + * @return void + */ + function initAsSpecimen() + { + global $user,$langs,$conf; + + $now=dol_now(); + + // Initialise parametres + $this->id=0; + $this->ref = 'SPECIMEN'; + $this->specimen=1; + $this->date_create = $now; + $this->date_debut = $now; + $this->date_fin = $now; + $this->date_approve = $now; + + $this->status = 5; + $this->fk_statut = 5; + + $this->fk_user_author = $user->id; + $this->fk_user_valid = $user->id; + $this->fk_user_approve = $user->id; + $this->fk_user_validator = $user->id; + + $this->note_private='Private note'; + $this->note_public='SPECIMEN'; + $nbp = 5; + $xnbp = 0; + while ($xnbp < $nbp) + { + $line=new ExpenseReportLine($this->db); + $line->comments=$langs->trans("Comment")." ".$xnbp; + $line->date=($now-3600*(1+$xnbp)); + $line->total_ht=100; + $line->total_tva=20; + $line->total_ttc=120; + $line->qty=1; + $line->vatrate=20; + $line->value_unit=120; + $line->fk_expensereport=0; + $line->type_fees_code='TRA'; + + $line->projet_ref = 'ABC'; + + $this->lines[$xnbp]=$line; + $xnbp++; + + $this->total_ht+=$line->total_ht; + $this->total_tva+=$line->total_tva; + $this->total_ttc+=$line->total_ttc; + } + } + + /** + * fetch_line_by_project + * + * @param int $projectid Project id + * @param User $user User + * @return int <0 if KO, >0 if OK + */ + function fetch_line_by_project($projectid,$user='') + { + global $conf,$db,$langs; + + $langs->load('trips'); + + if($user->rights->expensereport->lire) { + + $sql = "SELECT de.fk_expensereport, de.date, de.comments, de.total_ht, de.total_ttc"; + $sql.= " FROM ".MAIN_DB_PREFIX."expensereport_det as de"; + $sql.= " WHERE de.fk_projet = ".$projectid; + + dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG); + $result = $db->query($sql) ; + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + $total_HT = 0; + $total_TTC = 0; + + while ($i < $num) + { + + $objp = $db->fetch_object($result); + + $sql2 = "SELECT d.rowid, d.fk_user_author, d.ref, d.fk_statut"; + $sql2.= " FROM ".MAIN_DB_PREFIX."expensereport as d"; + $sql2.= " WHERE d.rowid = '".$objp->fk_expensereport."'"; + + $result2 = $db->query($sql2); + $obj = $db->fetch_object($result2); + + $objp->fk_user_author = $obj->fk_user_author; + $objp->ref = $obj->ref; + $objp->fk_c_expensereport_status = $obj->fk_statut; + $objp->rowid = $obj->rowid; + + $total_HT = $total_HT + $objp->total_ht; + $total_TTC = $total_TTC + $objp->total_ttc; + $author = new User($db); + $author->fetch($objp->fk_user_author); + + print ''; + print ''.$objp->ref_num.''; + print ''.dol_print_date($objp->date,'day').''; + print ''.$author->getNomUrl().''; + print ''.$objp->comments.''; + print ''.price($objp->total_ht).''; + print ''.price($objp->total_ttc).''; + print ''; + + switch($objp->fk_c_expensereport_status) { + case 4: + print img_picto($langs->trans('StatusOrderCanceled'),'statut5'); + break; + case 1: + print $langs->trans('Draft').' '.img_picto($langs->trans('Draft'),'statut0'); + break; + case 2: + print $langs->trans('TripForValid').' '.img_picto($langs->trans('TripForValid'),'statut3'); + break; + case 5: + print $langs->trans('TripForPaid').' '.img_picto($langs->trans('TripForPaid'),'statut3'); + break; + case 6: + print $langs->trans('TripPaid').' '.img_picto($langs->trans('TripPaid'),'statut4'); + break; + } + /* + if ($status==4) return img_picto($langs->trans('StatusOrderCanceled'),'statut5'); + if ($status==1) return img_picto($langs->trans('StatusOrderDraft'),'statut0'); + if ($status==2) return img_picto($langs->trans('StatusOrderValidated'),'statut1'); + if ($status==2) return img_picto($langs->trans('StatusOrderOnProcess'),'statut3'); + if ($status==5) return img_picto($langs->trans('StatusOrderToBill'),'statut4'); + if ($status==6) return img_picto($langs->trans('StatusOrderOnProcess'),'statut6'); + */ + print ''; + print ''; + + $i++; + } + + print ''.$langs->trans("Number").': '.$i.''; + print ''.$langs->trans("TotalHT").' : '.price($total_HT).''; + print ''.$langs->trans("TotalTTC").' : '.price($total_TTC).''; + print ' '; + print ''; + + } + else + { + $this->error=$db->error(); + return -1; + } + } + + } + + /** + * recalculer + * TODO Replace this with call to update_price if not already done + * + * @param int $id Id of expense report + * @return int <0 if KO, >0 if OK + */ + function recalculer($id) + { + $sql = 'SELECT tt.total_ht, tt.total_ttc, tt.total_tva'; + $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as tt'; + $sql.= ' WHERE tt.'.$this->fk_element.' = '.$id; + + $total_ht = 0; $total_tva = 0; $total_ttc = 0; + + dol_syslog('ExpenseReport::recalculer sql='.$sql,LOG_DEBUG); + + $result = $this->db->query($sql); + if($result) + { + $num = $this->db->num_rows($result); + $i = 0; + while ($i < $num): + $objp = $this->db->fetch_object($result); + $total_ht+=$objp->total_ht; + $total_tva+=$objp->total_tva; + $i++; + endwhile; + + $total_ttc = $total_ht + $total_tva; + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql.= " total_ht = ".$total_ht; + $sql.= " , total_ttc = ".$total_ttc; + $sql.= " , total_tva = ".$total_tva; + $sql.= " WHERE rowid = ".$id; + $result = $this->db->query($sql); + if($result): + $this->db->free($result); + return 1; + else: + $this->error=$this->db->error(); + dol_syslog('ExpenseReport::recalculer: Error '.$this->error,LOG_ERR); + return -3; + endif; + } + else + { + $this->error=$this->db->error(); + dol_syslog('ExpenseReport::recalculer: Error '.$this->error,LOG_ERR); + return -3; + } + } + + /** + * fetch_lines + * + * @return int <0 if OK, >0 if KO + */ + function fetch_lines() + { + $this->lines=array(); + + $sql = ' SELECT de.rowid, de.comments, de.qty, de.value_unit, de.date,'; + $sql.= ' de.'.$this->fk_element.', de.fk_c_type_fees, de.fk_projet, de.tva_tx as vatrate,'; + $sql.= ' de.total_ht, de.total_tva, de.total_ttc,'; + $sql.= ' ctf.code as code_type_fees, ctf.label as libelle_type_fees,'; + $sql.= ' p.ref as ref_projet, p.title as title_projet'; + $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as de'; + $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON de.fk_c_type_fees = ctf.id'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as p ON de.fk_projet = p.rowid'; + $sql.= ' WHERE de.'.$this->fk_element.' = '.$this->id; + + dol_syslog('ExpenseReport::fetch_lines sql='.$sql, LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + $i = 0; + while ($i < $num) + { + $objp = $this->db->fetch_object($resql); + + $deplig = new ExpenseReportLine($this->db); + + $deplig->rowid = $objp->rowid; + $deplig->comments = $objp->comments; + $deplig->qty = $objp->qty; + $deplig->value_unit = $objp->value_unit; + $deplig->date = $objp->date; + + $deplig->fk_expensereport = $objp->fk_expensereport; + $deplig->fk_c_type_fees = $objp->fk_c_type_fees; + $deplig->fk_projet = $objp->fk_projet; + + $deplig->total_ht = $objp->total_ht; + $deplig->total_tva = $objp->total_tva; + $deplig->total_ttc = $objp->total_ttc; + + $deplig->type_fees_code = $objp->code_type_fees; + $deplig->type_fees_libelle = $objp->libelle_type_fees; + $deplig->vatrate = $objp->vatrate; + $deplig->projet_ref = $objp->ref_projet; + $deplig->projet_title = $objp->title_projet; + + $this->lignes[$i] = $deplig; + $this->lines[$i] = $deplig; + + $i++; + } + $this->db->free($resql); + return 1; + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog('ExpenseReport::fetch_lines: Error '.$this->error, LOG_ERR); + return -3; + } + } + + + /** + * delete + * + * @param int $rowid Id to delete (optional) + * @param User $fuser User that delete + * @return int <0 if KO, >0 if OK + */ + function delete($rowid=0, User $fuser=null) + { + global $user,$langs,$conf; + + if (! $rowid) $rowid=$this->id; + + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line.' WHERE '.$this->fk_element.' = '.$rowid; + if ($this->db->query($sql)) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid = '.$rowid; + $resql=$this->db->query($sql); + if ($resql) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error()." sql=".$sql; + dol_syslog("ExpenseReport.class::delete ".$this->error, LOG_ERR); + $this->db->rollback(); + return -6; + } + } + else + { + $this->error=$this->db->error()." sql=".$sql; + dol_syslog("ExpenseReport.class::delete ".$this->error, LOG_ERR); + $this->db->rollback(); + return -4; + } + } + + /** + * Set to status validate + * + * @param User $fuser User + * @return int <0 if KO, >0 if OK + */ + function setValidate($fuser) + { + global $conf,$langs; + + $expld_car = (empty($conf->global->NDF_EXPLODE_CHAR))?"-":$conf->global->NDF_EXPLODE_CHAR; + + // Sélection du numéro de ref suivant + $ref_next = $this->getNextNumRef(); + $ref_number_int = ($this->ref+1)-1; + + // Sélection de la date de début de la NDF + $sql = 'SELECT date_debut'; + $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element; + $sql.= ' WHERE rowid = '.$this->id; + $result = $this->db->query($sql); + $objp = $this->db->fetch_object($result); + $this->date_debut = $this->db->jdate($objp->date_debut); + + // Création du ref_number suivant + if($ref_next) + { + $prefix="ER"; + if (! empty($conf->global->EXPENSE_REPORT_PREFIX)) $prefix=$conf->global->EXPENSE_REPORT_PREFIX; + $this->ref = strtoupper($fuser->login).$expld_car.$prefix.$this->ref.$expld_car.dol_print_date($this->date_debut,'%y%m%d'); + } + + if ($this->fk_statut != 2) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql.= " SET ref = '".$this->ref."', fk_statut = 2, fk_user_valid = ".$fuser->id.","; + $sql.= " ref_number_int = ".$ref_number_int; + $sql.= ' WHERE rowid = '.$this->id; + + dol_syslog(get_class($this)."::set_save sql=".$sql, LOG_DEBUG); + + if ($this->db->query($sql)) + { + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + else + { + dol_syslog(get_class($this)."::set_save expensereport already with save status", LOG_WARNING); + } + } + + /** + * set_save_from_refuse + * + * @param User $fuser User + * @return int <0 if KO, >0 if OK + */ + function set_save_from_refuse($fuser) + { + global $conf,$langs; + + // Sélection de la date de début de la NDF + $sql = 'SELECT date_debut'; + $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element; + $sql.= ' WHERE rowid = '.$this->id; + + $result = $this->db->query($sql); + + $objp = $this->db->fetch_object($result); + + $this->date_debut = $this->db->jdate($objp->date_debut); + + if ($this->fk_statut != 2) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql.= " SET fk_statut = 2"; + $sql.= ' WHERE rowid = '.$this->id; + + dol_syslog(get_class($this)."::set_save_from_refuse sql=".$sql, LOG_DEBUG); + + if ($this->db->query($sql)) + { + return 1; + } + else + { + $this->error=$this->db->lasterror(); + return -1; + } + } + else + { + dol_syslog(get_class($this)."::set_save_from_refuse expensereport already with save status", LOG_WARNING); + } + } + + /** + * Set status to approved + * + * @param User $fuser User + * @return int <0 if KO, >0 if OK + */ + function setApproved($fuser) + { + $now=dol_now(); + + // date approval + $this->date_approve = $this->db->idate($now); + if ($this->fk_statut != 5) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql.= " SET ref = '".$this->ref."', fk_statut = 5, fk_user_approve = ".$fuser->id.","; + $sql.= " date_approve='".$this->date_approve."'"; + $sql.= ' WHERE rowid = '.$this->id; + if ($this->db->query($sql)) + { + return 1; + } + else + { + $this->error=$this->db->lasterror(); + return -1; + } + } + else + { + dol_syslog(get_class($this)."::set_valide expensereport already with valide status", LOG_WARNING); + } + } + + /** + * setDeny + * + * @param User $fuser User + * @param Details $details Details + */ + function setDeny($fuser,$details) + { + $now = dol_now(); + + // date de refus + if ($this->fk_statut != 99) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql.= " SET ref = '".$this->ref."', fk_statut = 99, fk_user_refuse = ".$fuser->id.","; + $sql.= " date_refuse='".$this->db->idate($now)."',"; + $sql.= " detail_refuse='".$this->db->escape($details)."'"; + $sql.= " fk_user_approve=NULL,"; + $sql.= ' WHERE rowid = '.$this->id; + if ($this->db->query($sql)) + { + $this->fk_statut = 99; + $this->fk_user_refuse = $fuser->id; + $this->detail_refuse = $details; + $this->date_refuse = $now; + return 1; + } + else + { + $this->error=$this->db->lasterror(); + return -1; + } + } + else + { + dol_syslog(get_class($this)."::setDeny expensereport already with refuse status", LOG_WARNING); + } + } + + /** + * set_unpaid + * + * @param User $fuser User + * @return int <0 if KO, >0 if OK + */ + function set_unpaid($fuser) + { + if ($this->fk_c_deplacement_statuts != 5) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql.= " SET fk_statut = 5"; + $sql.= ' WHERE rowid = '.$this->id; + + dol_syslog(get_class($this)."::set_unpaid sql=".$sql, LOG_DEBUG); + + if ($this->db->query($sql)): + return 1; + else: + $this->error=$this->db->error(); + return -1; + endif; + } + else + { + dol_syslog(get_class($this)."::set_unpaid expensereport already with unpaid status", LOG_WARNING); + } + } + + /** + * set_cancel + * + * @param User $fuser User + * @param string $detail Detail + * @return int <0 if KO, >0 if OK + */ + function set_cancel($fuser,$detail) + { + $this->date_cancel = $this->db->idate(gmmktime()); + if ($this->fk_statut != 4) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; + $sql.= " SET fk_statut = 4, fk_user_cancel = ".$fuser->id; + $sql.= ", date_cancel='".$this->date_cancel."'"; + $sql.= " ,detail_cancel='".$this->db->escape($detail)."'"; + $sql.= ' WHERE rowid = '.$this->id; + + dol_syslog(get_class($this)."::set_cancel sql=".$sql, LOG_DEBUG); + + if ($this->db->query($sql)) + { + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + else + { + dol_syslog(get_class($this)."::set_cancel expensereport already with cancel status", LOG_WARNING); + } + } + + /** + * Return next reference of expense report not already used + * + * @return string free ref + */ + function getNextNumRef() + { + global $conf; + + $expld_car = (empty($conf->global->NDF_EXPLODE_CHAR))?"-":$conf->global->NDF_EXPLODE_CHAR; + $num_car = (empty($conf->global->NDF_NUM_CAR_REF))?"5":$conf->global->NDF_NUM_CAR_REF; + + $sql = 'SELECT de.ref_number_int'; + $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' de'; + $sql.= ' ORDER BY de.ref_number_int DESC'; + + $result = $this->db->query($sql); + + if($this->db->num_rows($result) > 0): + $objp = $this->db->fetch_object($result); + $this->ref = $objp->ref_number_int; + $this->ref++; + while(strlen($this->ref) < $num_car): + $this->ref = "0".$this->ref; + endwhile; + else: + $this->ref = 1; + while(strlen($this->ref) < $num_car): + $this->ref = "0".$this->ref; + endwhile; + endif; + + if ($result): + return 1; + else: + $this->error=$this->db->error(); + return -1; + endif; + } + + /** + * Return clicable name (with picto eventually) + * + * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto + * @return string String with URL + */ + function getNomUrl($withpicto=0) + { + global $langs; + + $result=''; + + $link = ''; + $linkend=''; + + $picto='trip'; + + $label=$langs->trans("Show").': '.$this->ref; + + if ($withpicto) $result.=($link.img_object($label,$picto).$linkend); + if ($withpicto && $withpicto != 2) $result.=' '; + if ($withpicto != 2) $result.=$link.$this->ref.$linkend; + return $result; + } + + /** + * Update total of an expense report when you add a line. + * + * @param string $ligne_total_ht Amount without taxes + * @param string $ligne_total_tva Amount of all taxes + * @return void + */ + function update_totaux_add($ligne_total_ht,$ligne_total_tva) + { + $this->total_ht = $this->total_ht + $ligne_total_ht; + $this->total_tva = $this->total_tva + $ligne_total_tva; + $this->total_ttc = $this->total_ht + $this->total_tva; + + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql.= " total_ht = ".$this->total_ht; + $sql.= " , total_ttc = ".$this->total_ttc; + $sql.= " , total_tva = ".$this->total_tva; + $sql.= " WHERE rowid = ".$this->id; + + $result = $this->db->query($sql); + if ($result): + return 1; + else: + $this->error=$this->db->error(); + return -1; + endif; + } + + /** + * Update total of an expense report when you delete a line. + * + * @param string $ligne_total_ht Amount without taxes + * @param string $ligne_total_tva Amount of all taxes + * @return void + */ + function update_totaux_del($ligne_total_ht,$ligne_total_tva) + { + $this->total_ht = $this->total_ht - $ligne_total_ht; + $this->total_tva = $this->total_tva - $ligne_total_tva; + $this->total_ttc = $this->total_ht + $this->total_tva; + + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql.= " total_ht = ".$this->total_ht; + $sql.= " , total_ttc = ".$this->total_ttc; + $sql.= " , total_tva = ".$this->total_tva; + $sql.= " WHERE rowid = ".$this->id; + + $result = $this->db->query($sql); + if ($result): + return 1; + else: + $this->error=$this->db->error(); + return -1; + endif; + } + + + /** + * updateline + * + * @param int $rowid Line to edit + * @param int $type_fees_id Type payment + * @param int $projet_id Project id + * @param double $vatrate Vat rate + * @param string $comments Description + * @param real $qty Qty + * @param double $value_unit Value init + * @param int $date Date + * @param int $expensereport_id Expense report id + * @return int <0 if KO, >0 if OK + */ + function updateline($rowid, $type_fees_id, $projet_id, $vatrate, $comments, $qty, $value_unit, $date, $expensereport_id) + { + global $user; + + if ($this->fk_statut==0 || $this->fk_statut==99) + { + $this->db->begin(); + + // calcul de tous les totaux de la ligne + $total_ttc = price2num($qty*$value_unit, 'MT'); + + $tx_tva = $vatrate / 100; + $tx_tva = $tx_tva + 1; + $total_ht = price2num($total_ttc/$tx_tva, 'MT'); + + $total_tva = price2num($total_ttc - $total_ht, 'MT'); + // fin calculs + + $ligne = new ExpenseReportLine($this->db); + $ligne->comments = $comments; + $ligne->qty = $qty; + $ligne->value_unit = $value_unit; + $ligne->date = $date; + + $ligne->fk_expensereport= $expensereport_id; + $ligne->fk_c_type_fees = $type_fees_id; + $ligne->fk_projet = $projet_id; + + $ligne->total_ht = $total_ht; + $ligne->total_tva = $total_tva; + $ligne->total_ttc = $total_ttc; + $ligne->vatrate = price2num($vatrate); + $ligne->rowid = $rowid; + + // Select des infos sur le type fees + $sql = "SELECT c.code as code_type_fees, c.label as libelle_type_fees"; + $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees as c"; + $sql.= " WHERE c.id = ".$type_fees_id; + $result = $this->db->query($sql); + $objp_fees = $this->db->fetch_object($result); + $ligne->type_fees_code = $objp_fees->code_type_fees; + $ligne->type_fees_libelle = $objp_fees->libelle_type_fees; + + // Select des informations du projet + $sql = "SELECT p.ref as ref_projet, p.title as title_projet"; + $sql.= " FROM ".MAIN_DB_PREFIX."projet as p"; + $sql.= " WHERE p.rowid = ".$projet_id; + $result = $this->db->query($sql); + $objp_projet = $this->db->fetch_object($result); + $ligne->projet_ref = $objp_projet->ref_projet; + $ligne->projet_title = $objp_projet->title_projet; + + $result = $ligne->update($user); + if ($result > 0) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$ligne->error; + $this->errors=$ligne->errors; + $this->db->rollback(); + return -2; + } + } + } + + /** + * deleteline + * + * @param int $rowid Row id + * @param User $fuser User + * @return int <0 if KO, >0 if OK + */ + function deleteline($rowid, $fuser='') + { + $this->db->begin(); + + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line; + $sql.= ' WHERE rowid = '.$rowid; + + dol_syslog(get_class($this)."::deleteline sql=".$sql); + $result = $this->db->query($sql); + if (!$result) + { + $this->error=$this->db->error(); + dol_syslog(get_class($this)."::deleteline Error ".$this->error, LOG_ERR); + $this->db->rollback(); + return -1; + } + + $this->db->commit(); + + return 1; + } + + /** + * periode_existe + * + * @param User $fuser User + * @param Date $date_debut Start date + * @param Date $date_fin End date + * @return int <0 if KO, >0 if OK + */ + function periode_existe($fuser, $date_debut, $date_fin) + { + $sql = "SELECT rowid, date_debut, date_fin"; + $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql.= " WHERE fk_user_author = '{$fuser->id}'"; + + dol_syslog(get_class($this)."::periode_existe sql=".$sql); + $result = $this->db->query($sql); + if($result) + { + $num_lignes = $this->db->num_rows($result); $i = 0; + + if ($num_lignes>0) + { + $date_d_form = $date_debut; + $date_f_form = $date_fin; + + $existe = false; + + while ($i < $num_lignes) + { + $objp = $this->db->fetch_object($result); + + $date_d_req = $this->db->jdate($objp->date_debut); // 3 + $date_f_req = $this->db->jdate($objp->date_fin); // 4 + + if (!($date_f_form < $date_d_req || $date_d_form > $date_f_req)) $existe = true; + + $i++; + } + + if($existe) return 1; + else return 0; + } + else + { + return 0; + } + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog(get_class($this)."::periode_existe Error ".$this->error, LOG_ERR); + return -1; + } + } + + + /** + * Return list of people with permission to validate trips and expenses + * + * @return array Array of user ids + */ + function fetch_users_approver_expensereport() + { + $users_validator=array(); + + $sql = "SELECT fk_user"; + $sql.= " FROM ".MAIN_DB_PREFIX."user_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd"; + $sql.= " WHERE ur.fk_id = rd.id and module = 'expensereport' AND perms = 'approve'"; // Permission 'Approve'; + + dol_syslog(get_class($this)."::fetch_users_approver_expensereport sql=".$sql); + $result = $this->db->query($sql); + if($result) + { + $num_lignes = $this->db->num_rows($result); $i = 0; + while ($i < $num_lignes) + { + $objp = $this->db->fetch_object($result); + array_push($users_validator,$objp->fk_user); + $i++; + } + return $users_validator; + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog(get_class($this)."::fetch_users_approver_expensereport Error ".$this->error, LOG_ERR); + return -1; + } + } + + /** + * Create a document onto disk accordign to template module. + * + * @param string $modele Force le mnodele a utiliser ('' to not force) + * @param Translate $outputlangs objet lang a utiliser pour traduction + * @param int $hidedetails Hide details of lines + * @param int $hidedesc Hide description + * @param int $hideref Hide ref + * @return int 0 if KO, 1 if OK + */ + public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0) + { + global $conf,$langs; + + $langs->load("trips"); + + // Positionne le modele sur le nom du modele a utiliser + if (! dol_strlen($modele)) + { + if (! empty($conf->global->EXPENSEREPORT_ADDON_PDF)) + { + $modele = $conf->global->EXPENSEREPORT_ADDON_PDF; + } + else + { + $modele = 'standard'; + } + } + + $modelpath = "core/modules/expensereport/doc/"; + + return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref); + } + + /** + * List of types + * + * @param int $active Active or not + * @return array + */ + function listOfTypes($active=1) + { + global $langs; + $ret=array(); + $sql = "SELECT id, code, label"; + $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees"; + $sql.= " WHERE active = ".$active; + dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG); + $result = $this->db->query($sql); + if ( $result ) + { + $num = $this->db->num_rows($result); + $i=0; + while ($i < $num) + { + $obj = $this->db->fetch_object($result); + $ret[$obj->code]=(($langs->trans($obj->code)!=$obj->code)?$langs->trans($obj->code):$obj->label); + $i++; + } + } + else + { + dol_print_error($this->db); + } + return $ret; + } } @@ -1472,235 +1472,235 @@ class ExpenseReport extends CommonObject */ class ExpenseReportLine { - var $db; - var $error; + var $db; + var $error; - var $rowid; - var $comments; - var $qty; - var $value_unit; - var $date; + var $rowid; + var $comments; + var $qty; + var $value_unit; + var $date; - var $fk_c_type_fees; - var $fk_projet; - var $fk_expensereport; + var $fk_c_type_fees; + var $fk_projet; + var $fk_expensereport; - var $type_fees_code; - var $type_fees_libelle; + var $type_fees_code; + var $type_fees_libelle; - var $projet_ref; - var $projet_title; + var $projet_ref; + var $projet_title; - var $vatrate; - var $total_ht; - var $total_tva; - var $total_ttc; + var $vatrate; + var $total_ht; + var $total_tva; + var $total_ttc; - /** - * Constructor - * - * @param DoliDB $db Handlet database - */ - function ExpenseReportLine($db) - { - $this->db= $db; - } + /** + * Constructor + * + * @param DoliDB $db Handlet database + */ + function ExpenseReportLine($db) + { + $this->db= $db; + } - /** - * fetch record - * - * @param int $rowid Id of object to load - * @return int <0 if KO, >0 if OK - */ - function fetch($rowid) - { - $sql = 'SELECT fde.rowid, fde.ref, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_projet, fde.date,'; - $sql.= ' fde.tva_tx as vatrate, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc,'; - $sql.= ' ctf.code as type_fees_code, ctf.label as type_fees_libelle,'; - $sql.= ' pjt.rowid as projet_id, pjt.title as projet_title, pjt.ref as projet_ref'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'expensereport_det as fde'; - $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pjt ON fde.fk_projet=pjt.rowid'; - $sql.= ' WHERE fde.rowid = '.$rowid; + /** + * fetch record + * + * @param int $rowid Id of object to load + * @return int <0 if KO, >0 if OK + */ + function fetch($rowid) + { + $sql = 'SELECT fde.rowid, fde.ref, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_projet, fde.date,'; + $sql.= ' fde.tva_tx as vatrate, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc,'; + $sql.= ' ctf.code as type_fees_code, ctf.label as type_fees_libelle,'; + $sql.= ' pjt.rowid as projet_id, pjt.title as projet_title, pjt.ref as projet_ref'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'expensereport_det as fde'; + $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pjt ON fde.fk_projet=pjt.rowid'; + $sql.= ' WHERE fde.rowid = '.$rowid; - $result = $this->db->query($sql); + $result = $this->db->query($sql); - if($result) - { - $objp = $this->db->fetch_object($result); + if($result) + { + $objp = $this->db->fetch_object($result); - $this->rowid = $objp->rowid; - $this->id = $obj->rowid; - $this->ref = $obj->ref; - $this->fk_expensereport = $objp->fk_expensereport; - $this->comments = $objp->comments; - $this->qty = $objp->qty; - $this->date = $objp->date; - $this->value_unit = $objp->value_unit; - $this->fk_c_type_fees = $objp->fk_c_type_fees; - $this->fk_projet = $objp->fk_projet; - $this->type_fees_code = $objp->type_fees_code; - $this->type_fees_libelle = $objp->type_fees_libelle; - $this->projet_ref = $objp->projet_ref; - $this->projet_title = $objp->projet_title; - $this->vatrate = $objp->vatrate; - $this->total_ht = $objp->total_ht; - $this->total_tva = $objp->total_tva; - $this->total_ttc = $objp->total_ttc; + $this->rowid = $objp->rowid; + $this->id = $obj->rowid; + $this->ref = $obj->ref; + $this->fk_expensereport = $objp->fk_expensereport; + $this->comments = $objp->comments; + $this->qty = $objp->qty; + $this->date = $objp->date; + $this->value_unit = $objp->value_unit; + $this->fk_c_type_fees = $objp->fk_c_type_fees; + $this->fk_projet = $objp->fk_projet; + $this->type_fees_code = $objp->type_fees_code; + $this->type_fees_libelle = $objp->type_fees_libelle; + $this->projet_ref = $objp->projet_ref; + $this->projet_title = $objp->projet_title; + $this->vatrate = $objp->vatrate; + $this->total_ht = $objp->total_ht; + $this->total_tva = $objp->total_tva; + $this->total_ttc = $objp->total_ttc; - $this->db->free($result); - } else { - dol_print_error($this->db); - } - } + $this->db->free($result); + } else { + dol_print_error($this->db); + } + } - /** - * insert - * - * @param int $notrigger 1=No trigger - * @return int <0 if KO, >0 if OK - */ - function insert($notrigger=0) - { - global $langs,$user,$conf; + /** + * insert + * + * @param int $notrigger 1=No trigger + * @return int <0 if KO, >0 if OK + */ + function insert($notrigger=0) + { + global $langs,$user,$conf; - $error=0; + $error=0; - dol_syslog("ExpenseReportLine::Insert rang=".$this->rang, LOG_DEBUG); + dol_syslog("ExpenseReportLine::Insert rang=".$this->rang, LOG_DEBUG); - // Clean parameters - $this->comments=trim($this->comments); - if (!$this->value_unit_HT) $this->value_unit_HT=0; - $this->qty = price2num($this->qty); - $this->vatrate = price2num($this->vatrate); + // Clean parameters + $this->comments=trim($this->comments); + if (!$this->value_unit_HT) $this->value_unit_HT=0; + $this->qty = price2num($this->qty); + $this->vatrate = price2num($this->vatrate); - $this->db->begin(); + $this->db->begin(); - $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'expensereport_det'; - $sql.= ' (fk_expensereport, fk_c_type_fees, fk_projet,'; - $sql.= ' tva_tx, comments, qty, value_unit, total_ht, total_tva, total_ttc, date)'; - $sql.= " VALUES (".$this->fk_expensereport.","; - $sql.= " ".$this->fk_c_type_fees.","; - $sql.= " ".($this->fk_projet>0?$this->fk_projet:'null').","; - $sql.= " ".$this->vatrate.","; - $sql.= " '".$this->db->escape($this->comments)."',"; - $sql.= " ".$this->qty.","; - $sql.= " ".$this->value_unit.","; - $sql.= " ".$this->total_ht.","; - $sql.= " ".$this->total_tva.","; - $sql.= " ".$this->total_ttc.","; - $sql.= "'".$this->db->idate($this->date)."'"; - $sql.= ")"; + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'expensereport_det'; + $sql.= ' (fk_expensereport, fk_c_type_fees, fk_projet,'; + $sql.= ' tva_tx, comments, qty, value_unit, total_ht, total_tva, total_ttc, date)'; + $sql.= " VALUES (".$this->fk_expensereport.","; + $sql.= " ".$this->fk_c_type_fees.","; + $sql.= " ".($this->fk_projet>0?$this->fk_projet:'null').","; + $sql.= " ".$this->vatrate.","; + $sql.= " '".$this->db->escape($this->comments)."',"; + $sql.= " ".$this->qty.","; + $sql.= " ".$this->value_unit.","; + $sql.= " ".$this->total_ht.","; + $sql.= " ".$this->total_tva.","; + $sql.= " ".$this->total_ttc.","; + $sql.= "'".$this->db->idate($this->date)."'"; + $sql.= ")"; - dol_syslog("ExpenseReportLine::insert sql=".$sql); + dol_syslog("ExpenseReportLine::insert sql=".$sql); - $resql=$this->db->query($sql); - if ($resql) - { - $this->rowid=$this->db->last_insert_id(MAIN_DB_PREFIX.'expensereport_det'); + $resql=$this->db->query($sql); + if ($resql) + { + $this->rowid=$this->db->last_insert_id(MAIN_DB_PREFIX.'expensereport_det'); - $tmpparent=new ExpenseReport($this->db); - $tmpparent->fetch($this->fk_expensereport); - $result = $tmpparent->update_price(); - if ($result < 0) - { - $error++; - $this->error = $tmpparent->error; - $this->errors = $tmpparent->errors; - } - } + $tmpparent=new ExpenseReport($this->db); + $tmpparent->fetch($this->fk_expensereport); + $result = $tmpparent->update_price(); + if ($result < 0) + { + $error++; + $this->error = $tmpparent->error; + $this->errors = $tmpparent->errors; + } + } - if (! $error) - { - $this->db->commit(); - return $this->rowid; - } - else - { - $this->error=$this->db->lasterror(); - dol_syslog("ExpenseReportLine::insert Error ".$this->error, LOG_ERR); - $this->db->rollback(); - return -2; - } - } + if (! $error) + { + $this->db->commit(); + return $this->rowid; + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog("ExpenseReportLine::insert Error ".$this->error, LOG_ERR); + $this->db->rollback(); + return -2; + } + } - /** - * update - * - * @param User $fuser User - * @return int <0 if KO, >0 if OK - */ - function update($fuser) - { - global $fuser,$langs,$conf; + /** + * update + * + * @param User $fuser User + * @return int <0 if KO, >0 if OK + */ + function update($fuser) + { + global $fuser,$langs,$conf; - $error=0; + $error=0; - // Clean parameters - $this->comments=trim($this->comments); - $this->vatrate = price2num($this->vatrate); + // Clean parameters + $this->comments=trim($this->comments); + $this->vatrate = price2num($this->vatrate); - $this->db->begin(); + $this->db->begin(); - // Mise a jour ligne en base - $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport_det SET"; - $sql.= " comments='".$this->db->escape($this->comments)."'"; - $sql.= ",value_unit=".$this->value_unit.""; - $sql.= ",qty=".$this->qty.""; - $sql.= ",date='".$this->db->idate($this->date)."'"; - $sql.= ",total_ht=".$this->total_ht.""; - $sql.= ",total_tva=".$this->total_tva.""; - $sql.= ",total_ttc=".$this->total_ttc.""; - $sql.= ",tva_tx=".$this->vatrate; - if ($this->fk_c_type_fees) $sql.= ",fk_c_type_fees=".$this->fk_c_type_fees; - else $sql.= ",fk_c_type_fees=null"; - if ($this->fk_projet) $sql.= ",fk_projet=".$this->fk_projet; - else $sql.= ",fk_projet=null"; - $sql.= " WHERE rowid = ".$this->rowid; + // Mise a jour ligne en base + $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport_det SET"; + $sql.= " comments='".$this->db->escape($this->comments)."'"; + $sql.= ",value_unit=".$this->value_unit.""; + $sql.= ",qty=".$this->qty.""; + $sql.= ",date='".$this->db->idate($this->date)."'"; + $sql.= ",total_ht=".$this->total_ht.""; + $sql.= ",total_tva=".$this->total_tva.""; + $sql.= ",total_ttc=".$this->total_ttc.""; + $sql.= ",tva_tx=".$this->vatrate; + if ($this->fk_c_type_fees) $sql.= ",fk_c_type_fees=".$this->fk_c_type_fees; + else $sql.= ",fk_c_type_fees=null"; + if ($this->fk_projet) $sql.= ",fk_projet=".$this->fk_projet; + else $sql.= ",fk_projet=null"; + $sql.= " WHERE rowid = ".$this->rowid; - dol_syslog("ExpenseReportLine::update sql=".$sql); + dol_syslog("ExpenseReportLine::update sql=".$sql); - $resql=$this->db->query($sql); - if ($resql) - { - $tmpparent=new ExpenseReport($this->db); - $result = $tmpparent->fetch($this->fk_expensereport); - if ($result > 0) - { - $result = $tmpparent->update_price(); - if ($result < 0) - { - $error++; - $this->error = $tmpparent->error; - $this->errors = $tmpparent->errors; - } - } - else - { - $error++; - $this->error = $tmpparent->error; - $this->errors = $tmpparent->errors; - } - } - else - { - $error++; - dol_print_error($this->db); - } + $resql=$this->db->query($sql); + if ($resql) + { + $tmpparent=new ExpenseReport($this->db); + $result = $tmpparent->fetch($this->fk_expensereport); + if ($result > 0) + { + $result = $tmpparent->update_price(); + if ($result < 0) + { + $error++; + $this->error = $tmpparent->error; + $this->errors = $tmpparent->errors; + } + } + else + { + $error++; + $this->error = $tmpparent->error; + $this->errors = $tmpparent->errors; + } + } + else + { + $error++; + dol_print_error($this->db); + } - if (! $error) - { - $this->db->commit(); - return 1; - } - else - { - $this->error=$this->db->lasterror(); - dol_syslog("ExpenseReportLine::update Error ".$this->error, LOG_ERR); - $this->db->rollback(); - return -2; - } - } + if (! $error) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog("ExpenseReportLine::update Error ".$this->error, LOG_ERR); + $this->db->rollback(); + return -2; + } + } } @@ -1708,74 +1708,74 @@ class ExpenseReportLine * Retourne la liste deroulante des differents etats d'une note de frais. * Les valeurs de la liste sont les id de la table c_expensereport_statuts * - * @param int $selected preselect status - * @param string $htmlname Name of HTML select - * @param int $useempty 1=Add empty line - * @return string HTML select with status + * @param int $selected preselect status + * @param string $htmlname Name of HTML select + * @param int $useempty 1=Add empty line + * @return string HTML select with status */ function select_expensereport_statut($selected='',$htmlname='fk_statut',$useempty=1) { - global $db; + global $db; - $tmpep=new ExpenseReport($db); + $tmpep=new ExpenseReport($db); - print ''; + print ''; } /** - * Return list of types of notes with select value = id + * Return list of types of notes with select value = id * - * @param int $selected Preselected type - * @param string $htmlname Name of field in form - * @param int $showempty Add an empty field - * @return string Select html + * @param int $selected Preselected type + * @param string $htmlname Name of field in form + * @param int $showempty Add an empty field + * @return string Select html */ function select_type_fees_id($selected='',$htmlname='type',$showempty=0) { - global $db,$langs,$user; - $langs->load("trips"); + global $db,$langs,$user; + $langs->load("trips"); - print ''; + if ($showempty) + { + print ''; + } - $sql = "SELECT c.id, c.code, c.label as type FROM ".MAIN_DB_PREFIX."c_type_fees as c"; - $sql.= " ORDER BY c.label ASC"; - $resql=$db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - $i = 0; + $sql = "SELECT c.id, c.code, c.label as type FROM ".MAIN_DB_PREFIX."c_type_fees as c"; + $sql.= " ORDER BY c.label ASC"; + $resql=$db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + $i = 0; - while ($i < $num) - { - $obj = $db->fetch_object($resql); - print '