From 9ce4b91f5d17e07617abe5c818f7b49289b39fa2 Mon Sep 17 00:00:00 2001 From: Dan Rusu Date: Mon, 11 Feb 2019 09:31:43 +0200 Subject: [PATCH] NEW | make credit note from invoice inspired from the action of the button "Make credit note" from UI. - load invoice data and lines - check invoice type - calculate totals and create new credit notes - create DiscountAbsolute & commit changes --- .../facture/class/api_invoices.class.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 555b6179677..dede6f735c6 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -839,6 +839,118 @@ class Invoices extends DolibarrApi return $this->_cleanObjectDatas($this->invoice); } + /** + * Make credt note from invoice + * + * @param int $id Invoice ID + * @url POST {id}/makecreditnote + * + * @return array An invoice object + * + * @throws 200 + * @throws 304 + * @throws 401 + * @throws 404 + * @throws 500 + */ + function makecreditnote($id) + { + if(! DolibarrApiAccess::$user->rights->facture->creer) { + throw new RestException(401); + } + + $this->db->begin(); + + $result = $this->invoice->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Invoice not found'); + } + + if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->invoice->fetch_thirdparty(); + if( ! $result ) { + throw new RestException(404, 'Thirdparty not found'); + } + + if (! $object->paye) // protection against multiple submit + { + $this->invoice->fetch_lines(); + + // Boucle sur chaque taux de tva + $i=0; + foreach($this->invoice->lines as $line) + { + $amount_ht[$line->tva_tx]+=$line->total_ht; + $amount_tva[$line->tva_tx]+=$line->total_tva; + $amount_ttc[$line->tva_tx]+=$line->total_ttc; + $i++; + } + + // Insert one discount by VAT rate category + $discount = new DiscountAbsolute($this->db); + if ($this->invoice->type == 2) $discount->description='(CREDIT_NOTE)'; + elseif ($this->invoice->type == 3) $discount->description='(DEPOSIT)'; + else { + $this->error="CantConvertToReducAnInvoiceOfThisType"; + return -1; + } + $discount->tva_tx=abs($this->invoice->total_ttc); + $discount->fk_soc=$this->invoice->socid; + $discount->fk_facture_source=$this->invoice->id; + + $error=0; + foreach($amount_ht as $tva_tx => $xxx) + { + $discount->amount_ht=abs($amount_ht[$tva_tx]); + $discount->amount_tva=abs($amount_tva[$tva_tx]); + $discount->amount_ttc=abs($amount_ttc[$tva_tx]); + $discount->tva_tx=abs($tva_tx); + + $result=$discount->create(DolibarrApiAccess::$user); + if ($result < 0) + { + $error++; + break; + } + } + + if (! $error) + { + // Classe facture + $result=$this->invoice->set_paid(DolibarrApiAccess::$user); + if ($result > 0) + { + //$mesg='OK'.$discount->id; + $this->db->commit(); + } + else + { + throw new RestException(500, 'Could not set paid'); + $this->db->rollback(); + } + } + else + { + throw new RestException(500, 'Discount error'); + $this->db->rollback(); + } + } + + $result = $this->invoice->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Invoice not found'); + } + + if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + return $this->_cleanObjectDatas($this->invoice); + } + /** * Add a discount line into an invoice (as an invoice line) using an existing absolute discount *