From 25d91cda4e9050c5f3713f66dc960a74336cf0a8 Mon Sep 17 00:00:00 2001 From: Thomas Binsfeld Date: Wed, 30 Jan 2019 15:41:24 +0100 Subject: [PATCH] [REF] Account Invoice Constraint Chronology: prepare methods for domains --- .../model/account_invoice.py | 77 +++++++++++-------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/account_invoice_constraint_chronology/model/account_invoice.py b/account_invoice_constraint_chronology/model/account_invoice.py index 76a1d2d9..0b167acb 100644 --- a/account_invoice_constraint_chronology/model/account_invoice.py +++ b/account_invoice_constraint_chronology/model/account_invoice.py @@ -10,20 +10,51 @@ from odoo.exceptions import UserError class AccountInvoice(models.Model): _inherit = "account.invoice" + @api.model + def _prepare_previous_invoices_domain(self, invoice): + return [ + ('state', 'not in', ['open', + 'paid', + 'cancel', + 'proforma', + 'proforma2']), + ('date_invoice', '!=', False), + ('date_invoice', '<', invoice.date_invoice), + ('journal_id', '=', invoice.journal_id.id), + ] + + @api.model + def _prepare_later_invoices_domain(self, invoice): + return [ + ('state', 'in', ['open', 'paid']), + ('date_invoice', '>', invoice.date_invoice), + ('journal_id', '=', invoice.journal_id.id), + ] + @api.multi def action_move_create(self): previously_validated = self.filtered(lambda inv: inv.move_name) res = super(AccountInvoice, self).action_move_create() for inv in self: - if inv.journal_id.check_chronology: - invoices = \ - self.search([('state', 'not in', - ['open', 'paid', 'cancel', 'proforma', - 'proforma2']), - ('date_invoice', '!=', False), - ('date_invoice', '<', inv.date_invoice), - ('journal_id', '=', inv.journal_id.id)], - limit=1) + if not inv.journal_id.check_chronology: + continue + invoices = self.search( + self._prepare_previous_invoices_domain(inv), limit=1) + if invoices: + date_invoice_format = datetime.datetime( + year=inv.date_invoice.year, + month=inv.date_invoice.month, + day=inv.date_invoice.day, + ) + date_invoice_tz = fields.Date.context_today( + self, date_invoice_format) + raise UserError(_( + "Chronology Error. Please confirm older draft invoices " + "before {date_invoice} and try again.").format( + date_invoice=date_invoice_tz)) + if inv not in previously_validated: + invoices = self.search( + self._prepare_later_invoices_domain(inv), limit=1) if invoices: date_invoice_format = datetime.datetime( year=inv.date_invoice.year, @@ -32,28 +63,8 @@ class AccountInvoice(models.Model): ) date_invoice_tz = fields.Date.context_today( self, date_invoice_format) - raise UserError(_("Chronology Error. " - "Please confirm older draft " - "invoices before %s and try again.") - % date_invoice_tz) - if inv not in previously_validated: - invoices = self.search([('state', 'in', ['open', 'paid']), - ('date_invoice', '>', - inv.date_invoice), - ('journal_id', '=', - inv.journal_id.id)], - limit=1) - - if invoices: - date_invoice_format = datetime.datetime( - year=inv.date_invoice.year, - month=inv.date_invoice.month, - day=inv.date_invoice.day, - ) - date_invoice_tz = fields.Date.context_today( - self, date_invoice_format) - raise UserError(_("Chronology Error. " - "There exist at least one invoice " - "with a later date to %s.") % - date_invoice_tz) + raise UserError(_( + "Chronology Error. There exist at least one invoice " + "with a later date to {date_invoice}.").format( + date_invoice=date_invoice_tz)) return res