2
0
forked from Wavyzz/dolibarr

NEW endpoint getContacts and Clean results

NEW Get contacts list of a given order
FIX bad function used to unlink a contact
FIX Result for link contact and unlink contact
This commit is contained in:
Cédric
2020-09-04 12:36:03 +02:00
committed by GitHub
parent 6eb1be02ad
commit 9141b6b092

View File

@@ -456,6 +456,40 @@ class Orders extends DolibarrApi
}
}
/**
* Get contacts of given order
*
* Return an array with contact informations
*
* @param int $id ID of order
* @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER)
*
* @url GET {id}/contacts
*
* @return array data without useless information
*
* @throws RestException
*/
public function getContacts($id, $type = '')
{
if (! DolibarrApiAccess::$user->rights->commande->lire) {
throw new RestException(401);
}
$result = $this->commande->fetch($id);
if ( ! $result ) {
throw new RestException(404, 'Order not found');
}
if ( ! DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
return $this->_cleanObjectDatas($contacts);
}
/**
* Add a contact type of given order
*
@@ -487,15 +521,24 @@ class Orders extends DolibarrApi
$result = $this->commande->add_contact($contactid, $type, 'external');
if (!$result) {
if ($result < 0) {
throw new RestException(500, 'Error when added the contact');
}
return $this->commande;
if ($result == 0) {
throw new RestException(304, 'contact already added');
}
return array(
'success' => array(
'code' => 200,
'message' => 'Contact linked to the order'
)
);
}
/**
* Delete a contact type of given order
* Unlink a contact type of given order
*
* @param int $id Id of order to update
* @param int $rowid Row key of the contact in the array contact_ids.
@@ -510,26 +553,31 @@ class Orders extends DolibarrApi
*/
public function deleteContact($id, $rowid)
{
if (!DolibarrApiAccess::$user->rights->commande->creer) {
if (! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401);
}
$result = $this->commande->fetch($id);
if (!$result) {
if (! $result) {
throw new RestException(404, 'Order not found');
}
if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
if (! DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->commande->delete_contact($rowid);
$result = $this->commande->delete_linked_contact($rowid);
if (!$result) {
throw new RestException(500, 'Error when deleted the contact');
}
return $this->commande;
return array(
'success' => array(
'code' => 200,
'message' => 'Contact unlinked from order'
)
);
}
/**