Rest API: Proposal, Orders, Invoices: Add contact details

This commit is contained in:
Jimmy L 2019-02-12 17:54:59 +01:00
parent 25550959e3
commit a172ea00a4
3 changed files with 93 additions and 11 deletions

View File

@ -57,12 +57,13 @@ class Proposals extends DolibarrApi
*
* Return an array with commercial proposal informations
*
* @param int $id ID of commercial proposal
* @param int $id ID of commercial proposal
* @param int $contact_list 0:Return array contains all properties, 1:Return array contains just id
* @return array|mixed data without useless information
*
* @throws RestException
*/
function get($id)
function get($id, $contact_list = 1)
{
if(! DolibarrApiAccess::$user->rights->propal->lire) {
throw new RestException(401);
@ -77,9 +78,8 @@ class Proposals extends DolibarrApi
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
// Add external contacts ids
$this->propal->contacts_ids = $this->propal->liste_contact(-1, 'external', 1);
// Add external contacts ids.
$this->propal->contacts_ids = $this->propal->liste_contact(-1, 'external', $contact_list);
$this->propal->fetchObjectLinked();
return $this->_cleanObjectDatas($this->propal);
}
@ -409,6 +409,86 @@ class Proposals extends DolibarrApi
}
}
/**
* Add a contact type of given commercial proposal
*
* @param int $id Id of commercial proposal 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->propal->creer) {
throw new RestException(401);
}
$result = $this->propal->fetch($id);
if(!$result) {
throw new RestException(404, 'Proposal not found');
}
if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
}
if(!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->propal->add_contact($contactid, $type, 'external');
if (!$result) {
throw new RestException(500, 'Error when added the contact');
}
return $this->propal;
}
/**
* Delete a contact type of given commercial proposal
*
* @param int $id Id of commercial proposal to update
* @param int $rowid Row key of the contact in the array contact_ids.
*
* @url DELETE {id}/contact/{lineid}
*
* @return int
* @throws 401
* @throws 404
* @throws 500
*/
function deleteContact($id, $rowid)
{
if(!DolibarrApiAccess::$user->rights->propal->creer) {
throw new RestException(401);
}
$result = $this->propal->fetch($id);
if(!$result) {
throw new RestException(404, 'Proposal not found');
}
if(!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->propal->delete_contact($rowid);
if (!$result) {
throw new RestException(500, 'Error when deleted the contact');
}
return $this->propal;
}
/**
* Update commercial proposal general fields (won't touch lines of commercial proposal)
*

View File

@ -56,12 +56,13 @@ class Orders extends DolibarrApi
*
* Return an array with order informations
*
* @param int $id ID of order
* @param int $id ID of order
* @param int $contact_list 0:Return array contains all properties, 1:Return array contains just id
* @return array|mixed data without useless information
*
* @throws RestException
*/
function get($id)
function get($id, $contact_list = 1)
{
if(! DolibarrApiAccess::$user->rights->commande->lire) {
throw new RestException(401);
@ -77,7 +78,7 @@ class Orders extends DolibarrApi
}
// Add external contacts ids
$this->commande->contacts_ids = $this->commande->liste_contact(-1, 'external', 1);
$this->commande->contacts_ids = $this->commande->liste_contact(-1, 'external', $contact_list);
$this->commande->fetchObjectLinked();
return $this->_cleanObjectDatas($this->commande);
}

View File

@ -55,12 +55,13 @@ class Invoices extends DolibarrApi
*
* Return an array with invoice informations
*
* @param int $id ID of invoice
* @param int $id ID of invoice
* @param int $contact_list 0:Return array contains all properties, 1:Return array contains just id
* @return array|mixed data without useless information
*
* @throws RestException
*/
function get($id)
function get($id, $contact_list = 1)
{
if(! DolibarrApiAccess::$user->rights->facture->lire) {
throw new RestException(401);
@ -82,7 +83,7 @@ class Invoices extends DolibarrApi
}
// Add external contacts ids
$this->invoice->contacts_ids = $this->invoice->liste_contact(-1, 'external', 1);
$this->invoice->contacts_ids = $this->invoice->liste_contact(-1, 'external', $contact_list);
$this->invoice->fetchObjectLinked();
return $this->_cleanObjectDatas($this->invoice);