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 >
*
* 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
/**
2019-03-06 18:04:36 +01:00
* \file 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
{
/**
2019-03-06 18:04:36 +01:00
* @ var BOM $bom { @ type BOM }
2019-02-26 19:27:04 +01:00
*/
public $bom ;
/**
* Constructor
*/
2019-02-26 21:48:21 +01:00
public function __construct ()
2019-02-26 19:27:04 +01:00
{
global $db , $conf ;
$this -> db = $db ;
2019-03-06 18:04:36 +01:00
$this -> bom = new BOM ( $this -> db );
2019-02-26 19:27:04 +01:00
}
/**
* Get properties of a bom object
*
* Return an array with bom informations
*
* @ param int $id ID of bom
* @ return array | mixed data without useless information
*
2019-03-06 18:04:36 +01:00
* @ url GET { id }
2019-02-26 19:27:04 +01:00
* @ throws RestException
*/
2019-02-26 21:48:21 +01:00
public function get ( $id )
2019-02-26 19:27:04 +01:00
{
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> bom -> read ) {
2019-02-26 19:27:04 +01:00
throw new RestException ( 401 );
}
$result = $this -> bom -> fetch ( $id );
2020-04-10 10:59:32 +02:00
if ( ! $result ) {
2019-03-06 18:04:36 +01:00
throw new RestException ( 404 , 'BOM not found' );
2019-02-26 19:27:04 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom' , $this -> bom -> id , 'bom_bom' )) {
2019-02-26 19:27:04 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
return $this -> _cleanObjectDatas ( $this -> bom );
}
/**
* List boms
*
* Get a list of boms
*
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
* @ param string $sqlfilters Other criteria to filter answers separated by a comma . Syntax example " (t.ref:like:'SO-%') and (t.date_creation:<:'20160101') "
* @ return array Array of order objects
*
* @ throws RestException
*/
2019-02-26 21:48:21 +01:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $sqlfilters = '' )
2019-02-26 19:27:04 +01:00
{
global $db , $conf ;
$obj_ret = array ();
2020-09-19 23:30:29 +02:00
$tmpobject = new BOM ( $this -> db );
2019-10-20 17:17:22 +02:00
2019-09-06 10:53:05 +02:00
$socid = DolibarrApiAccess :: $user -> socid ? DolibarrApiAccess :: $user -> socid : '' ;
2019-02-26 19:27:04 +01:00
2020-04-10 10:59:32 +02:00
$restrictonsocid = 0 ; // Set to 1 if there is a field socid in table of object
2019-02-26 19:27:04 +01:00
// If the internal user must only see his customers, force searching by him
$search_sale = 0 ;
2020-04-10 10:59:32 +02:00
if ( $restrictonsocid && ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socid ) $search_sale = DolibarrApiAccess :: $user -> id ;
2019-02-26 19:27:04 +01:00
$sql = " SELECT t.rowid " ;
2019-05-02 12:19:43 +02:00
if ( $restrictonsocid && ( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socid ) || $search_sale > 0 ) $sql .= " , sc.fk_soc, sc.fk_user " ; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . $tmpobject -> table_element . " as t " ;
2019-02-26 19:27:04 +01:00
2020-04-10 10:59:32 +02:00
if ( $restrictonsocid && ( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socid ) || $search_sale > 0 ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ; // We need this table joined to the select in order to filter by sale
$sql .= " WHERE 1 = 1 " ;
2019-02-26 19:27:04 +01:00
// Example of use $mode
//if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
//if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
2020-06-17 22:11:22 +02:00
if ( $tmpobject -> ismultientitymanaged ) $sql .= ' AND t.entity IN (' . getEntity ( $tmpobject -> element ) . ')' ;
2020-04-10 10:59:32 +02:00
if ( $restrictonsocid && ( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socid ) || $search_sale > 0 ) $sql .= " AND t.fk_soc = sc.fk_soc " ;
if ( $restrictonsocid && $socid ) $sql .= " AND t.fk_soc = " . $socid ;
if ( $restrictonsocid && $search_sale > 0 ) $sql .= " AND t.rowid = sc.fk_soc " ; // Join for the needed table to filter by sale
2019-02-26 19:27:04 +01:00
// Insert sale filter
2019-05-02 12:19:43 +02:00
if ( $restrictonsocid && $search_sale > 0 )
2019-02-26 19:27:04 +01:00
{
$sql .= " AND sc.fk_user = " . $search_sale ;
}
if ( $sqlfilters )
{
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApi :: _checkFilters ( $sqlfilters ))
2019-02-26 19:27:04 +01:00
{
throw new RestException ( 503 , 'Error when validating parameter sqlfilters ' . $sqlfilters );
}
2020-04-10 10:59:32 +02:00
$regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)' ;
$sql .= " AND ( " . preg_replace_callback ( '/' . $regexstring . '/' , 'DolibarrApi::_forge_criteria_callback' , $sqlfilters ) . " ) " ;
2019-02-26 19:27:04 +01:00
}
2020-09-19 23:30:29 +02:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
2020-04-10 10:59:32 +02:00
if ( $limit ) {
2019-02-26 19:27:04 +01:00
if ( $page < 0 )
{
$page = 0 ;
}
$offset = $limit * $page ;
2020-09-19 23:30:29 +02:00
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
2019-02-26 19:27:04 +01:00
}
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2019-02-26 19:27:04 +01:00
if ( $result )
{
2020-09-19 23:30:29 +02:00
$num = $this -> db -> num_rows ( $result );
2019-03-09 12:01:16 +01:00
$i = 0 ;
2019-02-26 19:27:04 +01:00
while ( $i < $num )
{
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
$bom_static = new BOM ( $this -> db );
2019-03-09 12:01:16 +01:00
if ( $bom_static -> fetch ( $obj -> rowid )) {
2019-02-26 19:27:04 +01:00
$obj_ret [] = $this -> _cleanObjectDatas ( $bom_static );
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-02-26 19:27:04 +01:00
throw new RestException ( 503 , 'Error when retrieve bom list' );
}
2020-04-10 10:59:32 +02:00
if ( ! count ( $obj_ret )) {
2019-02-26 19:27:04 +01:00
throw new RestException ( 404 , 'No bom found' );
}
return $obj_ret ;
}
/**
* Create bom object
*
* @ param array $request_data Request datas
* @ return int ID of bom
*/
2019-02-26 21:48:21 +01:00
public function post ( $request_data = null )
2019-02-26 19:27:04 +01:00
{
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> bom -> write ) {
2019-02-26 19:27:04 +01:00
throw new RestException ( 401 );
}
// Check mandatory fields
$result = $this -> _validate ( $request_data );
2020-04-10 10:59:32 +02:00
foreach ( $request_data as $field => $value ) {
2019-02-26 19:27:04 +01:00
$this -> bom -> $field = $value ;
}
2020-04-10 10:59:32 +02:00
if ( ! $this -> bom -> create ( DolibarrApiAccess :: $user )) {
2019-03-06 18:04:36 +01:00
throw new RestException ( 500 , " Error creating BOM " , array_merge ( array ( $this -> bom -> error ), $this -> bom -> errors ));
2019-02-26 19:27:04 +01:00
}
return $this -> bom -> id ;
}
/**
* Update bom
*
* @ param int $id Id of bom to update
* @ param array $request_data Datas
*
2019-03-06 18:04:36 +01:00
* @ return int
2019-02-26 19:27:04 +01:00
*/
2019-02-26 21:48:21 +01:00
public function put ( $id , $request_data = null )
2019-02-26 19:27:04 +01:00
{
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> bom -> write ) {
2019-02-26 19:27:04 +01:00
throw new RestException ( 401 );
}
$result = $this -> bom -> fetch ( $id );
2020-04-10 10:59:32 +02:00
if ( ! $result ) {
2019-03-06 18:04:36 +01:00
throw new RestException ( 404 , 'BOM not found' );
2019-02-26 19:27:04 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom' , $this -> bom -> id , 'bom_bom' )) {
2019-02-26 19:27:04 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2020-04-10 10:59:32 +02:00
foreach ( $request_data as $field => $value ) {
2019-03-06 18:04:36 +01:00
if ( $field == 'id' ) continue ;
2019-02-26 19:27:04 +01:00
$this -> bom -> $field = $value ;
}
2020-04-10 10:59:32 +02:00
if ( $this -> bom -> update ( $id , DolibarrApiAccess :: $user ) > 0 )
2019-03-06 18:04:36 +01:00
{
2019-02-26 19:27:04 +01:00
return $this -> get ( $id );
2020-05-21 15:05:19 +02:00
} else {
2019-05-02 12:19:43 +02:00
throw new RestException ( 500 , $this -> bom -> error );
2019-03-06 18:04:36 +01:00
}
2019-02-26 19:27:04 +01:00
}
/**
* Delete bom
*
2019-03-06 18:04:36 +01:00
* @ param int $id BOM ID
2019-02-26 19:27:04 +01:00
* @ return array
*/
2019-02-26 21:48:21 +01:00
public function delete ( $id )
2019-02-26 19:27:04 +01:00
{
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> bom -> delete ) {
2019-02-26 19:27:04 +01:00
throw new RestException ( 401 );
}
$result = $this -> bom -> fetch ( $id );
2020-04-10 10:59:32 +02:00
if ( ! $result ) {
2019-03-06 18:04:36 +01:00
throw new RestException ( 404 , 'BOM not found' );
2019-02-26 19:27:04 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'bom' , $this -> bom -> id , 'bom_bom' )) {
2019-02-26 19:27:04 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2020-04-10 10:59:32 +02:00
if ( ! $this -> bom -> delete ( DolibarrApiAccess :: $user ))
2019-02-26 19:27:04 +01:00
{
2019-03-06 18:04:36 +01:00
throw new RestException ( 500 , 'Error when deleting BOM : ' . $this -> bom -> error );
2019-02-26 19:27:04 +01:00
}
2019-10-31 22:15:18 +01:00
return array (
2019-02-26 19:27:04 +01:00
'success' => array (
'code' => 200 ,
2019-03-06 18:04:36 +01:00
'message' => 'BOM deleted'
2019-02-26 19:27:04 +01:00
)
);
}
2019-03-04 19:57:46 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2019-02-26 19:27:04 +01:00
/**
* Clean sensible object datas
*
2019-03-06 18:04:36 +01:00
* @ param object $object Object to clean
* @ return array Array of cleaned object properties
2019-02-26 19:27:04 +01:00
*/
2019-03-04 19:57:46 +01:00
protected function _cleanObjectDatas ( $object )
2019-02-26 19:27:04 +01:00
{
2019-03-04 19:57:46 +01:00
// phpcs:enable
2019-02-26 19:27:04 +01:00
$object = parent :: _cleanObjectDatas ( $object );
2019-11-02 14:49:26 +01:00
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 );
2019-02-26 19:27:04 +01:00
unset ( $object -> barcode_type );
unset ( $object -> barcode_type_code );
unset ( $object -> barcode_type_label );
2019-11-02 14:49:26 +01:00
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
2020-04-10 10:59:32 +02:00
if ( isset ( $object -> lines ) && is_array ( $object -> lines ) && count ( $object -> lines ) > 0 ) {
2019-11-02 14:49:26 +01:00
$nboflines = count ( $object -> lines );
2020-04-10 10:59:32 +02:00
for ( $i = 0 ; $i < $nboflines ; $i ++ )
2019-11-02 14:49:26 +01:00
{
$this -> _cleanObjectDatas ( $object -> lines [ $i ]);
unset ( $object -> lines [ $i ] -> lines );
unset ( $object -> lines [ $i ] -> note );
}
}
2019-02-26 19:27:04 +01:00
return $object ;
}
/**
* Validate fields before create or update object
*
2019-03-06 18:04:36 +01:00
* @ param array $data Array of data to validate
* @ return array
2019-02-26 19:27:04 +01:00
*
2019-03-06 18:04:36 +01:00
* @ throws RestException
2019-02-26 19:27:04 +01:00
*/
2019-02-26 21:48:21 +01:00
private function _validate ( $data )
2019-02-26 19:27:04 +01:00
{
2019-05-02 12:19:43 +02:00
$myobject = array ();
2019-09-30 21:51:04 +02:00
foreach ( $this -> bom -> fields as $field => $propfield ) {
2020-04-10 10:59:32 +02:00
if ( in_array ( $field , array ( 'rowid' , 'entity' , 'date_creation' , 'tms' , 'fk_user_creat' )) || $propfield [ 'notnull' ] != 1 ) continue ; // Not a mandatory field
2019-02-26 19:27:04 +01:00
if ( ! isset ( $data [ $field ]))
throw new RestException ( 400 , " $field field missing " );
2019-05-02 12:19:43 +02:00
$myobject [ $field ] = $data [ $field ];
2019-02-26 19:27:04 +01:00
}
2019-05-02 12:19:43 +02:00
return $myobject ;
2019-02-26 19:27:04 +01:00
}
}