Browse Source
Merge pull request #197 from richard-willowit/several_companies_90
Merge pull request #197 from richard-willowit/several_companies_90
Upgrade "Partner Several Companies" Module.pull/215/head
Pedro M. Baeza
9 years ago
12 changed files with 553 additions and 552 deletions
-
38partner_contact_in_several_companies/README.rst
-
17partner_contact_in_several_companies/__init__.py
-
39partner_contact_in_several_companies/__openerp__.py
-
16partner_contact_in_several_companies/demo/ir_actions.xml
-
13partner_contact_in_several_companies/demo/res_partner.xml
-
232partner_contact_in_several_companies/models.py
-
4partner_contact_in_several_companies/models/__init__.py
-
23partner_contact_in_several_companies/models/ir_actions.py
-
161partner_contact_in_several_companies/models/res_partner.py
-
22partner_contact_in_several_companies/tests/__init__.py
-
119partner_contact_in_several_companies/tests/test_partner_contact_in_several_companies.py
-
179partner_contact_in_several_companies/views/res_partner.xml
@ -1,19 +1,4 @@ |
|||
# -*- coding: utf-8 -*- |
|||
|
|||
# Odoo, Open Source Management Solution |
|||
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es> |
|||
# |
|||
# 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/>. |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from . import models |
@ -1,36 +1,35 @@ |
|||
# -*- coding: utf-8 -*- |
|||
|
|||
# Odoo, Open Source Management Solution |
|||
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es> |
|||
# |
|||
# 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/>. |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
|
|||
{ |
|||
"name": "Contacts in several partners", |
|||
"summary": "Allow to have one contact in several partners", |
|||
"version": "8.0.1.0.0", |
|||
"author": "Odoo Community Association (OCA)", |
|||
"version": "9.0.1.0.0", |
|||
"category": "Customer Relationship Management", |
|||
"website": "https://odoo-community.org/", |
|||
"author": "Odoo Community Association (OCA)", |
|||
"contributors": [ |
|||
'Xavier ALT <xal@openerp.com>', |
|||
'El Hadji Dem <elhadji.dem@savoirfairelinux.com>', |
|||
'TheCloneMaster <the.clone.master@gmail.com>', |
|||
'Sandy Carter <bwrsandman@gmail.com>', |
|||
'Rudolf Schnapka <rs@techno-flex.de>', |
|||
'Sebastien Alix <sebastien.alix@osiell.com>', |
|||
'Jairo Llopis <j.llopis@grupoesoc.es>', |
|||
'Richard deMeester <richard@willowit.com.au>', |
|||
], |
|||
"license": "AGPL-3", |
|||
'application': False, |
|||
'installable': True, |
|||
'auto_install': False, |
|||
"depends": [ |
|||
"partner_contact_personal_information_page", |
|||
"base" |
|||
], |
|||
"data": [ |
|||
"views/res_partner.xml", |
|||
], |
|||
"demo": [ |
|||
"demo/res_partner.xml", |
|||
"demo/ir_actions.xml", |
|||
], |
|||
'installable': False, |
|||
} |
@ -0,0 +1,16 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<odoo> |
|||
<data> |
|||
|
|||
<record id="action_partner_form" model="ir.actions.act_window"> |
|||
<field name="name">All Customers in All Positions</field> |
|||
<field name="type">ir.actions.act_window</field> |
|||
<field name="res_model">res.partner</field> |
|||
<field name="view_type">form</field> |
|||
<field name="view_mode">kanban,tree,form</field> |
|||
<field name="context">{"search_default_customer":1, 'search_show_all_positions': {'is_set': True, 'set_value': True}}</field> |
|||
<field name="search_view_id" ref="base.view_res_partner_filter"/> |
|||
</record> |
|||
|
|||
</data> |
|||
</odoo> |
@ -1,232 +0,0 @@ |
|||
# -*- coding: utf-8 -*- |
|||
############################################################################## |
|||
# |
|||
# OpenERP, Open Source Management Solution |
|||
# Copyright (C) 2013-TODAY OpenERP SA (<http://www.openerp.com>). |
|||
# |
|||
# 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/>. |
|||
# |
|||
############################################################################## |
|||
|
|||
from openerp.osv import fields, orm, expression |
|||
from openerp.tools.translate import _ |
|||
|
|||
|
|||
class res_partner(orm.Model): |
|||
_inherit = 'res.partner' |
|||
|
|||
def _type_selection(self, cr, uid, context=None): |
|||
return [ |
|||
('standalone', _('Standalone Contact')), |
|||
('attached', _('Attached to existing Contact')), |
|||
] |
|||
|
|||
def _get_contact_type(self, cr, uid, ids, field_name, args, context=None): |
|||
result = dict.fromkeys(ids, 'standalone') |
|||
for partner in self.browse(cr, uid, ids, context=context): |
|||
if partner.contact_id: |
|||
result[partner.id] = 'attached' |
|||
return result |
|||
|
|||
_columns = { |
|||
'contact_type': fields.function( |
|||
_get_contact_type, |
|||
type='selection', |
|||
selection=lambda self, *a, **kw: self._type_selection(*a, **kw), |
|||
string='Contact Type', |
|||
required=True, |
|||
select=1, |
|||
store=True, |
|||
), |
|||
'contact_id': fields.many2one( |
|||
'res.partner', |
|||
'Main Contact', |
|||
domain=[ |
|||
('is_company', '=', False), |
|||
('contact_type', '=', 'standalone'), |
|||
], |
|||
), |
|||
'other_contact_ids': fields.one2many( |
|||
'res.partner', |
|||
'contact_id', |
|||
'Others Positions', |
|||
), |
|||
} |
|||
|
|||
_defaults = { |
|||
'contact_type': 'standalone', |
|||
} |
|||
|
|||
def _basecontact_check_context(self, cr, user, mode, context=None): |
|||
""" Remove 'search_show_all_positions' for non-search mode. |
|||
Keeping it in context can result in unexpected behaviour (ex: reading |
|||
one2many might return wrong result - i.e with "attached contact" |
|||
removed even if it's directly linked to a company). |
|||
""" |
|||
context = dict(context or {}) |
|||
if mode != 'search': |
|||
context.pop('search_show_all_positions', None) |
|||
return context |
|||
|
|||
def search( |
|||
self, cr, user, args, offset=0, limit=None, order=None, |
|||
context=None, count=False): |
|||
""" Display only standalone contact matching ``args`` or having |
|||
attached contact matching ``args`` """ |
|||
if context is None: |
|||
context = {} |
|||
if context.get('search_show_all_positions') is False: |
|||
args = expression.normalize_domain(args) |
|||
attached_contact_args = expression.AND( |
|||
(args, [('contact_type', '=', 'attached')]) |
|||
) |
|||
attached_contact_ids = super(res_partner, self).search( |
|||
cr, user, attached_contact_args, context=context |
|||
) |
|||
args = expression.OR(( |
|||
expression.AND(([('contact_type', '=', 'standalone')], args)), |
|||
[('other_contact_ids', 'in', attached_contact_ids)], |
|||
)) |
|||
return super(res_partner, self).search( |
|||
cr, user, args, offset=offset, limit=limit, order=order, |
|||
context=context, count=count |
|||
) |
|||
|
|||
def create(self, cr, user, vals, context=None): |
|||
context = self._basecontact_check_context(cr, user, 'create', context) |
|||
if not vals.get('name') and vals.get('contact_id'): |
|||
vals['name'] = self.browse( |
|||
cr, user, vals['contact_id'], context=context).name |
|||
return super(res_partner, self).create(cr, user, vals, context=context) |
|||
|
|||
def read( |
|||
self, cr, user, ids, fields=None, context=None, |
|||
load='_classic_read'): |
|||
context = self._basecontact_check_context(cr, user, 'read', context) |
|||
return super(res_partner, self).read( |
|||
cr, user, ids, fields=fields, context=context, load=load) |
|||
|
|||
def write(self, cr, user, ids, vals, context=None): |
|||
context = self._basecontact_check_context(cr, user, 'write', context) |
|||
return super( |
|||
res_partner, self).write(cr, user, ids, vals, context=context) |
|||
|
|||
def unlink(self, cr, user, ids, context=None): |
|||
context = self._basecontact_check_context(cr, user, 'unlink', context) |
|||
return super(res_partner, self).unlink(cr, user, ids, context=context) |
|||
|
|||
def _commercial_partner_compute( |
|||
self, cr, uid, ids, name, args, context=None): |
|||
""" Returns the partner that is considered the commercial |
|||
entity of this partner. The commercial entity holds the master data |
|||
for all commercial fields (see :py:meth:`~_commercial_fields`) """ |
|||
result = super(res_partner, self)._commercial_partner_compute( |
|||
cr, uid, ids, name, args, context=context) |
|||
for partner in self.browse(cr, uid, ids, context=context): |
|||
if partner.contact_type == 'attached' and not partner.parent_id: |
|||
result[partner.id] = partner.contact_id.id |
|||
return result |
|||
|
|||
def _contact_fields(self, cr, uid, context=None): |
|||
""" Returns the list of contact fields that are synced from the parent |
|||
when a partner is attached to him. """ |
|||
return ['name', 'title'] |
|||
|
|||
def _contact_sync_from_parent(self, cr, uid, partner, context=None): |
|||
""" Handle sync of contact fields when a new parent contact entity |
|||
is set, as if they were related fields |
|||
""" |
|||
if partner.contact_id: |
|||
contact_fields = self._contact_fields(cr, uid, context=context) |
|||
sync_vals = self._update_fields_values( |
|||
cr, uid, partner.contact_id, contact_fields, context=context |
|||
) |
|||
partner.write(sync_vals) |
|||
|
|||
def update_contact(self, cr, uid, ids, vals, context=None): |
|||
if context is None: |
|||
context = {} |
|||
if context.get('__update_contact_lock'): |
|||
return |
|||
contact_fields = self._contact_fields(cr, uid, context=context) |
|||
contact_vals = dict( |
|||
(field, vals[field]) for field in contact_fields if field in vals |
|||
) |
|||
if contact_vals: |
|||
ctx = dict(context, __update_contact_lock=True) |
|||
self.write(cr, uid, ids, contact_vals, context=ctx) |
|||
|
|||
def _fields_sync(self, cr, uid, partner, update_values, context=None): |
|||
"""Sync commercial fields and address fields from company and to |
|||
children, contact fields from contact and to attached contact |
|||
after create/update, just as if those were all modeled as |
|||
fields.related to the parent |
|||
""" |
|||
super(res_partner, self)._fields_sync( |
|||
cr, uid, partner, update_values, context=context |
|||
) |
|||
contact_fields = self._contact_fields(cr, uid, context=context) |
|||
# 1. From UPSTREAM: sync from parent contact |
|||
if update_values.get('contact_id'): |
|||
self._contact_sync_from_parent(cr, uid, partner, context=context) |
|||
# 2. To DOWNSTREAM: sync contact fields to parent or related |
|||
elif any(field in contact_fields for field in update_values): |
|||
update_ids = [ |
|||
c.id for c in partner.other_contact_ids if not c.is_company |
|||
] |
|||
if partner.contact_id: |
|||
update_ids.append(partner.contact_id.id) |
|||
self.update_contact( |
|||
cr, uid, update_ids, update_values, context=context |
|||
) |
|||
|
|||
def onchange_contact_id(self, cr, uid, ids, contact_id, context=None): |
|||
values = {} |
|||
if contact_id: |
|||
values['name'] = self.browse( |
|||
cr, uid, contact_id, context=context).name |
|||
return {'value': values} |
|||
|
|||
def onchange_contact_type(self, cr, uid, ids, contact_type, context=None): |
|||
values = {} |
|||
if contact_type == 'standalone': |
|||
values['contact_id'] = False |
|||
return {'value': values} |
|||
|
|||
|
|||
class ir_actions_window(orm.Model): |
|||
_inherit = 'ir.actions.act_window' |
|||
|
|||
def read( |
|||
self, cr, user, ids, fields=None, context=None, |
|||
load='_classic_read'): |
|||
action_ids = ids |
|||
if isinstance(ids, (int, long)): |
|||
action_ids = [ids] |
|||
actions = super(ir_actions_window, self).read( |
|||
cr, user, action_ids, fields=fields, context=context, load=load |
|||
) |
|||
for action in actions: |
|||
if action.get('res_model', '') == 'res.partner': |
|||
# By default, only show standalone contact |
|||
action_context = action.get('context', '{}') or '{}' |
|||
if 'search_show_all_positions' not in action_context: |
|||
action['context'] = action_context.replace( |
|||
'{', "{'search_show_all_positions': False,", 1 |
|||
) |
|||
if isinstance(ids, (int, long)): |
|||
if actions: |
|||
return actions[0] |
|||
return False |
|||
return actions |
@ -0,0 +1,4 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from . import res_partner, ir_actions |
@ -0,0 +1,23 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from openerp import models, api |
|||
|
|||
|
|||
class IRActionsWindow(models.Model): |
|||
_inherit = 'ir.actions.act_window' |
|||
|
|||
@api.multi |
|||
def read(self, fields=None, context=None, load='_classic_read'): |
|||
actions = super(IRActionsWindow, self).read(fields=fields, load=load) |
|||
for action in actions: |
|||
if action.get('res_model', '') == 'res.partner': |
|||
# By default, only show standalone contact |
|||
action_context = action.get('context', '{}') or '{}' |
|||
if 'search_show_all_positions' not in action_context: |
|||
action['context'] = action_context.replace( |
|||
'{', |
|||
("{'search_show_all_positions': " |
|||
"{'is_set': True, 'set_value': False},"), |
|||
1) |
|||
return actions |
@ -0,0 +1,161 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from openerp import fields, models, _, api |
|||
from openerp.osv import expression |
|||
|
|||
|
|||
class ResPartner(models.Model): |
|||
_inherit = 'res.partner' |
|||
|
|||
contact_type = fields.Selection( |
|||
[('standalone', _('Standalone Contact')), |
|||
('attached', _('Attached to existing Contact')), |
|||
], |
|||
compute='_get_contact_type', |
|||
required=True, select=1, store=True, |
|||
default='standalone') |
|||
contact_id = fields.Many2one('res.partner', string='Main Contact', |
|||
domain=[('is_company', '=', False), |
|||
('contact_type', '=', 'standalone'), |
|||
], |
|||
) |
|||
other_contact_ids = fields.One2many('res.partner', 'contact_id', |
|||
string='Others Positions') |
|||
|
|||
@api.one |
|||
@api.depends('contact_id') |
|||
def _get_contact_type(self): |
|||
self.contact_type = self.contact_id and 'attached' or 'standalone' |
|||
|
|||
def _basecontact_check_context(self, mode): |
|||
""" Remove 'search_show_all_positions' for non-search mode. |
|||
Keeping it in context can result in unexpected behaviour (ex: reading |
|||
one2many might return wrong result - i.e with "attached contact" |
|||
removed even if it's directly linked to a company). |
|||
Actually, is easier to override a dictionary value to indicate it |
|||
should be ignored... |
|||
""" |
|||
if mode != 'search' \ |
|||
and 'search_show_all_positions' in self.env.context: |
|||
result = self.with_context( |
|||
search_show_all_positions={'is_set': False}) |
|||
else: |
|||
result = self |
|||
return result |
|||
|
|||
@api.model |
|||
def search(self, args, offset=0, limit=None, order=None, count=False): |
|||
""" Display only standalone contact matching ``args`` or having |
|||
attached contact matching ``args`` """ |
|||
if self.env.context.get('search_show_all_positions', {}).get('is_set') \ |
|||
and not self.env.context[ |
|||
'search_show_all_positions']['set_value']: |
|||
args = expression.normalize_domain(args) |
|||
attached_contact_args = expression.AND( |
|||
(args, [('contact_type', '=', 'attached')]) |
|||
) |
|||
attached_contacts = super(ResPartner, self).search( |
|||
attached_contact_args) |
|||
args = expression.OR(( |
|||
expression.AND(([('contact_type', '=', 'standalone')], args)), |
|||
[('other_contact_ids', 'in', attached_contacts.ids)], |
|||
)) |
|||
return super(ResPartner, self).search(args, offset=offset, |
|||
limit=limit, order=order, |
|||
count=count) |
|||
|
|||
@api.model |
|||
def create(self, vals): |
|||
""" When creating, use a modified self to alter the context (see |
|||
comment in _basecontact_check_context). Also, we need to ensure |
|||
that the name on an attached contact is the same as the name on the |
|||
contact it is attached to.""" |
|||
modified_self = self._basecontact_check_context('create') |
|||
if not vals.get('name') and vals.get('contact_id'): |
|||
vals['name'] = modified_self.browse(vals['contact_id']).name |
|||
return super(ResPartner, modified_self).create(vals) |
|||
|
|||
@api.multi |
|||
def read(self, fields=None, load='_classic_read'): |
|||
modified_self = self._basecontact_check_context('read') |
|||
return super(ResPartner, modified_self).read(fields=fields, load=load) |
|||
|
|||
@api.multi |
|||
def write(self, vals): |
|||
modified_self = self._basecontact_check_context('write') |
|||
return super(ResPartner, modified_self).write(vals) |
|||
|
|||
@api.multi |
|||
def unlink(self): |
|||
modified_self = self._basecontact_check_context('unlink') |
|||
return super(ResPartner, modified_self).unlink() |
|||
|
|||
@api.multi |
|||
def _commercial_partner_compute(self, name, args): |
|||
""" Returns the partner that is considered the commercial |
|||
entity of this partner. The commercial entity holds the master data |
|||
for all commercial fields (see :py:meth:`~_commercial_fields`) """ |
|||
result = super(ResPartner, self)._commercial_partner_compute(name, |
|||
args) |
|||
for partner in self: |
|||
if partner.contact_type == 'attached' and not partner.parent_id: |
|||
result[partner.id] = partner.contact_id.id |
|||
return result |
|||
|
|||
def _contact_fields(self): |
|||
""" Returns the list of contact fields that are synced from the parent |
|||
when a partner is attached to him. """ |
|||
return ['name', 'title'] |
|||
|
|||
def _contact_sync_from_parent(self): |
|||
""" Handle sync of contact fields when a new parent contact entity |
|||
is set, as if they were related fields |
|||
""" |
|||
self.ensure_one() |
|||
if self.contact_id: |
|||
contact_fields = self._contact_fields() |
|||
sync_vals = self._update_fields_values(self.contact_id, |
|||
contact_fields) |
|||
self.write(sync_vals) |
|||
|
|||
def update_contact(self, vals): |
|||
if self.env.context.get('__update_contact_lock'): |
|||
return |
|||
contact_fields = self._contact_fields() |
|||
contact_vals = dict( |
|||
(field, vals[field]) for field in contact_fields if field in vals |
|||
) |
|||
if contact_vals: |
|||
self.with_context(__update_contact_lock=True).write(contact_vals) |
|||
|
|||
@api.model |
|||
def _fields_sync(self, partner, update_values): |
|||
"""Sync commercial fields and address fields from company and to |
|||
children, contact fields from contact and to attached contact |
|||
after create/update, just as if those were all modeled as |
|||
fields.related to the parent |
|||
""" |
|||
super(ResPartner, self)._fields_sync(partner, update_values) |
|||
contact_fields = self._contact_fields() |
|||
# 1. From UPSTREAM: sync from parent contact |
|||
if update_values.get('contact_id'): |
|||
partner._contact_sync_from_parent() |
|||
# 2. To DOWNSTREAM: sync contact fields to parent or related |
|||
elif any(field in contact_fields for field in update_values): |
|||
update_ids = [ |
|||
c.id for c in partner.other_contact_ids if not c.is_company |
|||
] |
|||
if partner.contact_id: |
|||
update_ids.append(partner.contact_id.id) |
|||
self.browse(update_ids).update_contact(update_values) |
|||
|
|||
@api.onchange('contact_id') |
|||
def _onchange_contact_id(self): |
|||
if self.contact_id: |
|||
self.name = self.contact_id.name |
|||
|
|||
@api.onchange('contact_type') |
|||
def _onchange_contact_type(self): |
|||
if self.contact_type == 'standalone': |
|||
self.contact_id = False |
@ -1,22 +1,4 @@ |
|||
# -*- coding: utf-8 ⁻*- |
|||
############################################################################## |
|||
# |
|||
# OpenERP, Open Source Business Applications |
|||
# Copyright (C) 2013-TODAY OpenERP S.A. (<http://openerp.com>). |
|||
# |
|||
# 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/>. |
|||
# |
|||
############################################################################## |
|||
# -*- coding: utf-8 -*- |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from . import test_partner_contact_in_several_companies |
Write
Preview
Loading…
Cancel
Save
Reference in new issue