diff --git a/addons/account/models/company.py b/addons/account/models/company.py index 25f1c1592b1..ee14977ed93 100644 --- a/addons/account/models/company.py +++ b/addons/account/models/company.py @@ -62,13 +62,14 @@ Best Regards,''') account_setup_coa_done = fields.Boolean(string='Chart of Account Checked', help="Technical field holding the status of the chart of account setup step.") account_setup_bar_closed = fields.Boolean(string='Setup Bar Closed', help="Technical field set to True when setup bar has been closed by the user.") - @api.constrains('fiscalyear_last_month', 'fiscalyear_last_day') - def _fiscalyear_last_day_in_month(self): + @api.model + def _verify_fiscalyear_last_day(self, company_id, last_day, last_month): + company = self.browse(company_id) + last_day = last_day or company.fiscalyear_last_day + last_month = last_month or company.fiscalyear_last_month current_year = datetime.now().year - for company in self: - if company.fiscalyear_last_day > calendar.monthrange(current_year, company.fiscalyear_last_month)[1]: - raise ValidationError(_('The day %s does not exist for the month %s when setting the fiscal year on company %s') - % (company.fiscalyear_last_day, company.fiscalyear_last_month, company.name)) + last_day_of_month = calendar.monthrange(current_year, last_month)[1] + return last_day > last_day_of_month and last_day_of_month or last_day @api.multi def compute_fiscalyear_dates(self, date): diff --git a/addons/account/wizard/setup_wizards.py b/addons/account/wizard/setup_wizards.py index 463c958d2ab..f31cb6812b9 100644 --- a/addons/account/wizard/setup_wizards.py +++ b/addons/account/wizard/setup_wizards.py @@ -10,10 +10,12 @@ class FinancialYearOpeningWizard(models.TransientModel): company_id = fields.Many2one(comodel_name='res.company', required=True) opening_move_posted = fields.Boolean(string='Opening Move Posted', compute='_compute_opening_move_posted') opening_date = fields.Date(string='Opening Date', required=True, related='company_id.account_opening_date', help="Date from which the accounting is managed in Odoo. It is the date of the opening entry.") - fiscalyear_last_day = fields.Integer(related="company_id.fiscalyear_last_day", required=True) + fiscalyear_last_day = fields.Integer(related="company_id.fiscalyear_last_day", required=True, + help="The last day of the month will be taken if the chosen day doesn't exist.") fiscalyear_last_month = fields.Selection(selection=[(1, 'January'), (2, 'February'), (3, 'March'), (4, 'April'), (5, 'May'), (6, 'June'), (7, 'July'), (8, 'August'), (9, 'September'), (10, 'October'), (11, 'November'), (12, 'December')], related="company_id.fiscalyear_last_month", - required=True) + required=True, + help="The last day of the month will be taken if the chosen day doesn't exist.") account_setup_fy_data_done = fields.Boolean(string='Financial year setup marked as done', compute="_compute_setup_marked_done") @api.depends('company_id.account_setup_fy_data_done') @@ -34,6 +36,17 @@ class FinancialYearOpeningWizard(models.TransientModel): """ Forces fiscal year setup state to 'undone'.""" self.company_id.account_setup_fy_data_done = False + @api.multi + def write(self, vals): + if 'fiscalyear_last_day' in vals or 'fiscalyear_last_month' in vals: + for wizard in self: + company = wizard.company_id + vals['fiscalyear_last_day'] = company._verify_fiscalyear_last_day( + company.id, + vals.get('fiscalyear_last_day'), + vals.get('fiscalyear_last_month')) + return super(FinancialYearOpeningWizard, self).write(vals) + class OpeningAccountMoveWizard(models.TransientModel): _name = 'account.opening'