diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 344a5d280c7..28157085270 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -71,6 +71,27 @@ 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 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); } @@ -646,6 +667,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->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->getListOfPayments(); + if( $result < 0) { + throw new RestException(405, $this->invoice->error); + } + + return $result; + } + + + /** * Clean sensible object datas *