From 299f9f1eb06f9df950df1dc02e368caa9b3a9871 Mon Sep 17 00:00:00 2001 From: qdp-odoo Date: Wed, 5 Dec 2018 13:58:46 +0000 Subject: [PATCH] [REF] account: refactoring of the invoice validation Better split of invoice_validate() and action_move_create() functions: the later now only deals with the account.move creation while the former assigns missing values (dates) and gets its number. The move now always receives its name from the invoice, which means there's no need anymore of the 'invoice' arg in account.move post() method. We now also put the origin of the invoice in the move ref, in order to allow searching on that reference in the bank statement reconciliation widget. closes odoo/odoo#29343 --- addons/account/models/account_invoice.py | 57 ++++++++++++++----- addons/account/models/account_move.py | 19 ++----- .../stock_account/models/account_invoice.py | 4 +- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py index 629a5c5d261..34d510af50d 100644 --- a/addons/account/models/account_invoice.py +++ b/addons/account/models/account_invoice.py @@ -908,8 +908,8 @@ class AccountInvoice(models.Model): if to_open_invoices.filtered(lambda inv: not inv.account_id): raise UserError(_('No account was found to create the invoice, be sure you have installed a chart of account.')) to_open_invoices.action_date_assign() - to_open_invoices.action_move_create() - return to_open_invoices.invoice_validate() + to_open_invoices.invoice_validate() + return to_open_invoices.action_move_create() @api.multi def action_invoice_paid(self): @@ -1184,12 +1184,6 @@ class AccountInvoice(models.Model): raise UserError(_('Please add at least one invoice line.')) if inv.move_id: continue - - - if not inv.date_invoice: - inv.write({'date_invoice': fields.Date.context_today(self)}) - if not inv.date_due: - inv.write({'date_due': inv.date_invoice}) company_currency = inv.company_id.currency_id # create move lines (one per invoice line + eventual taxes and analytic lines) @@ -1243,17 +1237,22 @@ class AccountInvoice(models.Model): line = inv.finalize_invoice_move_lines(line) date = inv.date or inv.date_invoice + move_ref = inv.reference + if inv.origin: + if move_ref: + move_ref += ' (%s)' % inv.origin + else: + move_ref = inv.origin move_vals = { - 'ref': inv.reference, + 'ref': move_ref, 'line_ids': line, 'journal_id': inv.journal_id.id, 'date': date, 'narration': inv.comment, + 'name': inv.number, } move = account_move.create(move_vals) - # Pass invoice in method post: used if you want to get the same - # account move reference when creating the same invoice after a cancelled one: - move.post(invoice = inv) + move.post() # make the invoice point to that move vals = { 'move_id': move.id, @@ -1287,12 +1286,40 @@ class AccountInvoice(models.Model): for invoice in self.filtered(lambda invoice: invoice.partner_id not in invoice.message_partner_ids): invoice.message_subscribe([invoice.partner_id.id]) + for invoice in self: + vals = {'state': 'open'} + if not invoice.date_invoice: + vals['date_invoice'] = fields.Date.context_today(self) + if not invoice.date_due: + vals['date_due'] = vals.get('date_invoice', invoice.date_invoice) + + if (invoice.move_name and invoice.move_name != '/'): + new_name = invoice.move_name + else: + new_name = False + journal = invoice.journal_id + if journal.sequence_id: + # If invoice is actually refund and journal has a refund_sequence then use that one or use the regular one + sequence = journal.sequence_id + if invoice.type in ['out_refund', 'in_refund'] and journal.refund_sequence: + if not journal.refund_sequence_id: + raise UserError(_('Please define a sequence for the credit notes')) + sequence = journal.refund_sequence_id + + new_name = sequence.with_context(ir_sequence_date=invoice.date or invoice.date_invoice).next_by_id() + else: + raise UserError(_('Please define a sequence on the journal.')) + #give the invoice its number directly as it's needed in _get_computed_reference() + invoice.number = new_name + # Auto-compute reference, if not already existing and if configured on company if not invoice.reference and invoice.type == 'out_invoice': - invoice.reference = invoice._get_computed_reference() - self._check_duplicate_supplier_reference() + vals['reference'] = invoice._get_computed_reference() - return self.write({'state': 'open'}) + invoice.write(vals) + + self._check_duplicate_supplier_reference() + return True @api.model def line_get_convert(self, line, part): diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 11936ab3470..b724a2be893 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -282,7 +282,7 @@ class AccountMove(models.Model): return res @api.multi - def post(self, invoice=False): + def post(self): self._post_validate() for move in self: move.line_ids.create_analytic_lines() @@ -290,20 +290,11 @@ class AccountMove(models.Model): new_name = False journal = move.journal_id - if invoice and invoice.move_name and invoice.move_name != '/': - new_name = invoice.move_name + if journal.sequence_id: + sequence = journal.sequence_id + new_name = sequence.with_context(ir_sequence_date=move.date).next_by_id() else: - if journal.sequence_id: - # If invoice is actually refund and journal has a refund_sequence then use that one or use the regular one - sequence = journal.sequence_id - if invoice and invoice.type in ['out_refund', 'in_refund'] and journal.refund_sequence: - if not journal.refund_sequence_id: - raise UserError(_('Please define a sequence for the credit notes')) - sequence = journal.refund_sequence_id - - new_name = sequence.with_context(ir_sequence_date=move.date).next_by_id() - else: - raise UserError(_('Please define a sequence on the journal.')) + raise UserError(_('Please define a sequence on the journal.')) if new_name: move.name = new_name diff --git a/addons/stock_account/models/account_invoice.py b/addons/stock_account/models/account_invoice.py index b2effb1f0da..3dd0491cf00 100644 --- a/addons/stock_account/models/account_invoice.py +++ b/addons/stock_account/models/account_invoice.py @@ -55,8 +55,8 @@ class AccountInvoice(models.Model): return product.product_tmpl_id._get_product_accounts()['stock_output'] return product.product_tmpl_id.get_product_accounts()['stock_input'] - def invoice_validate(self): - res = super(AccountInvoice, self).invoice_validate() + def action_move_create(self): + res = super(AccountInvoice, self).action_move_create() self.filtered(lambda i: i.company_id.anglo_saxon_accounting)._anglo_saxon_reconcile_valuation() return res