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).
{
'name': 'Account Move Fiscal Year',
'summary': """
"name": "Account Move Fiscal Year",
"summary": """
Display the fiscal year on journal entries/item""",
'version': '12.0.1.1.0',
'license': 'AGPL-3',
'author': 'ACSONE SA/NV, Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/account-financial-tools',
'depends': [
'account_fiscal_year',
"version": "14.0.1.0.0",
"license": "AGPL-3",
"author": "ACSONE SA/NV, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-financial-tools",
"depends": [
"account_fiscal_year",
],
'data': [
'views/account_move.xml',
'views/account_move_line.xml',
"data": [
"views/account_move.xml",
"views/account_move_line.xml",
],
}

View File

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

View File

@ -6,8 +6,8 @@ from odoo import fields, models
class AccountMoveLine(models.Model):
_inherit = 'account.move.line'
_inherit = "account.move.line"
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):
_inherit = 'res.company'
_inherit = "res.company"
def find_daterange_fy(self, date):
"""
try to find a date range with type 'fiscalyear'
with @param:date contained in its date_start/date_end interval
"""
fiscalyear = self.env['account.fiscal.year'].search([
('company_id', '=', self.id),
('date_from', '<=', date),
('date_to', '>=', date),
], limit=1)
fiscalyear = self.env["account.fiscal.year"].search(
[
("company_id", "=", self.id),
("date_from", "<=", date),
("date_to", ">=", date),
],
limit=1,
)
return fiscalyear

View File

@ -6,122 +6,164 @@ from odoo.tests.common import TransactionCase
class TestAccountMoveFiscalYear(TransactionCase):
def setUp(self):
super(TestAccountMoveFiscalYear, self).setUp()
self.AccountObj = self.env['account.account']
self.AccountJournalObj = self.env['account.journal']
self.AccountMoveObj = self.env['account.move']
self.DateRangeObj = self.env['account.fiscal.year']
self.AccountObj = self.env["account.account"]
self.AccountJournalObj = self.env["account.journal"]
self.AccountMoveObj = self.env["account.move"]
self.DateRangeObj = self.env["account.fiscal.year"]
self.bank_journal = self.AccountJournalObj.search([
('type', '=', 'bank')
], limit=1)
self.bank_journal = self.AccountJournalObj.search(
[("type", "=", "bank")], limit=1
)
self.account_type_recv = self.env.ref(
'account.data_account_type_receivable')
self.account_type_rev = self.env.ref(
'account.data_account_type_revenue')
self.account_type_recv = self.env.ref("account.data_account_type_receivable")
self.account_type_rev = self.env.ref("account.data_account_type_revenue")
self.account_recv = self.AccountObj.create({
'code': 'RECV_DR',
'name': "Receivable (test)",
'reconcile': True,
'user_type_id': self.account_type_recv.id,
})
self.account_sale = self.AccountObj.create({
'code': 'SALE_DR',
'name': "Receivable (sale)",
'reconcile': True,
'user_type_id': self.account_type_rev.id,
})
self.account_recv = self.AccountObj.create(
{
"code": "RECV_DR",
"name": "Receivable (test)",
"reconcile": True,
"user_type_id": self.account_type_recv.id,
}
)
self.account_sale = self.AccountObj.create(
{
"code": "SALE_DR",
"name": "Receivable (sale)",
"reconcile": True,
"user_type_id": self.account_type_rev.id,
}
)
self.date_range_2017 = self.DateRangeObj.create({
'name': "2017",
'date_from': '2017-01-01',
'date_to': '2017-12-31',
})
self.date_range_2017 = self.DateRangeObj.create(
{
"name": "2017",
"date_from": "2017-01-01",
"date_to": "2017-12-31",
}
)
self.date_range_2018 = self.DateRangeObj.create({
'name': "2018",
'date_from': '2018-01-01',
'date_to': '2018-12-31',
})
self.date_range_2018 = self.DateRangeObj.create(
{
"name": "2018",
"date_from": "2018-01-01",
"date_to": "2018-12-31",
}
)
def create_account_move(self, date_str):
return self.AccountMoveObj.create({
'journal_id': self.bank_journal.id,
'date': date_str,
'line_ids': [
(0, 0, {
'name': "Debit",
'debit': 1000,
'account_id': self.account_recv.id,
}),
(0, 0, {
'name': "Credit",
'credit': 1000,
'account_id': self.account_sale.id,
}),
]
})
return self.AccountMoveObj.create(
{
"journal_id": self.bank_journal.id,
"date": date_str,
"line_ids": [
(
0,
0,
{
"name": "Debit",
"debit": 1000,
"account_id": self.account_recv.id,
},
),
(
0,
0,
{
"name": "Credit",
"credit": 1000,
"account_id": self.account_sale.id,
},
),
],
}
)
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)
self.assertEquals(
move.date_range_fy_id, self.date_range_2017,
msg="Move period should be 2017")
self.assertTrue(all([
line.date_range_fy_id == self.date_range_2017
for line in move.line_ids
]), msg="All lines period should be 2017")
move.date_range_fy_id,
self.date_range_2017,
msg="Move period should be 2017",
)
self.assertTrue(
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)
self.assertFalse(
bool(move.date_range_fy_id),
msg="Move shouldn't have any date range")
bool(move.date_range_fy_id), msg="Move shouldn't have any date range"
)
def test_02_account_move_date_range_fy_id_search(self):
january_2017 = Date.to_date('2017-01-01')
january_2018 = Date.to_date('2018-01-01')
january_2019 = Date.to_date('2019-01-01')
january_2017 = Date.to_date("2017-01-01")
january_2018 = Date.to_date("2018-01-01")
january_2019 = Date.to_date("2019-01-01")
move_2017 = self.create_account_move(january_2017)
move_2018 = self.create_account_move(january_2018)
move_2019 = self.create_account_move(january_2019)
moves = self.AccountMoveObj.search([
('date_range_fy_id', 'ilike', '2017'),
])
moves = self.AccountMoveObj.search(
[
("date_range_fy_id", "ilike", "2017"),
]
)
self.assertTrue(all([
move_2017 in moves,
move_2018 not in moves,
move_2019 not in moves,
]), msg="There should be only moves in 2017")
self.assertTrue(
all(
[
move_2017 in moves,
move_2018 not in moves,
move_2019 not in moves,
]
),
msg="There should be only moves in 2017",
)
moves = self.AccountMoveObj.search([
('date_range_fy_id', '=', self.date_range_2017.id),
])
moves = self.AccountMoveObj.search(
[
("date_range_fy_id", "=", self.date_range_2017.id),
]
)
self.assertTrue(all([
move_2017 in moves,
move_2018 not in moves,
move_2019 not in moves,
]))
self.assertTrue(
all(
[
move_2017 in moves,
move_2018 not in moves,
move_2019 not in moves,
]
)
)
moves = self.AccountMoveObj.search([
('date_range_fy_id', 'in', (
self.date_range_2017.id,
self.date_range_2018.id
)),
])
moves = self.AccountMoveObj.search(
[
(
"date_range_fy_id",
"in",
(self.date_range_2017.id, self.date_range_2018.id),
),
]
)
self.assertTrue(all([
move_2017 in moves,
move_2018 in moves,
move_2019 not in moves,
]))
self.assertTrue(
all(
[
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
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record model="ir.ui.view" id="account_move_form_view">
<field name="name">account.move.form (in account_move_fiscal_year)</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="priority" eval="20"/>
<field name="inherit_id" ref="account.view_move_form" />
<field name="priority" eval="20" />
<field name="arch" type="xml">
<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>
@ -21,12 +20,12 @@
<record model="ir.ui.view" id="account_move_search_view">
<field name="name">account.move.search (in account_move_fiscal_year)</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_account_move_filter"/>
<field name="priority" eval="20"/>
<field name="inherit_id" ref="account.view_account_move_filter" />
<field name="priority" eval="20" />
<field name="arch" type="xml">
<field name="date" position="after">
<field name="date_range_fy_id"/>
<field name="date_range_fy_id" />
</field>
</field>
@ -35,12 +34,12 @@
<record model="ir.ui.view" id="account_move_tree_view">
<field name="name">account.move.tree (in account_move_fiscal_year)</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_tree"/>
<field name="priority" eval="20"/>
<field name="inherit_id" ref="account.view_move_tree" />
<field name="priority" eval="20" />
<field name="arch" type="xml">
<field name="date" position="after">
<field name="date_range_fy_id"/>
<field name="date_range_fy_id" />
</field>
</field>

View File

@ -1,32 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2017 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<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="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_form"/>
<field name="priority" eval="20"/>
<field name="inherit_id" ref="account.view_move_line_form" />
<field name="priority" eval="20" />
<field name="arch" type="xml">
<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>
</record>
<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="inherit_id" ref="account.view_account_move_line_filter"/>
<field name="priority" eval="20"/>
<field name="inherit_id" ref="account.view_account_move_line_filter" />
<field name="priority" eval="20" />
<field name="arch" type="xml">
<field name="date" position="after">
<field name="date_range_fy_id"/>
<field name="date_range_fy_id" />
</field>
</field>
@ -35,12 +36,12 @@
<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="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_tree"/>
<field name="priority" eval="20"/>
<field name="inherit_id" ref="account.view_move_line_tree" />
<field name="priority" eval="20" />
<field name="arch" type="xml">
<field name="date" position="after">
<field name="date_range_fy_id"/>
<field name="date_range_fy_id" />
</field>
</field>