In order to improve the fiscal year management, we allow the user to define custom fiscal years
using the account.fiscalyear model.
Was task: https://www.odoo.com/web#id=39878&view_type=form&model=project.task&menu_id=
Was PR #20936
57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo.exceptions import ValidationError
|
|
from odoo import api, fields, models, _
|
|
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
class AccountFiscalYear(models.Model):
|
|
_name = 'account.fiscal.year'
|
|
_description = 'Fiscal Year'
|
|
|
|
name = fields.Char(string='Name', required=True)
|
|
date_from = fields.Date(string='Start Date', required=True,
|
|
help='Start Date, included in the fiscal year.')
|
|
date_to = fields.Date(string='End Date', required=True,
|
|
help='Ending Date, included in the fiscal year.')
|
|
company_id = fields.Many2one('res.company', string='Company', required=True,
|
|
default=lambda self: self.env.user.company_id)
|
|
|
|
@api.constrains('date_from', 'date_to', 'company_id')
|
|
def _check_dates(self):
|
|
'''
|
|
Check interleaving between fiscal years.
|
|
There are 3 cases to consider:
|
|
|
|
s1 s2 e1 e2
|
|
( [----)----]
|
|
|
|
s2 s1 e2 e1
|
|
[----(----] )
|
|
|
|
s1 s2 e2 e1
|
|
( [----] )
|
|
'''
|
|
for fy in self:
|
|
# Starting date must be prior to the ending date
|
|
date_from = datetime.strptime(fy.date_from, DEFAULT_SERVER_DATE_FORMAT)
|
|
date_to = datetime.strptime(fy.date_to, DEFAULT_SERVER_DATE_FORMAT)
|
|
if date_to < date_from:
|
|
raise ValidationError(_('The ending date must not be prior to the starting date.'))
|
|
|
|
|
|
domain = [
|
|
('id', '!=', fy.id),
|
|
('company_id', '=', fy.company_id.id),
|
|
'|', '|',
|
|
'&', ('date_from', '<=', fy.date_from), ('date_to', '>=', fy.date_from),
|
|
'&', ('date_from', '<=', fy.date_to), ('date_to', '>=', fy.date_to),
|
|
'&', ('date_from', '<=', fy.date_from), ('date_to', '>=', fy.date_to),
|
|
]
|
|
|
|
if self.search_count(domain) > 0:
|
|
raise ValidationError(_('You can not have an overlap between two fiscal years, please correct the start and/or end dates of your fiscal years.'))
|