Skip to content

Commit

Permalink
Update api_invoices.class.php
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Oct 26, 2017
1 parent da0a205 commit f4a06ae
Showing 1 changed file with 12 additions and 78 deletions.
90 changes: 12 additions & 78 deletions htdocs/compta/facture/class/api_invoices.class.php
Expand Up @@ -653,28 +653,28 @@ function settopaid($id, $close_code='', $close_note='')


/**
* 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
* @throws 401
* @throws 404
* @throws 405
*/
function addDiscount($id, $discountId) {
function useDiscount($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)) {
if(empty($discountid)) {
throw new RestException(400, 'Discount ID is mandatory');
}

Expand All @@ -687,7 +687,7 @@ function addDiscount($id, $discountId) {
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);
}
Expand All @@ -696,20 +696,20 @@ function addDiscount($id, $discountId) {
}

/**
* 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
* @throws 401
* @throws 404
* @throws 405
*/
function deductCredit($id, $creditId) {
function useCreditNote($id, $discountid) {

require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php';

Expand All @@ -727,7 +727,7 @@ function deductCredit($id, $creditId) {
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');
}
Expand All @@ -741,7 +741,7 @@ function deductCredit($id, $creditId) {
}

/**
* Get a payment list of a given invoice
* Get list of payments of a given invoice
*
* @param int $id Id of invoice
*
Expand Down Expand Up @@ -779,72 +779,6 @@ function getPayments($id) {
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
*
Expand Down

0 comments on commit f4a06ae

Please sign in to comment.