2016-09-09 18:09:36 +00:00
< ? php
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
* Copyright ( C ) 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2025-11-20 00:47:46 +00:00
* Copyright ( C ) 2020 - 2025 Frédéric France < frederic . france @ free . fr >
2025-03-02 11:13:32 +00:00
* Copyright ( C ) 2025 MDW < mdeweerd @ users . noreply . github . com >
2025-06-04 08:37:25 +00:00
* Copyright ( C ) 2025 William Mead < william @ m34d . com >
2025-12-07 12:40:02 +00:00
* Copyright ( C ) 2025 Kowal Jessica < jessicakowal69 @ gmail . com >
2026-01-19 12:27:22 +00:00
* Copyright ( C ) 2026 Charlene Benke < charlene @ patas - monkey . com >
2017-10-07 12:11:01 +00:00
*
2016-09-09 18:09:36 +00: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
2019-09-23 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2016-09-09 18:09:36 +00:00
*/
2024-02-19 16:55:00 +00:00
use Luracast\Restler\RestException ;
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/paymentexpensereport.class.php' ;
2025-12-07 10:52:13 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php' ;
2016-09-09 18:09:36 +00:00
/**
* API class for Expense Reports
*
2025-06-04 08:37:25 +00:00
* @ since 5.0 . 0 Initial implementation
*
2017-10-07 12:11:01 +00:00
* @ access protected
2016-09-09 18:09:36 +00:00
* @ class DolibarrApiAccess { @ requires user , external }
*/
class ExpenseReports extends DolibarrApi
{
2020-10-31 17:51:30 +00:00
/**
2025-03-02 19:40:15 +00:00
* @ var string [] Mandatory fields , checked when create and update object
2020-10-31 17:51:30 +00:00
*/
2021-02-25 21:45:02 +00:00
public static $FIELDS = array (
2025-06-03 15:06:04 +00:00
'fk_user_author' ,
'date_debut' ,
'date_fin' ,
2020-10-31 17:51:30 +00:00
);
2016-09-09 18:09:36 +00:00
2025-12-07 10:52:13 +00:00
/**
* @ var string [] Mandatory fields , checked when create and update object
*/
public static $FIELDSLINE = array (
'date' ,
'fk_c_type_fees' ,
'qty' ,
2025-12-08 07:41:22 +00:00
'value_unit' ,
2025-12-07 10:52:13 +00:00
'vatrate'
);
2024-02-19 16:55:00 +00:00
/**
2025-03-02 19:40:15 +00:00
* @ var string [] Mandatory fields , checked when create and update object
2024-02-19 16:55:00 +00:00
*/
public static $FIELDSPAYMENT = array (
" fk_typepayment " ,
2026-01-19 12:27:50 +00:00
'datep' ,
2024-02-19 16:55:00 +00:00
'amounts' ,
);
2020-10-31 17:51:30 +00:00
/**
2025-03-02 11:13:32 +00:00
* @ var ExpenseReport { @ type ExpenseReport }
2020-10-31 17:51:30 +00:00
*/
public $expensereport ;
2017-10-07 12:11:01 +00:00
2020-10-31 17:51:30 +00:00
/**
* Constructor
*/
public function __construct ()
{
2024-02-19 16:55:00 +00:00
global $db ;
2020-10-31 17:51:30 +00:00
$this -> db = $db ;
$this -> expensereport = new ExpenseReport ( $this -> db );
}
2017-10-07 12:11:01 +00:00
2020-10-31 17:51:30 +00:00
/**
2025-06-04 08:37:25 +00:00
* Get an expense report
2020-10-31 17:51:30 +00:00
*
2024-01-13 18:48:41 +00:00
* Return an array with Expense Report information
2020-10-31 17:51:30 +00:00
*
2025-06-04 08:37:25 +00:00
* @ since 5.0 . 0 Initial implementation
*
* @ param int $id ID of Expense Report
* @ return Object Object with cleaned properties
2020-10-31 17:51:30 +00:00
*
2023-09-26 16:43:25 +00:00
* @ throws RestException
2020-10-31 17:51:30 +00:00
*/
public function get ( $id )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'lire' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
2016-09-09 18:09:36 +00:00
2020-10-31 17:51:30 +00:00
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Expense report not found' );
}
2017-10-07 12:11:01 +00:00
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
2016-09-09 18:09:36 +00:00
2020-10-31 17:51:30 +00:00
$this -> expensereport -> fetchObjectLinked ();
return $this -> _cleanObjectDatas ( $this -> expensereport );
}
2016-09-09 18:09:36 +00:00
2020-10-31 17:51:30 +00:00
/**
2025-06-04 08:37:25 +00:00
* List expense reports
2020-10-31 17:51:30 +00:00
*
* Get a list of Expense Reports
*
2025-06-04 08:37:25 +00:00
* @ since 5.0 . 0 Initial implementation
*
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit List limit
* @ param int $page Page number
* @ param string $user_ids User ids filter field . Example : '1' or '1,2,3' { @ pattern /^ [ 0 - 9 ,] * $ / i }
2026-05-29 10:16:42 +00:00
* @ param string $sqlfilters Other criteria to filter answers separated by a comma . Syntax example " (t.ref:like:'SO-%') and (t.date_creation:>:'20160101') "
2025-06-04 08:37:25 +00:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
* @ param bool $pagination_data If this parameter is set to true the response will include pagination data . Default value is false . Page starts from 0 *
* @ return array Array of order objects
2025-03-02 14:10:25 +00:00
* @ phan - return ExpenseReport []
* @ phpstan - return ExpenseReport []
2025-06-04 08:37:25 +00:00
*
* @ throws RestException
2020-10-31 17:51:30 +00:00
*/
2024-09-04 21:14:45 +00:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $user_ids = '' , $sqlfilters = '' , $properties = '' , $pagination_data = false )
2020-10-31 17:51:30 +00:00
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'lire' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2021-04-08 17:05:28 +00:00
}
2020-10-31 17:51:30 +00:00
$obj_ret = array ();
// case of external user, $societe param is ignored and replaced by user's socid
2025-07-01 18:34:09 +00:00
//$socid = DolibarrApiAccess::$user->socid ?: $societe;
2020-10-31 17:51:30 +00:00
$sql = " SELECT t.rowid " ;
2023-04-28 07:31:33 +00:00
$sql .= " FROM " . MAIN_DB_PREFIX . " expensereport AS t LEFT JOIN " . MAIN_DB_PREFIX . " expensereport_extrafields AS ef ON (ef.fk_object = t.rowid) " ; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
2020-10-31 17:51:30 +00:00
$sql .= ' WHERE t.entity IN (' . getEntity ( 'expensereport' ) . ')' ;
2021-02-25 21:45:02 +00:00
if ( $user_ids ) {
2021-03-22 10:30:18 +00:00
$sql .= " AND t.fk_user_author IN ( " . $this -> db -> sanitize ( $user_ids ) . " ) " ;
2021-02-25 21:45:02 +00:00
}
2020-10-31 17:51:30 +00:00
// Add sql filters
2021-02-25 21:45:02 +00:00
if ( $sqlfilters ) {
2021-12-20 19:49:32 +00:00
$errormessage = '' ;
2023-02-25 18:48:33 +00:00
$sql .= forgeSQLFromUniversalSearchCriteria ( $sqlfilters , $errormessage );
if ( $errormessage ) {
throw new RestException ( 400 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
2020-10-31 17:51:30 +00:00
}
}
2024-09-04 21:14:45 +00:00
//this query will return total orders with the filters given
$sqlTotals = str_replace ( 'SELECT t.rowid' , 'SELECT count(t.rowid) as total' , $sql );
2020-10-31 17:51:30 +00:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
if ( $page < 0 ) {
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
2017-10-07 12:11:01 +00:00
2020-10-31 17:51:30 +00:00
$result = $this -> db -> query ( $sql );
2021-02-25 21:45:02 +00:00
if ( $result ) {
2020-10-31 17:51:30 +00:00
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
2020-10-31 08:32:00 +00:00
$i = 0 ;
2021-02-25 21:45:02 +00:00
while ( $i < $min ) {
2020-10-31 17:51:30 +00:00
$obj = $this -> db -> fetch_object ( $result );
$expensereport_static = new ExpenseReport ( $this -> db );
if ( $expensereport_static -> fetch ( $obj -> rowid )) {
2023-09-26 16:04:48 +00:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $expensereport_static ), $properties );
2020-10-31 17:51:30 +00:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve Expense Report list : ' . $this -> db -> lasterror ());
}
2023-12-31 13:11:05 +00:00
2024-09-04 21:14:45 +00:00
//if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
if ( $pagination_data ) {
$totalsResult = $this -> db -> query ( $sqlTotals );
$total = $this -> db -> fetch_object ( $totalsResult ) -> total ;
$tmp = $obj_ret ;
$obj_ret = [];
$obj_ret [ 'data' ] = $tmp ;
$obj_ret [ 'pagination' ] = [
'total' => ( int ) $total ,
'page' => $page , //count starts from 0
'page_count' => ceil (( int ) $total / $limit ),
'limit' => $limit
];
}
2020-10-31 17:51:30 +00:00
return $obj_ret ;
}
2016-09-09 18:09:36 +00:00
2020-10-31 17:51:30 +00:00
/**
2025-06-04 08:37:25 +00:00
* Create an expense report
*
* @ since 5.0 . 0 Initial implementation
2020-10-31 17:51:30 +00:00
*
2025-06-04 08:37:25 +00:00
* @ param array $request_data Request data
2025-03-02 11:13:32 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2025-06-04 08:37:25 +00:00
* @ return int ID of Expense Report
*
* @ throws RestException
2020-10-31 17:51:30 +00:00
*/
public function post ( $request_data = null )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
2025-09-11 10:25:22 +00:00
throw new RestException ( 403 , " Insufficiant rights " );
2020-10-31 17:51:30 +00:00
}
2021-04-08 17:05:28 +00:00
2020-10-31 17:51:30 +00:00
// Check mandatory fields
$result = $this -> _validate ( $request_data );
2016-09-09 18:09:36 +00:00
2020-10-31 17:51:30 +00:00
foreach ( $request_data as $field => $value ) {
2023-12-15 11:15:33 +00:00
if ( $field === 'caller' ) {
2024-01-13 18:48:41 +00:00
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
2024-04-02 10:28:55 +00:00
$this -> expensereport -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2026-08-09 17:59:33 +00:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
$this -> expensereport -> array_options [ $index ] = $this -> _checkValExtrafieldsForAPI ( $index , $val , $this -> expensereport );
}
continue ;
}
if ( ! in_array ( $field , array ( 'fk_statut' , 'fk_user_approve' ))) { // Exclude properties that must be set by other workflow methods
$this -> expensereport -> $field = $this -> _checkValForAPI ( $field , $value , $this -> expensereport );
}
2020-10-31 17:51:30 +00:00
}
/* if ( isset ( $request_data [ " lines " ])) {
2021-02-25 21:45:02 +00:00
$lines = array ();
foreach ( $request_data [ " lines " ] as $line ) {
array_push ( $lines , ( object ) $line );
}
$this -> expensereport -> lines = $lines ;
} */
2020-10-31 17:51:30 +00:00
if ( $this -> expensereport -> create ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , " Error creating expensereport " , array_merge ( array ( $this -> expensereport -> error ), $this -> expensereport -> errors ));
}
2017-10-07 12:11:01 +00:00
2020-10-31 17:51:30 +00:00
return $this -> expensereport -> id ;
}
2016-09-09 18:09:36 +00:00
2020-10-31 17:51:30 +00:00
/**
2025-12-07 12:58:04 +00:00
* Get lines of an expense report
2020-10-31 17:51:30 +00:00
*
2025-12-07 13:14:52 +00:00
* @ since 23.0 . 0 Initial implementation
*
2025-12-07 12:58:04 +00:00
* @ param int $id ID of the expense report
2020-10-31 17:51:30 +00:00
*
* @ url GET { id } / lines
*
2025-12-07 12:58:04 +00:00
* @ return array
2025-12-07 13:21:23 +00:00
* @ phan - return ExpenseReportLine []
* @ phpstan - return ExpenseReportLine []
2025-12-07 12:58:04 +00:00
*
* @ throws RestException 403
* @ throws RestException 404
2020-10-31 17:51:30 +00:00
*/
2021-02-25 21:45:02 +00:00
public function getLines ( $id )
{
2025-12-07 13:14:52 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'lire' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2021-02-25 21:45:02 +00:00
}
$result = $this -> expensereport -> fetch ( $id );
2025-12-07 13:14:52 +00:00
if ( ! $result ) {
2026-03-26 15:46:59 +00:00
throw new RestException ( 404 , 'Expense report not found' );
2021-02-25 21:45:02 +00:00
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2021-02-25 21:45:02 +00:00
}
2025-12-07 12:58:04 +00:00
$this -> expensereport -> fetch_lines ();
2021-02-25 21:45:02 +00:00
$result = array ();
foreach ( $this -> expensereport -> lines as $line ) {
2025-12-07 12:58:04 +00:00
$result [] = $this -> _cleanObjectDatas ( $line );
2021-02-25 21:45:02 +00:00
}
return $result ;
}
2017-10-07 12:11:01 +00:00
2020-10-31 13:32:18 +00:00
/**
2025-12-07 10:52:13 +00:00
* Add a line to an expense report
2020-10-31 13:32:18 +00:00
*
2025-12-07 13:16:11 +00:00
* @ since 23.0 . 0 Initial implementation
*
* @ param int $id ID of the expense report to update
2025-12-07 10:52:13 +00:00
* @ param array $request_data Expense Report line data
* @ phan - param ? array < string , mixed > $request_data
* @ phpstan - param ? array < string , mixed > $request_data
2020-10-31 13:32:18 +00:00
*
2025-12-07 10:52:13 +00:00
* @ url POST { id } / line
2020-10-31 13:32:18 +00:00
*
* @ return int
2025-12-07 10:52:13 +00:00
*
* @ throws RestException
2020-10-31 13:32:18 +00:00
*/
2021-02-25 21:45:02 +00:00
public function postLine ( $id , $request_data = null )
{
2025-12-07 10:52:13 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
throw new RestException ( 403 );
}
2021-02-25 21:45:02 +00:00
2025-12-07 10:52:13 +00:00
$result = $this -> _validateLine ( $request_data );
2021-02-25 21:45:02 +00:00
2025-12-07 10:52:13 +00:00
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Expense report not found' );
}
2021-04-25 17:21:48 +00:00
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2025-12-07 10:52:13 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2021-04-25 17:21:48 +00:00
2025-12-07 10:52:13 +00:00
if ( $this -> expensereport -> status != ExpenseReport :: STATUS_DRAFT ) {
throw new RestException ( 403 , 'Expense report must be in draft status to add lines' );
}
2021-04-25 17:21:48 +00:00
2025-12-07 10:52:13 +00:00
$request_data = ( object ) $request_data ;
2021-02-25 21:45:02 +00:00
2025-12-07 10:52:13 +00:00
$request_data -> comments = sanitizeVal ( $request_data -> comments , 'restricthtml' );
$result = $this -> expensereport -> addline (
$request_data -> qty ,
2025-12-08 07:40:20 +00:00
$request_data -> value_unit ,
2025-12-07 10:52:13 +00:00
( int ) $request_data -> fk_c_type_fees ,
$request_data -> vatrate ,
$request_data -> date ,
$request_data -> comments ,
$request_data -> fk_project ,
2026-07-24 15:44:31 +00:00
( int ) $request_data -> fk_c_exp_tax_cat ,
2025-12-07 10:52:13 +00:00
$request_data -> type ,
$request_data -> fk_ecm_files
);
2021-02-25 21:45:02 +00:00
2025-12-07 10:52:13 +00:00
if ( $result > 0 ) {
return $result ;
} else {
throw new RestException ( 500 , 'Error adding line to expense report: ' . $this -> expensereport -> error );
}
2021-02-25 21:45:02 +00:00
}
2017-10-07 12:11:01 +00:00
2020-10-31 13:32:18 +00:00
/**
2025-12-08 07:40:20 +00:00
* Update a line of an expense report
2020-10-31 13:32:18 +00:00
*
2025-12-08 07:40:20 +00:00
* @ since 23.0 . 0 Initial implementation
*
* @ param int $id ID of the expense report
* @ param int $lineid ID of the line to update
* @ param array $request_data Expense Report data
2025-03-02 11:13:32 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2020-10-31 13:32:18 +00:00
*
* @ url PUT { id } / lines / { lineid }
*
2025-12-08 07:40:20 +00:00
* @ return Object | false Object with cleaned properties
*
* @ throws RestException 403
* @ throws RestException 404
* @ throws RestException 500
2020-10-31 13:32:18 +00:00
*/
2021-02-25 21:45:02 +00:00
public function putLine ( $id , $lineid , $request_data = null )
{
2025-12-08 07:40:20 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
throw new RestException ( 403 );
2021-02-25 21:45:02 +00:00
}
$result = $this -> expensereport -> fetch ( $id );
2025-12-08 14:52:12 +00:00
if ( ! $result ) {
2026-03-26 15:46:59 +00:00
throw new RestException ( 404 , 'Expense report not found' );
2021-02-25 21:45:02 +00:00
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2021-02-25 21:45:02 +00:00
}
2021-04-25 17:21:48 +00:00
2025-12-08 07:40:20 +00:00
if ( $this -> expensereport -> status != ExpenseReport :: STATUS_DRAFT ) {
throw new RestException ( 403 , 'Expense report must be in draft status to update lines' );
}
$line = new ExpenseReportLine ( $this -> db );
$result = $line -> fetch ( $lineid );
if ( $result <= 0 ) {
throw new RestException ( 404 , 'Expense report line not found' );
}
2021-02-25 21:45:02 +00:00
$request_data = ( object ) $request_data ;
2021-04-25 17:21:48 +00:00
2025-12-08 07:40:20 +00:00
$request_data -> comments = sanitizeVal ( $request_data -> comments , 'restricthtml' );
2021-04-25 17:21:48 +00:00
2021-02-25 21:45:02 +00:00
$updateRes = $this -> expensereport -> updateline (
2025-12-08 07:40:20 +00:00
$lineid ,
( int ) $request_data -> fk_c_type_fees ,
$request_data -> fk_project ,
$request_data -> vatrate ,
$request_data -> comments ,
$request_data -> qty ,
$request_data -> value_unit ,
$request_data -> date ,
$id ,
2026-07-24 15:44:31 +00:00
( int ) $request_data -> fk_c_exp_tax_cat ,
2025-12-08 07:40:20 +00:00
$request_data -> fk_ecm_files
2021-02-25 21:45:02 +00:00
);
if ( $updateRes > 0 ) {
$result = $this -> get ( $id );
unset ( $result -> line );
return $this -> _cleanObjectDatas ( $result );
2025-12-08 07:40:20 +00:00
} else {
throw new RestException ( 500 , 'Error updating line: ' . $this -> expensereport -> error );
2021-02-25 21:45:02 +00:00
}
}
2016-09-09 18:09:36 +00:00
2020-10-31 13:32:18 +00:00
/**
2025-12-07 12:40:02 +00:00
* Delete a line from an expense report
2020-10-31 13:32:18 +00:00
*
2025-12-07 13:15:26 +00:00
* @ since 23.0 . 0 Initial implementation
*
2025-12-07 12:40:02 +00:00
* @ param int $id ID of the expense report to update
* @ param int $lineid ID of line to delete
2020-10-31 13:32:18 +00:00
*
* @ url DELETE { id } / lines / { lineid }
*
2025-12-07 12:40:02 +00:00
* @ return object
*
* @ throws RestException 403
* @ throws RestException 404
* @ throws RestException 500
2020-10-31 13:32:18 +00:00
*/
2021-02-25 21:45:02 +00:00
public function deleteLine ( $id , $lineid )
{
2025-12-07 12:40:02 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
throw new RestException ( 403 );
}
2021-02-25 21:45:02 +00:00
2025-12-07 12:40:02 +00:00
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Expense report not found' );
}
2021-02-25 21:45:02 +00:00
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2025-12-07 12:40:02 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2021-02-25 21:45:02 +00:00
2025-12-07 12:40:02 +00:00
// Check if line exists
$lineExists = false ;
$this -> expensereport -> fetch_lines ();
foreach ( $this -> expensereport -> lines as $line ) {
if ( $line -> id == $lineid ) {
$lineExists = true ;
break ;
}
}
2021-02-25 21:45:02 +00:00
2025-12-07 12:40:02 +00:00
if ( ! $lineExists ) {
throw new RestException ( 404 , 'Line not found' );
}
2021-02-25 21:45:02 +00:00
2025-12-07 12:40:02 +00:00
if ( $this -> expensereport -> status != ExpenseReport :: STATUS_DRAFT ) {
throw new RestException ( 403 , 'Expense report must be in draft status to delete lines' );
}
$result = $this -> expensereport -> deleteLine ( $lineid );
if ( $result > 0 ) {
return $this -> get ( $id );
} else {
throw new RestException ( 500 , 'Error deleting line: ' . $this -> expensereport -> error );
}
2021-02-25 21:45:02 +00:00
}
2017-10-07 12:11:01 +00:00
2020-10-31 13:32:18 +00:00
/**
2025-06-04 08:37:25 +00:00
* Update expense report general fields
*
* Does not touch lines of the expense report
*
* @ since 5.0 . 0 Initial implementation
2020-10-31 13:32:18 +00:00
*
2025-06-04 08:37:25 +00:00
* @ param int $id ID of Expense Report to update
* @ param array $request_data Expense report data
2025-03-02 11:13:32 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2025-06-04 08:37:25 +00:00
* @ return Object Updated object
2020-10-31 13:32:18 +00:00
*
* @ throws RestException 401 Not allowed
* @ throws RestException 404 Expense report not found
2022-03-18 15:14:20 +00:00
* @ throws RestException 500 System error
2020-10-31 13:32:18 +00:00
*/
public function put ( $id , $request_data = null )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 13:32:18 +00:00
}
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
2026-03-26 15:46:59 +00:00
throw new RestException ( 404 , 'Expense report not found' );
2020-10-31 13:32:18 +00:00
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 13:32:18 +00:00
}
foreach ( $request_data as $field => $value ) {
2021-02-25 21:45:02 +00:00
if ( $field == 'id' ) {
continue ;
}
2023-12-15 11:15:33 +00:00
if ( $field === 'caller' ) {
2024-01-13 18:48:41 +00:00
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
2024-04-02 10:28:55 +00:00
$this -> expensereport -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2025-01-16 09:37:29 +00:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
2026-05-08 23:16:35 +00:00
$this -> expensereport -> array_options [ $index ] = $this -> _checkValExtrafieldsForAPI ( $index , $val , $this -> expensereport );
2025-01-16 09:37:29 +00:00
}
continue ;
}
2025-01-20 13:57:45 +00:00
2026-08-09 17:59:33 +00:00
if ( ! in_array ( $field , array ( 'fk_statut' , 'fk_user_approve' ))) { // Exclude properties that must be set by other workflow methods
$this -> expensereport -> $field = $this -> _checkValForAPI ( $field , $value , $this -> expensereport );
}
2020-10-31 13:32:18 +00:00
}
2021-02-25 21:45:02 +00:00
if ( $this -> expensereport -> update ( DolibarrApiAccess :: $user ) > 0 ) {
2020-10-31 13:32:18 +00:00
return $this -> get ( $id );
} else {
throw new RestException ( 500 , $this -> expensereport -> error );
}
}
/**
2025-06-04 08:37:25 +00:00
* Delete expense report
2020-10-31 13:32:18 +00:00
*
2025-06-04 08:37:25 +00:00
* @ since 5.0 . 0 Initial implementation
2020-10-31 13:32:18 +00:00
*
2025-06-04 08:37:25 +00:00
* @ param int $id Expense Report ID
* @ return array
2025-03-02 14:10:25 +00:00
* @ phan - return array { success : array { code : int , message : string }}
* @ phpstan - return array { success : array { code : int , message : string }}
2025-06-04 08:37:25 +00:00
*
* @ throws RestException
2020-10-31 13:32:18 +00:00
*/
public function delete ( $id )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'supprimer' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 13:32:18 +00:00
}
2021-04-08 17:05:28 +00:00
2020-10-31 13:32:18 +00:00
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
2026-03-26 15:46:59 +00:00
throw new RestException ( 404 , 'Expense report not found' );
2020-10-31 13:32:18 +00:00
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2024-02-09 14:58:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 13:32:18 +00:00
}
if ( ! $this -> expensereport -> delete ( DolibarrApiAccess :: $user )) {
throw new RestException ( 500 , 'Error when delete Expense Report : ' . $this -> expensereport -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'Expense Report deleted'
)
);
}
2025-12-07 13:10:57 +00:00
/**
* Set an expense report to draft
*
* @ since 23.0 . 0 Initial implementation
*
* @ param int $id Expense report ID
*
* @ url POST { id } / settodraft
*
* @ return Object
*
* @ throws RestException 304
* @ throws RestException 403
* @ throws RestException 404
*/
public function setToDraft ( $id )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
throw new RestException ( 403 , " Insufficiant rights " );
}
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Expense report not found' );
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2025-12-07 13:10:57 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2026-02-09 23:44:46 +00:00
$result = $this -> expensereport -> setStatut ( ExpenseReport :: STATUS_DRAFT );
2025-12-07 13:10:57 +00:00
if ( $result == 0 ) {
throw new RestException ( 304 , 'Error nothing done. May be object is already draft' );
}
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when setting to draft expense report: ' . $this -> expensereport -> error );
}
return $this -> _cleanObjectDatas ( $this -> expensereport );
}
2020-10-31 13:32:18 +00:00
/**
2025-06-14 09:54:00 +00:00
* Validate an expense report
2020-10-31 13:32:18 +00:00
*
2025-06-14 09:54:00 +00:00
* If you get a bad value for param notrigger check , provide this in body
2020-10-31 13:32:18 +00:00
* {
2025-06-14 09:54:00 +00:00
* " notrigger " : 0
2020-10-31 13:32:18 +00:00
* }
2025-06-14 09:54:00 +00:00
*
2025-06-22 01:00:19 +00:00
* @ since 22.0 . 0 Initial implementation
2025-06-14 09:54:00 +00:00
*
* @ param int $id Expense report ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
*
* @ url POST { id } / validate
*
* @ return Object
*
* @ throws RestException
2020-10-31 13:32:18 +00:00
*/
2025-06-14 09:54:00 +00:00
public function validate ( $id , $notrigger = 0 )
2021-02-25 21:45:02 +00:00
{
2025-06-14 09:54:00 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
2025-09-11 10:25:22 +00:00
throw new RestException ( 403 , " Insufficiant rights " );
2021-02-25 21:45:02 +00:00
}
$result = $this -> expensereport -> fetch ( $id );
2025-06-14 09:54:00 +00:00
if ( ! $result ) {
throw new RestException ( 404 , 'Expense report not found' );
2021-02-25 21:45:02 +00:00
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2021-02-25 21:45:02 +00:00
}
2025-06-14 09:54:00 +00:00
$result = $this -> expensereport -> setValidate ( DolibarrApiAccess :: $user , $notrigger );
if ( $result == 0 ) {
throw new RestException ( 304 , 'Error nothing done. May be object is already validated' );
}
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when validating expense report: ' . $this -> expensereport -> error );
2021-02-25 21:45:02 +00:00
}
2025-06-14 09:54:00 +00:00
return $this -> _cleanObjectDatas ( $this -> expensereport );
}
2024-02-19 16:55:00 +00:00
2025-06-22 00:59:28 +00:00
/**
* Approve an expense report
*
* If you get a bad value for param notrigger check , provide this in body
* {
* " notrigger " : 0
* }
*
* @ since 22.0 . 0 Initial implementation
*
* @ param int $id Expense report ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
*
* @ url POST { id } / approve
*
* @ return Object
*
* @ throws RestException
*/
public function approve ( $id , $notrigger = 0 )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'approve' )) {
2025-09-11 10:25:22 +00:00
throw new RestException ( 403 , " Insufficiant rights " );
2025-06-22 00:59:28 +00:00
}
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Expense report not found' );
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2025-06-22 00:59:28 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> expensereport -> setApproved ( DolibarrApiAccess :: $user , $notrigger );
if ( $result == 0 ) {
throw new RestException ( 304 , 'Error nothing done. May be object is already approved' );
}
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when approving expense report: ' . $this -> expensereport -> error );
}
return $this -> _cleanObjectDatas ( $this -> expensereport );
}
2025-06-22 00:56:22 +00:00
/**
* Deny an expense report
*
* If you get a bad value for param notrigger check , provide this in body
* {
* " notrigger " : 0
* }
*
* @ since 22.0 . 0 Initial implementation
*
* @ param int $id Expense report ID
* @ param string $details Comments for denial
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
*
* @ url POST { id } / deny
*
* @ return Object
*
* @ throws RestException
*/
public function deny ( $id , $details , $notrigger = 0 )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'approve' )) {
2025-09-11 10:25:22 +00:00
throw new RestException ( 403 , " Insufficiant rights " );
2025-06-22 00:56:22 +00:00
}
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Expense report not found' );
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2025-06-22 00:56:22 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> expensereport -> setDeny ( DolibarrApiAccess :: $user , $details , $notrigger );
if ( $result == 0 ) {
throw new RestException ( 304 , 'Error nothing done. May be object is already denied' );
}
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when denying expense report: ' . $this -> expensereport -> error );
}
2025-08-24 18:25:03 +00:00
2025-06-22 00:56:22 +00:00
return $this -> _cleanObjectDatas ( $this -> expensereport );
}
2026-01-19 12:27:22 +00:00
/**
* Set to paid an expense report
*
* If you get a bad value for param notrigger check , provide this in body
* {
* " notrigger " : 0
* }
*
* @ since 22.0 . 0 Initial implementation
*
* @ param int $id Expense report ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
*
* @ url POST { id } / setpaid
*
* @ return Object
*
* @ throws RestException
*/
public function setPaid ( $id , $notrigger = 0 )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'to_paid' )) {
throw new RestException ( 403 , " Insufficiant rights " );
}
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Expense report not found' );
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2026-01-19 12:27:22 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> expensereport -> setPaid ( $id , DolibarrApiAccess :: $user , $notrigger );
if ( $result == 0 ) {
throw new RestException ( 304 , 'Error nothing done. May be object is already approved' );
}
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when approving expense report: ' . $this -> expensereport -> error );
}
return $this -> _cleanObjectDatas ( $this -> expensereport );
}
2025-12-07 13:32:27 +00:00
/**
* Cancel an expense report
*
* @ since 23.0 . 0 Initial implementation
*
* @ param int $id ID of the expense report
* @ param string $detail Comments for cancellation
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
*
* @ url POST { id } / cancel
*
* @ return Object
*
* @ throws RestException 403
* @ throws RestException 404
* @ throws RestException 500
*/
public function cancel ( $id , $detail , $notrigger = 0 )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
throw new RestException ( 403 , " Insufficiant rights " );
}
$result = $this -> expensereport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Expense report not found' );
}
2026-03-26 15:46:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'expensereport' , $this -> expensereport )) {
2025-12-07 13:32:27 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
if ( $this -> expensereport -> status == ExpenseReport :: STATUS_CANCELED ) {
throw new RestException ( 403 , 'Expense report already canceled' );
}
$result = $this -> expensereport -> set_cancel ( DolibarrApiAccess :: $user , $detail , $notrigger );
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when cancelling expense report: ' . $this -> expensereport -> error );
}
2025-12-07 13:36:06 +00:00
$result = $this -> expensereport -> fetch ( $id );
return $this -> _cleanObjectDatas ( $this -> expensereport );
2025-12-07 13:32:27 +00:00
}
2025-06-22 00:59:28 +00:00
2024-02-19 16:55:00 +00:00
/**
2025-06-04 08:37:25 +00:00
* Get the list of payments of an expense report
2024-02-19 16:55:00 +00:00
*
2025-06-04 08:37:25 +00:00
* @ since 20.0 . 0 Initial implementation
*
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit List limit
* @ param int $page Page number
* @ return array List of paymentExpenseReport objects
2025-03-02 14:10:25 +00:00
* @ phan - return PaymentExpenseReport []
* @ phpstan - return PaymentExpenseReport []
2024-02-19 16:55:00 +00:00
*
* @ url GET / payments
*
* @ throws RestException
*/
public function getAllPayments ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 )
{
$list = array ();
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'lire' )) {
throw new RestException ( 403 );
}
$sql = " SELECT t.rowid FROM " . MAIN_DB_PREFIX . " payment_expensereport as t, " . MAIN_DB_PREFIX . " expensereport as e " ;
$sql .= " WHERE e.rowid = t.fk_expensereport " ;
$sql .= ' AND e.entity IN (' . getEntity ( 'expensereport' ) . ')' ;
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
if ( $page < 0 ) {
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
dol_syslog ( " API Rest request " );
$result = $this -> db -> query ( $sql );
if ( $result ) {
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
for ( $i = 0 ; $i < $min ; $i ++ ) {
$obj = $this -> db -> fetch_object ( $result );
$paymentExpenseReport = new PaymentExpenseReport ( $this -> db );
if ( $paymentExpenseReport -> fetch ( $obj -> rowid ) > 0 ) {
$list [] = $this -> _cleanObjectDatas ( $paymentExpenseReport );
}
}
} else {
throw new RestException ( 503 , 'Error when retrieving list of paymentexpensereport: ' . $this -> db -> lasterror ());
}
return $list ;
}
/**
2025-06-04 08:37:25 +00:00
* Get an expense report payment
*
* @ since 20.0 . 0 Initial implementation
2024-02-19 16:55:00 +00:00
*
* @ param int $pid Payment ID
2025-06-04 08:37:25 +00:00
* @ return object PaymentExpenseReport object
2024-02-19 16:55:00 +00:00
*
* @ url GET / payments / { pid }
*
* @ throws RestException
*/
public function getPayments ( $pid )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'lire' )) {
throw new RestException ( 403 );
}
$paymentExpenseReport = new PaymentExpenseReport ( $this -> db );
$result = $paymentExpenseReport -> fetch ( $pid );
if ( ! $result ) {
throw new RestException ( 404 , 'paymentExpenseReport not found' );
}
return $this -> _cleanObjectDatas ( $paymentExpenseReport );
}
/**
2025-06-04 08:37:25 +00:00
* Create a payment for an expense report
2024-02-19 16:55:00 +00:00
*
2025-06-04 08:37:25 +00:00
* @ since 20.0 . 0 Initial implementation
*
* @ param int $id ID of an expense report
* @ param array $request_data { @ from body } Request data
2025-03-02 11:13:32 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2025-06-04 08:37:25 +00:00
* @ return int ID of paymentExpenseReport
2024-02-19 16:55:00 +00:00
*
* @ url POST { id } / payments
2025-06-04 08:37:25 +00:00
* @ throws RestException
2024-02-19 16:55:00 +00:00
*/
public function addPayment ( $id , $request_data = null )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
throw new RestException ( 403 );
}
// Check mandatory fields
$result = $this -> _validatepayment ( $request_data );
$paymentExpenseReport = new PaymentExpenseReport ( $this -> db );
$paymentExpenseReport -> fk_expensereport = $id ;
foreach ( $request_data as $field => $value ) {
$paymentExpenseReport -> $field = $this -> _checkValForAPI ( $field , $value , $paymentExpenseReport );
}
2024-02-21 15:02:31 +00:00
if ( $paymentExpenseReport -> create ( DolibarrApiAccess :: $user ) < 0 ) {
2024-02-19 16:55:00 +00:00
throw new RestException ( 500 , 'Error creating paymentExpenseReport' , array_merge ( array ( $paymentExpenseReport -> error ), $paymentExpenseReport -> errors ));
}
2024-02-27 14:30:37 +00:00
if ( isModEnabled ( " bank " )) {
2024-02-19 16:55:00 +00:00
$paymentExpenseReport -> addPaymentToBank (
DolibarrApiAccess :: $user ,
'payment_expensereport' ,
'(ExpenseReportPayment)' ,
( int ) $request_data [ 'accountid' ],
'' ,
''
);
}
return $paymentExpenseReport -> id ;
}
/**
2025-06-04 08:37:25 +00:00
* Update a payment of an expense report
2024-02-19 16:55:00 +00:00
*
2025-06-04 08:37:25 +00:00
* @ since 20.0 . 0 Initial implementation
*
* @ param int $id ID of paymentExpenseReport
* @ param array $request_data data
2025-03-02 11:13:32 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2025-06-04 08:37:25 +00:00
* @ return object
2024-02-19 16:55:00 +00:00
*
* @ url PUT { id } / payments
2025-06-04 08:37:25 +00:00
* @ throws RestException
2024-02-19 16:55:00 +00:00
*/
public function updatePayment ( $id , $request_data = null )
{
2024-03-19 21:02:42 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' )) {
2024-02-19 16:55:00 +00:00
throw new RestException ( 403 );
}
$paymentExpenseReport = new PaymentExpenseReport ( $this -> db );
$result = $paymentExpenseReport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'payment of expense report not found' );
}
foreach ( $request_data as $field => $value ) {
if ( $field == 'id' ) {
continue ;
}
$paymentExpenseReport -> $field = $this -> _checkValForAPI ( $field , $value , $paymentExpenseReport );
}
if ( $paymentExpenseReport -> update ( DolibarrApiAccess :: $user ) > 0 ) {
return $this -> get ( $id );
} else {
throw new RestException ( 500 , $paymentExpenseReport -> error );
}
}
/**
* Delete paymentExpenseReport
*
* @ param int $id ID of payment ExpenseReport
* @ return array
*
* @ url DELETE { id } / payments
*/
/* public function delete ( $id )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'expensereport' , 'creer' ) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 );
2024-02-19 16:55:00 +00:00
}
$paymentExpenseReport = new PaymentExpenseReport ( $this -> db );
$result = $paymentExpenseReport -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'paymentExpenseReport not found' );
}
if ( $paymentExpenseReport -> delete ( DolibarrApiAccess :: $user ) < 0 ) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'error when deleting paymentExpenseReport' );
2024-02-19 16:55:00 +00:00
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'paymentExpenseReport deleted'
)
);
} */
2020-10-31 17:51:30 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
2025-11-20 00:47:46 +00:00
* @ phpstan - template T
2020-10-31 17:51:30 +00:00
*
* @ param Object $object Object to clean
* @ return Object Object with cleaned properties
2025-11-20 00:47:46 +00:00
* @ phpstan - param T $object
* @ phpstan - return T
2020-10-31 17:51:30 +00:00
*/
protected function _cleanObjectDatas ( $object )
{
// phpcs:enable
$object = parent :: _cleanObjectDatas ( $object );
unset ( $object -> fk_statut );
unset ( $object -> statut );
unset ( $object -> user );
unset ( $object -> thirdparty );
unset ( $object -> cond_reglement );
unset ( $object -> shipping_method_id );
unset ( $object -> barcode_type );
unset ( $object -> barcode_type_code );
unset ( $object -> barcode_type_label );
unset ( $object -> barcode_type_coder );
unset ( $object -> code_paiement );
unset ( $object -> code_statut );
unset ( $object -> fk_c_paiement );
unset ( $object -> fk_incoterms );
unset ( $object -> label_incoterms );
unset ( $object -> location_incoterms );
unset ( $object -> mode_reglement_id );
unset ( $object -> cond_reglement_id );
unset ( $object -> name );
unset ( $object -> lastname );
unset ( $object -> firstname );
unset ( $object -> civility_id );
unset ( $object -> cond_reglement_id );
unset ( $object -> contact );
unset ( $object -> contact_id );
unset ( $object -> state );
unset ( $object -> state_id );
unset ( $object -> state_code );
unset ( $object -> country );
unset ( $object -> country_id );
unset ( $object -> country_code );
unset ( $object -> note ); // We already use note_public and note_pricate
return $object ;
}
2017-12-01 14:39:18 +00:00
2020-10-31 17:51:30 +00:00
/**
* Validate fields before create or update object
*
2025-03-02 14:10:25 +00:00
* @ param ? array < string , string > $data Array with data to verify
* @ return array < string , string >
2020-10-31 17:51:30 +00:00
* @ throws RestException
*/
private function _validate ( $data )
{
2025-03-02 14:10:25 +00:00
if ( $data === null ) {
$data = array ();
}
2020-10-31 17:51:30 +00:00
$expensereport = array ();
foreach ( ExpenseReports :: $FIELDS as $field ) {
2021-02-25 21:45:02 +00:00
if ( ! isset ( $data [ $field ])) {
2020-10-31 17:51:30 +00:00
throw new RestException ( 400 , " $field field missing " );
2021-02-25 21:45:02 +00:00
}
2020-10-31 17:51:30 +00:00
$expensereport [ $field ] = $data [ $field ];
}
return $expensereport ;
}
2024-02-19 16:55:00 +00:00
/**
* Validate fields before create or update object
*
2025-03-02 14:10:25 +00:00
* @ param ? array < string , string > $data Array with data to verify
* @ return array < string , string >
2024-02-19 16:55:00 +00:00
* @ throws RestException
*/
private function _validatepayment ( $data )
{
2025-03-02 14:10:25 +00:00
if ( $data === null ) {
$data = array ();
}
2024-02-19 16:55:00 +00:00
$expensereport = array ();
foreach ( ExpenseReports :: $FIELDSPAYMENT as $field ) {
if ( ! isset ( $data [ $field ])) {
throw new RestException ( 400 , " $field field missing " );
}
$expensereport [ $field ] = $data [ $field ];
}
return $expensereport ;
}
2025-12-07 10:52:13 +00:00
/**
* Validate fields before create or update object
*
* @ param ? array < string , null | int | float | string > $data Data to validate
* @ return array < string , null | int | float | string > Return array with validated mandatory fields and their value
*
* @ throws RestException
*/
private function _validateLine ( $data )
{
if ( $data === null ) {
$data = array ();
}
$expenseReport = array ();
foreach ( ExpenseReports :: $FIELDSLINE as $field ) {
if ( ! isset ( $data [ $field ])) {
throw new RestException ( 400 , " $field field missing " );
}
$expenseReport [ $field ] = $data [ $field ];
}
return $expenseReport ;
}
2016-09-09 18:09:36 +00:00
}