Browse Source

date_range: adapt to multicompany

pull/1430/head
mreficent 7 years ago
committed by Laurent Mignon (ACSONE)
parent
commit
b291ccfc72
  1. 20
      date_range/models/date_range.py
  2. 17
      date_range/models/date_range_type.py
  3. 4
      date_range/security/date_range_security.xml
  4. 22
      date_range/wizard/date_range_generator.py

20
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:

17
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]))

4
date_range/security/date_range_security.xml

@ -3,11 +3,11 @@
<record id="date_range_type_comp_rule" model="ir.rule">
<field name="name">Date Range Type multi-company</field>
<field name="model_id" ref="model_date_range_type"/>
<field name="domain_force"> ['|',('company_id','=',user.company_id.id),('company_id','=',False)]</field>
<field name="domain_force"> ['|',('company_id','child_of',[user.company_id.id]),('company_id','=',False)]</field>
</record>
<record id="date_range_comp_rule" model="ir.rule">
<field name="name">Date Range multi-company</field>
<field name="model_id" ref="model_date_range"/>
<field name="domain_force"> ['|',('company_id','=',user.company_id.id),('company_id','=',False)]</field>
<field name="domain_force"> ['|',('company_id','child_of',[user.company_id.id]),('company_id','=',False)]</field>
</record>
</odoo>

22
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()

Loading…
Cancel
Save