From 288a876491ec67529bc0166ba266ec345fa18234 Mon Sep 17 00:00:00 2001 From: Dan Rusu Date: Mon, 11 Feb 2019 09:14:06 +0200 Subject: [PATCH 1/8] add API method to create credit note from invoice --- .../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..0fb203f50d1 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 * From 0698ddbd7779a6eec7c546c470d63c8c65e6d1cc Mon Sep 17 00:00:00 2001 From: Dan Rusu Date: Mon, 11 Feb 2019 09:24:16 +0200 Subject: [PATCH 2/8] Revert "add API method to create credit note from invoice" This reverts commit 288a876491ec67529bc0166ba266ec345fa18234. --- .../facture/class/api_invoices.class.php | 112 ------------------ 1 file changed, 112 deletions(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 0fb203f50d1..555b6179677 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -839,118 +839,6 @@ 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 * From 9ce4b91f5d17e07617abe5c818f7b49289b39fa2 Mon Sep 17 00:00:00 2001 From: Dan Rusu Date: Mon, 11 Feb 2019 09:31:43 +0200 Subject: [PATCH 3/8] 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 * From 57e2971a22ad350b96e2a1bc8f303d909e9d7d41 Mon Sep 17 00:00:00 2001 From: Dan Rusu Date: Mon, 11 Feb 2019 19:58:01 +0200 Subject: [PATCH 4/8] fix code indent --- htdocs/compta/facture/class/api_invoices.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index dede6f735c6..8fbfa34bce9 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -866,7 +866,7 @@ class Invoices extends DolibarrApi throw new RestException(404, 'Invoice not found'); } - if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { + if( ! DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } @@ -944,7 +944,7 @@ class Invoices extends DolibarrApi throw new RestException(404, 'Invoice not found'); } - if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { + if( ! DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } From 3d0af97e79934afeff07f80383dfb2e6d3db87ac Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 23 Feb 2019 14:24:37 +0100 Subject: [PATCH 5/8] Update api_invoices.class.php --- htdocs/compta/facture/class/api_invoices.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index ceaca36e39d..3a36f711e4e 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -921,10 +921,10 @@ class Invoices extends DolibarrApi } /** - * Make credt note from invoice + * Create credit note from invoice * * @param int $id Invoice ID - * @url POST {id}/makecreditnote + * @url POST {id}/createCreditNote * * @return array An invoice object * @@ -934,7 +934,7 @@ class Invoices extends DolibarrApi * @throws 404 * @throws 500 */ - function makecreditnote($id) + function createCreditNote($id) { if(! DolibarrApiAccess::$user->rights->facture->creer) { throw new RestException(401); @@ -960,7 +960,7 @@ class Invoices extends DolibarrApi { $this->invoice->fetch_lines(); - // Boucle sur chaque taux de tva + // Loop on each vat rate $i=0; foreach($this->invoice->lines as $line) { From 5bf3a1f175fff94df4f39be7660dfd7eb5995b2b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 23 Feb 2019 14:36:51 +0100 Subject: [PATCH 6/8] Update api_invoices.class.php --- htdocs/compta/facture/class/api_invoices.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 3a36f711e4e..60c034bc240 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -921,7 +921,7 @@ class Invoices extends DolibarrApi } /** - * Create credit note from invoice + * Create a discount (credit available) for a credit note or a deposit. * * @param int $id Invoice ID * @url POST {id}/createCreditNote @@ -934,7 +934,7 @@ class Invoices extends DolibarrApi * @throws 404 * @throws 500 */ - function createCreditNote($id) + function markAsCreditAvailable($id) { if(! DolibarrApiAccess::$user->rights->facture->creer) { throw new RestException(401); From eb733728c3420aae5c02568f4e07f2900bfd7274 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 23 Feb 2019 14:37:09 +0100 Subject: [PATCH 7/8] Update api_invoices.class.php --- htdocs/compta/facture/class/api_invoices.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 60c034bc240..0cb005ddba5 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -924,7 +924,7 @@ class Invoices extends DolibarrApi * Create a discount (credit available) for a credit note or a deposit. * * @param int $id Invoice ID - * @url POST {id}/createCreditNote + * @url POST {id}/markAsCreditAvailable * * @return array An invoice object * From ca456512657be03f972cbcd6cb16e21844011b34 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 23 Feb 2019 14:38:13 +0100 Subject: [PATCH 8/8] Update api_invoices.class.php --- htdocs/compta/facture/class/api_invoices.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 0cb005ddba5..afecfd33b45 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -956,7 +956,7 @@ class Invoices extends DolibarrApi throw new RestException(404, 'Thirdparty not found'); } - if (! $object->paye) // protection against multiple submit + if (! $this->invoice->paye) // protection against multiple submit { $this->invoice->fetch_lines(); @@ -1015,7 +1015,7 @@ class Invoices extends DolibarrApi } else { - throw new RestException(500, 'Discount error'); + throw new RestException(500, 'Discount creation error'); $this->db->rollback(); } }