# -*- coding: utf-8 -*-
|
|
|
|
from odoo import models, fields, api
|
|
from odoo.exceptions import UserError, ValidationError
|
|
from psycopg2 import sql, DatabaseError
|
|
|
|
from werkzeug import utils
|
|
|
|
class opendons_relationshipType(models.Model):
|
|
_name = 'opendons_relationship.type'
|
|
_description = 'type de la relation'
|
|
|
|
name = fields.Char('Name', required=True)
|
|
description = fields.Text()
|
|
|
|
nature_ids = fields.One2many(
|
|
'opendons_relationship.nature',
|
|
'type_id',
|
|
string='Nature',
|
|
required=True,
|
|
track_visibility='onchange')
|
|
company_id = fields.Many2one(
|
|
"res.company",
|
|
string="Company",
|
|
required=True,
|
|
default=lambda self: self.env.company
|
|
|
|
)
|
|
@api.constrains('name')
|
|
def _check_name(self):
|
|
|
|
count=self.search_count([('name','=',self.name),('id', '!=', self.id)])
|
|
if count>0:
|
|
raise UserError('This name already exist')
|
|
|
|
class opendons_relationshipNature(models.Model):
|
|
_name = 'opendons_relationship.nature'
|
|
_description = 'nature de la relation'
|
|
|
|
name = fields.Char('Name', required=True)
|
|
description = fields.Text()
|
|
type_id = fields.Many2one(
|
|
'opendons_relationship.type', string='Type', tracking=True,
|
|
required=True)
|
|
company_id = fields.Many2one(
|
|
"res.company",
|
|
string="Company",
|
|
required=True,
|
|
default=lambda self: self.env.company
|
|
|
|
)
|
|
|
|
@api.constrains('name')
|
|
def _check_name(self):
|
|
|
|
count=self.search_count([('name','=',self.name),('id', '!=', self.id)])
|
|
if count>0:
|
|
raise UserError('This name already exist')
|
|
|
|
class opendons_relationshipPartner(models.Model):
|
|
_name = 'opendons_relationship.partner'
|
|
_description = 'relations pour un contact'
|
|
|
|
type_id=fields.Many2one('opendons_relationship.type',string='type')
|
|
nature_id=fields.Many2one('opendons_relationship.nature',string='nature')
|
|
|
|
#nature = fields.Char(related='type_id.nature_id.name')
|
|
partner_id = fields.Many2one(
|
|
'res.partner',
|
|
string='partner',
|
|
required=True,
|
|
index=True,
|
|
readonly=True,
|
|
track_visibility='onchange',
|
|
ondelete='restrict'
|
|
)
|
|
partnerRelationship_id = fields.Many2one(
|
|
'res.partner',
|
|
string='partner relationship',
|
|
required=True,
|
|
index=True,
|
|
track_visibility='onchange',
|
|
ondelete='restrict'
|
|
)
|
|
active=fields.Boolean(string='active',default=True,track_visibility='always')
|
|
#date_active=fields.Date(readonly=True,default=lambda self: fields.Date.today(),string='Date active',track_visibility='always')
|
|
date_active=fields.Date(readonly=True,string='Date active',track_visibility='always')
|
|
date_inactive=fields.Date(readonly=True,string='Date inactive',track_visibility='always')
|
|
|
|
@api.onchange('type_id')
|
|
def _onchange_type_id(self):
|
|
res = {}
|
|
res['domain']={'nature_id':[('type_id', '=', self.type_id.id)]}
|
|
return res
|
|
|
|
|
|
# def create(self,vals):
|
|
|
|
|
|
# # vals['active']=fields.Date.context_today(self)
|
|
# res = super(opendons_relationshipPartner, self).create(vals)
|
|
|
|
# return res
|
|
|
|
def write(self,vals):
|
|
date_active=False
|
|
date_inactive=False
|
|
for val in vals:
|
|
if val=='active' and not vals['active']:
|
|
date_inactive=fields.Date.context_today(self)
|
|
if val=='active' and vals['active']:
|
|
date_inactive=False
|
|
date_active=fields.Date.context_today(self)
|
|
|
|
|
|
vals['date_inactive']=date_inactive
|
|
vals['date_active']=date_active
|
|
|
|
|
|
res = super(opendons_relationshipPartner, self).write(vals)
|
|
|
|
|
|
return res
|
|
|
|
@api.model
|
|
def create(self,vals):
|
|
|
|
vals['date_active']=fields.Date.context_today(self)
|
|
res = super(opendons_relationshipPartner, self).create(vals)
|
|
|
|
#Attention la relation est reciproque entre les 2 contacts,donc on doit créer la relation pour l'autre contact
|
|
vals2={}
|
|
vals2['type_id']=int(res.type_id)
|
|
vals2['nature_id']=int(res.nature_id)
|
|
vals2['partner_id']=int(res.partnerRelationship_id)
|
|
vals2['partnerRelationship_id']=int(res.partner_id)
|
|
vals2['active']=True
|
|
vals2['date_active']=fields.Date.context_today(self)
|
|
|
|
|
|
res2=super(opendons_relationshipPartner, self).create(vals2)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
# sql_constraints = [
|
|
# ('qualifier_uniq', 'unique (qualifier_id,partner_id)', 'A qualifier could be defined only one time on same partner.')
|
|
# ]
|
|
|
|
# @api.constrains('qualifier_id')
|
|
# def _check_qualifier_id(self):
|
|
# for q in self:
|
|
# result=self.search_count([('qualifier_id','=',int(self.qualifier_id)),('id', '!=', self.id),('partner_id','=',int(self.partner_id))])
|
|
# if result>0:
|
|
# raise UserError('A qualifier could be defined only one time on same partner.')
|