Merge pull request #10599 from ptibogxiv/patch-114

NEW  link/delete contact in invoice API
This commit is contained in:
Laurent Destailleur 2019-02-14 13:42:35 +01:00 committed by GitHub
commit a18496d1d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -372,6 +372,86 @@ class Invoices extends DolibarrApi
throw new RestException(304, $this->invoice->error);
}
}
/**
* Add a contact type of given invoice
*
* @param int $id Id of invoice to update
* @param int $contactid Id of contact to add
* @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER)
*
* @url POST {id}/contact/{contactid}/{type}
*
* @return int
* @throws 401
* @throws 404
*/
function postContact($id, $contactid, $type)
{
if(!DolibarrApiAccess::$user->rights->facture->creer) {
throw new RestException(401);
}
$result = $this->facture->fetch($id);
if(!$result) {
throw new RestException(404, 'Invoice not found');
}
if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
}
if(!DolibarrApi::_checkAccessToResource('invoice', $this->facture->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->facture->add_contact($contactid, $type, 'external');
if (!$result) {
throw new RestException(500, 'Error when added the contact');
}
return $this->facture;
}
/**
* Delete a contact type of given invoice
*
* @param int $id Id of invoice to update
* @param int $rowid Row key of the contact in the array contact_ids.
*
* @url DELETE {id}/contact/{rowid}
*
* @return int
* @throws 401
* @throws 404
* @throws 500
*/
function deleteContact($id, $rowid)
{
if(!DolibarrApiAccess::$user->rights->facture->creer) {
throw new RestException(401);
}
$result = $this->facture->fetch($id);
if(!$result) {
throw new RestException(404, 'Invoice not found');
}
if(!DolibarrApi::_checkAccessToResource('invoice', $this->facture->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->facture->delete_contact($rowid);
if (!$result) {
throw new RestException(500, 'Error when deleted the contact');
}
return $this->facture;
}
/**
* Deletes a line of a given invoice