Skip to content

Commit

Permalink
Merge pull request #7703 from Oeris/develop-api
Browse files Browse the repository at this point in the history
FIX Data related to customer moved to thirparties api
  • Loading branch information
eldy committed Oct 26, 2017
2 parents 9283aaf + a741a6b commit 6a98375
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 18 deletions.
107 changes: 89 additions & 18 deletions htdocs/compta/facture/class/api_invoices.class.php
Expand Up @@ -77,21 +77,6 @@ function get($id)
$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);
}
Expand Down Expand Up @@ -667,8 +652,96 @@ function settopaid($id, $close_code='', $close_note='')
}


/**
* 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
*
* @url POST {id}/usediscount/{discountid}
*
* @return int
* @throws 400
* @throws 401
* @throws 404
* @throws 405
*/
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)) {
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;
}

/**
* Add an available credit note discount to payments of an existing invoice (Consume the credit note)
*
* @param int $id Id of invoice
* @param int $discountid Id of a discount coming from a credit note
*
* @url POST {id}/usecreditnote/{creditnoteid}
*
* @return int
* @throws 400
* @throws 401
* @throws 404
* @throws 405
*/
function useCreditNote($id, $discountid) {

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($discountid);
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
* Get list of payments of a given invoice
*
* @param int $id Id of invoice
*
Expand Down Expand Up @@ -706,8 +779,6 @@ function getPayments($id) {
return $result;
}



/**
* Clean sensible object datas
*
Expand Down
109 changes: 109 additions & 0 deletions htdocs/societe/class/api_thirdparties.class.php
Expand Up @@ -83,6 +83,13 @@ function get($id)
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);
}

Expand Down Expand Up @@ -331,6 +338,108 @@ function addCategory($id, $request_data = NULL) {
return $this->company;
}


/**
* 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}/fixedamountdiscounts
*
* @return array List of deposit and credit notes
*
* @throws 400
* @throws 401
* @throws 404
* @throws 503
*/
function getFixedAmountDiscounts($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',$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, 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 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);

$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;
}


/**
* 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
*
Expand Down

0 comments on commit 6a98375

Please sign in to comment.