2
0

[IMP] account_move_fiscal_year : black, isort, prettier

This commit is contained in:
xavier-bouquiaux 2021-11-02 14:36:24 +01:00 committed by BT-anieto
parent c218c49e85
commit 2f59892ad7
No known key found for this signature in database
7 changed files with 204 additions and 152 deletions

View File

@ -2,18 +2,18 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{ {
'name': 'Account Move Fiscal Year', "name": "Account Move Fiscal Year",
'summary': """ "summary": """
Display the fiscal year on journal entries/item""", Display the fiscal year on journal entries/item""",
'version': '12.0.1.1.0', "version": "14.0.1.0.0",
'license': 'AGPL-3', "license": "AGPL-3",
'author': 'ACSONE SA/NV, Odoo Community Association (OCA)', "author": "ACSONE SA/NV, Odoo Community Association (OCA)",
'website': 'https://github.com/OCA/account-financial-tools', "website": "https://github.com/OCA/account-financial-tools",
'depends': [ "depends": [
'account_fiscal_year', "account_fiscal_year",
], ],
'data': [ "data": [
'views/account_move.xml', "views/account_move.xml",
'views/account_move_line.xml', "views/account_move_line.xml",
], ],
} }

View File

@ -7,38 +7,45 @@ from odoo.osv import expression
class AccountMove(models.Model): class AccountMove(models.Model):
_inherit = 'account.move' _inherit = "account.move"
date_range_fy_id = fields.Many2one( date_range_fy_id = fields.Many2one(
comodel_name='account.fiscal.year', string="Fiscal year", comodel_name="account.fiscal.year",
compute='_compute_date_range_fy', search='_search_date_range_fy') string="Fiscal year",
compute="_compute_date_range_fy",
search="_search_date_range_fy",
)
@api.multi @api.multi
@api.depends('date', 'company_id') @api.depends("date", "company_id")
def _compute_date_range_fy(self): def _compute_date_range_fy(self):
for rec in self: for rec in self:
date = fields.Date.to_date(rec.date) date = fields.Date.to_date(rec.date)
company = rec.company_id company = rec.company_id
rec.date_range_fy_id =\ rec.date_range_fy_id = company and company.find_daterange_fy(date) or False
company and company.find_daterange_fy(date) or False
@api.model @api.model
def _search_date_range_fy(self, operator, value): def _search_date_range_fy(self, operator, value):
if operator in ('=', '!=', 'in', 'not in'): if operator in ("=", "!=", "in", "not in"):
date_range_domain = [('id', operator, value)] date_range_domain = [("id", operator, value)]
else: else:
date_range_domain = [('name', operator, value)] date_range_domain = [("name", operator, value)]
date_ranges = self.env['account.fiscal.year'].search(date_range_domain) date_ranges = self.env["account.fiscal.year"].search(date_range_domain)
domain = [('id', '=', -1)] domain = [("id", "=", -1)]
for date_range in date_ranges: for date_range in date_ranges:
domain = expression.OR([domain, [ domain = expression.OR(
'&', [
('date', '>=', date_range.date_from), domain,
('date', '<=', date_range.date_to), [
'|', "&",
('company_id', '=', False), ("date", ">=", date_range.date_from),
('company_id', '=', date_range.company_id.id), ("date", "<=", date_range.date_to),
]]) "|",
("company_id", "=", False),
("company_id", "=", date_range.company_id.id),
],
]
)
return domain return domain

View File

@ -6,8 +6,8 @@ from odoo import fields, models
class AccountMoveLine(models.Model): class AccountMoveLine(models.Model):
_inherit = 'account.move.line' _inherit = "account.move.line"
date_range_fy_id = fields.Many2one( date_range_fy_id = fields.Many2one(
related='move_id.date_range_fy_id', related="move_id.date_range_fy_id",
) )

View File

@ -5,17 +5,20 @@ from odoo import models
class ResCompany(models.Model): class ResCompany(models.Model):
_inherit = 'res.company' _inherit = "res.company"
def find_daterange_fy(self, date): def find_daterange_fy(self, date):
""" """
try to find a date range with type 'fiscalyear' try to find a date range with type 'fiscalyear'
with @param:date contained in its date_start/date_end interval with @param:date contained in its date_start/date_end interval
""" """
fiscalyear = self.env['account.fiscal.year'].search([ fiscalyear = self.env["account.fiscal.year"].search(
('company_id', '=', self.id), [
('date_from', '<=', date), ("company_id", "=", self.id),
('date_to', '>=', date), ("date_from", "<=", date),
], limit=1) ("date_to", ">=", date),
],
limit=1,
)
return fiscalyear return fiscalyear

View File

@ -6,122 +6,164 @@ from odoo.tests.common import TransactionCase
class TestAccountMoveFiscalYear(TransactionCase): class TestAccountMoveFiscalYear(TransactionCase):
def setUp(self): def setUp(self):
super(TestAccountMoveFiscalYear, self).setUp() super(TestAccountMoveFiscalYear, self).setUp()
self.AccountObj = self.env['account.account'] self.AccountObj = self.env["account.account"]
self.AccountJournalObj = self.env['account.journal'] self.AccountJournalObj = self.env["account.journal"]
self.AccountMoveObj = self.env['account.move'] self.AccountMoveObj = self.env["account.move"]
self.DateRangeObj = self.env['account.fiscal.year'] self.DateRangeObj = self.env["account.fiscal.year"]
self.bank_journal = self.AccountJournalObj.search([ self.bank_journal = self.AccountJournalObj.search(
('type', '=', 'bank') [("type", "=", "bank")], limit=1
], limit=1) )
self.account_type_recv = self.env.ref( self.account_type_recv = self.env.ref("account.data_account_type_receivable")
'account.data_account_type_receivable') self.account_type_rev = self.env.ref("account.data_account_type_revenue")
self.account_type_rev = self.env.ref(
'account.data_account_type_revenue')
self.account_recv = self.AccountObj.create({ self.account_recv = self.AccountObj.create(
'code': 'RECV_DR', {
'name': "Receivable (test)", "code": "RECV_DR",
'reconcile': True, "name": "Receivable (test)",
'user_type_id': self.account_type_recv.id, "reconcile": True,
}) "user_type_id": self.account_type_recv.id,
self.account_sale = self.AccountObj.create({ }
'code': 'SALE_DR', )
'name': "Receivable (sale)", self.account_sale = self.AccountObj.create(
'reconcile': True, {
'user_type_id': self.account_type_rev.id, "code": "SALE_DR",
}) "name": "Receivable (sale)",
"reconcile": True,
"user_type_id": self.account_type_rev.id,
}
)
self.date_range_2017 = self.DateRangeObj.create({ self.date_range_2017 = self.DateRangeObj.create(
'name': "2017", {
'date_from': '2017-01-01', "name": "2017",
'date_to': '2017-12-31', "date_from": "2017-01-01",
}) "date_to": "2017-12-31",
}
)
self.date_range_2018 = self.DateRangeObj.create({ self.date_range_2018 = self.DateRangeObj.create(
'name': "2018", {
'date_from': '2018-01-01', "name": "2018",
'date_to': '2018-12-31', "date_from": "2018-01-01",
}) "date_to": "2018-12-31",
}
)
def create_account_move(self, date_str): def create_account_move(self, date_str):
return self.AccountMoveObj.create({ return self.AccountMoveObj.create(
'journal_id': self.bank_journal.id, {
'date': date_str, "journal_id": self.bank_journal.id,
'line_ids': [ "date": date_str,
(0, 0, { "line_ids": [
'name': "Debit", (
'debit': 1000, 0,
'account_id': self.account_recv.id, 0,
}), {
(0, 0, { "name": "Debit",
'name': "Credit", "debit": 1000,
'credit': 1000, "account_id": self.account_recv.id,
'account_id': self.account_sale.id, },
}), ),
] (
}) 0,
0,
{
"name": "Credit",
"credit": 1000,
"account_id": self.account_sale.id,
},
),
],
}
)
def test_01_account_move_date_range_fy_id_compute(self): def test_01_account_move_date_range_fy_id_compute(self):
january_1st = Date.to_date('2017-01-01') january_1st = Date.to_date("2017-01-01")
move = self.create_account_move(january_1st) move = self.create_account_move(january_1st)
self.assertEquals( self.assertEquals(
move.date_range_fy_id, self.date_range_2017, move.date_range_fy_id,
msg="Move period should be 2017") self.date_range_2017,
self.assertTrue(all([ msg="Move period should be 2017",
line.date_range_fy_id == self.date_range_2017 )
for line in move.line_ids self.assertTrue(
]), msg="All lines period should be 2017") all(
[
line.date_range_fy_id == self.date_range_2017
for line in move.line_ids
]
),
msg="All lines period should be 2017",
)
january_2019 = Date.to_date('2019-01-01') january_2019 = Date.to_date("2019-01-01")
move = self.create_account_move(january_2019) move = self.create_account_move(january_2019)
self.assertFalse( self.assertFalse(
bool(move.date_range_fy_id), bool(move.date_range_fy_id), msg="Move shouldn't have any date range"
msg="Move shouldn't have any date range") )
def test_02_account_move_date_range_fy_id_search(self): def test_02_account_move_date_range_fy_id_search(self):
january_2017 = Date.to_date('2017-01-01') january_2017 = Date.to_date("2017-01-01")
january_2018 = Date.to_date('2018-01-01') january_2018 = Date.to_date("2018-01-01")
january_2019 = Date.to_date('2019-01-01') january_2019 = Date.to_date("2019-01-01")
move_2017 = self.create_account_move(january_2017) move_2017 = self.create_account_move(january_2017)
move_2018 = self.create_account_move(january_2018) move_2018 = self.create_account_move(january_2018)
move_2019 = self.create_account_move(january_2019) move_2019 = self.create_account_move(january_2019)
moves = self.AccountMoveObj.search([ moves = self.AccountMoveObj.search(
('date_range_fy_id', 'ilike', '2017'), [
]) ("date_range_fy_id", "ilike", "2017"),
]
)
self.assertTrue(all([ self.assertTrue(
move_2017 in moves, all(
move_2018 not in moves, [
move_2019 not in moves, move_2017 in moves,
]), msg="There should be only moves in 2017") move_2018 not in moves,
move_2019 not in moves,
]
),
msg="There should be only moves in 2017",
)
moves = self.AccountMoveObj.search([ moves = self.AccountMoveObj.search(
('date_range_fy_id', '=', self.date_range_2017.id), [
]) ("date_range_fy_id", "=", self.date_range_2017.id),
]
)
self.assertTrue(all([ self.assertTrue(
move_2017 in moves, all(
move_2018 not in moves, [
move_2019 not in moves, move_2017 in moves,
])) move_2018 not in moves,
move_2019 not in moves,
]
)
)
moves = self.AccountMoveObj.search([ moves = self.AccountMoveObj.search(
('date_range_fy_id', 'in', ( [
self.date_range_2017.id, (
self.date_range_2018.id "date_range_fy_id",
)), "in",
]) (self.date_range_2017.id, self.date_range_2018.id),
),
]
)
self.assertTrue(all([ self.assertTrue(
move_2017 in moves, all(
move_2018 in moves, [
move_2019 not in moves, move_2017 in moves,
])) move_2018 in moves,
move_2019 not in moves,
]
)
)

View File

@ -1,18 +1,17 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2017 ACSONE SA/NV <!-- Copyright 2017 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo> <odoo>
<record model="ir.ui.view" id="account_move_form_view"> <record model="ir.ui.view" id="account_move_form_view">
<field name="name">account.move.form (in account_move_fiscal_year)</field> <field name="name">account.move.form (in account_move_fiscal_year)</field>
<field name="model">account.move</field> <field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/> <field name="inherit_id" ref="account.view_move_form" />
<field name="priority" eval="20"/> <field name="priority" eval="20" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="date" position="after"> <field name="date" position="after">
<field name="date_range_fy_id" options="{'no_open': True}"/> <field name="date_range_fy_id" options="{'no_open': True}" />
</field> </field>
</field> </field>
@ -21,12 +20,12 @@
<record model="ir.ui.view" id="account_move_search_view"> <record model="ir.ui.view" id="account_move_search_view">
<field name="name">account.move.search (in account_move_fiscal_year)</field> <field name="name">account.move.search (in account_move_fiscal_year)</field>
<field name="model">account.move</field> <field name="model">account.move</field>
<field name="inherit_id" ref="account.view_account_move_filter"/> <field name="inherit_id" ref="account.view_account_move_filter" />
<field name="priority" eval="20"/> <field name="priority" eval="20" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="date" position="after"> <field name="date" position="after">
<field name="date_range_fy_id"/> <field name="date_range_fy_id" />
</field> </field>
</field> </field>
@ -35,12 +34,12 @@
<record model="ir.ui.view" id="account_move_tree_view"> <record model="ir.ui.view" id="account_move_tree_view">
<field name="name">account.move.tree (in account_move_fiscal_year)</field> <field name="name">account.move.tree (in account_move_fiscal_year)</field>
<field name="model">account.move</field> <field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_tree"/> <field name="inherit_id" ref="account.view_move_tree" />
<field name="priority" eval="20"/> <field name="priority" eval="20" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="date" position="after"> <field name="date" position="after">
<field name="date_range_fy_id"/> <field name="date_range_fy_id" />
</field> </field>
</field> </field>

View File

@ -1,46 +1,47 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2017 ACSONE SA/NV <!-- Copyright 2017 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo> <odoo>
<record model="ir.ui.view" id="account_move_line_form_view"> <record model="ir.ui.view" id="account_move_line_form_view">
<field name="name">account.move.line.form (in account_move_fiscal_year)</field> <field name="name">account.move.line.form (in account_move_fiscal_year)</field>
<field name="model">account.move.line</field> <field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_form"/> <field name="inherit_id" ref="account.view_move_line_form" />
<field name="priority" eval="20"/> <field name="priority" eval="20" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="date" position="after"> <field name="date" position="after">
<field name="date_range_fy_id" options="{'no_open': True}"/> <field name="date_range_fy_id" options="{'no_open': True}" />
</field> </field>
</field> </field>
</record> </record>
<record model="ir.ui.view" id="account_move_line_search_view"> <record model="ir.ui.view" id="account_move_line_search_view">
<field name="name">account.move.line.search (in account_move_fiscal_year)</field> <field
name="name"
>account.move.line.search (in account_move_fiscal_year)</field>
<field name="model">account.move.line</field> <field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_account_move_line_filter"/> <field name="inherit_id" ref="account.view_account_move_line_filter" />
<field name="priority" eval="20"/> <field name="priority" eval="20" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="date" position="after"> <field name="date" position="after">
<field name="date_range_fy_id"/> <field name="date_range_fy_id" />
</field> </field>
</field> </field>
</record> </record>
<record model="ir.ui.view" id="account_move_line_tree_view"> <record model="ir.ui.view" id="account_move_line_tree_view">
<field name="name">account.move.line.tree (in account_move_fiscal_year)</field> <field name="name">account.move.line.tree (in account_move_fiscal_year)</field>
<field name="model">account.move.line</field> <field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_tree"/> <field name="inherit_id" ref="account.view_move_line_tree" />
<field name="priority" eval="20"/> <field name="priority" eval="20" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="date" position="after"> <field name="date" position="after">
<field name="date_range_fy_id"/> <field name="date_range_fy_id" />
</field> </field>
</field> </field>