[FIX] l10n_fr_pos_cert: calculate valid previous month date

This traceback arises when the current day of the date is 31 while the
schedule action (Generate Monthly Sales Closing) runs.

Scenarios to produce the issue:
[1]
* Imagine the 'date_stop' value is '31-10-2023'
* When the scheduled action('Generate Monthly Sales Closing') runs
* The value of the 'month_target will be 9' and 'year_target' remains same
* The value of 'interval_from' will be '31-09-2023' (an invalid date).

[2]
* Imagine the 'date_stop' value is '31-03-2023'
* The value of 'interval_from' will be '31-02-2023' (an invalid date)

It leads to the traceback
Error: 'ValueError: day is out of range for month'

See:-
https://github.com/odoo/odoo/blob/5fb9825384895fd303bd15c3e4f8dbc55d35904f/addons/l10n_fr_pos_cert/models/account_closing.py#L124-L134

After applying this commit, it will resolve the issue by giving a valid date
in all scenarios.

sentry-4589899768

closes odoo/odoo#143982

X-original-commit: 836d2dae11b663c54bc47f7bc020bced3f243bf8
Signed-off-by: Robin Heinz (rhe) <rhe@odoo.com>
Signed-off-by: Altaf Shaik (alsh) <alsh@odoo.com>
This commit is contained in:
alsh-odoo
2023-11-30 10:13:24 +00:00
parent 39874b0e9c
commit d0f9d8cca7
@@ -4,6 +4,7 @@ from datetime import datetime, timedelta
from odoo import models, api, fields
from odoo.fields import Datetime as FieldDateTime
from dateutil.relativedelta import relativedelta
from odoo.tools.translate import _
from odoo.exceptions import UserError
from odoo.osv.expression import AND
@@ -129,13 +130,10 @@ class AccountClosing(models.Model):
interval_from = date_stop - timedelta(days=1)
name_interval = _('Daily Closing')
elif frequency == 'monthly':
month_target = date_stop.month > 1 and date_stop.month - 1 or 12
year_target = month_target < 12 and date_stop.year or date_stop.year - 1
interval_from = date_stop.replace(year=year_target, month=month_target)
interval_from = date_stop - relativedelta(months=1)
name_interval = _('Monthly Closing')
elif frequency == 'annually':
year_target = date_stop.year - 1
interval_from = date_stop.replace(year=year_target)
interval_from = date_stop - relativedelta(years=1)
name_interval = _('Annual Closing')
return {'interval_from': FieldDateTime.to_string(interval_from),