1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
25
from tools import UpdateableStr, UpdateableDict
27
_MERGE_FORM = UpdateableStr()
28
_MERGE_FIELDS = UpdateableDict()
30
partner_form = '''<?xml version="1.0"?>
31
<form string="Merge Two Partners">
32
<field name="partner_id1" />
33
<field name="partner_id2" />
37
'partner_id1': {'string': 'First partner', 'type': 'many2one', 'relation':'res.partner', 'help': 'Select first partner to merge', 'required': True},
38
'partner_id2': {'string': 'Second partner', 'type': 'many2one', 'relation':'res.partner', 'help': 'Select second partner to merge', 'required': True},
41
class wizard_merge_partners(wizard.interface):
43
def _build_form(self, cr, uid, data, context):
51
pool = pooler.get_pool(cr.dbname)
52
quest_form='''<?xml version="1.0"?>
53
<form string="%s">''' % _('Merge Partners')
54
partner_ids = ",".join(map(str, [data['form']['partner_id1'], data['form']['partner_id2']]))
55
fields_ids = pool.get('ir.model.fields').search(cr, uid, [('model', '=', 'res.partner')], context=context)
56
fields_data = pool.get('ir.model.fields').read(cr, uid, fields_ids, ['name', 'field_description', 'ttype', 'relation'], context=context)
57
for field in fields_data:
58
filter_name[str(field['name'])] = str(field['field_description'])
59
filter_type[str(field['name'])] = [str(field['ttype']), str(field['relation'])]
60
if field['ttype'] == 'many2many':
61
m2m_list.append(str(field['name']))
64
partner_data = pool.get('res.partner').read(cr, uid, [data['form']['partner_id1'], data['form']['partner_id2']], m2m_list ,context=context)
66
m2m_dict[m2m] = [(6, 0, partner_data[0][m2m] + partner_data[1][m2m])]
68
cr.execute('SELECT * FROM res_partner AS part WHERE part.id in ('+partner_ids+')')
69
result = cr.dictfetchall()
70
for part1 in result[0]:
71
for part2 in result[1]:
73
if part1 not in ('create_date', 'write_date', 'id', 'write_uid'):# to be check
74
if result[0][part1] is not None and result[1][part2] is not None and result[0][part1] == result[1][part2]:
75
res[part1] = str(result[0][part1])
76
elif (result[0][part1] is not None or result[1][part2] is not None) and result[0][part1] != result[1][part2]:
77
quest_form = quest_form + '<field name="%s"/><newline/>' % (part1,)
80
if result[0][part1] in (True, False):
81
result[0][part1] = str(result[0][part1])
82
if result[1][part2] in (True, False):
83
result[1][part2] = str(result[1][part2])
84
if result[0][part1] is None:
85
result[0][part1] = 'None'
87
if result[1][part2] is None:
88
result[1][part2] = 'None'
94
if filter_type[part1][0] == 'many2one':
95
relation = filter_type[part1][1].replace('.','_')
96
if result[0][part1] != 'None':
97
cr.execute("select * from "+relation+" where id=%s"%(str(result[0][part1])))
98
first_m2o = cr.dictfetchone()
99
if first_m2o.has_key('name') and first_m2o['name']:
101
y = first_m2o['name']
102
if result[1][part2] != 'None':
103
cr.execute("select * from "+relation+" where id=%s"%(str(result[1][part2])))
104
second_m2o = cr.dictfetchone()
105
if second_m2o.has_key('name') and second_m2o['name']:
107
b = second_m2o['name']
109
quest_fields['%s' % (part1,)] = {'string': filter_name[part1], 'type': 'selection', 'selection':[(a, b), ('','None')]}
111
quest_fields['%s' % (part1,)] = {'string': filter_name[part1], 'type': 'selection', 'selection':[(x, y), ('','None')]}
113
quest_fields['%s' % (part1,)] = {'string': filter_name[part1], 'type': 'selection', 'selection':[(x, y),(a, b)],}
115
quest_form = quest_form + '''</form>'''
116
_MERGE_FORM. __init__(quest_form)
117
_MERGE_FIELDS.__init__(quest_fields)
118
return {'res': res, 'm2m_dict': m2m_dict}
120
def _create_partner(self, cr, uid, data, context):
121
pool = pooler.get_pool(cr.dbname)
122
res = data['form']['res']
123
part1 = data['form']['partner_id1']
124
part2 = data['form']['partner_id2']
125
data['form'].pop('res')
126
data['form'].pop('partner_id1')
127
data['form'].pop('partner_id2')
128
res.update(data['form'])
129
res.update(data['form']['m2m_dict'])
130
for key, val in res.items():
131
if val in ('True', 'False'):
133
part_id = pool.get('res.partner').create(cr, uid, res, context)
135
# For one2many fields on res.partner
136
cr.execute("select name, model from ir_model_fields where relation='res.partner' and ttype not in ('many2many', 'one2many');")
137
for name, model_raw in cr.fetchall():
138
model = model_raw.replace('.','_')
139
cr.execute("update "+model+" set "+name+"="+str(part_id)+" where "+str(name)+" in ("+str(part1)+", "+str(part2)+")")
140
pool.get('res.partner').write(cr, uid, [part1, part2], {'active': False})
146
'result': {'type': 'form', 'arch': partner_form, 'fields': partner_fields, 'state': [('end', 'Cancel'), ('next', 'Next')]}
149
'actions': [_build_form],
150
'result':{'type': 'form', 'arch': _MERGE_FORM, 'fields': _MERGE_FIELDS, 'state': [('end', 'Cancel'), ('next_1', 'Next')]}
153
'actions': [_create_partner],
154
'result': {'type': 'state', 'state': 'end'}
158
wizard_merge_partners('base_partner.merge')
160
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
b'\\ No newline at end of file'