2019-02-26 19:27:04 +01:00
< ? php
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
* Copyright ( C ) 2019 Maxime Kohlhaas < maxime @ atm - consulting . fr >
2024-04-06 17:38:39 +02:00
* Copyright ( C ) 2020 - 2024 Frédéric France < frederic . france @ free . fr >
2023-09-26 18:43:25 +02:00
* Copyright ( C ) 2022 Christian Humpel < christian . humpel @ live . com >
2019-02-26 19:27:04 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2019-02-26 19:27:04 +01:00
*/
use Luracast\Restler\RestException ;
2019-03-06 18:04:36 +01:00
require_once DOL_DOCUMENT_ROOT . '/bom/class/bom.class.php' ;
2019-02-26 19:27:04 +01:00
/**
2024-04-06 17:38:39 +02:00
* \file htdocs / bom / class / api_boms . class . php
2019-02-26 19:27:04 +01:00
* \ingroup bom
2020-06-17 22:11:22 +02:00
* \brief File for API management of BOM .
2019-02-26 19:27:04 +01:00
*/
/**
2020-06-17 22:11:22 +02:00
* API class for BOM
2019-02-26 19:27:04 +01:00
*
* @ access protected
* @ class DolibarrApiAccess { @ requires user , external }
*/
2019-05-02 12:19:43 +02:00
class Boms extends DolibarrApi
2019-02-26 19:27:04 +01:00
{
2020-10-31 18:51:30 +01:00
/**
* @ var BOM $bom { @ type BOM }
*/
public $bom ;
/**
* Constructor
*/
public function __construct ()
{
2024-02-19 17:55:00 +01:00
global $db ;
2019-02-26 19:27:04 +01:00
$this -> db = $db ;
2020-10-31 18:51:30 +01:00
$this -> bom = new BOM ( $this -> db );
}
/**
* Get properties of a bom object
*
2024-01-12 20:58:09 +01:00
* Return an array with bom information
2019-02-26 19:27:04 +01:00
*
2023-09-26 18:43:25 +02:00
* @ param int $id ID of bom
* @ return Object Object with cleaned properties
2020-10-31 18:51:30 +01:00
*
* @ url GET { id }
2024-01-17 20:52:32 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-17 20:52:32 +01:00
* @ throws RestException 404 BOM not found
2020-10-31 18:51:30 +01:00
*/
public function get ( $id )
{
2024-02-01 13:34:55 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'read' )) {
throw new RestException ( 403 );
2019-02-26 19:27:04 +01:00
}
2020-10-31 18:51:30 +01:00
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
2019-02-26 19:27:04 +01:00
2020-10-31 18:51:30 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom' , $this -> bom -> id , 'bom_bom' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2019-02-26 19:27:04 +01:00
}
return $this -> _cleanObjectDatas ( $this -> bom );
2020-10-31 18:51:30 +01:00
}
/**
* List boms
*
* Get a list of boms
*
2023-09-26 18:43:25 +02:00
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
2020-10-31 18:51:30 +01: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 20:58:09 +01:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
2020-10-31 18:51:30 +01:00
* @ return array Array of order objects
*
2024-01-17 20:52:32 +01:00
* @ throws RestException 400 Bad sqlfilters
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-17 20:52:32 +01:00
* @ throws RestException 503 Error retrieving list of boms
2020-10-31 18:51:30 +01:00
*/
2023-09-26 18:04:48 +02:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $sqlfilters = '' , $properties = '' )
2020-10-31 18:51:30 +01:00
{
2024-02-01 13:34:55 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'read' )) {
throw new RestException ( 403 );
2021-04-08 18:51:42 +02:00
}
2020-10-31 18:51:30 +01:00
$obj_ret = array ();
$tmpobject = new BOM ( $this -> db );
$socid = DolibarrApiAccess :: $user -> socid ? DolibarrApiAccess :: $user -> socid : '' ;
$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 10:44:50 +01:00
if ( $restrictonsocid && ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'client' , 'voir' ) && ! $socid ) {
2021-02-23 17:51:46 +01:00
$search_sale = DolibarrApiAccess :: $user -> id ;
}
2020-10-31 18:51:30 +01:00
$sql = " SELECT t.rowid " ;
2024-01-09 10:44:50 +01: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 18:51:30 +01:00
$sql .= " WHERE 1 = 1 " ;
2021-02-23 17:51:46 +01:00
if ( $tmpobject -> ismultientitymanaged ) {
$sql .= ' AND t.entity IN (' . getEntity ( $tmpobject -> element ) . ')' ;
}
if ( $restrictonsocid && $socid ) {
2021-03-22 13:31:06 +01:00
$sql .= " AND t.fk_soc = " . (( int ) $socid );
2021-02-23 17:51:46 +01:00
}
2024-01-09 10:44:50 +01: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 18:51:30 +01:00
}
2021-02-23 17:51:46 +01:00
if ( $sqlfilters ) {
2021-12-20 20:49:32 +01:00
$errormessage = '' ;
2023-02-25 19:48:33 +01:00
$sql .= forgeSQLFromUniversalSearchCriteria ( $sqlfilters , $errormessage );
if ( $errormessage ) {
throw new RestException ( 400 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
2020-10-31 18:51:30 +01:00
}
}
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
2021-02-23 17:51:46 +01:00
if ( $page < 0 ) {
2020-10-31 18:51:30 +01:00
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
$result = $this -> db -> query ( $sql );
2021-02-23 17:51:46 +01:00
if ( $result ) {
2020-10-31 18:51:30 +01:00
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
2021-02-23 17:51:46 +01:00
while ( $i < $num ) {
2020-10-31 18:51:30 +01:00
$obj = $this -> db -> fetch_object ( $result );
$bom_static = new BOM ( $this -> db );
if ( $bom_static -> fetch ( $obj -> rowid )) {
2023-09-26 18:04:48 +02:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $bom_static ), $properties );
2020-10-31 18:51:30 +01:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve bom list' );
}
2023-12-31 14:11:05 +01:00
2019-02-26 19:27:04 +01:00
return $obj_ret ;
2020-10-31 18:51:30 +01:00
}
/**
* Create bom object
*
* @ param array $request_data Request datas
2024-02-22 01:32:55 +01:00
* @ return int ID of bom
2024-01-17 20:52:32 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-17 20:52:32 +01:00
* @ throws RestException 500 Error retrieving list of boms
2020-10-31 18:51:30 +01:00
*/
public function post ( $request_data = null )
{
2024-02-01 13:34:55 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
// Check mandatory fields
$result = $this -> _validate ( $request_data );
foreach ( $request_data as $field => $value ) {
2023-12-15 12:15:33 +01:00
if ( $field === 'caller' ) {
2024-01-12 20:58:09 +01: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 12:28:55 +02:00
$this -> bom -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 12:15:33 +01:00
continue ;
}
2024-04-02 12:28:55 +02:00
$this -> bom -> $field = $this -> _checkValForAPI ( $field , $value , $this -> bom );
2020-10-31 18:51:30 +01:00
}
2023-01-04 16:33:39 +01:00
2023-01-04 17:41:26 +01:00
$this -> checkRefNumbering ();
2023-01-04 16:33:39 +01:00
2020-10-31 18:51:30 +01: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 01:32:55 +01:00
* @ param int $id Id of bom to update
* @ param array $request_data Datas
* @ return Object Object after update
2024-01-17 20:52:32 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-17 20:52:32 +01:00
* @ throws RestException 404 BOM not found
* @ throws RestException 500 Error updating bom
2020-10-31 18:51:30 +01:00
*/
public function put ( $id , $request_data = null )
{
2024-02-01 13:34:55 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2020-10-31 18:51:30 +01: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 13:34:55 +01:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2019-02-26 19:27:04 +01:00
}
2020-10-31 18:51:30 +01:00
foreach ( $request_data as $field => $value ) {
2021-02-23 17:51:46 +01:00
if ( $field == 'id' ) {
continue ;
}
2023-12-15 12:15:33 +01:00
if ( $field === 'caller' ) {
2024-01-12 20:58:09 +01: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 12:28:55 +02:00
$this -> bom -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 12:15:33 +01:00
continue ;
}
2024-04-02 12:28:55 +02:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
2025-01-16 10:37:29 +01:00
$this -> bom -> array_options [ $index ] = $this -> _checkValForAPI ( $field , $val , $this -> bom );
2024-04-02 12:28:55 +02:00
}
continue ;
}
$this -> bom -> $field = $this -> _checkValForAPI ( $field , $value , $this -> bom );
2020-10-31 18:51:30 +01:00
}
2023-01-04 17:41:26 +01:00
$this -> checkRefNumbering ();
2023-01-04 16:33:39 +01:00
2020-10-31 18:51:30 +01:00
if ( $this -> bom -> update ( DolibarrApiAccess :: $user ) > 0 ) {
return $this -> get ( $id );
} else {
throw new RestException ( 500 , $this -> bom -> error );
}
}
/**
* Delete bom
*
* @ param int $id BOM ID
* @ return array
2024-01-17 20:52:32 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-17 20:52:32 +01:00
* @ throws RestException 404 BOM not found
* @ throws RestException 500 Error deleting bom
2020-10-31 18:51:30 +01:00
*/
public function delete ( $id )
{
2024-02-01 13:34:55 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'delete' )) {
throw new RestException ( 403 );
2019-02-26 19:27:04 +01:00
}
2020-10-31 18:51:30 +01:00
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
2019-02-26 19:27:04 +01:00
2020-10-31 18:51:30 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom' , $this -> bom -> id , 'bom_bom' )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 18:51:30 +01:00
}
2019-02-26 19:27:04 +01:00
2021-02-23 17:51:46 +01:00
if ( ! $this -> bom -> delete ( DolibarrApiAccess :: $user )) {
2020-10-31 18:51:30 +01:00
throw new RestException ( 500 , 'Error when deleting BOM : ' . $this -> bom -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'BOM deleted'
)
);
}
2022-08-14 12:55:48 +02:00
/**
* Get lines of an BOM
*
* @ param int $id Id of BOM
*
* @ url GET { id } / lines
*
* @ return array
2024-01-17 20:52:32 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-17 20:52:32 +01:00
* @ throws RestException 404 BOM not found
2022-08-14 12:55:48 +02:00
*/
public function getLines ( $id )
{
2024-02-01 13:34:55 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'read' )) {
throw new RestException ( 403 );
2022-08-14 12:55:48 +02:00
}
$result = $this -> bom -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'BOM not found' );
}
2022-08-14 15:58:49 +02:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom_bom' , $this -> bom -> id )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2022-08-14 12:55:48 +02:00
}
$this -> bom -> getLinesArray ();
$result = array ();
foreach ( $this -> bom -> lines as $line ) {
array_push ( $result , $this -> _cleanObjectDatas ( $line ));
}
return $result ;
}
2020-10-31 18:51:30 +01:00
2022-08-15 15:18:08 +02:00
/**
* Add a line to given BOM
*
* @ param int $id Id of BOM to update
* @ param array $request_data BOMLine data
*
* @ url POST { id } / lines
*
* @ return int
2024-01-17 20:52:32 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-17 20:52:32 +01:00
* @ throws RestException 404 BOM not found
* @ throws RestException 500 Error adding bom line
2022-08-15 15:18:08 +02:00
*/
public function postLine ( $id , $request_data = null )
{
2024-02-01 13:34:55 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2022-08-15 15:18:08 +02: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 13:34:55 +01:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2022-08-15 15:18:08 +02: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 18:07:56 +02:00
$request_data -> position ,
2022-08-15 15:18:08 +02:00
$request_data -> fk_bom_child ,
2024-01-10 14:05:46 +01:00
$request_data -> import_key ,
2024-11-04 12:34:44 +01:00
$request_data -> fk_unit ,
$request_data -> array_options ,
$request_data -> fk_default_workstation
2022-08-15 15:18:08 +02:00
);
if ( $updateRes > 0 ) {
return $updateRes ;
} else {
2024-01-17 20:52:32 +01:00
throw new RestException ( 500 , $this -> bom -> error );
2022-08-15 15:18:08 +02:00
}
}
2022-08-18 18:07:56 +02: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
*
* @ url PUT { id } / lines / { lineid }
*
2023-01-24 23:11:28 +01:00
* @ return object | bool
2024-01-17 20:52:32 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-17 20:52:32 +01:00
* @ throws RestException 404 BOM not found
2022-08-18 18:07:56 +02:00
*/
public function putLine ( $id , $lineid , $request_data = null )
{
2024-02-01 13:34:55 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2022-08-18 18:07:56 +02: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 13:34:55 +01:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2022-08-18 18:07:56 +02: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 14:05:46 +01:00
$request_data -> import_key ,
2024-11-04 12:34:44 +01:00
$request_data -> fk_unit ,
$request_data -> array_options ,
$request_data -> fk_default_workstation
2022-08-18 18:07:56 +02: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 21:13:40 +01:00
* @ return array
2022-08-18 18:07:56 +02:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-17 20:52:32 +01:00
* @ throws RestException 404 BOM not found
* @ throws RestException 500 Error deleting bom line
2022-08-18 18:07:56 +02:00
*/
public function deleteLine ( $id , $lineid )
{
2024-02-01 13:34:55 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'bom' , 'write' )) {
throw new RestException ( 403 );
2022-08-18 18:07:56 +02: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 13:34:55 +01:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2022-08-18 18:07:56 +02:00
}
//Check the rowid is a line of current bom object
$lineIdIsFromObject = false ;
foreach ( $this -> bom -> lines as $bl ) {
2022-08-18 18:12:40 +02:00
if ( $bl -> id == $lineid ) {
2022-08-18 18:07:56 +02:00
$lineIdIsFromObject = true ;
break ;
}
}
if ( ! $lineIdIsFromObject ) {
2022-08-18 18:12:40 +02:00
throw new RestException ( 500 , 'Line to delete (rowid: ' . $lineid . ') is not a line of BOM (id: ' . $this -> bom -> id . ')' );
2022-08-18 18:07:56 +02:00
}
2024-02-02 23:46:12 +01:00
$updateRes = $this -> bom -> deleteLine ( DolibarrApiAccess :: $user , $lineid );
2022-08-18 18:07:56 +02:00
if ( $updateRes > 0 ) {
2024-03-01 21:13:40 +01:00
return array (
'success' => array (
'code' => 200 ,
'message' => 'line ' . $lineid . ' deleted'
)
);
2022-08-18 18:07:56 +02:00
} else {
2024-01-17 20:52:32 +01:00
throw new RestException ( 500 , $this -> bom -> error );
2022-08-18 18:07:56 +02:00
}
}
2020-10-31 18:51:30 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
*
* @ param Object $object Object to clean
* @ return Object Object with cleaned properties
*/
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 12:28:55 +02: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 18:51:30 +01: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 17:51:46 +01:00
for ( $i = 0 ; $i < $nboflines ; $i ++ ) {
2020-10-31 18:51:30 +01: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
*
* @ param array $data Array of data to validate
* @ return array
*
* @ throws RestException
*/
private function _validate ( $data )
{
$myobject = array ();
foreach ( $this -> bom -> fields as $field => $propfield ) {
2021-02-23 17:51:46 +01:00
if ( in_array ( $field , array ( 'rowid' , 'entity' , 'date_creation' , 'tms' , 'fk_user_creat' )) || $propfield [ 'notnull' ] != 1 ) {
continue ; // Not a mandatory field
}
if ( ! isset ( $data [ $field ])) {
2020-10-31 18:51:30 +01:00
throw new RestException ( 400 , " $field field missing " );
2021-02-23 17:51:46 +01:00
}
2023-12-04 12:01:45 +01:00
$myobject [ $field ] = $data [ $field ];
2020-10-31 18:51:30 +01:00
}
return $myobject ;
}
2023-01-04 16:33:39 +01:00
/**
* Validate the ref field and get the next Number if it ' s necessary .
*
* @ return void
*/
2023-12-06 16:47:44 +01:00
private function checkRefNumbering ()
2023-01-04 16:33:39 +01:00
{
$ref = substr ( $this -> bom -> ref , 1 , 4 );
2023-01-04 18:14:58 +01:00
if ( $this -> bom -> status > 0 && $ref == 'PROV' ) {
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 11:55:37 +01:00
if ( strtolower ( $this -> bom -> ref ) == 'auto' ) {
2023-01-04 18:32:30 +01:00
if ( empty ( $this -> bom -> id ) && $this -> bom -> status == 0 ) {
2023-01-04 18:14:58 +01:00
$this -> bom -> ref = '' ; // 'ref' will auto incremented with '(PROV' + newID + ')'
} else {
$this -> bom -> fetch_product ();
$numref = $this -> bom -> getNextNumRef ( $this -> bom -> product );
$this -> bom -> ref = $numref ;
}
2023-01-04 16:33:39 +01:00
}
}
2019-02-26 19:27:04 +01:00
}