[IMP]: Add attachment as Different module.
[odoo/odoo.git] / addons / import_sugarcrm / import_sugarcrm.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (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 Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21 from osv import fields, osv
22 from operator import itemgetter
23 import sugar
24 import sugarcrm_fields_mapping
25 from tools.translate import _
26 import pprint
27 pp = pprint.PrettyPrinter(indent=4)
28
29 def create_mapping(obj, cr, uid, res_model, open_id, sugar_id, context):
30     model_data = {
31         'name':  sugar_id,
32         'model': res_model,
33         'module': 'sugarcrm_import',
34         'res_id': open_id
35     }
36     model_obj = obj.pool.get('ir.model.data')
37     model_obj.create(cr, uid, model_data, context=context)
38     return True
39
40 def find_mapped_id(obj, cr, uid, res_model, sugar_id, context):
41     model_obj = obj.pool.get('ir.model.data')
42     return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context)
43
44 def get_all(sugar_obj, cr, uid, model, sugar_val, context=None):
45        models = sugar_obj.pool.get(model)
46        model_code = sugar_val[0:2]
47        all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', model_code.upper())]) 
48        output = sorted([(o.id, o.name)
49                 for o in models.browse(cr, uid, all_model_ids,
50                                        context=context)],
51                key=itemgetter(1))
52        return output
53
54 def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
55     """Get states or create new state"""
56     state_id = False
57     res_country_state_obj = sugar_obj.pool.get('res.country.state')
58     
59     state = get_all(sugar_obj,
60         cr, uid, 'res.country.state', sugar_val, context=context)
61     if state:
62         state_id = state and state[0][0]
63     else:
64        state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
65     return state_id   
66
67 def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None):
68     """Get Country or Create new country"""
69     res_country_obj = sugar_obj.pool.get('res.country')
70     country_id = False
71     country_code = sugar_country_val[0:2]
72     country = get_all(sugar_obj,
73         cr, uid, 'res.country', sugar_country_val, context=context)
74     if country:
75         country_id = country and country[0][0] 
76     else:
77         country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})  
78     return country_id
79
80 def import_partner_address(sugar_obj, cr, uid, context=None):
81     if not context:
82         context = {}
83     map_partner_address = {
84              'id': 'id',              
85              'name': ['first_name', 'last_name'],
86             'phone': 'phone_work',
87             'mobile': 'phone_mobile',
88             'fax': 'phone_fax',
89             'function': 'title',
90             'street': 'primary_address_street',
91             'zip': 'primary_address_postalcode',
92             'city': 'primary_address_city',
93             'country_id.id': 'country_id.id',
94             'state_id.id': 'state_id.id'
95             }
96     address_obj = sugar_obj.pool.get('res.partner.address')
97     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
98     sugar_data = sugar.search(PortType, sessionid, 'Contacts')
99     for val in sugar_data:
100         if val.get('primary_address_country'):
101             country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
102             state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
103             val['country_id.id'] =  country_id
104             val['state_id.id'] =  state        
105         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address)
106         address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
107     return True
108     
109 def get_users_department(sugar_obj, cr, uid, val, context=None):
110     if not context:
111        context={}
112     department_id = False       
113     department_obj = sugar_obj.pool.get('hr.department')
114     department_ids = department_obj.search(cr, uid, [('name', '=', val)])
115     if department_ids:
116         department_id = department_ids[0]
117     elif val:
118         department_id = department_obj.create(cr, uid, {'name': val})
119     return department_id 
120
121 def import_users(sugar_obj, cr, uid, context=None):
122     if not context:
123         context = {}
124     department_id = False        
125     map_user = {'id' : 'id', 
126              'name': ['first_name', 'last_name'],
127             'login': 'user_name',
128             'context_lang' : 'context_lang',
129             'password' : 'password',
130             '.id' : '.id',
131             'context_department_id.id': 'context_department_id.id',
132             } 
133     user_obj = sugar_obj.pool.get('res.users')
134     PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
135     sugar_data = sugar.search(PortType,sessionid, 'Users')
136     for val in sugar_data:
137         user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))])
138         if user_ids: 
139             val['.id'] = str(user_ids[0])
140         else:
141             val['password'] = 'sugarcrm' #default password for all user
142         department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
143         val['context_department_id.id'] = department_id     
144         val['context_lang'] = context.get('lang','en_US')
145         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user)
146         #All data has to be imported separatly because they don't have the same field
147         user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
148     return True
149
150 def get_lead_status(surgar_obj, cr, uid, sugar_val,context=None):
151     if not context:
152         context = {}
153     stage_id = False
154     stage_dict = {'status': #field in the sugarcrm database
155         { #Mapping of sugarcrm stage : openerp opportunity stage
156             'New' : 'New',
157             'Assigned':'Qualification',
158             'In Progress': 'Proposition',
159             'Recycled': 'Negotiation',
160             'Dead': 'Lost'
161         },}
162     stage = stage_dict['status'].get(sugar_val['status'], '')
163     stage_pool = surgar_obj.pool.get('crm.case.stage')
164     stage_ids = stage_pool.search(cr, uid, [('type', '=', 'lead'), ('name', '=', stage)])
165     for stage in stage_pool.browse(cr, uid, stage_ids, context):
166         stage_id = stage.id
167     return stage_id
168
169 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
170     if not context:
171         context = {}
172     state = False
173     state_dict = {'status': #field in the sugarcrm database
174         { #Mapping of sugarcrm stage : openerp opportunity stage
175             'New' : 'draft',
176             'Assigned':'open',
177             'In Progress': 'open',
178             'Recycled': 'cancel',
179             'Dead': 'done'
180         },}
181     state = state_dict['status'].get(sugar_val['status'], '')
182     return state
183
184 def get_opportunity_status(surgar_obj, cr, uid, sugar_val,context=None):
185     if not context:
186         context = {}
187     stage_id = False
188     stage_dict = { 'sales_stage':
189             {#Mapping of sugarcrm stage : openerp opportunity stage Mapping
190                'Need Analysis': 'New',
191                'Closed Lost': 'Lost',
192                'Closed Won': 'Won',
193                'Value Proposition': 'Proposition',
194                 'Negotiation/Review': 'Negotiation'
195             },
196     }
197     stage = stage_dict['sales_stage'].get(sugar_val['sales_stage'], '')
198     stage_pool = surgar_obj.pool.get('crm.case.stage')
199     stage_ids = stage_pool.search(cr, uid, [('type', '=', 'opportunity'), ('name', '=', stage)])
200     for stage in stage_pool.browse(cr, uid, stage_ids, context):
201         stage_id = stage.id
202     return stage_id
203
204 def get_user_address(sugar_obj, cr, uid, val, context=None):
205     address_obj = sugar_obj.pool.get('res.partner.address')
206     map_user_address = {
207     'name': ['first_name', 'last_name'],
208     'city': 'address_city',
209     'country_id': 'country_id',
210     'state_id': 'state_id',
211     'street': 'address_street',
212     'zip': 'address_postalcode',
213     }
214     address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') +' '+ val.get('last_name'))])
215     if val.get('address_country'):
216         country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
217         state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
218         val['country_id'] =  country_id
219         val['state_id'] =  state_id
220     fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address)
221     dict_val = dict(zip(fields,datas))
222     if address_ids:
223         address_obj.write(cr, uid, address_ids, dict_val)
224     else:        
225         new_address_id = address_obj.create(cr,uid, dict_val)
226         return new_address_id
227     return True
228
229 def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
230         address_obj = sugar_obj.pool.get('res.partner.address')
231         new_address_id = False
232         if type == 'invoice':
233             type_address = 'billing'
234         else:
235             type_address = 'shipping'     
236     
237         map_partner_address.update({
238             'street': type_address + '_address_street',
239             'zip': type_address +'_address_postalcode',
240             'city': type_address +'_address_city',
241              'country_id': 'country_id',
242              'type': 'type',
243             })
244         val['type'] = type
245         if val.get(type_address +'_address_country'):
246             country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context)
247             state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context)
248             val['country_id'] =  country_id
249             val['state_id'] =  state
250         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address)
251         #Convert To list into Dictionary(Key, val). value pair.
252         dict_val = dict(zip(fields,datas))
253         new_address_id = address_obj.create(cr,uid, dict_val)
254         return new_address_id
255     
256 def get_address(sugar_obj, cr, uid, val, context=None):
257     map_partner_address={}
258     address_id=[]
259     address_obj = sugar_obj.pool.get('res.partner.address')
260     address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
261     if address_ids:
262         return address_ids 
263     else:
264         map_partner_address = {
265             'id': 'id',                    
266             'name': 'name',
267             'partner_id/id': 'account_id',
268             'phone': 'phone_office',
269             'mobile': 'phone_mobile',
270             'fax': 'phone_fax',
271             'type': 'type',
272             }
273         if val.get('billing_address_street'):
274             address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
275             
276         if val.get('shipping_address_street'):
277             address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
278         return address_id
279     return True
280
281 def import_partners(sugar_obj, cr, uid, context=None):
282     if not context:
283         context = {}
284     map_partner = {
285                 'id': 'id',
286                 'name': 'name',
287                 'website': 'website',
288                 'user_id/id': 'assigned_user_id',
289                 'ref': 'sic_code',
290                 'comment': ['description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
291                 'customer': 'customer',
292                 'supplier': 'supplier', 
293                 }
294     partner_obj = sugar_obj.pool.get('res.partner')
295     address_obj = sugar_obj.pool.get('res.partner.address')
296     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
297     sugar_data = sugar.search(PortType, sessionid, 'Accounts')
298     for val in sugar_data:
299         add_id = get_address(sugar_obj, cr, uid, val, context)
300         if val.get('account_type') in  ('Customer', 'Prospect', 'Other'):
301             val['customer'] = '1'
302         else:
303             val['supplier'] = '1'
304         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner)
305         partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
306         for address in  address_obj.browse(cr,uid,add_id):
307             data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
308             if data_id:
309                 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})                
310     return True
311
312 def get_category(sugar_obj, cr, uid, model, name, context=None):
313     categ_id = False
314     categ_obj = sugar_obj.pool.get('crm.case.categ')
315     categ_ids = categ_obj.search(cr, uid, [('object_id.model','=',model), ('name', 'like', name)] )
316     if categ_ids:
317          categ_id = categ_ids[0]
318     else:
319          categ_id = categ_obj.create(cr, uid, {'name': name, 'object_id.model': model})
320     return categ_id     
321
322 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
323     
324     alarm_dict = {'60': '1 minute before',
325                   '300': '5 minutes before',
326                   '600': '10 minutes before',
327                   '900': '15 minutes before',
328                   '1800':'30 minutes before',
329                   '3600': '1 hour before',
330      }
331     alarm_id = False
332     alarm_obj = sugar_obj.pool.get('res.alarm')
333     if alarm_dict.get(val):
334         alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
335         for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
336             alarm_id = alarm.id
337     return alarm_id 
338     
339 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
340     if not context:
341         context = {}
342     state = False
343     state_dict = {'status': #field in the sugarcrm database
344         { #Mapping of sugarcrm stage : openerp meeting stage
345             'Planned' : 'draft',
346             'Held':'open',
347             'Not Held': 'draft',
348         },}
349     state = state_dict['status'].get(val, '')
350     return state    
351
352 def get_task_state(sugar_obj, cr, uid, val, context=None):
353     if not context:
354         context = {}
355     state = False
356     state_dict = {'status': #field in the sugarcrm database
357         { #Mapping of sugarcrm stage : openerp meeting stage
358             'Completed' : 'done',
359             'Not Started':'draft',
360             'In Progress': 'open',
361             'Pending Input': 'draft',
362             'deferred': 'cancel'
363         },}
364     state = state_dict['status'].get(val, '')
365     return state    
366
367 def get_project_state(sugar_obj, cr, uid, val,context=None):
368     if not context:
369         context = {}
370     state = False
371     state_dict = {'status': #field in the sugarcrm database
372         { #Mapping of sugarcrm staus : openerp Projects state
373             'Draft' : 'draft',
374             'In Review': 'open',
375             'Published': 'close',
376         },}
377     state = state_dict['status'].get(val, '')
378     return state    
379
380 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
381     if not context:
382         context = {}
383     state = False
384     state_dict = {'status': #field in the sugarcrm database
385         { #Mapping of sugarcrm status : openerp Porject Tasks state
386              'Not Started': 'draft',
387              'In Progress': 'open',
388              'Completed': 'done',
389             'Pending Input': 'pending',
390             'Deferred': 'cancelled',
391         },}
392     state = state_dict['status'].get(val, '')
393     return state    
394
395 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
396     if not context:
397         context = {}
398     priority = False
399     priority_dict = {'priority': #field in the sugarcrm database
400         { #Mapping of sugarcrm status : openerp Porject Tasks state
401             'High': '0',
402             'Medium': '2',
403             'Low': '3'
404         },}
405     priority = priority_dict['priority'].get(val, '')
406     return priority    
407
408
409 def get_account(sugar_obj, cr, uid, val, context=None):
410     if not context:
411         context = {}
412     partner_id = False    
413     partner_address_id = False
414     model_obj = sugar_obj.pool.get('ir.model.data')
415     address_obj = sugar_obj.pool.get('res.partner.address')
416     if val.get('parent_type') == 'Accounts':
417         model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
418         if model_ids:
419             model = model_obj.browse(cr, uid, model_ids)[0]
420             partner_id = model.res_id
421             address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
422             partner_address_id = address_ids and address_ids[0]
423             
424     if val.get('parent_type') == 'Contacts':
425         model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
426         for model in model_obj.browse(cr, uid, model_ids):
427             partner_address_id = model.res_id
428             address_id = address_obj.browse(cr, uid, partner_address_id)
429             partner_id = address_id and address_id.partner_id or False
430     return partner_id, partner_address_id                             
431
432 def import_tasks(sugar_obj, cr, uid, context=None):
433     if not context:
434         context = {}
435     map_task = {'id' : 'id',
436                 'name': 'name',
437                 'date': 'date_entered',
438                 'user_id/id': 'assigned_user_id',
439                 'categ_id/.id': 'categ_id/.id',
440                 'partner_id/.id': 'partner_id/.id',
441                 'partner_address_id/.id': 'partner_address_id/.id',
442                 'state': 'state'
443     }
444     meeting_obj = sugar_obj.pool.get('crm.meeting')
445     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
446     categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
447     sugar_data = sugar.search(PortType, sessionid, 'Tasks')
448     for val in sugar_data:
449         partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
450         if not partner_xml_id:
451             raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
452         partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
453         val['partner_id/.id'] = partner_id
454         val['partner_address_id/.id'] = partner_address_id
455         val['categ_id/.id'] = categ_id
456         val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context=None)
457         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task)
458         meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
459     return True    
460     
461 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
462     if not context:
463         context = {}
464     model_obj = sugar_obj.pool.get('ir.model.data')
465     att_obj = sugar_obj.pool.get('calendar.attendee')
466     meeting_obj = sugar_obj.pool.get('crm.meeting')
467     user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
468     for user in user_dict: 
469         user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
470         user_resource_id = model_obj.browse(cr, uid, user_model_ids)        
471         if user_resource_id:
472             user_id = user_resource_id[0].res_id 
473             attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
474             if attend_ids:
475                  attendees = attend_ids[0]
476             else:      
477                 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
478             meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
479             meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
480             if meeting_xml_id:
481                 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})       
482     return True   
483     
484 def import_meetings(sugar_obj, cr, uid, context=None):
485     if not context:
486         context = {}
487     map_meeting = {'id' : 'id',
488                     'name': 'name',
489                     'date': 'date_start',
490                     'duration': ['duration_hours', 'duration_minutes'],
491                     'location': 'location',
492                     'alarm_id/.id': 'alarm_id/.id',
493                     'user_id/id': 'assigned_user_id',
494                     'partner_id/.id':'partner_id/.id',
495                     'partner_address_id/.id':'partner_address_id/.id',
496                     'state': 'state'
497     }
498     meeting_obj = sugar_obj.pool.get('crm.meeting')
499     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
500     sugar_data = sugar.search(PortType, sessionid, 'Meetings')
501     for val in sugar_data:
502         partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
503         val['partner_id/.id'] = partner_id
504         val['partner_address_id/.id'] = partner_address_id
505         val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
506         val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
507         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting)
508         meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
509         get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
510     return True    
511
512 def get_calls_state(sugar_obj, cr, uid, val,context=None):
513     if not context:
514         context = {}
515     state = False
516     state_dict = {'status': #field in the sugarcrm database
517         { #Mapping of sugarcrm stage : openerp calls stage
518             'Planned' : 'open',
519             'Held':'done',
520             'Not Held': 'pending',
521         },}
522     state = state_dict['status'].get(val, '')
523     return state   
524
525 def import_calls(sugar_obj, cr, uid, context=None):
526     if not context:
527         context = {}
528     map_calls = {'id' : 'id',
529                     'name': 'name',
530                     'date': 'date_start',
531                     'duration': ['duration_hours', 'duration_minutes'],
532                     'user_id/id': 'assigned_user_id',
533                     'partner_id/.id': 'partner_id/.id',
534                     'partner_address_id/.id': 'partner_address_id/.id',
535                     'categ_id/.id': 'categ_id/.id',
536                    'state': 'state',
537     }
538     phonecall_obj = sugar_obj.pool.get('crm.phonecall')
539     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
540     sugar_data = sugar.search(PortType, sessionid, 'Calls')
541     for val in sugar_data:
542         categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))         
543         val['categ_id/.id'] = categ_id
544         partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
545         val['partner_id/.id'] = partner_id
546         val['partner_address_id/.id'] = partner_address_id
547         val['state'] =  get_calls_state(sugar_obj, cr, uid, val.get('status'), context)  
548         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls)
549         phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
550     return True
551     
552 def import_resources(sugar_obj, cr, uid, context=None):
553     if not context:
554         context = {}
555     map_resource = {'id' : 'user_hash',
556                     'name': ['first_name', 'last_name'],
557     }
558     resource_obj = sugar_obj.pool.get('resource.resource')
559     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
560     sugar_data = sugar.search(PortType, sessionid, 'Employees')
561     for val in sugar_data:
562         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource)
563         resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
564     return True    
565
566 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
567     if not context:
568         context = {}
569     priority = False
570     priority_dict = {'priority': #field in the sugarcrm database
571         { #Mapping of sugarcrm priority : openerp bugs priority
572             'Urgent': '1',
573             'High': '2',
574             'Medium': '3',
575             'Low': '4'
576         },}
577     priority = priority_dict['priority'].get(val, '')
578     return priority    
579
580 def get_bug_state(sugar_obj, cr, uid, val,context=None):
581     if not context:
582         context = {}
583     state = False
584     state_dict = {'status': #field in the sugarcrm database
585         { #Mapping of sugarcrm status : openerp Bugs state
586             'New' : 'draft',
587             'Assigned':'open',
588             'Closed': 'done',
589             'Pending': 'pending',
590             'Rejected': 'cancel',
591         },}
592     state = state_dict['status'].get(val, '')
593     return state
594
595 def import_bug(sugar_obj, cr, uid, context=None):
596     if not context:
597         context = {}
598     map_resource = {'id' : 'id',
599                     'name': 'name',
600                     'categ_id.id': 'categ_id.id',
601                     'priority':'priority',
602                     'description': 'description',
603                     'state': 'state'
604     }
605     issue_obj = sugar_obj.pool.get('project.issue')
606     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
607     sugar_data = sugar.search(PortType, sessionid, 'Bugs')
608     for val in sugar_data:
609         val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
610         val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
611         val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
612         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource)
613         issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
614     return True    
615
616 def get_job_id(sugar_obj, cr, uid, val, context=None):
617     if not context:
618         context={}
619     job_id = False    
620     job_obj = sugar_obj.pool.get('hr.job')        
621     job_ids = job_obj.search(cr, uid, [('name', '=', val)])
622     if job_ids:
623         job_id = job_ids[0]
624     else:
625         job_id = job_obj.create(cr, uid, {'name': val})
626     return job_id
627     
628 def get_attachment(sugar_obj, cr, uid, val, model, File, context=None):
629     if not context:
630         context = {}
631     attachment_obj = sugar_obj.pool.get('ir.attachment')
632     model_obj = sugar_obj.pool.get('ir.model.data')
633     mailgate_obj = sugar_obj.pool.get('mailgate.message')
634     new_attachment_id = attachment_obj.create(cr, uid, {'name': val.get('name'), 'datas': File, 'res_id': val['res_id'],'res_model': val['model']})
635     message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
636     message_xml_id = model_obj.browse(cr, uid, message_model_ids)
637     if message_xml_id:
638          mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})             
639     return True    
640     
641 def import_history(sugar_obj, cr, uid, context=None):
642     if not context:
643         context = {}
644     map_attachment = {'id' : 'id',
645                       'name':'name',
646                       'date':'date_entered',
647                       'user_id/id': 'assigned_user_id',
648                       'description': 'description_html',
649                       'res_id': 'res_id',
650                       'model': 'model',
651     }
652     mailgate_obj = sugar_obj.pool.get('mailgate.message')
653     model_obj =  sugar_obj.pool.get('ir.model.data')
654     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
655     sugar_data = sugar.search(PortType, sessionid, 'Notes')
656     for val in sugar_data:
657          File = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
658          model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id'))])
659          for model in model_obj.browse(cr, uid, model_ids):
660             val['res_id'] = model.res_id
661             val['model'] = model.model
662          fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment)            
663          mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
664          get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, context)
665     return True       
666     
667 def import_employees(sugar_obj, cr, uid, context=None):
668     if not context:
669         context = {}
670     map_employee = {'id' : 'user_hash',
671                     'resource_id/.id': 'resource_id/.id',
672                     'name': ['first_name', 'last_name'],
673                     'work_phone': 'phone_work',
674                     'mobile_phone':  'phone_mobile',
675                     'user_id/name': ['first_name', 'last_name'], 
676                     'address_home_id/.id': 'address_home_id/.id',
677                     'notes': 'description',
678                     #TODO: Creation of Employee create problem.
679                  #   'coach_id/id': 'reports_to_id',
680                     'job_id/.id': 'job_id/.id'
681     }
682     employee_obj = sugar_obj.pool.get('hr.employee')
683     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
684     sugar_data = sugar.search(PortType, sessionid, 'Employees')
685     for val in sugar_data:
686         address_id = get_user_address(sugar_obj, cr, uid, val, context)
687         val['address_home_id/.id'] = address_id
688         model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
689         resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
690         if resource_id:
691             val['resource_id/.id'] = resource_id[0].res_id
692         val['job_id/.id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
693         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee)
694         employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
695     return True
696
697 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
698     if not context:
699         context = {}
700     contact_title_obj = sugar_obj.pool.get('res.partner.title')
701     title_id = False            
702     title_ids = contact_title_obj.search(cr, uid, [('shortcut', '=', salutation), ('domain', '=', domain)])
703     if title_ids:
704          title_id = title_ids[0]
705     elif salutation:
706          title_id = contact_title_obj.create(cr, uid, {'name': salutation, 'shortcut': salutation, 'domain': domain})
707     return title_id
708     
709 def import_emails(sugar_obj, cr, uid, context=None):
710     if not context:
711         context= {}
712     map_emails = {'id': 'id',
713     'name':'name',
714     'date':'date_sent',
715     'email_from': 'from_addr_name',
716     'email_to': 'reply_to_addr',
717     'email_cc': 'cc_addrs_names',
718     'email_bcc': 'bcc_addrs_names',
719     'message_id': 'message_id',
720     'user_id/id': 'assigned_user_id',
721     'description': 'description_html',
722     'res_id': 'res_id',
723     'model': 'model',
724     }
725     mailgate_obj = sugar_obj.pool.get('mailgate.message')
726     model_obj = sugar_obj.pool.get('ir.model.data')
727     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
728     sugar_data = sugar.search(PortType, sessionid, 'Emails')
729     for val in sugar_data:
730         model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id'))])
731         for model in model_obj.browse(cr, uid, model_ids):
732             val['res_id'] = model.res_id
733             val['model'] = model.model
734         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails)
735         mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
736     return True    
737     
738 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
739     if not context:
740         context={}
741     partner_id = False
742     partner_invoice_id = False        
743     model_obj = sugar_obj.pool.get('ir.model.data')
744     partner_obj = sugar_obj.pool.get('res.partner')
745     partner_address_obj = sugar_obj.pool.get('res.partner.address')
746     sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
747     for account_id in sugar_project_account:
748         model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
749         if model_ids:
750             model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
751             partner_id = partner_obj.browse(cr, uid, model_id).id
752             address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
753             partner_invoice_id = address_ids[0] 
754     return partner_id, partner_invoice_id      
755     
756 def import_projects(sugar_obj, cr, uid, context=None):
757     if not context:
758         context = {}
759     map_project = {'id': 'id',
760         'name': 'name',
761         'date_start': 'estimated_start_date',
762         'date': 'estimated_end_date',
763         'user_id/id': 'assigned_user_id',
764         'partner_id/.id': 'partner_id/.id',
765         'contact_id/.id': 'contact_id/.id', 
766          'state': 'state'   
767     }
768     project_obj = sugar_obj.pool.get('project.project')
769     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
770     sugar_data = sugar.search(PortType, sessionid, 'Project')
771     for val in sugar_data:
772         partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context) 
773         val['partner_id/.id'] = partner_id
774         val['contact_id/.id'] = partner_invoice_id 
775         val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
776         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project)
777         project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
778     return True 
779
780
781 def import_project_tasks(sugar_obj, cr, uid, context=None):
782     if not context:
783         context = {}
784     map_project_task = {'id': 'id',
785         'name': 'name',
786         'date_start': 'date_start',
787         'date_end': 'date_finish',
788         'progress': 'progress',
789         'project_id/name': 'project_name',
790         'planned_hours': 'planned_hours',
791         'total_hours': 'total_hours',        
792         'priority': 'priority',
793         'description': 'description',
794         'user_id/id': 'assigned_user_id',
795          'state': 'state'   
796     }
797     task_obj = sugar_obj.pool.get('project.task')
798     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
799     sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
800     for val in sugar_data:
801         val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
802         val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
803         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task)
804         task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
805     return True 
806     
807 def import_leads(sugar_obj, cr, uid, context=None):
808     if not context:
809         context = {}
810     map_lead = {
811             'id' : 'id',
812             'name': ['first_name', 'last_name'],
813             'contact_name': ['first_name', 'last_name'],
814             'description': ['description', 'refered_by', 'lead_source', 'lead_source_description', 'website'],
815             'partner_name': 'account_name',
816             'email_from': 'email1',
817             'phone': 'phone_work',
818             'mobile': 'phone_mobile',
819             'title.id': 'title.id',
820             'function':'title',
821             'street': 'primary_address_street',
822             'street2': 'alt_address_street',
823             'zip': 'primary_address_postalcode',
824             'city':'primary_address_city',
825             'user_id/id' : 'assigned_user_id',
826             'stage_id.id' : 'stage_id.id',
827             'type' : 'type',
828             'state': 'state',
829             }
830         
831     lead_obj = sugar_obj.pool.get('crm.lead')
832     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
833     sugar_data = sugar.search(PortType, sessionid, 'Leads')
834     for val in sugar_data:
835         if val.get('opportunity_id'):
836             continue
837         title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'contact', context)
838         val['title.id'] = title_id
839         val['type'] = 'lead'
840         stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
841         val['stage_id.id'] = stage_id
842         val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
843         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead)
844         lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
845     return True
846
847 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
848     if not context:
849         context={}
850     partner_contact_name = False        
851     model_obj = sugar_obj.pool.get('ir.model.data')
852     partner_address_obj = sugar_obj.pool.get('res.partner.address')
853     model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
854     model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name 
855     sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
856     sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
857     sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
858     if sugar_contact: 
859         for contact in sugar_contact:
860             model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
861             if model_ids:
862                 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
863                 address_id = partner_address_obj.browse(cr, uid, model_id)
864                 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
865                 partner_contact_name = address_id.name
866             else:
867                 partner_contact_name = val.get('account_name')    
868     return partner_contact_name 
869
870 def import_opportunities(sugar_obj, cr, uid, context=None):
871     if not context:
872         context = {}
873     map_opportunity = {'id' : 'id',
874         'name': 'name',
875         'probability': 'probability',
876         'partner_id/name': 'account_name',
877         'title_action': 'next_step',
878         'partner_address_id/name': 'partner_address_id/name',
879         'planned_revenue': 'amount',
880         'date_deadline':'date_closed',
881         'user_id/id' : 'assigned_user_id',
882         'stage_id.id' : 'stage_id.id',
883         'type' : 'type',
884         'categ_id.id': 'categ_id.id'
885     }
886     lead_obj = sugar_obj.pool.get('crm.lead')
887     partner_obj = sugar_obj.pool.get('res.partner')
888     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
889     sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
890     for val in sugar_data:
891         partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
892         if not partner_xml_id:
893             raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
894         partner_contact_name = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
895         val['partner_address_id/name'] = partner_contact_name
896         val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))                    
897         val['type'] = 'opportunity'
898         stage_id = get_opportunity_status(sugar_obj, cr, uid, val, context)
899         val['stage_id.id'] = stage_id
900         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
901         lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
902     return True
903
904 MAP_FIELDS = {'Opportunities':  #Object Mapping name
905                     {'dependencies' : ['Users', 'Accounts', 'Contacts'],  #Object to import before this table
906                      'process' : import_opportunities,
907                      },
908               'Leads':
909                     {'dependencies' : ['Users', 'Accounts', 'Contacts'],  #Object to import before this table
910                      'process' : import_leads,
911                     },
912               'Contacts':
913                     {'dependencies' : ['Users'],  #Object to import before this table
914                      'process' : import_partner_address,
915                     },
916               'Accounts':
917                     {'dependencies' : ['Users'],  #Object to import before this table
918                      'process' : import_partners,
919                     },
920               'Users': 
921                     {'dependencies' : [],
922                      'process' : import_users,
923                     },
924               'Meetings': 
925                     {'dependencies' : ['Users', 'Tasks'],
926                      'process' : import_meetings,
927                     },        
928               'Tasks': 
929                     {'dependencies' : ['Users', 'Accounts', 'Contacts'],
930                      'process' : import_tasks,
931                     },  
932               'Calls': 
933                     {'dependencies' : ['Users', 'Accounts', 'Contacts'],
934                      'process' : import_calls,
935                     },                        
936               'Employees': 
937                     {'dependencies' : ['Resources'],
938                      'process' : import_employees,
939                     },
940               'Emails': 
941                     {'dependencies' : ['Users'],
942                      'process' : import_emails,
943                     },    
944               'Projects': 
945                     {'dependencies' : ['Users', 'Accounts', 'Contacts'],
946                      'process' : import_projects,
947                     },                        
948               'Project Tasks': 
949                     {'dependencies' : ['Users', 'Projects'],
950                      'process' : import_project_tasks,
951                     },
952               'Bugs': 
953                     {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
954                      'process' : import_bug,
955                     },   
956               'Notes': 
957                     {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
958                      'process' : import_history,
959                     },                    
960               'Resources': 
961                     {'dependencies' : ['Users'],
962                      'process' : import_resources,
963                     },                                      
964           }
965
966 class import_sugarcrm(osv.osv):
967     """Import SugarCRM DATA"""
968     
969     _name = "import.sugarcrm"
970     _description = __doc__
971     _columns = {
972         'lead': fields.boolean('Leads', help="If Leads are checked, SugarCRM Leads data imported in OpenERP crm-Lead form"),
973         'opportunity': fields.boolean('Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
974         'user': fields.boolean('User', help="If Users  are checked, SugarCRM Users data imported in OpenERP Users form"),
975         'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
976         'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM  Accounts data imported in OpenERP partners form"),
977         'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
978         'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
979         'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
980         'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
981         'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
982         'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
983         'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
984         'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
985         'username': fields.char('User Name', size=64),
986         'password': fields.char('Password', size=24),
987     }
988     _defaults = {
989        'lead': True,
990        'opportunity': True,
991        'user' : True,
992        'contact' : True,
993        'account' : True,
994         'employee' : True,
995         'meeting' : True,
996         'call' : True,    
997         'email' : True, 
998         'project' : True,   
999         'project_task': True,     
1000         'bug': True,
1001     }
1002     
1003     def get_key(self, cr, uid, ids, context=None):
1004         """Select Key as For which Module data we want import data."""
1005         if not context:
1006             context = {}
1007         key_list = []
1008         for current in self.browse(cr, uid, ids, context):
1009             if current.lead:
1010                 key_list.append('Leads')
1011             if current.opportunity:
1012                 key_list.append('Opportunities')
1013             if current.user:
1014                 key_list.append('Users')
1015             if current.contact:
1016                 key_list.append('Contacts')
1017             if current.account:
1018                 key_list.append('Accounts') 
1019             if current.employee:
1020                 key_list.append('Employees')  
1021             if current.meeting:
1022                 key_list.append('Meetings')
1023             if current.call:
1024                 key_list.append('Calls')
1025             if current.email:
1026                 key_list.append('Emails') 
1027             if current.project:
1028                 key_list.append('Projects')
1029             if current.project_task:
1030                 key_list.append('Project Tasks')
1031             if current.bug:
1032                 key_list.append('Bugs')
1033             if current.attachment:
1034                 key_list.append('Notes')                                    
1035         return key_list
1036
1037     def import_all(self, cr, uid, ids, context=None):
1038         """Import all sugarcrm data into openerp module"""
1039         if not context:
1040             context = {}
1041         keys = self.get_key(cr, uid, ids, context)
1042         imported = set() #to invoid importing 2 times the sames modules
1043         for key in keys:
1044             if not key in imported:
1045                 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1046                 MAP_FIELDS[key]['process'](self, cr, uid, context)
1047                 imported.add(key)
1048
1049         obj_model = self.pool.get('ir.model.data')
1050         model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1051         resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1052         return {
1053                 'view_type': 'form',
1054                 'view_mode': 'form',
1055                 'res_model': 'import.message',
1056                 'views': [(resource_id,'form')],
1057                 'type': 'ir.actions.act_window',
1058                 'target': 'new',
1059             }
1060
1061     def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1062         for dependency in dep:
1063             if not dependency in imported:
1064                 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1065                 dict[dependency]['process'](self, cr, uid, context)
1066                 imported.add(dependency)
1067         return True        
1068
1069 import_sugarcrm()