From 6742700c04a927b9b66a65b0e47a3fd9c1ea6bf5 Mon Sep 17 00:00:00 2001 From: c3do Date: Sat, 19 Oct 2019 16:36:11 +0200 Subject: [PATCH 1/6] Can get product by ref, ref_ext, barcode through API Rest --- htdocs/product/class/api_products.class.php | 136 ++++++++++++++++---- 1 file changed, 108 insertions(+), 28 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 81e2659d918..d5b5cb77f7a 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -59,15 +59,11 @@ class Products extends DolibarrApi } /** - * Get properties of a product object (from its ID, Ref, Ref_ext or Barcode) + * Get properties of a product object by id * * Return an array with product information. - * TODO implement getting a product by ref or by $ref_ext * * @param int $id ID of product - * @param string $ref Ref of element - * @param string $ref_ext Ref ext of element - * @param string $barcode Barcode of element * @param int $includestockdata Load also information about stock (slower) * @return array|mixed Data without useless information * @@ -75,32 +71,72 @@ class Products extends DolibarrApi * @throws 403 * @throws 404 */ - public function get($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0) + public function get($id, $includestockdata = 0) { - if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) { - throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode'); - } + return $this->_fetch($id, '', '', '', $includestockdata); + } - $id = (empty($id)?0:$id); + /** + * Get properties of a product object by ref + * + * Return an array with product information. + * + * @param string $ref Ref of element + * @param int $includestockdata Load also information about stock (slower) + * + * @return array|mixed Data without useless information + * + * @url GET byRef/{ref} + * + * @throws 401 + * @throws 403 + * @throws 404 + */ + public function getByRef($ref, $includestockdata = 0) + { + return $this->_fetch('', $ref, '', '', $includestockdata); + } - if(! DolibarrApiAccess::$user->rights->produit->lire) { - throw new RestException(403); - } + /** + * Get properties of a product object by ref_ext + * + * Return an array with product information. + * + * @param string $ref_ext Ref_ext of element + * @param int $includestockdata Load also information about stock (slower) + * + * @return array|mixed Data without useless information + * + * @url GET byRefExt/{ref_ext} + * + * @throws 401 + * @throws 403 + * @throws 404 + */ + public function getByRefExt($ref_ext, $includestockdata = 0) + { + return $this->_fetch('', '', $ref_ext, '', $includestockdata); + } - $result = $this->product->fetch($id, $ref, $ref_ext, $barcode); - if(! $result ) { - throw new RestException(404, 'Product not found'); - } - - if(! DolibarrApi::_checkAccessToResource('product', $this->product->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - if ($includestockdata) { - $this->product->load_stock(); - } - - return $this->_cleanObjectDatas($this->product); + /** + * Get properties of a product object by barcode + * + * Return an array with product information. + * + * @param string $barcode Barcode of element + * @param int $includestockdata Load also information about stock (slower) + * + * @return array|mixed Data without useless information + * + * @url GET byBarcode/{barcode} + * + * @throws 401 + * @throws 403 + * @throws 404 + */ + public function getByBarcode($barcode, $includestockdata = 0) + { + return $this->_fetch('', '', '', $barcode, $includestockdata); } /** @@ -246,7 +282,7 @@ class Products extends DolibarrApi } $this->product->$field = $value; } - + $updatetype = false; if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) { $updatetype = true; @@ -696,4 +732,48 @@ class Products extends DolibarrApi } return $product; } + + /** + * Get properties of a product object + * + * Return an array with product information. + * + * @param int $id ID of product + * @param string $ref Ref of element + * @param string $ref_ext Ref ext of element + * @param string $barcode Barcode of element + * @param int $includestockdata Load also information about stock (slower) + * @return array|mixed Data without useless information + * + * @throws 401 + * @throws 403 + * @throws 404 + */ + private function _fetch($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0) + { + if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) { + throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode'); + } + + $id = (empty($id)?0:$id); + + if(! DolibarrApiAccess::$user->rights->produit->lire) { + throw new RestException(403); + } + + $result = $this->product->fetch($id, $ref, $ref_ext, $barcode); + if(! $result ) { + throw new RestException(404, 'Product not found'); + } + + if(! DolibarrApi::_checkAccessToResource('product', $this->product->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + if ($includestockdata) { + $this->product->load_stock(); + } + + return $this->_cleanObjectDatas($this->product); + } } From bb70db82c29f76a2ddecbad50504c9a616dfa3a5 Mon Sep 17 00:00:00 2001 From: c3do Date: Sat, 19 Oct 2019 16:43:10 +0200 Subject: [PATCH 2/6] Can get thirdparty by email through API Rest --- .../societe/class/api_thirdparties.class.php | 100 ++++++++++++------ 1 file changed, 70 insertions(+), 30 deletions(-) diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index 8b95e85f899..617c6ad1f2a 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -71,38 +71,26 @@ class Thirdparties extends DolibarrApi * * @throws RestException */ - public function get($id) + public function get($id) { - if(! DolibarrApiAccess::$user->rights->societe->lire) { - throw new RestException(401); - } - if ($id ==0) { - $result = $this->company->initAsSpecimen(); - } else { - $result = $this->company->fetch($id); - } - if( ! $result ) { - throw new RestException(404, 'Thirdparty not found'); - } + return $this->_fetch($id); + } - if( ! DolibarrApi::_checkAccessToResource('societe', $this->company->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) { - $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice - $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice - } else { - $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')"; - $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')"; - } - - $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount); - $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote); - $this->company->absolute_discount = price2num($absolute_discount, 'MT'); - $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT'); - - return $this->_cleanObjectDatas($this->company); + /** + * Get properties of a thirdparty object by email. + * + * Return an array with thirdparty informations + * + * @param string $sortfield Sort field + * @return array|mixed data without useless information + * + * @url GET byEmail/{email} + * + * @throws RestException + */ + public function getByEmail($email) + { + return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email); } /** @@ -1674,4 +1662,56 @@ class Thirdparties extends DolibarrApi } return $thirdparty; } + + /** + * Fetch properties of a thirdparty object. + * + * Return an array with thirdparty informations + * + * @param int $rowid Id of third party to load + * @param string $ref Reference of third party, name (Warning, this can return several records) + * @param string $ref_ext External reference of third party (Warning, this information is a free field not provided by Dolibarr) + * @param string $ref_int Internal reference of third party (not used by dolibarr) + * @param string $idprof1 Prof id 1 of third party (Warning, this can return several records) + * @param string $idprof2 Prof id 2 of third party (Warning, this can return several records) + * @param string $idprof3 Prof id 3 of third party (Warning, this can return several records) + * @param string $idprof4 Prof id 4 of third party (Warning, this can return several records) + * @param string $idprof5 Prof id 5 of third party (Warning, this can return several records) + * @param string $idprof6 Prof id 6 of third party (Warning, this can return several records) + * @param string $email Email of third party (Warning, this can return several records) + * @param string $ref_alias Name_alias of third party (Warning, this can return several records) + * @return array|mixed data without useless information + * + * @throws RestException + */ + private function _fetch($rowid, $ref = '', $ref_ext = '', $ref_int = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '') + { + if(! DolibarrApiAccess::$user->rights->societe->lire) { + throw new RestException(401); + } + + $result = $this->company->fetch($rowid, $ref, $ref_ext, $ref_int, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias); + if( ! $result ) { + throw new RestException(404, 'Thirdparty not found'); + } + + if( ! DolibarrApi::_checkAccessToResource('societe', $this->company->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) { + $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice + $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice + } else { + $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')"; + $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')"; + } + + $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount); + $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote); + $this->company->absolute_discount = price2num($absolute_discount, 'MT'); + $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT'); + + return $this->_cleanObjectDatas($this->company); + } } From ae3ce37a57c3f65a5400c396cc3196b90fd22ea9 Mon Sep 17 00:00:00 2001 From: c3do Date: Sat, 19 Oct 2019 16:45:30 +0200 Subject: [PATCH 3/6] copyright mentions --- htdocs/product/class/api_products.class.php | 1 + htdocs/societe/class/api_thirdparties.class.php | 1 + 2 files changed, 2 insertions(+) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index d5b5cb77f7a..d6066b1a03a 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -1,5 +1,6 @@ + * Copyright (C) 2019 Cedric Ancelin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index 617c6ad1f2a..982f64e44f0 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -1,6 +1,7 @@ * Copyright (C) 2018 Pierre Chéné + * Copyright (C) 2019 Cedric Ancelin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 29bad6c7e8dc12758b5e1a446b0b6b6334059487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Sat, 19 Oct 2019 16:55:12 +0200 Subject: [PATCH 4/6] Replace $sortfield by $email in annotation --- htdocs/societe/class/api_thirdparties.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index 982f64e44f0..15670a2095d 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -82,7 +82,7 @@ class Thirdparties extends DolibarrApi * * Return an array with thirdparty informations * - * @param string $sortfield Sort field + * @param string $email Sort field * @return array|mixed data without useless information * * @url GET byEmail/{email} From 4292d72ea3b008805dc6dfcb353e4ed8ccb47f31 Mon Sep 17 00:00:00 2001 From: c3do Date: Sat, 19 Oct 2019 23:42:55 +0200 Subject: [PATCH 5/6] Can get order by ref, ref_ext, ref_int --- htdocs/commande/class/api_orders.class.php | 101 ++++++++++++++++++--- 1 file changed, 86 insertions(+), 15 deletions(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index fb044b1fafa..40732fbd045 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -52,7 +52,7 @@ class Orders extends DolibarrApi } /** - * Get properties of an order object + * Get properties of an order object by id * * Return an array with order informations * @@ -64,26 +64,97 @@ class Orders extends DolibarrApi */ public function get($id, $contact_list = 1) { - if(! DolibarrApiAccess::$user->rights->commande->lire) { - throw new RestException(401); - } + return $this->_fetch($id, '', '', '', $contact_list); + } - $result = $this->commande->fetch($id); + /** + * Get properties of an order object by ref + * + * Return an array with order informations + * + * @param string $ref Ref of object + * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id + * @return array|mixed data without useless information + * + * @url GET byRef/{ref} + * + * @throws RestException + */ + public function getByRef($ref, $contact_list = 1) + { + return $this->_fetch('', $ref, '', '', $contact_list); + } + + /** + * Get properties of an order object by ref_ext + * + * Return an array with order informations + * + * @param string $ref_ext External reference of object + * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id + * @return array|mixed data without useless information + * + * @url GET byRefExt/{ref_ext} + * + * @throws RestException + */ + public function getByRefExt($ref_ext, $contact_list = 1) + { + return $this->_fetch('', '', $ref_ext, '', $contact_list); + } + + /** + * Get properties of an order object by ref_int + * + * Return an array with order informations + * + * @param string $ref_int Internal reference of object + * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id + * @return array|mixed data without useless information + * + * @url GET byRefInt/{ref_int} + * + * @throws RestException + */ + public function getByRefInt($ref_int, $contact_list = 1) + { + return $this->_fetch('', '', '', $ref_int, $contact_list); + } + + /** + * Get properties of an order object + * + * Return an array with order informations + * + * @param int $id ID of order + * @param string $ref Ref of object + * @param string $ref_ext External reference of object + * @param string $ref_int Internal reference of other objec + * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id + * @return array|mixed data without useless information + * + * @throws RestException + */ + private function _fetch($id, $ref = '', $ref_ext = '', $ref_int = '', $contact_list = 1) + { + if(! DolibarrApiAccess::$user->rights->commande->lire) { + throw new RestException(401); + } + + $result = $this->commande->fetch($id, $ref, $ref_ext, $ref_int); 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); - } - - // Add external contacts ids - $this->commande->contacts_ids = $this->commande->liste_contact(-1, 'external', $contact_list); - $this->commande->fetchObjectLinked(); - return $this->_cleanObjectDatas($this->commande); - } - + if( ! DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + // Add external contacts ids + $this->commande->contacts_ids = $this->commande->liste_contact(-1, 'external', $contact_list); + $this->commande->fetchObjectLinked(); + return $this->_cleanObjectDatas($this->commande); + } /** * List orders From 54b78863319cda11b4f556b0e65809e569725802 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 20 Oct 2019 13:40:18 +0200 Subject: [PATCH 6/6] Update api_orders.class.php --- htdocs/commande/class/api_orders.class.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 40732fbd045..5dddf4d0f6f 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -103,24 +103,6 @@ class Orders extends DolibarrApi return $this->_fetch('', '', $ref_ext, '', $contact_list); } - /** - * Get properties of an order object by ref_int - * - * Return an array with order informations - * - * @param string $ref_int Internal reference of object - * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id - * @return array|mixed data without useless information - * - * @url GET byRefInt/{ref_int} - * - * @throws RestException - */ - public function getByRefInt($ref_int, $contact_list = 1) - { - return $this->_fetch('', '', '', $ref_int, $contact_list); - } - /** * Get properties of an order object *