From 41104de7ce7ab76468325012fd6115f56ed996b5 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Tue, 24 Oct 2017 11:47:25 +0200 Subject: [PATCH 01/10] NEW Get available assets of an invoice using the REST API Returns a list of paid down-payments, available avoirs, etc. with the payments detail --- .../facture/class/api_invoices.class.php | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index ae2ab7a94ea..173468b5630 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)) { @@ -704,7 +704,71 @@ class Invoices extends DolibarrApi return $result; } + /** + * Get a list of available assets (down-payments, assets) + * + * @param int $id Id of the invoice + * + * @url GET {id}/getavailableassets + * + * @return array + * @throws 400 + * @throws 401 + * @throws 404 + * @throws 405 + */ + function getavailableassets($id) { + if(! DolibarrApiAccess::$user->rights->facture->lire) { + 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->list_qualified_avoir_invoices($this->invoice->socid); + if( $result < 0) { + throw new RestException(405, $this->invoice->error); + } + + $return = array(); + foreach ($result as $invoiceID=>$line) { + + if($invoiceID == $id) { // ignore current invoice + continue; + } + + $result = $this->invoice->fetch($invoiceID); + if( ! $result ) { + throw new RestException(404, 'Invoice '.$invoiceID.' not found'); + } + + $result = $this->invoice->getListOfPayments(); + if( $result < 0) { + throw new RestException(405, $this->invoice->error); + } + + if(count($result) == 0) { // ignore unpaid invoices + continue; + } + + // format results + $line["id"] = $invoiceID; + $line["payments"] = $result; + $return[] = $line; + } + + return $return; + } /** * Clean sensible object datas From c34a6db13ae606baecd8570a2aad619622e2adce Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Tue, 24 Oct 2017 11:53:39 +0200 Subject: [PATCH 02/10] Rename assets with avoirs --- 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 173468b5630..b83903d55e1 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -705,7 +705,7 @@ class Invoices extends DolibarrApi } /** - * Get a list of available assets (down-payments, assets) + * Get a list of available assets (down-payments, avoirs) * * @param int $id Id of the invoice * From 327f31611cbf889a01d1fe4f3b4d24159ed8a4da Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Tue, 24 Oct 2017 14:21:22 +0200 Subject: [PATCH 03/10] FIX Data related to customer moved to thirparties api Related to commit : https://github.com/Dolibarr/dolibarr/pull/7691/commits/b6825703b47d7897d78b4a99456e5ab75d181cac --- htdocs/societe/class/api_thirdparties.class.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index dd743984044..6ed404e28cd 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -83,6 +83,13 @@ class Thirdparties extends DolibarrApi throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } + $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 = $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 b4c484cf0ba370cf59158e9d8a1d692e7b33a0ce Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Tue, 24 Oct 2017 14:29:28 +0200 Subject: [PATCH 04/10] NEW Get available assets of an invoice using the REST API Returns a list of paid down-payments, available avoirs, etc. with the payments detail --- .../facture/class/api_invoices.class.php | 80 +++++++++++++++---- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 28157085270..d8dd5dbeb1a 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -77,21 +77,6 @@ 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 of customer - /* TODO Move this into thirdparty API - $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); } @@ -707,6 +692,71 @@ class Invoices extends DolibarrApi } + /** + * Get a list of available assets (down-payments, avoirs) + * + * @param int $id Id of the invoice + * + * @url GET {id}/getavailableassets + * + * @return array + * @throws 400 + * @throws 401 + * @throws 404 + * @throws 405 + */ + function getavailableassets($id) { + + if(! DolibarrApiAccess::$user->rights->facture->lire) { + 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->list_qualified_avoir_invoices($this->invoice->socid); + if( $result < 0) { + throw new RestException(405, $this->invoice->error); + } + + $return = array(); + foreach ($result as $invoiceID=>$line) { + + if($invoiceID == $id) { // ignore current invoice + continue; + } + + $result = $this->invoice->fetch($invoiceID); + if( ! $result ) { + throw new RestException(404, 'Invoice '.$invoiceID.' not found'); + } + + $result = $this->invoice->getListOfPayments(); + if( $result < 0) { + throw new RestException(405, $this->invoice->error); + } + + if(count($result) == 0) { // ignore unpaid invoices + continue; + } + + // format results + $line["id"] = $invoiceID; + $line["payments"] = $result; + $return[] = $line; + } + + return $return; + } /** * Clean sensible object datas From cb71e921fd3be4dea758f7eb066c474bda381f35 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Wed, 25 Oct 2017 11:39:23 +0200 Subject: [PATCH 05/10] NEW Get credit notes or deposits of a thirdparty Ability to filter : available or applied Returns a list of credit notes or deposits --- .../societe/class/api_thirdparties.class.php | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index dd743984044..c7b6d882514 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -331,6 +331,68 @@ class Thirdparties extends DolibarrApi return $this->company; } + + /** + * Get exceptional discount of a thirdparty + * + * @param int $id ID of the thirdparty + * @param string $filter Filter exceptional discount. "none" will return every discount, "available" returns unapplied discounts, "used" returns applied discounts {@choice none,available,used} + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * + * @url GET {id}/exceptionaldiscounts + * + * @return array List of deposit and credit notes + * + * @throws 400 + * @throws 401 + * @throws 404 + * @throws 503 + */ + function getExceptionalDiscount($id,$filter="none",$sortfield = "f.type", $sortorder = 'ASC') + { + if(! DolibarrApiAccess::$user->rights->societe->lire) { + throw new RestException(401); + } + + if(empty($id)) { + throw new RestException(400, 'Thirdparty ID is mandatory'); + } + + if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->company->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Thirdparty not found'); + } + + + $sql = "SELECT f.facnumber, f.type as factype, re.fk_facture_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_facture"; + $sql .= " FROM llx_societe_remise_except as re, llx_facture as f"; + $sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".$id; + if ($filter == "available") $sql .= " AND re.fk_facture IS NULL"; + if ($filter == "used") $sql .= " AND re.fk_facture IS NOT NULL"; + + $sql.= $this->db->order($sortfield, $sortorder); + + $result = $this->db->query($sql); + if( ! $result ) { + throw new RestException(503, $this->db->lasterror()); + } else { + $num = $this->db->num_rows($result); + while ( $obj = $this->db->fetch_object($result) ) { + $obj_ret[] = $obj; + } + + } + + return $obj_ret; + } + + + /** * Clean sensible object datas * From 0f98572c2b614747d9931fd4fe057326c85dff81 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Wed, 25 Oct 2017 14:04:36 +0200 Subject: [PATCH 06/10] FIX Filter didn't work for credit note Modify the SQL request to be able to filter on credit note --- htdocs/societe/class/api_thirdparties.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index c7b6d882514..9adc76462f5 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -369,11 +369,11 @@ class Thirdparties extends DolibarrApi } - $sql = "SELECT f.facnumber, f.type as factype, re.fk_facture_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_facture"; + $sql = "SELECT f.facnumber, f.type as factype, re.fk_facture_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_facture, re.fk_facture_line"; $sql .= " FROM llx_societe_remise_except as re, llx_facture as f"; $sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".$id; - if ($filter == "available") $sql .= " AND re.fk_facture IS NULL"; - if ($filter == "used") $sql .= " AND re.fk_facture IS NOT NULL"; + if ($filter == "available") $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL"; + if ($filter == "used") $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)"; $sql.= $this->db->order($sortfield, $sortorder); From 257acb6679d7ae7ac3a1482f9e77ca3d91ca9176 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Wed, 25 Oct 2017 14:41:12 +0200 Subject: [PATCH 07/10] NEW Insert a discount in a specific invoice using the REST API Add ability to insert a discount in a specific invoice. --- .../facture/class/api_invoices.class.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 9738cec55c1..54f47dc87db 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -667,6 +667,49 @@ class Invoices extends DolibarrApi } + /** + * Insert a discount in a specific invoice + * + * @param int $id Id of invoice + * @param int $discountId Id of discount + * + * @url POST {id}/adddiscount/{discountId} + * + * @return int + * @throws 400 + * @throws 401 + * @throws 404 + * @throws 405 + */ + function addDiscount($id, $discountId) { + + if(! DolibarrApiAccess::$user->rights->facture->creer) { + throw new RestException(401); + } + if(empty($id)) { + throw new RestException(400, 'Invoice ID is mandatory'); + } + if(empty($discountId)) { + throw new RestException(400, 'Discount 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->insert_discount($discountId); + if( $result < 0) { + throw new RestException(405, $this->invoice->error); + } + + return $result; + } + /** * Get a payment list of a given invoice * From da0a205a10231eef89aa6015cb038bfc17dc968d Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Wed, 25 Oct 2017 17:09:37 +0200 Subject: [PATCH 08/10] NEW Deduct an available credit to an existing invoice Deduct an available credit to an existing invoice using the REST API --- .../facture/class/api_invoices.class.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 0640f1fdf4f..40b0eb5741e 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -695,6 +695,51 @@ class Invoices extends DolibarrApi return $result; } + /** + * Deduct an available credit to an existing invoice + * + * @param int $id Id of invoice + * @param int $creditId Id of the credit to deduct + * + * @url POST {id}/deductcredit/{creditId} + * + * @return int + * @throws 400 + * @throws 401 + * @throws 404 + * @throws 405 + */ + function deductCredit($id, $creditId) { + + require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php'; + + if(! DolibarrApiAccess::$user->rights->facture->creer) { + throw new RestException(401); + } + if(empty($id)) { + throw new RestException(400, 'Invoice ID is mandatory'); + } + if(empty($creditId)) { + throw new RestException(400, 'Credit ID is mandatory'); + } + + if( ! DolibarrApi::_checkAccessToResource('facture',$id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + $discount = new DiscountAbsolute($this->db); + $result = $discount->fetch($creditId); + if( ! $result ) { + throw new RestException(404, 'Credit not found'); + } + + $result = $discount->link_to_invoice(0, $id); + if( $result < 0) { + throw new RestException(405, $discount->error); + } + + return $result; + } + /** * Get a payment list of a given invoice * From f4a06aef3f12f5c392b55ccde93a75118ef9426a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 27 Oct 2017 00:36:06 +0200 Subject: [PATCH 09/10] Update api_invoices.class.php --- .../facture/class/api_invoices.class.php | 90 +++---------------- 1 file changed, 12 insertions(+), 78 deletions(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 40b0eb5741e..cc5db388af7 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -653,12 +653,12 @@ class Invoices extends DolibarrApi /** - * Insert a discount in a specific invoice + * Add a discount line into an invoice (as an invoice line) using an existing absolute discount (Consume the discount) * * @param int $id Id of invoice - * @param int $discountId Id of discount + * @param int $discountid Id of discount * - * @url POST {id}/adddiscount/{discountId} + * @url POST {id}/usediscount/{discountid} * * @return int * @throws 400 @@ -666,7 +666,7 @@ class Invoices extends DolibarrApi * @throws 404 * @throws 405 */ - function addDiscount($id, $discountId) { + function useDiscount($id, $discountid) { if(! DolibarrApiAccess::$user->rights->facture->creer) { throw new RestException(401); @@ -674,7 +674,7 @@ class Invoices extends DolibarrApi if(empty($id)) { throw new RestException(400, 'Invoice ID is mandatory'); } - if(empty($discountId)) { + if(empty($discountid)) { throw new RestException(400, 'Discount ID is mandatory'); } @@ -687,7 +687,7 @@ class Invoices extends DolibarrApi throw new RestException(404, 'Invoice not found'); } - $result = $this->invoice->insert_discount($discountId); + $result = $this->invoice->insert_discount($discountid); if( $result < 0) { throw new RestException(405, $this->invoice->error); } @@ -696,12 +696,12 @@ class Invoices extends DolibarrApi } /** - * Deduct an available credit to an existing invoice + * Add an available credit note discount to payments of an existing invoice (Consume the credit note) * * @param int $id Id of invoice - * @param int $creditId Id of the credit to deduct + * @param int $discountid Id of a discount coming from a credit note * - * @url POST {id}/deductcredit/{creditId} + * @url POST {id}/usecreditnote/{creditnoteid} * * @return int * @throws 400 @@ -709,7 +709,7 @@ class Invoices extends DolibarrApi * @throws 404 * @throws 405 */ - function deductCredit($id, $creditId) { + function useCreditNote($id, $discountid) { require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php'; @@ -727,7 +727,7 @@ class Invoices extends DolibarrApi throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } $discount = new DiscountAbsolute($this->db); - $result = $discount->fetch($creditId); + $result = $discount->fetch($discountid); if( ! $result ) { throw new RestException(404, 'Credit not found'); } @@ -741,7 +741,7 @@ class Invoices extends DolibarrApi } /** - * Get a payment list of a given invoice + * Get list of payments of a given invoice * * @param int $id Id of invoice * @@ -779,72 +779,6 @@ class Invoices extends DolibarrApi return $result; } - /** - * Get a list of available assets (down-payments, avoirs) - * - * @param int $id Id of the invoice - * - * @url GET {id}/getavailableassets - * - * @return array - * @throws 400 - * @throws 401 - * @throws 404 - * @throws 405 - */ - function getavailableassets($id) { - - if(! DolibarrApiAccess::$user->rights->facture->lire) { - 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->list_qualified_avoir_invoices($this->invoice->socid); - if( $result < 0) { - throw new RestException(405, $this->invoice->error); - } - - $return = array(); - foreach ($result as $invoiceID=>$line) { - - if($invoiceID == $id) { // ignore current invoice - continue; - } - - $result = $this->invoice->fetch($invoiceID); - if( ! $result ) { - throw new RestException(404, 'Invoice '.$invoiceID.' not found'); - } - - $result = $this->invoice->getListOfPayments(); - if( $result < 0) { - throw new RestException(405, $this->invoice->error); - } - - if(count($result) == 0) { // ignore unpaid invoices - continue; - } - - // format results - $line["id"] = $invoiceID; - $line["payments"] = $result; - $return[] = $line; - } - - return $return; - } - /** * Clean sensible object datas * From a741a6bb3ea6ea08207df635d43079fb846d13b3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 27 Oct 2017 00:41:56 +0200 Subject: [PATCH 10/10] Update api_thirdparties.class.php --- .../societe/class/api_thirdparties.class.php | 50 +++++++++++++++++-- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index ee1fd17d39a..08e67919de9 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -340,14 +340,14 @@ class Thirdparties extends DolibarrApi /** - * Get exceptional discount of a thirdparty + * Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers...) * * @param int $id ID of the thirdparty * @param string $filter Filter exceptional discount. "none" will return every discount, "available" returns unapplied discounts, "used" returns applied discounts {@choice none,available,used} * @param string $sortfield Sort field * @param string $sortorder Sort order * - * @url GET {id}/exceptionaldiscounts + * @url GET {id}/fixedamountdiscounts * * @return array List of deposit and credit notes * @@ -356,7 +356,7 @@ class Thirdparties extends DolibarrApi * @throws 404 * @throws 503 */ - function getExceptionalDiscount($id,$filter="none",$sortfield = "f.type", $sortorder = 'ASC') + function getFixedAmountDiscounts($id, $filter="none", $sortfield = "f.type", $sortorder = 'ASC') { if(! DolibarrApiAccess::$user->rights->societe->lire) { throw new RestException(401); @@ -366,7 +366,7 @@ class Thirdparties extends DolibarrApi throw new RestException(400, 'Thirdparty ID is mandatory'); } - if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) { + if( ! DolibarrApi::_checkAccessToResource('societe',$id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } @@ -392,13 +392,53 @@ class Thirdparties extends DolibarrApi while ( $obj = $this->db->fetch_object($result) ) { $obj_ret[] = $obj; } - } return $obj_ret; } + /** + * Return list of invoices qualified to be corrected by a credit note. + * Invoices matching the following rules are returned + * (validated + payment on process) or classified (payed completely or payed partialy) + not already replaced + not already a credit note + * + * @param int $id Id of thirdparty + * + * @url GET {id}/getinvoicesqualifiedforcreditnote + * + * @return array + * @throws 400 + * @throws 401 + * @throws 404 + * @throws 405 + */ + function getInvoicesQualifiedForCreditNote($id) { + + if(! DolibarrApiAccess::$user->rights->facture->lire) { + throw new RestException(401); + } + if(empty($id)) { + throw new RestException(400, 'Thirdparty ID is mandatory'); + } + + if( ! DolibarrApi::_checkAccessToResource('societe',$id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + /*$result = $this->thirdparty->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Thirdparty not found'); + }*/ + + $result = $this->thirdparty->list_qualified_avoir_invoices($id); + if( $result < 0) { + throw new RestException(405, $this->thirdparty->error); + } + + return $result; + } + /** * Clean sensible object datas