integrate Contact Management for Supplier Orders

This commit is contained in:
Christian Humpel 2023-01-21 23:20:43 +01:00
parent 9a5f19984b
commit e48b16a9c5

View File

@ -288,6 +288,155 @@ class SupplierOrders extends DolibarrApi
return false;
}
/**
* Get contacts of given supplier order
*
* Return an array with contact informations
*
* @param int $id ID of supplier order
* @param string $source Source of the contact (internal, external, all).
* @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER, SALESREPFOLL, ...)
* @return Object Object with cleaned properties
*
* @url GET {id}/contacts
*
* @throws RestException
*/
public function getContacts($id, $source, $type = '')
{
if (!DolibarrApiAccess::$user->rights->fournisseur->commande->lire) {
throw new RestException(401);
}
$result = $this->order->fetch($id);
if (!$result) {
throw new RestException(404, 'Supplier order not found');
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$contacts = array();
if ($source == 'all' || $source == 'external') {
$tmpContacts = $this->order->liste_contact(-1, 'external', 0, $type);
$contacts = array_merge($contacts, $tmpContacts);
}
if ($source == 'all' || $source == 'internal') {
$tmpContacts = $this->order->liste_contact(-1, 'internal', 0, $type);
$contacts = array_merge($contacts, $tmpContacts);
}
return $this->_cleanObjectDatas($contacts);
}
/**
* Add a contact type of given supplier order
*
* @param int $id Id of supplier order to update
* @param int $contactid Id of contact/user to add
* @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER, SALESREPFOLL, ...)
* @param string $source Source of the contact (external, internal)
* @return array
*
* @url POST {id}/contact/{contactid}/{type}/{source}
*
* @throws RestException 401
* @throws RestException 404
*/
public function postContact($id, $contactid, $type, $source)
{
if (!DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
throw new RestException(401);
}
$result = $this->order->fetch($id);
if (!$result) {
throw new RestException(404, 'Supplier order not found');
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->order->add_contact($contactid, $type, $source);
if ($result < 0) {
throw new RestException(500, 'Error when added the contact');
}
if ($result == 0) {
throw new RestException(304, 'contact already added');
}
return array(
'success' => array(
'code' => 200,
'message' => 'Contact linked to the order'
)
);
}
/**
* Unlink a contact type of given supplier order
*
* @param int $id Id of supplier order to update
* @param int $contactid Id of contact/user to add
* @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER, SALESREPFOLL, ...).
* @param string $source Source of the contact (internal, external).
*
* @url DELETE {id}/contact/{contactid}/{type}/{source}
*
* @return array
*
* @throws RestException 401
* @throws RestException 404
* @throws RestException 500 System error
*/
public function deleteContact($id, $contactid, $type, $source)
{
if (!DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
throw new RestException(401);
}
$result = $this->order->fetch($id);
if (!$result) {
throw new RestException(404, 'Supplier order not found');
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$contacts = $this->order->liste_contact(-1, $source, 0, $type);
$contactToUnlink = 0;
foreach ($contacts as $contact) {
if ($contact['id'] == $contactid && $contact['code'] == $type) {
$contactToUnlink = $contact['rowid'];
break;
}
}
if ($contactToUnlink == 0) {
throw new RestException(404, 'Linked contact not found');
}
$result = $this->order->delete_contact($contact['rowid']);
if (!$result) {
throw new RestException(500, 'Error when deleted the contact');
}
return array(
'success' => array(
'code' => 200,
'message' => 'Contact unlinked from supplier order'
)
);
}
/**
* Delete supplier order
*