New REST API improvements to thirdparties bank accounts and gateways
- API endpoints has been added and documented to allow user to add, update and delete payment gateways like the one used by Stripe module (stored in table llx_societe_account) - API endpoints for CompanyBankAccounts has been slightly modified to be harmonized with other API endpoints (this should not be a problem since these methods are not yet in Dolibarr latest stable version (aka 8.0)) - Some documentation has been improved (to be shown in REST explorer) - societeaccount.class.php has been slightly modified to include missing $login member ; notnull value of member fk_website is now -1 to put it to NULL when updating the record.
This commit is contained in:
parent
c0f98ab4c8
commit
d8e15e529c
@ -32,7 +32,7 @@ class Thirdparties extends DolibarrApi
|
||||
* @var array $FIELDS Mandatory fields, checked when create and update object
|
||||
*/
|
||||
static $FIELDS = array(
|
||||
'name',
|
||||
'name'
|
||||
);
|
||||
|
||||
/**
|
||||
@ -49,6 +49,7 @@ class Thirdparties extends DolibarrApi
|
||||
$this->db = $db;
|
||||
|
||||
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
|
||||
require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
|
||||
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
|
||||
|
||||
$this->company = new Societe($this->db);
|
||||
@ -114,8 +115,7 @@ class Thirdparties extends DolibarrApi
|
||||
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.nom:like:'TheCompany%') and (t.date_creation:<:'20160101')"
|
||||
* @return array Array of thirdparty objects
|
||||
*/
|
||||
function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode=0, $sqlfilters = '')
|
||||
{
|
||||
function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode=0, $sqlfilters = '') {
|
||||
global $db, $conf;
|
||||
|
||||
$obj_ret = array();
|
||||
@ -130,7 +130,6 @@ class Thirdparties extends DolibarrApi
|
||||
$sql = "SELECT t.rowid";
|
||||
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)
|
||||
$sql.= " FROM ".MAIN_DB_PREFIX."societe as t";
|
||||
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX . "societe_extrafields as te ON te.fk_object = t.rowid";
|
||||
|
||||
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
|
||||
$sql.= ", ".MAIN_DB_PREFIX."c_stcomm as st";
|
||||
@ -215,7 +214,7 @@ class Thirdparties extends DolibarrApi
|
||||
if ($this->company->create(DolibarrApiAccess::$user) < 0)
|
||||
throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
|
||||
|
||||
return $this->company->id;
|
||||
return $this->company->id;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -248,7 +247,7 @@ class Thirdparties extends DolibarrApi
|
||||
if($this->company->update($id, DolibarrApiAccess::$user,1,'','','update'))
|
||||
return $this->get($id);
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -314,12 +313,12 @@ class Thirdparties extends DolibarrApi
|
||||
$object->client = $object->client | $soc_origin->client;
|
||||
$object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
|
||||
$listofproperties=array(
|
||||
'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'
|
||||
'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'
|
||||
);
|
||||
foreach ($listofproperties as $property)
|
||||
{
|
||||
@ -328,7 +327,7 @@ class Thirdparties extends DolibarrApi
|
||||
|
||||
// Concat some data
|
||||
$listofproperties=array(
|
||||
'note_public', 'note_private'
|
||||
'note_public', 'note_private'
|
||||
);
|
||||
foreach ($listofproperties as $property)
|
||||
{
|
||||
@ -374,27 +373,27 @@ class Thirdparties extends DolibarrApi
|
||||
if (! $error)
|
||||
{
|
||||
$objects = array(
|
||||
'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',
|
||||
'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',
|
||||
'Livraison' => '/livraison/class/livraison.class.php',
|
||||
'Product' => '/product/class/product.class.php',
|
||||
'Project' => '/projet/class/project.class.php',
|
||||
'User' => '/user/class/user.class.php',
|
||||
'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',
|
||||
'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',
|
||||
'Livraison' => '/livraison/class/livraison.class.php',
|
||||
'Product' => '/product/class/product.class.php',
|
||||
'Project' => '/projet/class/project.class.php',
|
||||
'User' => '/user/class/user.class.php',
|
||||
);
|
||||
|
||||
//First, all core objects must update their tables
|
||||
@ -414,8 +413,8 @@ class Thirdparties extends DolibarrApi
|
||||
if (!$errors)
|
||||
{
|
||||
$reshook = $hookmanager->executeHooks('replaceThirdparty', array(
|
||||
'soc_origin' => $soc_origin->id,
|
||||
'soc_dest' => $object->id
|
||||
'soc_origin' => $soc_origin->id,
|
||||
'soc_dest' => $object->id
|
||||
), $soc_dest, $action);
|
||||
|
||||
if ($reshook < 0)
|
||||
@ -930,8 +929,7 @@ class Thirdparties extends DolibarrApi
|
||||
* @throws 404
|
||||
* @throws 405
|
||||
*/
|
||||
function getInvoicesQualifiedForReplacement($id)
|
||||
{
|
||||
function getInvoicesQualifiedForReplacement($id) {
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->facture->lire) {
|
||||
throw new RestException(401);
|
||||
@ -973,8 +971,7 @@ class Thirdparties extends DolibarrApi
|
||||
* @throws 404
|
||||
* @throws 405
|
||||
*/
|
||||
function getInvoicesQualifiedForCreditNote($id)
|
||||
{
|
||||
function getInvoicesQualifiedForCreditNote($id) {
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->facture->lire) {
|
||||
throw new RestException(401);
|
||||
@ -1003,23 +1000,24 @@ class Thirdparties extends DolibarrApi
|
||||
/**
|
||||
* Get CompanyBankAccount objects for thirdparty
|
||||
*
|
||||
* @param int $socid Thirdparty id
|
||||
* @param int $id ID of thirdparty
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @url GET {id}/bankaccounts
|
||||
*/
|
||||
function getCompanyBankAccount($socid)
|
||||
{
|
||||
function getCompanyBankAccount($id){
|
||||
|
||||
global $db, $conf;
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->facture->lire) {
|
||||
throw new RestException(401);
|
||||
}
|
||||
if(empty($socid)) {
|
||||
if(empty($id)) {
|
||||
throw new RestException(400, 'Thirdparty ID is mandatory');
|
||||
}
|
||||
|
||||
if( ! DolibarrApi::_checkAccessToResource('societe',$socid)) {
|
||||
if( ! DolibarrApi::_checkAccessToResource('societe',$id)) {
|
||||
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
|
||||
}
|
||||
|
||||
@ -1030,7 +1028,7 @@ class Thirdparties extends DolibarrApi
|
||||
$sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
|
||||
$sql.= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
|
||||
$sql.= " FROM ".MAIN_DB_PREFIX."societe_rib";
|
||||
if ($socid) $sql.= " WHERE fk_soc = ".$socid." ";
|
||||
if ($id) $sql.= " WHERE fk_soc = ".$id." ";
|
||||
|
||||
|
||||
$result = $db->query($sql);
|
||||
@ -1068,10 +1066,10 @@ class Thirdparties extends DolibarrApi
|
||||
foreach($accounts as $account){
|
||||
$object= [];
|
||||
foreach($account as $key => $value)
|
||||
if(in_array($key, $fields)){
|
||||
$object[$key] = $value;
|
||||
if(in_array($key, $fields)){
|
||||
$object[$key] = $value;
|
||||
|
||||
}
|
||||
}
|
||||
$returnAccounts[] = $object;
|
||||
}
|
||||
|
||||
@ -1081,14 +1079,14 @@ class Thirdparties extends DolibarrApi
|
||||
|
||||
/**
|
||||
* Create CompanyBankAccount object for thirdparty
|
||||
* @param int $socid thirdparty id
|
||||
* @param array $request_data Request datas
|
||||
* @param int $id ID of thirdparty
|
||||
* @param array $request_data
|
||||
*
|
||||
* @return object ID of thirdparty
|
||||
*
|
||||
* @url POST {socid}/CompanyBankAccount
|
||||
* @url POST {id}/bankaccounts
|
||||
*/
|
||||
function createCompanyBankAccount($socid, $request_data = null)
|
||||
function createCompanyBankAccount($id, $request_data = null)
|
||||
{
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
@ -1096,7 +1094,7 @@ class Thirdparties extends DolibarrApi
|
||||
|
||||
$account = new CompanyBankAccount($this->db);
|
||||
|
||||
$account->socid = $socid;
|
||||
$account->socid = $id;
|
||||
|
||||
foreach($request_data as $field => $value) {
|
||||
$account->$field = $value;
|
||||
@ -1109,22 +1107,22 @@ class Thirdparties extends DolibarrApi
|
||||
if ($account->update(DolibarrApiAccess::$user) < 0)
|
||||
throw new RestException(500, 'Error updating values');
|
||||
|
||||
return $account;
|
||||
return $account;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update CompanyBankAccount object for thirdparty
|
||||
*
|
||||
* @param int $socid Thirdparty id
|
||||
* @param int $id CompanyBankAccount's id
|
||||
* @param array $request_data Request datas
|
||||
* @param int $id ID of thirdparty
|
||||
* @param int $bankaccount_id ID of CompanyBankAccount
|
||||
* @param array $request_data
|
||||
*
|
||||
* @return object ID of thirdparty
|
||||
*
|
||||
* @url PUT {socid}/CompanyBankAccount/{id}
|
||||
* @url PUT {id}/bankaccounts/{bankaccount_id}
|
||||
*/
|
||||
function updateCompanyBankAccount($socid, $id, $request_data = null)
|
||||
function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
|
||||
{
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
@ -1132,10 +1130,10 @@ class Thirdparties extends DolibarrApi
|
||||
|
||||
$account = new CompanyBankAccount($this->db);
|
||||
|
||||
$account->fetch($id, $socid, -1, '');
|
||||
$account->fetchFromApi($bankaccount_id, $id);
|
||||
|
||||
|
||||
if($account->socid != $socid){
|
||||
if($account->socid != $bankaccount_id){
|
||||
throw new RestException(401);
|
||||
}
|
||||
|
||||
@ -1151,15 +1149,16 @@ class Thirdparties extends DolibarrApi
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id CompanyBankAccount's id
|
||||
* @param int $socid Thirdparty id
|
||||
* Delete a bank account attached to a thirdparty
|
||||
*
|
||||
* @param int $id ID of thirdparty
|
||||
* @param int $bankaccount_id ID of CompanyBankAccount
|
||||
*
|
||||
* @return int -1 if error 1 if correct deletion
|
||||
*
|
||||
* @url DELETE {socid}/CompanyBankAccount/{id}
|
||||
* @url DELETE {id}/bankaccounts/{bankaccount_id}
|
||||
*/
|
||||
function deleteCompanyBankAccount($id, $socid)
|
||||
{
|
||||
function deleteCompanyBankAccount($bankaccount_id, $id){
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
@ -1167,14 +1166,373 @@ class Thirdparties extends DolibarrApi
|
||||
|
||||
$account = new CompanyBankAccount($this->db);
|
||||
|
||||
$account->fetch($id);
|
||||
$account->fetch($bankaccount_id);
|
||||
|
||||
if(!$account->socid == $socid)
|
||||
if(!$account->socid == $id)
|
||||
throw new RestException(401);
|
||||
|
||||
return $account->delete(DolibarrApiAccess::$user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific gateway attached to a thirdparty (by specifying the site key)
|
||||
*
|
||||
* @param int $id ID of thirdparty
|
||||
* @param string $site Site key
|
||||
*
|
||||
* @return SocieteAccount[]
|
||||
* @throws 401 Unauthorized: User does not have permission to read thirdparties
|
||||
* @throws 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
|
||||
*
|
||||
* @url GET {id}/gateways/
|
||||
*/
|
||||
function getSocieteAccounts($id, $site=null){
|
||||
|
||||
global $db, $conf;
|
||||
|
||||
if(!DolibarrApiAccess::$user->rights->societe->lire) {
|
||||
throw new RestException(401);
|
||||
}
|
||||
|
||||
if(!DolibarrApi::_checkAccessToResource('societe',$id)) {
|
||||
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";
|
||||
$sql.= " WHERE fk_soc = $id";
|
||||
if($site) $sql .= " AND site ='$site'";
|
||||
|
||||
$result = $db->query($sql);
|
||||
|
||||
if($result->num_rows == 0){
|
||||
throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
|
||||
}
|
||||
|
||||
$i=0;
|
||||
|
||||
$accounts =[];
|
||||
|
||||
$num = $db->num_rows($result);
|
||||
while ($i < $num)
|
||||
{
|
||||
$obj = $db->fetch_object($result);
|
||||
$account = new SocieteAccount($db);
|
||||
|
||||
if($account->fetch($obj->rowid)) {
|
||||
$accounts[] = $account;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
|
||||
$fields = ['id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms'];
|
||||
|
||||
$returnAccounts = [];
|
||||
|
||||
foreach($accounts as $account){
|
||||
$object= [];
|
||||
foreach($account as $key => $value)
|
||||
if(in_array($key, $fields)){
|
||||
$object[$key] = $value;
|
||||
|
||||
}
|
||||
$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
|
||||
* @param array $request_data
|
||||
*
|
||||
* @return SocieteAccount
|
||||
* @throws 401 Unauthorized: User does not have permission to read thirdparties
|
||||
* @throws 409 Conflict: A SocieteAccount entity (gateway) already exists for this company and site.
|
||||
* @throws 422 Unprocessable Entity: You must pass the site attribute in your request data !
|
||||
* @throws 500 Internal Server Error: Error creating SocieteAccount account
|
||||
* @status 201
|
||||
*
|
||||
* @url POST {id}/gateways
|
||||
*/
|
||||
function createSocieteAccount($id, $request_data = null)
|
||||
{
|
||||
global $db;
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
}
|
||||
|
||||
if(!isset($request_data['site'])) {
|
||||
throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
|
||||
}
|
||||
|
||||
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id." AND site = '". $request_data['site']."' ";
|
||||
$result = $db->query($sql);
|
||||
|
||||
if($result->num_rows == 0 ){
|
||||
$account = new SocieteAccount($this->db);
|
||||
if(!isset($request_data['login'])) {
|
||||
$account->login = "";
|
||||
}
|
||||
$account->fk_soc = $id;
|
||||
|
||||
foreach($request_data as $field => $value) {
|
||||
$account->$field = $value;
|
||||
}
|
||||
|
||||
if ($account->create(DolibarrApiAccess::$user) < 0)
|
||||
throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
|
||||
|
||||
$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
|
||||
* @param array $request_data
|
||||
*
|
||||
* @return SocieteAccount
|
||||
* @throws 401 Unauthorized: User does not have permission to read thirdparties
|
||||
* @throws 422 Unprocessable Entity: You must pass the site attribute in your request data !
|
||||
* @throws 500 Internal Server Error: Error updating SocieteAccount entity
|
||||
*
|
||||
* @throws RestException
|
||||
* @url PUT {id}/gateways/{site}
|
||||
*/
|
||||
function putSocieteAccount($id, $site, $request_data = null)
|
||||
{
|
||||
|
||||
global $db;
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
}
|
||||
|
||||
$sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '$site' ";
|
||||
$result = $db->query($sql);
|
||||
|
||||
// We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
|
||||
if($result->num_rows == 0 ){
|
||||
if(!isset($request_data['key_account'])) {
|
||||
throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
|
||||
}
|
||||
$account = new SocieteAccount($this->db);
|
||||
if(!isset($request_data['login'])) {
|
||||
$account->login = "";
|
||||
}
|
||||
|
||||
foreach($request_data as $field => $value) {
|
||||
$account->$field = $value;
|
||||
}
|
||||
|
||||
$account->fk_soc = $id;
|
||||
$account->site = $site;
|
||||
|
||||
if ($account->create(DolibarrApiAccess::$user) < 0)
|
||||
throw new RestException(500, 'Error creating SocieteAccount entity.');
|
||||
// We found an existing SocieteAccount entity, we are replacing it
|
||||
} else {
|
||||
|
||||
if(isset($request_data['site']) && $request_data['site'] !== $site) {
|
||||
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id." AND site = '". $request_data['site']."' ";
|
||||
$result = $db->query($sql);
|
||||
|
||||
if($result->num_rows !== 0)
|
||||
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.");
|
||||
}
|
||||
|
||||
$obj = $db->fetch_object($result);
|
||||
|
||||
$account = new SocieteAccount($this->db);
|
||||
$account->id = $obj->rowid;
|
||||
$account->fk_soc = $id;
|
||||
$account->site = $site;
|
||||
if(!isset($request_data['login'])) {
|
||||
$account->login = "";
|
||||
}
|
||||
$account->fk_user_creat = $obj->fk_user_creat;
|
||||
$account->date_creation = $obj->date_creation;
|
||||
|
||||
foreach($request_data as $field => $value) {
|
||||
$account->$field = $value;
|
||||
}
|
||||
|
||||
if ($account->update(DolibarrApiAccess::$user) < 0)
|
||||
throw new RestException(500, 'Error updating SocieteAccount entity.');
|
||||
}
|
||||
|
||||
$this->_cleanObjectDatas($account);
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update specified values of a specific site gateway attached to a thirdparty
|
||||
*
|
||||
* @param int $id Id of thirdparty
|
||||
* @param string $site Site key
|
||||
* @param array $request_data
|
||||
*
|
||||
* @return SocieteAccount
|
||||
* @throws 401 Unauthorized: User does not have permission to read thirdparties
|
||||
* @throws 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
|
||||
* @throws 409 Conflict: Another SocieteAccount entity already exists for this thirdparty with this site key.
|
||||
* @throws 500 Internal Server Error: Error updating SocieteAccount entity
|
||||
*
|
||||
* @url PATCH {id}/gateways/{site}
|
||||
*/
|
||||
function patchSocieteAccount($id, $site, $request_data = null)
|
||||
{
|
||||
global $db;
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
}
|
||||
|
||||
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '$site' ";
|
||||
$result = $db->query($sql);
|
||||
|
||||
if($result->num_rows == 0 ){
|
||||
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
|
||||
if(isset($request_data['site']) && $request_data['site'] !== $site) {
|
||||
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id." AND site = '". $request_data['site']."' ";
|
||||
$result = $db->query($sql);
|
||||
|
||||
if($result->num_rows !== 0)
|
||||
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.");
|
||||
}
|
||||
|
||||
$obj = $db->fetch_object($result);
|
||||
$account = new SocieteAccount($this->db);
|
||||
$account->fetch($obj->rowid);
|
||||
|
||||
foreach($request_data as $field => $value) {
|
||||
$account->$field = $value;
|
||||
}
|
||||
|
||||
if ($account->update(DolibarrApiAccess::$user) < 0)
|
||||
throw new RestException(500, 'Error updating SocieteAccount account');
|
||||
|
||||
$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
|
||||
* @throws 401 Unauthorized: User does not have permission to delete thirdparties gateways
|
||||
* @throws 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
|
||||
* @throws 500 Internal Server Error: Error deleting SocieteAccount entity
|
||||
*
|
||||
* @url DELETE {id}/gateways/{site}
|
||||
*/
|
||||
function deleteSocieteAccount($site, $id){
|
||||
|
||||
global /** @var Database $db */
|
||||
$db;
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
}
|
||||
|
||||
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '$site' ";
|
||||
$result = $db->query($sql);
|
||||
|
||||
if($result->num_rows == 0 ){
|
||||
throw new RestException(404);
|
||||
} else {
|
||||
$obj = $db->fetch_object($result);
|
||||
$account = new SocieteAccount($this->db);
|
||||
$account->fetch($obj->rowid);
|
||||
|
||||
if($account->delete(DolibarrApiAccess::$user) < 0) {
|
||||
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
|
||||
* @throws 401 Unauthorized: User does not have permission to delete thirdparties gateways
|
||||
* @throws 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
|
||||
* @throws 500 Internal Server Error: Error deleting SocieteAccount entity
|
||||
*
|
||||
* @url DELETE {id}/gateways
|
||||
*/
|
||||
function deleteSocieteAccounts($id){
|
||||
|
||||
global /** @var Database $db */
|
||||
$db;
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
}
|
||||
|
||||
/**
|
||||
* We select all the records that match the socid
|
||||
*/
|
||||
|
||||
$sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
|
||||
$sql.= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id ";
|
||||
|
||||
$result = $db->query($sql);
|
||||
|
||||
if($result->num_rows == 0 ){
|
||||
throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
|
||||
} else {
|
||||
$i=0;
|
||||
|
||||
$num = $db->num_rows($result);
|
||||
while ($i < $num)
|
||||
{
|
||||
$obj = $db->fetch_object($result);
|
||||
$account = new SocieteAccount($db);
|
||||
$account->fetch($obj->rowid);
|
||||
|
||||
if($account->delete(DolibarrApiAccess::$user) < 0) {
|
||||
throw new RestException(500, 'Error while deleting gateways attached to this third party');
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean sensible object datas
|
||||
@ -1182,8 +1540,7 @@ class Thirdparties extends DolibarrApi
|
||||
* @param object $object Object to clean
|
||||
* @return array Array of cleaned object properties
|
||||
*/
|
||||
function _cleanObjectDatas($object)
|
||||
{
|
||||
function _cleanObjectDatas($object) {
|
||||
|
||||
$object = parent::_cleanObjectDatas($object);
|
||||
|
||||
@ -1215,7 +1572,7 @@ class Thirdparties extends DolibarrApi
|
||||
foreach (Thirdparties::$FIELDS as $field) {
|
||||
if (!isset($data[$field]))
|
||||
throw new RestException(400, "$field field missing");
|
||||
$thirdparty[$field] = $data[$field];
|
||||
$thirdparty[$field] = $data[$field];
|
||||
}
|
||||
return $thirdparty;
|
||||
}
|
||||
|
||||
@ -84,7 +84,7 @@ class SocieteAccount extends CommonObject
|
||||
'pass_temp' => array('type'=>'varchar(128)', 'label'=>'Temp', 'visible'=>0, 'enabled'=>0, 'position'=>32, 'notnull'=>-1,),
|
||||
'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'visible'=>1, 'enabled'=>1, 'position'=>40, 'notnull'=>-1, 'index'=>1),
|
||||
'site' => array('type'=>'varchar(128)', 'label'=>'Site', 'visible'=>-1, 'enabled'=>1, 'position'=>41),
|
||||
'fk_website' => array('type'=>'integer:Website:website/class/website.class.php', 'label'=>'WebSite', 'visible'=>1, 'enabled'=>1, 'position'=>42, 'notnull'=>1, 'index'=>1),
|
||||
'fk_website' => array('type'=>'integer:Website:website/class/website.class.php', 'label'=>'WebSite', 'visible'=>1, 'enabled'=>1, 'position'=>42, 'notnull'=>-1, 'index'=>1),
|
||||
'date_last_login' => array('type'=>'datetime', 'label'=>'LastConnexion', 'visible'=>2, 'enabled'=>1, 'position'=>50, 'notnull'=>0,),
|
||||
'date_previous_login' => array('type'=>'datetime', 'label'=>'PreviousConnexion', 'visible'=>2, 'enabled'=>1, 'position'=>51, 'notnull'=>0,),
|
||||
//'note_public' => array('type'=>'text', 'label'=>'NotePublic', 'visible'=>-1, 'enabled'=>1, 'position'=>45, 'notnull'=>-1,),
|
||||
@ -99,6 +99,7 @@ class SocieteAccount extends CommonObject
|
||||
public $rowid;
|
||||
public $entity;
|
||||
public $key_account;
|
||||
public $login;
|
||||
public $pass_encoding;
|
||||
public $pass_crypted;
|
||||
public $pass_temp;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user