2018-01-27 17:59:07 +00:00
< ? php
2026-01-24 12:42:54 +00:00
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
* Copyright ( C ) 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2026-03-25 13:11:24 +00:00
* Copyright ( C ) 2025 - 2026 William Mead < william @ m34d . com >
2026-01-24 12:42:54 +00:00
* Copyright ( C ) 2025 - 2026 Charlene Benke < charlene @ patas - monkey . com >
2025-09-28 18:56:54 +00:00
* Copyright ( C ) 2025 Frédéric France < frederic . france @ free . fr >
2018-01-27 17:59:07 +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 />.
2018-01-27 17:59:07 +00:00
*/
2023-03-12 12:55:33 +00:00
/**
* \file htdocs / fichinter / class / api_interventions . class . php
* \ingroup fichinter
* \brief File of API to manage intervention
*/
use Luracast\Restler\RestException ;
require_once DOL_DOCUMENT_ROOT . '/fichinter/class/fichinter.class.php' ;
2025-09-28 20:41:42 +00:00
include_once DOL_DOCUMENT_ROOT . " /fichinter/class/fichinterligne.class.php " ;
2018-01-27 17:59:07 +00:00
/**
2020-04-12 14:36:25 +00:00
* API class for Interventions
2018-01-27 17:59:07 +00:00
*
2025-05-04 09:24:38 +00:00
* @ since 7.0 . 0 Initial implementation
*
* @ access protected
* @ class DolibarrApiAccess { @ requires user , external }
2018-01-27 17:59:07 +00:00
*/
2018-02-03 19:58:26 +00:00
class Interventions extends DolibarrApi
2018-01-27 17:59:07 +00:00
{
2020-10-31 13:32:18 +00:00
/**
2025-01-29 00:03:16 +00:00
* @ var string [] Mandatory fields , checked when create and update object
2020-10-31 13:32:18 +00:00
*/
2021-02-25 22:00:32 +00:00
public static $FIELDS = array (
2025-01-29 00:03:16 +00:00
'socid' ,
'fk_project' ,
'description' ,
2020-10-31 13:32:18 +00:00
);
/**
2025-01-29 00:03:16 +00:00
* @ var string [] Mandatory fields , checked when create and update object
2020-10-31 13:32:18 +00:00
*/
2021-02-25 22:00:32 +00:00
public static $FIELDSLINE = array (
2025-01-29 00:03:16 +00:00
'description' ,
'date' ,
2025-08-28 15:29:04 +00:00
'duration' ,
2020-10-31 13:32:18 +00:00
);
/**
2025-01-20 23:33:58 +00:00
* @ var Fichinter { @ type fichinter }
2020-10-31 13:32:18 +00:00
*/
public $fichinter ;
/**
* Constructor
*/
public function __construct ()
{
2025-05-05 00:22:02 +00:00
global $db ;
2020-10-31 13:32:18 +00:00
$this -> db = $db ;
$this -> fichinter = new Fichinter ( $this -> db );
}
/**
2025-05-04 09:24:38 +00:00
* Get an intervention
2025-04-20 17:00:04 +00:00
* Return an array with intervention information
2020-10-31 13:32:18 +00:00
*
2025-05-04 09:24:38 +00:00
* @ since 7.0 . 0 Initial implementation
*
2025-05-05 00:22:02 +00:00
* @ param int $id ID of intervention
* @ param string $ref Ref of object
* @ param string $ref_ext External reference of object
2025-05-05 02:36:28 +00:00
* @ param int $contact_list 0 : Returned array of contacts / addresses contains all properties , 1 : Return array contains just id , - 1 : Do not return contacts / adddesses
2025-05-04 09:24:38 +00:00
* @ return Object Cleaned intervention object
2020-10-31 13:32:18 +00:00
*
2025-04-29 04:40:04 +00:00
* @ throws RestException
2020-10-31 13:32:18 +00:00
*/
2025-05-05 02:36:28 +00:00
public function get ( $id , $ref = '' , $ref_ext = '' , $contact_list = 1 )
2020-10-31 13:32:18 +00:00
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'lire' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 13:32:18 +00:00
}
2025-05-05 00:22:02 +00:00
$result = $this -> fichinter -> fetch ( $id , $ref , $ref_ext );
2020-10-31 13:32:18 +00:00
if ( ! $result ) {
throw new RestException ( 404 , 'Intervention not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'fichinter' , $this -> fichinter -> id )) {
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
}
2025-05-05 00:22:02 +00:00
if ( $contact_list > - 1 ) {
// Add external contacts ids
$tmparray = $this -> fichinter -> liste_contact ( - 1 , 'external' , $contact_list );
if ( is_array ( $tmparray )) {
$this -> fichinter -> contacts_ids = $tmparray ;
}
$tmparray = $this -> fichinter -> liste_contact ( - 1 , 'internal' , $contact_list );
if ( is_array ( $tmparray )) {
$this -> fichinter -> contacts_ids_internal = $tmparray ;
}
2025-04-20 17:00:04 +00:00
}
2025-05-05 00:22:02 +00:00
$this -> fichinter -> fetchObjectLinked ();
2020-10-31 13:32:18 +00:00
return $this -> _cleanObjectDatas ( $this -> fichinter );
}
/**
2025-05-04 09:24:38 +00:00
* List interventions
*
* Get a list of interventions
*
* @ since 7.0 . 0 Initial implementation
*
* @ 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 orders 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') "
2025-05-04 09:24:38 +00:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of property names
2025-05-13 09:49:26 +00:00
* @ param string $contact_type Type of contacts : thirdparty , internal or external
2025-05-12 18:06:38 +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-09-19 23:11:19 +00:00
* @ param int $loadlinkedobjects Load also linked objects
2025-05-04 09:24:38 +00:00
* @ return array Array of order objects
2025-01-29 00:03:16 +00:00
* @ phan - return array < object >
* @ phpstan - return array < object >
2020-10-31 13:32:18 +00:00
*
* @ throws RestException
*/
2025-09-19 23:11:19 +00:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $thirdparty_ids = '' , $sqlfilters = '' , $properties = '' , $contact_type = '' , $pagination_data = false , $loadlinkedobjects = 0 )
2020-10-31 13:32:18 +00:00
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'lire' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2021-04-08 17:05:28 +00:00
}
2025-09-21 21:23:39 +00:00
global $hookmanager ;
2020-10-31 13:32:18 +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 13:32:18 +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-25 22:00:32 +00:00
$search_sale = DolibarrApiAccess :: $user -> id ;
}
2020-10-31 13:32:18 +00:00
$sql = " SELECT t.rowid " ;
2025-07-18 00:35:50 +00:00
$sql .= " FROM " . MAIN_DB_PREFIX . " fichinter AS t " ;
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . " societe AS s ON (s.rowid = t.fk_soc) " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " fichinter_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 13:32:18 +00:00
$sql .= ' WHERE t.entity IN (' . getEntity ( 'intervention' ) . ')' ;
2021-02-25 22:00:32 +00:00
if ( $socids ) {
2021-03-22 10:30:18 +00:00
$sql .= " AND t.fk_soc IN ( " . $this -> db -> sanitize ( $socids ) . " ) " ;
2021-02-25 22:00:32 +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 13:32:18 +00:00
}
2025-09-21 21:23:39 +00:00
2021-02-25 22:00:32 +00:00
if ( $sqlfilters ) {
2025-09-23 21:22:57 +00:00
$parameters = array ( 'sqlfilters' => $sqlfilters , 'apiroute' => 'interventions' , 'apimethod' => __METHOD__ );
2025-09-21 21:23:39 +00:00
$action = 'list' ;
2025-09-23 21:22:57 +00:00
$reshook = $hookmanager -> executeHooks ( 'printFieldListWhere' , $parameters , $this -> fichinter , $action ); // Note that $action and $object may have been modified by hook
2025-09-21 21:23:39 +00:00
if ( $reshook > 0 ) {
2025-09-28 18:56:54 +00:00
$sql = $hookmanager -> resPrint ;
2025-09-21 21:23:39 +00:00
} elseif ( $reshook == 0 ) {
$sql .= $hookmanager -> resPrint ;
2025-09-28 18:56:54 +00:00
}
2025-09-21 21:23:39 +00:00
2025-09-28 18:56:54 +00:00
$errormessage = '' ;
$sql .= forgeSQLFromUniversalSearchCriteria ( $sqlfilters , $errormessage );
if ( $errormessage ) {
throw new RestException ( 400 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
2020-10-31 13:32:18 +00:00
}
}
2025-05-12 18:06:38 +00:00
//this query will return total interventions with the filters given
$sqlTotals = str_replace ( 'SELECT t.rowid' , 'SELECT count(t.rowid) as total' , $sql );
2020-10-31 13:32:18 +00:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
2021-02-25 22:00:32 +00:00
if ( $page < 0 ) {
2020-10-31 13:32:18 +00:00
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
dol_syslog ( " API Rest request " );
$result = $this -> db -> query ( $sql );
2021-02-25 22:00:32 +00:00
if ( $result ) {
2020-10-31 13:32:18 +00:00
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
$i = 0 ;
2021-02-25 22:00:32 +00:00
while ( $i < $min ) {
2020-10-31 13:32:18 +00:00
$obj = $this -> db -> fetch_object ( $result );
$fichinter_static = new Fichinter ( $this -> db );
if ( $fichinter_static -> fetch ( $obj -> rowid )) {
2025-04-20 17:00:04 +00:00
if ( $contact_type ) {
$fichinter_static -> contacts_ids = $fichinter_static -> liste_contact ( - 1 , $contact_type , 1 );
}
2025-09-19 23:11:19 +00:00
if ( $loadlinkedobjects ) {
// retrieve linked objects
$fichinter_static -> fetchObjectLinked ();
}
2023-09-26 16:04:48 +00:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $fichinter_static ), $properties );
2020-10-31 13:32:18 +00:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve intervention list : ' . $this -> db -> lasterror ());
}
2023-12-31 13:11:05 +00:00
2025-05-12 18:06:38 +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 13:32:18 +00:00
return $obj_ret ;
}
/**
2025-05-04 09:24:38 +00:00
* Create an intervention
*
* @ since 7.0 . 0 Initial implementation
2020-10-31 13:32:18 +00:00
*
2025-05-04 09:24:38 +00:00
* @ param array $request_data Request data
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
* @ return int ID of created intervention
*
* @ throws RestException
2020-10-31 13:32:18 +00:00
*/
public function post ( $request_data = null )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'creer' )) {
2025-09-11 10:25:22 +00:00
throw new RestException ( 403 , " Insufficiant rights " );
2020-10-31 13:32:18 +00:00
}
// Check mandatory fields
$result = $this -> _validate ( $request_data );
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 -> fichinter -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2024-04-02 10:28:55 +00:00
$this -> fichinter -> $field = $this -> _checkValForAPI ( $field , $value , $this -> fichinter );
2020-10-31 13:32:18 +00:00
}
if ( $this -> fichinter -> create ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , " Error creating intervention " , array_merge ( array ( $this -> fichinter -> error ), $this -> fichinter -> errors ));
}
return $this -> fichinter -> id ;
}
2025-04-15 23:05:03 +00:00
/**
2025-05-04 09:24:38 +00:00
* Update intervention general fields ( won ' t touch lines of fichinter )
*
* @ since 22.0 . 0 Initial implementation
2025-04-15 23:05:03 +00:00
*
2025-05-04 09:24:38 +00:00
* @ param int $id ID of fichinter to update
* @ param array $request_data Request data
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
* @ return Object Updated object
*
* @ throws RestException
2025-04-15 23:05:03 +00:00
*/
public function put ( $id , $request_data = null )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'creer' )) {
throw new RestException ( 403 );
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Fichinter not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'fichinter' , $this -> fichinter -> id )) {
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
foreach ( $request_data as $field => $value ) {
if ( $field == 'id' ) {
continue ;
}
if ( $field === 'caller' ) {
// 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
$this -> fichinter -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
continue ;
}
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
2026-05-08 23:16:35 +00:00
$this -> fichinter -> array_options [ $index ] = $this -> _checkValExtrafieldsForAPI ( $index , $val , $this -> fichinter );
2025-04-15 23:05:03 +00:00
}
continue ;
}
$this -> fichinter -> $field = $this -> _checkValForAPI ( $field , $value , $this -> fichinter );
}
if ( $this -> fichinter -> update ( DolibarrApiAccess :: $user ) > 0 ) {
return $this -> get ( $id );
} else {
throw new RestException ( 500 , $this -> fichinter -> error );
}
}
2020-10-31 13:32:18 +00:00
/**
2025-04-20 17:00:04 +00:00
* Get lines of intervention
2020-10-31 13:32:18 +00:00
*
* @ param int $id Id of intervention
*
* @ url GET { id } / lines
*
* @ return int
*/
/* TODO
2021-02-25 22:00:32 +00:00
public function getLines ( $id )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'lire' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2021-02-25 22:00:32 +00:00
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Intervention not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'fichinter' , $this -> fichinter -> id )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2021-02-25 22:00:32 +00:00
}
$this -> fichinter -> getLinesArray ();
$result = array ();
foreach ( $this -> fichinter -> lines as $line ) {
array_push ( $result , $this -> _cleanObjectDatas ( $line ));
}
return $result ;
}
*/
2018-02-03 19:58:26 +00:00
2020-10-31 17:51:30 +00:00
/**
2025-05-04 09:24:38 +00:00
* Add a line to an intervention
*
* @ since 7.0 . 0 Initial implementation
2020-10-31 17:51:30 +00:00
*
2025-05-04 09:24:38 +00:00
* @ param int $id ID of intervention to update
* @ param array $request_data Request data
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2020-10-31 17:51:30 +00:00
*
2025-05-04 09:24:38 +00:00
* @ url POST { id } / lines
2020-10-31 17:51:30 +00:00
*
2025-05-04 09:24:38 +00:00
* @ return int 0 if ok , < 0 if ko
*
* @ throws RestException
2020-10-31 17:51:30 +00:00
*/
public function postLine ( $id , $request_data = null )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'creer' )) {
2025-09-11 10:25:22 +00:00
throw new RestException ( 403 , " Insufficiant rights " );
2020-10-31 17:51:30 +00:00
}
// Check mandatory fields
$result = $this -> _validateLine ( $request_data );
2018-01-27 17:59:07 +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 -> fichinter -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2024-04-02 10:28:55 +00:00
$this -> fichinter -> $field = $this -> _checkValForAPI ( $field , $value , $this -> fichinter );
2020-10-31 17:51:30 +00:00
}
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
if ( ! $result ) {
throw new RestException ( 404 , 'Intervention not found' );
}
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'fichinter' , $this -> fichinter -> id )) {
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
}
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
$updateRes = $this -> fichinter -> addLine (
2021-02-25 22:00:32 +00:00
DolibarrApiAccess :: $user ,
$id ,
$this -> fichinter -> description ,
$this -> fichinter -> date ,
2025-01-20 23:33:58 +00:00
$this -> fichinter -> duration
2020-10-31 17:51:30 +00:00
);
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
if ( $updateRes > 0 ) {
return $updateRes ;
} else {
throw new RestException ( 400 , $this -> fichinter -> error );
}
}
2019-02-25 19:35:59 +00:00
2020-10-31 17:51:30 +00:00
/**
2025-05-04 09:24:38 +00:00
* Delete an intervention
*
* @ since 8.0 . 0 Initial implementation
2020-10-31 17:51:30 +00:00
*
2025-05-04 09:24:38 +00:00
* @ param int $id Intervention ID
* @ return array
2025-01-29 00:03:16 +00:00
* @ phan - return array < string , array { code : int , message : string } >
* @ phpstan - return array < string , array { code : int , message : string } >
2025-05-04 09:24:38 +00:00
*
* @ throws RestException
2020-10-31 17:51:30 +00:00
*/
public function delete ( $id )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'supprimer' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Intervention not found' );
}
2019-02-25 19:35:59 +00:00
2020-10-31 17:51:30 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'fichinter' , $this -> fichinter -> id )) {
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
}
2019-02-25 19:35:59 +00:00
2020-10-31 17:51:30 +00:00
if ( ! $this -> fichinter -> delete ( DolibarrApiAccess :: $user )) {
throw new RestException ( 500 , 'Error when delete intervention : ' . $this -> fichinter -> error );
}
2018-02-05 19:19:00 +00:00
2020-10-31 17:51:30 +00:00
return array (
'success' => array (
'code' => 200 ,
'message' => 'Intervention deleted'
)
);
}
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
/**
* Validate an intervention
*
* If you get a bad value for param notrigger check , provide this in body
* {
* " notrigger " : 0
* }
*
2025-05-04 09:24:38 +00:00
* @ since 7.0 . 0 Initial implementation
2020-10-31 17:51:30 +00:00
*
2025-05-04 09:24:38 +00:00
* @ param int $id Intervention ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
2020-10-31 17:51:30 +00:00
*
2025-05-04 09:24:38 +00:00
* @ url POST { id } / validate
*
* @ return Object
*
* @ throws RestException
2020-10-31 17:51:30 +00:00
*/
public function validate ( $id , $notrigger = 0 )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'creer' )) {
2025-09-11 10:25:22 +00:00
throw new RestException ( 403 , " Insufficiant rights " );
2020-10-31 17:51:30 +00:00
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Intervention not found' );
}
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'fichinter' , $this -> fichinter -> id )) {
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
}
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
$result = $this -> fichinter -> setValid ( DolibarrApiAccess :: $user , $notrigger );
if ( $result == 0 ) {
throw new RestException ( 304 , 'Error nothing done. May be object is already validated' );
}
if ( $result < 0 ) {
2023-11-26 14:52:05 +00:00
throw new RestException ( 500 , 'Error when validating Intervention: ' . $this -> fichinter -> error );
2020-10-31 17:51:30 +00:00
}
2018-02-03 19:58:26 +00:00
2020-10-31 17:51:30 +00:00
$this -> fichinter -> fetchObjectLinked ();
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
return $this -> _cleanObjectDatas ( $this -> fichinter );
}
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
/**
* Close an intervention
*
2025-11-17 21:15:44 +00:00
* If you get a bad value for param notrigger check , provide this in body
* {
* " notrigger " : 0
* }
*
2025-05-04 09:24:38 +00:00
* @ since 7.0 . 0 Initial implementation
2025-11-17 21:15:44 +00:00
* @ since 23.0 . 0 Added notrigger parameter
2020-10-31 17:51:30 +00:00
*
2025-11-17 21:15:44 +00:00
* @ param int $id Intervention ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers { @ required true }
2020-10-31 17:51:30 +00:00
*
2025-05-04 09:24:38 +00:00
* @ url POST { id } / close
*
* @ return Object
*
* @ throws RestException
2025-11-17 21:15:44 +00:00
*
2020-10-31 17:51:30 +00:00
*/
2025-11-17 21:15:44 +00:00
public function close ( $id , $notrigger = 0 )
2020-10-31 17:51:30 +00:00
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'creer' )) {
2025-09-11 10:25:22 +00:00
throw new RestException ( 403 , " Insufficiant rights " );
2020-10-31 17:51:30 +00:00
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Intervention not found' );
}
2018-02-03 19:58:26 +00:00
2020-10-31 17:51:30 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'fichinter' , $this -> fichinter -> id )) {
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
}
2018-01-27 17:59:07 +00:00
2025-11-17 21:15:44 +00:00
$result = $this -> fichinter -> setClose ( DolibarrApiAccess :: $user , $notrigger );
2020-10-31 17:51:30 +00:00
if ( $result == 0 ) {
throw new RestException ( 304 , 'Error nothing done. May be object is already closed' );
}
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when closing Intervention: ' . $this -> fichinter -> error );
}
2018-02-03 19:58:26 +00:00
2020-10-31 17:51:30 +00:00
$this -> fichinter -> fetchObjectLinked ();
2018-01-27 17:59:07 +00:00
2020-10-31 17:51:30 +00:00
return $this -> _cleanObjectDatas ( $this -> fichinter );
}
2018-01-27 17:59:07 +00:00
2025-09-28 20:41:42 +00:00
/**
2026-03-24 23:55:09 +00:00
* Delete the line of the intervention
2025-09-28 20:41:42 +00:00
*
2026-03-24 23:55:09 +00:00
* @ since 23.0 . 0 Initial implementation
2025-09-28 20:41:42 +00:00
*
2026-03-24 23:55:09 +00:00
* @ param int $id ID of the intervention
* @ param int $lineid ID of the line to delete
* @ return Object Object with cleaned properties
*
* @ throws RestException 403
* @ throws RestException 404
* @ throws RestException 405
2025-09-28 20:41:42 +00:00
*
* @ url DELETE / { id } / lines / { lineid }
*/
2026-03-24 23:55:09 +00:00
public function deleteLine ( $id , $lineid )
2025-09-28 20:41:42 +00:00
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'creer' )) {
throw new RestException ( 403 );
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
2026-03-24 23:55:09 +00:00
throw new RestException ( 404 , 'Intervention not found' );
2025-09-28 20:41:42 +00:00
}
if ( $this -> fichinter -> status != 0 ) {
2026-03-24 23:55:09 +00:00
throw new RestException ( 403 , 'Intervention not in draft status : ' . $this -> fichinter -> getLibStatut ( 1 ));
2025-09-28 20:41:42 +00:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'ficheinter' , $this -> fichinter -> id )) {
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$objectline = new FichinterLigne ( $this -> db );
if ( $objectline -> fetch ( $lineid ) <= 0 ) {
2026-03-24 23:55:09 +00:00
throw new RestException ( 404 , 'Intervention line not found' );
2025-09-28 20:41:42 +00:00
}
$updateRes = $objectline -> deleteLine ( DolibarrApiAccess :: $user );
2026-03-24 23:55:09 +00:00
if ( $updateRes >= 0 ) {
2025-09-28 20:41:42 +00:00
return $this -> _cleanObjectDatas ( $this -> fichinter );
} else {
throw new RestException ( 405 , $this -> fichinter -> error );
}
}
/**
2025-12-17 09:11:50 +00:00
* Sets an intervention as draft
*
* @ since 23.0 . 0 Initial implementation
2025-09-28 20:41:42 +00:00
*
2025-12-17 09:11:50 +00:00
* @ param int $id ID of intervention
2025-09-28 20:41:42 +00:00
*
* @ return Object Object with cleaned properties
*
2025-12-17 09:11:50 +00:00
* @ url POST { id } / settodraft
2025-09-28 20:41:42 +00:00
*
* @ throws RestException 304
2025-12-17 09:11:50 +00:00
* @ throws RestException 403
2025-09-28 20:41:42 +00:00
* @ throws RestException 404
* @ throws RestException 500 System error
*/
public function settodraft ( $id )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'creer' )) {
2025-12-17 09:11:50 +00:00
throw new RestException ( 403 , " Insufficiant rights " );
2025-09-28 20:41:42 +00:00
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
2025-12-17 09:11:50 +00:00
throw new RestException ( 404 , 'Intervention not found' );
2025-09-28 20:41:42 +00:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'ficheinter' , $this -> fichinter -> id )) {
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> fichinter -> setDraft ( DolibarrApiAccess :: $user );
if ( $result == 0 ) {
2025-12-17 09:11:50 +00:00
throw new RestException ( 304 , 'Nothing done. . May be object is already set as draft.' );
2025-09-28 20:41:42 +00:00
}
if ( $result < 0 ) {
2025-12-17 09:11:50 +00:00
throw new RestException ( 500 , 'Error when closing intervention: ' . $this -> fichinter -> error );
2025-09-28 20:41:42 +00:00
}
2025-12-17 09:11:50 +00:00
$this -> fichinter -> fetchObjectLinked ();
2025-09-28 20:41:42 +00:00
return $this -> _cleanObjectDatas ( $this -> fichinter );
}
/**
* Adds a contact to an interventional
*
* @ param int $id Order ID
* @ param int $fk_socpeople Id of thirdparty contact ( if source = 'external' ) or id of user ( if source = 'internal' ) to link
* @ param string $type_contact Type of contact ( code ) . Must a code found into table llx_c_type_contact . For example : BILLING
* @ param string $source external = Contact extern ( llx_socpeople ), internal = Contact intern ( llx_user )
* @ param int $notrigger Disable all triggers
*
* @ url POST { id } / contacts
*
* @ return object
*
* @ throws RestException 304
* @ throws RestException 401
* @ throws RestException 404
* @ throws RestException 500 System error
*/
public function addContact ( $id , $fk_socpeople , $type_contact , $source , $notrigger = 0 )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'creer' )) {
throw new RestException ( 403 );
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Interventional not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'ficheinter' , $this -> fichinter -> id )) {
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> fichinter -> add_contact ( $fk_socpeople , $type_contact , $source , $notrigger );
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error : ' . $this -> fichinter -> error );
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Interventional not found' );
}
return $this -> _cleanObjectDatas ( $this -> fichinter );
}
2025-10-04 18:01:57 +00:00
/**
* Get contacts of given interventional
*
* Return an array with contact information
*
* @ param int $id ID of interventional
* @ param string $type Type of the interventional
2026-01-24 12:42:54 +00:00
* @ param string $source Source of the contact ( internal , external )
2025-10-04 18:01:57 +00:00
* @ return array < int , mixed > Object with cleaned properties
*
* @ url GET { id } / contacts
*
* @ throws RestException
*/
2026-01-24 12:42:54 +00:00
public function getContacts ( $id , $type = '' , $source = '' )
2025-10-04 18:01:57 +00:00
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'lire' )) {
throw new RestException ( 403 );
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Interventional not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'ficheinter' , $this -> fichinter -> id )) {
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2026-01-24 12:42:54 +00:00
if ( empty ( $source ) || $source == 'external' ) {
$contacts = $this -> fichinter -> liste_contact ( - 1 , 'external' , 0 , $type );
} else {
$contacts = array ();
}
if ( empty ( $source ) || $source == 'internal' ) {
$socpeoples = $this -> fichinter -> liste_contact ( - 1 , 'internal' , 0 , $type );
} else {
$socpeoples = array ();
}
2025-10-04 18:01:57 +00:00
$contacts = array_merge ( $contacts , $socpeoples );
return $contacts ;
}
/**
2025-09-28 20:41:42 +00:00
* Delete a contact type of given interventional
*
2025-10-04 18:01:57 +00:00
* @ param int $id Id of interventional to update
* @ param int $contactid Row key of the contact in the array contact_ids .
* @ param string $type Type of the contact ( BILLING , SHIPPING , CUSTOMER ) .
* @ return Object Object deleted
2025-09-28 20:41:42 +00:00
*
* @ url DELETE { id } / contact / { contactid } / { type }
*
* @ throws RestException 401
* @ throws RestException 404
* @ throws RestException 500 System error
*/
public function deleteContact ( $id , $contactid , $type )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'ficheinter' , 'creer' )) {
throw new RestException ( 403 );
}
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Interventional not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'ficheinter' , $this -> fichinter -> id )) {
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
foreach ( array ( 'internal' , 'external' ) as $source ) {
$contacts = $this -> fichinter -> liste_contact ( - 1 , $source );
foreach ( $contacts as $contact ) {
if ( $contact [ 'id' ] == $contactid && $contact [ 'code' ] == $type ) {
$result = $this -> fichinter -> delete_contact ( $contact [ 'rowid' ]);
if ( ! $result ) {
throw new RestException ( 500 , 'Error when deleted the contact' );
}
}
}
}
return $this -> _cleanObjectDatas ( $this -> fichinter );
}
/**
2026-03-25 13:11:24 +00:00
* Update a line of an intervention
2025-09-28 20:41:42 +00:00
*
2026-03-25 13:11:24 +00:00
* @ param int $id ID of order to update
* @ param int $lineid ID of line to update
* @ param array $request_data Intervention line data
2025-09-28 20:41:42 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2026-03-25 13:11:24 +00:00
* @ return Object Object with cleaned properties
2025-09-28 20:41:42 +00:00
*
2026-03-25 13:11:24 +00:00
* @ throws RestException 403
* @ throws RestException 404
* @ throws RestException 500
2025-09-28 20:41:42 +00:00
*
* @ url PUT / { id } / lines / { lineid }
*/
2026-03-25 13:11:24 +00:00
public function putLine ( $id , $lineid , $request_data )
2025-09-28 20:41:42 +00:00
{
$result = $this -> fichinter -> fetch ( $id );
if ( ! $result ) {
2026-03-25 13:11:24 +00:00
throw new RestException ( 404 , 'Intervention not found' );
2025-09-28 20:41:42 +00:00
}
if ( $this -> fichinter -> status != 0 ) {
2026-03-25 13:11:24 +00:00
throw new RestException ( 403 , 'Intervention not in draft status : ' . $this -> fichinter -> getLibStatut ( 1 ));
2025-09-28 20:41:42 +00:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'ficheinter' , $this -> fichinter -> id )) {
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$objectline = new FichinterLigne ( $this -> db );
if ( $objectline -> fetch ( $lineid ) <= 0 ) {
2026-03-25 13:11:24 +00:00
throw new RestException ( 404 , 'Intervention line not found' );
2025-09-28 20:41:42 +00:00
}
$request_data = ( object ) $request_data ;
2026-05-25 00:44:25 +00:00
if ( isset ( $request_data -> desc ) || isset ( $request_data -> description )) {
$objectline -> desc = sanitizeVal ( $request_data -> desc ? ? $request_data -> description , 'restricthtml' );
}
if ( isset ( $request_data -> date )) {
$objectline -> date = ( int ) sanitizeVal ( $request_data -> date );
}
if ( isset ( $request_data -> duration )) {
$objectline -> duration = ( int ) sanitizeVal ( $request_data -> duration );
}
if ( isset ( $request_data -> rang )) {
$objectline -> rang = ( int ) sanitizeVal ( $request_data -> rang );
}
2025-09-28 20:41:42 +00:00
$updateRes = $objectline -> update ( DolibarrApiAccess :: $user );
2026-03-25 13:11:24 +00:00
if ( $updateRes >= 0 ) {
$result = $this -> fichinter -> fetch ( $id );
if ( $result > 0 ) {
return $this -> _cleanObjectDatas ( $this -> fichinter );
} else {
throw new RestException ( 500 , $this -> fichinter -> error );
}
} else {
throw new RestException ( 500 , $objectline -> error );
2025-09-28 20:41:42 +00:00
}
}
2020-10-31 17:51:30 +00:00
/**
* Validate fields before create or update object
*
2025-01-29 00:03:16 +00:00
* @ param ? array < null | int | float | string > $data Data to validate
* @ return array < string , null | int | float | string >
2020-10-31 17:51:30 +00:00
*
* @ throws RestException
*/
private function _validate ( $data )
{
$fichinter = array ();
foreach ( Interventions :: $FIELDS as $field ) {
2021-02-25 22:00:32 +00:00
if ( ! isset ( $data [ $field ])) {
2020-10-31 17:51:30 +00:00
throw new RestException ( 400 , " $field field missing " );
2021-02-25 22:00:32 +00:00
}
2020-10-31 17:51:30 +00:00
$fichinter [ $field ] = $data [ $field ];
}
return $fichinter ;
}
2018-02-03 19:58:26 +00:00
2020-10-31 17:51:30 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
2025-05-04 09:24:38 +00:00
* Clean sensible object data
2025-11-20 00:47:46 +00:00
* @ phpstan - template T
2020-10-31 17:51:30 +00:00
*
2025-05-04 09:24:38 +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 );
2018-02-03 19:58:26 +00:00
2023-11-24 09:10:24 +00:00
unset ( $object -> labelStatus );
unset ( $object -> labelStatusShort );
2018-02-03 19:58:26 +00:00
2020-10-31 17:51:30 +00:00
return $object ;
}
/**
* Validate fields before create or update object
*
2025-03-02 11:13:32 +00:00
* @ param ? array < string , null | int | float | string > $data Data to validate
2025-01-29 00:03:16 +00:00
* @ return array < string , null | int | float | string > Return array with validated mandatory fields and their value
2020-10-31 17:51:30 +00:00
*
* @ throws RestException
*/
private function _validateLine ( $data )
{
2025-03-02 11:13:32 +00:00
if ( $data === null ) {
$data = array ();
}
2020-10-31 17:51:30 +00:00
$fichinter = array ();
foreach ( Interventions :: $FIELDSLINE as $field ) {
2021-02-25 22:00:32 +00:00
if ( ! isset ( $data [ $field ])) {
2020-10-31 17:51:30 +00:00
throw new RestException ( 400 , " $field field missing " );
2021-02-25 22:00:32 +00:00
}
2020-10-31 17:51:30 +00:00
$fichinter [ $field ] = $data [ $field ];
}
return $fichinter ;
}
2018-01-27 17:59:07 +00:00
}