2015-05-01 16:12:30 +02:00
< ? php
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
2018-07-19 15:15:54 +02:00
* Copyright ( C ) 2018 Pierre Chéné < pierre . chene44 @ gmail . com >
2019-10-19 16:45:30 +02:00
* Copyright ( C ) 2019 Cedric Ancelin < icedo . anc @ gmail . com >
2021-03-08 00:17:58 +01:00
* Copyright ( C ) 2020 - 2021 Frédéric France < frederic . france @ netlogic . fr >
2015-05-01 16:12: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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2015-05-01 16:12:30 +02:00
*/
2018-02-12 04:07:45 +01:00
use Luracast\Restler\RestException ;
2015-05-01 16:12:30 +02:00
/**
2016-06-15 17:08:17 +02:00
* API class for thirdparties
2015-05-01 16:12:30 +02:00
*
2017-10-03 13:07:07 +02:00
* @ access protected
2015-05-03 01:54:04 +02:00
* @ class DolibarrApiAccess { @ requires user , external }
2017-10-03 13:07:07 +02:00
*
2015-05-01 16:12:30 +02:00
*/
2016-06-15 17:08:17 +02:00
class Thirdparties extends DolibarrApi
2015-05-06 00:55:42 +02:00
{
2018-02-12 04:07:45 +01:00
/**
*
* @ var array $FIELDS Mandatory fields , checked when create and update object
*/
2021-02-26 21:17:52 +01:00
public static $FIELDS = array (
2018-08-26 17:29:04 +02:00
'name'
2018-02-12 04:07:45 +01:00
);
/**
* @ var Societe $company { @ type Societe }
*/
public $company ;
/**
* Constructor
*/
2020-10-31 14:32:18 +01:00
public function __construct ()
2018-02-12 04:07:45 +01:00
{
2015-05-03 18:30:59 +02:00
global $db , $conf ;
2015-05-01 16:12:30 +02:00
$this -> db = $db ;
2017-10-22 14:48:42 +02:00
2020-12-21 15:25:15 +01:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2018-08-26 17:29:04 +02:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societeaccount.class.php' ;
2017-10-22 14:48:42 +02:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2018-10-16 21:43:40 +02:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php' ;
2017-10-22 14:48:42 +02:00
2018-02-12 04:07:45 +01:00
$this -> company = new Societe ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( ! empty ( $conf -> global -> SOCIETE_EMAIL_MANDATORY )) {
2018-02-12 04:07:45 +01:00
static :: $FIELDS [] = 'email' ;
}
}
/**
* Get properties of a thirdparty object
*
* Return an array with thirdparty informations
*
2020-08-29 11:37:16 +02:00
* @ param int $id Id of third party to load
2020-12-21 15:23:04 +01:00
* @ return array | mixed Cleaned Societe object
2018-02-12 04:07:45 +01:00
*
* @ throws RestException
*/
2020-10-31 14:32:18 +01:00
public function get ( $id )
2018-02-12 04:07:45 +01:00
{
2020-10-31 14:32:18 +01:00
return $this -> _fetch ( $id );
}
2018-02-12 04:07:45 +01:00
2019-10-19 16:43:10 +02:00
/**
* Get properties of a thirdparty object by email .
*
* Return an array with thirdparty informations
*
2020-08-29 11:37:16 +02:00
* @ param string $email Email of third party to load
2020-12-21 15:23:04 +01:00
* @ return array | mixed Cleaned Societe object
2019-10-19 16:43:10 +02:00
*
2020-08-31 21:45:51 +02:00
* @ url GET email / { email }
2019-10-19 16:43:10 +02:00
*
* @ throws RestException
*/
public function getByEmail ( $email )
{
2020-10-31 14:32:18 +01:00
return $this -> _fetch ( '' , '' , '' , '' , '' , '' , '' , '' , '' , '' , $email );
2018-02-12 04:07:45 +01:00
}
2020-08-29 11:39:10 +02:00
2020-08-29 11:37:16 +02:00
/**
* Get properties of a thirdparty object by barcode .
*
* Return an array with thirdparty informations
*
2020-08-29 11:43:56 +02:00
* @ param string $barcode Barcode of third party to load
2020-12-21 15:23:04 +01:00
* @ return array | mixed Cleaned Societe object
2020-08-29 11:37:16 +02:00
*
* @ url GET barcode / { barcode }
*
* @ throws RestException
*/
public function getByBarcode ( $barcode )
{
2020-10-31 14:32:18 +01:00
return $this -> _fetch ( '' , '' , '' , $barcode );
2020-08-29 11:37:16 +02:00
}
2018-02-12 04:07:45 +01:00
/**
* List thirdparties
*
* Get a list of thirdparties
*
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
* @ param int $mode Set to 1 to show only customers
* Set to 2 to show only prospects
* Set to 3 to show only those are not customer neither prospect
2019-10-15 09:52:14 +02:00
* Set to 4 to show only suppliers
2021-05-12 00:00:51 +02:00
* @ param int $category Use this param to filter list by category
* @ param string $sqlfilters Other criteria to filter answers separated by a comma . Syntax example " ((t.nom:like:'TheCompany%') or (t.name_alias:like:'TheCompany%')) and (t.datec:<:'20160101') "
2018-02-12 04:07:45 +01:00
* @ return array Array of thirdparty objects
*/
2020-10-31 14:32:18 +01:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $mode = 0 , $category = 0 , $sqlfilters = '' )
{
2018-02-12 04:07:45 +01:00
$obj_ret = array ();
2021-04-08 19:05:28 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> lire ) {
throw new RestException ( 401 );
}
2018-02-12 04:07:45 +01:00
// case of external user, we force socids
2019-09-06 10:53:05 +02:00
$socids = DolibarrApiAccess :: $user -> socid ? DolibarrApiAccess :: $user -> socid : '' ;
2018-02-12 04:07:45 +01:00
// If the internal user must only see his customers, force searching by him
$search_sale = 0 ;
2021-02-26 21:17:52 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) {
$search_sale = DolibarrApiAccess :: $user -> id ;
}
2018-02-12 04:07:45 +01:00
$sql = " SELECT t.rowid " ;
2021-02-26 21:17:52 +01:00
if (( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) || $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)
}
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe as t " ;
2020-10-31 14:32:18 +01:00
if ( $category > 0 ) {
2021-02-26 21:17:52 +01:00
if ( $mode != 4 ) {
$sql .= " , " . MAIN_DB_PREFIX . " categorie_societe as c " ;
}
if ( ! in_array ( $mode , array ( 1 , 2 , 3 ))) {
$sql .= " , " . MAIN_DB_PREFIX . " categorie_fournisseur as cc " ;
}
}
if (( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) || $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
2020-10-31 14:32:18 +01:00
}
2019-11-14 12:09:15 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " c_stcomm as st " ;
2020-05-28 19:50:27 +02:00
$sql .= " WHERE t.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
2020-04-15 19:24:16 +02:00
$sql .= " AND t.fk_stcomm = st.id " ;
2020-04-15 18:20:42 +02:00
2021-02-26 21:17:52 +01:00
if ( $mode == 1 ) {
$sql .= " AND t.client IN (1, 3) " ;
} elseif ( $mode == 2 ) {
$sql .= " AND t.client IN (2, 3) " ;
} elseif ( $mode == 3 ) {
$sql .= " AND t.client IN (0) " ;
} elseif ( $mode == 4 ) {
$sql .= " AND t.fournisseur IN (1) " ;
}
2020-04-15 18:20:42 +02:00
2020-10-31 14:32:18 +01:00
// Select thirdparties of given category
if ( $category > 0 ) {
2021-02-26 21:17:52 +01:00
if ( ! empty ( $mode ) && $mode != 4 ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND c.fk_categorie = " . (( int ) $category ) . " AND c.fk_soc = t.rowid " ;
2021-02-26 21:17:52 +01:00
} elseif ( ! empty ( $mode ) && $mode == 4 ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND cc.fk_categorie = " . (( int ) $category ) . " AND cc.fk_soc = t.rowid " ;
2021-02-26 21:17:52 +01:00
} else {
2021-06-09 15:36:47 +02:00
$sql .= " AND ((c.fk_categorie = " . (( int ) $category ) . " AND c.fk_soc = t.rowid) OR (cc.fk_categorie = " . (( int ) $category ) . " AND cc.fk_soc = t.rowid)) " ;
2021-02-26 21:17:52 +01:00
}
2020-10-31 14:32:18 +01:00
}
2020-04-15 18:20:42 +02:00
2021-02-26 21:17:52 +01:00
if (( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) || $search_sale > 0 ) {
$sql .= " AND t.rowid = sc.fk_soc " ;
}
2018-02-12 04:07:45 +01:00
//if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
2021-02-26 21:17:52 +01:00
if ( $socids ) {
2021-03-22 11:30:18 +01:00
$sql .= " AND t.rowid IN ( " . $this -> db -> sanitize ( $socids ) . " ) " ;
2021-02-26 21:17:52 +01:00
}
if ( $search_sale > 0 ) {
$sql .= " AND t.rowid = sc.fk_soc " ; // Join for the needed table to filter by sale
}
2018-02-12 04:07:45 +01:00
// Insert sale filter
2021-02-26 21:17:52 +01:00
if ( $search_sale > 0 ) {
2021-03-22 11:30:18 +01:00
$sql .= " AND sc.fk_user = " . (( int ) $search_sale );
2018-02-12 04:07:45 +01:00
}
// Add sql filters
2021-02-26 21:17:52 +01:00
if ( $sqlfilters ) {
2021-12-20 20:49:32 +01:00
$errormessage = '' ;
if ( ! DolibarrApi :: _checkFilters ( $sqlfilters , $errormessage )) {
throw new RestException ( 503 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
2018-02-12 04:07:45 +01:00
}
2021-05-03 14:58:29 +02:00
$regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)' ;
2019-11-14 12:09:15 +01:00
$sql .= " AND ( " . preg_replace_callback ( '/' . $regexstring . '/' , 'DolibarrApi::_forge_criteria_callback' , $sqlfilters ) . " ) " ;
2018-02-12 04:07:45 +01:00
}
2020-09-19 23:30:29 +02:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
2018-02-12 04:07:45 +01:00
if ( $limit ) {
2020-11-05 23:03:44 +01:00
if ( $page < 0 ) {
2018-02-12 04:07:45 +01:00
$page = 0 ;
}
$offset = $limit * $page ;
2020-09-19 23:30:29 +02:00
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
2018-02-12 04:07:45 +01:00
}
2015-05-04 00:33:44 +02:00
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 21:17:52 +01:00
if ( $result ) {
2020-09-19 23:30:29 +02:00
$num = $this -> db -> num_rows ( $result );
2018-02-12 04:07:45 +01:00
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
2020-10-31 14:32:18 +01:00
$i = 0 ;
2021-02-26 21:17:52 +01:00
while ( $i < $min ) {
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
$soc_static = new Societe ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $soc_static -> fetch ( $obj -> rowid )) {
2018-02-12 04:07:45 +01:00
$obj_ret [] = $this -> _cleanObjectDatas ( $soc_static );
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-09-19 23:30:29 +02:00
throw new RestException ( 503 , 'Error when retrieve thirdparties : ' . $this -> db -> lasterror ());
2018-02-12 04:07:45 +01:00
}
2019-11-14 12:09:15 +01:00
if ( ! count ( $obj_ret )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparties not found' );
}
2015-05-03 16:59:47 +02:00
return $obj_ret ;
2018-02-12 04:07:45 +01:00
}
/**
* Create thirdparty object
*
* @ param array $request_data Request datas
* @ return int ID of thirdparty
*/
2020-10-31 14:32:18 +01:00
public function post ( $request_data = null )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
// Check mandatory fields
$result = $this -> _validate ( $request_data );
2019-11-14 12:09:15 +01:00
foreach ( $request_data as $field => $value ) {
2018-02-12 04:07:45 +01:00
$this -> company -> $field = $value ;
}
2021-02-26 21:17:52 +01:00
if ( $this -> company -> create ( DolibarrApiAccess :: $user ) < 0 ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 500 , 'Error creating thirdparty' , array_merge ( array ( $this -> company -> error ), $this -> company -> errors ));
2021-02-26 21:17:52 +01:00
}
2018-02-12 04:07:45 +01:00
2018-08-26 17:29:04 +02:00
return $this -> company -> id ;
2018-02-12 04:07:45 +01:00
}
/**
* Update thirdparty
*
* @ param int $id Id of thirdparty to update
* @ param array $request_data Datas
2020-12-21 15:23:04 +01:00
* @ return array | mixed | boolean
2018-02-12 04:07:45 +01:00
*/
2020-10-31 14:32:18 +01:00
public function put ( $id , $request_data = null )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> company -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-11-14 12:09:15 +01:00
foreach ( $request_data as $field => $value ) {
2021-02-26 21:17:52 +01:00
if ( $field == 'id' ) {
continue ;
}
2018-02-12 04:07:45 +01:00
$this -> company -> $field = $value ;
}
2020-11-05 23:03:44 +01:00
if ( $this -> company -> update ( $id , DolibarrApiAccess :: $user , 1 , '' , '' , 'update' )) {
2018-02-12 04:07:45 +01:00
return $this -> get ( $id );
2020-11-05 23:03:44 +01:00
}
2018-02-12 04:07:45 +01:00
2018-08-26 17:29:04 +02:00
return false ;
2018-02-12 04:07:45 +01:00
}
/**
* Merge a thirdparty into another one .
*
* Merge content ( properties , notes ) and objects ( like invoices , events , orders , proposals , ... ) of a thirdparty into a target thirdparty ,
* then delete the merged thirdparty .
* If a property has a defined value both in thirdparty to delete and thirdparty to keep , the value into the thirdparty to
* delete will be ignored , the value of target thirdparty will remain , except for notes ( content is concatenated ) .
*
* @ param int $id ID of thirdparty to keep ( the target thirdparty )
* @ param int $idtodelete ID of thirdparty to remove ( the thirdparty to delete ), once data has been merged into the target thirdparty .
* @ return int
*
* @ url PUT { id } / merge / { idtodelete }
*/
2020-10-31 14:32:18 +01:00
public function merge ( $id , $idtodelete )
2018-02-12 04:07:45 +01:00
{
2020-11-25 00:16:36 +01:00
global $hookmanager ;
2018-02-12 04:07:45 +01:00
$error = 0 ;
2021-02-26 21:17:52 +01:00
if ( $id == $idtodelete ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 400 , 'Try to merge a thirdparty into itself' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2015-05-03 18:30:59 +02:00
throw new RestException ( 401 );
}
2017-10-03 13:07:07 +02:00
2019-11-14 12:09:15 +01:00
$result = $this -> company -> fetch ( $id ); // include the fetch of extra fields
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
2017-10-03 13:07:07 +02:00
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> company -> id )) {
2015-05-03 18:30:59 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2020-09-19 23:30:29 +02:00
$this -> companytoremove = new Societe ( $this -> db );
2018-02-12 04:07:45 +01:00
2019-11-14 12:09:15 +01:00
$result = $this -> companytoremove -> fetch ( $idtodelete ); // include the fetch of extra fields
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> companytoremove -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$soc_origin = $this -> companytoremove ;
$object = $this -> company ;
$user = DolibarrApiAccess :: $user ;
// Call same code than into action 'confirm_merge'
2020-09-19 23:30:29 +02:00
$this -> db -> begin ();
2018-02-12 04:07:45 +01:00
// Recopy some data
$object -> client = $object -> client | $soc_origin -> client ;
$object -> fournisseur = $object -> fournisseur | $soc_origin -> fournisseur ;
2019-11-14 12:09:15 +01:00
$listofproperties = array (
2018-08-26 17:29:04 +02:00
'address' , 'zip' , 'town' , 'state_id' , 'country_id' , 'phone' , 'phone_pro' , 'fax' , 'email' , 'skype' , 'url' , 'barcode' ,
'idprof1' , 'idprof2' , 'idprof3' , 'idprof4' , 'idprof5' , 'idprof6' ,
'tva_intra' , 'effectif_id' , 'forme_juridique' , 'remise_percent' , 'remise_supplier_percent' , 'mode_reglement_supplier_id' , 'cond_reglement_supplier_id' , 'name_bis' ,
'stcomm_id' , 'outstanding_limit' , 'price_level' , 'parent' , 'default_lang' , 'ref' , 'ref_ext' , 'import_key' , 'fk_incoterms' , 'fk_multicurrency' ,
'code_client' , 'code_fournisseur' , 'code_compta' , 'code_compta_fournisseur' ,
'model_pdf' , 'fk_projet'
2018-02-12 04:07:45 +01:00
);
2021-02-26 21:17:52 +01:00
foreach ( $listofproperties as $property ) {
if ( empty ( $object -> $property )) {
$object -> $property = $soc_origin -> $property ;
}
2018-02-12 04:07:45 +01:00
}
// Concat some data
2019-11-14 12:09:15 +01:00
$listofproperties = array (
2018-08-26 17:29:04 +02:00
'note_public' , 'note_private'
2018-02-12 04:07:45 +01:00
);
2021-02-26 21:17:52 +01:00
foreach ( $listofproperties as $property ) {
2018-02-12 04:07:45 +01:00
$object -> $property = dol_concatdesc ( $object -> $property , $soc_origin -> $property );
}
// Merge extrafields
2021-02-26 21:17:52 +01:00
if ( is_array ( $soc_origin -> array_options )) {
foreach ( $soc_origin -> array_options as $key => $val ) {
if ( empty ( $object -> array_options [ $key ])) {
$object -> array_options [ $key ] = $val ;
}
2018-02-12 04:07:45 +01:00
}
}
// Merge categories
2020-09-19 23:30:29 +02:00
$static_cat = new Categorie ( $this -> db );
2018-02-12 04:07:45 +01:00
$custcats = $static_cat -> containing ( $soc_origin -> id , 'customer' , 'id' );
$object -> setCategories ( $custcats , 'customer' );
$suppcats = $static_cat -> containing ( $soc_origin -> id , 'supplier' , 'id' );
$object -> setCategories ( $suppcats , 'supplier' );
// If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys.
if ( $soc_origin -> code_client == $object -> code_client
|| $soc_origin -> code_fournisseur == $object -> code_fournisseur
2021-02-26 21:17:52 +01:00
|| $soc_origin -> barcode == $object -> barcode ) {
2018-02-12 04:07:45 +01:00
dol_syslog ( " We clean customer and supplier code so we will be able to make the update of target " );
$soc_origin -> code_client = '' ;
$soc_origin -> code_fournisseur = '' ;
$soc_origin -> barcode = '' ;
$soc_origin -> update ( $soc_origin -> id , $user , 0 , 1 , 1 , 'merge' );
}
// Update
$result = $object -> update ( $object -> id , $user , 0 , 1 , 1 , 'merge' );
2021-02-26 21:17:52 +01:00
if ( $result < 0 ) {
2018-02-12 04:07:45 +01:00
$error ++ ;
}
// Move links
2020-11-24 23:19:19 +01:00
if ( ! $error ) {
2020-10-16 17:08:23 +02:00
// This list is also into the societe/card.php file
// TODO Mutualise the list into object societe.class.php
2018-02-12 04:07:45 +01:00
$objects = array (
2018-08-26 17:29:04 +02:00
'Adherent' => '/adherents/class/adherent.class.php' ,
'Societe' => '/societe/class/societe.class.php' ,
'Categorie' => '/categories/class/categorie.class.php' ,
'ActionComm' => '/comm/action/class/actioncomm.class.php' ,
'Propal' => '/comm/propal/class/propal.class.php' ,
'Commande' => '/commande/class/commande.class.php' ,
'Facture' => '/compta/facture/class/facture.class.php' ,
'FactureRec' => '/compta/facture/class/facture-rec.class.php' ,
'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php' ,
2020-10-16 17:08:23 +02:00
'Mo' => '/mrp/class/mo.class.php' ,
2018-08-26 17:29:04 +02:00
'Contact' => '/contact/class/contact.class.php' ,
'Contrat' => '/contrat/class/contrat.class.php' ,
'Expedition' => '/expedition/class/expedition.class.php' ,
'Fichinter' => '/fichinter/class/fichinter.class.php' ,
'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php' ,
'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php' ,
'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php' ,
'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php' ,
2020-10-05 13:59:06 +02:00
'Livraison' => '/delivery/class/delivery.class.php' ,
2018-08-26 17:29:04 +02:00
'Product' => '/product/class/product.class.php' ,
'Project' => '/projet/class/project.class.php' ,
2020-10-16 17:08:23 +02:00
'Ticket' => '/ticket/class/ticket.class.php' ,
2021-05-06 16:41:06 +02:00
'User' => '/user/class/user.class.php' ,
'Account' => '/compta/bank/class/account.class.php'
2018-02-12 04:07:45 +01:00
);
//First, all core objects must update their tables
2021-02-26 21:17:52 +01:00
foreach ( $objects as $object_name => $object_file ) {
2018-02-12 04:07:45 +01:00
require_once DOL_DOCUMENT_ROOT . $object_file ;
2020-11-24 23:19:19 +01:00
if ( ! $error && ! $object_name :: replaceThirdparty ( $this -> db , $soc_origin -> id , $object -> id )) {
$error ++ ;
2020-09-19 23:30:29 +02:00
//setEventMessages($this->db->lasterror(), null, 'errors');
2018-02-12 04:07:45 +01:00
}
}
}
// External modules should update their ones too
2020-11-24 23:19:19 +01:00
if ( ! $error ) {
2020-10-31 14:32:18 +01:00
$reshook = $hookmanager -> executeHooks ( 'replaceThirdparty' , array (
2018-08-26 17:29:04 +02:00
'soc_origin' => $soc_origin -> id ,
'soc_dest' => $object -> id
2018-02-12 04:07:45 +01:00
), $soc_dest , $action );
2020-11-24 23:19:19 +01:00
if ( $reshook < 0 ) {
2018-02-12 04:07:45 +01:00
//setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
2020-11-24 23:19:19 +01:00
$error ++ ;
2018-02-12 04:07:45 +01:00
}
}
2020-11-24 23:19:19 +01:00
if ( ! $error ) {
2019-11-14 12:09:15 +01:00
$object -> context = array ( 'merge' => 1 , 'mergefromid' => $soc_origin -> id );
2018-02-12 04:07:45 +01:00
// Call trigger
2019-11-14 12:09:15 +01:00
$result = $object -> call_trigger ( 'COMPANY_MODIFY' , $user );
2020-11-24 23:19:19 +01:00
if ( $result < 0 ) {
2018-02-12 04:07:45 +01:00
//setEventMessages($object->error, $object->errors, 'errors');
$error ++ ;
}
// End call triggers
}
2020-11-24 23:19:19 +01:00
if ( ! $error ) {
2018-02-12 04:07:45 +01:00
//We finally remove the old thirdparty
2020-11-24 23:19:19 +01:00
if ( $soc_origin -> delete ( $soc_origin -> id , $user ) < 1 ) {
$error ++ ;
2018-02-12 04:07:45 +01:00
}
}
// End of merge
2020-11-24 23:19:19 +01:00
if ( $error ) {
2020-09-19 23:30:29 +02:00
$this -> db -> rollback ();
2018-02-12 04:07:45 +01:00
throw new RestException ( 500 , 'Error failed to merged thirdparty ' . $this -> companytoremove -> id . ' into ' . $id . '. Enable and read log file for more information.' );
2020-05-21 15:05:19 +02:00
} else {
2020-09-19 23:30:29 +02:00
$this -> db -> commit ();
2018-01-31 18:01:56 +01:00
}
2018-02-12 04:07:45 +01:00
return $this -> get ( $id );
}
/**
* Delete thirdparty
*
* @ param int $id Thirparty ID
* @ return integer
*/
2020-10-31 14:32:18 +01:00
public function delete ( $id )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> supprimer ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> company -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2020-10-31 14:32:18 +01:00
$this -> company -> oldcopy = clone $this -> company ;
2018-02-12 04:07:45 +01:00
return $this -> company -> delete ( $id );
2020-10-31 14:32:18 +01:00
}
2020-07-31 17:48:44 +02:00
/**
* Set new price level for the given thirdparty
*
* @ param int $id ID of thirdparty
* @ param int $priceLevel Price level to apply to thirdparty
* @ return object Thirdparty data without useless information
*
* @ url PUT { id } / setpricelevel
*
* @ throws RestException 400 Price level out of bounds
* @ throws RestException 401 Access not allowed for your login
* @ throws RestException 404 Thirdparty not found
* @ throws RestException 500 Error fetching / setting price level
* @ throws RestException 501 Request needs modules " Thirdparties " and " Products " and setting Multiprices activated
*/
public function setThirdpartyPriceLevel ( $id , $priceLevel )
{
global $conf ;
if ( empty ( $conf -> societe -> enabled )) {
throw new RestException ( 501 , 'Module "Thirdparties" needed for this request' );
}
if ( empty ( $conf -> product -> enabled )) {
throw new RestException ( 501 , 'Module "Products" needed for this request' );
}
if ( empty ( $conf -> global -> PRODUIT_MULTIPRICES )) {
throw new RestException ( 501 , 'Multiprices features activation needed for this request' );
}
if ( $priceLevel < 1 || $priceLevel > $conf -> global -> PRODUIT_MULTIPRICES_LIMIT ) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 400 , 'Price level must be between 1 and ' . $conf -> global -> PRODUIT_MULTIPRICES_LIMIT );
2020-07-31 17:48:44 +02:00
}
if ( empty ( DolibarrApiAccess :: $user -> rights -> societe -> creer )) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 401 , 'Access to thirdparty ' . $id . ' not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-07-31 17:48:44 +02:00
}
$result = $this -> company -> fetch ( $id );
if ( $result < 0 ) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 404 , 'Thirdparty ' . $id . ' not found' );
2020-07-31 17:48:44 +02:00
}
if ( empty ( $result )) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 500 , 'Error fetching thirdparty ' . $id , array_merge ( array ( $this -> company -> error ), $this -> company -> errors ));
2020-07-31 17:48:44 +02:00
}
if ( empty ( DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> company -> id ))) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 401 , 'Access to thirdparty ' . $id . ' not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-07-31 17:48:44 +02:00
}
2021-04-01 10:05:37 +02:00
$result = $this -> company -> setPriceLevel ( $priceLevel , DolibarrApiAccess :: $user );
2020-07-31 17:48:44 +02:00
if ( $result <= 0 ) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 500 , 'Error setting new price level for thirdparty ' . $id , array ( $this -> company -> db -> lasterror ()));
2020-07-31 17:48:44 +02:00
}
return $this -> _cleanObjectDatas ( $this -> company );
2018-02-12 04:07:45 +01:00
}
/**
* Get customer categories for a thirdparty
*
* @ param int $id ID of thirdparty
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
*
* @ return mixed
*
* @ url GET { id } / categories
*/
2020-10-31 14:32:18 +01:00
public function getCategories ( $id , $sortfield = " s.rowid " , $sortorder = 'ASC' , $limit = 0 , $page = 0 )
2017-10-03 13:07:07 +02:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> categorie -> lire ) {
2017-10-03 19:09:46 +02:00
throw new RestException ( 401 );
}
2018-02-12 04:07:45 +01:00
$result = $this -> company -> fetch ( $id );
2021-02-26 21:17:52 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
2017-10-03 19:01:54 +02:00
$categories = new Categorie ( $this -> db );
2017-10-03 13:07:07 +02:00
2017-10-03 19:01:54 +02:00
$result = $categories -> getListForItem ( $id , 'customer' , $sortfield , $sortorder , $limit , $page );
2017-10-03 13:07:07 +02:00
2021-02-26 21:17:52 +01:00
if ( is_numeric ( $result ) && $result < 0 ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 503 , 'Error when retrieve category list : ' . $categories -> error );
}
2021-02-26 21:17:52 +01:00
if ( is_numeric ( $result ) && $result == 0 ) { // To fix a return of 0 instead of empty array of method getListForItem
2018-02-12 04:07:45 +01:00
return array ();
}
return $result ;
}
/**
* Add a customer category to a thirdparty
*
* @ param int $id Id of thirdparty
* @ param int $category_id Id of category
*
* @ return mixed
*
* @ url POST { id } / categories / { category_id }
*/
2020-10-31 14:32:18 +01:00
public function addCategory ( $id , $category_id )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
$category = new Categorie ( $this -> db );
$result = $category -> fetch ( $category_id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'category not found' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> company -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'category' , $category -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-01-27 11:55:16 +01:00
$category -> add_type ( $this -> company , 'customer' );
2018-02-12 04:07:45 +01:00
return $this -> _cleanObjectDatas ( $this -> company );
}
/**
* Remove the link between a customer category and the thirdparty
*
* @ param int $id Id of thirdparty
* @ param int $category_id Id of category
*
* @ return mixed
*
* @ url DELETE { id } / categories / { category_id }
*/
2020-10-31 14:32:18 +01:00
public function deleteCategory ( $id , $category_id )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
$category = new Categorie ( $this -> db );
$result = $category -> fetch ( $category_id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'category not found' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> company -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'category' , $category -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-01-27 11:55:16 +01:00
$category -> del_type ( $this -> company , 'customer' );
2018-02-12 04:07:45 +01:00
return $this -> _cleanObjectDatas ( $this -> company );
}
/**
* Get supplier categories for a thirdparty
*
* @ param int $id ID of thirdparty
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
*
* @ return mixed
*
* @ url GET { id } / supplier_categories
*/
2020-10-31 14:32:18 +01:00
public function getSupplierCategories ( $id , $sortfield = " s.rowid " , $sortorder = 'ASC' , $limit = 0 , $page = 0 )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> categorie -> lire ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
$result = $this -> company -> fetch ( $id );
2021-02-26 21:17:52 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
2017-10-03 19:01:54 +02:00
}
2018-02-12 04:07:45 +01:00
$categories = new Categorie ( $this -> db );
$result = $categories -> getListForItem ( $id , 'supplier' , $sortfield , $sortorder , $limit , $page );
2021-02-26 21:17:52 +01:00
if ( is_numeric ( $result ) && $result < 0 ) {
2017-10-03 19:01:54 +02:00
throw new RestException ( 503 , 'Error when retrieve category list : ' . $categories -> error );
}
2021-02-26 21:17:52 +01:00
if ( is_numeric ( $result ) && $result == 0 ) { // To fix a return of 0 instead of empty array of method getListForItem
2018-02-12 04:07:45 +01:00
return array ();
}
2017-10-03 19:01:54 +02:00
return $result ;
2018-02-12 04:07:45 +01:00
}
/**
* Add a supplier category to a thirdparty
*
* @ param int $id Id of thirdparty
* @ param int $category_id Id of category
*
* @ return mixed
*
* @ url POST { id } / supplier_categories / { category_id }
*/
2020-10-31 14:32:18 +01:00
public function addSupplierCategory ( $id , $category_id )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
$category = new Categorie ( $this -> db );
$result = $category -> fetch ( $category_id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'category not found' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> company -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'category' , $category -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-01-27 11:55:16 +01:00
$category -> add_type ( $this -> company , 'supplier' );
2018-02-12 04:07:45 +01:00
return $this -> _cleanObjectDatas ( $this -> company );
}
/**
* Remove the link between a category and the thirdparty
*
* @ param int $id Id of thirdparty
* @ param int $category_id Id of category
*
* @ return mixed
*
* @ url DELETE { id } / supplier_categories / { category_id }
*/
2020-10-31 14:32:18 +01:00
public function deleteSupplierCategory ( $id , $category_id )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
$category = new Categorie ( $this -> db );
$result = $category -> fetch ( $category_id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'category not found' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> company -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'category' , $category -> id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2019-01-27 11:55:16 +01:00
$category -> del_type ( $this -> company , 'supplier' );
2018-02-12 04:07:45 +01:00
return $this -> _cleanObjectDatas ( $this -> company );
}
/**
* Get outstanding proposals of thirdparty
*
* @ param int $id ID of the thirdparty
* @ param string $mode 'customer' or 'supplier'
*
* @ url GET { id } / outstandingproposals
*
* @ return array List of outstandings proposals of thirdparty
*
2020-02-20 13:42:43 +01:00
* @ throws RestException 400
* @ throws RestException 401
* @ throws RestException 404
2018-02-12 04:07:45 +01:00
*/
2020-10-31 14:32:18 +01:00
public function getOutStandingProposals ( $id , $mode = 'customer' )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> lire ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
2019-11-14 12:09:15 +01:00
if ( empty ( $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 400 , 'Thirdparty ID is mandatory' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
2018-02-01 02:29:41 +01:00
$result = $this -> company -> getOutstandingProposals ( $mode );
unset ( $result [ 'total_ht' ]);
unset ( $result [ 'total_ttc' ]);
return $result ;
2018-02-12 04:07:45 +01:00
}
/**
* Get outstanding orders of thirdparty
*
* @ param int $id ID of the thirdparty
* @ param string $mode 'customer' or 'supplier'
*
* @ url GET { id } / outstandingorders
*
* @ return array List of outstandings orders of thirdparty
*
2020-02-20 13:42:43 +01:00
* @ throws RestException 400
* @ throws RestException 401
* @ throws RestException 404
2018-02-12 04:07:45 +01:00
*/
2020-10-31 14:32:18 +01:00
public function getOutStandingOrder ( $id , $mode = 'customer' )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> lire ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
2019-11-14 12:09:15 +01:00
if ( empty ( $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 400 , 'Thirdparty ID is mandatory' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
$result = $this -> company -> getOutstandingOrders ( $mode );
unset ( $result [ 'total_ht' ]);
unset ( $result [ 'total_ttc' ]);
return $result ;
}
/**
* Get outstanding invoices of thirdparty
*
* @ param int $id ID of the thirdparty
* @ param string $mode 'customer' or 'supplier'
*
* @ url GET { id } / outstandinginvoices
*
* @ return array List of outstandings invoices of thirdparty
*
2020-02-20 13:42:43 +01:00
* @ throws RestException 400
* @ throws RestException 401
* @ throws RestException 404
2018-02-12 04:07:45 +01:00
*/
2020-10-31 14:32:18 +01:00
public function getOutStandingInvoices ( $id , $mode = 'customer' )
2018-02-12 04:07:45 +01:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> lire ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
2019-11-14 12:09:15 +01:00
if ( empty ( $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 400 , 'Thirdparty ID is mandatory' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
$result = $this -> company -> getOutstandingBills ( $mode );
unset ( $result [ 'total_ht' ]);
unset ( $result [ 'total_ttc' ]);
return $result ;
}
2019-10-25 17:14:00 +02:00
/**
* Get representatives of thirdparty
*
* @ param int $id ID of the thirdparty
* @ param string $mode 0 = Array with properties , 1 = Array of id .
*
* @ url GET { id } / representatives
*
* @ return array List of representatives of thirdparty
*
2020-02-20 13:42:43 +01:00
* @ throws RestException 400
* @ throws RestException 401
* @ throws RestException 404
2019-10-25 17:14:00 +02:00
*/
2020-10-31 14:32:18 +01:00
public function getSalesRepresentatives ( $id , $mode = 0 )
2019-10-25 17:14:00 +02:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> lire ) {
2019-10-25 17:14:00 +02:00
throw new RestException ( 401 );
}
2019-11-14 12:09:15 +01:00
if ( empty ( $id )) {
2019-10-25 17:14:00 +02:00
throw new RestException ( 400 , 'Thirdparty ID is mandatory' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $id )) {
2019-10-25 17:14:00 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> company -> fetch ( $id );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2019-10-25 17:14:00 +02:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
$result = $this -> company -> getSalesRepresentatives ( DolibarrApiAccess :: $user , $mode );
return $result ;
}
2018-02-12 04:07:45 +01:00
/**
* Get fixed amount discount of a thirdparty ( all sources : deposit , credit note , commercial offers ... )
*
* @ param int $id ID of the thirdparty
* @ param string $filter Filter exceptional discount . " none " will return every discount , " available " returns unapplied discounts , " used " returns applied discounts { @ choice none , available , used }
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
*
* @ url GET { id } / fixedamountdiscounts
*
* @ return array List of fixed discount of thirdparty
*
2020-02-20 13:42:43 +01:00
* @ throws RestException 400
* @ throws RestException 401
* @ throws RestException 404
* @ throws RestException 503
2018-02-12 04:07:45 +01:00
*/
2020-10-31 14:32:18 +01:00
public function getFixedAmountDiscounts ( $id , $filter = " none " , $sortfield = " f.type " , $sortorder = 'ASC' )
2018-02-12 04:07:45 +01:00
{
$obj_ret = array ();
2019-11-16 23:24:29 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> lire ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
2019-11-16 23:24:29 +01:00
if ( empty ( $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 400 , 'Thirdparty ID is mandatory' );
}
2019-11-16 23:24:29 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> company -> fetch ( $id );
2019-11-16 23:24:29 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 404 , 'Thirdparty not found' );
}
2018-12-02 14:31:45 +01:00
$sql = " SELECT f.ref, f.type as factype, re.fk_facture_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_facture, re.fk_facture_line " ;
2018-02-12 04:07:45 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_remise_except as re, " . MAIN_DB_PREFIX . " facture as f " ;
2021-03-30 19:12:07 +02:00
$sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = " . (( int ) $id );
2021-02-26 21:17:52 +01:00
if ( $filter == " available " ) {
$sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL " ;
}
if ( $filter == " used " ) {
$sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL) " ;
}
2018-02-12 04:07:45 +01:00
2019-11-14 12:09:15 +01:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
2018-02-12 04:07:45 +01:00
$result = $this -> db -> query ( $sql );
2019-11-14 12:09:15 +01:00
if ( ! $result ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 503 , $this -> db -> lasterror ());
} else {
$num = $this -> db -> num_rows ( $result );
2019-11-14 12:09:15 +01:00
while ( $obj = $this -> db -> fetch_object ( $result )) {
2018-02-12 04:07:45 +01:00
$obj_ret [] = $obj ;
}
}
return $obj_ret ;
}
/**
* Return list of invoices qualified to be replaced by another invoice .
*
* @ param int $id Id of thirdparty
*
* @ url GET { id } / getinvoicesqualifiedforreplacement
*
* @ return array
2020-02-20 13:42:43 +01:00
* @ throws RestException 400
* @ throws RestException 401
* @ throws RestException 404
* @ throws RestException 405
2018-02-12 04:07:45 +01:00
*/
2020-10-31 14:32:18 +01:00
public function getInvoicesQualifiedForReplacement ( $id )
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> lire ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
2019-11-14 12:09:15 +01:00
if ( empty ( $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 400 , 'Thirdparty ID is mandatory' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
/* $result = $this -> thirdparty -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Thirdparty not found' );
} */
$invoice = new Facture ( $this -> db );
$result = $invoice -> list_replacable_invoices ( $id );
2019-11-14 12:09:15 +01:00
if ( $result < 0 ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 405 , $this -> thirdparty -> error );
}
return $result ;
}
/**
* Return list of invoices qualified to be corrected by a credit note .
* Invoices matching the following rules are returned
2021-02-03 19:36:02 +01:00
* ( validated + payment on process ) or classified ( paid completely or paid partialy ) + not already replaced + not already a credit note
2018-02-12 04:07:45 +01:00
*
* @ param int $id Id of thirdparty
*
* @ url GET { id } / getinvoicesqualifiedforcreditnote
*
* @ return array
2020-02-20 13:42:43 +01:00
*
* @ throws RestException 400
* @ throws RestException 401
* @ throws RestException 404
* @ throws RestException 405
2018-02-12 04:07:45 +01:00
*/
2020-10-31 14:32:18 +01:00
public function getInvoicesQualifiedForCreditNote ( $id )
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> lire ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 );
}
2019-11-14 12:09:15 +01:00
if ( empty ( $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 400 , 'Thirdparty ID is mandatory' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $id )) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
/* $result = $this -> thirdparty -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Thirdparty not found' );
} */
$invoice = new Facture ( $this -> db );
$result = $invoice -> list_qualified_avoir_invoices ( $id );
2019-11-14 12:09:15 +01:00
if ( $result < 0 ) {
2018-02-12 04:07:45 +01:00
throw new RestException ( 405 , $this -> thirdparty -> error );
}
return $result ;
}
2018-08-26 18:15:14 +02:00
2018-06-27 10:33:57 +02:00
/**
* Get CompanyBankAccount objects for thirdparty
2018-06-30 01:34:51 +02:00
*
2018-08-26 17:29:04 +02:00
* @ param int $id ID of thirdparty
2018-06-30 01:34:51 +02:00
*
2018-06-27 10:33:57 +02:00
* @ return array
2018-08-26 17:29:04 +02:00
*
* @ url GET { id } / bankaccounts
2018-06-27 10:33:57 +02:00
*/
2020-10-31 14:32:18 +01:00
public function getCompanyBankAccount ( $id )
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> lire ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 401 );
}
2019-11-14 12:09:15 +01:00
if ( empty ( $id )) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 400 , 'Thirdparty ID is mandatory' );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $id )) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
/**
2018-06-27 11:01:20 +02:00
* We select all the records that match the socid
2018-06-27 10:33:57 +02:00
*/
$sql = " SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio, " ;
2019-11-14 12:09:15 +01:00
$sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_rib " ;
2021-02-26 21:17:52 +01:00
if ( $id ) {
2021-03-30 19:12:07 +02:00
$sql .= " WHERE fk_soc = " . (( int ) $id );
2021-02-26 21:17:52 +01:00
}
2018-06-27 10:33:57 +02:00
2018-06-30 01:34:51 +02:00
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2018-06-27 10:33:57 +02:00
2019-11-14 12:09:15 +01:00
if ( $result -> num_rows == 0 ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 404 , 'Account not found' );
2018-06-30 01:34:51 +02:00
}
2018-06-27 10:33:57 +02:00
2019-11-14 12:09:15 +01:00
$i = 0 ;
2018-06-27 10:33:57 +02:00
2019-07-14 00:31:28 +02:00
$accounts = array ();
2018-06-27 10:33:57 +02:00
2021-02-26 21:17:52 +01:00
if ( $result ) {
2020-09-19 23:30:29 +02:00
$num = $this -> db -> num_rows ( $result );
2021-02-26 21:17:52 +01:00
while ( $i < $num ) {
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
$account = new CompanyBankAccount ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( $account -> fetch ( $obj -> rowid )) {
2018-06-27 10:33:57 +02:00
$accounts [] = $account ;
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2018-06-27 10:33:57 +02:00
throw new RestException ( 404 , 'Account not found' );
}
2018-06-30 01:34:51 +02:00
2018-06-27 10:33:57 +02:00
2019-07-14 00:31:28 +02:00
$fields = array ( 'socid' , 'default_rib' , 'frstrecur' , '1000110000001' , 'datec' , 'datem' , 'label' , 'bank' , 'bic' , 'iban' , 'id' , 'rum' );
2018-06-27 10:33:57 +02:00
2019-07-14 00:31:28 +02:00
$returnAccounts = array ();
2018-06-27 10:33:57 +02:00
2019-10-27 17:01:23 +01:00
foreach ( $accounts as $account ) {
$object = array ();
foreach ( $account as $key => $value ) {
if ( in_array ( $key , $fields )) {
2018-08-26 17:29:04 +02:00
$object [ $key ] = $value ;
}
2019-10-27 17:01:23 +01:00
}
2018-06-27 10:33:57 +02:00
$returnAccounts [] = $object ;
2018-06-30 01:34:51 +02:00
}
2018-06-27 10:33:57 +02:00
return $returnAccounts ;
}
/**
* Create CompanyBankAccount object for thirdparty
2018-08-26 17:29:04 +02:00
* @ param int $id ID of thirdparty
2018-08-26 20:16:21 +02:00
* @ param array $request_data Request data
2018-06-30 01:34:51 +02:00
*
2020-12-21 15:23:04 +01:00
* @ return array | mixed BankAccount of thirdparty
2018-06-30 01:34:51 +02:00
*
2018-08-26 17:29:04 +02:00
* @ url POST { id } / bankaccounts
2018-06-27 10:33:57 +02:00
*/
2020-10-31 14:32:18 +01:00
public function createCompanyBankAccount ( $id , $request_data = null )
2018-06-27 10:33:57 +02:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 401 );
}
2020-04-13 09:15:01 +02:00
if ( $this -> company -> fetch ( $id ) <= 0 ) {
throw new RestException ( 404 , 'Error creating Company Bank account, Company doesn\'t exists' );
}
2018-06-27 10:33:57 +02:00
$account = new CompanyBankAccount ( $this -> db );
2018-08-26 17:29:04 +02:00
$account -> socid = $id ;
2018-06-27 10:33:57 +02:00
2019-11-14 12:09:15 +01:00
foreach ( $request_data as $field => $value ) {
2018-06-27 10:33:57 +02:00
$account -> $field = $value ;
}
2021-02-26 21:17:52 +01:00
if ( $account -> create ( DolibarrApiAccess :: $user ) < 0 ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 500 , 'Error creating Company Bank account' );
2021-02-26 21:17:52 +01:00
}
2018-06-27 10:33:57 +02:00
2020-04-13 09:15:01 +02:00
if ( empty ( $account -> rum )) {
require_once DOL_DOCUMENT_ROOT . '/compta/prelevement/class/bonprelevement.class.php' ;
$prelevement = new BonPrelevement ( $this -> db );
$account -> rum = $prelevement -> buildRumNumber ( $this -> company -> code_client , $account -> datec , $account -> id );
$account -> date_rum = dol_now ();
}
2018-06-27 10:33:57 +02:00
2021-02-26 21:17:52 +01:00
if ( $account -> update ( DolibarrApiAccess :: $user ) < 0 ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 500 , 'Error updating values' );
2021-02-26 21:17:52 +01:00
}
2018-06-30 01:34:51 +02:00
2020-04-13 09:25:06 +02:00
return $this -> _cleanObjectDatas ( $account );
2018-06-27 10:33:57 +02:00
}
/**
* Update CompanyBankAccount object for thirdparty
2018-06-30 01:34:51 +02:00
*
2018-08-26 17:29:04 +02:00
* @ param int $id ID of thirdparty
* @ param int $bankaccount_id ID of CompanyBankAccount
2018-08-26 20:16:21 +02:00
* @ param array $request_data Request data
2018-06-30 01:34:51 +02:00
*
2020-12-21 15:23:04 +01:00
* @ return array | mixed BankAccount of thirdparty
2018-06-30 01:34:51 +02:00
*
2018-08-26 17:29:04 +02:00
* @ url PUT { id } / bankaccounts / { bankaccount_id }
2018-06-27 10:33:57 +02:00
*/
2020-10-31 14:32:18 +01:00
public function updateCompanyBankAccount ( $id , $bankaccount_id , $request_data = null )
2018-06-27 10:33:57 +02:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 401 );
}
2020-04-13 09:15:01 +02:00
if ( $this -> company -> fetch ( $id ) <= 0 ) {
throw new RestException ( 404 , 'Error creating Company Bank account, Company doesn\'t exists' );
}
2018-06-27 10:33:57 +02:00
$account = new CompanyBankAccount ( $this -> db );
2018-08-28 02:05:37 +02:00
$account -> fetch ( $bankaccount_id , $id , - 1 , '' );
2018-06-27 10:33:57 +02:00
2019-11-14 12:09:15 +01:00
if ( $account -> socid != $id ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 401 );
}
2018-06-30 01:34:51 +02:00
2018-06-27 10:33:57 +02:00
2019-11-14 12:09:15 +01:00
foreach ( $request_data as $field => $value ) {
2018-06-27 10:33:57 +02:00
$account -> $field = $value ;
}
2020-04-13 09:39:31 +02:00
if ( empty ( $account -> rum )) {
require_once DOL_DOCUMENT_ROOT . '/compta/prelevement/class/bonprelevement.class.php' ;
$prelevement = new BonPrelevement ( $this -> db );
$account -> rum = $prelevement -> buildRumNumber ( $this -> company -> code_client , $account -> datec , $account -> id );
$account -> date_rum = dol_now ();
}
2021-02-26 21:17:52 +01:00
if ( $account -> update ( DolibarrApiAccess :: $user ) < 0 ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 500 , 'Error updating values' );
2021-02-26 21:17:52 +01:00
}
2018-06-30 01:34:51 +02:00
2020-04-13 09:25:06 +02:00
return $this -> _cleanObjectDatas ( $account );
2018-06-27 10:33:57 +02:00
}
/**
2018-08-26 17:29:04 +02:00
* Delete a bank account attached to a thirdparty
*
* @ param int $id ID of thirdparty
* @ param int $bankaccount_id ID of CompanyBankAccount
2018-06-30 01:34:51 +02:00
*
2018-06-27 11:01:20 +02:00
* @ return int - 1 if error 1 if correct deletion
2018-06-30 01:34:51 +02:00
*
2018-08-26 17:29:04 +02:00
* @ url DELETE { id } / bankaccounts / { bankaccount_id }
2018-06-27 10:33:57 +02:00
*/
2020-10-31 14:32:18 +01:00
public function deleteCompanyBankAccount ( $id , $bankaccount_id )
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 401 );
}
$account = new CompanyBankAccount ( $this -> db );
2018-08-26 17:29:04 +02:00
$account -> fetch ( $bankaccount_id );
2018-06-27 10:33:57 +02:00
2021-02-26 21:17:52 +01:00
if ( ! $account -> socid == $id ) {
2018-06-27 10:33:57 +02:00
throw new RestException ( 401 );
2021-02-26 21:17:52 +01:00
}
2018-06-27 10:33:57 +02:00
return $account -> delete ( DolibarrApiAccess :: $user );
}
2017-10-27 01:08:14 +02:00
2018-08-26 17:29:04 +02:00
/**
2018-09-19 14:47:11 +02:00
* Generate a Document from a bank account record ( like SEPA mandate )
2018-09-19 18:33:11 +02:00
*
2018-09-28 10:46:59 +02:00
* @ param int $id Thirdparty id
* @ param int $companybankid Companybank id
* @ param string $model Model of document to generate
2018-09-19 14:44:16 +02:00
* @ return void
2018-09-19 18:33:11 +02:00
*
2018-09-19 14:47:11 +02:00
* @ url GET { id } / generateBankAccountDocument / { companybankid } / { model }
2018-07-19 15:09:32 +02:00
*/
2018-09-19 14:47:11 +02:00
public function generateBankAccountDocument ( $id , $companybankid = null , $model = 'sepamandate' )
{
2020-11-25 00:16:36 +01:00
global $conf , $langs ;
2018-09-20 17:09:31 +02:00
2020-11-25 00:16:36 +01:00
$langs -> loadLangs ( array ( " main " , " dict " , " commercial " , " products " , " companies " , " banks " , " bills " , " withdrawals " ));
2018-07-19 15:09:32 +02:00
2020-11-25 00:16:36 +01:00
if ( $this -> company -> fetch ( $id ) <= 0 ) {
throw new RestException ( 404 , 'Thirdparty not found' );
}
2018-09-20 17:09:31 +02:00
2020-11-25 00:16:36 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-07-19 15:09:32 +02:00
throw new RestException ( 401 );
2020-11-25 00:16:36 +01:00
}
2018-09-20 17:09:31 +02:00
$this -> company -> setDocModel ( DolibarrApiAccess :: $user , $model );
2018-07-19 15:09:32 +02:00
$this -> company -> fk_bank = $this -> company -> fk_account ;
2018-09-20 17:09:31 +02:00
2020-11-25 00:16:36 +01:00
$outputlangs = $langs ;
2019-11-14 12:09:15 +01:00
$newlang = '' ;
2018-09-20 17:09:31 +02:00
2020-11-25 00:16:36 +01:00
//if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
if ( ! empty ( $conf -> global -> MAIN_MULTILANGS ) && empty ( $newlang )) {
if ( isset ( $this -> company -> thirdparty -> default_lang )) {
$newlang = $this -> company -> thirdparty -> default_lang ; // for proposal, order, invoice, ...
} elseif ( isset ( $this -> company -> default_lang )) {
$newlang = $this -> company -> default_lang ; // for thirdparty
}
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $newlang )) {
2019-01-27 11:55:16 +01:00
$outputlangs = new Translate ( " " , $conf );
2018-07-19 15:09:32 +02:00
$outputlangs -> setDefaultLang ( $newlang );
}
2018-09-20 17:09:31 +02:00
2018-07-19 15:09:32 +02:00
$sql = " SELECT rowid " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_rib " ;
2021-02-26 21:17:52 +01:00
if ( $id ) {
2021-03-22 13:31:06 +01:00
$sql .= " WHERE fk_soc = " . (( int ) $id );
2021-02-26 21:17:52 +01:00
}
if ( $companybankid ) {
2021-03-22 13:31:06 +01:00
$sql .= " AND rowid = " . (( int ) $companybankid );
2021-02-26 21:17:52 +01:00
}
2018-09-19 18:33:11 +02:00
2019-11-14 12:09:15 +01:00
$i = 0 ;
$accounts = array ();
2018-09-20 17:09:31 +02:00
$result = $this -> db -> query ( $sql );
2020-11-25 00:16:36 +01:00
if ( $result ) {
if ( $this -> db -> num_rows ( $result ) == 0 ) {
2018-09-20 17:09:31 +02:00
throw new RestException ( 404 , 'Bank account not found' );
}
2018-07-19 15:09:32 +02:00
$num = $this -> db -> num_rows ( $result );
2020-11-25 00:16:36 +01:00
while ( $i < $num ) {
2018-07-19 15:09:32 +02:00
$obj = $this -> db -> fetch_object ( $result );
2018-09-20 17:09:31 +02:00
2018-07-19 15:09:32 +02:00
$account = new CompanyBankAccount ( $this -> db );
2018-09-20 17:09:31 +02:00
if ( $account -> fetch ( $obj -> rowid )) {
2018-07-19 15:09:32 +02:00
$accounts [] = $account ;
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-11-25 00:16:36 +01:00
throw new RestException ( 500 , 'Sql error ' . $this -> db -> lasterror ());
2018-07-19 15:09:32 +02:00
}
$moreparams = array (
2020-11-25 00:16:36 +01:00
'use_companybankid' => $accounts [ 0 ] -> id ,
'force_dir_output' => $conf -> societe -> multidir_output [ $this -> company -> entity ] . '/' . dol_sanitizeFileName ( $this -> company -> id )
2018-07-19 15:09:32 +02:00
);
2018-09-20 17:09:31 +02:00
2020-11-25 00:16:36 +01:00
$result = $this -> company -> generateDocument ( $model , $outputlangs , 0 , 0 , 0 , $moreparams );
2018-09-20 17:09:31 +02:00
2020-11-25 00:16:36 +01:00
if ( $result > 0 ) {
2018-07-19 15:09:32 +02:00
return array ( " success " => $result );
2020-05-21 15:05:19 +02:00
} else {
2022-03-18 16:14:20 +01:00
throw new RestException ( 500 , 'Error generating the document ' . $this -> error );
2018-07-19 15:09:32 +02:00
}
2020-10-31 14:32:18 +01:00
}
2018-07-19 15:09:32 +02:00
2020-10-31 14:32:18 +01:00
/**
2018-08-26 17:29:04 +02:00
* Get a specific gateway attached to a thirdparty ( by specifying the site key )
*
* @ param int $id ID of thirdparty
* @ param string $site Site key
*
2020-12-21 15:23:04 +01:00
* @ return array | mixed
2020-02-20 13:42:43 +01:00
* @ throws RestException 401 Unauthorized : User does not have permission to read thirdparties
* @ throws RestException 404 Not Found : Specified thirdparty ID does not belongs to an existing thirdparty
2018-08-26 17:29:04 +02:00
*
* @ url GET { id } / gateways /
*/
2020-10-31 14:32:18 +01:00
public function getSocieteAccounts ( $id , $site = null )
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> lire ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 401 );
}
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $id )) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
/**
* We select all the records that match the socid
*/
$sql = " SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM " . MAIN_DB_PREFIX . " societe_account " ;
2021-03-22 13:31:06 +01:00
$sql .= " WHERE fk_soc = " . (( int ) $id );
2021-02-26 21:17:52 +01:00
if ( $site ) {
2021-03-22 13:41:43 +01:00
$sql .= " AND site =' " . $this -> db -> escape ( $site ) . " ' " ;
2021-02-26 21:17:52 +01:00
}
2018-08-26 17:29:04 +02:00
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2018-08-26 17:29:04 +02:00
2020-10-31 11:57:35 +01:00
if ( $result && $this -> db -> num_rows ( $result ) == 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 404 , 'This thirdparty does not have any gateway attached or does not exist.' );
}
2019-11-14 12:09:15 +01:00
$i = 0 ;
2018-08-26 17:29:04 +02:00
2019-07-14 00:31:28 +02:00
$accounts = array ();
2018-08-26 17:29:04 +02:00
2020-09-19 23:30:29 +02:00
$num = $this -> db -> num_rows ( $result );
2021-02-26 21:17:52 +01:00
while ( $i < $num ) {
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
$account = new SocieteAccount ( $this -> db );
2018-08-26 17:29:04 +02:00
2019-11-14 12:09:15 +01:00
if ( $account -> fetch ( $obj -> rowid )) {
2018-08-26 17:29:04 +02:00
$accounts [] = $account ;
}
$i ++ ;
}
2019-07-14 00:31:28 +02:00
$fields = array ( 'id' , 'fk_soc' , 'key_account' , 'site' , 'date_creation' , 'tms' );
2018-08-26 17:29:04 +02:00
2019-07-14 00:31:28 +02:00
$returnAccounts = array ();
2018-08-26 17:29:04 +02:00
2019-10-27 17:01:23 +01:00
foreach ( $accounts as $account ) {
$object = array ();
foreach ( $account as $key => $value ) {
2019-11-16 23:24:29 +01:00
if ( in_array ( $key , $fields )) {
2018-08-26 17:29:04 +02:00
$object [ $key ] = $value ;
}
2019-10-27 17:01:23 +01:00
}
2018-08-26 17:29:04 +02:00
$returnAccounts [] = $object ;
}
return $returnAccounts ;
}
/**
* Create and attach a new gateway to an existing thirdparty
*
* Possible fields for request_data ( request body ) are specified in < code > llx_societe_account </ code > table .< br >
* See < a href = " https://wiki.dolibarr.org/index.php/Table_llx_societe_account " > Table llx_societe_account </ a > wiki page for more information < br >< br >
* < u > Example body payload :</ u > < pre > { " key_account " : " cus_DAVkLSs1LYyYI " , " site " : " stripe " } </ pre >
*
* @ param int $id ID of thirdparty
2018-08-26 20:16:21 +02:00
* @ param array $request_data Request data
2018-08-26 17:29:04 +02:00
*
2020-12-21 15:23:04 +01:00
* @ return array | mixed
2020-02-20 13:54:41 +01:00
*
2020-02-20 13:42:43 +01:00
* @ throws RestException 401 Unauthorized : User does not have permission to read thirdparties
* @ throws RestException 409 Conflict : A SocieteAccount entity ( gateway ) already exists for this company and site .
* @ throws RestException 422 Unprocessable Entity : You must pass the site attribute in your request data !
* @ throws RestException 500 Internal Server Error : Error creating SocieteAccount account
2018-08-26 17:29:04 +02:00
*
* @ url POST { id } / gateways
*/
2020-10-31 14:32:18 +01:00
public function createSocieteAccount ( $id , $request_data = null )
2018-08-26 17:29:04 +02:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 401 );
}
2019-11-14 12:09:15 +01:00
if ( ! isset ( $request_data [ 'site' ])) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 422 , 'Unprocessable Entity: You must pass the site attribute in your request data !' );
}
2021-03-30 19:12:07 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " societe_account WHERE fk_soc = " . (( int ) $id ) . " AND site = ' " . $this -> db -> escape ( $request_data [ 'site' ]) . " ' " ;
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2018-08-26 17:29:04 +02:00
2020-10-31 11:57:35 +01:00
if ( $result && $this -> db -> num_rows ( $result ) == 0 ) {
2018-08-26 17:29:04 +02:00
$account = new SocieteAccount ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( ! isset ( $request_data [ 'login' ])) {
2018-08-26 17:29:04 +02:00
$account -> login = " " ;
}
$account -> fk_soc = $id ;
2019-11-14 12:09:15 +01:00
foreach ( $request_data as $field => $value ) {
2018-08-26 17:29:04 +02:00
$account -> $field = $value ;
}
2021-02-26 21:17:52 +01:00
if ( $account -> create ( DolibarrApiAccess :: $user ) < 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 500 , 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!' );
2021-02-26 21:17:52 +01:00
}
2018-08-26 17:29:04 +02:00
$this -> _cleanObjectDatas ( $account );
return $account ;
} else {
throw new RestException ( 409 , 'A SocieteAccount entity already exists for this company and site.' );
}
}
/**
* Create and attach a new ( or replace an existing ) specific site gateway to a thirdparty
*
* You < strong > MUST </ strong > pass all values to keep ( otherwise , they will be deleted ) !< br >
* If you just need to update specific fields prefer < code > PATCH / thirdparties / { id } / gateways / { site } </ code > endpoint .< br >< br >
* When a < strong > SocieteAccount </ strong > entity does not exist for the < code > id </ code > and < code > site </ code >
* supplied , a new one will be created . In that case < code > fk_soc </ code > and < code > site </ code > members form
* request body payload will be ignored and < code > id </ code > and < code > site </ code > query strings parameters
* will be used instead .
*
* @ param int $id ID of thirdparty
* @ param string $site Site key
2018-08-26 20:16:21 +02:00
* @ param array $request_data Request data
2018-08-26 17:29:04 +02:00
*
2020-12-21 15:23:04 +01:00
* @ return array | mixed
2018-08-26 17:29:04 +02:00
*
2020-02-20 13:42:43 +01:00
* @ throws RestException 401 Unauthorized : User does not have permission to read thirdparties
* @ throws RestException 422 Unprocessable Entity : You must pass the site attribute in your request data !
* @ throws RestException 500 Internal Server Error : Error updating SocieteAccount entity
*
2018-08-26 17:29:04 +02:00
* @ url PUT { id } / gateways / { site }
*/
2020-10-31 14:32:18 +01:00
public function putSocieteAccount ( $id , $site , $request_data = null )
2018-08-26 17:29:04 +02:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 401 );
}
2020-09-19 23:30:29 +02:00
$sql = " SELECT rowid, fk_user_creat, date_creation FROM " . MAIN_DB_PREFIX . " societe_account WHERE fk_soc = $id AND site = ' " . $this -> db -> escape ( $site ) . " ' " ;
$result = $this -> db -> query ( $sql );
2018-08-26 17:29:04 +02:00
// We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
2020-10-31 09:50:50 +01:00
if ( $result && $this -> db -> num_rows == 0 ) {
2019-11-14 12:09:15 +01:00
if ( ! isset ( $request_data [ 'key_account' ])) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 422 , 'Unprocessable Entity: You must pass the key_account attribute in your request data !' );
}
$account = new SocieteAccount ( $this -> db );
2019-11-14 12:09:15 +01:00
if ( ! isset ( $request_data [ 'login' ])) {
2018-08-26 17:29:04 +02:00
$account -> login = " " ;
}
2019-11-14 12:09:15 +01:00
foreach ( $request_data as $field => $value ) {
2018-08-26 17:29:04 +02:00
$account -> $field = $value ;
}
$account -> fk_soc = $id ;
$account -> site = $site ;
2019-10-27 17:01:23 +01:00
if ( $account -> create ( DolibarrApiAccess :: $user ) < 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 500 , 'Error creating SocieteAccount entity.' );
2019-10-27 17:01:23 +01:00
}
// We found an existing SocieteAccount entity, we are replacing it
2018-08-26 17:29:04 +02:00
} else {
2019-10-27 17:01:23 +01:00
if ( isset ( $request_data [ 'site' ]) && $request_data [ 'site' ] !== $site ) {
2021-03-30 19:12:07 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " societe_account WHERE fk_soc = " . (( int ) $id ) . " AND site = ' " . $this -> db -> escape ( $request_data [ 'site' ]) . " ' " ;
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2018-08-26 17:29:04 +02:00
2020-10-31 11:57:35 +01:00
if ( $result && $this -> db -> num_rows ( $result ) !== 0 ) {
2019-11-16 23:24:29 +01:00
throw new RestException ( 409 , " You are trying to update this thirdparty SocieteAccount (gateway record) from $site to " . $request_data [ 'site' ] . " but another SocieteAccount entity already exists with this site key. " );
2019-10-27 17:01:23 +01:00
}
2018-08-26 17:29:04 +02:00
}
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
2018-08-26 17:29:04 +02:00
$account = new SocieteAccount ( $this -> db );
$account -> id = $obj -> rowid ;
$account -> fk_soc = $id ;
$account -> site = $site ;
2019-11-14 12:09:15 +01:00
if ( ! isset ( $request_data [ 'login' ])) {
2018-08-26 17:29:04 +02:00
$account -> login = " " ;
}
$account -> fk_user_creat = $obj -> fk_user_creat ;
$account -> date_creation = $obj -> date_creation ;
2019-11-14 12:09:15 +01:00
foreach ( $request_data as $field => $value ) {
2018-08-26 17:29:04 +02:00
$account -> $field = $value ;
}
2021-02-26 21:17:52 +01:00
if ( $account -> update ( DolibarrApiAccess :: $user ) < 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 500 , 'Error updating SocieteAccount entity.' );
2021-02-26 21:17:52 +01:00
}
2018-08-26 17:29:04 +02:00
}
$this -> _cleanObjectDatas ( $account );
return $account ;
}
/**
2021-04-30 10:57:21 +02:00
* Update specified values of a specific gateway attached to a thirdparty
2018-08-26 17:29:04 +02:00
*
2021-04-30 10:57:21 +02:00
* @ param int $id Id of thirdparty
* @ param string $site Site key
* @ param array $request_data Request data
2018-08-26 17:29:04 +02:00
*
2020-12-21 15:23:04 +01:00
* @ return array | mixed
2020-02-20 13:54:41 +01:00
*
2020-02-20 13:42:43 +01:00
* @ throws RestException 401 Unauthorized : User does not have permission to read thirdparties
* @ throws RestException 404 Not Found : Specified thirdparty ID does not belongs to an existing thirdparty
* @ throws RestException 409 Conflict : Another SocieteAccount entity already exists for this thirdparty with this site key .
* @ throws RestException 500 Internal Server Error : Error updating SocieteAccount entity
2018-08-26 17:29:04 +02:00
*
* @ url PATCH { id } / gateways / { site }
*/
2020-10-31 14:32:18 +01:00
public function patchSocieteAccount ( $id , $site , $request_data = null )
2018-08-26 17:29:04 +02:00
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 401 );
}
2021-04-30 10:57:21 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " societe_account WHERE fk_soc = " . (( int ) $id ) . " AND site = ' " . $this -> db -> escape ( $site ) . " ' " ;
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2018-08-26 17:29:04 +02:00
2020-10-31 11:57:35 +01:00
if ( $result && $this -> db -> num_rows ( $result ) == 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 404 , " This thirdparty does not have $site gateway attached or does not exist. " );
} else {
// If the user tries to edit the site member, we check first if
2019-11-14 12:09:15 +01:00
if ( isset ( $request_data [ 'site' ]) && $request_data [ 'site' ] !== $site ) {
2021-03-30 19:12:07 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " societe_account WHERE fk_soc = " . (( int ) $id ) . " AND site = ' " . $this -> db -> escape ( $request_data [ 'site' ]) . " ' " ;
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2018-08-26 17:29:04 +02:00
2021-02-26 21:17:52 +01:00
if ( $result && $this -> db -> num_rows ( $result ) !== 0 ) {
2021-04-30 10:57:21 +02:00
throw new RestException ( 409 , " You are trying to update this thirdparty SocieteAccount (gateway record) site member from " . $site . " to " . $request_data [ 'site' ] . " but another SocieteAccount entity already exists for this thirdparty with this site key. " );
2021-02-26 21:17:52 +01:00
}
2018-08-26 17:29:04 +02:00
}
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
2018-08-26 17:29:04 +02:00
$account = new SocieteAccount ( $this -> db );
$account -> fetch ( $obj -> rowid );
2019-11-14 12:09:15 +01:00
foreach ( $request_data as $field => $value ) {
2018-08-26 17:29:04 +02:00
$account -> $field = $value ;
}
2021-02-26 21:17:52 +01:00
if ( $account -> update ( DolibarrApiAccess :: $user ) < 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 500 , 'Error updating SocieteAccount account' );
2021-02-26 21:17:52 +01:00
}
2018-08-26 17:29:04 +02:00
$this -> _cleanObjectDatas ( $account );
return $account ;
}
}
/**
* Delete a specific site gateway attached to a thirdparty ( by gateway id )
*
* @ param int $id ID of thirdparty
* @ param int $site Site key
*
* @ return void
2020-02-20 13:42:43 +01:00
* @ throws RestException 401 Unauthorized : User does not have permission to delete thirdparties gateways
* @ throws RestException 404 Not Found : Specified thirdparty ID does not belongs to an existing thirdparty
* @ throws RestException 500 Internal Server Error : Error deleting SocieteAccount entity
2018-08-26 17:29:04 +02:00
*
* @ url DELETE { id } / gateways / { site }
*/
2020-10-31 14:32:18 +01:00
public function deleteSocieteAccount ( $id , $site )
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 401 );
}
2020-09-19 23:30:29 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " societe_account WHERE fk_soc = $id AND site = ' " . $this -> db -> escape ( $site ) . " ' " ;
$result = $this -> db -> query ( $sql );
2018-08-26 17:29:04 +02:00
2020-10-31 11:57:35 +01:00
if ( $result && $this -> db -> num_rows ( $result ) == 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 404 );
} else {
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
2018-08-26 17:29:04 +02:00
$account = new SocieteAccount ( $this -> db );
$account -> fetch ( $obj -> rowid );
2019-11-14 12:09:15 +01:00
if ( $account -> delete ( DolibarrApiAccess :: $user ) < 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 500 , " Error while deleting $site gateway attached to this third party " );
}
}
}
/**
* Delete all gateways attached to a thirdparty
*
* @ param int $id ID of thirdparty
*
* @ return void
2020-10-31 09:50:50 +01:00
* @ throws RestException 401 Unauthorized : User does not have permission to delete thirdparties gateways
* @ throws RestException 404 Not Found : Specified thirdparty ID does not belongs to an existing thirdparty
* @ throws RestException 500 Internal Server Error : Error deleting SocieteAccount entity
2018-08-26 17:29:04 +02:00
*
* @ url DELETE { id } / gateways
*/
2020-10-31 14:32:18 +01:00
public function deleteSocieteAccounts ( $id )
{
2019-11-14 12:09:15 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> creer ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 401 );
}
/**
* We select all the records that match the socid
*/
$sql = " SELECT rowid, fk_soc, key_account, site, date_creation, tms " ;
2021-03-30 19:12:07 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_account WHERE fk_soc = " . (( int ) $id );
2018-08-26 17:29:04 +02:00
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2018-08-26 17:29:04 +02:00
2020-10-31 11:57:35 +01:00
if ( $result && $this -> db -> num_rows ( $result ) == 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 404 , 'This third party does not have any gateway attached or does not exist.' );
} else {
2019-11-14 12:09:15 +01:00
$i = 0 ;
2018-08-26 17:29:04 +02:00
2020-09-19 23:30:29 +02:00
$num = $this -> db -> num_rows ( $result );
2021-02-26 21:17:52 +01:00
while ( $i < $num ) {
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
$account = new SocieteAccount ( $this -> db );
2018-08-26 17:29:04 +02:00
$account -> fetch ( $obj -> rowid );
2019-11-14 12:09:15 +01:00
if ( $account -> delete ( DolibarrApiAccess :: $user ) < 0 ) {
2018-08-26 17:29:04 +02:00
throw new RestException ( 500 , 'Error while deleting gateways attached to this third party' );
}
$i ++ ;
}
}
}
2017-10-25 11:39:23 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2017-05-24 15:24:35 +02:00
/**
* Clean sensible object datas
*
2020-10-30 18:02:58 +01:00
* @ param Object $object Object to clean
2020-12-21 15:23:04 +01:00
* @ return array | mixed Object with cleaned properties
2017-05-24 15:24:35 +02:00
*/
2019-03-04 19:57:46 +01:00
protected function _cleanObjectDatas ( $object )
2020-10-31 14:32:18 +01:00
{
// phpcs:enable
2018-02-12 04:07:45 +01:00
$object = parent :: _cleanObjectDatas ( $object );
2017-10-03 13:07:07 +02:00
2019-11-16 23:24:29 +01:00
unset ( $object -> nom ); // ->name already defined and nom deprecated
unset ( $object -> name_bis ); // ->name_alias already defined
unset ( $object -> note ); // ->note_private and note_public already defined
2019-11-02 14:20:43 +01:00
unset ( $object -> departement );
unset ( $object -> departement_code );
unset ( $object -> pays );
unset ( $object -> particulier );
2019-11-02 14:26:27 +01:00
unset ( $object -> prefix_comm );
2018-01-11 01:16:14 +01:00
2020-04-10 10:59:32 +02:00
unset ( $object -> commercial_id ); // This property is used in create/update only. It does not exists in read mode because there is several sales representatives.
2020-01-13 14:14:51 +01:00
2018-02-12 04:07:45 +01:00
unset ( $object -> total_ht );
unset ( $object -> total_tva );
unset ( $object -> total_localtax1 );
unset ( $object -> total_localtax2 );
unset ( $object -> total_ttc );
2017-10-03 13:07:07 +02:00
2018-02-12 04:07:45 +01:00
unset ( $object -> lines );
unset ( $object -> thirdparty );
2017-12-21 16:50:18 +01:00
2019-11-16 23:24:29 +01:00
unset ( $object -> fk_delivery_address ); // deprecated feature
2019-11-02 14:20:43 +01:00
2020-12-04 23:55:47 +01:00
unset ( $object -> skype );
unset ( $object -> twitter );
unset ( $object -> facebook );
unset ( $object -> linkedin );
2021-10-27 16:39:34 +02:00
unset ( $object -> instagram );
unset ( $object -> snapchat );
unset ( $object -> googleplus );
unset ( $object -> youtube );
unset ( $object -> whatsapp );
2020-12-04 23:55:47 +01:00
2018-02-12 04:07:45 +01:00
return $object ;
2017-10-03 13:07:07 +02:00
}
2017-05-24 15:24:35 +02:00
/**
2018-02-12 04:07:45 +01:00
* Validate fields before create or update object
*
* @ param array $data Datas to validate
* @ return array
*
* @ throws RestException
*/
2020-10-31 14:32:18 +01:00
private function _validate ( $data )
{
$thirdparty = array ();
foreach ( Thirdparties :: $FIELDS as $field ) {
2021-02-26 21:17:52 +01:00
if ( ! isset ( $data [ $field ])) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 400 , " $field field missing " );
2021-02-26 21:17:52 +01:00
}
2020-10-31 14:32:18 +01:00
$thirdparty [ $field ] = $data [ $field ];
}
return $thirdparty ;
}
/**
* Fetch properties of a thirdparty object .
*
* Return an array with thirdparty informations
*
2021-03-08 16:22:26 +01:00
* @ param int $rowid Id of third party to load ( Use 0 to get a specimen record , use null to use other search criterias )
2019-10-19 16:43:10 +02:00
* @ param string $ref Reference of third party , name ( Warning , this can return several records )
* @ param string $ref_ext External reference of third party ( Warning , this information is a free field not provided by Dolibarr )
2020-08-29 11:37:16 +02:00
* @ param string $barcode Barcode of third party to load
2019-10-19 16:43:10 +02:00
* @ param string $idprof1 Prof id 1 of third party ( Warning , this can return several records )
* @ param string $idprof2 Prof id 2 of third party ( Warning , this can return several records )
* @ param string $idprof3 Prof id 3 of third party ( Warning , this can return several records )
* @ param string $idprof4 Prof id 4 of third party ( Warning , this can return several records )
* @ param string $idprof5 Prof id 5 of third party ( Warning , this can return several records )
* @ param string $idprof6 Prof id 6 of third party ( Warning , this can return several records )
* @ param string $email Email of third party ( Warning , this can return several records )
* @ param string $ref_alias Name_alias of third party ( Warning , this can return several records )
2020-12-21 15:23:04 +01:00
* @ return array | mixed cleaned Societe object
2020-10-31 14:32:18 +01:00
*
* @ throws RestException
*/
private function _fetch ( $rowid , $ref = '' , $ref_ext = '' , $barcode = '' , $idprof1 = '' , $idprof2 = '' , $idprof3 = '' , $idprof4 = '' , $idprof5 = '' , $idprof6 = '' , $email = '' , $ref_alias = '' )
{
global $conf ;
2021-04-08 19:05:28 +02:00
2020-10-31 14:32:18 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> lire ) {
throw new RestException ( 401 );
}
2021-04-08 19:05:28 +02:00
2021-03-08 00:17:58 +01:00
if ( $rowid === 0 ) {
2020-10-29 23:56:42 +01:00
$result = $this -> company -> initAsSpecimen ();
} else {
$result = $this -> company -> fetch ( $rowid , $ref , $ref_ext , $barcode , $idprof1 , $idprof2 , $idprof3 , $idprof4 , $idprof5 , $idprof6 , $email , $ref_alias );
}
2020-10-31 14:32:18 +01:00
if ( ! $result ) {
throw new RestException ( 404 , 'Thirdparty not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $this -> company -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
$filterabsolutediscount = " fk_facture_source IS NULL " ; // If we want deposit to be substracted to payments only and not to total of final invoice
$filtercreditnote = " fk_facture_source IS NOT NULL " ; // If we want deposit to be substracted to payments only and not to total of final invoice
} else {
$filterabsolutediscount = " fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%') " ;
$filtercreditnote = " fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%') " ;
}
$absolute_discount = $this -> company -> getAvailableDiscounts ( '' , $filterabsolutediscount );
$absolute_creditnote = $this -> company -> getAvailableDiscounts ( '' , $filtercreditnote );
$this -> company -> absolute_discount = price2num ( $absolute_discount , 'MT' );
$this -> company -> absolute_creditnote = price2num ( $absolute_creditnote , 'MT' );
return $this -> _cleanObjectDatas ( $this -> company );
}
2015-05-01 16:12:30 +02:00
}