From b8cb080f0d5d46a5ba5d74f22cddd741e3197e25 Mon Sep 17 00:00:00 2001 From: Pierre Penelon Date: Thu, 22 Oct 2020 09:26:46 +0200 Subject: [PATCH] ADD Retrieve discount from invoice from API As I was unable to retrieve discount ID of created discount from invoice with API. This commit add a route to get discount from a specific invoice ID. --- .../facture/class/api_invoices.class.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 0ab4974ce7f..07c8a1569c0 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -981,6 +981,45 @@ class Invoices extends DolibarrApi return $this->_cleanObjectDatas($this->invoice); } + /** + * Get discount from invoice + * + * @param int $id Id of invoice + * + * @url GET {id}/discount + * + * @return mixed + */ + public function getDiscount($id) + { + require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php'; + + if (!DolibarrApiAccess::$user->rights->facture->lire) { + 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); + } + + $discountcheck = new DiscountAbsolute($this->db); + $result = $discountcheck->fetch(0, $this->invoice->id); + + if($result == 0){ + throw new RestException(404, 'Discount not found'); + } + else if($result < 0){ + throw new RestException(500, $discountcheck->error); + } + + return parent::_cleanObjectDatas($discountcheck); + } + /** * Create a discount (credit available) for a credit note or a deposit. *