From bd0079b3f1d37ae5568d96515415eb05f59718ec Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Mon, 23 Oct 2017 16:45:49 +0200 Subject: [PATCH 1/6] NEW Retrieve available discounts and payments details from an invoice Adds the ability to get availbales discounts and payments details of a specific invoice unsing the REST API. --- .../facture/class/api_invoices.class.php | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 344a5d280c7..475d3736741 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -19,6 +19,7 @@ use Luracast\Restler\RestException; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; + /** * API class for invoices * @@ -71,6 +72,25 @@ class Invoices extends DolibarrApi throw new RestException(404, 'Invoice not found'); } + // Get payment details + $this->invoice->totalpaye = $this->invoice->getSommePaiement(); + $this->invoice->totalcreditnotes = $this->invoice->getSumCreditNotesUsed(); + $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed(); + $this->invoice->resteapayer = price2num($this->invoice->total_ttc - $this->invoice->totalpaye - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits, 'MT'); + + // get available discounts + $soc = new Societe($this->db); + if ($this->invoice->socid > 0) + $res = $soc->fetch($this->invoice->socid); + if($res) { + $filterabsolutediscount = "fk_facture_source IS NULL OR (fk_facture_source IS NOT NULL AND (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 = $soc->getAvailableDiscounts('', $filterabsolutediscount); + $absolute_creditnote = $soc->getAvailableDiscounts('', $filtercreditnote); + $this->invoice->absolute_discount = price2num($absolute_discount, 'MT'); + $this->invoice->absolute_creditnote = price2num($absolute_creditnote, 'MT'); + } + if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } @@ -421,7 +441,7 @@ class Invoices extends DolibarrApi * @param int $id Id of invoice * @param array $request_data InvoiceLine data * - * @url POST {id}/lines + * @url POST {id}/addline * * @return int */ From 2a9ddf74377518e0063daf50f62d019005abc2a9 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Mon, 23 Oct 2017 16:49:00 +0200 Subject: [PATCH 2/6] Remove whitespace --- htdocs/compta/facture/class/api_invoices.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 475d3736741..f3bb3c6480e 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -19,7 +19,6 @@ use Luracast\Restler\RestException; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; - /** * API class for invoices * From 0a43a124202f69fc3bcbe963b0589d7682abdea9 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Tue, 24 Oct 2017 10:50:38 +0200 Subject: [PATCH 3/6] NEW Get a payment list of a given invoice using the REST API Add the getpayments method to retrieve a list of payments of a given invoice --- .../facture/class/api_invoices.class.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index f3bb3c6480e..ae2ab7a94ea 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -665,6 +665,47 @@ class Invoices extends DolibarrApi } + /** + * Get a payment list of a given invoice + * + * @param int $id Id of invoice + * + * @url GET {id}/getpayments + * + * @return array + * @throws 400 + * @throws 401 + * @throws 404 + * @throws 405 + */ + function getPayments($id) { + + if(! DolibarrApiAccess::$user->rights->facture->creer) { + throw new RestException(401); + } + if(empty($id)) { + throw new RestException(400, 'Invoice ID is mandatory'); + } + + if( ! DolibarrApi::_checkAccessToResource('facture',$id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->invoice->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Invoice not found'); + } + + $result = $this->invoice->getListOfPayments(); + if( $result < 0) { + throw new RestException(405, $this->invoice->error); + } + + return $result; + } + + + /** * Clean sensible object datas * From 02c8f7f905455fd997181b6f174bdb1db9a2099c Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Tue, 24 Oct 2017 11:25:31 +0200 Subject: [PATCH 4/6] Modify rights check in the getPayments method --- htdocs/compta/facture/class/api_invoices.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index ae2ab7a94ea..1dfaea3d580 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -680,7 +680,7 @@ class Invoices extends DolibarrApi */ function getPayments($id) { - if(! DolibarrApiAccess::$user->rights->facture->creer) { + if(! DolibarrApiAccess::$user->rights->facture->lire) { throw new RestException(401); } if(empty($id)) { From b6825703b47d7897d78b4a99456e5ab75d181cac Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 24 Oct 2017 12:47:15 +0200 Subject: [PATCH 5/6] Update api_invoices.class.php --- htdocs/compta/facture/class/api_invoices.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 1dfaea3d580..52ef2a90d1b 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -77,7 +77,8 @@ class Invoices extends DolibarrApi $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed(); $this->invoice->resteapayer = price2num($this->invoice->total_ttc - $this->invoice->totalpaye - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits, 'MT'); - // get available discounts + // get available discounts of customer + /* TODO Move this into thirdparty API $soc = new Societe($this->db); if ($this->invoice->socid > 0) $res = $soc->fetch($this->invoice->socid); @@ -89,6 +90,7 @@ class Invoices extends DolibarrApi $this->invoice->absolute_discount = price2num($absolute_discount, 'MT'); $this->invoice->absolute_creditnote = price2num($absolute_creditnote, 'MT'); } + */ if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); From 8592e1b1e8043ba6468301545c696107e589574c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 24 Oct 2017 12:49:37 +0200 Subject: [PATCH 6/6] Update api_invoices.class.php --- htdocs/compta/facture/class/api_invoices.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 52ef2a90d1b..28157085270 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -442,7 +442,7 @@ class Invoices extends DolibarrApi * @param int $id Id of invoice * @param array $request_data InvoiceLine data * - * @url POST {id}/addline + * @url POST {id}/lines * * @return int */