diff --git a/addons/point_of_sale/models/__init__.py b/addons/point_of_sale/models/__init__.py index 2934be43e10..9023c405f2c 100644 --- a/addons/point_of_sale/models/__init__.py +++ b/addons/point_of_sale/models/__init__.py @@ -12,4 +12,5 @@ from . import pos_order from . import pos_session from . import product from . import res_partner +from . import res_company from . import res_config_settings diff --git a/addons/point_of_sale/models/pos_session.py b/addons/point_of_sale/models/pos_session.py index 02ddaf2fd93..502536c785f 100644 --- a/addons/point_of_sale/models/pos_session.py +++ b/addons/point_of_sale/models/pos_session.py @@ -158,6 +158,14 @@ class PosSession(models.Model): ]) > 1: raise ValidationError(_("Another session is already opened for this point of sale.")) + @api.constrains('start_at') + def _check_start_date(self): + for record in self: + company = record.config_id.journal_id.company_id + start_date = record.start_at.date() + if (company.period_lock_date and start_date <= company.period_lock_date) or (company.fiscalyear_lock_date and start_date <= company.fiscalyear_lock_date): + raise ValidationError(_("You cannot create a session before the accounting lock date.")) + @api.model def create(self, values): config_id = values.get('config_id') or self.env.context.get('default_config_id') diff --git a/addons/point_of_sale/models/res_company.py b/addons/point_of_sale/models/res_company.py new file mode 100644 index 00000000000..91a833ba7d6 --- /dev/null +++ b/addons/point_of_sale/models/res_company.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- + +from odoo import api, models, fields, _ +from odoo.exceptions import ValidationError + +class ResCompany(models.Model): + _inherit = 'res.company' + + @api.constrains('period_lock_date', 'fiscalyear_lock_date') + def validate_period_lock_date(self): + """ This constrains makes it impossible to change the period lock date if + some open POS session exists into it. Without that, these POS sessions + would trigger an error message saying that the period has been locked when + trying to close them. + """ + pos_session_model = self.env['pos.session'] + for record in self: + sessions_in_period = pos_session_model.search([('state', '!=', 'closed'), '|', ('start_at', '<=', record.period_lock_date), ('start_at', '<=', record.fiscalyear_lock_date)]) + if sessions_in_period: + sessions_str = ', '.join(sessions_in_period.mapped('name')) + raise ValidationError(_("Please close all the point of sale sessions in this period before closing it. Open sessions are: %s ") % (sessions_str)) \ No newline at end of file