diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 2559510dea8..b6d91b235ba 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,79 @@ 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 + * + * 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 diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 81e2659d918..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 @@ -59,15 +60,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 +72,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 +283,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 +733,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); + } } diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index 8b95e85f899..15670a2095d 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 @@ -71,38 +72,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 $email 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 +1663,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); + } }