2016-06-26 17:23:23 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-06-05 12:08:57 +02:00
|
|
|
# Copyright 2017 Fabien Bourgeois <fabien@yaltik.com>
|
2016-06-26 17:23:23 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-06-05 12:08:57 +02:00
|
|
|
""" GOLEM Families """
|
|
|
|
|
2017-04-29 09:16:41 +02:00
|
|
|
from odoo import models, fields, api, _
|
2016-06-26 17:23:23 +02:00
|
|
|
|
|
|
|
|
2016-07-08 19:28:00 +02:00
|
|
|
class ResPartner(models.Model):
|
2017-06-05 12:08:57 +02:00
|
|
|
""" Partner adaptations """
|
2016-07-08 19:28:00 +02:00
|
|
|
_inherit = 'res.partner'
|
2016-06-26 17:23:23 +02:00
|
|
|
|
2018-01-25 14:26:59 +01:00
|
|
|
family_member_ids = fields.One2many(related='family_id.member_ids')
|
2018-01-26 13:51:37 +01:00
|
|
|
family_street = fields.Char(related='family_id.street')
|
|
|
|
family_street2 = fields.Char(related='family_id.street2')
|
|
|
|
family_zip = fields.Char(related='family_id.zip')
|
|
|
|
family_city = fields.Char(related='family_id.city')
|
|
|
|
family_state_id = fields.Many2one(related='family_id.state_id')
|
|
|
|
family_country_id = fields.Many2one(related='family_id.country_id')
|
|
|
|
family_phone = fields.Char(related='family_id.phone')
|
|
|
|
family_mobile = fields.Char(related='family_id.mobile')
|
|
|
|
family_email = fields.Char(related='family_id.email')
|
|
|
|
family_website = fields.Char(related='family_id.website')
|
2018-01-25 14:26:59 +01:00
|
|
|
|
2016-07-08 19:28:00 +02:00
|
|
|
family_id = fields.Many2one('golem.family', string='Family', index=True)
|
2016-07-21 16:45:54 +02:00
|
|
|
family_role = fields.Many2one('golem.family.role', string='Role',
|
|
|
|
index=True)
|
2018-01-29 15:35:52 +01:00
|
|
|
family_count = fields.Integer(related='family_id.count')
|
2016-06-27 08:10:54 +02:00
|
|
|
|
2016-07-11 20:13:34 +02:00
|
|
|
@api.multi
|
|
|
|
def button_family_members(self):
|
2017-06-05 12:08:57 +02:00
|
|
|
""" Go to family view, from partner """
|
2016-07-11 20:13:34 +02:00
|
|
|
self.ensure_one()
|
|
|
|
return {'name': _('Family Members'),
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'res_model': 'golem.family',
|
|
|
|
'view_mode': 'form',
|
|
|
|
'res_id': self.family_id.id}
|
|
|
|
|
2018-01-23 15:00:02 +01:00
|
|
|
@api.onchange('family_id')
|
2018-01-23 15:28:56 +01:00
|
|
|
def onchange_family(self):
|
2018-01-23 15:00:02 +01:00
|
|
|
""" Sets as family address if there was no precedence """
|
|
|
|
for member in self:
|
2018-01-23 15:28:56 +01:00
|
|
|
if member.family_id and not any((member.lastname, member.street, \
|
|
|
|
member.street2, member.zip, member.city)):
|
|
|
|
member.update({'lastname': member.family_id.name,
|
2018-01-29 15:35:52 +01:00
|
|
|
'street': member.family_id.street,
|
|
|
|
'street2': member.family_id.street2,
|
|
|
|
'zip': member.family_id.zip,
|
|
|
|
'city': member.family_id.city})
|
2018-01-23 15:00:02 +01:00
|
|
|
|
2016-07-08 22:00:22 +02:00
|
|
|
class GolemMember(models.Model):
|
2017-06-05 12:08:57 +02:00
|
|
|
""" Member adaptations """
|
2016-07-08 22:00:22 +02:00
|
|
|
_inherit = 'golem.member'
|
|
|
|
|
2016-07-13 20:11:56 +02:00
|
|
|
@api.onchange('family_id')
|
|
|
|
def onchange_family(self):
|
|
|
|
""" Sets lastname as family name if there was no precedence """
|
2017-06-05 12:08:57 +02:00
|
|
|
for member in self:
|
|
|
|
if not member.lastname:
|
|
|
|
member.lastname = member.family_id.name
|
2016-07-13 20:11:56 +02:00
|
|
|
|
2016-06-27 08:10:54 +02:00
|
|
|
@api.multi
|
|
|
|
def button_family_members(self):
|
2017-06-05 12:08:57 +02:00
|
|
|
""" Go to family view, from member """
|
2016-06-27 08:10:54 +02:00
|
|
|
self.ensure_one()
|
2017-06-05 12:08:57 +02:00
|
|
|
member = self[0]
|
2016-06-27 08:10:54 +02:00
|
|
|
return {'name': _('Family Members'),
|
|
|
|
'type': 'ir.actions.act_window',
|
2016-07-08 19:28:00 +02:00
|
|
|
'res_model': 'golem.family',
|
|
|
|
'view_mode': 'form',
|
2017-06-05 12:08:57 +02:00
|
|
|
'res_id': member.family_id.id}
|
2016-06-26 17:23:23 +02:00
|
|
|
|
2018-08-01 12:17:15 +02:00
|
|
|
@api.multi
|
|
|
|
def create_family(self):
|
|
|
|
""" Create family from service user """
|
|
|
|
self.ensure_one()
|
|
|
|
member = self[0]
|
|
|
|
if member.family_id:
|
|
|
|
member.family_id = False
|
|
|
|
data = {'name': member.lastname,
|
|
|
|
'street': member.street,
|
|
|
|
'street2': member.street2,
|
|
|
|
'zip': member.zip,
|
|
|
|
'city': member.city,
|
|
|
|
'member_ids': [(4, member.partner_id.id, False)]}
|
|
|
|
self.env['golem.family'].create(data)
|
|
|
|
# self.family_id = new_family.id
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def create(self, values):
|
|
|
|
""" Handles family fields at creation """
|
|
|
|
family_id = values.get('family_id')
|
|
|
|
if family_id:
|
|
|
|
del values['family_id']
|
|
|
|
members = super(GolemMember, self).create(values)
|
|
|
|
if family_id:
|
|
|
|
members.mapped('partner_id').write({'family_id': family_id})
|
|
|
|
return members
|
|
|
|
|
2016-06-26 17:23:23 +02:00
|
|
|
|
|
|
|
class GolemFamily(models.Model):
|
2017-06-05 12:08:57 +02:00
|
|
|
""" GOLEM Family Entity """
|
2016-07-08 19:28:00 +02:00
|
|
|
_name = 'golem.family'
|
|
|
|
_description = 'GOLEM Family Entity'
|
|
|
|
_inherit = 'mail.thread'
|
2016-06-26 17:23:23 +02:00
|
|
|
|
2016-07-08 19:28:00 +02:00
|
|
|
@api.model
|
|
|
|
def _get_default_nationality_id(self):
|
|
|
|
return self.env.ref('base.main_company').country_id
|
2016-06-27 08:10:54 +02:00
|
|
|
|
2018-01-29 15:35:52 +01:00
|
|
|
name = fields.Char(index=True, required=True)
|
|
|
|
street = fields.Char()
|
|
|
|
street2 = fields.Char()
|
|
|
|
zip = fields.Char()
|
|
|
|
city = fields.Char()
|
2016-07-08 19:28:00 +02:00
|
|
|
state_id = fields.Many2one('res.country.state', 'State',
|
|
|
|
ondelete='restrict')
|
|
|
|
country_id = fields.Many2one('res.country', 'Country',
|
|
|
|
ondelete='restrict',
|
|
|
|
default=_get_default_nationality_id)
|
2018-01-29 15:35:52 +01:00
|
|
|
phone = fields.Char()
|
|
|
|
mobile = fields.Char()
|
|
|
|
email = fields.Char()
|
|
|
|
website = fields.Char()
|
2016-06-26 17:23:23 +02:00
|
|
|
|
2016-07-13 20:06:35 +02:00
|
|
|
member_ids = fields.One2many('res.partner', 'family_id', 'Members',
|
|
|
|
domain=[('is_company', '=', False)])
|
2018-01-29 15:35:52 +01:00
|
|
|
note = fields.Text()
|
|
|
|
count = fields.Integer(compute='_compute_count', store=True)
|
2016-07-08 19:28:00 +02:00
|
|
|
|
|
|
|
@api.depends('member_ids')
|
|
|
|
def _compute_count(self):
|
2017-06-05 12:08:57 +02:00
|
|
|
for family in self:
|
|
|
|
family.count = len(family.member_ids)
|
2016-06-26 17:23:23 +02:00
|
|
|
|
2018-01-22 12:37:06 +01:00
|
|
|
@api.onchange('member_ids')
|
2018-01-29 15:35:52 +01:00
|
|
|
def onchange_members(self):
|
2018-01-22 12:37:06 +01:00
|
|
|
""" Sets as member address if there was no precedence """
|
2018-01-23 11:36:54 +01:00
|
|
|
for family in self:
|
|
|
|
if family.member_ids and not any((family.street, family.street2,\
|
|
|
|
family.zip, family.city)):
|
2018-01-23 11:14:39 +01:00
|
|
|
family.update({'street': family.member_ids[0].street,
|
|
|
|
'street2': family.member_ids[0].street2,
|
|
|
|
'zip': family.member_ids[0].zip,
|
2018-01-29 15:35:52 +01:00
|
|
|
'city': family.member_ids[0].city})
|
2016-06-26 17:23:23 +02:00
|
|
|
|
2016-07-08 19:28:00 +02:00
|
|
|
class GolemFamilyRole(models.Model):
|
2017-06-05 12:08:57 +02:00
|
|
|
""" GOLEM Family Role """
|
2016-07-21 16:45:54 +02:00
|
|
|
_name = 'golem.family.role'
|
|
|
|
_description = 'GOLEM Family Role'
|
|
|
|
_sql_constraints = [('golem_family_role_name_uniq',
|
2016-06-26 17:23:23 +02:00
|
|
|
'UNIQUE (name)',
|
2016-07-08 19:28:00 +02:00
|
|
|
'Family role must be unique.')]
|
2016-06-26 17:23:23 +02:00
|
|
|
|
2016-07-08 19:28:00 +02:00
|
|
|
name = fields.Char('Role')
|