diff --git a/date_range/models/date_range.py b/date_range/models/date_range.py index 42796c544..9fbd47545 100644 --- a/date_range/models/date_range.py +++ b/date_range/models/date_range.py @@ -20,7 +20,8 @@ class DateRange(models.Model): date_end = fields.Date(string='End date', required=True) type_id = fields.Many2one( comodel_name='date.range.type', string='Type', index=1, required=True, - ondelete='restrict') + ondelete='restrict', domain="['|', ('company_id', '=', company_id), " + "('company_id', '=', False)]") type_name = fields.Char( string='Type', related='type_id.name', readonly=True, store=True) company_id = fields.Many2one( @@ -34,6 +35,23 @@ class DateRange(models.Model): ('date_range_uniq', 'unique (name,type_id, company_id)', 'A date range must be unique per company !')] + @api.onchange('company_id') + def _onchange_company_id(self): + if self.company_id and self.type_id.company_id and \ + self.type_id.company_id != self.company_id: + self._cache.update( + self._convert_to_cache({'type_id': False}, update=True)) + + @api.multi + @api.constrains('company_id', 'type_id') + def _check_company_id_type_id(self): + for rec in self.sudo(): + if rec.company_id and rec.type_id.company_id and\ + rec.company_id != rec.type_id.company_id: + raise ValidationError( + _('The Company in the Date Range and in ' + 'Date Range Type must be the same.')) + @api.constrains('type_id', 'date_start', 'date_end', 'company_id') def _validate_range(self): for this in self: diff --git a/date_range/models/date_range_type.py b/date_range/models/date_range_type.py index bff6e4f5d..3450c6452 100644 --- a/date_range/models/date_range_type.py +++ b/date_range/models/date_range_type.py @@ -3,6 +3,8 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import api, fields, models +from odoo.tools.translate import _ +from odoo.exceptions import ValidationError class DateRangeType(models.Model): @@ -22,7 +24,22 @@ class DateRangeType(models.Model): company_id = fields.Many2one( comodel_name='res.company', string='Company', index=1, default=_default_company) + date_range_ids = fields.One2many('date.range', 'type_id', string='Ranges') _sql_constraints = [ ('date_range_type_uniq', 'unique (name,company_id)', 'A date range type must be unique per company !')] + + @api.constrains('company_id') + def _check_company_id(self): + if not self.env.context.get('bypass_company_validation', False): + for rec in self.sudo(): + if not rec.company_id: + continue + if bool(self.date_range_ids.filtered( + lambda r: r.company_id and + r.company_id != rec.company_id)): + raise ValidationError( + _('You cannot change the company, as this ' + 'Date Range Type is assigned to Date Range ' + '(%s).') % (self.date_range_ids.name_get()[0][1])) diff --git a/date_range/security/date_range_security.xml b/date_range/security/date_range_security.xml index 87862f84b..70f04266b 100644 --- a/date_range/security/date_range_security.xml +++ b/date_range/security/date_range_security.xml @@ -3,11 +3,11 @@ Date Range Type multi-company - ['|',('company_id','=',user.company_id.id),('company_id','=',False)] + ['|',('company_id','child_of',[user.company_id.id]),('company_id','=',False)] Date Range multi-company - ['|',('company_id','=',user.company_id.id),('company_id','=',False)] + ['|',('company_id','child_of',[user.company_id.id]),('company_id','=',False)] diff --git a/date_range/security/ir.model.access.csv b/date_range/security/ir.model.access.csv index aabe7149d..1fe149604 100644 --- a/date_range/security/ir.model.access.csv +++ b/date_range/security/ir.model.access.csv @@ -2,4 +2,4 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_date_range_date_range,date_range.date_range,model_date_range,base.group_user,1,0,0,0 access_date_range_date_range_type,date_range.date_range_type,model_date_range_type,base.group_user,1,0,0,0 access_date_range_date_range_config,date_range.date_range.config,model_date_range,base.group_system,1,1,1,1 -access_date_range_date_range_type_config,date_range.date_range_type.config,model_date_range_type,base.group_system,1,1,1,1 \ No newline at end of file +access_date_range_date_range_type_config,date_range.date_range_type.config,model_date_range_type,base.group_system,1,1,1,1 diff --git a/date_range/wizard/date_range_generator.py b/date_range/wizard/date_range_generator.py index 970af6b71..2e9d8658d 100644 --- a/date_range/wizard/date_range_generator.py +++ b/date_range/wizard/date_range_generator.py @@ -3,6 +3,8 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import api, fields, models +from odoo.tools.translate import _ +from odoo.exceptions import ValidationError from dateutil.rrule import (rrule, YEARLY, MONTHLY, @@ -22,7 +24,8 @@ class DateRangeGenerator(models.TransientModel): date_start = fields.Date(strint='Start date', required=True) type_id = fields.Many2one( comodel_name='date.range.type', string='Type', required=True, - ondelete='cascade') + domain="['|', ('company_id', '=', company_id), " + "('company_id', '=', False)]", ondelete='cascade') company_id = fields.Many2one( comodel_name='res.company', string='Company', default=_default_company) @@ -59,6 +62,23 @@ class DateRangeGenerator(models.TransientModel): 'company_id': self.company_id.id}) return date_ranges + @api.onchange('company_id') + def _onchange_company_id(self): + if self.company_id and self.type_id.company_id and \ + self.type_id.company_id != self.company_id: + self._cache.update( + self._convert_to_cache({'type_id': False}, update=True)) + + @api.multi + @api.constrains('company_id', 'type_id') + def _check_company_id_type_id(self): + for rec in self.sudo(): + if rec.company_id and rec.type_id.company_id and\ + rec.company_id != rec.type_id.company_id: + raise ValidationError( + _('The Company in the Date Range Generator and in ' + 'Date Range Type must be the same.')) + @api.multi def action_apply(self): date_ranges = self._compute_date_ranges()