From da0a205a10231eef89aa6015cb038bfc17dc968d Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Wed, 25 Oct 2017 17:09:37 +0200 Subject: [PATCH] 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 *