2015-02-14 18:39:45 +01:00
< ? php
2018-11-27 11:36:31 +01:00
/* Copyright ( C ) 2011 Dimitri Mouillard < dmouillard @ teclib . com >
* Copyright ( C ) 2015 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2015 Alexandre Spangaro < aspangaro @ zendsi . com >
2018-12-04 14:29:47 +01:00
* Copyright ( C ) 2018 Nicolas ZABOURI < info @ inovea - conseil . com >
2018-10-27 21:21:59 +02:00
* Copyright ( c ) 2018 Frédéric France < frederic . france @ netlogic . fr >
2018-11-27 11:36:31 +01:00
* Copyright ( C ) 2016 - 2018 Ferran Marcet < fmarcet @ 2 byte . es >
2015-03-07 15:34:58 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2015-03-17 11:23:45 +01:00
/**
* \file htdocs / expensereport / class / expensereport . class . php
* \ingroup expensereport
* \brief File to manage Expense Reports
*/
2015-03-07 15:34:58 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2017-06-15 11:08:53 +02:00
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport_ik.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport_rule.class.php' ;
2015-02-14 18:39:45 +01:00
/**
* Class to manage Trips and Expenses
*/
class ExpenseReport extends CommonObject
{
2018-08-22 11:48:26 +02:00
/**
* @ var string ID to identify managed object
*/
public $element = 'expensereport' ;
2018-08-29 15:37:35 +02:00
2018-08-22 12:04:50 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
public $table_element = 'expensereport' ;
2018-08-29 15:37:35 +02:00
2015-06-29 13:12:37 +02:00
var $table_element_line = 'expensereport_det' ;
var $fk_element = 'fk_expensereport' ;
2016-10-14 17:44:41 +02:00
var $picto = 'trip' ;
2015-06-29 13:12:37 +02:00
2017-12-01 15:39:18 +01:00
var $lines = array ();
2017-07-08 17:57:35 +02:00
2016-09-09 20:09:36 +02:00
public $date_debut ;
2017-07-08 17:57:35 +02:00
2016-09-09 20:09:36 +02:00
public $date_fin ;
2015-06-29 13:12:37 +02:00
var $status ;
2016-09-06 12:47:18 +02:00
var $fk_statut ; // -- 0=draft, 2=validated (attente approb), 4=canceled, 5=approved, 6=payed, 99=denied
2015-06-29 13:12:37 +02:00
var $fk_c_paiement ;
var $paid ;
var $user_author_infos ;
var $user_validator_infos ;
2015-02-14 18:39:45 +01:00
2015-07-15 20:22:40 +02:00
var $fk_typepayment ;
var $num_payment ;
2015-06-29 13:12:37 +02:00
var $code_paiement ;
var $code_statut ;
2015-08-28 20:11:16 +02:00
// ACTIONS
2015-06-29 13:12:37 +02:00
2015-08-28 20:11:16 +02:00
// Create
2015-06-29 13:12:37 +02:00
var $date_create ;
2017-02-24 15:51:10 +01:00
var $fk_user_author ; // Note fk_user_author is not the 'author' but the guy the expense report is for.
2015-06-29 13:12:37 +02:00
2015-08-28 20:11:16 +02:00
// Update
var $date_modif ;
var $fk_user_modif ;
2017-07-08 17:57:35 +02:00
2015-06-29 13:12:37 +02:00
// Refus
var $date_refuse ;
var $detail_refuse ;
var $fk_user_refuse ;
// Annulation
var $date_cancel ;
var $detail_cancel ;
var $fk_user_cancel ;
2018-03-19 17:20:44 +01:00
var $fk_user_validator ; // User that is defined to approve
2015-06-29 13:12:37 +02:00
// Validation
2018-03-19 17:20:44 +01:00
var $date_valid ; // User making validation
2015-06-29 13:12:37 +02:00
var $fk_user_valid ;
var $user_valid_infos ;
// Approve
var $date_approve ;
2018-03-19 17:20:44 +01:00
var $fk_user_approve ; // User that has approved
2015-06-29 13:12:37 +02:00
// Paiement
var $user_paid_infos ;
/*
END ACTIONS
*/
2018-10-04 09:33:30 +02:00
/**
* Draft status
2017-06-15 11:08:53 +02:00
*/
const STATUS_DRAFT = 0 ;
2015-06-29 13:12:37 +02:00
2017-06-15 11:08:53 +02:00
/**
* Validated ( need to be paid )
*/
const STATUS_VALIDATED = 2 ;
2015-06-29 13:12:37 +02:00
2017-06-15 11:08:53 +02:00
/**
* Classified approved
*/
const STATUS_APPROVED = 5 ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
/**
* Classified refused
*/
const STATUS_REFUSED = 99 ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
/**
* Classified paid .
*/
const STATUS_CLOSED = 6 ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
/**
2015-06-29 13:12:37 +02:00
* 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
2015-05-28 06:16:58 +02:00
$this -> statuts_short = array ( 0 => 'Draft' , 2 => 'Validated' , 4 => 'Canceled' , 5 => 'Approved' , 6 => 'Paid' , 99 => 'Refused' );
2015-06-23 10:13:08 +02:00
$this -> statuts = array ( 0 => 'Draft' , 2 => 'ValidatedWaitingApproval' , 4 => 'Canceled' , 5 => 'Approved' , 6 => 'Paid' , 99 => 'Refused' );
2018-09-04 09:42:28 +02:00
$this -> statuts_logo = array ( 0 => 'statut0' , 2 => 'statut1' , 4 => 'statut5' , 5 => 'statut3' , 6 => 'statut6' , 99 => 'statut5' );
2015-06-29 13:12:37 +02:00
}
/**
* Create object in database
*
* @ param User $user User that create
2017-02-02 11:42:38 +01:00
* @ param int $notrigger Disable triggers
2015-06-29 13:12:37 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2017-02-02 11:42:38 +01:00
function create ( $user , $notrigger = 0 )
2015-06-29 13:12:37 +02:00
{
global $conf ;
$now = dol_now ();
2017-02-27 01:19:03 +01:00
$error = 0 ;
2017-07-08 18:32:34 +02:00
2017-04-14 18:59:01 +02:00
// Check parameters
2017-07-08 18:32:34 +02:00
if ( empty ( $this -> date_debut ) || empty ( $this -> date_fin ))
2017-04-14 18:59:01 +02:00
{
$this -> error = 'ErrorFieldRequired' ;
return - 1 ;
}
2017-07-08 18:32:34 +02:00
2017-02-24 15:51:10 +01:00
$fuserid = $this -> fk_user_author ; // Note fk_user_author is not the 'author' but the guy the expense report is for.
2016-11-13 22:00:57 +01:00
if ( empty ( $fuserid )) $fuserid = $user -> id ;
2017-07-08 17:57:35 +02:00
2015-06-29 13:12:37 +02:00
$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 " ;
2018-03-19 17:20:44 +01:00
$sql .= " ,fk_user_approve " ;
2015-08-28 20:11:16 +02:00
$sql .= " ,fk_user_modif " ;
2015-06-29 13:12:37 +02:00
$sql .= " ,fk_statut " ;
$sql .= " ,fk_c_paiement " ;
$sql .= " ,paid " ;
$sql .= " ,note_public " ;
$sql .= " ,note_private " ;
2016-05-16 17:21:32 +02:00
$sql .= " ,entity " ;
2015-06-29 13:12:37 +02:00
$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 ) . " ' " ;
2016-11-13 22:00:57 +01:00
$sql .= " , " . $fuserid ;
2015-06-29 13:12:37 +02:00
$sql .= " , " . ( $this -> fk_user_validator > 0 ? $this -> fk_user_validator : " null " );
2018-03-19 17:20:44 +01:00
$sql .= " , " . ( $this -> fk_user_approve > 0 ? $this -> fk_user_approve : " null " );
2015-08-28 20:11:16 +02:00
$sql .= " , " . ( $this -> fk_user_modif > 0 ? $this -> fk_user_modif : " null " );
2015-06-29 13:12:37 +02:00
$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 " );
2016-05-16 17:21:32 +02:00
$sql .= " , " . $conf -> entity ;
2015-06-29 13:12:37 +02:00
$sql .= " ) " ;
$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 . ')' ;
2017-05-12 16:55:11 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . " SET ref=' " . $this -> db -> escape ( $this -> ref ) . " ' WHERE rowid= " . $this -> id ;
2015-06-29 13:12:37 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) $error ++ ;
2017-11-12 20:41:25 +01:00
if ( is_array ( $this -> lines ) && count ( $this -> lines ) > 0 )
{
2017-09-15 13:25:54 +02:00
foreach ( $this -> lines as $i => $val )
{
$newndfline = new ExpenseReportLine ( $this -> db );
$newndfline = $this -> lines [ $i ];
$newndfline -> fk_expensereport = $this -> id ;
if ( $result >= 0 )
{
$result = $newndfline -> insert ();
}
if ( $result < 0 )
{
$error ++ ;
break ;
}
}
2015-06-29 13:12:37 +02:00
}
2017-11-12 20:41:25 +01:00
if ( ! $error )
{
$result = $this -> insertExtraFields ();
if ( $result < 0 ) $error ++ ;
}
2015-06-29 13:12:37 +02:00
if ( ! $error )
{
$result = $this -> update_price ();
if ( $result > 0 )
{
2017-07-08 18:32:34 +02:00
2017-02-02 11:42:38 +01:00
if ( ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'EXPENSE_REPORT_CREATE' , $user );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
if ( empty ( $error ))
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
$this -> db -> rollback ();
return - 4 ;
}
2015-06-29 13:12:37 +02:00
}
else
{
$this -> db -> rollback ();
return - 3 ;
}
}
else
{
dol_syslog ( get_class ( $this ) . " ::create error " . $this -> error , LOG_ERR );
$this -> db -> rollback ();
return - 2 ;
}
}
else
{
2017-04-14 16:50:30 +02:00
$this -> error = $this -> db -> lasterror () . " sql= " . $sql ;
2015-06-29 13:12:37 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
2016-11-13 22:00:57 +01:00
/**
* Load an object from its id and create a new one in database
*
2017-05-29 13:19:45 +02:00
* @ param int $fk_user_author Id of new user
* @ return int New id of clone
2016-11-13 22:00:57 +01:00
*/
2017-05-29 13:19:45 +02:00
function createFromClone ( $fk_user_author )
2016-11-13 22:00:57 +01:00
{
global $user , $hookmanager ;
2017-07-08 17:57:35 +02:00
2016-11-13 22:00:57 +01:00
$error = 0 ;
2017-07-08 17:57:35 +02:00
2017-05-29 13:19:45 +02:00
if ( empty ( $fk_user_author )) $fk_user_author = $user -> id ;
2017-07-08 17:57:35 +02:00
2016-11-13 22:00:57 +01:00
$this -> context [ 'createfromclone' ] = 'createfromclone' ;
2017-08-22 10:11:32 +02:00
2016-11-13 22:00:57 +01:00
$this -> db -> begin ();
2017-07-08 17:57:35 +02:00
2016-11-13 22:00:57 +01:00
// get extrafields so they will be clone
2017-04-14 16:50:30 +02:00
//foreach($this->lines as $line)
2016-11-13 22:00:57 +01:00
//$line->fetch_optionals($line->rowid);
2017-07-08 17:57:35 +02:00
2017-04-14 16:50:30 +02:00
// Load source object
$objFrom = clone $this ;
$this -> id = 0 ;
$this -> ref = '' ;
$this -> status = 0 ;
$this -> fk_statut = 0 ;
// Clear fields
2017-05-29 13:19:45 +02:00
$this -> fk_user_author = $fk_user_author ; // Note fk_user_author is not the 'author' but the guy the expense report is for.
2017-04-14 16:50:30 +02:00
$this -> fk_user_valid = '' ;
$this -> date_create = '' ;
$this -> date_creation = '' ;
$this -> date_validation = '' ;
// Create clone
$result = $this -> create ( $user );
if ( $result < 0 ) $error ++ ;
if ( ! $error )
{
// Hook of thirdparty module
if ( is_object ( $hookmanager ))
2016-11-13 22:00:57 +01:00
{
2017-04-14 16:50:30 +02:00
$parameters = array ( 'objFrom' => $objFrom );
$action = '' ;
$reshook = $hookmanager -> executeHooks ( 'createFrom' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook < 0 ) $error ++ ;
2016-11-13 22:00:57 +01:00
}
2017-04-14 16:50:30 +02:00
}
unset ( $this -> context [ 'createfromclone' ]);
// End
if ( ! $error )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
2016-11-13 22:00:57 +01:00
}
2017-07-08 17:57:35 +02:00
2015-06-29 13:12:37 +02:00
/**
* update
*
2017-02-27 02:14:37 +01:00
* @ param User $user User making change
* @ param int $notrigger Disable triggers
* @ param User $userofexpensereport New user we want to have the expense report on .
* @ return int < 0 if KO , > 0 if OK
2015-06-29 13:12:37 +02:00
*/
2017-02-27 01:23:21 +01:00
function update ( $user , $notrigger = 0 , $userofexpensereport = null )
2015-06-29 13:12:37 +02:00
{
global $langs ;
2017-02-02 11:42:38 +01:00
$error = 0 ;
$this -> db -> begin ();
2017-07-08 18:32:34 +02:00
2015-06-29 13:12:37 +02:00
$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 ) . " ' " ;
2017-02-24 15:51:10 +01:00
if ( $userofexpensereport && is_object ( $userofexpensereport ))
{
$sql .= " , fk_user_author = " . ( $userofexpensereport -> id > 0 ? " ' " . $userofexpensereport -> id . " ' " : " null " ); // Note fk_user_author is not the 'author' but the guy the expense report is for.
}
2015-06-29 13:12:37 +02:00
$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 " );
2018-03-19 17:20:44 +01:00
$sql .= " , fk_user_approve = " . ( $this -> fk_user_approve > 0 ? $this -> fk_user_approve : " null " );
2017-02-24 15:51:10 +01:00
$sql .= " , fk_user_modif = " . $user -> id ;
2015-06-29 13:12:37 +02:00
$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 )
{
2017-02-02 11:42:38 +01:00
if ( ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'EXPENSE_REPORT_UPDATE' , $user );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
if ( empty ( $error ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 2 ;
}
2015-06-29 13:12:37 +02:00
}
else
{
2017-02-02 11:42:38 +01:00
$this -> db -> rollback ();
2015-06-29 13:12:37 +02:00
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
/**
* Load an object from database
*
2016-09-09 20:09:36 +02:00
* @ param int $id Id { @ min 1 }
* @ param string $ref Ref { @ name ref }
2015-06-29 13:12:37 +02:00
* @ 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)
2015-08-28 20:11:16 +02:00
$sql .= " d.date_debut, d.date_fin, d.date_create, d.tms as date_modif, d.date_valid, d.date_approve, " ; // DATES (datetime)
$sql .= " d.fk_user_author, d.fk_user_modif, d.fk_user_validator, " ;
2015-06-29 13:12:37 +02:00
$sql .= " d.fk_user_valid, d.fk_user_approve, " ;
2018-11-23 09:42:43 +01:00
$sql .= " d.fk_statut as status, d.fk_c_paiement, d.paid, " ;
2015-06-29 13:12:37 +02:00
$sql .= " dp.libelle as libelle_paiement, dp.code as code_paiement " ; // INNER JOIN paiement
2017-06-09 09:25:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as d " ;
2018-03-07 17:32:57 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_paiement as dp ON d.fk_c_paiement = dp.id " ;
2015-06-29 13:12:37 +02:00
if ( $ref ) $sql .= " WHERE d.ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
else $sql .= " WHERE d.rowid = " . $id ;
2017-07-15 17:32:22 +02:00
//$sql.= $restrict;
2015-06-29 13:12:37 +02:00
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 );
2015-08-28 20:11:16 +02:00
$this -> date_modif = $this -> db -> jdate ( $obj -> date_modif );
2015-06-29 13:12:37 +02:00
$this -> date_refuse = $this -> db -> jdate ( $obj -> date_refuse );
$this -> date_cancel = $this -> db -> jdate ( $obj -> date_cancel );
2017-02-24 15:51:10 +01:00
$this -> fk_user_author = $obj -> fk_user_author ; // Note fk_user_author is not the 'author' but the guy the expense report is for.
2015-08-28 20:11:16 +02:00
$this -> fk_user_modif = $obj -> fk_user_modif ;
2015-06-29 13:12:37 +02:00
$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 ;
2017-07-08 17:57:35 +02:00
2015-06-29 13:12:37 +02:00
$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 );
$user_approver = new User ( $this -> db );
2018-03-19 17:20:44 +01:00
if ( $this -> fk_user_approve > 0 ) $user_approver -> fetch ( $this -> fk_user_approve );
elseif ( $this -> fk_user_validator > 0 ) $user_approver -> fetch ( $this -> fk_user_validator ); // For backward compatibility
2015-06-29 13:12:37 +02:00
$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 ;
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 -> lines = array ();
$result = $this -> fetch_lines ();
return $result ;
}
else
{
return 0 ;
}
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
* Classify the expense report as paid
*
* @ param int $id Id of expense report
* @ param user $fuser User making change
2017-02-02 11:42:38 +01:00
* @ param int $notrigger Disable triggers
2015-06-29 13:12:37 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2017-02-02 11:42:38 +01:00
function set_paid ( $id , $fuser , $notrigger = 0 )
2015-06-29 13:12:37 +02:00
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2017-02-02 11:42:38 +01:00
$error = 0 ;
$this -> db -> begin ();
2017-07-08 18:32:34 +02:00
2015-06-29 13:12:37 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " expensereport " ;
2016-01-28 13:57:40 +01:00
$sql .= " SET fk_statut = 6, paid=1 " ;
2015-06-29 13:12:37 +02:00
$sql .= " WHERE rowid = " . $id . " AND fk_statut = 5 " ;
dol_syslog ( get_class ( $this ) . " ::set_paid sql= " . $sql , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> affected_rows ( $resql ))
{
2017-02-02 11:42:38 +01:00
if ( ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'EXPENSE_REPORT_PAID' , $fuser );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
2017-07-08 18:32:34 +02:00
2017-02-02 11:42:38 +01:00
if ( empty ( $error ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 2 ;
}
2015-06-29 13:12:37 +02:00
}
else
{
2017-02-02 11:42:38 +01:00
$this -> db -> commit ();
2015-06-29 13:12:37 +02:00
return 0 ;
}
}
else
{
2017-02-02 11:42:38 +01:00
$this -> db -> rollback ();
2015-06-29 13:12:37 +02:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
/**
* 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 );
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
* Returns the label of a statut
*
* @ param int $status id statut
2017-01-22 16:03:08 +01:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto , 6 = Long label + Picto
2015-06-29 13:12:37 +02:00
* @ return string Label
*/
function LibStatut ( $status , $mode = 0 )
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-06-29 13:12:37 +02:00
global $langs ;
if ( $mode == 0 )
2015-07-07 12:27:36 +02:00
return $langs -> transnoentities ( $this -> statuts [ $status ]);
2015-06-29 13:12:37 +02:00
2018-09-02 18:49:23 +02:00
elseif ( $mode == 1 )
2015-07-07 12:27:36 +02:00
return $langs -> transnoentities ( $this -> statuts_short [ $status ]);
2015-06-29 13:12:37 +02:00
2018-09-02 18:49:23 +02:00
elseif ( $mode == 2 )
2015-07-07 12:27:36 +02:00
return img_picto ( $langs -> transnoentities ( $this -> statuts_short [ $status ]), $this -> statuts_logo [ $status ]) . ' ' . $langs -> transnoentities ( $this -> statuts_short [ $status ]);
2015-06-29 13:12:37 +02:00
2018-09-02 18:49:23 +02:00
elseif ( $mode == 3 )
2015-07-07 12:27:36 +02:00
return img_picto ( $langs -> transnoentities ( $this -> statuts_short [ $status ]), $this -> statuts_logo [ $status ]);
2015-06-29 13:12:37 +02:00
2018-09-02 18:49:23 +02:00
elseif ( $mode == 4 )
2015-07-07 12:27:36 +02:00
return img_picto ( $langs -> transnoentities ( $this -> statuts_short [ $status ]), $this -> statuts_logo [ $status ]) . ' ' . $langs -> transnoentities ( $this -> statuts [ $status ]);
2015-06-29 13:12:37 +02:00
2018-09-02 18:49:23 +02:00
elseif ( $mode == 5 )
2015-07-07 12:27:36 +02:00
return '<span class="hideonsmartphone">' . $langs -> transnoentities ( $this -> statuts_short [ $status ]) . ' </span>' . img_picto ( $langs -> transnoentities ( $this -> statuts_short [ $status ]), $this -> statuts_logo [ $status ]);
2015-06-29 13:12:37 +02:00
2018-09-02 18:49:23 +02:00
elseif ( $mode == 6 )
2017-01-22 16:03:08 +01:00
return $langs -> transnoentities ( $this -> statuts [ $status ]) . ' ' . img_picto ( $langs -> transnoentities ( $this -> statuts_short [ $status ]), $this -> statuts_logo [ $status ]);
2015-06-29 13:12:37 +02:00
}
/**
* 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, " ;
2017-07-08 21:39:48 +02:00
//$sql.= " f.fk_user_author as fk_user_creation,"; // This is not user of creation but user the expense is for.
2015-06-29 13:12:37 +02:00
$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 ;
}
2015-08-28 20:11:16 +02:00
if ( $obj -> fk_user_approve )
{
$auser = new User ( $this -> db );
$auser -> fetch ( $obj -> fk_user_approve );
$this -> user_approve = $auser ;
}
2015-06-29 13:12:37 +02:00
}
$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 ;
2017-08-29 19:44:35 +02:00
$this -> date_valid = $now ;
2015-06-29 13:12:37 +02:00
$this -> date_approve = $now ;
2017-07-08 18:32:34 +02:00
$type_fees_id = 2 ; // TF_TRIP
2015-06-29 13:12:37 +02:00
$this -> status = 5 ;
$this -> fk_statut = 5 ;
$this -> fk_user_author = $user -> id ;
2018-03-19 17:20:44 +01:00
$this -> fk_user_validator = $user -> id ;
2015-06-29 13:12:37 +02:00
$this -> fk_user_valid = $user -> id ;
$this -> fk_user_approve = $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' ;
2017-04-14 16:50:30 +02:00
$line -> fk_c_type_fees = $type_fees_id ;
2015-06-29 13:12:37 +02:00
$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 ;
}
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
* 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 = '' )
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-06-29 13:12:37 +02:00
global $conf , $db , $langs ;
$langs -> load ( 'trips' );
2017-04-14 16:50:30 +02:00
if ( $user -> rights -> expensereport -> lire ) {
2015-06-29 13:12:37 +02:00
$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 '<tr>' ;
print '<td><a href="' . DOL_URL_ROOT . '/expensereport/card.php?id=' . $objp -> rowid . '">' . $objp -> ref_num . '</a></td>' ;
print '<td align="center">' . dol_print_date ( $objp -> date , 'day' ) . '</td>' ;
2017-10-03 16:00:52 +02:00
print '<td>' . $author -> getNomUrl ( 1 ) . '</td>' ;
2015-06-29 13:12:37 +02:00
print '<td>' . $objp -> comments . '</td>' ;
print '<td align="right">' . price ( $objp -> total_ht ) . '</td>' ;
print '<td align="right">' . price ( $objp -> total_ttc ) . '</td>' ;
print '<td align="right">' ;
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 '</td>' ;
print '</tr>' ;
$i ++ ;
}
print '<tr class="liste_total"><td colspan="4">' . $langs -> trans ( " Number " ) . ': ' . $i . '</td>' ;
print '<td align="right" width="100">' . $langs -> trans ( " TotalHT " ) . ' : ' . price ( $total_HT ) . '</td>' ;
print '<td align="right" width="100">' . $langs -> trans ( " TotalTTC " ) . ' : ' . price ( $total_TTC ) . '</td>' ;
print '<td> </td>' ;
print '</tr>' ;
}
else
{
2017-04-14 16:50:30 +02:00
$this -> error = $db -> lasterror ();
2015-06-29 13:12:37 +02:00
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 ;
$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 :
2017-04-14 16:50:30 +02:00
$this -> error = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . " ::recalculer: Error " . $this -> error , LOG_ERR );
2015-06-29 13:12:37 +02:00
return - 3 ;
endif ;
}
else
{
2017-04-14 16:50:30 +02:00
$this -> error = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . " ::recalculer: Error " . $this -> error , LOG_ERR );
2015-06-29 13:12:37 +02:00
return - 3 ;
}
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
* fetch_lines
*
* @ return int < 0 if OK , > 0 if KO
*/
function fetch_lines ()
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-06-29 13:12:37 +02:00
$this -> lines = array ();
2017-08-22 10:11:32 +02:00
$sql = ' SELECT de.rowid, de.comments, de.qty, de.value_unit, de.date, de.rang,' ;
2017-06-15 11:08:53 +02:00
$sql .= ' de.' . $this -> fk_element . ', de.fk_c_type_fees, de.fk_c_exp_tax_cat, de.fk_projet, de.tva_tx,' ;
2015-06-29 13:12:37 +02:00
$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' ;
2016-04-29 13:23:06 +02:00
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_type_fees as ctf ON de.fk_c_type_fees = ctf.id' ;
2015-06-29 13:12:37 +02:00
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'projet as p ON de.fk_projet = p.rowid' ;
$sql .= ' WHERE de.' . $this -> fk_element . ' = ' . $this -> id ;
2017-08-22 10:11:32 +02:00
if ( ! empty ( $conf -> global -> EXPENSEREPORT_LINES_SORTED_BY_ROWID ))
{
$sql .= ' ORDER BY de.rang ASC, de.rowid ASC' ;
}
else
{
$sql .= ' ORDER BY de.rang ASC, de.date ASC' ;
}
2015-06-29 13:12:37 +02:00
$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 ;
2017-06-15 11:08:53 +02:00
$deplig -> id = $objp -> id ;
2015-06-29 13:12:37 +02:00
$deplig -> comments = $objp -> comments ;
$deplig -> qty = $objp -> qty ;
$deplig -> value_unit = $objp -> value_unit ;
$deplig -> date = $objp -> date ;
2017-06-15 11:08:53 +02:00
$deplig -> dates = $this -> db -> jdate ( $objp -> date );
2015-06-29 13:12:37 +02:00
$deplig -> fk_expensereport = $objp -> fk_expensereport ;
2017-08-22 10:11:32 +02:00
$deplig -> fk_c_type_fees = $objp -> fk_c_type_fees ;
2017-06-15 11:08:53 +02:00
$deplig -> fk_c_exp_tax_cat = $objp -> fk_c_exp_tax_cat ;
2017-08-22 10:11:32 +02:00
$deplig -> fk_projet = $objp -> fk_projet ;
2015-06-29 13:12:37 +02:00
2017-08-22 10:11:32 +02:00
$deplig -> total_ht = $objp -> total_ht ;
$deplig -> total_tva = $objp -> total_tva ;
$deplig -> total_ttc = $objp -> total_ttc ;
2015-06-29 13:12:37 +02:00
2016-04-29 13:23:06 +02:00
$deplig -> type_fees_code = empty ( $objp -> code_type_fees ) ? 'TF_OTHER' : $objp -> code_type_fees ;
2015-06-29 13:12:37 +02:00
$deplig -> type_fees_libelle = $objp -> libelle_type_fees ;
2015-07-16 20:51:22 +02:00
$deplig -> tva_tx = $objp -> tva_tx ;
$deplig -> vatrate = $objp -> tva_tx ;
2015-06-29 13:12:37 +02:00
$deplig -> projet_ref = $objp -> ref_projet ;
$deplig -> projet_title = $objp -> title_projet ;
2017-08-22 10:11:32 +02:00
$deplig -> rang = $objp -> rang ;
2015-06-29 13:12:37 +02:00
$this -> lines [ $i ] = $deplig ;
$i ++ ;
}
$this -> db -> free ( $resql );
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
2017-04-14 16:50:30 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch_lines: Error " . $this -> error , LOG_ERR );
2015-06-29 13:12:37 +02:00
return - 3 ;
}
}
/**
* delete
*
* @ param User $fuser User that delete
* @ return int < 0 if KO , > 0 if OK
*/
2016-09-09 15:36:33 +02:00
function delete ( User $fuser = null )
2015-06-29 13:12:37 +02:00
{
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 ;
2017-04-14 16:50:30 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " . $this -> error , LOG_ERR );
2015-06-29 13:12:37 +02:00
$this -> db -> rollback ();
return - 6 ;
}
}
else
{
$this -> error = $this -> db -> error () . " sql= " . $sql ;
2017-04-14 16:50:30 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " . $this -> error , LOG_ERR );
2015-06-29 13:12:37 +02:00
$this -> db -> rollback ();
return - 4 ;
}
}
/**
* Set to status validate
*
* @ param User $fuser User
2017-02-02 11:42:38 +01:00
* @ param int $notrigger Disable triggers
2017-04-14 16:50:30 +02:00
* @ return int < 0 if KO , 0 if nothing done , > 0 if OK
2015-06-29 13:12:37 +02:00
*/
2017-02-02 11:42:38 +01:00
function setValidate ( $fuser , $notrigger = 0 )
2015-06-29 13:12:37 +02:00
{
2017-08-27 17:52:44 +02:00
global $conf , $langs , $user ;
2015-06-29 13:12:37 +02:00
2017-02-02 11:42:38 +01:00
$error = 0 ;
2017-08-29 19:44:35 +02:00
$now = dol_now ();
2017-08-27 17:52:44 +02:00
// Protection
if ( $this -> statut == self :: STATUS_VALIDATED )
2015-06-29 13:12:37 +02:00
{
2017-08-27 17:52:44 +02:00
dol_syslog ( get_class ( $this ) . " ::valid action abandonned: already validated " , LOG_WARNING );
return 0 ;
2015-06-29 13:12:37 +02:00
}
2017-08-29 19:44:35 +02:00
$this -> date_valid = $now ; // Required for the getNextNum later.
2017-08-27 17:52:44 +02:00
// Define new ref
if ( ! $error && ( preg_match ( '/^[\(]?PROV/i' , $this -> ref ) || empty ( $this -> ref ))) // empty should not happened, but when it occurs, the test save life
2015-06-29 13:12:37 +02:00
{
2017-08-27 17:52:44 +02:00
$num = $this -> getNextNumRef ();
}
else
{
$num = $this -> ref ;
}
2018-02-08 14:25:44 +01:00
if ( empty ( $num ) || $num < 0 ) return - 1 ;
2017-08-29 19:44:35 +02:00
2017-08-27 17:52:44 +02:00
$this -> newref = $num ;
2017-08-29 19:44:35 +02:00
2017-08-27 17:52:44 +02:00
$this -> db -> begin ();
2016-04-29 12:26:41 +02:00
2017-08-27 17:52:44 +02:00
// Validate
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET ref = ' " . $num . " ', " ;
$sql .= " fk_statut = " . self :: STATUS_VALIDATED . " , " ;
2017-08-29 19:44:35 +02:00
$sql .= " date_valid=' " . $this -> db -> idate ( $this -> date_valid ) . " ', " ;
2017-08-27 17:52:44 +02:00
$sql .= " fk_user_valid = " . $user -> id ;
$sql .= " WHERE rowid = " . $this -> id ;
2017-07-08 17:57:35 +02:00
2017-08-27 17:52:44 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'EXPENSE_REPORT_VALIDATE' , $fuser );
2017-02-02 11:42:38 +01:00
2017-08-27 17:52:44 +02:00
if ( $result < 0 ) {
$error ++ ;
2017-02-02 11:42:38 +01:00
}
2017-08-27 17:52:44 +02:00
// End call triggers
}
2017-07-08 18:32:34 +02:00
2017-08-27 17:52:44 +02:00
if ( ! $error )
{
$this -> oldref = $this -> ref ;
// Rename directory if dir was a temporary ref
if ( preg_match ( '/^[\(]?PROV/i' , $this -> ref ))
{
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2017-08-29 19:44:35 +02:00
2017-08-27 17:52:44 +02:00
// On renomme repertoire ($this->ref = ancienne ref, $num = nouvelle ref)
// in order not to lose the attachments
$oldref = dol_sanitizeFileName ( $this -> ref );
$newref = dol_sanitizeFileName ( $num );
$dirsource = $conf -> expensereport -> dir_output . '/' . $oldref ;
$dirdest = $conf -> expensereport -> dir_output . '/' . $newref ;
if ( file_exists ( $dirsource ))
{
dol_syslog ( get_class ( $this ) . " ::valid() rename dir " . $dirsource . " into " . $dirdest );
2017-08-29 19:44:35 +02:00
2017-08-27 17:52:44 +02:00
if ( @ rename ( $dirsource , $dirdest ))
{
dol_syslog ( " Rename ok " );
// Rename docs starting with $oldref with $newref
$listoffiles = dol_dir_list ( $conf -> expensereport -> dir_output . '/' . $newref , 'files' , 1 , '^' . preg_quote ( $oldref , '/' ));
foreach ( $listoffiles as $fileentry )
{
$dirsource = $fileentry [ 'name' ];
$dirdest = preg_replace ( '/^' . preg_quote ( $oldref , '/' ) . '/' , $newref , $dirsource );
$dirsource = $fileentry [ 'path' ] . '/' . $dirsource ;
$dirdest = $fileentry [ 'path' ] . '/' . $dirdest ;
@ rename ( $dirsource , $dirdest );
}
}
}
2017-02-02 11:42:38 +01:00
}
2017-08-27 17:52:44 +02:00
}
// Set new ref and current status
if ( ! $error )
{
$this -> ref = $num ;
$this -> statut = self :: STATUS_VALIDATED ;
}
if ( empty ( $error ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
2017-02-02 11:42:38 +01:00
$this -> db -> rollback ();
2017-08-27 17:52:44 +02:00
$this -> error = $this -> db -> error ();
return - 2 ;
}
2015-06-29 13:12:37 +02:00
}
else
{
2017-08-27 17:52:44 +02:00
$this -> db -> rollback ();
$this -> error = $this -> db -> lasterror ();
return - 1 ;
2015-06-29 13:12:37 +02:00
}
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
* set_save_from_refuse
*
* @ param User $fuser User
* @ return int < 0 if KO , > 0 if OK
*/
function set_save_from_refuse ( $fuser )
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-06-29 13:12:37 +02:00
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
2017-02-02 11:42:38 +01:00
* @ param int $notrigger Disable triggers
2017-04-14 16:50:30 +02:00
* @ return int < 0 if KO , 0 if nothing done , > 0 if OK
2015-06-29 13:12:37 +02:00
*/
2017-02-02 11:42:38 +01:00
function setApproved ( $fuser , $notrigger = 0 )
2015-06-29 13:12:37 +02:00
{
$now = dol_now ();
2017-02-02 11:42:38 +01:00
$error = 0 ;
2015-06-29 13:12:37 +02:00
// date approval
2018-03-29 08:49:06 +02:00
$this -> date_approve = $now ;
2015-06-29 13:12:37 +02:00
if ( $this -> fk_statut != 5 )
{
2017-02-02 11:42:38 +01:00
$this -> db -> begin ();
2017-07-08 18:32:34 +02:00
2015-06-29 13:12:37 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2017-05-12 16:55:11 +02:00
$sql .= " SET ref = ' " . $this -> db -> escape ( $this -> ref ) . " ', fk_statut = 5, fk_user_approve = " . $fuser -> id . " , " ;
$sql .= " date_approve=' " . $this -> db -> idate ( $this -> date_approve ) . " ' " ;
2015-06-29 13:12:37 +02:00
$sql .= ' WHERE rowid = ' . $this -> id ;
if ( $this -> db -> query ( $sql ))
{
2017-02-02 11:42:38 +01:00
if ( ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'EXPENSE_REPORT_APPROVE' , $fuser );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
2017-07-08 18:32:34 +02:00
2017-02-02 11:42:38 +01:00
if ( empty ( $error ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 2 ;
}
2015-06-29 13:12:37 +02:00
}
else
{
2017-02-02 11:42:38 +01:00
$this -> db -> rollback ();
2015-06-29 13:12:37 +02:00
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
else
{
2017-04-14 16:50:30 +02:00
dol_syslog ( get_class ( $this ) . " ::setApproved expensereport already with approve status " , LOG_WARNING );
2015-06-29 13:12:37 +02:00
}
2017-07-08 18:32:34 +02:00
2017-04-14 16:50:30 +02:00
return 0 ;
2015-06-29 13:12:37 +02:00
}
/**
* setDeny
*
* @ param User $fuser User
* @ param Details $details Details
2017-02-02 11:42:38 +01:00
* @ param int $notrigger Disable triggers
2018-08-14 12:23:10 +02:00
* @ return int
2015-06-29 13:12:37 +02:00
*/
2017-02-02 11:42:38 +01:00
function setDeny ( $fuser , $details , $notrigger = 0 )
2015-06-29 13:12:37 +02:00
{
$now = dol_now ();
2017-02-02 11:42:38 +01:00
$error = 0 ;
2015-06-29 13:12:37 +02:00
// date de refus
if ( $this -> fk_statut != 99 )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2017-05-12 16:55:11 +02:00
$sql .= " SET ref = ' " . $this -> db -> escape ( $this -> ref ) . " ', fk_statut = 99, fk_user_refuse = " . $fuser -> id . " , " ;
2015-06-29 13:12:37 +02:00
$sql .= " date_refuse=' " . $this -> db -> idate ( $now ) . " ', " ;
2015-07-15 15:39:50 +02:00
$sql .= " detail_refuse=' " . $this -> db -> escape ( $details ) . " ', " ;
$sql .= " fk_user_approve = NULL " ;
2015-06-29 13:12:37 +02:00
$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 ;
2017-07-08 18:32:34 +02:00
2017-02-02 11:42:38 +01:00
if ( ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'EXPENSE_REPORT_DENY' , $fuser );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
2017-07-08 18:32:34 +02:00
2017-02-02 11:42:38 +01:00
if ( empty ( $error ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 2 ;
}
2015-06-29 13:12:37 +02:00
}
else
{
2017-02-02 11:42:38 +01:00
$this -> db -> rollback ();
2015-06-29 13:12:37 +02:00
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
else
{
dol_syslog ( get_class ( $this ) . " ::setDeny expensereport already with refuse status " , LOG_WARNING );
}
}
2015-02-14 18:39:45 +01:00
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-18 06:12:05 +02:00
/**
2015-06-29 13:12:37 +02:00
* set_unpaid
2015-06-18 06:12:05 +02:00
*
2015-06-29 13:12:37 +02:00
* @ param User $fuser User
2017-02-02 11:42:38 +01:00
* @ param int $notrigger Disable triggers
2015-06-29 13:12:37 +02:00
* @ return int < 0 if KO , > 0 if OK
2015-06-18 06:12:05 +02:00
*/
2017-02-02 11:42:38 +01:00
function set_unpaid ( $fuser , $notrigger = 0 )
2015-06-18 06:12:05 +02:00
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2017-02-02 11:42:38 +01:00
$error = 0 ;
2017-07-08 18:32:34 +02:00
2015-06-29 13:12:37 +02:00
if ( $this -> fk_c_deplacement_statuts != 5 )
{
2017-02-02 11:42:38 +01:00
$this -> db -> begin ();
2017-07-08 18:32:34 +02:00
2015-06-29 13:12:37 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET fk_statut = 5 " ;
$sql .= ' WHERE rowid = ' . $this -> id ;
2015-06-18 06:12:05 +02:00
2015-06-29 13:12:37 +02:00
dol_syslog ( get_class ( $this ) . " ::set_unpaid sql= " . $sql , LOG_DEBUG );
2017-02-02 11:42:38 +01:00
if ( $this -> db -> query ( $sql ))
{
if ( ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'EXPENSE_REPORT_UNPAID' , $fuser );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
2017-07-08 18:32:34 +02:00
2017-02-02 11:42:38 +01:00
if ( empty ( $error ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 2 ;
}
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 1 ;
}
2015-06-29 13:12:37 +02:00
}
else
2015-06-18 06:12:05 +02:00
{
2015-06-29 13:12:37 +02:00
dol_syslog ( get_class ( $this ) . " ::set_unpaid expensereport already with unpaid status " , LOG_WARNING );
}
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
* set_cancel
*
* @ param User $fuser User
* @ param string $detail Detail
2017-02-02 11:42:38 +01:00
* @ param int $notrigger Disable triggers
2015-06-29 13:12:37 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2017-02-02 11:42:38 +01:00
function set_cancel ( $fuser , $detail , $notrigger = 0 )
2015-06-29 13:12:37 +02:00
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2017-02-02 11:42:38 +01:00
$error = 0 ;
2015-06-29 13:12:37 +02:00
$this -> date_cancel = $this -> db -> idate ( gmmktime ());
if ( $this -> fk_statut != 4 )
{
2017-02-02 11:42:38 +01:00
$this -> db -> begin ();
2017-07-08 18:32:34 +02:00
2015-06-29 13:12:37 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET fk_statut = 4, fk_user_cancel = " . $fuser -> id ;
2017-05-12 16:55:11 +02:00
$sql .= " , date_cancel=' " . $this -> db -> idate ( $this -> date_cancel ) . " ' " ;
2015-06-29 13:12:37 +02:00
$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 ))
{
2017-02-02 11:42:38 +01:00
if ( ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'EXPENSE_REPORT_CANCEL' , $fuser );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
2017-07-08 18:32:34 +02:00
2017-02-02 11:42:38 +01:00
if ( empty ( $error ))
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 2 ;
}
2015-06-29 13:12:37 +02:00
}
else
{
2017-02-02 11:42:38 +01:00
$this -> db -> rollback ();
2015-06-29 13:12:37 +02:00
$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 ()
{
2017-08-27 10:48:58 +02:00
global $langs , $conf ;
$langs -> load ( " trips " );
2015-06-29 13:12:37 +02:00
2017-08-27 10:48:58 +02:00
if ( ! empty ( $conf -> global -> EXPENSEREPORT_ADDON ))
{
$mybool = false ;
2015-06-29 13:12:37 +02:00
2017-08-27 10:48:58 +02:00
$file = $conf -> global -> EXPENSEREPORT_ADDON . " .php " ;
$classname = $conf -> global -> EXPENSEREPORT_ADDON ;
2017-07-08 17:57:35 +02:00
2017-08-27 10:48:58 +02:00
// Include file with class
2018-10-27 21:21:59 +02:00
$dirmodels = array_merge ( array ( '/' ), ( array ) $conf -> modules_parts [ 'models' ]);
2017-08-27 10:48:58 +02:00
foreach ( $dirmodels as $reldir )
{
$dir = dol_buildpath ( $reldir . " core/modules/expensereport/ " );
2015-06-29 13:12:37 +02:00
2017-08-27 10:48:58 +02:00
// Load file with numbering class (if found)
$mybool |=@ include_once $dir . $file ;
}
2018-10-27 21:21:59 +02:00
if ( $mybool === false ) {
dol_print_error ( '' , " Failed to include file " . $file );
2017-08-27 10:48:58 +02:00
return '' ;
}
$obj = new $classname ();
$numref = $obj -> getNextValue ( $this );
if ( $numref != " " )
{
return $numref ;
}
else
{
$this -> error = $obj -> error ;
2017-08-29 19:44:35 +02:00
$this -> errors = $obj -> errors ;
2017-08-27 10:48:58 +02:00
//dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
2017-11-17 13:09:03 +01:00
return - 1 ;
2017-08-27 10:48:58 +02:00
}
}
else
{
2017-11-17 13:09:03 +01:00
$this -> error = " Error_EXPENSEREPORT_ADDON_NotDefined " ;
return - 2 ;
2017-08-27 10:48:58 +02:00
}
2015-06-29 13:12:37 +02:00
}
/**
* Return clicable name ( with picto eventually )
*
2017-10-03 16:00:52 +02:00
* @ param int $withpicto 0 = No picto , 1 = Include picto into link , 2 = Only picto
* @ param int $max Max length of shown ref
* @ param int $short 1 = Return just URL
* @ param string $moretitle Add more text to title tooltip
* @ param int $notooltip 1 = Disable tooltip
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
* @ return string String with URL
2015-06-29 13:12:37 +02:00
*/
2017-10-03 16:00:52 +02:00
function getNomUrl ( $withpicto = 0 , $max = 0 , $short = 0 , $moretitle = '' , $notooltip = 0 , $save_lastsearch_value =- 1 )
2015-06-29 13:12:37 +02:00
{
2016-11-12 10:41:57 +01:00
global $langs , $conf ;
2015-06-29 13:12:37 +02:00
$result = '' ;
2016-11-12 10:41:57 +01:00
$url = DOL_URL_ROOT . '/expensereport/card.php?id=' . $this -> id ;
if ( $short ) return $url ;
2015-06-29 13:12:37 +02:00
2016-11-12 10:41:57 +01:00
$label = '<u>' . $langs -> trans ( " ShowExpenseReport " ) . '</u>' ;
if ( ! empty ( $this -> ref ))
$label .= '<br><b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . $this -> ref ;
if ( ! empty ( $this -> total_ht ))
$label .= '<br><b>' . $langs -> trans ( 'AmountHT' ) . ':</b> ' . price ( $this -> total_ht , 0 , $langs , 0 , - 1 , - 1 , $conf -> currency );
if ( ! empty ( $this -> total_tva ))
$label .= '<br><b>' . $langs -> trans ( 'VAT' ) . ':</b> ' . price ( $this -> total_tva , 0 , $langs , 0 , - 1 , - 1 , $conf -> currency );
if ( ! empty ( $this -> total_ttc ))
$label .= '<br><b>' . $langs -> trans ( 'AmountTTC' ) . ':</b> ' . price ( $this -> total_ttc , 0 , $langs , 0 , - 1 , - 1 , $conf -> currency );
if ( $moretitle ) $label .= ' - ' . $moretitle ;
2017-10-03 16:00:52 +02:00
//if ($option != 'nolink')
//{
// Add param to save lastsearch_values or not
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) $add_save_lastsearch_values = 1 ;
if ( $add_save_lastsearch_values ) $url .= '&save_lastsearch_values=1' ;
//}
2016-11-12 10:41:57 +01:00
$ref = $this -> ref ;
if ( empty ( $ref )) $ref = $this -> id ;
$linkclose = '' ;
if ( empty ( $notooltip ))
{
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER ))
{
$label = $langs -> trans ( " ShowExpenseReport " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
}
$linkclose .= ' title="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
$linkclose .= ' class="classfortooltip"' ;
}
2015-06-29 13:12:37 +02:00
2016-11-12 10:41:57 +01:00
$linkstart = '<a href="' . $url . '"' ;
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2015-06-29 13:12:37 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
if ( $withpicto ) $result .= img_object (( $notooltip ? '' : $label ), $this -> picto , ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 );
if ( $withpicto != 2 ) $result .= ( $max ? dol_trunc ( $ref , $max ) : $ref );
$result .= $linkend ;
2015-06-29 13:12:37 +02:00
return $result ;
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
* 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 )
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-06-29 13:12:37 +02:00
$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 ;
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
* 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 )
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-06-29 13:12:37 +02:00
$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 ;
}
2017-06-15 11:08:53 +02:00
/**
* addline
2017-08-22 10:11:32 +02:00
*
2017-06-15 11:08:53 +02:00
* @ param real $qty Qty
* @ param double $up Value init
* @ param int $fk_c_type_fees Type payment
* @ param double $vatrate Vat rate
* @ param string $date Date
* @ param string $comments Description
* @ param int $fk_project Project id
* @ param int $fk_c_exp_tax_cat Car category id
* @ param int $type Type line
* @ return int < 0 if KO , > 0 if OK
*/
function addline ( $qty = 0 , $up = 0 , $fk_c_type_fees = 0 , $vatrate = 0 , $date = '' , $comments = '' , $fk_project = 0 , $fk_c_exp_tax_cat = 0 , $type = 0 )
{
2018-12-04 14:54:58 +01:00
global $conf , $langs , $mysoc ;
2017-06-15 11:08:53 +02:00
dol_syslog ( get_class ( $this ) . " ::addline qty= $qty , up= $up , fk_c_type_fees= $fk_c_type_fees , vatrate= $vatrate , date= $date , fk_project= $fk_project , type= $type , comments= $comments " , LOG_DEBUG );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
if ( $this -> fk_statut == self :: STATUS_DRAFT )
2018-11-27 11:36:31 +01:00
{
2018-12-03 12:28:17 +01:00
if ( empty ( $qty )) $qty = 0 ;
if ( empty ( $fk_c_type_fees ) || $fk_c_type_fees < 0 ) $fk_c_type_fees = 0 ;
if ( empty ( $fk_c_exp_tax_cat ) || $fk_c_exp_tax_cat < 0 ) $fk_c_exp_tax_cat = 0 ;
if ( empty ( $vatrate ) || $vatrate < 0 ) $vatrate = 0 ;
if ( empty ( $date )) $date = '' ;
if ( empty ( $fk_project )) $fk_project = 0 ;
$qty = price2num ( $qty );
if ( ! preg_match ( '/\((.*)\)/' , $vatrate )) {
$vatrate = price2num ( $vatrate ); // $txtva can have format '5.0(XXX)' or '5'
}
$up = price2num ( $up );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$this -> db -> begin ();
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$this -> line = new ExpenseReportLine ( $this -> db );
2018-12-04 14:54:58 +01:00
$localtaxes_type = getLocalTaxesFromRate ( $vatrate , 0 , $mysoc , $this -> thirdparty );
$vat_src_code = '' ;
2018-04-10 14:38:41 +02:00
if ( preg_match ( '/\((.*)\)/' , $vatrate , $reg ))
{
$vat_src_code = $reg [ 1 ];
$vatrate = preg_replace ( '/\s*\(.*\)/' , '' , $vatrate ); // Remove code into vatrate.
}
$vatrate = preg_replace ( '/\*/' , '' , $vatrate );
2017-06-15 11:08:53 +02:00
$seller = '' ; // seller is unknown
2018-12-04 14:54:58 +01:00
$tmp = calcul_price_total ( $qty , $up , 0 , $vatrate , 0 , 0 , 0 , 'TTC' , 0 , $type , $seller , $localtaxes_type );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$this -> line -> value_unit = $up ;
$this -> line -> vatrate = price2num ( $vatrate );
$this -> line -> total_ttc = $tmp [ 2 ];
$this -> line -> total_ht = $tmp [ 0 ];
$this -> line -> total_tva = $tmp [ 1 ];
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$this -> line -> fk_expensereport = $this -> id ;
$this -> line -> qty = $qty ;
$this -> line -> date = $date ;
$this -> line -> fk_c_type_fees = $fk_c_type_fees ;
$this -> line -> fk_c_exp_tax_cat = $fk_c_exp_tax_cat ;
$this -> line -> comments = $comments ;
$this -> line -> fk_projet = $fk_project ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$this -> applyOffset ();
$this -> checkRules ( $type , $seller );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$result = $this -> line -> insert ( 0 , true );
if ( $result > 0 )
{
$result = $this -> update_price (); // This method is designed to add line from user input so total calculation must be done using 'auto' mode.
if ( $result > 0 )
{
$this -> db -> commit ();
return $this -> line -> rowid ;
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
}
else
{
$this -> error = $this -> line -> error ;
dol_syslog ( get_class ( $this ) . " ::addline error= " . $this -> error , LOG_ERR );
$this -> db -> rollback ();
return - 2 ;
}
}
else
{
dol_syslog ( get_class ( $this ) . " ::addline status of expense report must be Draft to allow use of ->addline() " , LOG_ERR );
$this -> error = 'ErrorExpenseNotDraft' ;
return - 3 ;
}
}
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
/**
* Check constraint of rules and update price if needed
2017-08-22 10:11:32 +02:00
*
* @ param int $type type of line
2017-06-15 11:08:53 +02:00
* @ param string $seller seller , but actually he is unknown
* @ return true or false
*/
function checkRules ( $type = 0 , $seller = '' )
{
global $user , $conf , $db , $langs ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$langs -> load ( 'trips' );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
if ( empty ( $conf -> global -> MAIN_USE_EXPENSE_RULE )) return true ; // if don't use rules
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$rulestocheck = ExpenseReportRule :: getAllRule ( $this -> line -> fk_c_type_fees , $this -> line -> date , $this -> fk_user_author );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$violation = 0 ;
$rule_warning_message_tab = array ();
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$current_total_ttc = $this -> line -> total_ttc ;
$new_current_total_ttc = $this -> line -> total_ttc ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
// check if one is violated
foreach ( $rulestocheck as $rule )
{
if ( in_array ( $rule -> code_expense_rules_type , array ( 'EX_DAY' , 'EX_MON' , 'EX_YEA' ))) $amount_to_test = $this -> line -> getExpAmount ( $rule , $this -> fk_user_author , $rule -> code_expense_rules_type );
else $amount_to_test = $current_total_ttc ; // EX_EXP
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$amount_to_test = $amount_to_test - $current_total_ttc + $new_current_total_ttc ; // if amount as been modified by a previous rule
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
if ( $amount_to_test > $rule -> amount )
{
$violation ++ ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
if ( $rule -> restrictive )
{
$this -> error = 'ExpenseReportConstraintViolationError' ;
$this -> errors [] = $this -> error ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$new_current_total_ttc -= $amount_to_test - $rule -> amount ; // ex, entered 16€, limit 12€, subtracts 4€;
$rule_warning_message_tab [] = $langs -> trans ( 'ExpenseReportConstraintViolationError' , $rule -> id , price ( $amount_to_test , 0 , $langs , 1 , - 1 , - 1 , $conf -> currency ), price ( $rule -> amount , 0 , $langs , 1 , - 1 , - 1 , $conf -> currency ), $langs -> trans ( 'by' . $rule -> code_expense_rules_type , price ( $new_current_total_ttc , 0 , $langs , 1 , - 1 , - 1 , $conf -> currency )));
}
else
{
$this -> error = 'ExpenseReportConstraintViolationWarning' ;
$this -> errors [] = $this -> error ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$rule_warning_message_tab [] = $langs -> trans ( 'ExpenseReportConstraintViolationWarning' , $rule -> id , price ( $amount_to_test , 0 , $langs , 1 , - 1 , - 1 , $conf -> currency ), price ( $rule -> amount , 0 , $langs , 1 , - 1 , - 1 , $conf -> currency ), $langs -> trans ( 'nolimitby' . $rule -> code_expense_rules_type ));
}
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
// No break, we sould test if another rule is violated
}
}
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$this -> line -> rule_warning_message = implode ( '\n' , $rule_warning_message_tab );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
if ( $violation > 0 )
{
$tmp = calcul_price_total ( $this -> line -> qty , $new_current_total_ttc / $this -> line -> qty , 0 , $this -> line -> vatrate , 0 , 0 , 0 , 'TTC' , 0 , $type , $seller );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$this -> line -> value_unit = $tmp [ 5 ];
$this -> line -> total_ttc = $tmp [ 2 ];
$this -> line -> total_ht = $tmp [ 0 ];
$this -> line -> total_tva = $tmp [ 1 ];
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
return false ;
}
else return true ;
}
/**
* Method to apply the offset if needed
2017-08-22 10:11:32 +02:00
*
2017-06-15 11:08:53 +02:00
* @ return boolean true = applied , false = not applied
*/
function applyOffset ()
{
global $conf ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
if ( empty ( $conf -> global -> MAIN_USE_EXPENSE_IK )) return false ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$userauthor = new User ( $this -> db );
if ( $userauthor -> fetch ( $this -> fk_user_author ) <= 0 )
{
$this -> error = 'ErrorCantFetchUser' ;
$this -> errors [] = 'ErrorCantFetchUser' ;
return false ;
}
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$range = ExpenseReportIk :: getRangeByUser ( $userauthor , $this -> line -> fk_c_exp_tax_cat );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
if ( empty ( $range ))
{
$this -> error = 'ErrorNoRangeAvailable' ;
$this -> errors [] = 'ErrorNoRangeAvailable' ;
return false ;
}
2017-08-22 10:11:32 +02:00
2017-10-26 18:30:01 +02:00
if ( ! empty ( $conf -> global -> MAIN_EXPENSE_APPLY_ENTIRE_OFFSET )) $ikoffset = $range -> ikoffset ;
else $ikoffset = $range -> ikoffset / 12 ; // The amount of offset is a global value for the year
2017-08-22 10:11:32 +02:00
2017-10-26 18:30:01 +02:00
// Test if ikoffset has been applied for the current month
2017-06-15 11:08:53 +02:00
if ( ! $this -> offsetAlreadyGiven ())
{
2017-10-26 18:30:01 +02:00
$new_up = $range -> coef + ( $ikoffset / $this -> line -> qty );
2017-06-15 11:08:53 +02:00
$tmp = calcul_price_total ( $this -> line -> qty , $new_up , 0 , $this -> line -> vatrate , 0 , 0 , 0 , 'TTC' , 0 , $type , $seller );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$this -> line -> value_unit = $tmp [ 5 ];
$this -> line -> total_ttc = $tmp [ 2 ];
$this -> line -> total_ht = $tmp [ 0 ];
$this -> line -> total_tva = $tmp [ 1 ];
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
return true ;
}
2015-06-29 13:12:37 +02:00
2017-06-15 11:08:53 +02:00
return false ;
}
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
/**
2017-10-26 18:30:01 +02:00
* If the sql find any rows then the ikoffset is already given ( ikoffset is applied at the first expense report line )
2017-08-22 10:11:32 +02:00
*
2017-06-15 11:08:53 +02:00
* @ return bool
*/
function offsetAlreadyGiven ()
{
$sql = 'SELECT e.rowid FROM ' . MAIN_DB_PREFIX . 'expensereport e' ;
$sql .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'expensereport_det d ON (e.rowid = d.fk_expensereport)' ;
$sql .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'c_type_fees f ON (d.fk_c_type_fees = f.id AND f.code = "EX_KME")' ;
$sql .= ' WHERE e.fk_user_author = ' . ( int ) $this -> fk_user_author ;
$sql .= ' AND YEAR(d.date) = "' . dol_print_date ( $this -> line -> date , '%Y' ) . '" AND MONTH(d.date) = "' . dol_print_date ( $this -> line -> date , '%m' ) . '"' ;
if ( ! empty ( $this -> line -> id )) $sql .= ' AND d.rowid <> ' . $this -> line -> id ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
dol_syslog ( get_class ( $this ) . " ::offsetAlreadyGiven sql= " . $sql );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
if ( $num > 0 ) return true ;
}
else
{
dol_print_error ( $this -> db );
}
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
return false ;
}
2015-06-29 13:12:37 +02:00
/**
* updateline
*
* @ param int $rowid Line to edit
* @ param int $type_fees_id Type payment
* @ param int $projet_id Project id
2017-07-08 17:57:35 +02:00
* @ param double $vatrate Vat rate . Can be '8.5* (8.5NPROM...)'
2015-06-29 13:12:37 +02:00
* @ 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
2017-06-15 11:08:53 +02:00
* @ param int $fk_c_exp_tax_cat id of category of car
2015-06-29 13:12:37 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2017-06-15 11:08:53 +02:00
function updateline ( $rowid , $type_fees_id , $projet_id , $vatrate , $comments , $qty , $value_unit , $date , $expensereport_id , $fk_c_exp_tax_cat = 0 )
2015-06-29 13:12:37 +02:00
{
2017-07-08 17:57:35 +02:00
global $user , $mysoc ;
2015-06-29 13:12:37 +02:00
if ( $this -> fk_statut == 0 || $this -> fk_statut == 99 )
{
$this -> db -> begin ();
2017-07-08 17:57:35 +02:00
$type = 0 ; // TODO What if type is service ?
// We don't know seller and buyer for expense reports
$seller = $mysoc ;
$buyer = new Societe ( $this -> db );
$localtaxes_type = getLocalTaxesFromRate ( $vatrate , 0 , $buyer , $seller );
// Clean vat code
$vat_src_code = '' ;
if ( preg_match ( '/\((.*)\)/' , $vatrate , $reg ))
{
$vat_src_code = $reg [ 1 ];
$vatrate = preg_replace ( '/\s*\(.*\)/' , '' , $vatrate ); // Remove code into vatrate.
}
$vatrate = preg_replace ( '/\*/' , '' , $vatrate );
$tmp = calcul_price_total ( $qty , $value_unit , 0 , $vatrate , 0 , 0 , 0 , 'TTC' , 0 , $type , $seller , $localtaxes_type );
2017-12-01 15:39:18 +01:00
// calcul total of line
2017-07-08 17:57:35 +02:00
//$total_ttc = price2num($qty*$value_unit, 'MT');
2015-06-29 13:12:37 +02:00
$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
2017-06-15 11:08:53 +02:00
$this -> line = new ExpenseReportLine ( $this -> db );
$this -> line -> comments = $comments ;
$this -> line -> qty = $qty ;
$this -> line -> value_unit = $value_unit ;
$this -> line -> date = $date ;
2015-06-29 13:12:37 +02:00
2017-06-15 11:08:53 +02:00
$this -> line -> fk_expensereport = $expensereport_id ;
$this -> line -> fk_c_type_fees = $type_fees_id ;
$this -> line -> fk_c_exp_tax_cat = $fk_c_exp_tax_cat ;
$this -> line -> fk_projet = $projet_id ;
2015-06-29 13:12:37 +02:00
2017-07-27 16:01:53 +02:00
$this -> line -> vat_src_code = $vat_src_code ;
$this -> line -> vatrate = price2num ( $vatrate );
$this -> line -> total_ttc = $tmp [ 2 ];
$this -> line -> total_ht = $tmp [ 0 ];
$this -> line -> total_tva = $tmp [ 1 ];
$this -> line -> localtax1_tx = $localtaxes_type [ 1 ];
$this -> line -> localtax2_tx = $localtaxes_type [ 3 ];
$this -> line -> localtax1_type = $localtaxes_type [ 0 ];
$this -> line -> localtax2_type = $localtaxes_type [ 2 ];
2017-06-15 11:08:53 +02:00
$this -> line -> rowid = $rowid ;
$this -> line -> id = $rowid ;
2015-06-29 13:12:37 +02:00
// 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 );
2017-06-15 11:08:53 +02:00
$this -> line -> type_fees_code = $objp_fees -> code_type_fees ;
$this -> line -> type_fees_libelle = $objp_fees -> libelle_type_fees ;
2015-06-29 13:12:37 +02:00
// 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 );
2016-04-28 21:56:28 +02:00
if ( $result ) {
$objp_projet = $this -> db -> fetch_object ( $result );
}
2017-06-15 11:08:53 +02:00
$this -> line -> projet_ref = $objp_projet -> ref_projet ;
$this -> line -> projet_title = $objp_projet -> title_projet ;
2015-06-29 13:12:37 +02:00
2017-06-15 11:08:53 +02:00
$this -> applyOffset ();
$this -> checkRules ();
2015-06-29 13:12:37 +02:00
2017-06-15 11:08:53 +02:00
$result = $this -> line -> update ( $user );
2015-06-29 13:12:37 +02:00
if ( $result > 0 )
2015-06-18 06:12:05 +02:00
{
2015-06-29 13:12:37 +02:00
$this -> db -> commit ();
2015-06-18 06:12:05 +02:00
return 1 ;
}
else
2015-06-29 13:12:37 +02:00
{
2017-06-15 11:08:53 +02:00
$this -> error = $this -> line -> error ;
$this -> errors = $this -> line -> errors ;
2015-06-29 13:12:37 +02:00
$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 ;
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
* 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 )
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-06-29 13:12:37 +02:00
$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
2015-06-18 06:12:05 +02:00
{
return 0 ;
}
}
else
{
2015-06-29 13:12:37 +02:00
$this -> error = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . " ::periode_existe Error " . $this -> error , LOG_ERR );
return - 1 ;
}
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-06-29 13:12:37 +02:00
/**
2016-12-03 18:13:23 +01:00
* Return list of people with permission to validate expense reports .
* Search for permission " approve expense report "
2015-06-29 13:12:37 +02:00
*
* @ return array Array of user ids
*/
function fetch_users_approver_expensereport ()
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-06-29 13:12:37 +02:00
$users_validator = array ();
2016-12-03 18:13:23 +01:00
$sql = " SELECT DISTINCT ur.fk_user " ;
2015-06-29 13:12:37 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user_rights as ur, " . MAIN_DB_PREFIX . " rights_def as rd " ;
2016-12-03 18:13:23 +01:00
$sql .= " WHERE ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve' " ; // Permission 'Approve';
$sql .= " UNION " ;
$sql .= " SELECT DISTINCT ugu.fk_user " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " usergroup_user as ugu, " . MAIN_DB_PREFIX . " usergroup_rights as ur, " . MAIN_DB_PREFIX . " rights_def as rd " ;
$sql .= " WHERE ugu.fk_usergroup = ur.fk_usergroup AND ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve' " ; // Permission 'Approve';
//print $sql;
2017-07-08 17:57:35 +02:00
2015-06-29 13:12:37 +02:00
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 );
2015-06-18 06:12:05 +02:00
return - 1 ;
}
}
2015-02-14 18:39:45 +01:00
2015-06-29 13:12:37 +02:00
/**
* 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
2018-06-15 09:47:28 +02:00
* @ param null | array $moreparams Array to provide more information
2015-06-29 13:12:37 +02:00
* @ return int 0 if KO , 1 if OK
*/
2018-06-15 09:47:28 +02:00
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 , $moreparams = null )
2015-06-29 13:12:37 +02:00
{
global $conf , $langs ;
$langs -> load ( " trips " );
2017-01-16 21:16:05 +01:00
if ( ! dol_strlen ( $modele )) {
$modele = 'standard' ;
if ( $this -> modelpdf ) {
$modele = $this -> modelpdf ;
} elseif ( ! empty ( $conf -> global -> EXPENSEREPORT_ADDON_PDF )) {
$modele = $conf -> global -> EXPENSEREPORT_ADDON_PDF ;
}
}
2015-06-29 13:12:37 +02:00
$modelpath = " core/modules/expensereport/doc/ " ;
2018-06-15 09:47:28 +02:00
return $this -> commonGenerateDocument ( $modelpath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref , $moreparams );
2015-06-29 13:12:37 +02:00
}
/**
* 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 ;
}
2015-02-22 13:16:49 +01:00
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
/**
2015-08-17 05:29:12 +02:00
* Charge indicateurs this -> nb pour le tableau de bord
*
* @ return int < 0 if KO , > 0 if OK
*/
function load_state_board ()
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-08-17 05:29:12 +02:00
global $conf ;
$this -> nb = array ();
$sql = " SELECT count(ex.rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " expensereport as ex " ;
$sql .= " WHERE ex.fk_statut > 0 " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND ex.entity IN ( " . getEntity ( 'expensereport' ) . " ) " ;
2015-08-17 05:29:12 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$this -> nb [ " expensereports " ] = $obj -> nb ;
}
$this -> db -> free ( $resql );
return 1 ;
}
else
{
dol_print_error ( $this -> db );
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-08-17 05:29:12 +02:00
/**
* Load indicators for dashboard ( this -> nbtodo and this -> nbtodolate )
*
* @ param User $user Objet user
2016-01-18 11:35:45 +01:00
* @ param string $option 'topay' or 'toapprove'
2015-08-17 05:29:12 +02:00
* @ return WorkboardResponse | int < 0 if KO , WorkboardResponse if OK
*/
2016-01-18 11:35:45 +01:00
function load_board ( $user , $option = 'topay' )
2015-08-17 05:29:12 +02:00
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-08-17 05:29:12 +02:00
global $conf , $langs ;
if ( $user -> societe_id ) return - 1 ; // protection pour eviter appel par utilisateur externe
$now = dol_now ();
2017-01-26 18:08:00 +01:00
$userchildids = $user -> getAllChildIds ( 1 );
2017-07-08 17:57:35 +02:00
2015-08-17 05:29:12 +02:00
$sql = " SELECT ex.rowid, ex.date_valid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " expensereport as ex " ;
2016-01-18 11:35:45 +01:00
if ( $option == 'toapprove' ) $sql .= " WHERE ex.fk_statut = 2 " ;
else $sql .= " WHERE ex.fk_statut = 5 " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND ex.entity IN ( " . getEntity ( 'expensereport' ) . " ) " ;
2017-03-01 14:37:59 +01:00
$sql .= " AND (ex.fk_user_author IN ( " . join ( ',' , $userchildids ) . " ) " ;
$sql .= " OR ex.fk_user_validator IN ( " . join ( ',' , $userchildids ) . " )) " ;
2015-08-17 05:29:12 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$langs -> load ( " members " );
$response = new WorkboardResponse ();
2016-01-18 11:35:45 +01:00
if ( $option == 'toapprove' )
{
$response -> warning_delay = $conf -> expensereport -> approve -> warning_delay / 60 / 60 / 24 ;
$response -> label = $langs -> trans ( " ExpenseReportsToApprove " );
$response -> url = DOL_URL_ROOT . '/expensereport/list.php?mainmenu=hrm&statut=2' ;
}
else
{
$response -> warning_delay = $conf -> expensereport -> payment -> warning_delay / 60 / 60 / 24 ;
$response -> label = $langs -> trans ( " ExpenseReportsToPay " );
$response -> url = DOL_URL_ROOT . '/expensereport/list.php?mainmenu=hrm&statut=5' ;
}
2017-03-28 10:44:52 +02:00
$response -> img = img_object ( '' , " trip " );
2015-08-17 05:29:12 +02:00
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$response -> nbtodo ++ ;
2017-07-08 17:57:35 +02:00
2016-01-18 11:35:45 +01:00
if ( $option == 'toapprove' )
{
2016-12-22 15:49:06 +01:00
if ( $this -> db -> jdate ( $obj -> date_valid ) < ( $now - $conf -> expensereport -> approve -> warning_delay )) {
2016-01-18 11:35:45 +01:00
$response -> nbtodolate ++ ;
}
}
else
{
2016-12-22 15:49:06 +01:00
if ( $this -> db -> jdate ( $obj -> date_valid ) < ( $now - $conf -> expensereport -> payment -> warning_delay )) {
2016-01-18 11:35:45 +01:00
$response -> nbtodolate ++ ;
}
}
2015-08-17 05:29:12 +02:00
}
return $response ;
}
else
{
dol_print_error ( $this -> db );
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2017-07-08 17:57:35 +02:00
2016-06-06 16:31:50 +02:00
/**
* Return if an expense report is late or not
*
* @ param string $option 'topay' or 'toapprove'
* @ return boolean True if late , False if not late
*/
public function hasDelay ( $option )
{
global $conf ;
2017-07-08 17:57:35 +02:00
2016-06-06 16:31:50 +02:00
//Only valid members
if ( $option == 'toapprove' && $this -> status != 2 ) return false ;
if ( $option == 'topay' && $this -> status != 5 ) return false ;
2017-07-08 17:57:35 +02:00
2016-06-06 16:31:50 +02:00
$now = dol_now ();
if ( $option == 'toapprove' )
2016-12-22 15:49:06 +01:00
{
return ( $this -> datevalid ? $this -> datevalid : $this -> date_valid ) < ( $now - $conf -> expensereport -> approve -> warning_delay );
}
2016-06-06 16:31:50 +02:00
else
2016-12-22 15:49:06 +01:00
return ( $this -> datevalid ? $this -> datevalid : $this -> date_valid ) < ( $now - $conf -> expensereport -> payment -> warning_delay );
2017-07-08 17:57:35 +02:00
}
2018-01-22 03:07:11 +01:00
/**
* Return if an expensereport was dispatched into bookkeeping
*
* @ return int < 0 if KO , 0 = no , 1 = yes
*/
public function getVentilExportCompta ()
{
$alreadydispatched = 0 ;
$type = 'expense_report' ;
$sql = " SELECT COUNT(ab.rowid) as nb FROM " . MAIN_DB_PREFIX . " accounting_bookkeeping as ab WHERE ab.doc_type=' " . $type . " ' AND ab.fk_doc = " . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj )
{
$alreadydispatched = $obj -> nb ;
}
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
if ( $alreadydispatched )
{
return 1 ;
}
return 0 ;
}
2018-12-04 23:16:41 +01:00
/**
* Return amount of payments already done
2018-12-05 11:10:42 +01:00
*
2018-12-04 23:16:41 +01:00
* @ return int Amount of payment already done , < 0 if KO
*/
public function getSumPayments ()
{
$table = 'payment_expensereport' ;
$field = 'fk_expensereport' ;
$sql = 'SELECT sum(amount) as amount' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $table ;
$sql .= ' WHERE ' . $field . ' = ' . $this -> id ;
dol_syslog ( get_class ( $this ) . " ::getSumPayments " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$this -> db -> free ( $resql );
return $obj -> amount ;
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2015-02-14 18:39:45 +01:00
}
/**
* Class of expense report details lines
*/
2015-02-15 22:49:23 +01:00
class ExpenseReportLine
2015-02-14 18:39:45 +01:00
{
2018-08-22 11:11:59 +02:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2018-08-29 15:37:35 +02:00
2018-08-22 10:37:16 +02:00
/**
* @ var string Error code ( or message )
*/
public $error = '' ;
2015-06-29 13:12:37 +02:00
2018-10-09 08:56:58 +02:00
/**
* @ var int ID
*/
public $rowid ;
public $comments ;
public $qty ;
public $value_unit ;
public $date ;
2018-10-10 11:30:33 +02:00
/**
* @ var int ID
*/
2018-10-09 08:56:58 +02:00
public $fk_c_type_fees ;
2018-10-10 11:30:33 +02:00
/**
* @ var int ID
*/
2018-10-09 08:56:58 +02:00
public $fk_c_exp_tax_cat ;
2018-10-10 11:30:33 +02:00
/**
* @ var int ID
*/
2018-10-09 08:56:58 +02:00
public $fk_projet ;
2018-10-10 11:30:33 +02:00
/**
* @ var int ID
*/
2018-10-09 08:56:58 +02:00
public $fk_expensereport ;
public $type_fees_code ;
public $type_fees_libelle ;
public $projet_ref ;
public $projet_title ;
2015-06-29 13:12:37 +02:00
var $vatrate ;
var $total_ht ;
var $total_tva ;
var $total_ttc ;
/**
* Constructor
*
* @ param DoliDB $db Handlet database
*/
2015-12-10 16:37:37 +01:00
function __construct ( $db )
2015-06-29 13:12:37 +02:00
{
$this -> db = $db ;
}
/**
2015-08-28 18:20:51 +02:00
* Fetch record for expense report detailed line
2015-06-29 13:12:37 +02:00
*
* @ param int $rowid Id of object to load
* @ return int < 0 if KO , > 0 if OK
*/
function fetch ( $rowid )
{
2017-06-15 11:08:53 +02:00
$sql = 'SELECT fde.rowid, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_c_exp_tax_cat, fde.fk_projet, fde.date,' ;
2017-07-08 18:50:58 +02:00
$sql .= ' fde.tva_tx as vatrate, fde.vat_src_code, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc,' ;
2015-06-29 13:12:37 +02:00
$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' ;
2018-06-25 16:25:37 +02:00
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id' ; // Sometimes type of expense report has been removed, so we use a left join here.
2015-06-29 13:12:37 +02:00
$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 )
{
$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 ;
2017-06-15 11:08:53 +02:00
$this -> dates = $this -> db -> jdate ( $objp -> date );
2015-06-29 13:12:37 +02:00
$this -> value_unit = $objp -> value_unit ;
$this -> fk_c_type_fees = $objp -> fk_c_type_fees ;
2017-06-15 11:08:53 +02:00
$this -> fk_c_exp_tax_cat = $objp -> fk_c_exp_tax_cat ;
2015-06-29 13:12:37 +02:00
$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 ;
2017-07-08 18:50:58 +02:00
$this -> vat_src_code = $objp -> vat_src_code ;
2015-06-29 13:12:37 +02:00
$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 );
}
}
/**
* insert
*
* @ param int $notrigger 1 = No trigger
2017-06-15 11:08:53 +02:00
* @ param bool $fromaddline false = keep default behavior , true = exclude the update_price () of parent object
2015-06-29 13:12:37 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2017-06-15 11:08:53 +02:00
function insert ( $notrigger = 0 , $fromaddline = false )
2015-06-29 13:12:37 +02:00
{
global $langs , $user , $conf ;
$error = 0 ;
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 );
2017-06-15 11:08:53 +02:00
if ( empty ( $this -> fk_c_exp_tax_cat )) $this -> fk_c_exp_tax_cat = 0 ;
2015-06-29 13:12:37 +02:00
$this -> db -> begin ();
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'expensereport_det' ;
$sql .= ' (fk_expensereport, fk_c_type_fees, fk_projet,' ;
2017-07-27 16:01:53 +02:00
$sql .= ' tva_tx, vat_src_code, comments, qty, value_unit, total_ht, total_tva, total_ttc, date, rule_warning_message, fk_c_exp_tax_cat)' ;
2018-03-10 15:52:11 +01:00
$sql .= " VALUES ( " . $this -> db -> escape ( $this -> fk_expensereport ) . " , " ;
$sql .= " " . $this -> db -> escape ( $this -> fk_c_type_fees ) . " , " ;
$sql .= " " . $this -> db -> escape ( $this -> fk_projet > 0 ? $this -> fk_projet : 'null' ) . " , " ;
$sql .= " " . $this -> db -> escape ( $this -> vatrate ) . " , " ;
2017-07-08 18:50:58 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> vat_src_code ) . " ', " ;
2015-06-29 13:12:37 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> comments ) . " ', " ;
2018-03-10 15:52:11 +01:00
$sql .= " " . $this -> db -> escape ( $this -> qty ) . " , " ;
$sql .= " " . $this -> db -> escape ( $this -> value_unit ) . " , " ;
$sql .= " " . $this -> db -> escape ( $this -> total_ht ) . " , " ;
$sql .= " " . $this -> db -> escape ( $this -> total_tva ) . " , " ;
$sql .= " " . $this -> db -> escape ( $this -> total_ttc ) . " , " ;
2017-06-15 11:08:53 +02:00
$sql .= " ' " . $this -> db -> idate ( $this -> date ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> rule_warning_message ) . " ', " ;
2018-03-10 15:52:11 +01:00
$sql .= " " . $this -> db -> escape ( $this -> fk_c_exp_tax_cat );
2015-06-29 13:12:37 +02:00
$sql .= " ) " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> rowid = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . 'expensereport_det' );
2017-06-15 11:08:53 +02:00
if ( ! $fromaddline )
{
$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 ;
2017-08-22 10:11:32 +02:00
}
2017-06-15 11:08:53 +02:00
}
2015-06-29 13:12:37 +02:00
}
2017-06-15 11:08:53 +02:00
else
{
$error ++ ;
}
2015-06-29 13:12:37 +02:00
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 ;
}
}
2017-06-15 11:08:53 +02:00
/**
* Function to get total amount in expense reports for a same rule
2017-08-22 10:11:32 +02:00
*
2017-06-15 11:08:53 +02:00
* @ param ExpenseReportRule $rule object rule to check
* @ param int $fk_user user author id
* @ param string $mode day | EX_DAY / month | EX_MON / year | EX_YEA to get amount
* @ return amount
*/
public function getExpAmount ( ExpenseReportRule $rule , $fk_user , $mode = 'day' )
{
$amount = 0 ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$sql = 'SELECT SUM(d.total_ttc) as total_amount' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'expensereport_det d' ;
$sql .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'expensereport e ON (d.fk_expensereport = e.rowid)' ;
$sql .= ' WHERE e.fk_user_author = ' . $fk_user ;
if ( ! empty ( $this -> id )) $sql .= ' AND d.rowid <> ' . $this -> id ;
$sql .= ' AND d.fk_c_type_fees = ' . $rule -> fk_c_type_fees ;
if ( $mode == 'day' || $mode == 'EX_DAY' ) $sql .= ' AND d.date = \'' . dol_print_date ( $this -> date , '%Y-%m-%d' ) . '\'' ;
elseif ( $mode == 'mon' || $mode == 'EX_MON' ) $sql .= ' AND DATE_FORMAT(d.date, \'%Y-%m\') = \'' . dol_print_date ( $this -> date , '%Y-%m' ) . '\'' ;
elseif ( $mode == 'year' || $mode == 'EX_YEA' ) $sql .= ' AND DATE_FORMAT(d.date, \'%Y\') = \'' . dol_print_date ( $this -> date , '%Y' ) . '\'' ;
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
dol_syslog ( 'ExpenseReportLine::getExpAmountByDay sql=' . $sql );
2017-08-22 10:11:32 +02:00
2017-06-15 11:08:53 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
if ( $num > 0 )
{
$obj = $this -> db -> fetch_object ( $resql );
$amount = ( double ) $obj -> total_amount ;
}
}
else
{
dol_print_error ( $this -> db );
}
2017-08-22 10:11:32 +02:00
2015-06-29 13:12:37 +02:00
2017-06-15 11:08:53 +02:00
return $amount + $this -> total_ttc ;
}
2017-08-22 10:11:32 +02:00
2015-06-29 13:12:37 +02:00
/**
* update
*
* @ param User $fuser User
* @ return int < 0 if KO , > 0 if OK
*/
function update ( $fuser )
{
global $fuser , $langs , $conf ;
$error = 0 ;
// Clean parameters
$this -> comments = trim ( $this -> comments );
$this -> vatrate = price2num ( $this -> vatrate );
2016-07-17 12:18:29 +02:00
$this -> value_unit = price2num ( $this -> value_unit );
2017-06-15 11:08:53 +02:00
if ( empty ( $this -> fk_c_exp_tax_cat )) $this -> fk_c_exp_tax_cat = 0 ;
2015-06-29 13:12:37 +02:00
$this -> db -> begin ();
2017-12-01 15:39:18 +01:00
// Update line in database
2015-06-29 13:12:37 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " expensereport_det SET " ;
$sql .= " comments=' " . $this -> db -> escape ( $this -> comments ) . " ' " ;
2018-03-10 15:52:11 +01:00
$sql .= " ,value_unit= " . $this -> db -> escape ( $this -> value_unit );
$sql .= " ,qty= " . $this -> db -> escape ( $this -> qty );
2015-06-29 13:12:37 +02:00
$sql .= " ,date=' " . $this -> db -> idate ( $this -> date ) . " ' " ;
2018-03-10 15:52:11 +01:00
$sql .= " ,total_ht= " . $this -> db -> escape ( $this -> total_ht ) . " " ;
$sql .= " ,total_tva= " . $this -> db -> escape ( $this -> total_tva ) . " " ;
$sql .= " ,total_ttc= " . $this -> db -> escape ( $this -> total_ttc ) . " " ;
$sql .= " ,tva_tx= " . $this -> db -> escape ( $this -> vatrate );
2017-07-27 16:01:53 +02:00
$sql .= " ,vat_src_code=' " . $this -> db -> escape ( $this -> vat_src_code ) . " ' " ;
2017-06-15 11:08:53 +02:00
$sql .= " ,rule_warning_message=' " . $this -> db -> escape ( $this -> rule_warning_message ) . " ' " ;
2018-03-10 15:52:11 +01:00
$sql .= " ,fk_c_exp_tax_cat= " . $this -> db -> escape ( $this -> fk_c_exp_tax_cat );
if ( $this -> fk_c_type_fees ) $sql .= " ,fk_c_type_fees= " . $this -> db -> escape ( $this -> fk_c_type_fees );
2015-06-29 13:12:37 +02:00
else $sql .= " ,fk_c_type_fees=null " ;
2018-03-10 15:52:11 +01:00
if ( $this -> fk_projet ) $sql .= " ,fk_projet= " . $this -> db -> escape ( $this -> fk_projet );
2015-06-29 13:12:37 +02:00
else $sql .= " ,fk_projet=null " ;
2018-03-10 15:52:11 +01:00
$sql .= " WHERE rowid = " . $this -> db -> escape ( $this -> rowid );
2015-06-29 13:12:37 +02:00
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 );
}
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 ;
}
}
2015-02-14 18:39:45 +01:00
}
2015-02-15 13:42:21 +01:00
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-02-14 18:39:45 +01:00
/**
* 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
*
2015-06-29 13:12:37 +02:00
* @ param int $selected preselect status
* @ param string $htmlname Name of HTML select
* @ param int $useempty 1 = Add empty line
2015-11-28 15:28:59 +01:00
* @ param int $useshortlabel Use short labels
2015-06-29 13:12:37 +02:00
* @ return string HTML select with status
2015-02-14 18:39:45 +01:00
*/
2015-11-28 15:28:59 +01:00
function select_expensereport_statut ( $selected = '' , $htmlname = 'fk_statut' , $useempty = 1 , $useshortlabel = 0 )
2015-02-14 18:39:45 +01:00
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-07-15 15:11:23 +02:00
global $db , $langs ;
2015-06-29 13:12:37 +02:00
$tmpep = new ExpenseReport ( $db );
print '<select class="flat" name="' . $htmlname . '">' ;
if ( $useempty ) print '<option value="-1"> </option>' ;
2015-11-28 15:28:59 +01:00
$arrayoflabels = $tmpep -> statuts ;
if ( $useshortlabel ) $arrayoflabels = $tmpep -> statuts_short ;
foreach ( $arrayoflabels as $key => $val )
2015-06-29 13:12:37 +02:00
{
if ( $selected != '' && $selected == $key )
{
print '<option value="' . $key . '" selected>' ;
}
else
{
print '<option value="' . $key . '">' ;
}
2015-07-15 15:11:23 +02:00
print $langs -> trans ( $val );
2015-06-29 13:12:37 +02:00
print '</option>' ;
}
print '</select>' ;
2015-02-14 18:39:45 +01:00
}
2018-09-02 18:49:23 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2015-02-14 18:39:45 +01:00
/**
2015-06-29 13:12:37 +02:00
* Return list of types of notes with select value = id
2015-02-14 18:39:45 +01:00
*
2015-06-29 13:12:37 +02:00
* @ param int $selected Preselected type
* @ param string $htmlname Name of field in form
* @ param int $showempty Add an empty field
2017-02-06 20:08:47 +01:00
* @ param int $active 1 = Active only , 0 = Unactive only , - 1 = All
2015-06-29 13:12:37 +02:00
* @ return string Select html
2015-02-14 18:39:45 +01:00
*/
2017-02-06 20:08:47 +01:00
function select_type_fees_id ( $selected = '' , $htmlname = 'type' , $showempty = 0 , $active = 1 )
2015-02-14 18:39:45 +01:00
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2015-06-29 13:12:37 +02:00
global $db , $langs , $user ;
$langs -> load ( " trips " );
print '<select class="flat" name="' . $htmlname . '">' ;
if ( $showempty )
{
print '<option value="-1"' ;
if ( $selected == - 1 ) print ' selected' ;
print '> </option>' ;
}
$sql = " SELECT c.id, c.code, c.label as type FROM " . MAIN_DB_PREFIX . " c_type_fees as c " ;
2017-02-06 20:08:47 +01:00
if ( $active >= 0 ) $sql .= " WHERE c.active = " . $active ;
2015-06-29 13:12:37 +02:00
$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 '<option value="' . $obj -> id . '"' ;
if ( $obj -> code == $selected || $obj -> id == $selected ) print ' selected' ;
print '>' ;
if ( $obj -> code != $langs -> trans ( $obj -> code )) print $langs -> trans ( $obj -> code );
else print $langs -> trans ( $obj -> type );
$i ++ ;
}
}
print '</select>' ;
2015-05-07 12:20:20 +02:00
}