2019-02-26 18:27:04 +00:00
< ? php
2025-11-09 00:19:31 +00:00
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
2019-02-26 18:27:04 +00:00
* Copyright ( C ) 2019 Maxime Kohlhaas < maxime @ atm - consulting . fr >
2025-11-09 00:19:31 +00:00
* Copyright ( C ) 2020 - 2025 Frédéric France < frederic . france @ free . fr >
2023-09-26 16:43:25 +00:00
* Copyright ( C ) 2022 Christian Humpel < christian . humpel @ live . com >
2025-03-02 11:13:32 +00:00
* Copyright ( C ) 2025 MDW < mdeweerd @ users . noreply . github . com >
2019-02-26 18:27:04 +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 />.
2019-02-26 18:27:04 +00:00
*/
use Luracast\Restler\RestException ;
2019-03-06 17:04:36 +00:00
require_once DOL_DOCUMENT_ROOT . '/bom/class/bom.class.php' ;
2019-02-26 18:27:04 +00:00
/**
2024-04-06 15:38:39 +00:00
* \file htdocs / bom / class / api_boms . class . php
2019-02-26 18:27:04 +00:00
* \ingroup bom
2020-06-17 20:11:22 +00:00
* \brief File for API management of BOM .
2019-02-26 18:27:04 +00:00
*/
/**
2020-06-17 20:11:22 +00:00
* API class for BOM
2019-02-26 18:27:04 +00:00
*
* @ access protected
* @ class DolibarrApiAccess { @ requires user , external }
*/
2019-05-02 10:19:43 +00:00
class Boms extends DolibarrApi
2019-02-26 18:27:04 +00:00
{
2020-10-31 17:51:30 +00:00
/**
2025-03-02 11:13:32 +00:00
* @ var BOM { @ type BOM }
2020-10-31 17:51:30 +00:00
*/
public $bom ;
/**
* Constructor
*/
public function __construct ()
{
2024-02-19 16:55:00 +00:00
global $db ;
2019-02-26 18:27:04 +00:00
$this -> db = $db ;
2020-10-31 17:51:30 +00:00
$this -> bom = new BOM ( $this -> db );
}
/**
* Get properties of a bom object
*
2024-01-12 19:58:09 +00:00
* Return an array with bom information
2019-02-26 18:27:04 +00:00
*
2023-09-26 16:43:25 +00:00
* @ param int $id ID of bom
* @ return Object Object with cleaned properties
2025-03-02 14:10:25 +00:00
* @ phan - return BOM
* @ phpstan - return BOM
2020-10-31 17:51:30 +00:00
*
* @ url GET { id }
2024-01-17 19:52:32 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-17 19:52:32 +00:00
* @ throws RestException 404 BOM not found
2020-10-31 17:51:30 +00:00
*/
public function get ( $id )
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'read' )) {
throw new RestException ( 403 );
2019-02-26 18:27:04 +00:00
}
2020-10-31 17:51:30 +00:00
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
2019-02-26 18:27:04 +00:00
2020-10-31 17:51:30 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom' , $this -> bom -> id , 'bom_bom' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2019-02-26 18:27:04 +00:00
}
return $this -> _cleanObjectDatas ( $this -> bom );
2020-10-31 17:51:30 +00:00
}
/**
* List boms
*
* Get a list of boms
*
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
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-12 19:58:09 +00:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
2020-10-31 17:51:30 +00:00
* @ return array Array of order objects
2025-03-02 14:10:25 +00:00
* @ phan - return BOM []
* @ phpstan - return BOM []
2020-10-31 17:51:30 +00:00
*
2024-01-17 19:52:32 +00:00
* @ throws RestException 400 Bad sqlfilters
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-17 19:52:32 +00:00
* @ throws RestException 503 Error retrieving list of boms
2020-10-31 17:51:30 +00:00
*/
2023-09-26 16:04:48 +00:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $sqlfilters = '' , $properties = '' )
2020-10-31 17:51:30 +00:00
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'read' )) {
throw new RestException ( 403 );
2021-04-08 16:51:42 +00:00
}
2020-10-31 17:51:30 +00:00
$obj_ret = array ();
$tmpobject = new BOM ( $this -> db );
2025-07-01 18:34:09 +00:00
$socid = DolibarrApiAccess :: $user -> socid ? : '' ;
2020-10-31 17:51:30 +00:00
$restrictonsocid = 0 ; // Set to 1 if there is a field socid in table of object
// If the internal user must only see his customers, force searching by him
$search_sale = 0 ;
2024-01-09 09:44:50 +00:00
if ( $restrictonsocid && ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'client' , 'voir' ) && ! $socid ) {
2021-02-23 16:51:46 +00:00
$search_sale = DolibarrApiAccess :: $user -> id ;
}
2020-10-31 17:51:30 +00:00
$sql = " SELECT t.rowid " ;
2024-01-09 09:44:50 +00:00
$sql .= " FROM " . MAIN_DB_PREFIX . $tmpobject -> table_element . " AS t " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . $tmpobject -> table_element . " _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 1 = 1 " ;
2021-02-23 16:51:46 +00:00
if ( $tmpobject -> ismultientitymanaged ) {
$sql .= ' AND t.entity IN (' . getEntity ( $tmpobject -> element ) . ')' ;
}
if ( $restrictonsocid && $socid ) {
2021-03-22 12:31:06 +00:00
$sql .= " AND t.fk_soc = " . (( int ) $socid );
2021-02-23 16:51:46 +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
}
2021-02-23 16:51:46 +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
}
}
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
2021-02-23 16:51:46 +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 16:51:46 +00:00
if ( $result ) {
2020-10-31 17:51:30 +00:00
$i = 0 ;
2025-09-28 20:20:44 +00:00
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
while ( $i < $min ) {
2020-10-31 17:51:30 +00:00
$obj = $this -> db -> fetch_object ( $result );
$bom_static = new BOM ( $this -> db );
if ( $bom_static -> fetch ( $obj -> rowid )) {
2023-09-26 16:04:48 +00:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $bom_static ), $properties );
2020-10-31 17:51:30 +00:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve bom list' );
}
2023-12-31 13:11:05 +00:00
2019-02-26 18:27:04 +00:00
return $obj_ret ;
2020-10-31 17:51:30 +00:00
}
/**
* Create bom object
*
2025-03-02 11:13:32 +00:00
* @ param array $request_data Request data
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-02-22 00:32:55 +00:00
* @ return int ID of bom
2024-01-17 19:52:32 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-17 19:52:32 +00:00
* @ throws RestException 500 Error retrieving list of boms
2020-10-31 17:51:30 +00:00
*/
public function post ( $request_data = null )
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2020-10-31 17:51:30 +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-12 19:58:09 +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 -> bom -> 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 -> bom -> $field = $this -> _checkValForAPI ( $field , $value , $this -> bom );
2020-10-31 17:51:30 +00:00
}
2023-01-04 15:33:39 +00:00
2023-01-04 16:41:26 +00:00
$this -> checkRefNumbering ();
2023-01-04 15:33:39 +00:00
2020-10-31 17:51:30 +00:00
if ( ! $this -> bom -> create ( DolibarrApiAccess :: $user )) {
throw new RestException ( 500 , " Error creating BOM " , array_merge ( array ( $this -> bom -> error ), $this -> bom -> errors ));
}
return $this -> bom -> id ;
}
/**
* Update bom
*
2024-02-22 00:32:55 +00:00
* @ param int $id Id of bom 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 Object after update
2025-03-02 14:10:25 +00:00
* @ phan - return BOM
* @ phpstan - return BOM
2024-01-17 19:52:32 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-17 19:52:32 +00:00
* @ throws RestException 404 BOM not found
* @ throws RestException 500 Error updating bom
2020-10-31 17:51:30 +00:00
*/
public function put ( $id , $request_data = null )
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom' , $this -> bom -> id , 'bom_bom' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2019-02-26 18:27:04 +00:00
}
2020-10-31 17:51:30 +00:00
foreach ( $request_data as $field => $value ) {
2021-02-23 16:51:46 +00:00
if ( $field == 'id' ) {
continue ;
}
2023-12-15 11:15:33 +00:00
if ( $field === 'caller' ) {
2024-01-12 19:58:09 +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 -> bom -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2024-04-02 10:28:55 +00:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
2026-05-08 23:16:35 +00:00
$this -> bom -> array_options [ $index ] = $this -> _checkValExtrafieldsForAPI ( $index , $val , $this -> bom );
2024-04-02 10:28:55 +00:00
}
continue ;
}
$this -> bom -> $field = $this -> _checkValForAPI ( $field , $value , $this -> bom );
2020-10-31 17:51:30 +00:00
}
2023-01-04 16:41:26 +00:00
$this -> checkRefNumbering ();
2023-01-04 15:33:39 +00:00
2020-10-31 17:51:30 +00:00
if ( $this -> bom -> update ( DolibarrApiAccess :: $user ) > 0 ) {
return $this -> get ( $id );
} else {
throw new RestException ( 500 , $this -> bom -> error );
}
}
2026-07-10 15:37:11 +00:00
/**
* Validate BOM
*
* @ param int $id BOM ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
* @ return Object Object with cleaned properties
*
* @ url POST { id } / validate
*
* @ throws RestException 304
* @ throws RestException 401
* @ throws RestException 404
* @ throws RestException 500 System error
*/
public function validate ( $id , $notrigger = 0 )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
}
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Bom not found' );
}
$result = $this -> bom -> 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 BOM: ' . $this -> bom -> error );
}
$result = $this -> bom -> fetch ( $id );
return $this -> _cleanObjectDatas ( $this -> bom );
}
2020-10-31 17:51:30 +00:00
/**
* Delete bom
*
* @ param int $id BOM 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 }}
2024-01-17 19:52:32 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-17 19:52:32 +00:00
* @ throws RestException 404 BOM not found
* @ throws RestException 500 Error deleting bom
2020-10-31 17:51:30 +00:00
*/
public function delete ( $id )
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'delete' )) {
throw new RestException ( 403 );
2019-02-26 18:27:04 +00:00
}
2020-10-31 17:51:30 +00:00
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
2019-02-26 18:27:04 +00:00
2020-10-31 17:51:30 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom' , $this -> bom -> id , 'bom_bom' )) {
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-26 18:27:04 +00:00
2021-02-23 16:51:46 +00:00
if ( ! $this -> bom -> delete ( DolibarrApiAccess :: $user )) {
2020-10-31 17:51:30 +00:00
throw new RestException ( 500 , 'Error when deleting BOM : ' . $this -> bom -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'BOM deleted'
)
);
}
2022-08-14 10:55:48 +00:00
/**
* Get lines of an BOM
*
* @ param int $id Id of BOM
*
* @ url GET { id } / lines
*
* @ return array
2025-03-02 14:10:25 +00:00
* @ phan - return BOMLine []
* @ phpstan - return BOMLine []
2024-01-17 19:52:32 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-17 19:52:32 +00:00
* @ throws RestException 404 BOM not found
2022-08-14 10:55:48 +00:00
*/
public function getLines ( $id )
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'read' )) {
throw new RestException ( 403 );
2022-08-14 10:55:48 +00:00
}
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
2022-08-14 13:58:49 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom_bom' , $this -> bom -> id )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2022-08-14 10:55:48 +00:00
}
$this -> bom -> getLinesArray ();
$result = array ();
foreach ( $this -> bom -> lines as $line ) {
array_push ( $result , $this -> _cleanObjectDatas ( $line ));
}
return $result ;
}
2020-10-31 17:51:30 +00:00
2022-08-15 13:18:08 +00:00
/**
* Add a line to given BOM
*
* @ param int $id Id of BOM to update
* @ param array $request_data BOMLine data
2025-03-02 11:13:32 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2022-08-15 13:18:08 +00:00
*
* @ url POST { id } / lines
*
* @ return int
2024-01-17 19:52:32 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-17 19:52:32 +00:00
* @ throws RestException 404 BOM not found
* @ throws RestException 500 Error adding bom line
2022-08-15 13:18:08 +00:00
*/
public function postLine ( $id , $request_data = null )
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2022-08-15 13:18:08 +00:00
}
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom_bom' , $this -> bom -> id )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2022-08-15 13:18:08 +00:00
}
$request_data = ( object ) $request_data ;
$updateRes = $this -> bom -> addLine (
$request_data -> fk_product ,
$request_data -> qty ,
$request_data -> qty_frozen ,
$request_data -> disable_stock_change ,
$request_data -> efficiency ,
2022-08-18 16:07:56 +00:00
$request_data -> position ,
2022-08-15 13:18:08 +00:00
$request_data -> fk_bom_child ,
2024-01-10 13:05:46 +00:00
$request_data -> import_key ,
2024-11-04 11:34:44 +00:00
$request_data -> fk_unit ,
$request_data -> array_options ,
$request_data -> fk_default_workstation
2022-08-15 13:18:08 +00:00
);
if ( $updateRes > 0 ) {
return $updateRes ;
} else {
2024-01-17 19:52:32 +00:00
throw new RestException ( 500 , $this -> bom -> error );
2022-08-15 13:18:08 +00:00
}
}
2022-08-18 16:07:56 +00:00
/**
* Update a line to given BOM
*
* @ param int $id Id of BOM to update
* @ param int $lineid Id of line to update
* @ param array $request_data BOMLine data
2025-03-02 11:13:32 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2022-08-18 16:07:56 +00:00
*
* @ url PUT { id } / lines / { lineid }
*
2023-01-24 22:11:28 +00:00
* @ return object | bool
2024-01-17 19:52:32 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-17 19:52:32 +00:00
* @ throws RestException 404 BOM not found
2022-08-18 16:07:56 +00:00
*/
public function putLine ( $id , $lineid , $request_data = null )
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2022-08-18 16:07:56 +00:00
}
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom_bom' , $this -> bom -> id )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2022-08-18 16:07:56 +00:00
}
$request_data = ( object ) $request_data ;
$updateRes = $this -> bom -> updateLine (
$lineid ,
$request_data -> qty ,
$request_data -> qty_frozen ,
$request_data -> disable_stock_change ,
$request_data -> efficiency ,
$request_data -> position ,
2024-01-10 13:05:46 +00:00
$request_data -> import_key ,
2024-11-04 11:34:44 +00:00
$request_data -> fk_unit ,
$request_data -> array_options ,
$request_data -> fk_default_workstation
2022-08-18 16:07:56 +00:00
);
if ( $updateRes > 0 ) {
$result = $this -> get ( $id );
unset ( $result -> line );
return $this -> _cleanObjectDatas ( $result );
}
return false ;
}
/**
* Delete a line to given BOM
*
*
* @ param int $id Id of BOM to update
* @ param int $lineid Id of line to delete
*
* @ url DELETE { id } / lines / { lineid }
*
2024-03-01 20:13:40 +00:00
* @ 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 }}
2022-08-18 16:07:56 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-17 19:52:32 +00:00
* @ throws RestException 404 BOM not found
* @ throws RestException 500 Error deleting bom line
2022-08-18 16:07:56 +00:00
*/
public function deleteLine ( $id , $lineid )
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2022-08-18 16:07:56 +00:00
}
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom_bom' , $this -> bom -> id )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2022-08-18 16:07:56 +00:00
}
//Check the rowid is a line of current bom object
$lineIdIsFromObject = false ;
foreach ( $this -> bom -> lines as $bl ) {
2022-08-18 16:12:40 +00:00
if ( $bl -> id == $lineid ) {
2022-08-18 16:07:56 +00:00
$lineIdIsFromObject = true ;
break ;
}
}
if ( ! $lineIdIsFromObject ) {
2022-08-18 16:12:40 +00:00
throw new RestException ( 500 , 'Line to delete (rowid: ' . $lineid . ') is not a line of BOM (id: ' . $this -> bom -> id . ')' );
2022-08-18 16:07:56 +00:00
}
2024-02-02 22:46:12 +00:00
$updateRes = $this -> bom -> deleteLine ( DolibarrApiAccess :: $user , $lineid );
2022-08-18 16:07:56 +00:00
if ( $updateRes > 0 ) {
2024-03-01 20:13:40 +00:00
return array (
'success' => array (
'code' => 200 ,
'message' => 'line ' . $lineid . ' deleted'
)
);
2022-08-18 16:07:56 +00:00
} else {
2024-01-17 19:52:32 +00:00
throw new RestException ( 500 , $this -> bom -> error );
2022-08-18 16:07:56 +00:00
}
}
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 -> rowid );
unset ( $object -> canvas );
unset ( $object -> name );
unset ( $object -> lastname );
unset ( $object -> firstname );
unset ( $object -> civility_id );
unset ( $object -> statut );
unset ( $object -> state );
unset ( $object -> state_id );
unset ( $object -> state_code );
unset ( $object -> region );
unset ( $object -> region_code );
unset ( $object -> country );
unset ( $object -> country_id );
unset ( $object -> country_code );
unset ( $object -> barcode_type );
unset ( $object -> barcode_type_code );
unset ( $object -> barcode_type_label );
unset ( $object -> barcode_type_coder );
unset ( $object -> total_ht );
unset ( $object -> total_tva );
unset ( $object -> total_localtax1 );
unset ( $object -> total_localtax2 );
unset ( $object -> total_ttc );
unset ( $object -> fk_account );
unset ( $object -> comments );
unset ( $object -> note );
unset ( $object -> mode_reglement_id );
unset ( $object -> cond_reglement_id );
unset ( $object -> cond_reglement );
unset ( $object -> shipping_method_id );
unset ( $object -> fk_incoterms );
unset ( $object -> label_incoterms );
unset ( $object -> location_incoterms );
2024-04-02 10:28:55 +00:00
unset ( $object -> multicurrency_code );
unset ( $object -> multicurrency_tx );
unset ( $object -> multicurrency_total_ht );
unset ( $object -> multicurrency_total_ttc );
unset ( $object -> multicurrency_total_tva );
unset ( $object -> multicurrency_total_localtax1 );
unset ( $object -> multicurrency_total_localtax2 );
2020-10-31 17:51:30 +00:00
// If object has lines, remove $db property
if ( isset ( $object -> lines ) && is_array ( $object -> lines ) && count ( $object -> lines ) > 0 ) {
$nboflines = count ( $object -> lines );
2021-02-23 16:51:46 +00:00
for ( $i = 0 ; $i < $nboflines ; $i ++ ) {
2020-10-31 17:51:30 +00:00
$this -> _cleanObjectDatas ( $object -> lines [ $i ]);
unset ( $object -> lines [ $i ] -> lines );
unset ( $object -> lines [ $i ] -> note );
}
}
return $object ;
}
/**
* Validate fields before create or update object
*
2025-03-02 14:10:25 +00:00
* @ param ? array < string , string > $data Array of data to validate
* @ 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
$myobject = array ();
foreach ( $this -> bom -> fields as $field => $propfield ) {
2025-03-24 19:52:13 +00:00
if ( in_array ( $field , array ( 'rowid' , 'entity' , 'date_creation' , 'tms' , 'fk_user_creat' )) || empty ( $propfield [ 'notnull' ]) || $propfield [ 'notnull' ] != 1 ) {
2021-02-23 16:51:46 +00:00
continue ; // Not a mandatory field
}
if ( ! isset ( $data [ $field ])) {
2020-10-31 17:51:30 +00:00
throw new RestException ( 400 , " $field field missing " );
2021-02-23 16:51:46 +00:00
}
2023-12-04 11:01:45 +00:00
$myobject [ $field ] = $data [ $field ];
2020-10-31 17:51:30 +00:00
}
return $myobject ;
}
2023-01-04 15:33:39 +00:00
/**
* Validate the ref field and get the next Number if it ' s necessary .
*
* @ return void
*/
2023-12-06 15:47:44 +00:00
private function checkRefNumbering ()
2023-01-04 15:33:39 +00:00
{
$ref = substr ( $this -> bom -> ref , 1 , 4 );
2025-11-09 00:19:31 +00:00
if ( $this -> bom -> status > BOM :: STATUS_DRAFT && $ref == 'PROV' ) {
2023-01-04 17:14:58 +00:00
throw new RestException ( 400 , " Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field. " );
}
2023-01-05 10:55:37 +00:00
if ( strtolower ( $this -> bom -> ref ) == 'auto' ) {
2025-11-09 00:19:31 +00:00
if ( empty ( $this -> bom -> id ) && $this -> bom -> status == BOM :: STATUS_DRAFT ) {
2023-01-04 17:14:58 +00:00
$this -> bom -> ref = '' ; // 'ref' will auto incremented with '(PROV' + newID + ')'
} else {
2025-11-09 00:19:31 +00:00
$res = $this -> bom -> fetch_product ();
if ( $res > 0 && $this -> bom -> product instanceof Product ) {
$numref = $this -> bom -> getNextNumRef ( $this -> bom -> product ); // @phan-suppress-current-line PhanTypeMismatchArgumentNullable
$this -> bom -> ref = $numref ;
} else {
throw new RestException ( 400 , " Error when generating automatic increment on the 'ref' field. " );
}
2023-01-04 17:14:58 +00:00
}
2023-01-04 15:33:39 +00:00
}
}
2019-02-26 18:27:04 +00:00
}