~deneroteam/asperience-openerp-addons/trunk

« back to all changes in this revision

Viewing changes to asperience_fusion/wizard/wizard_fusion.py

  • Committer: David Halgand
  • Date: 2012-05-03 12:50:32 UTC
  • Revision ID: halgandd@asperience.fr-20120503125032-8hlcpowtnd6aijov
update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2011 ASPerience SARL (<http://www.asperience.fr>). All Rights Reserved
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License as published by
 
9
#    the Free Software Foundation, either version 3 of the License, or
 
10
#    (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
from osv import fields, osv
 
23
import os, base64
 
24
from tools import config
 
25
import tools
 
26
import decimal_precision as dp
 
27
 
 
28
 
 
29
class wizard_fusion(osv.osv_memory):
 
30
    _name = "wizard.fusion"
 
31
    _description = "wizard.fusion"
 
32
 
 
33
    _columns = {
 
34
        'name': fields.char('Name', size=64),
 
35
        'partner_ids': fields.many2many('res.partner', "wizard_fusion_partner_rel", "wiz_id", "part_id"),
 
36
        'partner_id': fields.many2one('res.partner','Partner Destination'),
 
37
        'product_ids': fields.many2many('product.product', "wizard_fusion_product_rel", "wiz_id", "prod_id"),
 
38
        'product_id': fields.many2one('product.product','Product Destination'),
 
39
        'diff': fields.text('diff'),
 
40
    }
 
41
    
 
42
    def _get_partner_ids(self, cr, uid, context=None):
 
43
        if context['active_model'] == 'res.partner':
 
44
            return context['active_ids']
 
45
        return False
 
46
    
 
47
    def _get_product_ids(self, cr, uid, context=None):
 
48
        if context['active_model'] == 'product.product':
 
49
            return context['active_ids']
 
50
        return False
 
51
    
 
52
    def _get_name(self, cr, uid, context=None):
 
53
        return context['active_model']
 
54
  
 
55
    def _get_diff(self, cr, uid, context=None):
 
56
        if len(context['active_ids']) >= 1:
 
57
            obj = self.pool.get(context['active_model'])
 
58
            read_objs = obj.read(cr,uid,context['active_ids'])
 
59
            diff = ""
 
60
            for key in sorted(read_objs[0].keys()):
 
61
                ok = True
 
62
                value=read_objs[0][key]
 
63
                for read_obj in read_objs[1:]:
 
64
                    if read_obj[key] != value:
 
65
                        ok = False
 
66
                if not ok:
 
67
                    diff += key.ljust(50)+" --> "+tools.ustr(value).ljust(50)
 
68
                    for read_obj in read_objs[1:]:
 
69
                        diff +="| "+tools.ustr(read_obj[key]).ljust(50)+"\n"
 
70
            return diff
 
71
        return False 
 
72
    
 
73
    _defaults = {
 
74
        'name': _get_name,
 
75
        'partner_ids': _get_partner_ids,
 
76
        'product_ids': _get_product_ids,
 
77
        'diff': _get_diff,
 
78
    }
 
79
    
 
80
    def fusion(self, cr, uid, ids, context=None):
 
81
        for wizard in self.browse(cr,uid,ids,context):
 
82
            log = ""
 
83
            obj_table = wizard.name.replace('.','_')
 
84
            obj_ids = False
 
85
            obj_id = False
 
86
            if wizard.name == "res.partner":
 
87
                obj_ids = wizard.partner_ids
 
88
                obj_id = wizard.partner_id
 
89
                
 
90
                sql = " select b.relname as table,c.relname,d.attname as column \
 
91
                        from pg_constraint a \
 
92
                             join pg_class b on a.conrelid=b.oid \
 
93
                             join pg_class c on a.confrelid=c.oid \
 
94
                             join pg_attribute d on d.attnum = any(a.conkey) and b.oid=d.attrelid \
 
95
                             where c.relname = '"+obj_table+"'"
 
96
                             
 
97
                cr.execute(sql)
 
98
                res = cr.dictfetchall()
 
99
                for obj in obj_ids:
 
100
                    if obj.id != obj_id.id:
 
101
                        for table in res:
 
102
                            if table['table'][-4:] == '_rel':
 
103
                                sql = "select b.relname as table,c.relname,d.attname as column \
 
104
                                from pg_constraint a \
 
105
                                     join pg_class b on a.conrelid=b.oid \
 
106
                                     join pg_class c on a.confrelid=c.oid \
 
107
                                     join pg_attribute d on d.attnum = any(a.conkey) and b.oid=d.attrelid \
 
108
                                where b.relname = '%s'" % table['table']
 
109
                                cr.execute(sql)
 
110
                                res2 = cr.dictfetchall()
 
111
                                if len(res2) == 2:
 
112
                                    field1 = res2[0]['column']
 
113
                                    field2 = res2[1]['column']  
 
114
                                    sql = """delete from %s where oid in (
 
115
                                                select b.oid
 
116
                                                from %s b
 
117
                                                where exists( select 1
 
118
                                                from %s a
 
119
                                                where a.oid != b.oid and
 
120
                                                a.%s = b.%s and
 
121
                                                a.%s = b.%s and
 
122
                                                a.oid < b.oid )
 
123
                                    )""" % (table['table'],table['table'],table['table'],field1,field1,field2,field2)
 
124
                                    cr.execute(sql)
 
125
                                    sql = """DELETE FROM %s WHERE %s = %s """%(table['table'], table['column'], obj.id)
 
126
                                    if cr.execute(sql) != None:
 
127
                                        log += "Erreur sur : "+str(sql)+","
 
128
                            else:
 
129
                                sql = """UPDATE %s SET %s = %s WHERE %s = %s """%(table['table'], table['column'], obj_id.id,table['column'], obj.id)
 
130
                                if cr.execute(sql) != None:
 
131
                                    log += "Erreur sur : "+str(sql)+","
 
132
                                     
 
133
                        obj.write({'active':False})
 
134
            elif wizard.name == "product.product":
 
135
                obj_ids = wizard.product_ids
 
136
                obj_id = wizard.product_id
 
137
                tmpl_id = wizard.product_id.product_tmpl_id
 
138
                
 
139
                sql = " select b.relname as table,c.relname,d.attname as column \
 
140
                        from pg_constraint a \
 
141
                             join pg_class b on a.conrelid=b.oid \
 
142
                             join pg_class c on a.confrelid=c.oid \
 
143
                             join pg_attribute d on d.attnum = any(a.conkey) and b.oid=d.attrelid \
 
144
                             where c.relname = '"+"product_template"+"'"
 
145
                             
 
146
                cr.execute(sql)
 
147
                res = cr.dictfetchall()
 
148
                for obj in obj_ids:
 
149
                    if obj.id != obj_id.id:
 
150
                        for table in res:
 
151
                            if table['table'] == 'product_product' and table['column'] == 'product_tmpl_id':
 
152
                                continue
 
153
                                
 
154
                            if table['table'][-4:] == '_rel':
 
155
                                sql = "select b.relname as table,c.relname,d.attname as column \
 
156
                                from pg_constraint a \
 
157
                                     join pg_class b on a.conrelid=b.oid \
 
158
                                     join pg_class c on a.confrelid=c.oid \
 
159
                                     join pg_attribute d on d.attnum = any(a.conkey) and b.oid=d.attrelid \
 
160
                                where b.relname = '%s'" % table['table']
 
161
                                cr.execute(sql)
 
162
                                res2 = cr.dictfetchall()
 
163
                                if len(res2) == 2:
 
164
                                    field1 = res2[0]['column']
 
165
                                    field2 = res2[1]['column']  
 
166
                                    sql = """delete from %s where oid in (
 
167
                                                select b.oid
 
168
                                                from %s b
 
169
                                                where exists( select 1
 
170
                                                from %s a
 
171
                                                where a.oid != b.oid and
 
172
                                                a.%s = b.%s and
 
173
                                                a.%s = b.%s and
 
174
                                                a.oid < b.oid )
 
175
                                    )""" % (table['table'],table['table'],table['table'],field1,field1,field2,field2)
 
176
                                    cr.execute(sql)
 
177
                                    sql = """DELETE FROM %s WHERE %s = %s """%(table['table'], table['column'], obj.id)
 
178
                                    if cr.execute(sql) != None:
 
179
                                        log += "Erreur sur : "+str(sql)+","
 
180
                            else:
 
181
                                sql = """UPDATE %s SET %s = %s WHERE %s = %s """%(table['table'], table['column'], tmpl_id.id,table['column'], obj.product_tmpl_id.id)
 
182
                                if cr.execute(sql) != None:
 
183
                                    log += "Erreur sur : "+str(sql)+","
 
184
                                        
 
185
                                
 
186
                sql = " select b.relname as table,c.relname,d.attname as column \
 
187
                        from pg_constraint a \
 
188
                             join pg_class b on a.conrelid=b.oid \
 
189
                             join pg_class c on a.confrelid=c.oid \
 
190
                             join pg_attribute d on d.attnum = any(a.conkey) and b.oid=d.attrelid \
 
191
                             where c.relname = '"+obj_table+"'"
 
192
                             
 
193
                cr.execute(sql)
 
194
                res = cr.dictfetchall()
 
195
                for obj in obj_ids:
 
196
                    if obj.id != obj_id.id:
 
197
                        for table in res:
 
198
                            if table['table'][-4:] == '_rel':
 
199
                                sql = "select b.relname as table,c.relname,d.attname as column \
 
200
                                from pg_constraint a \
 
201
                                     join pg_class b on a.conrelid=b.oid \
 
202
                                     join pg_class c on a.confrelid=c.oid \
 
203
                                     join pg_attribute d on d.attnum = any(a.conkey) and b.oid=d.attrelid \
 
204
                                where b.relname = '%s'" % table['table']
 
205
                                cr.execute(sql)
 
206
                                res2 = cr.dictfetchall()
 
207
                                if len(res2) == 2:
 
208
                                    field1 = res2[0]['column']
 
209
                                    field2 = res2[1]['column']  
 
210
                                    sql = """delete from %s where oid in (
 
211
                                                select b.oid
 
212
                                                from %s b
 
213
                                                where exists( select 1
 
214
                                                from %s a
 
215
                                                where a.oid != b.oid and
 
216
                                                a.%s = b.%s and
 
217
                                                a.%s = b.%s and
 
218
                                                a.oid < b.oid )
 
219
                                    )""" % (table['table'],table['table'],table['table'],field1,field1,field2,field2)
 
220
                                    cr.execute(sql)
 
221
                                sql = """DELETE FROM %s WHERE %s = %s """%(table['table'], table['column'], obj.id)
 
222
                                if cr.execute(sql) != None:
 
223
                                    log += "Erreur sur : "+str(sql)+","
 
224
                            else:
 
225
                                sql = """UPDATE %s SET %s = %s WHERE %s = %s """%(table['table'], table['column'], obj_id.id,table['column'], obj.id)
 
226
                                if cr.execute(sql) != None:
 
227
                                    log += "Erreur sur : "+str(sql)+","
 
228
                       
 
229
                        obj.write({'active':False})
 
230
            self.log(cr,uid,wizard.id,log)
 
231
                    
 
232
        return {'type': 'ir.actions.act_window_close'}
 
233
wizard_fusion ()
 
234
 
 
235
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
 
b'\\ No newline at end of file'