diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index cc7db3db852..d53634a4e08 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -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' + ) + ); } /**