diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index d8dd5dbeb1a..0640f1fdf4f 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -652,6 +652,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 * @@ -691,7 +734,6 @@ class Invoices extends DolibarrApi return $result; } - /** * Get a list of available assets (down-payments, avoirs) * @@ -731,30 +773,30 @@ class Invoices extends DolibarrApi $return = array(); foreach ($result as $invoiceID=>$line) { - if($invoiceID == $id) { // ignore current invoice - continue; - } + 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->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); - } + throw new RestException(405, $this->invoice->error); + } - if(count($result) == 0) { // ignore unpaid invoices - continue; - } + if(count($result) == 0) { // ignore unpaid invoices + continue; + } - // format results + // format results $line["id"] = $invoiceID; - $line["payments"] = $result; - $return[] = $line; + $line["payments"] = $result; + $return[] = $line; } - + return $return; } diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index 6ed404e28cd..ee1fd17d39a 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -338,6 +338,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, 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; + } + + + /** * Clean sensible object datas *