From 9fd46306bf97b3fedd53322b48632f119255f371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 18 Jan 2021 23:17:20 +0100 Subject: [PATCH] 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 --- CHANGELOG | 1 + complaint.py | 34 +++++++++++++++++++------------ tests/scenario_sale_complaint.rst | 17 +++++++++------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8e26e56354..17677e8fe5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Make an invoice correction for credit note action * Compute amount of complaint action * Add active field to complaint type diff --git a/complaint.py b/complaint.py index 9065baba4f..fc40dbacdb 100644 --- a/complaint.py +++ b/complaint.py @@ -581,19 +581,27 @@ class Action(ModelSQL, ModelView): 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 --git a/tests/scenario_sale_complaint.rst b/tests/scenario_sale_complaint.rst index a063aee2cb..eba11eb55f 100644 --- a/tests/scenario_sale_complaint.rst +++ b/tests/scenario_sale_complaint.rst @@ -216,9 +216,11 @@ Create a complaint to credit the invoice:: >>> 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 @@ Create a complaint to credit partially the invoice:: >>> 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 @@ Create a complaint to credit a invoice line:: >>> 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