@ -2,8 +2,8 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# pylint: disable=api-one-deprecated
# pylint: disable=api-one-deprecated
""" Store relations (connections) between partners. """
""" Store relations (connections) between partners. """
from openerp import _ , api , fields , models
from openerp .exceptions import ValidationError
from odoo import _ , api , fields , models
from odoo .exceptions import ValidationError
class ResPartnerRelation ( models . Model ) :
class ResPartnerRelation ( models . Model ) :
@ -49,20 +49,19 @@ class ResPartnerRelation(models.Model):
vals [ ' left_partner_id ' ] = context . get ( ' active_id ' )
vals [ ' left_partner_id ' ] = context . get ( ' active_id ' )
return super ( ResPartnerRelation , self ) . create ( vals )
return super ( ResPartnerRelation , self ) . create ( vals )
@api.one
@api.constrains ( ' date_start ' , ' date_end ' )
@api.constrains ( ' date_start ' , ' date_end ' )
def _check_dates ( self ) :
def _check_dates ( self ) :
""" End date should not be before start date, if not filled
""" End date should not be before start date, if not filled
: raises ValidationError : When constraint is violated
: raises ValidationError : When constraint is violated
"""
"""
if ( self . date_start and self . date_end and
self . date_start > self . date_end ) :
for record in self :
if ( record . date_start and record . date_end and
record . date_start > record . date_end ) :
raise ValidationError (
raise ValidationError (
_ ( ' The starting date cannot be after the ending date. ' )
_ ( ' The starting date cannot be after the ending date. ' )
)
)
@api.one
@api.constrains ( ' left_partner_id ' , ' type_id ' )
@api.constrains ( ' left_partner_id ' , ' type_id ' )
def _check_partner_left ( self ) :
def _check_partner_left ( self ) :
""" Check left partner for required company or person
""" Check left partner for required company or person
@ -71,7 +70,6 @@ class ResPartnerRelation(models.Model):
"""
"""
self . _check_partner ( " left " )
self . _check_partner ( " left " )
@api.one
@api.constrains ( ' right_partner_id ' , ' type_id ' )
@api.constrains ( ' right_partner_id ' , ' type_id ' )
def _check_partner_right ( self ) :
def _check_partner_right ( self ) :
""" Check right partner for required company or person
""" Check right partner for required company or person
@ -80,43 +78,43 @@ class ResPartnerRelation(models.Model):
"""
"""
self . _check_partner ( " right " )
self . _check_partner ( " right " )
@api.one
@api.multi
def _check_partner ( self , side ) :
def _check_partner ( self , side ) :
""" Check partner for required company or person, and for category
""" Check partner for required company or person, and for category
: param str side : left or right
: param str side : left or right
: raises ValidationError : When constraint is violated
: raises ValidationError : When constraint is violated
"""
"""
for record in self :
assert side in [ ' left ' , ' right ' ]
assert side in [ ' left ' , ' right ' ]
ptype = getattr ( self . type_id , " contact_type_ %s " % side )
partner = getattr ( self , ' %s _partner_id ' % side )
ptype = getattr ( record . type_id , " contact_type_ %s " % side )
partner = getattr ( record , ' %s _partner_id ' % side )
if ( ( ptype == ' c ' and not partner . is_company ) or
if ( ( ptype == ' c ' and not partner . is_company ) or
( ptype == ' p ' and partner . is_company ) ) :
( ptype == ' p ' and partner . is_company ) ) :
raise ValidationError (
raise ValidationError (
_ ( ' The %s partner is not applicable for this relation type. ' ) %
side
_ ( ' The %s partner is not applicable for this '
' relation type. ' ) % side
)
)
category = getattr ( self . type_id , " partner_category_ %s " % side )
category = getattr ( record . type_id , " partner_category_ %s " % side )
if category and category . id not in partner . category_id . ids :
if category and category . id not in partner . category_id . ids :
raise ValidationError (
raise ValidationError (
_ ( ' The %s partner does not have category %s . ' ) %
_ ( ' The %s partner does not have category %s . ' ) %
( side , category . name )
( side , category . name )
)
)
@api.one
@api.constrains ( ' left_partner_id ' , ' right_partner_id ' )
@api.constrains ( ' left_partner_id ' , ' right_partner_id ' )
def _check_not_with_self ( self ) :
def _check_not_with_self ( self ) :
""" Not allowed to link partner to same partner
""" Not allowed to link partner to same partner
: raises ValidationError : When constraint is violated
: raises ValidationError : When constraint is violated
"""
"""
if self . left_partner_id == self . right_partner_id :
if not ( self . type_id and self . type_id . allow_self ) :
for record in self :
if record . left_partner_id == record . right_partner_id :
if not ( record . type_id and record . type_id . allow_self ) :
raise ValidationError (
raise ValidationError (
_ ( ' Partners cannot have a relation with themselves. ' )
_ ( ' Partners cannot have a relation with themselves. ' )
)
)
@api.one
@api.constrains (
@api.constrains (
' left_partner_id ' ,
' left_partner_id ' ,
' type_id ' ,
' type_id ' ,
@ -132,25 +130,27 @@ class ResPartnerRelation(models.Model):
"""
"""
# pylint: disable=no-member
# pylint: disable=no-member
# pylint: disable=no-value-for-parameter
# pylint: disable=no-value-for-parameter
for record in self :
domain = [
domain = [
( ' type_id ' , ' = ' , self . type_id . id ) ,
( ' id ' , ' != ' , self . id ) ,
( ' left_partner_id ' , ' = ' , self . left_partner_id . id ) ,
( ' right_partner_id ' , ' = ' , self . right_partner_id . id ) ,
( ' type_id ' , ' = ' , record . type_id . id ) ,
( ' id ' , ' != ' , record . id ) ,
( ' left_partner_id ' , ' = ' , record . left_partner_id . id ) ,
( ' right_partner_id ' , ' = ' , record . right_partner_id . id ) ,
]
]
if self . date_start :
if record . date_start :
domain + = [
domain + = [
' | ' ,
' | ' ,
( ' date_end ' , ' = ' , False ) ,
( ' date_end ' , ' = ' , False ) ,
( ' date_end ' , ' >= ' , self . date_start ) ,
( ' date_end ' , ' >= ' , record . date_start ) ,
]
]
if self . date_end :
if record . date_end :
domain + = [
domain + = [
' | ' ,
' | ' ,
( ' date_start ' , ' = ' , False ) ,
( ' date_start ' , ' = ' , False ) ,
( ' date_start ' , ' <= ' , self . date_end ) ,
( ' date_start ' , ' <= ' , record . date_end ) ,
]
]
if self . search ( domain ) :
if record . search ( domain ) :
raise ValidationError (
raise ValidationError (
_ ( ' There is already a similar relation with overlapping dates ' )
_ ( ' There is already a similar relation with '
' overlapping dates ' )
)
)