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
*/
use Luracast\Restler\RestException ;
require_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php' ;
/**
* API class for donations
*
* @ access protected
* @ class DolibarrApiAccess { @ requires user , external }
*/
class Donations extends DolibarrApi
{
/**
* @ var array $FIELDS Mandatory fields , checked when create and update object
*/
static $FIELDS = array (
'socid'
);
/**
* @ var Don $don { @ type Don }
*/
public $don ;
/**
* Constructor
*/
2019-02-25 20:35:59 +01:00
public function __construct ()
2019-01-18 17:56:56 +01:00
{
2019-02-25 20:35:59 +01:00
global $db , $conf ;
$this -> db = $db ;
2019-02-24 14:30:02 +01:00
$this -> don = new Don ( $this -> db );
2019-01-18 17:56:56 +01:00
}
/**
* Get properties of an donation object
*
* Return an array with donation informations
*
* @ param int $id ID of order
* @ return array | mixed data without useless information
2019-03-10 09:00:59 +01:00
*
2019-01-18 17:56:56 +01:00
* @ throws RestException
*/
2019-02-25 20:35:59 +01:00
public function get ( $id )
2019-01-18 17:56:56 +01:00
{
if ( ! DolibarrApiAccess :: $user -> rights -> don -> lire ) {
throw new RestException ( 401 );
}
$result = $this -> don -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Donation not found' );
}
2019-02-26 18:19:44 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'don' , $this -> don -> id )) {
2019-01-18 17:56:56 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
// 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 );
}
/**
* List donations
*
2019-02-24 16:36:06 +01:00
* Get a list of donations
2019-01-18 17:56:56 +01:00
*
2019-03-10 09:00:59 +01:00
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
2019-08-30 17:14:59 +02:00
* @ param string $thirdparty_ids Thirdparty ids to filter orders of ( example '1' or '1,2,3' ) { @ pattern /^ [ 0 - 9 ,] * $ / i }
2019-03-10 09:00:59 +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') "
* @ return array Array of order objects
2019-01-18 17:56:56 +01:00
*
* @ throws RestException
*/
2019-02-25 20:35:59 +01:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $thirdparty_ids = '' , $sqlfilters = '' )
2019-01-18 17:56:56 +01:00
{
global $db , $conf ;
$obj_ret = array ();
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid
2019-09-06 10:53:05 +02:00
$socids = DolibarrApiAccess :: $user -> socid ? DolibarrApiAccess :: $user -> socid : $thirdparty_ids ;
2019-01-18 17:56:56 +01:00
$sql = " SELECT t.rowid " ;
2019-02-24 16:36:06 +01:00
if (( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) ) $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)
2019-01-18 17:56:56 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " don as t " ;
$sql .= ' WHERE t.entity IN (' . getEntity ( 'don' ) . ')' ;
2019-02-24 16:36:06 +01:00
if (( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) ) $sql .= " AND t.fk_soc = sc.fk_soc " ;
if ( $thirdparty_ids ) $sql .= " AND t.fk_soc = " . $thirdparty_ids . " " ;
2019-02-25 20:35:59 +01:00
2019-01-18 17:56:56 +01:00
// Add sql filters
if ( $sqlfilters )
{
if ( ! DolibarrApi :: _checkFilters ( $sqlfilters ))
{
throw new RestException ( 503 , 'Error when validating parameter sqlfilters ' . $sqlfilters );
}
$regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)' ;
$sql .= " AND ( " . preg_replace_callback ( '/' . $regexstring . '/' , 'DolibarrApi::_forge_criteria_callback' , $sqlfilters ) . " ) " ;
}
$sql .= $db -> order ( $sortfield , $sortorder );
if ( $limit ) {
if ( $page < 0 )
{
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $db -> plimit ( $limit + 1 , $offset );
}
dol_syslog ( " API Rest request " );
$result = $db -> query ( $sql );
2019-02-25 20:35:59 +01:00
2019-01-18 17:56:56 +01:00
if ( $result )
{
$num = $db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
$i = 0 ;
while ( $i < $min )
{
$obj = $db -> fetch_object ( $result );
2019-02-24 16:36:06 +01:00
$don_static = new Don ( $db );
if ( $don_static -> fetch ( $obj -> rowid )) {
2019-01-18 17:56:56 +01:00
// Add external contacts ids
2019-02-24 16:36:06 +01:00
//$don_static->contacts_ids = $don_static->liste_contact(-1, 'external', 1);
$obj_ret [] = $this -> _cleanObjectDatas ( $don_static );
2019-01-18 17:56:56 +01:00
}
$i ++ ;
}
}
else {
2019-02-24 16:36:06 +01:00
throw new RestException ( 503 , 'Error when retrieve donation list : ' . $db -> lasterror ());
2019-01-18 17:56:56 +01:00
}
if ( ! count ( $obj_ret )) {
2019-02-24 16:36:06 +01:00
throw new RestException ( 404 , 'No donation found' );
2019-01-18 17:56:56 +01:00
}
2019-02-25 20:35:59 +01:00
2019-01-18 17:56:56 +01:00
return $obj_ret ;
}
/**
* Create donation object
*
* @ param array $request_data Request data
* @ return int ID of order
*/
2019-02-25 20:35:59 +01:00
public function post ( $request_data = null )
2019-01-18 17:56:56 +01:00
{
2019-02-25 20:35:59 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> don -> creer ) {
2019-03-10 09:00:59 +01:00
throw new RestException ( 401 , " Insuffisant rights " );
}
2019-01-18 17:56:56 +01:00
// Check mandatory fields
$result = $this -> _validate ( $request_data );
2019-02-25 20:35:59 +01:00
foreach ( $request_data as $field => $value ) {
2019-02-24 16:36:06 +01:00
$this -> don -> $field = $value ;
2019-01-18 17:56:56 +01:00
}
/* if ( isset ( $request_data [ " lines " ])) {
$lines = array ();
foreach ( $request_data [ " lines " ] as $line ) {
array_push ( $lines , ( object ) $line );
}
2019-02-24 16:36:06 +01:00
$this -> don -> lines = $lines ;
2019-01-18 17:56:56 +01:00
} */
2019-02-24 16:36:06 +01:00
if ( $this -> don -> create ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , " Error creating order " , array_merge ( array ( $this -> don -> error ), $this -> don -> errors ));
2019-01-18 17:56:56 +01:00
}
2019-02-24 16:36:06 +01:00
return $this -> don -> id ;
2019-01-18 17:56:56 +01:00
}
/**
* Update order general fields ( won ' t touch lines of order )
*
* @ param int $id Id of order to update
* @ param array $request_data Datas
*
* @ return int
*/
2019-02-25 20:35:59 +01:00
public function put ( $id , $request_data = null )
2019-01-18 17:56:56 +01:00
{
2019-02-24 16:36:06 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> don -> creer ) {
2019-01-18 17:56:56 +01:00
throw new RestException ( 401 );
}
2019-02-24 16:36:06 +01:00
$result = $this -> don -> fetch ( $id );
2019-01-18 17:56:56 +01:00
if ( ! $result ) {
2019-02-24 16:36:06 +01:00
throw new RestException ( 404 , 'Donation not found' );
2019-01-18 17:56:56 +01:00
}
2019-02-24 16:36:06 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'donation' , $this -> don -> id )) {
2019-01-18 17:56:56 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-02-25 20:35:59 +01:00
foreach ( $request_data as $field => $value ) {
2019-01-18 17:56:56 +01:00
if ( $field == 'id' ) continue ;
2019-02-24 16:36:06 +01:00
$this -> don -> $field = $value ;
2019-01-18 17:56:56 +01:00
}
2019-02-24 16:36:06 +01:00
if ( $this -> don -> update ( DolibarrApiAccess :: $user ) > 0 )
2019-01-18 17:56:56 +01:00
{
return $this -> get ( $id );
}
else
{
2019-02-24 16:36:06 +01:00
throw new RestException ( 500 , $this -> don -> error );
2019-01-18 17:56:56 +01:00
}
}
/**
* Delete donation
*
* @ param int $id Order ID
* @ return array
*/
2019-02-25 20:35:59 +01:00
public function delete ( $id )
2019-01-18 17:56:56 +01:00
{
if ( ! DolibarrApiAccess :: $user -> rights -> don -> supprimer ) {
throw new RestException ( 401 );
}
$result = $this -> don -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Donation not found' );
}
2019-02-24 16:36:06 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'donation' , $this -> don -> id )) {
2019-01-18 17:56:56 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
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
*
* If you get a bad value for param notrigger check , provide this in body
* {
* " 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
*
* @ throws 304
* @ throws 401
* @ throws 404
* @ throws 500
*
* @ return array
*/
2019-02-25 20:35:59 +01:00
public function validate ( $id , $idwarehouse = 0 , $notrigger = 0 )
2019-01-18 17:56:56 +01:00
{
2019-02-24 16:36:06 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> don -> creer ) {
2019-01-18 17:56:56 +01:00
throw new RestException ( 401 );
}
2019-02-24 16:36:06 +01:00
$result = $this -> don -> fetch ( $id );
2019-01-18 17:56:56 +01:00
if ( ! $result ) {
throw new RestException ( 404 , 'Donation not found' );
}
2019-01-27 11:55:16 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'don' , $this -> don -> id )) {
2019-01-18 17:56:56 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-02-24 16:36:06 +01:00
$result = $this -> don -> valid ( DolibarrApiAccess :: $user , $idwarehouse , $notrigger );
2019-01-18 17:56:56 +01:00
if ( $result == 0 ) {
throw new RestException ( 304 , 'Error nothing done. May be object is already validated' );
}
if ( $result < 0 ) {
2019-02-24 16:36:06 +01:00
throw new RestException ( 500 , 'Error when validating Order: ' . $this -> don -> error );
2019-01-18 17:56:56 +01:00
}
2019-02-24 16:36:06 +01:00
$result = $this -> don -> fetch ( $id );
2019-01-18 17:56:56 +01:00
if ( ! $result ) {
throw new RestException ( 404 , 'Order not found' );
}
2019-02-24 16:36:06 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'don' , $this -> don -> id )) {
2019-01-18 17:56:56 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-02-24 16:36:06 +01:00
$this -> don -> fetchObjectLinked ();
2019-01-18 17:56:56 +01:00
2019-02-24 16:36:06 +01:00
return $this -> _cleanObjectDatas ( $this -> don );
2019-01-18 17:56:56 +01:00
}
2019-03-04 19:57:46 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2019-01-18 17:56:56 +01:00
/**
* Clean sensible object datas
*
* @ param object $object Object to clean
* @ return array Array of cleaned object properties
*/
2019-03-04 19:57:46 +01:00
protected function _cleanObjectDatas ( $object )
2019-01-18 17:56:56 +01:00
{
2019-03-04 19:57:46 +01:00
// phpcs:enable
2019-01-18 17:56:56 +01:00
$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
*/
2019-02-25 20:35:59 +01:00
private function _validate ( $data )
2019-01-18 17:56:56 +01:00
{
2019-02-24 16:36:06 +01:00
$don = array ();
2019-01-18 17:56:56 +01:00
foreach ( Orders :: $FIELDS as $field ) {
if ( ! isset ( $data [ $field ]))
throw new RestException ( 400 , $field . " field missing " );
2019-02-24 16:36:06 +01:00
$don [ $field ] = $data [ $field ];
2019-01-18 17:56:56 +01:00
}
2019-02-24 16:36:06 +01:00
return $don ;
2019-01-18 17:56:56 +01:00
}
}