From 41104de7ce7ab76468325012fd6115f56ed996b5 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Tue, 24 Oct 2017 11:47:25 +0200 Subject: [PATCH 1/5] 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 2/5] 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 cb71e921fd3be4dea758f7eb066c474bda381f35 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Wed, 25 Oct 2017 11:39:23 +0200 Subject: [PATCH 3/5] 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 4/5] 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 5/5] 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 *