Merge pull request #23651 from Humml87/develop_REST_API_SupplierOrder_Contacts

New: REST API SupplierOrders contact management
This commit is contained in:
Laurent Destailleur 2023-01-23 01:38:44 +01:00 committed by GitHub
commit faa9145d6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 150 additions and 0 deletions

View File

@ -1397,6 +1397,7 @@ abstract class CommonObject
$transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
$libelle_type = ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->libelle);
$tab[$i] = array(
'parentId' => $this->id,
'source' => $obj->source,
'socid' => $obj->socid,
'id' => $obj->id,

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
*