From 91a1d6c17ee2b7f6298116943a8e174d133c4700 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Mon, 2 Oct 2017 10:55:34 +0200 Subject: [PATCH 1/3] 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 --- .../facture/class/api_invoices.class.php | 75 +++++++++++++++++-- 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index e28d19b95b2..539f44900a4 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -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); + + } /** From 5b91a6a37b6f8ce64dd67598f1c88bb8dafe3ca3 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Mon, 2 Oct 2017 11:17:46 +0200 Subject: [PATCH 2/3] NEW Sets an invoice as paid using the REST API Adds the ability to set an invoice as paid using the REST API --- .../facture/class/api_invoices.class.php | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 539f44900a4..a39eba1eca6 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -378,7 +378,7 @@ class Invoices extends DolibarrApi } /** - * Set invoice status as draft + * Sets an invoice as draft * * @param int $id Order ID * @param int $idwarehouse Warehouse ID @@ -484,6 +484,61 @@ class Invoices extends DolibarrApi } + /** + * Sets an invoice as paid + * + * @param int $id Order ID + * @param string $close_code Code renseigne si on classe a payee completement alors que paiement incomplet (cas escompte par exemple) + * @param string $close_note Commentaire renseigne si on classe a payee alors que paiement incomplet (cas escompte par exemple) + * + * @url POST {id}/paid + * + * @return array An invoice object + * + * @throws 200 + * @throws 304 + * @throws 401 + * @throws 404 + * @throws 500 + */ + function set_paid($id, $close_code='', $close_note='') + { + 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_paid(DolibarrApiAccess::$user, $close_code, $close_note); + if ($result == 0) { + throw new RestException(304, 'Error nothing done. May be object is already validated'); + } + 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); + + + } + + /** * Clean sensible object datas * From 503129c9f4d7f3efa8c0826c4eaa6c35f85f3eee Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Mon, 2 Oct 2017 11:51:54 +0200 Subject: [PATCH 3/3] Rename method draft and paid to settodraft and settopaid --- htdocs/compta/facture/class/api_invoices.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index a39eba1eca6..a1f76807ab6 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -383,7 +383,7 @@ class Invoices extends DolibarrApi * @param int $id Order ID * @param int $idwarehouse Warehouse ID * - * @url POST {id}/draft + * @url POST {id}/settodraft * * @return array * @@ -394,7 +394,7 @@ class Invoices extends DolibarrApi * @throws 500 * */ - function draft($id, $idwarehouse=-1) + function settodraft($id, $idwarehouse=-1) { if(! DolibarrApiAccess::$user->rights->facture->creer) { throw new RestException(401); @@ -491,7 +491,7 @@ class Invoices extends DolibarrApi * @param string $close_code Code renseigne si on classe a payee completement alors que paiement incomplet (cas escompte par exemple) * @param string $close_note Commentaire renseigne si on classe a payee alors que paiement incomplet (cas escompte par exemple) * - * @url POST {id}/paid + * @url POST {id}/settopaid * * @return array An invoice object * @@ -501,7 +501,7 @@ class Invoices extends DolibarrApi * @throws 404 * @throws 500 */ - function set_paid($id, $close_code='', $close_note='') + function settopaid($id, $close_code='', $close_note='') { if(! DolibarrApiAccess::$user->rights->facture->creer) { throw new RestException(401);