diff --git a/partner_multi_relation/models/res_partner_relation.py b/partner_multi_relation/models/res_partner_relation.py
index 0907ab202..37335715f 100644
--- a/partner_multi_relation/models/res_partner_relation.py
+++ b/partner_multi_relation/models/res_partner_relation.py
@@ -43,12 +43,22 @@ class ResPartnerRelation(models.Model):
@api.model
def create(self, vals):
- """Override create to correct values, before being stored."""
+ """Override create to correct values, before being stored.
+ Check to make sure each unique relation stays unique"""
context = self.env.context
if 'left_partner_id' not in vals and context.get('active_id'):
vals['left_partner_id'] = context.get('active_id')
+ if not self.check_is_unique(vals):
+ raise ValidationError(_('Relation already exists!'))
return super(ResPartnerRelation, self).create(vals)
+ @api.model
+ def write(self, vals):
+ """Check to make sure each unique relation stays unique"""
+ if not self.check_is_unique(vals):
+ raise ValidationError(_('Relation already exists!'))
+ return super(ResPartnerRelation, self).write(vals)
+
@api.constrains('date_start', 'date_end')
def _check_dates(self):
"""End date should not be before start date, if not filled
@@ -154,3 +164,24 @@ class ResPartnerRelation(models.Model):
_('There is already a similar relation with '
'overlapping dates')
)
+
+ @api.multi
+ def check_is_unique(self, vals):
+ if 'type_id' in vals:
+ type_id = vals['type_id']
+ else:
+ type_id = self.type_id.id
+ type_rec = self.env['res.partner.relation.type'].\
+ search([('id', '=', type_id)])
+ if 'right_partner_id' in vals:
+ right = vals['right_partner_id']
+ else:
+ right = self.right_partner_id.id
+ res = self.search([('type_id', '=', type_id),
+ ('right_partner_id', '=', right)])
+ if (type_rec.is_unique and len(res) > 0):
+ return False
+ for rec in res:
+ if rec.type_id.is_unique:
+ return False
+ return True
diff --git a/partner_multi_relation/models/res_partner_relation_type.py b/partner_multi_relation/models/res_partner_relation_type.py
index dbcfd2570..c7d59e182 100644
--- a/partner_multi_relation/models/res_partner_relation_type.py
+++ b/partner_multi_relation/models/res_partner_relation_type.py
@@ -73,6 +73,11 @@ class ResPartnerRelationType(models.Model):
" that do not fit the new criteria.\n"
"Specify how this situation should be handled.",
)
+ is_unique = fields.Boolean(
+ string='Is Unique',
+ help="If a relation type is unique, then there cannot be 2"
+ "relations with the same parties and the same type.",
+ )
@api.model
def get_partner_types(self):
@@ -255,7 +260,6 @@ class ResPartnerRelationType(models.Model):
def write(self, vals):
"""Handle existing relations if conditions change."""
self.check_existing(vals)
-
for rec in self:
rec_vals = vals.copy()
if rec_vals.get('is_symmetric', rec.is_symmetric):
diff --git a/partner_multi_relation/views/res_partner_relation_type.xml b/partner_multi_relation/views/res_partner_relation_type.xml
index b82e704b5..8af8a910f 100644
--- a/partner_multi_relation/views/res_partner_relation_type.xml
+++ b/partner_multi_relation/views/res_partner_relation_type.xml
@@ -11,6 +11,7 @@
+
@@ -47,6 +48,7 @@
+