NEW Set invoices as draft using the REST API

Adds the ability to set an invoice status as draft using the REST API
Change response when validating an invoice: before returned 200 + text message, now returns an invoice object
This commit is contained in:
Neil Orley 2017-10-02 10:55:34 +02:00
parent 989f285d32
commit 91a1d6c17e

View File

@ -378,7 +378,59 @@ class Invoices extends DolibarrApi
}
/**
* Validate an order
* Set invoice status as draft
*
* @param int $id Order ID
* @param int $idwarehouse Warehouse ID
*
* @url POST {id}/draft
*
* @return array
*
* @throws 200
* @throws 304
* @throws 401
* @throws 404
* @throws 500
*
*/
function draft($id, $idwarehouse=-1)
{
if(! DolibarrApiAccess::$user->rights->facture->creer) {
throw new RestException(401);
}
$result = $this->invoice->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Invoice not found');
}
if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->invoice->set_draft(DolibarrApiAccess::$user, $idwarehouse);
if ($result == 0) {
throw new RestException(304, 'Nothing done.');
}
if ($result < 0) {
throw new RestException(500, 'Error : '.$this->invoice->error);
}
$result = $this->invoice->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Invoice not found');
}
if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
return $this->_cleanObjectDatas($this->invoice);
}
/**
* Validate an invoice
*
* @param int $id Order ID
* @param int $idwarehouse Warehouse ID
@ -411,18 +463,25 @@ class Invoices extends DolibarrApi
$result = $this->invoice->validate(DolibarrApiAccess::$user, '', $idwarehouse, $notrigger);
if ($result == 0) {
throw new RestException(500, 'Error nothing done. May be object is already validated');
throw new RestException(304, 'Error nothing done. May be object is already validated');
}
if ($result < 0) {
throw new RestException(500, 'Error when validating Invoice: '.$this->invoice->error);
}
return array(
'success' => array(
'code' => 200,
'message' => 'Invoice validated (Ref='.$this->invoice->ref.')'
)
);
$result = $this->invoice->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Invoice not found');
}
if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
return $this->_cleanObjectDatas($this->invoice);
}
/**