[FIX] tools/date_utils: Fix get_fiscal_year with leap years

Suppose calling get_fiscal_year(date, day=29, month=2) with date(year=2017, month=2, day=28)

The computation steps are:
date_from = date(year=2017, month=2, day=28) - 1 year = date(year=2016, month=2, day=28)
date_from = date_from + 1 day = date(year=2016, month=2, day=29)

It's incorrect because the value of date_from must be date(year=2016, month=3, day=1)

closes odoo/odoo#29416
This commit is contained in:
Laurent Smet
2019-01-03 08:30:31 +00:00
parent 5807c3bd95
commit 67d3d30cf2
2 changed files with 19 additions and 1 deletions
@@ -72,8 +72,15 @@ class TestFiscalPosition(AccountingTestCase):
self.check_compute_fiscal_year(
company,
'2016-03-01',
'2016-02-29',
'2015-03-01',
'2016-02-29',
)
self.check_compute_fiscal_year(
company,
'2016-03-01',
'2016-03-01',
'2017-02-28',
)
+11
View File
@@ -55,8 +55,19 @@ def get_fiscal_year(date, day=31, month=12):
'''
max_day = calendar.monthrange(date.year, month)[1]
date_to = type(date)(date.year, month, min(day, max_day))
# Force at 29 February instead of 28 in case of leap year.
if date_to.month == 2 and date_to.day == 28 and max_day == 29:
date_to = type(date)(date.year, 2, 29)
if date <= date_to:
date_from = date_to - relativedelta(years=1)
max_day = calendar.monthrange(date_from.year, date_from.month)[1]
# Force at 29 February instead of 28 in case of leap year.
if date_from.month == 2 and date_from.day == 28 and max_day == 29:
date_from = type(date)(date_from.year, 2, 29)
date_from += relativedelta(days=1)
else:
date_from = date_to + relativedelta(days=1)