2019-01-18 17:56:56 +01:00
< ? php
2019-03-10 09:00:59 +01:00
/* Copyright ( C ) 2019 Thibault FOUCART < support @ ptibogxiv . net >
* Copyright ( C ) 2019 Laurent Destailleur < eldy @ users . sourceforge . net >
2019-01-18 17:56:56 +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-01-18 17:56:56 +01:00
*/
2021-05-25 20:17:54 +02:00
use Luracast\Restler\RestException ;
2019-01-18 17:56:56 +01:00
2021-05-25 20:17:54 +02:00
require_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php' ;
2019-01-18 17:56:56 +01:00
/**
* API class for donations
*
* @ access protected
* @ class DolibarrApiAccess { @ requires user , external }
*/
class Donations extends DolibarrApi
{
2020-10-31 14:32:18 +01:00
/**
* @ var array $FIELDS Mandatory fields , checked when create and update object
*/
2021-02-25 22:18:34 +01:00
public static $FIELDS = array (
2021-05-21 16:01:04 +02:00
'amount'
2020-10-31 14:32:18 +01:00
);
/**
* @ var Don $don { @ type Don }
*/
public $don ;
/**
* Constructor
*/
public function __construct ()
{
global $db , $conf ;
$this -> db = $db ;
$this -> don = new Don ( $this -> db );
}
/**
* Get properties of an donation object
*
2024-01-13 19:48:41 +01:00
* Return an array with donation information
2020-10-31 14:32:18 +01:00
*
2023-06-19 00:52:43 +02:00
* @ param int $id ID of order
2023-09-26 18:43:25 +02:00
* @ return Object Object with cleaned properties
2020-10-31 14:32:18 +01:00
*
2023-09-26 18:43:25 +02:00
* @ throws RestException
2020-10-31 14:32:18 +01:00
*/
public function get ( $id )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'don' , 'lire' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2019-01-18 17:56:56 +01:00
}
2020-10-31 14:32:18 +01:00
$result = $this -> don -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Donation not found' );
}
2019-01-18 17:56:56 +01:00
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'don' , $this -> don -> id )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2019-01-18 17:56:56 +01:00
}
// Add external contacts ids
2019-02-24 16:36:06 +01:00
//$this->don->contacts_ids = $this->don->liste_contact(-1,'external',1);
//$this->don->fetchObjectLinked();
2019-01-18 17:56:56 +01:00
return $this -> _cleanObjectDatas ( $this -> don );
}
2020-10-31 14:32:18 +01:00
/**
* List donations
*
* Get a list of donations
*
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
* @ param string $thirdparty_ids Thirdparty ids to filter orders of ( example '1' or '1,2,3' ) { @ pattern /^ [ 0 - 9 ,] * $ / i }
* @ 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-13 19:48:41 +01:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
2024-08-18 23:12:07 +02:00
* @ param bool $pagination_data If this parameter is set to true the response will include pagination data . Default value is false . Page starts from 0 *
2020-10-31 14:32:18 +01:00
* @ return array Array of order objects
*
* @ throws RestException
*/
2024-08-18 23:12:07 +02:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $thirdparty_ids = '' , $sqlfilters = '' , $properties = '' , $pagination_data = false )
2020-10-31 14:32:18 +01:00
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'don' , 'lire' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2021-04-08 19:05:28 +02:00
}
2020-10-31 14:32:18 +01:00
$obj_ret = array ();
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid
$socids = DolibarrApiAccess :: $user -> socid ? DolibarrApiAccess :: $user -> socid : $thirdparty_ids ;
$sql = " SELECT t.rowid " ;
2024-02-09 15:58:49 +01:00
if (( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'client' , 'voir' ) && ! $socids )) {
2021-02-25 22:18:34 +01:00
$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)
}
2023-04-28 09:31:33 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " don AS t LEFT JOIN " . MAIN_DB_PREFIX . " don_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 14:32:18 +01:00
$sql .= ' WHERE t.entity IN (' . getEntity ( 'don' ) . ')' ;
2024-02-09 15:58:49 +01:00
if (( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'client' , 'voir' ) && ! $socids )) {
2021-02-25 22:18:34 +01:00
$sql .= " AND t.fk_soc = sc.fk_soc " ;
}
if ( $thirdparty_ids ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND t.fk_soc = " . (( int ) $thirdparty_ids ) . " " ;
2021-02-25 22:18:34 +01:00
}
2020-10-31 14:32:18 +01:00
// Add sql filters
2021-02-25 22:18:34 +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 14:32:18 +01:00
}
}
2024-08-18 23:12:07 +02:00
//this query will return total orders with the filters given
$sqlTotals = str_replace ( 'SELECT t.rowid' , 'SELECT count(t.rowid) as total' , $sql );
2020-10-31 14:32:18 +01:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
2021-02-25 22:18:34 +01:00
if ( $page < 0 ) {
2020-10-31 14:32:18 +01:00
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
dol_syslog ( " API Rest request " );
$result = $this -> db -> query ( $sql );
2021-02-25 22:18:34 +01:00
if ( $result ) {
2020-10-31 14:32:18 +01:00
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
$i = 0 ;
2021-02-25 22:18:34 +01:00
while ( $i < $min ) {
2020-10-31 14:32:18 +01:00
$obj = $this -> db -> fetch_object ( $result );
$don_static = new Don ( $this -> db );
if ( $don_static -> fetch ( $obj -> rowid )) {
// Add external contacts ids
//$don_static->contacts_ids = $don_static->liste_contact(-1, 'external', 1);
2023-09-26 18:04:48 +02:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $don_static ), $properties );
2020-10-31 14:32:18 +01:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve donation list : ' . $this -> db -> lasterror ());
}
2019-02-25 20:35:59 +01:00
2024-08-18 23:12:07 +02:00
//if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
if ( $pagination_data ) {
$totalsResult = $this -> db -> query ( $sqlTotals );
$total = $this -> db -> fetch_object ( $totalsResult ) -> total ;
$tmp = $obj_ret ;
$obj_ret = [];
$obj_ret [ 'data' ] = $tmp ;
$obj_ret [ 'pagination' ] = [
'total' => ( int ) $total ,
'page' => $page , //count starts from 0
'page_count' => ceil (( int ) $total / $limit ),
'limit' => $limit
];
}
2019-01-18 17:56:56 +01:00
return $obj_ret ;
2020-10-31 14:32:18 +01:00
}
/**
* Create donation object
*
* @ param array $request_data Request data
* @ return int ID of order
*/
public function post ( $request_data = null )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'don' , 'creer' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , " Insuffisant rights " );
2020-10-31 14:32:18 +01:00
}
2021-04-08 19:05:28 +02:00
2020-10-31 14:32:18 +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-13 19:48:41 +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 -> don -> 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 -> don -> $field = $this -> _checkValForAPI ( $field , $value , $this -> don );
2020-10-31 14:32:18 +01:00
}
/* if ( isset ( $request_data [ " lines " ])) {
2021-02-25 22:18:34 +01:00
$lines = array ();
foreach ( $request_data [ " lines " ] as $line ) {
array_push ( $lines , ( object ) $line );
}
$this -> don -> lines = $lines ;
} */
2019-01-18 17:56:56 +01:00
2020-10-31 14:32:18 +01:00
if ( $this -> don -> create ( DolibarrApiAccess :: $user ) < 0 ) {
2021-05-21 16:01:04 +02:00
throw new RestException ( 500 , " Error creating donation " , array_merge ( array ( $this -> don -> error ), $this -> don -> errors ));
2020-10-31 14:32:18 +01:00
}
return $this -> don -> id ;
}
/**
* Update order general fields ( won ' t touch lines of order )
*
2024-02-22 01:32:55 +01:00
* @ param int $id Id of order to update
* @ param array $request_data Datas
* @ return Object Updated object
2020-10-31 14:32:18 +01:00
*/
public function put ( $id , $request_data = null )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'don' , 'creer' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2019-01-18 17:56:56 +01:00
}
2020-10-31 14:32:18 +01:00
$result = $this -> don -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Donation not found' );
}
2019-01-18 17:56:56 +01:00
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'donation' , $this -> don -> id )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2019-01-18 17:56:56 +01:00
}
2020-10-31 14:32:18 +01:00
foreach ( $request_data as $field => $value ) {
2021-02-25 22:18:34 +01:00
if ( $field == 'id' ) {
continue ;
}
2023-12-15 12:15:33 +01:00
if ( $field === 'caller' ) {
2024-01-13 19:48:41 +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 -> don -> 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 -> don -> array_options [ $index ] = $this -> _checkValForAPI ( $field , $val , $this -> don );
}
continue ;
}
2025-01-20 14:57:45 +01:00
2024-04-02 12:28:55 +02:00
$this -> don -> $field = $this -> _checkValForAPI ( $field , $value , $this -> don );
2020-10-31 14:32:18 +01:00
}
2021-02-25 22:18:34 +01:00
if ( $this -> don -> update ( DolibarrApiAccess :: $user ) > 0 ) {
2020-10-31 14:32:18 +01:00
return $this -> get ( $id );
} else {
throw new RestException ( 500 , $this -> don -> error );
}
}
/**
* Delete donation
*
* @ param int $id Order ID
* @ return array
*/
public function delete ( $id )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'don' , 'supprimer' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2019-01-18 17:56:56 +01:00
}
2021-04-08 19:05:28 +02:00
2020-10-31 14:32:18 +01:00
$result = $this -> don -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Donation not found' );
}
2019-01-18 17:56:56 +01:00
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'donation' , $this -> don -> id )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2019-01-18 17:56:56 +01:00
}
2020-10-31 14:32:18 +01:00
if ( ! $this -> don -> delete ( DolibarrApiAccess :: $user )) {
throw new RestException ( 500 , 'Error when delete donation : ' . $this -> don -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'Donation deleted'
)
);
}
/**
* Validate an donation
*
2019-01-18 17:56:56 +01:00
* If you get a bad value for param notrigger check , provide this in body
2020-10-31 14:32:18 +01:00
* {
* " idwarehouse " : 0 ,
* " notrigger " : 0
* }
*
* @ param int $id Order ID
* @ param int $idwarehouse Warehouse ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
*
* @ url POST { id } / validate
*
2020-02-20 12:59:53 +01:00
* @ throws RestException 304
2024-02-01 19:16:58 +01:00
* @ throws RestException 403
2020-10-31 14:32:18 +01:00
* @ throws RestException 404
2022-03-18 16:14:20 +01:00
* @ throws RestException 500 System error
2020-10-31 14:32:18 +01:00
*
2023-01-24 23:11:28 +01:00
* @ return object
2020-10-31 14:32:18 +01:00
*/
public function validate ( $id , $idwarehouse = 0 , $notrigger = 0 )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'don' , 'creer' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2019-01-18 17:56:56 +01:00
}
2021-04-08 19:05:28 +02:00
2020-10-31 14:32:18 +01:00
$result = $this -> don -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Donation not found' );
}
2019-01-18 17:56:56 +01:00
2020-04-10 10:59:32 +02:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'don' , $this -> don -> id )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2019-01-18 17:56:56 +01:00
}
2024-03-07 20:16:48 +01:00
// @phan-suppress-next-line PhanPluginSuspiciousParamPosition
2021-05-23 18:09:10 +02:00
$result = $this -> don -> valid_promesse ( $id , DolibarrApiAccess :: $user -> id , $notrigger );
2019-01-18 17:56:56 +01:00
if ( $result == 0 ) {
2020-10-31 18:51:30 +01:00
throw new RestException ( 304 , 'Error nothing done. May be object is already validated' );
2019-01-18 17:56:56 +01:00
}
if ( $result < 0 ) {
2020-10-31 18:51:30 +01:00
throw new RestException ( 500 , 'Error when validating Order: ' . $this -> don -> error );
2019-01-18 17:56:56 +01:00
}
2020-10-31 18:51:30 +01:00
$result = $this -> don -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Order not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'don' , $this -> don -> id )) {
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
}
$this -> don -> fetchObjectLinked ();
return $this -> _cleanObjectDatas ( $this -> don );
}
// 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 -> note );
unset ( $object -> address );
unset ( $object -> barcode_type );
unset ( $object -> barcode_type_code );
unset ( $object -> barcode_type_label );
unset ( $object -> barcode_type_coder );
return $object ;
}
/**
* Validate fields before create or update object
*
* @ param array $data Array with data to verify
* @ return array
* @ throws RestException
*/
private function _validate ( $data )
{
$don = array ();
2021-05-21 16:01:04 +02:00
foreach ( Donations :: $FIELDS as $field ) {
2021-02-25 22:18:34 +01:00
if ( ! isset ( $data [ $field ])) {
2020-10-31 18:51:30 +01:00
throw new RestException ( 400 , $field . " field missing " );
2021-02-25 22:18:34 +01:00
}
2020-10-31 18:51:30 +01:00
$don [ $field ] = $data [ $field ];
}
return $don ;
}
2019-01-18 17:56:56 +01:00
}