changeset e1d5e7316d2a in modules/sale_complaint:default
details: 
https://hg.tryton.org/modules/sale_complaint?cmd=changeset;node=e1d5e7316d2a
description:
        Make an invoice correction for credit note action

        This is needed to keep the quantity invoiced coherent with the stock 
move
        quantity to update the unit price.

        issue9975
        review353061002
diffstat:

 CHANGELOG                         |   1 +
 complaint.py                      |  34 +++++++++++++++++++++-------------
 tests/scenario_sale_complaint.rst |  17 ++++++++++-------
 3 files changed, 32 insertions(+), 20 deletions(-)

diffs (92 lines):

diff -r 5428f95711d0 -r e1d5e7316d2a CHANGELOG
--- a/CHANGELOG Sun Dec 27 00:19:30 2020 +0100
+++ b/CHANGELOG Mon Jan 18 23:17:20 2021 +0100
@@ -1,3 +1,4 @@
+* Make an invoice correction for credit note action
 * Compute amount of complaint action
 * Add active field to complaint type
 
diff -r 5428f95711d0 -r e1d5e7316d2a complaint.py
--- a/complaint.py      Sun Dec 27 00:19:30 2020 +0100
+++ b/complaint.py      Mon Jan 18 23:17:20 2021 +0100
@@ -581,19 +581,27 @@
                     line2qty = {invoice_line: self.quantity}
                 if self.unit_price is not None:
                     line2price = {invoice_line: self.unit_price}
-            credit_note = invoice._credit()
-            credit_lines = []
-            for invoice_line in invoice_lines:
-                credit_line = invoice_line._credit()
-                credit_lines.append(credit_line)
-                credit_line.origin = self.complaint
-                if invoice_line in line2qty:
-                    credit_line.quantity = -line2qty[invoice_line]
-                if invoice_line in line2price:
-                    credit_line.unit_price = line2price[invoice_line]
-            credit_note.lines = credit_lines
-            credit_note.taxes = None
-            credit_note.save()
+            with Transaction().set_context(_account_invoice_correction=True):
+                credit_note, = Invoice.copy([invoice], default={
+                        'lines': [],
+                        })
+                # Copy each line one by one to get negative and positive lines
+                # following each other
+                for invoice_line in invoice_lines:
+                    qty = line2qty.get(invoice_line, invoice_line.quantity)
+                    unit_price = invoice_line.unit_price - line2price.get(
+                        invoice_line, invoice_line.unit_price)
+                    Line.copy([invoice_line], default={
+                            'invoice': credit_note.id,
+                            'quantity': -qty,
+                            'origin': str(self.complaint),
+                            })
+                    credit_line, = Line.copy([invoice_line], default={
+                            'invoice': credit_note.id,
+                            'quantity': qty,
+                            'unit_price': unit_price,
+                            'origin': str(self.complaint),
+                            })
             credit_note.update_taxes()
         else:
             return
diff -r 5428f95711d0 -r e1d5e7316d2a tests/scenario_sale_complaint.rst
--- a/tests/scenario_sale_complaint.rst Sun Dec 27 00:19:30 2020 +0100
+++ b/tests/scenario_sale_complaint.rst Mon Jan 18 23:17:20 2021 +0100
@@ -216,9 +216,11 @@
     >>> credit_note.type
     'out'
     >>> len(credit_note.lines)
-    2
+    4
     >>> sum(l.quantity for l in credit_note.lines)
-    -5.0
+    0.0
+    >>> credit_note.total_amount
+    Decimal('-50.00')
 
 Create a complaint to credit partially the invoice::
 
@@ -246,9 +248,9 @@
     >>> credit_note.type
     'out'
     >>> len(credit_note.lines)
-    2
+    4
     >>> sum(l.quantity for l in credit_note.lines)
-    -2.0
+    0.0
     >>> credit_note.total_amount
     Decimal('-15.00')
 
@@ -271,6 +273,7 @@
     >>> credit_note = action.result
     >>> credit_note.type
     'out'
-    >>> credit_note_line, = credit_note.lines
-    >>> credit_note_line.quantity
-    -1.0
+    >>> len(credit_note.lines)
+    2
+    >>> sum(l.quantity for l in credit_note.lines)
+    0.0

Reply via email to