2020-06-18 01:09:30 +02:00
< ? php
2024-06-30 21:04:19 +02:00
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
* Copyright ( C ) 2019 Maxime Kohlhaas < maxime @ atm - consulting . fr >
2025-02-06 22:16:48 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2024-06-30 21:04:19 +02:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2020-06-18 01:09:30 +02: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
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
use Luracast\Restler\RestException ;
require_once DOL_DOCUMENT_ROOT . '/mrp/class/mo.class.php' ;
/**
2021-10-02 13:42:19 +02:00
* \file htdocs / mrp / class / api_mos . class . php
2020-06-18 01:09:30 +02:00
* \ingroup mrp
* \brief File for API management of MO .
*/
/**
* API class for MO
*
* @ access protected
* @ class DolibarrApiAccess { @ requires user , external }
*/
class Mos extends DolibarrApi
{
/**
2025-02-06 22:16:48 +01:00
* @ var Mo { @ type Mo }
2020-06-18 01:09:30 +02:00
*/
public $mo ;
/**
* Constructor
*/
public function __construct ()
{
global $db , $conf ;
$this -> db = $db ;
$this -> mo = new Mo ( $this -> db );
}
/**
* Get properties of a MO object
*
2024-01-12 18:06:50 +01:00
* Return an array with MO information
2020-06-18 01:09:30 +02:00
*
2023-09-26 18:43:25 +02:00
* @ param int $id ID of MO
* @ return Object Object with cleaned properties
2020-06-18 01:09:30 +02:00
*
* @ url GET { id }
2023-09-26 18:43:25 +02:00
* @ throws RestException
2020-06-18 01:09:30 +02:00
*/
public function get ( $id )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'mrp' , 'read' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2020-06-18 01:09:30 +02:00
}
$result = $this -> mo -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'MO not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'mrp' , $this -> mo -> id , 'mrp_mo' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-06-18 01:09:30 +02:00
}
return $this -> _cleanObjectDatas ( $this -> mo );
}
/**
* List Mos
*
* Get a list of MOs
*
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-06-18 01:09:30 +02: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 12:26:37 +01:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
2020-06-18 01:09:30 +02:00
* @ return array Array of order objects
2025-02-06 22:16:48 +01:00
* @ phan - return Mo []
* @ phpstan - return Mo []
2020-06-18 01:09:30 +02:00
*
* @ throws RestException
*/
2023-09-26 18:04:48 +02:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $sqlfilters = '' , $properties = '' )
2020-06-18 01:09:30 +02:00
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'mrp' , 'read' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2021-04-08 18:51:42 +02:00
}
2020-06-18 01:09:30 +02:00
$obj_ret = array ();
2020-09-19 23:30:29 +02:00
$tmpobject = new Mo ( $this -> db );
2020-06-18 01:09:30 +02:00
2024-01-09 10:44:50 +01:00
$socid = DolibarrApiAccess :: $user -> socid ? DolibarrApiAccess :: $user -> socid : 0 ;
2020-06-18 01:09:30 +02: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-02-09 15:58:49 +01:00
if ( $restrictonsocid && ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'client' , 'voir' ) && ! $socid ) {
2020-12-11 19:02:13 +01:00
$search_sale = DolibarrApiAccess :: $user -> id ;
}
2020-06-18 01:09:30 +02: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-06-18 01:09:30 +02:00
$sql .= " WHERE 1 = 1 " ;
2020-12-11 19:02:13 +01:00
if ( $tmpobject -> ismultientitymanaged ) {
$sql .= ' AND t.entity IN (' . getEntity ( $tmpobject -> element ) . ')' ;
}
if ( $restrictonsocid && $socid ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND t.fk_soc = " . (( int ) $socid );
2020-12-11 19:02:13 +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-06-18 01:09:30 +02:00
}
2020-12-11 19:02:13 +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-06-18 01:09:30 +02:00
}
}
2020-09-19 23:30:29 +02:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
2020-06-18 01:09:30 +02:00
if ( $limit ) {
2020-12-11 19:02:13 +01:00
if ( $page < 0 ) {
2020-06-18 01:09:30 +02:00
$page = 0 ;
}
$offset = $limit * $page ;
2020-09-19 23:30:29 +02:00
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
2020-06-18 01:09:30 +02:00
}
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2020-12-11 19:02:13 +01:00
if ( $result ) {
2020-09-19 23:30:29 +02:00
$num = $this -> db -> num_rows ( $result );
2020-06-18 01:09:30 +02:00
$i = 0 ;
2020-12-11 19:02:13 +01:00
while ( $i < $num ) {
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
$tmp_object = new Mo ( $this -> db );
2020-06-18 01:09:30 +02:00
if ( $tmp_object -> fetch ( $obj -> rowid )) {
2023-09-26 18:04:48 +02:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $tmp_object ), $properties );
2020-06-18 01:09:30 +02:00
}
$i ++ ;
}
2020-12-11 19:02:13 +01:00
} else {
2020-06-18 01:09:30 +02:00
throw new RestException ( 503 , 'Error when retrieve MO list' );
}
2023-12-31 14:11:05 +01:00
2020-06-18 01:09:30 +02:00
return $obj_ret ;
}
/**
* Create MO object
*
* @ param array $request_data Request datas
2025-02-06 22:16:48 +01:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2020-06-18 01:09:30 +02:00
* @ return int ID of MO
*/
public function post ( $request_data = null )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'mrp' , 'write' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2020-06-18 01:09:30 +02: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 18:06:50 +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 -> mo -> 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 -> mo -> $field = $this -> _checkValForAPI ( $field , $value , $this -> mo );
2020-06-18 01:09:30 +02:00
}
2023-01-08 21:43:12 +01:00
$this -> checkRefNumbering ();
2020-06-18 01:09:30 +02:00
if ( ! $this -> mo -> create ( DolibarrApiAccess :: $user )) {
throw new RestException ( 500 , " Error creating MO " , array_merge ( array ( $this -> mo -> error ), $this -> mo -> errors ));
}
return $this -> mo -> id ;
}
/**
* Update MO
*
2024-02-22 01:32:55 +01:00
* @ param int $id Id of MO to update
* @ param array $request_data Datas
2025-02-06 22:16:48 +01:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-02-22 01:32:55 +01:00
* @ return Object Updated object
2020-06-18 01:09:30 +02:00
*/
public function put ( $id , $request_data = null )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'mrp' , 'write' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2020-06-18 01:09:30 +02:00
}
$result = $this -> mo -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'MO not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'mrp' , $this -> mo -> id , 'mrp_mo' )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-06-18 01:09:30 +02:00
}
foreach ( $request_data as $field => $value ) {
2020-12-11 19:02:13 +01:00
if ( $field == 'id' ) {
continue ;
}
2023-12-15 12:15:33 +01:00
if ( $field === 'caller' ) {
2024-01-12 18:06:50 +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 -> mo -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 12:15:33 +01:00
continue ;
}
2025-01-16 10:37:29 +01:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
$this -> mo -> array_options [ $index ] = $this -> _checkValForAPI ( $field , $val , $this -> mo );
}
continue ;
}
2025-01-20 12:28:49 +01:00
2024-04-02 12:28:55 +02:00
$this -> mo -> $field = $this -> _checkValForAPI ( $field , $value , $this -> mo );
2020-06-18 01:09:30 +02:00
}
2023-01-08 21:43:12 +01:00
$this -> checkRefNumbering ();
2020-12-11 19:02:13 +01:00
if ( $this -> mo -> update ( DolibarrApiAccess :: $user ) > 0 ) {
2020-06-18 01:09:30 +02:00
return $this -> get ( $id );
2020-06-20 18:46:49 +02:00
} else {
2020-06-18 01:09:30 +02:00
throw new RestException ( 500 , $this -> mo -> error );
}
}
/**
* Delete MO
*
* @ param int $id MO ID
* @ return array
2025-02-06 22:16:48 +01:00
* @ phan - return array < string , array { code : int , message : string } >
* @ phpstan - return array < string , array { code : int , message : string } >
2020-06-18 01:09:30 +02:00
*/
public function delete ( $id )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'mrp' , 'delete' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2020-06-18 01:09:30 +02:00
}
$result = $this -> mo -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'MO not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'mrp' , $this -> mo -> id , 'mrp_mo' )) {
2024-02-09 15:58:49 +01:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-06-18 01:09:30 +02:00
}
2020-12-11 19:02:13 +01:00
if ( ! $this -> mo -> delete ( DolibarrApiAccess :: $user )) {
2020-06-18 01:09:30 +02:00
throw new RestException ( 500 , 'Error when deleting MO : ' . $this -> mo -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'MO deleted'
)
);
}
2021-06-11 09:58:03 +02:00
/**
2024-05-29 13:58:51 +02:00
* Produce and consume all
2024-05-29 14:07:11 +02:00
*
2024-05-29 13:42:55 +02:00
* - If arraytoconsume and arraytoproduce are both filled , this fill an empty MO with the lines to consume and produce and record the consumption and production .
* - If arraytoconsume and arraytoproduce are not provided , it consumes and produces all existing lines .
2021-06-11 09:58:03 +02:00
*
* Example :
* {
* " inventorylabel " : " Produce and consume using API " ,
* " inventorycode " : " PRODUCEAPI-YY-MM-DD " ,
* " autoclose " : 1 ,
2024-05-29 14:12:09 +02:00
* " arraytoconsume " : [
* " objectid " : 123 , -- ID_of_product
* " qty " : " 2 " ,
* " fk_warehouse " : " 789 "
* ],
* " arraytoproduce " : [
* " objectid " : 456 , -- ID_of_product
2024-05-29 14:12:19 +02:00
* " qty " : " 1 " ,
2024-05-29 14:12:09 +02:00
* " fk_warehouse " : " 789 "
* ]
2021-06-11 09:58:03 +02:00
* }
*
2023-09-26 18:43:25 +02:00
* @ param int $id ID of state
* @ param array $request_data Request datas
2025-02-06 22:16:48 +01:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2021-06-11 09:58:03 +02:00
*
2024-05-29 13:51:00 +02:00
* @ url POST { id } / produceandconsumeall
2021-06-11 09:58:03 +02:00
*
* @ return int ID of MO
*/
2024-05-29 13:51:00 +02:00
public function produceAndConsumeAll ( $id , $request_data = null )
2021-06-11 09:58:03 +02:00
{
2021-06-16 12:08:34 +02:00
global $langs ;
$error = 0 ;
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'mrp' , 'write' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , 'Not enough permission' );
2021-06-11 09:58:03 +02:00
}
$result = $this -> mo -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'MO not found' );
}
if ( $this -> mo -> status != Mo :: STATUS_VALIDATED && $this -> mo -> status != Mo :: STATUS_INPROGRESS ) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 405 , 'Error bad status of MO' );
2021-06-11 09:58:03 +02:00
}
2023-12-15 12:15:33 +01:00
// Code for consume and produce...
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/mrp/lib/mrp_mo.lib.php' ;
$stockmove = new MouvementStock ( $this -> db );
2021-06-11 09:58:03 +02:00
$labelmovement = '' ;
$codemovement = '' ;
$autoclose = 1 ;
$arraytoconsume = array ();
$arraytoproduce = array ();
foreach ( $request_data as $field => $value ) {
if ( $field == 'inventorylabel' ) {
$labelmovement = $value ;
}
if ( $field == 'inventorycode' ) {
$codemovement = $value ;
}
if ( $field == 'autoclose' ) {
$autoclose = $value ;
}
2021-06-15 11:35:41 +02:00
if ( $field == 'arraytoconsume' ) {
$arraytoconsume = $value ;
}
if ( $field == 'arraytoproduce' ) {
$arraytoproduce = $value ;
}
2023-12-15 12:15:33 +01:00
if ( $field === 'caller' ) {
2024-01-12 18:06:50 +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
$stockmove -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 12:15:33 +01:00
continue ;
}
2021-06-11 09:58:03 +02:00
}
if ( empty ( $labelmovement )) {
2023-04-18 20:59:43 +02:00
throw new RestException ( 500 , " Field inventorylabel not provided " );
2021-06-11 09:58:03 +02:00
}
if ( empty ( $codemovement )) {
2023-04-18 20:59:43 +02:00
throw new RestException ( 500 , " Field inventorycode not provided " );
2021-06-11 09:58:03 +02:00
}
2021-10-12 12:14:03 +02:00
$consumptioncomplete = true ;
$productioncomplete = true ;
2021-06-15 11:35:41 +02:00
if ( ! empty ( $arraytoconsume ) && ! empty ( $arraytoproduce )) {
$pos = 0 ;
$arrayofarrayname = array ( " arraytoconsume " , " arraytoproduce " );
foreach ( $arrayofarrayname as $arrayname ) {
2023-11-23 17:30:43 +01:00
foreach ( ${$arrayname} as $value ) {
2021-06-15 11:35:41 +02:00
$tmpproduct = new Product ( $this -> db );
if ( empty ( $value [ " objectid " ])) {
throw new RestException ( 500 , " Field objectid required in " . $arrayname );
}
$tmpproduct -> fetch ( $value [ " qty " ]);
if ( empty ( $value [ " qty " ])) {
throw new RestException ( 500 , " Field qty required in " . $arrayname );
}
2023-12-27 12:12:20 +01:00
if ( $value [ " qty " ] != 0 ) {
2021-06-15 11:35:41 +02:00
$qtytoprocess = $value [ " qty " ];
if ( isset ( $value [ " fk_warehouse " ])) { // If there is a warehouse to set
if ( ! ( $value [ " fk_warehouse " ] > 0 )) { // If there is no warehouse set.
$error ++ ;
2021-08-31 20:31:05 +02:00
throw new RestException ( 500 , " Field fk_warehouse must be > 0 in " . $arrayname );
2021-06-15 11:35:41 +02:00
}
if ( $tmpproduct -> status_batch ) {
$error ++ ;
2021-08-31 20:31:05 +02:00
throw new RestException ( 500 , " Product " . $tmpproduct -> ref . " must be in batch " );
2021-06-15 11:35:41 +02:00
}
}
$idstockmove = 0 ;
if ( ! $error && $value [ " fk_warehouse " ] > 0 ) {
2024-05-29 13:42:55 +02:00
// Record consumption to do and stock movement
2021-06-15 11:35:41 +02:00
$id_product_batch = 0 ;
2021-10-12 12:04:43 +02:00
$stockmove -> setOrigin ( $this -> mo -> element , $this -> mo -> id );
2024-05-29 13:42:55 +02:00
if ( $arrayname == 'arraytoconsume' ) {
2021-06-15 11:35:41 +02:00
$moline = new MoLine ( $this -> db );
$moline -> fk_mo = $this -> mo -> id ;
$moline -> position = $pos ;
$moline -> fk_product = $value [ " objectid " ];
$moline -> fk_warehouse = $value [ " fk_warehouse " ];
$moline -> qty = $qtytoprocess ;
2024-06-30 21:04:19 +02:00
$moline -> batch = ( string ) $tmpproduct -> status_batch ;
2021-06-15 11:35:41 +02:00
$moline -> role = 'toproduce' ;
2024-08-17 19:32:52 +02:00
$moline -> fk_mrp_production = 0 ;
2021-06-15 11:35:41 +02:00
$moline -> fk_stock_movement = $idstockmove ;
$moline -> fk_user_creat = DolibarrApiAccess :: $user -> id ;
$resultmoline = $moline -> create ( DolibarrApiAccess :: $user );
if ( $resultmoline <= 0 ) {
$error ++ ;
throw new RestException ( 500 , $moline -> error );
}
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> livraison ( DolibarrApiAccess :: $user , $value [ " objectid " ], $value [ " fk_warehouse " ], $qtytoprocess , 0 , $labelmovement , dol_now (), '' , '' , ( string ) $tmpproduct -> status_batch , $id_product_batch , $codemovement );
2021-06-15 11:35:41 +02:00
} else {
$moline = new MoLine ( $this -> db );
$moline -> fk_mo = $this -> mo -> id ;
$moline -> position = $pos ;
$moline -> fk_product = $value [ " objectid " ];
$moline -> fk_warehouse = $value [ " fk_warehouse " ];
$moline -> qty = $qtytoprocess ;
2024-06-30 21:04:19 +02:00
$moline -> batch = ( string ) $tmpproduct -> status_batch ;
2021-06-15 11:35:41 +02:00
$moline -> role = 'toconsume' ;
2024-08-17 19:32:52 +02:00
$moline -> fk_mrp_production = 0 ;
2021-06-15 11:35:41 +02:00
$moline -> fk_stock_movement = $idstockmove ;
$moline -> fk_user_creat = DolibarrApiAccess :: $user -> id ;
$resultmoline = $moline -> create ( DolibarrApiAccess :: $user );
if ( $resultmoline <= 0 ) {
$error ++ ;
throw new RestException ( 500 , $moline -> error );
}
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> reception ( DolibarrApiAccess :: $user , $value [ " objectid " ], $value [ " fk_warehouse " ], $qtytoprocess , 0 , $labelmovement , '' , '' , ( string ) $tmpproduct -> status_batch , dol_now (), $id_product_batch , $codemovement );
2021-06-15 11:35:41 +02:00
}
if ( $idstockmove < 0 ) {
$error ++ ;
throw new RestException ( 500 , $stockmove -> error );
}
}
if ( ! $error ) {
2024-05-29 13:42:55 +02:00
// Record consumption done
2021-06-15 11:35:41 +02:00
$moline = new MoLine ( $this -> db );
$moline -> fk_mo = $this -> mo -> id ;
$moline -> position = $pos ;
$moline -> fk_product = $value [ " objectid " ];
$moline -> fk_warehouse = $value [ " fk_warehouse " ];
$moline -> qty = $qtytoprocess ;
2024-06-30 21:04:19 +02:00
$moline -> batch = ( string ) $tmpproduct -> status_batch ;
2021-06-15 11:35:41 +02:00
if ( $arrayname == " arraytoconsume " ) {
$moline -> role = 'consumed' ;
} else {
$moline -> role = 'produced' ;
}
2024-08-17 19:32:52 +02:00
$moline -> fk_mrp_production = 0 ;
2021-06-15 11:35:41 +02:00
$moline -> fk_stock_movement = $idstockmove ;
$moline -> fk_user_creat = DolibarrApiAccess :: $user -> id ;
$resultmoline = $moline -> create ( DolibarrApiAccess :: $user );
if ( $resultmoline <= 0 ) {
$error ++ ;
throw new RestException ( 500 , $moline -> error );
}
$pos ++ ;
}
}
}
}
if ( ! $error ) {
if ( $autoclose <= 0 ) {
$consumptioncomplete = false ;
$productioncomplete = false ;
}
}
} else {
$pos = 0 ;
foreach ( $this -> mo -> lines as $line ) {
if ( $line -> role == 'toconsume' ) {
$tmpproduct = new Product ( $this -> db );
$tmpproduct -> fetch ( $line -> fk_product );
if ( $line -> qty != 0 ) {
$qtytoprocess = $line -> qty ;
if ( isset ( $line -> fk_warehouse )) { // If there is a warehouse to set
if ( ! ( $line -> fk_warehouse > 0 )) { // If there is no warehouse set.
$langs -> load ( " errors " );
$error ++ ;
2021-09-13 01:32:20 +02:00
throw new RestException ( 500 , $langs -> trans ( " ErrorFieldRequiredForProduct " , $langs -> transnoentitiesnoconv ( " Warehouse " ), $tmpproduct -> ref ));
2021-06-15 11:35:41 +02:00
}
if ( $tmpproduct -> status_batch ) {
$langs -> load ( " errors " );
$error ++ ;
2021-09-13 01:32:20 +02:00
throw new RestException ( 500 , $langs -> trans ( " ErrorFieldRequiredForProduct " , $langs -> transnoentitiesnoconv ( " Batch " ), $tmpproduct -> ref ));
2021-06-15 11:35:41 +02:00
}
}
$idstockmove = 0 ;
if ( ! $error && $line -> fk_warehouse > 0 ) {
// Record stock movement
$id_product_batch = 0 ;
2021-10-22 14:27:42 +02:00
$stockmove -> origin_type = 'mo' ;
$stockmove -> origin_id = $this -> mo -> id ;
2021-06-15 11:35:41 +02:00
if ( $qtytoprocess >= 0 ) {
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> livraison ( DolibarrApiAccess :: $user , $line -> fk_product , $line -> fk_warehouse , $qtytoprocess , 0 , $labelmovement , dol_now (), '' , '' , ( string ) $tmpproduct -> status_batch , $id_product_batch , $codemovement );
2021-06-15 11:35:41 +02:00
} else {
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> reception ( DolibarrApiAccess :: $user , $line -> fk_product , $line -> fk_warehouse , $qtytoprocess , 0 , $labelmovement , '' , '' , ( string ) $tmpproduct -> status_batch , dol_now (), $id_product_batch , $codemovement );
2021-06-15 11:35:41 +02:00
}
if ( $idstockmove < 0 ) {
$error ++ ;
throw new RestException ( 500 , $stockmove -> error );
}
}
if ( ! $error ) {
// Record consumption
$moline = new MoLine ( $this -> db );
$moline -> fk_mo = $this -> mo -> id ;
$moline -> position = $pos ;
$moline -> fk_product = $line -> fk_product ;
$moline -> fk_warehouse = $line -> fk_warehouse ;
$moline -> qty = $qtytoprocess ;
2024-06-30 21:04:19 +02:00
$moline -> batch = ( string ) $tmpproduct -> status_batch ;
2021-06-15 11:35:41 +02:00
$moline -> role = 'consumed' ;
$moline -> fk_mrp_production = $line -> id ;
$moline -> fk_stock_movement = $idstockmove ;
$moline -> fk_user_creat = DolibarrApiAccess :: $user -> id ;
$resultmoline = $moline -> create ( DolibarrApiAccess :: $user );
if ( $resultmoline <= 0 ) {
$error ++ ;
throw new RestException ( 500 , $moline -> error );
}
$pos ++ ;
}
}
}
}
$pos = 0 ;
foreach ( $this -> mo -> lines as $line ) {
if ( $line -> role == 'toproduce' ) {
$tmpproduct = new Product ( $this -> db );
$tmpproduct -> fetch ( $line -> fk_product );
if ( $line -> qty != 0 ) {
$qtytoprocess = $line -> qty ;
if ( isset ( $line -> fk_warehouse )) { // If there is a warehouse to set
if ( ! ( $line -> fk_warehouse > 0 )) { // If there is no warehouse set.
$langs -> load ( " errors " );
$error ++ ;
2021-09-13 01:32:20 +02:00
throw new RestException ( 500 , $langs -> trans ( " ErrorFieldRequiredForProduct " , $langs -> transnoentitiesnoconv ( " Warehouse " ), $tmpproduct -> ref ));
2021-06-15 11:35:41 +02:00
}
if ( $tmpproduct -> status_batch ) {
$langs -> load ( " errors " );
$error ++ ;
2021-09-13 01:32:20 +02:00
throw new RestException ( 500 , $langs -> trans ( " ErrorFieldRequiredForProduct " , $langs -> transnoentitiesnoconv ( " Batch " ), $tmpproduct -> ref ));
2021-06-15 11:35:41 +02:00
}
}
$idstockmove = 0 ;
if ( ! $error && $line -> fk_warehouse > 0 ) {
// Record stock movement
$id_product_batch = 0 ;
2021-10-22 14:27:42 +02:00
$stockmove -> origin_type = 'mo' ;
$stockmove -> origin_id = $this -> mo -> id ;
2021-06-15 11:35:41 +02:00
if ( $qtytoprocess >= 0 ) {
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> reception ( DolibarrApiAccess :: $user , $line -> fk_product , $line -> fk_warehouse , $qtytoprocess , 0 , $labelmovement , '' , '' , ( string ) $tmpproduct -> status_batch , dol_now (), $id_product_batch , $codemovement );
2024-05-29 13:42:55 +02:00
} else {
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> livraison ( DolibarrApiAccess :: $user , $line -> fk_product , $line -> fk_warehouse , $qtytoprocess , 0 , $labelmovement , dol_now (), '' , '' , ( string ) $tmpproduct -> status_batch , $id_product_batch , $codemovement );
2021-06-15 11:35:41 +02:00
}
if ( $idstockmove < 0 ) {
$error ++ ;
throw new RestException ( 500 , $stockmove -> error );
}
}
if ( ! $error ) {
// Record consumption
$moline = new MoLine ( $this -> db );
$moline -> fk_mo = $this -> mo -> id ;
$moline -> position = $pos ;
$moline -> fk_product = $line -> fk_product ;
$moline -> fk_warehouse = $line -> fk_warehouse ;
$moline -> qty = $qtytoprocess ;
2024-06-30 21:04:19 +02:00
$moline -> batch = ( string ) $tmpproduct -> status_batch ;
2021-06-15 11:35:41 +02:00
$moline -> role = 'produced' ;
$moline -> fk_mrp_production = $line -> id ;
$moline -> fk_stock_movement = $idstockmove ;
$moline -> fk_user_creat = DolibarrApiAccess :: $user -> id ;
$resultmoline = $moline -> create ( DolibarrApiAccess :: $user );
if ( $resultmoline <= 0 ) {
$error ++ ;
throw new RestException ( 500 , $moline -> error );
}
$pos ++ ;
}
}
}
}
if ( ! $error ) {
if ( $autoclose > 0 ) {
foreach ( $this -> mo -> lines as $line ) {
if ( $line -> role == 'toconsume' ) {
$arrayoflines = $this -> mo -> fetchLinesLinked ( 'consumed' , $line -> id );
$alreadyconsumed = 0 ;
foreach ( $arrayoflines as $line2 ) {
$alreadyconsumed += $line2 [ 'qty' ];
}
if ( $alreadyconsumed < $line -> qty ) {
$consumptioncomplete = false ;
}
}
if ( $line -> role == 'toproduce' ) {
$arrayoflines = $this -> mo -> fetchLinesLinked ( 'produced' , $line -> id );
$alreadyproduced = 0 ;
foreach ( $arrayoflines as $line2 ) {
$alreadyproduced += $line2 [ 'qty' ];
}
if ( $alreadyproduced < $line -> qty ) {
$productioncomplete = false ;
}
}
}
} else {
$consumptioncomplete = false ;
$productioncomplete = false ;
}
}
}
2021-10-12 12:14:03 +02:00
2021-06-15 11:35:41 +02:00
// Update status of MO
2024-03-16 21:43:38 +01:00
dol_syslog ( " consumptioncomplete = " . json_encode ( $consumptioncomplete ) . " productioncomplete = " . json_encode ( $productioncomplete ));
2021-06-15 11:35:41 +02:00
if ( $consumptioncomplete && $productioncomplete ) {
2023-08-24 13:29:52 +02:00
$result = $this -> mo -> setStatut ( Mo :: STATUS_PRODUCED , 0 , '' , 'MRP_MO_PRODUCED' );
2021-06-15 11:35:41 +02:00
} else {
2023-08-24 13:29:52 +02:00
$result = $this -> mo -> setStatut ( Mo :: STATUS_INPROGRESS , 0 , '' , 'MRP_MO_PRODUCED' );
2021-06-15 11:35:41 +02:00
}
if ( $result <= 0 ) {
throw new RestException ( 500 , $this -> mo -> error );
}
2021-06-11 09:58:03 +02:00
return $this -> mo -> id ;
}
2024-05-29 13:58:51 +02:00
/**
* Produce and consume
*
* Example :
* {
* " inventorylabel " : " Produce and consume using API " ,
* " inventorycode " : " PRODUCEAPI-YY-MM-DD " ,
* " autoclose " : 1 ,
* " arraytoconsume " : [
* {
2024-05-29 14:12:09 +02:00
* " objectid " : " 123 " , -- rowid of MoLine
2024-05-29 13:58:51 +02:00
* " qty " : " 2 " ,
2024-06-30 15:11:57 +02:00
* " fk_warehouse " : " 789 " -- " 0 " or empty , if stock change is disabled .
2024-05-29 13:58:51 +02:00
* }
* ],
* " arraytoproduce " : [
* {
2024-05-29 14:12:09 +02:00
* " objectid " : " 456 " , -- rowid of MoLine
2024-05-29 13:58:51 +02:00
* " qty " : " 1 " ,
2024-05-29 14:12:09 +02:00
* " fk_warehouse " : " 789 " ,
* " pricetoproduce " : " 12.3 " -- optional
2024-05-29 13:58:51 +02:00
* }
* ]
* }
*
* @ param int $id ID of state
* @ param array $request_data Request datas
2025-02-06 22:16:48 +01:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-05-29 13:58:51 +02:00
*
* @ url POST { id } / produceandconsume
*
* @ return int ID of MO
*/
public function produceAndConsume ( $id , $request_data = null )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( " mrp " , " write " )) {
throw new RestException ( 403 , 'Not enough permission' );
}
$result = $this -> mo -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'MO not found' );
}
if ( $this -> mo -> status != Mo :: STATUS_VALIDATED && $this -> mo -> status != Mo :: STATUS_INPROGRESS ) {
throw new RestException ( 405 , 'Error bad status of MO' );
}
// Code for consume and produce...
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/mrp/lib/mrp_mo.lib.php' ;
$stockmove = new MouvementStock ( $this -> db );
$labelmovement = '' ;
$codemovement = '' ;
$autoclose = 1 ;
$arraytoconsume = array ();
$arraytoproduce = array ();
foreach ( $request_data as $field => $value ) {
if ( $field == 'inventorylabel' ) {
$labelmovement = $value ;
}
if ( $field == 'inventorycode' ) {
$codemovement = $value ;
}
if ( $field == 'autoclose' ) {
$autoclose = $value ;
}
if ( $field == 'arraytoconsume' ) {
$arraytoconsume = $value ;
}
if ( $field == 'arraytoproduce' ) {
$arraytoproduce = $value ;
}
if ( $field === 'caller' ) {
2024-05-29 14:46:55 +02: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-05-29 13:58:51 +02:00
$stockmove -> context [ 'caller' ] = $request_data [ 'caller' ];
continue ;
}
}
if ( empty ( $labelmovement )) {
throw new RestException ( 500 , " Field inventorylabel not provided " );
}
if ( empty ( $codemovement )) {
throw new RestException ( 500 , " Field inventorycode not provided " );
}
$this -> db -> begin ();
$pos = 0 ;
$arrayofarrayname = array ( " arraytoconsume " , " arraytoproduce " );
foreach ( $arrayofarrayname as $arrayname ) {
foreach ( ${$arrayname} as $value ) {
if ( empty ( $value [ " objectid " ])) {
throw new RestException ( 500 , " Field objectid required in " . $arrayname );
}
$molinetoprocess = new MoLine ( $this -> db );
$tmpmolineid = $molinetoprocess -> fetch ( $value [ " objectid " ]);
if ( $tmpmolineid <= 0 ) {
throw new RestException ( 500 , " MoLine with rowid " . $value [ " objectid " ] . " not exist. " );
}
$tmpproduct = new Product ( $this -> db );
$tmpproduct -> fetch ( $molinetoprocess -> fk_product );
if ( $tmpproduct -> status_batch ) {
throw new RestException ( 500 , " Product " . $tmpproduct -> ref . " must be in batch, this API can't handle it currently. " );
}
if ( empty ( $value [ " qty " ]) && $value [ " qty " ] != 0 ) {
throw new RestException ( 500 , " Field qty with lower or higher then 0 required in " . $arrayname );
}
$qtytoprocess = $value [ " qty " ];
2024-07-01 12:52:05 +02:00
$fk_warehousetoprocess = 0 ;
2024-06-30 15:11:57 +02:00
if ( $molinetoprocess -> disable_stock_change == false ) {
if ( isset ( $value [ " fk_warehouse " ])) { // If there is a warehouse to set
if ( ! ( $value [ " fk_warehouse " ] > 0 )) { // If there is no warehouse set.
throw new RestException ( 500 , " Field fk_warehouse required in " . $arrayname );
}
2024-05-29 13:58:51 +02:00
}
2024-07-01 12:52:05 +02:00
$fk_warehousetoprocess = ( int ) $value [ " fk_warehouse " ];
2024-05-29 13:58:51 +02:00
}
$pricetoproduce = 0 ;
if ( isset ( $value [ " pricetoproduce " ])) { // If there is a price to produce set.
if ( $value [ " pricetoproduce " ] > 0 ) { // Only use prices grater then 0.
$pricetoproduce = $value [ " pricetoproduce " ];
}
}
$idstockmove = 0 ;
2024-06-30 15:11:57 +02:00
if ( $molinetoprocess -> disable_stock_change == false ) {
// Record stock movement
$id_product_batch = 0 ;
$stockmove -> origin_type = 'mo' ;
$stockmove -> origin_id = $this -> mo -> id ;
if ( $arrayname == " arraytoconsume " ) {
if ( $qtytoprocess >= 0 ) {
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> livraison ( DolibarrApiAccess :: $user , $molinetoprocess -> fk_product , $fk_warehousetoprocess , $qtytoprocess , 0 , $labelmovement , dol_now (), '' , '' , ( string ) $tmpproduct -> status_batch , $id_product_batch , $codemovement );
2024-06-30 15:11:57 +02:00
} else {
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> reception ( DolibarrApiAccess :: $user , $molinetoprocess -> fk_product , $fk_warehousetoprocess , $qtytoprocess , 0 , $labelmovement , '' , '' , ( string ) $tmpproduct -> status_batch , dol_now (), $id_product_batch , $codemovement );
2024-06-30 15:11:57 +02:00
}
2024-05-29 13:58:51 +02:00
} else {
2024-06-30 15:11:57 +02:00
if ( $qtytoprocess >= 0 ) {
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> reception ( DolibarrApiAccess :: $user , $molinetoprocess -> fk_product , $fk_warehousetoprocess , $qtytoprocess , $pricetoproduce , $labelmovement , '' , '' , ( string ) $tmpproduct -> status_batch , dol_now (), $id_product_batch , $codemovement );
2024-06-30 15:11:57 +02:00
} else {
2025-02-06 22:16:48 +01:00
$idstockmove = $stockmove -> livraison ( DolibarrApiAccess :: $user , $molinetoprocess -> fk_product , $fk_warehousetoprocess , $qtytoprocess , 0 , $labelmovement , dol_now (), '' , '' , ( string ) $tmpproduct -> status_batch , $id_product_batch , $codemovement );
2024-06-30 15:11:57 +02:00
}
2024-05-29 13:58:51 +02:00
}
2024-06-30 15:11:57 +02:00
if ( $idstockmove <= 0 ) {
throw new RestException ( 500 , $stockmove -> error );
2024-05-29 13:58:51 +02:00
}
}
// Record consumption
$moline = new MoLine ( $this -> db );
$moline -> fk_mo = $this -> mo -> id ;
$moline -> position = $pos ;
$moline -> fk_product = $tmpproduct -> id ;
2024-06-30 15:11:57 +02:00
$moline -> fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess : null ;
2024-05-29 13:58:51 +02:00
$moline -> qty = $qtytoprocess ;
$moline -> batch = '' ;
$moline -> fk_mrp_production = $molinetoprocess -> id ;
2024-06-30 15:11:57 +02:00
$moline -> fk_stock_movement = $idstockmove > 0 ? $idstockmove : null ;
2024-05-29 13:58:51 +02:00
$moline -> fk_user_creat = DolibarrApiAccess :: $user -> id ;
if ( $arrayname == " arraytoconsume " ) {
$moline -> role = 'consumed' ;
} else {
$moline -> role = 'produced' ;
}
$resultmoline = $moline -> create ( DolibarrApiAccess :: $user );
if ( $resultmoline <= 0 ) {
throw new RestException ( 500 , $moline -> error );
}
2024-05-31 14:59:16 +02:00
$pos ++ ;
2024-05-29 13:58:51 +02:00
}
}
$consumptioncomplete = true ;
$productioncomplete = true ;
if ( $autoclose > 0 ) {
// Refresh Lines after consumptions.
$this -> mo -> fetchLines ();
foreach ( $this -> mo -> lines as $line ) {
if ( $line -> role == 'toconsume' ) {
$arrayoflines = $this -> mo -> fetchLinesLinked ( 'consumed' , $line -> id );
$alreadyconsumed = 0 ;
foreach ( $arrayoflines as $line2 ) {
$alreadyconsumed += $line2 [ 'qty' ];
}
if ( $alreadyconsumed < $line -> qty ) {
$consumptioncomplete = false ;
}
}
if ( $line -> role == 'toproduce' ) {
$arrayoflines = $this -> mo -> fetchLinesLinked ( 'produced' , $line -> id );
$alreadyproduced = 0 ;
foreach ( $arrayoflines as $line2 ) {
$alreadyproduced += $line2 [ 'qty' ];
}
if ( $alreadyproduced < $line -> qty ) {
$productioncomplete = false ;
}
}
}
} else {
$consumptioncomplete = false ;
$productioncomplete = false ;
}
// Update status of MO
2024-05-31 14:59:16 +02:00
dol_syslog ( " consumptioncomplete = " . ( string ) $consumptioncomplete . " productioncomplete = " . ( string ) $productioncomplete );
2024-05-29 13:58:51 +02:00
//var_dump("consumptioncomplete = ".$consumptioncomplete." productioncomplete = ".$productioncomplete);
2024-05-31 15:38:36 +02:00
if ( $consumptioncomplete && $productioncomplete ) {
2024-05-29 13:58:51 +02:00
$result = $this -> mo -> setStatut ( Mo :: STATUS_PRODUCED , 0 , '' , 'MRP_MO_PRODUCED' );
} else {
$result = $this -> mo -> setStatut ( Mo :: STATUS_INPROGRESS , 0 , '' , 'MRP_MO_PRODUCED' );
}
if ( $result <= 0 ) {
throw new RestException ( 500 , $this -> mo -> error );
}
$this -> db -> commit ();
return $this -> mo -> id ;
}
2021-06-11 09:58:03 +02:00
2020-06-18 01:09:30 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
*
2023-09-26 18:04:48 +02:00
* @ param Object $object Object to clean
* @ return Object Object with cleaned properties
2020-06-18 01:09:30 +02: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 );
// If object has lines, remove $db property
if ( isset ( $object -> lines ) && is_array ( $object -> lines ) && count ( $object -> lines ) > 0 ) {
$nboflines = count ( $object -> lines );
2020-12-11 19:02:13 +01:00
for ( $i = 0 ; $i < $nboflines ; $i ++ ) {
2020-06-18 01:09:30 +02:00
$this -> _cleanObjectDatas ( $object -> lines [ $i ]);
unset ( $object -> lines [ $i ] -> lines );
unset ( $object -> lines [ $i ] -> note );
}
}
return $object ;
}
/**
2025-02-06 22:16:48 +01:00
* Validate fields before creating or updating an object
2020-06-18 01:09:30 +02:00
*
2025-02-06 22:16:48 +01:00
* @ param ? array < null | int | float | string > $data Data to validate
* @ return array < string , null | int | float | string >
2020-06-18 01:09:30 +02:00
*
* @ throws RestException
*/
private function _validate ( $data )
{
$myobject = array ();
foreach ( $this -> mo -> fields as $field => $propfield ) {
2020-12-11 19:02:13 +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-06-18 01:09:30 +02:00
throw new RestException ( 400 , " $field field missing " );
2020-12-11 19:02:13 +01:00
}
2021-09-13 01:32:20 +02:00
$myobject [ $field ] = $data [ $field ];
2020-06-18 01:09:30 +02:00
}
return $myobject ;
}
2023-01-08 21:43:12 +01:00
/**
* Validate the ref field and get the next Number if it ' s necessary .
*
* @ return void
*/
2024-01-21 17:37:08 +01:00
private function checkRefNumbering ()
2023-01-08 21:43:12 +01:00
{
$ref = substr ( $this -> mo -> ref , 1 , 4 );
if ( $this -> mo -> 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. " );
}
if ( strtolower ( $this -> mo -> ref ) == 'auto' ) {
if ( empty ( $this -> mo -> id ) && $this -> mo -> status == 0 ) {
$this -> mo -> ref = '' ; // 'ref' will auto incremented with '(PROV' + newID + ')'
} else {
$this -> mo -> fetch_product ();
$numref = $this -> mo -> getNextNumRef ( $this -> mo -> product );
$this -> mo -> ref = $numref ;
}
}
}
2020-06-18 01:09:30 +02:00
}