From 73fe991aedd84b49953efe48b2b92983417dac1e Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Fri, 10 Nov 2017 15:46:53 +0100 Subject: [PATCH 1/6] NEW Return the order object when validate Return the order object when the order is validated using the REST API --- htdocs/commande/class/api_orders.class.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index a6788f95698..298333e1fb8 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -492,13 +492,17 @@ class Orders extends DolibarrApi if ($result < 0) { throw new RestException(500, 'Error when validating Order: '.$this->commande->error); } + $result = $this->commande->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Order not found'); + } - return array( - 'success' => array( - 'code' => 200, - 'message' => 'Order validated (Ref='.$this->commande->ref.')' - ) - ); + if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $this->commande->fetchObjectLinked(); + return $this->_cleanObjectDatas($this->commande); } /** From 182fe4a90f99fbcc664f2243b5d7f3636e8cd94b Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Fri, 10 Nov 2017 15:48:43 +0100 Subject: [PATCH 2/6] Change HTTP response code when nothing modified --- htdocs/commande/class/api_orders.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 298333e1fb8..60f1be1e00c 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -490,7 +490,7 @@ class Orders extends DolibarrApi throw new RestException(500, 'Error nothing done. May be object is already validated'); } if ($result < 0) { - throw new RestException(500, 'Error when validating Order: '.$this->commande->error); + throw new RestException(304, 'Error when validating Order: '.$this->commande->error); } $result = $this->commande->fetch($id); if( ! $result ) { From c68f717071cba21726653ac9cee4a6ff7df33a1a Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Fri, 10 Nov 2017 16:22:20 +0100 Subject: [PATCH 3/6] NEW Update availability Add the ability to update availability while updating an invoice using the REST API --- htdocs/commande/class/api_orders.class.php | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 60f1be1e00c..086379a3989 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -413,6 +413,12 @@ class Orders extends DolibarrApi $this->commande->$field = $value; } + // Update availability + if( isset($this->commande->availability_id) && !empty($this->commande->availability_id) ) { + if($this->commande->availability($this->commande->availability_id) < 0) + throw new RestException(400, 'Error while updating availability'); + } + if($this->commande->update($id, DolibarrApiAccess::$user, 1, '', '', 'update')) return $this->get($id); @@ -545,6 +551,50 @@ class Orders extends DolibarrApi ); } + /** + * Set an order to draft + * + * @param int $id Order ID + * + * @url POST {id}/settodraft + * + * @return array + */ + function settodraft($id) + { + if(! DolibarrApiAccess::$user->rights->commande->creer) { + throw new RestException(401); + } + $result = $this->commande->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Order not found'); + } + + if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->commande->set_draft(DolibarrApiAccess::$user); + if ($result == 0) { + throw new RestException(304, 'Nothing done. May be object is already closed'); + } + if ($result < 0) { + throw new RestException(500, 'Error when closing Order: '.$this->commande->error); + } + + $result = $this->commande->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Order not found'); + } + + if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $this->commande->fetchObjectLinked(); + return $this->_cleanObjectDatas($this->commande); + } + /** * Clean sensible object datas From 4dfb0a8f1bf04248536acf77b2a92f8cee98535a Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Fri, 10 Nov 2017 16:42:06 +0100 Subject: [PATCH 4/6] NEW add the ability to regenerate an pdf for the order module add the ability to regenerate an pdf for the order module --- htdocs/api/class/api_documents.class.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/htdocs/api/class/api_documents.class.php b/htdocs/api/class/api_documents.class.php index da97215dc0c..669fc09911e 100644 --- a/htdocs/api/class/api_documents.class.php +++ b/htdocs/api/class/api_documents.class.php @@ -119,6 +119,20 @@ class Documents extends DolibarrApi throw new RestException(500, 'Error generating document'); } } + if ($module_part == 'commande' || $module_part == 'order') + { + require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; + $this->order = new Commande($this->db); + $result = $this->order->fetch(0, preg_replace('/\.[^\.]+$/', '', basename($original_file))); + if( ! $result ) { + throw new RestException(404, 'Order not found'); + } + $result = $this->order->generateDocument($this->order->modelpdf, $langs, $hidedetails, $hidedesc, $hideref); + if( $result <= 0 ) { + throw new RestException(500, 'Error generating document'); + } + } + } $filename = basename($original_file); From bf05a0b8e005353774318c33f86b59c9754a4144 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 14 Nov 2017 01:19:04 +0100 Subject: [PATCH 5/6] Update api_orders.class.php --- htdocs/commande/class/api_orders.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 086379a3989..7f2a5b03ae4 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -396,16 +396,16 @@ class Orders extends DolibarrApi * @return int */ function put($id, $request_data = NULL) { - if(! DolibarrApiAccess::$user->rights->commande->creer) { + if (! DolibarrApiAccess::$user->rights->commande->creer) { throw new RestException(401); } $result = $this->commande->fetch($id); - if( ! $result ) { + if (! $result) { throw new RestException(404, 'Order not found'); } - if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { + if (! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } foreach($request_data as $field => $value) { @@ -414,12 +414,12 @@ class Orders extends DolibarrApi } // Update availability - if( isset($this->commande->availability_id) && !empty($this->commande->availability_id) ) { - if($this->commande->availability($this->commande->availability_id) < 0) + if (!empty($this->commande->availability_id)) { + if ($this->commande->availability($this->commande->availability_id) < 0) throw new RestException(400, 'Error while updating availability'); } - if($this->commande->update($id, DolibarrApiAccess::$user, 1, '', '', 'update')) + if ($this->commande->update($id, DolibarrApiAccess::$user, 1, '', '', 'update')) return $this->get($id); return false; From 4212000cafde2965b61a39f1061c5088d23b7618 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 14 Nov 2017 01:21:06 +0100 Subject: [PATCH 6/6] Update api_orders.class.php --- htdocs/commande/class/api_orders.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 7f2a5b03ae4..64958b01ef6 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -493,10 +493,10 @@ class Orders extends DolibarrApi $result = $this->commande->valid(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(304, 'Error when validating Order: '.$this->commande->error); + throw new RestException(500, 'Error when validating Order: '.$this->commande->error); } $result = $this->commande->fetch($id); if( ! $result ) {