2
0

[IMP] account_invoice_constraint_chronology: black, isort, prettier

This commit is contained in:
Alexey Pelykh 2021-04-05 16:26:59 +02:00 committed by Zina Rasoamanana
parent 2e68ed8bf7
commit 99ecd163a6
5 changed files with 132 additions and 102 deletions

View File

@ -3,7 +3,7 @@
import datetime import datetime
from odoo import models, api, fields, _ from odoo import _, api, fields, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.tools.misc import format_date from odoo.tools.misc import format_date
@ -14,35 +14,34 @@ class AccountInvoice(models.Model):
@api.model @api.model
def _prepare_previous_invoices_domain(self, invoice): def _prepare_previous_invoices_domain(self, invoice):
domain = [ domain = [
('state', 'not in', ['open', (
'paid', "state",
'cancel', "not in",
'in_payment', ["open", "paid", "cancel", "in_payment", "proforma", "proforma2"],
'proforma', ),
'proforma2']), ("date_invoice", "!=", False),
('date_invoice', '!=', False), ("date_invoice", "<", invoice.date_invoice),
('date_invoice', '<', invoice.date_invoice), ("journal_id", "=", invoice.journal_id.id),
('journal_id', '=', invoice.journal_id.id),
] ]
if ( if (
invoice.journal_id.refund_sequence invoice.journal_id.refund_sequence
and invoice.journal_id.sequence_id != invoice.journal_id.refund_sequence_id and invoice.journal_id.sequence_id != invoice.journal_id.refund_sequence_id
): ):
domain.append(('type', '=', invoice.type)) domain.append(("type", "=", invoice.type))
return domain return domain
@api.model @api.model
def _prepare_later_invoices_domain(self, invoice): def _prepare_later_invoices_domain(self, invoice):
domain = [ domain = [
('state', 'in', ['open', 'in_payment', 'paid']), ("state", "in", ["open", "in_payment", "paid"]),
('date_invoice', '>', invoice.date_invoice), ("date_invoice", ">", invoice.date_invoice),
('journal_id', '=', invoice.journal_id.id), ("journal_id", "=", invoice.journal_id.id),
] ]
if ( if (
invoice.journal_id.refund_sequence invoice.journal_id.refund_sequence
and invoice.journal_id.sequence_id != invoice.journal_id.refund_sequence_id and invoice.journal_id.sequence_id != invoice.journal_id.refund_sequence_id
): ):
domain.append(('type', '=', invoice.type)) domain.append(("type", "=", invoice.type))
return domain return domain
@api.multi @api.multi
@ -52,8 +51,7 @@ class AccountInvoice(models.Model):
for inv in self: for inv in self:
if not inv.journal_id.check_chronology: if not inv.journal_id.check_chronology:
continue continue
invoices = self.search( invoices = self.search(self._prepare_previous_invoices_domain(inv), limit=1)
self._prepare_previous_invoices_domain(inv), limit=1)
if invoices: if invoices:
date_invoice_format = datetime.datetime( date_invoice_format = datetime.datetime(
year=inv.date_invoice.year, year=inv.date_invoice.year,
@ -61,15 +59,18 @@ class AccountInvoice(models.Model):
day=inv.date_invoice.day, day=inv.date_invoice.day,
) )
date_invoice_tz = format_date( date_invoice_tz = format_date(
self.env, fields.Date.context_today( self.env, fields.Date.context_today(self, date_invoice_format)
self, date_invoice_format)) )
raise UserError(_( raise UserError(
_(
"Chronology Error. Please confirm older draft invoices " "Chronology Error. Please confirm older draft invoices "
"before {date_invoice} and try again.").format( "before {date_invoice} and try again."
date_invoice=date_invoice_tz)) ).format(date_invoice=date_invoice_tz)
)
if inv not in previously_validated: if inv not in previously_validated:
invoices = self.search( invoices = self.search(
self._prepare_later_invoices_domain(inv), limit=1) self._prepare_later_invoices_domain(inv), limit=1
)
if invoices: if invoices:
date_invoice_format = datetime.datetime( date_invoice_format = datetime.datetime(
year=inv.date_invoice.year, year=inv.date_invoice.year,
@ -77,10 +78,12 @@ class AccountInvoice(models.Model):
day=inv.date_invoice.day, day=inv.date_invoice.day,
) )
date_invoice_tz = format_date( date_invoice_tz = format_date(
self.env, fields.Date.context_today( self.env, fields.Date.context_today(self, date_invoice_format)
self, date_invoice_format)) )
raise UserError(_( raise UserError(
_(
"Chronology Error. There exist at least one invoice " "Chronology Error. There exist at least one invoice "
"with a later date to {date_invoice}.").format( "with a later date to {date_invoice}."
date_invoice=date_invoice_tz)) ).format(date_invoice=date_invoice_tz)
)
return res return res

View File

@ -1,18 +1,16 @@
# Copyright 2015-2019 ACSONE SA/NV (<http://acsone.eu>) # Copyright 2015-2019 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import models, fields, api from odoo import api, fields, models
class AccountJournal(models.Model): class AccountJournal(models.Model):
_inherit = ['account.journal'] _inherit = ["account.journal"]
check_chronology = fields.Boolean( check_chronology = fields.Boolean(default=False,)
default=False,
)
@api.onchange('type') @api.onchange("type")
def _onchange_type(self): def _onchange_type(self):
self.ensure_one() self.ensure_one()
if self.type not in ['sale', 'purchase']: if self.type not in ["sale", "purchase"]:
self.check_chronology = False self.check_chronology = False

View File

@ -9,44 +9,62 @@ from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
class TestAccountConstraintChronology(common.SavepointCase): class TestAccountConstraintChronology(common.SavepointCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
super(TestAccountConstraintChronology, cls).setUpClass() super(TestAccountConstraintChronology, cls).setUpClass()
# Needed to create invoice # Needed to create invoice
cls.account_type1 = cls.env['account.account.type'].\ cls.account_type1 = cls.env["account.account.type"].create(
create({'name': 'acc type test 1', {
'type': 'receivable', "name": "acc type test 1",
'include_initial_balance': True}) "type": "receivable",
cls.account_type2 = cls.env['account.account.type']. \ "include_initial_balance": True,
create({'name': 'acc type test 2', }
'type': 'other', )
'include_initial_balance': True}) cls.account_type2 = cls.env["account.account.type"].create(
cls.account_account = cls.env['account.account'].\ {
create({'name': 'acc test', "name": "acc type test 2",
'code': 'X2020', "type": "other",
'user_type_id': cls.account_type1.id, "include_initial_balance": True,
'reconcile': True}) }
cls.account_account_line = cls.env['account.account']. \ )
create({'name': 'acc inv line test', cls.account_account = cls.env["account.account"].create(
'code': 'X2021', {
'user_type_id': cls.account_type2.id, "name": "acc test",
'reconcile': True}) "code": "X2020",
cls.sequence = cls.env['ir.sequence'].create( "user_type_id": cls.account_type1.id,
{'name': 'Journal Sale', "reconcile": True,
'prefix': 'SALE', 'padding': 6, }
'company_id': cls.env.ref("base.main_company").id}) )
cls.account_journal_sale = cls.env['account.journal']\ cls.account_account_line = cls.env["account.account"].create(
.create({'name': 'Sale journal', {
'code': 'SALE', "name": "acc inv line test",
'type': 'sale', "code": "X2021",
'sequence_id': cls.sequence.id}) "user_type_id": cls.account_type2.id,
cls.product = cls.env['product.product'].create( "reconcile": True,
{'name': 'product name'}) }
cls.analytic_account = cls.env['account.analytic.account'].\ )
create({'name': 'test account'}) cls.sequence = cls.env["ir.sequence"].create(
{
"name": "Journal Sale",
"prefix": "SALE",
"padding": 6,
"company_id": cls.env.ref("base.main_company").id,
}
)
cls.account_journal_sale = cls.env["account.journal"].create(
{
"name": "Sale journal",
"code": "SALE",
"type": "sale",
"sequence_id": cls.sequence.id,
}
)
cls.product = cls.env["product.product"].create({"name": "product name"})
cls.analytic_account = cls.env["account.analytic.account"].create(
{"name": "test account"}
)
def get_journal_check(self, value): def get_journal_check(self, value):
journal = self.account_journal_sale.copy() journal = self.account_journal_sale.copy()
@ -54,24 +72,28 @@ class TestAccountConstraintChronology(common.SavepointCase):
return journal return journal
def create_simple_invoice(self, journal_id, date): def create_simple_invoice(self, journal_id, date):
invoice = self.env['account.invoice'].create({ invoice = self.env["account.invoice"].create(
'partner_id': self.env.ref('base.res_partner_2').id, {
'account_id': self.account_account.id, "partner_id": self.env.ref("base.res_partner_2").id,
'type': 'in_invoice', "account_id": self.account_account.id,
'journal_id': journal_id, "type": "in_invoice",
'date_invoice': date, "journal_id": journal_id,
'state': 'draft', "date_invoice": date,
}) "state": "draft",
}
)
self.env['account.invoice.line'].create({ self.env["account.invoice.line"].create(
'product_id': self.product.id, {
'quantity': 1.0, "product_id": self.product.id,
'price_unit': 100.0, "quantity": 1.0,
'invoice_id': invoice.id, "price_unit": 100.0,
'name': 'product that cost 100', "invoice_id": invoice.id,
'account_id': self.account_account_line.id, "name": "product that cost 100",
'account_analytic_id': self.analytic_account.id, "account_id": self.account_account_line.id,
}) "account_analytic_id": self.analytic_account.id,
}
)
return invoice return invoice
def test_invoice_draft(self): def test_invoice_draft(self):
@ -82,8 +104,9 @@ class TestAccountConstraintChronology(common.SavepointCase):
self.create_simple_invoice(journal.id, date) self.create_simple_invoice(journal.id, date)
date = today.strftime(DEFAULT_SERVER_DATE_FORMAT) date = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
invoice_2 = self.create_simple_invoice(journal.id, date) invoice_2 = self.create_simple_invoice(journal.id, date)
self.assertTrue((invoice_2.state == 'draft'), self.assertTrue(
"Initial invoice state is not Draft") (invoice_2.state == "draft"), "Initial invoice state is not Draft"
)
with self.assertRaises(UserError): with self.assertRaises(UserError):
invoice_2.action_invoice_open() invoice_2.action_invoice_open()
@ -95,8 +118,9 @@ class TestAccountConstraintChronology(common.SavepointCase):
self.create_simple_invoice(journal.id, date) self.create_simple_invoice(journal.id, date)
date = today.strftime(DEFAULT_SERVER_DATE_FORMAT) date = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
invoice_2 = self.create_simple_invoice(journal.id, date) invoice_2 = self.create_simple_invoice(journal.id, date)
self.assertTrue((invoice_2.state == 'draft'), self.assertTrue(
"Initial invoice state is not Draft") (invoice_2.state == "draft"), "Initial invoice state is not Draft"
)
self.assertTrue(invoice_2.action_invoice_open()) self.assertTrue(invoice_2.action_invoice_open())
def test_invoice_validate(self): def test_invoice_validate(self):
@ -105,13 +129,15 @@ class TestAccountConstraintChronology(common.SavepointCase):
tomorrow = today + timedelta(days=1) tomorrow = today + timedelta(days=1)
date_tomorrow = tomorrow.strftime(DEFAULT_SERVER_DATE_FORMAT) date_tomorrow = tomorrow.strftime(DEFAULT_SERVER_DATE_FORMAT)
invoice_1 = self.create_simple_invoice(journal.id, date_tomorrow) invoice_1 = self.create_simple_invoice(journal.id, date_tomorrow)
self.assertTrue((invoice_1.state == 'draft'), self.assertTrue(
"Initial invoice state is not Draft") (invoice_1.state == "draft"), "Initial invoice state is not Draft"
)
invoice_1.action_invoice_open() invoice_1.action_invoice_open()
date = today.strftime(DEFAULT_SERVER_DATE_FORMAT) date = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
invoice_2 = self.create_simple_invoice(journal.id, date) invoice_2 = self.create_simple_invoice(journal.id, date)
self.assertTrue((invoice_2.state == 'draft'), self.assertTrue(
"Initial invoice state is not Draft") (invoice_2.state == "draft"), "Initial invoice state is not Draft"
)
with self.assertRaises(UserError): with self.assertRaises(UserError):
invoice_2.action_invoice_open() invoice_2.action_invoice_open()
@ -122,15 +148,16 @@ class TestAccountConstraintChronology(common.SavepointCase):
date = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT) date = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
self.create_simple_invoice(journal.id, date) self.create_simple_invoice(journal.id, date)
invoice_2 = self.create_simple_invoice(journal.id, False) invoice_2 = self.create_simple_invoice(journal.id, False)
self.assertTrue((invoice_2.state == 'draft'), self.assertTrue(
"Initial invoice state is not Draft") (invoice_2.state == "draft"), "Initial invoice state is not Draft"
)
with self.assertRaises(UserError): with self.assertRaises(UserError):
invoice_2.action_invoice_open() invoice_2.action_invoice_open()
def test_journal_change_type(self): def test_journal_change_type(self):
self.account_journal_sale.check_chronology = True self.account_journal_sale.check_chronology = True
self.assertTrue(self.account_journal_sale.check_chronology) self.assertTrue(self.account_journal_sale.check_chronology)
self.account_journal_sale.type = 'bank' self.account_journal_sale.type = "bank"
self.account_journal_sale._onchange_type() self.account_journal_sale._onchange_type()
self.assertFalse(self.account_journal_sale.check_chronology) self.assertFalse(self.account_journal_sale.check_chronology)

View File

@ -8,8 +8,10 @@
<field name="inherit_id" ref="account.view_account_journal_form" /> <field name="inherit_id" ref="account.view_account_journal_form" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="refund_sequence" position="after"> <field name="refund_sequence" position="after">
<field name="check_chronology" <field
attrs="{'readonly': [('type', 'not in', ['sale', 'purchase'])]}"/> name="check_chronology"
attrs="{'readonly': [('type', 'not in', ['sale', 'purchase'])]}"
/>
</field> </field>
</field> </field>
</record> </record>