2017-10-12 13:42:47 +02:00
|
|
|
# Copyright 2017 ACSONE SA/NV
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
|
|
|
|
from odoo import api, fields, models
|
|
|
|
from odoo.osv import expression
|
|
|
|
|
|
|
|
|
|
|
|
class AccountMove(models.Model):
|
|
|
|
|
2021-11-02 14:36:24 +01:00
|
|
|
_inherit = "account.move"
|
2017-10-12 13:42:47 +02:00
|
|
|
|
|
|
|
date_range_fy_id = fields.Many2one(
|
2021-11-02 14:36:24 +01:00
|
|
|
comodel_name="account.fiscal.year",
|
|
|
|
string="Fiscal year",
|
|
|
|
compute="_compute_date_range_fy",
|
|
|
|
search="_search_date_range_fy",
|
|
|
|
)
|
2017-10-12 13:42:47 +02:00
|
|
|
|
2021-11-02 14:36:24 +01:00
|
|
|
@api.depends("date", "company_id")
|
2017-10-12 13:42:47 +02:00
|
|
|
def _compute_date_range_fy(self):
|
|
|
|
for rec in self:
|
2018-12-14 10:42:31 +01:00
|
|
|
date = fields.Date.to_date(rec.date)
|
2017-10-12 13:42:47 +02:00
|
|
|
company = rec.company_id
|
2021-11-02 14:36:24 +01:00
|
|
|
rec.date_range_fy_id = company and company.find_daterange_fy(date) or False
|
2017-10-12 13:42:47 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _search_date_range_fy(self, operator, value):
|
2021-11-02 14:36:24 +01:00
|
|
|
if operator in ("=", "!=", "in", "not in"):
|
|
|
|
date_range_domain = [("id", operator, value)]
|
2017-10-12 13:42:47 +02:00
|
|
|
else:
|
2021-11-02 14:36:24 +01:00
|
|
|
date_range_domain = [("name", operator, value)]
|
2017-10-12 13:42:47 +02:00
|
|
|
|
2021-11-02 14:36:24 +01:00
|
|
|
date_ranges = self.env["account.fiscal.year"].search(date_range_domain)
|
2017-10-12 13:42:47 +02:00
|
|
|
|
2021-11-02 14:36:24 +01:00
|
|
|
domain = [("id", "=", -1)]
|
2017-10-12 13:42:47 +02:00
|
|
|
for date_range in date_ranges:
|
2021-11-02 14:36:24 +01:00
|
|
|
domain = expression.OR(
|
|
|
|
[
|
|
|
|
domain,
|
|
|
|
[
|
|
|
|
"&",
|
|
|
|
("date", ">=", date_range.date_from),
|
|
|
|
("date", "<=", date_range.date_to),
|
|
|
|
"|",
|
|
|
|
("company_id", "=", False),
|
|
|
|
("company_id", "=", date_range.company_id.id),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
)
|
2017-10-12 13:42:47 +02:00
|
|
|
return domain
|