From d0f9d8cca7fb3e3f704fbdf6932da81af7018fe7 Mon Sep 17 00:00:00 2001 From: alsh-odoo Date: Thu, 2 Nov 2023 11:49:07 +0530 Subject: [PATCH] [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) Signed-off-by: Altaf Shaik (alsh) --- addons/l10n_fr_pos_cert/models/account_closing.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/addons/l10n_fr_pos_cert/models/account_closing.py b/addons/l10n_fr_pos_cert/models/account_closing.py index 173c62d7f5c..a08058fae90 100644 --- a/addons/l10n_fr_pos_cert/models/account_closing.py +++ b/addons/l10n_fr_pos_cert/models/account_closing.py @@ -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),