2017-10-16 15:56:59 +00:00
< ? php
2023-09-26 16:43:25 +00:00
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
2020-10-31 08:27:58 +00:00
* Copyright ( C ) 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2025-11-20 00:47:46 +00:00
* Copyright ( C ) 2018 - 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 >
2017-10-16 15:56:59 +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 />.
2017-10-16 15:56:59 +00:00
*/
2025-03-02 11:13:32 +00:00
use Luracast\Restler\RestException ;
2017-10-16 15:56:59 +00:00
2025-03-02 11:13:32 +00:00
require_once DOL_DOCUMENT_ROOT . '/contrat/class/contrat.class.php' ;
2025-11-01 21:47:18 +00:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2017-10-16 15:56:59 +00:00
/**
* API class for contracts
*
* @ access protected
* @ class DolibarrApiAccess { @ requires user , external }
*/
class Contracts extends DolibarrApi
{
2020-10-31 17:51:30 +00:00
/**
2025-03-03 00:49:20 +00:00
* @ var string [] Mandatory fields , checked when create and update object
2020-10-31 17:51:30 +00:00
*/
2023-12-04 11:01:45 +00:00
public static $FIELDS = array (
2020-10-31 17:51:30 +00:00
'socid' ,
2026-04-20 10:50:40 +00:00
'date_contrat'
2020-10-31 17:51:30 +00:00
);
/**
2025-03-02 11:13:32 +00:00
* @ var Contrat { @ type Contrat }
2020-10-31 17:51:30 +00:00
*/
public $contract ;
/**
* Constructor
*/
public function __construct ()
{
2025-11-03 01:00:01 +00:00
global $db ;
2020-10-31 17:51:30 +00:00
$this -> db = $db ;
$this -> contract = new Contrat ( $this -> db );
}
/**
* Get properties of a contract object
*
2024-01-14 11:26:37 +00:00
* Return an array with contract information
2020-10-31 17:51:30 +00:00
*
2023-06-18 22:52:43 +00:00
* @ param int $id ID of contract
2025-08-01 22:35:16 +00:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
* @ param bool $withLines true or false to display or hide lines
2023-09-26 16:43:25 +00:00
* @ return Object Object with cleaned properties
2025-11-01 21:47:18 +00:00
*
* @ throws RestException 403 Access denied
* @ throws RestException 404 Not found
* @ throws RestException 503 Error
2020-10-31 17:51:30 +00:00
*/
2025-08-01 22:35:16 +00:00
public function get ( $id , $properties = '' , $withLines = true )
2020-10-31 17:51:30 +00:00
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'lire' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
2025-11-01 21:47:18 +00:00
if ( $id == 0 ) {
throw new RestException ( 400 , 'No contract with id=0 can exist' );
}
2020-10-31 17:51:30 +00:00
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contract not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
$this -> contract -> fetchObjectLinked ();
2025-08-01 22:35:16 +00:00
if ( ! $withLines ) {
unset ( $this -> contract -> lines );
}
return $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $this -> contract ), $properties );
2020-10-31 17:51:30 +00:00
}
/**
* List contracts
*
* Get a list of contracts
*
2023-09-26 16:43:25 +00:00
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
* @ param string $thirdparty_ids Thirdparty ids to filter contracts of ( 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') "
2024-01-14 11:26:37 +00:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
2024-08-20 08:18:15 +00:00
* @ 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 *
2025-08-01 22:35:16 +00:00
* @ param bool $withLines true or false to display or hide lines
2024-08-20 08:18:15 +00:00
* @ return array Array of order objects
2025-03-03 00:49:20 +00:00
* @ phan - return Contrat [] | array { data : Contrat [], pagination : array { total : int , page : int , page_count : int , limit : int }}
* @ phpstan - return Contrat [] | array { data : Contrat [], pagination : array { total : int , page : int , page_count : int , limit : int }}
2020-10-31 17:51:30 +00:00
*
2025-11-01 21:47:18 +00:00
* @ throws RestException 400 Bad Request
* @ throws RestException 403 Access denied
2020-10-31 17:51:30 +00:00
* @ throws RestException 503 Error
*/
2025-08-01 22:35:16 +00:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $thirdparty_ids = '' , $sqlfilters = '' , $properties = '' , $pagination_data = false , $withLines = true )
2020-10-31 17:51:30 +00:00
{
global $db , $conf ;
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , '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, $thirdparty_ids param is ignored and replaced by user's socid
2025-07-01 18:34:09 +00:00
$socids = DolibarrApiAccess :: $user -> socid ? : $thirdparty_ids ;
2020-10-31 17:51:30 +00:00
// If the internal user must only see his customers, force searching by him
$search_sale = 0 ;
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'client' , 'voir' ) && ! $socids ) {
2021-02-23 20:24:38 +00:00
$search_sale = DolibarrApiAccess :: $user -> id ;
}
2020-10-31 17:51:30 +00:00
$sql = " SELECT t.rowid " ;
2025-07-18 00:35:50 +00:00
$sql .= " FROM " . MAIN_DB_PREFIX . " contrat AS t " ;
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . " societe AS s ON (s.rowid = t.fk_soc) " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " contrat_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 ( 'contrat' ) . ')' ;
2021-02-23 20:24:38 +00:00
if ( $socids ) {
2021-03-22 10:30:18 +00:00
$sql .= " AND t.fk_soc IN ( " . $this -> db -> sanitize ( $socids ) . " ) " ;
2021-02-23 20:24:38 +00:00
}
2024-01-09 09:44:50 +00:00
// Search on sale representative
if ( $search_sale && $search_sale != '-1' ) {
if ( $search_sale == - 2 ) {
$sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . " societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc) " ;
} elseif ( $search_sale > 0 ) {
$sql .= " AND EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . " societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = " . (( int ) $search_sale ) . " ) " ;
}
2020-10-31 17:51:30 +00:00
}
// Add sql filters
2021-02-23 20:24:38 +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-08-20 08:18:15 +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 ) {
2021-02-23 20:24:38 +00:00
if ( $page < 0 ) {
2020-10-31 17:51:30 +00:00
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
$result = $this -> db -> query ( $sql );
2021-02-23 20:24:38 +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 ));
$i = 0 ;
2021-02-23 20:24:38 +00:00
while ( $i < $min ) {
2020-10-31 17:51:30 +00:00
$obj = $this -> db -> fetch_object ( $result );
$contrat_static = new Contrat ( $this -> db );
if ( $contrat_static -> fetch ( $obj -> rowid )) {
2025-08-01 22:35:16 +00:00
if ( ! $withLines ) {
unset ( $contrat_static -> lines );
}
2023-09-26 16:04:48 +00:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $contrat_static ), $properties );
2020-10-31 17:51:30 +00:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve contrat list : ' . $this -> db -> lasterror ());
}
2023-12-31 13:11:05 +00:00
2024-08-20 08:18:15 +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 ;
}
/**
* Create contract object
*
* @ 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
2020-10-31 17:51:30 +00:00
* @ return int ID of contrat
2025-11-01 21:47:18 +00:00
*
* @ throws RestException 400 Bad Request
* @ throws RestException 403 Access denied
* @ throws RestException 404 Not found
* @ throws RestException 500 Error
2020-10-31 17:51:30 +00:00
*/
public function post ( $request_data = null )
{
2025-11-03 01:00:01 +00:00
global $conf ;
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'creer' )) {
2025-10-31 19:46:50 +00:00
throw new RestException ( 403 , " Missing permission: Create/modify contracts/subscriptions " );
2020-10-31 17:51:30 +00:00
}
2025-10-31 19:46:50 +00:00
2026-04-23 11:15:21 +00:00
// Check mandatory fields
$this -> _validate ( $request_data );
2026-04-23 11:20:43 +00:00
// Check thirdparty validity
2025-10-31 19:46:50 +00:00
$socid = ( int ) $request_data [ 'socid' ];
2025-11-09 21:20:59 +00:00
$thirdpartytmp = new Societe ( $this -> db );
$thirdparty_result = $thirdpartytmp -> fetch ( $socid );
if ( $thirdparty_result < 1 ) {
throw new RestException ( 404 , 'Thirdparty with id=' . $socid . ' not found or not allowed' );
}
2025-11-24 20:14:24 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $thirdpartytmp -> id )) {
throw new RestException ( 404 , 'Thirdparty with id=' . $thirdpartytmp -> id . ' not found or not allowed' );
}
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-14 11:26:37 +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 -> contract -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2025-11-01 21:47:18 +00:00
if ( $field == 'id' ) {
throw new RestException ( 400 , 'Creating with id field is forbidden' );
}
2025-11-05 21:35:25 +00:00
if ( $field == 'entity' && (( int ) $value ) != (( int ) $conf -> entity )) {
throw new RestException ( 403 , 'Creating with entity=' . (( int ) $value ) . ' MUST be the same entity=' . (( int ) $conf -> entity ) . ' as your API user/key belongs to' );
2025-11-01 21:47:18 +00:00
}
if ( $field == 'socid' ) {
$thirdparty = new Societe ( $this -> db );
$result = $thirdparty -> fetch (( int ) $value );
if ( $result < 1 ) {
throw new RestException ( 404 , 'Thirdparty with id=' . (( int ) $value ) . ' not found' );
}
}
2023-12-15 11:15:33 +00:00
2024-04-02 10:28:55 +00:00
$this -> contract -> $field = $this -> _checkValForAPI ( $field , $value , $this -> contract );
2020-10-31 17:51:30 +00:00
}
/* if ( isset ( $request_data [ " lines " ])) {
2021-02-23 20:24:38 +00:00
$lines = array ();
foreach ( $request_data [ " lines " ] as $line ) {
array_push ( $lines , ( object ) $line );
}
$this -> contract -> lines = $lines ;
} */
2020-10-31 17:51:30 +00:00
if ( $this -> contract -> create ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , " Error creating contract " , array_merge ( array ( $this -> contract -> error ), $this -> contract -> errors ));
}
return $this -> contract -> id ;
}
/**
* Get lines of a contract
*
2025-03-31 17:52:36 +00:00
* @ param int $id Id of contract
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
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-03-31 17:52:36 +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 contrat det objects
* @ phan - return ContratLigne []
* @ phpstan - return ContratLigne []
2020-10-31 17:51:30 +00:00
*
* @ url GET { id } / lines
*
2025-03-31 17:52:36 +00:00
* @ throws RestException 404 Not Found
* @ throws RestException 503 Error
2020-10-31 17:51:30 +00:00
*/
2025-03-31 17:52:36 +00:00
public function getLines ( $id , $sortfield = " d.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $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 ( 'contrat' , 'lire' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contract not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
2025-03-31 17:52:36 +00:00
$obj_ret = [];
$sql = " SELECT d.rowid " ;
$sql .= " FROM " . $this -> db -> prefix () . " contratdet AS d " ;
$sql .= " LEFT JOIN " . $this -> db -> prefix () . " contrat AS c ON (c.rowid = d.fk_contrat) " ;
$sql .= " LEFT JOIN " . $this -> db -> prefix () . " contratdet_extrafields AS ef ON (ef.fk_object = d.rowid) " ;
$sql .= " WHERE d.fk_contrat = " . (( int ) $id );
$sql .= ' AND c.entity IN (' . getEntity ( 'contrat' ) . ')' ;
if ( $sqlfilters ) {
$errormessage = '' ;
$sql .= forgeSQLFromUniversalSearchCriteria ( $sqlfilters , $errormessage );
if ( $errormessage ) {
throw new RestException ( 400 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
}
}
$sqlTotals = str_replace ( 'SELECT d.rowid' , 'SELECT count(d.rowid) as total' , $sql );
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
if ( $page < 0 ) {
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
2020-10-31 17:51:30 +00:00
}
2025-03-31 17:52:36 +00:00
$result = $this -> db -> query ( $sql );
if ( $result ) {
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
$i = 0 ;
while ( $i < $min ) {
$obj = $this -> db -> fetch_object ( $result );
$contratdet_static = new ContratLigne ( $this -> db );
if ( $contratdet_static -> fetch ( $obj -> rowid )) {
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $contratdet_static ), $properties );
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve contratdet list : ' . $this -> db -> lasterror ());
}
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 ,
'page_count' => ceil (( int ) $total / $limit ),
'limit' => $limit
];
}
return $obj_ret ;
2020-10-31 17:51:30 +00:00
}
/**
* Add a line to given contract
*
* @ param int $id Id of contrat to update
* @ param array $request_data Contractline 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 17:51:30 +00:00
*
* @ url POST { id } / lines
*
* @ return int | bool
*/
public function postLine ( $id , $request_data = null )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'creer' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contract not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
2021-04-25 17:21:48 +00:00
2020-10-31 17:51:30 +00:00
$request_data = ( object ) $request_data ;
2021-04-25 17:21:48 +00:00
2022-03-30 10:16:17 +00:00
$request_data -> desc = sanitizeVal ( $request_data -> desc , 'restricthtml' );
$request_data -> price_base_type = sanitizeVal ( $request_data -> price_base_type );
2021-04-25 17:21:48 +00:00
2020-10-31 17:51:30 +00:00
$updateRes = $this -> contract -> addline (
$request_data -> desc ,
$request_data -> subprice ,
$request_data -> qty ,
$request_data -> tva_tx ,
$request_data -> localtax1_tx ,
$request_data -> localtax2_tx ,
$request_data -> fk_product ,
$request_data -> remise_percent ,
2022-10-07 10:02:53 +00:00
$request_data -> date_start ,
$request_data -> date_end ,
2021-04-25 17:21:48 +00:00
$request_data -> price_base_type ? $request_data -> price_base_type : 'HT' ,
2020-10-31 17:51:30 +00:00
$request_data -> subprice_excl_tax ,
$request_data -> info_bits ,
$request_data -> fk_fournprice ,
$request_data -> pa_ht ,
$request_data -> array_options ,
$request_data -> fk_unit ,
$request_data -> rang
);
if ( $updateRes > 0 ) {
return $updateRes ;
}
return false ;
}
/**
* Update a line to given contract
*
* @ param int $id Id of contrat to update
* @ param int $lineid Id of line to update
* @ param array $request_data Contractline 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 17:51:30 +00:00
*
* @ url PUT { id } / lines / { lineid }
*
2020-11-25 17:12:59 +00:00
* @ return Object | bool
2020-10-31 17:51:30 +00:00
*/
public function putLine ( $id , $lineid , $request_data = null )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'creer' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contrat not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
$request_data = ( object ) $request_data ;
2022-03-30 10:16:17 +00:00
$request_data -> desc = sanitizeVal ( $request_data -> desc , 'restricthtml' );
$request_data -> price_base_type = sanitizeVal ( $request_data -> price_base_type );
2021-04-25 17:21:48 +00:00
2020-10-31 17:51:30 +00:00
$updateRes = $this -> contract -> updateline (
$lineid ,
$request_data -> desc ,
$request_data -> subprice ,
$request_data -> qty ,
$request_data -> remise_percent ,
2022-10-07 10:02:53 +00:00
$request_data -> date_start ,
$request_data -> date_end ,
2020-10-31 17:51:30 +00:00
$request_data -> tva_tx ,
$request_data -> localtax1_tx ,
$request_data -> localtax2_tx ,
2022-10-07 10:02:53 +00:00
$request_data -> date_start_real ,
$request_data -> date_end_real ,
2021-04-25 17:21:48 +00:00
$request_data -> price_base_type ? $request_data -> price_base_type : 'HT' ,
2020-10-31 17:51:30 +00:00
$request_data -> info_bits ,
$request_data -> fk_fourn_price ,
$request_data -> pa_ht ,
$request_data -> array_options ,
$request_data -> fk_unit
);
if ( $updateRes > 0 ) {
2025-04-12 14:41:34 +00:00
if ( getDolGlobalInt ( 'API_CONTRAT_PUTLINE_OUTPUT_LINE_ONLY' )) {
$result = new ContratLigne ( $this -> db );
$result -> fetch ( $lineid );
foreach ( array (
'array_languages' ,
'contacts_ids' ,
'linked_objects' ,
'linkedObjectsIds' ,
'actiontypecode' ,
'module' ,
'canvas' ,
'user' ,
'origin' ,
'origin_id' ,
'ref_ext' ,
'status' ,
'country_id' ,
'country_code' ,
'state_id' ,
'region_id' ,
'barcode_type' ,
'barcode_type_coder' ,
'mode_reglement_id' ,
'cond_reglement_id' ,
'demand_reason_id' ,
'transport_mode_id' ,
'shipping_method' ,
'shipping_method_id' ,
'model_pdf' ,
'last_main_doc' ,
'fk_bank' ,
'fk_account' ,
'lines' ,
'name' ,
'firstname' ,
'lastname' ,
'date_creation' ,
'date_validation' ,
'date_modification' ,
'date_cloture' ,
'user_author' ,
'user_creation' ,
'user_creation_id' ,
'user_valid' ,
'user_validation' ,
'user_validation_id' ,
'user_modification' ,
'user_modification_id' ,
'cond_reglement_supplier_id' ,
'deposit_percent' ,
'retained_warranty_fk_cond_reglement' ,
'date_commande' ,
'fk_user_creat' ,
'fk_user_modif' ,
'specimen' ,
'fk_unit' ,
'date_debut_prevue' ,
'date_debut_reel' ,
'date_fin_prevue' ,
'date_fin_reel' ,
'weight' ,
'weight_units' ,
'width' ,
'width_units' ,
'length' ,
'length_units' ,
'height' ,
'height_units' ,
'surface' ,
'surface_units' ,
'volume' ,
'volume_units' ,
'multilangs' ,
'desc' ,
'product' ,
'fk_product_type' ,
'warehouse_id' ,
'totalpaid' ,
'type' ,
'libelle'
) as $fieldToUnset ) {
unset ( $result -> { $fieldToUnset });
}
} else {
$result = $this -> get ( $id );
unset ( $result -> line );
}
2020-10-31 17:51:30 +00:00
return $this -> _cleanObjectDatas ( $result );
2025-09-02 22:02:21 +00:00
} else {
throw new RestException ( 500 , implode ( ';' , $this -> contract -> errors ));
2020-10-31 17:51:30 +00:00
}
}
/**
* Activate a service line of a given contract
*
2023-09-26 16:43:25 +00:00
* @ param int $id Id of contract to activate
* @ param int $lineid Id of line to activate
* @ param string $datestart { @ from body } Date start { @ type timestamp }
2020-10-31 17:51:30 +00:00
* @ param string $dateend { @ from body } Date end { @ type timestamp }
2023-09-26 16:43:25 +00:00
* @ param string $comment { @ from body } Comment
2020-10-31 17:51:30 +00:00
*
* @ url PUT { id } / lines / { lineid } / activate
*
2020-11-25 17:12:59 +00:00
* @ return Object | bool
2025-11-01 21:47:18 +00:00
*
* @ throws RestException 403 Access denied
* @ throws RestException 404 Not found
2020-10-31 17:51:30 +00:00
*/
public function activateLine ( $id , $lineid , $datestart , $dateend = null , $comment = null )
{
2025-11-01 21:47:18 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'activer' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contrat not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
2025-03-03 13:23:57 +00:00
$updateRes = $this -> contract -> active_line ( DolibarrApiAccess :: $user , $lineid , ( int ) $datestart , $dateend , $comment );
2020-10-31 17:51:30 +00:00
if ( $updateRes > 0 ) {
$result = $this -> get ( $id );
unset ( $result -> line );
return $this -> _cleanObjectDatas ( $result );
}
return false ;
}
/**
* Unactivate a service line of a given contract
*
2023-09-26 16:43:25 +00:00
* @ param int $id Id of contract to activate
* @ param int $lineid Id of line to activate
* @ param string $datestart { @ from body } Date start { @ type timestamp }
* @ param string $comment { @ from body } Comment
2020-10-31 17:51:30 +00:00
*
* @ url PUT { id } / lines / { lineid } / unactivate
*
2020-11-25 17:12:59 +00:00
* @ return Object | bool
2025-11-01 21:47:18 +00:00
*
* @ throws RestException 403 Access denied
* @ throws RestException 404 Not found
2020-10-31 17:51:30 +00:00
*/
public function unactivateLine ( $id , $lineid , $datestart , $comment = null )
{
2025-11-01 21:47:18 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'desactiver' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contrat not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
2025-03-03 13:23:57 +00:00
$updateRes = $this -> contract -> close_line ( DolibarrApiAccess :: $user , $lineid , ( int ) $datestart , $comment );
2020-10-31 17:51:30 +00:00
if ( $updateRes > 0 ) {
$result = $this -> get ( $id );
unset ( $result -> line );
return $this -> _cleanObjectDatas ( $result );
}
return false ;
}
/**
* Delete a line to given contract
*
*
* @ param int $id Id of contract to update
* @ param int $lineid Id of line to delete
*
* @ url DELETE { id } / lines / { lineid }
*
* @ return array | mixed
*
* @ throws RestException 401
* @ throws RestException 404
*/
public function deleteLine ( $id , $lineid )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'creer' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contrat not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
// TODO Check the lineid $lineid is a line of object
2024-02-02 22:46:12 +00:00
$updateRes = $this -> contract -> deleteLine ( $lineid , DolibarrApiAccess :: $user );
2020-10-31 17:51:30 +00:00
if ( $updateRes > 0 ) {
return $this -> get ( $id );
} else {
2023-12-04 11:01:45 +00:00
throw new RestException ( 405 , $this -> contract -> error );
2020-10-31 17:51:30 +00:00
}
}
/**
* Update contract general fields ( won ' t touch lines of contract )
*
2024-02-22 00:32:55 +00:00
* @ param int $id Id of contract to update
2025-03-02 11:13:32 +00:00
* @ param array $request_data Data
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-02-22 00:32:55 +00:00
* @ return Object Updated object
2025-11-01 21:47:18 +00:00
*
* @ throws RestException 400 Bad Request
* @ throws RestException 403 Access denied
* @ throws RestException 404 Not found
* @ throws RestException 500 Error
2020-10-31 17:51:30 +00:00
*/
public function put ( $id , $request_data = null )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'creer' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
2025-11-01 21:47:18 +00:00
if ( $id == 0 ) {
throw new RestException ( 400 , 'No contract with id=0 can exist' );
}
2025-12-04 19:38:22 +00:00
2020-10-31 17:51:30 +00:00
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contrat not found' );
}
2025-11-02 10:22:45 +00:00
$old_socid = $this -> contract -> socid ;
2025-11-09 21:20:59 +00:00
$oldthirdpartytmp = new Societe ( $this -> db );
$old_thirdparty_result = $oldthirdpartytmp -> fetch ( $old_socid );
if ( $old_thirdparty_result < 1 ) {
throw new RestException ( 404 , 'Thirdparty with id=' . $old_socid . ' not found or not allowed' );
}
2025-12-03 20:52:35 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $old_socid )) {
throw new RestException ( 403 , 'Access to old thirdparty=' . $old_socid . ' is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2025-11-09 21:20:59 +00:00
}
2025-10-31 18:55:17 +00:00
2020-10-31 17:51:30 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
foreach ( $request_data as $field => $value ) {
2021-02-23 20:24:38 +00:00
if ( $field == 'id' ) {
2025-11-01 21:47:18 +00:00
throw new RestException ( 400 , 'Updating with id field is forbidden' );
}
if ( $field == 'entity' && $value != $this -> contract -> entity ) {
throw new RestException ( 400 , 'Changing entity of a contract using the APIs is not possible' );
2021-02-23 20:24:38 +00:00
}
2023-12-15 11:15:33 +00:00
if ( $field === 'caller' ) {
2024-01-14 11:26:37 +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 -> contract -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2025-11-01 21:47:18 +00:00
if ( $field == 'socid' ) {
$thirdparty = new Societe ( $this -> db );
$result = $thirdparty -> fetch (( int ) $value );
if ( $result < 1 ) {
throw new RestException ( 404 , 'Thirdparty with id=' . (( int ) $value ) . ' not found' );
}
}
2024-04-01 09:02:29 +00:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
2026-05-08 23:16:35 +00:00
$this -> contract -> array_options [ $index ] = $this -> _checkValExtrafieldsForAPI ( $index , $val , $this -> contract );
2024-04-01 09:02:29 +00:00
}
continue ;
}
2023-12-15 11:15:33 +00:00
2025-11-02 10:22:45 +00:00
if ( $field == 'socid' ) {
2025-11-09 21:20:59 +00:00
$new_socid = ( int ) $value ;
$loopthirdpartytmp = new Societe ( $this -> db );
$new_thirdparty_result = $loopthirdpartytmp -> fetch ( $new_socid );
if ( $new_thirdparty_result < 1 ) {
throw new RestException ( 404 , 'Thirdparty with id=' . $new_socid . ' not found or not allowed' );
2025-11-02 10:22:45 +00:00
}
2025-12-03 20:52:35 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $new_socid )) {
throw new RestException ( 403 , 'Access to new thirdparty=' . $new_socid . ' is not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2025-11-02 10:22:45 +00:00
}
2024-04-02 10:28:55 +00:00
$this -> contract -> $field = $this -> _checkValForAPI ( $field , $value , $this -> contract );
2020-10-31 17:51:30 +00:00
}
if ( $this -> contract -> update ( DolibarrApiAccess :: $user ) > 0 ) {
return $this -> get ( $id );
} else {
throw new RestException ( 500 , $this -> contract -> error );
}
}
/**
* Delete contract
*
* @ param int $id Contract 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-11-01 21:47:18 +00:00
*
* @ throws RestException 400 Bad Request
* @ throws RestException 403 Access denied
* @ throws RestException 404 Not found
* @ throws RestException 500 Error
2020-10-31 17:51:30 +00:00
*/
public function delete ( $id )
{
2023-06-19 21:11:08 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'supprimer' )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Missing permission: Delete contracts/subscriptions' );
2020-10-31 17:51:30 +00:00
}
2025-11-01 21:47:18 +00:00
if ( $id == 0 ) {
throw new RestException ( 400 , 'No contract with id=0 can exist' );
}
2020-10-31 17:51:30 +00:00
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contract not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
if ( ! $this -> contract -> delete ( DolibarrApiAccess :: $user )) {
throw new RestException ( 500 , 'Error when delete contract : ' . $this -> contract -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'Contract deleted'
)
);
}
/**
* Validate a contract
*
* @ param int $id Contract ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
*
* @ url POST { id } / validate
*
* @ 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 }}
*
2020-10-31 17:51:30 +00:00
* FIXME An error 403 is returned if the request has an empty body .
* Error message : " Forbidden: Content type `text/plain` is not supported. "
* Workaround : send this in the body
* {
* " notrigger " : 0
* }
2025-11-01 21:47:18 +00:00
*
* @ throws RestException 304 Not Modified
* @ throws RestException 403 Access denied
* @ throws RestException 404 Not found
* @ throws RestException 500 Error
2020-10-31 17:51:30 +00:00
*/
public function validate ( $id , $notrigger = 0 )
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'creer' )) {
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
2025-11-01 21:47:18 +00:00
if ( $id == 0 ) {
throw new RestException ( 400 , 'No contract with id=0 can exist' );
}
2020-10-31 17:51:30 +00:00
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contract not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contract -> validate ( 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 Contract: ' . $this -> contract -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'Contract validated (Ref=' . $this -> contract -> ref . ')'
)
);
}
/**
* Close all services of a contract
*
* @ param int $id Contract ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
*
* @ url POST { id } / close
*
* @ 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 }}
*
2020-10-31 17:51:30 +00:00
* FIXME An error 403 is returned if the request has an empty body .
* Error message : " Forbidden: Content type `text/plain` is not supported. "
* Workaround : send this in the body
* {
* " notrigger " : 0
* }
2025-11-01 21:47:18 +00:00
*
* @ throws RestException 304 Not Modified
* @ throws RestException 403 Access denied
* @ throws RestException 404 Not found
* @ throws RestException 500 Error
2020-10-31 17:51:30 +00:00
*/
public function close ( $id , $notrigger = 0 )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'contrat' , 'creer' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
2025-11-01 21:47:18 +00:00
if ( $id == 0 ) {
throw new RestException ( 400 , 'No contract with id=0 can exist' );
}
2020-10-31 17:51:30 +00:00
$result = $this -> contract -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contract not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contrat' , $this -> contract -> id )) {
2025-11-09 21:20:59 +00:00
throw new RestException ( 403 , 'Access to this contract is not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contract -> closeAll ( DolibarrApiAccess :: $user , $notrigger );
if ( $result == 0 ) {
throw new RestException ( 304 , 'Error nothing done. May be object is already close' );
}
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when closing Contract: ' . $this -> contract -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'Contract closed (Ref=' . $this -> contract -> ref . '). All services were closed.'
)
);
}
// 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 -> address );
unset ( $object -> civility_id );
return $object ;
}
/**
* 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 >
2025-11-01 21:47:18 +00:00
*
* @ throws RestException 400 Bad Request
2020-10-31 17:51:30 +00:00
*/
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
$contrat = array ();
foreach ( Contracts :: $FIELDS as $field ) {
2021-02-23 20:24:38 +00:00
if ( ! isset ( $data [ $field ])) {
2020-10-31 17:51:30 +00:00
throw new RestException ( 400 , " $field field missing " );
2021-02-23 20:24:38 +00:00
}
2020-10-31 17:51:30 +00:00
$contrat [ $field ] = $data [ $field ];
}
return $contrat ;
}
2017-10-16 15:56:59 +00:00
}