[IMP]: Usability Improvement
[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 OPENERP_FIEDS_MAPS = {'Leads': 'crm.lead',
30                       'Opportunities': 'crm.lead',
31                       'Contacts': 'res.partner.address',
32                       'Accounts': 'res.partner',
33                       'Resources': 'resource.resource',
34                       'Users': 'res.users',
35                       'Meetings': 'crm.meeting',
36                       'Calls': 'crm.phonecall',
37                       'Claims': 'crm.claim',
38                       'Employee': 'hr.employee',
39                       'Project': 'project.project',
40                       'ProjectTask': 'project.task',
41                       'Bugs': 'project.issue',
42                       'Documents': 'ir.attachment',
43                       
44   }
45
46 def find_mapped_id(obj, cr, uid, res_model, sugar_id, context):
47     model_obj = obj.pool.get('ir.model.data')
48     return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context)
49
50 def mapped_id(obj, cr, uid, res_model, sugar_id, id, context):
51     """
52         This function create the mapping between an already existing data and the similar data of sugarcrm
53         @param res_model: model of the mapped object
54         @param sugar_id: external sugar id
55         @param id: id in the database
56         
57         @return : the xml_id or sugar_id 
58     """
59     ir_model_data_obj = obj.pool.get('ir.model.data')
60     id = ir_model_data_obj._update(cr, uid, res_model,
61                      'sugarcrm_import', {}, mode='update', xml_id=sugar_id,
62                      noupdate=True, res_id=id, context=context)
63     return sugar_id
64
65 def import_object(sugar_obj, cr, uid, fields, data, model, table, name, domain_search=False,  context=None):
66     """
67         This method will import an object in the openerp, usefull for field that is only a char in sugar and is an object in openerp
68         use import_data that will take care to create/update or do nothing with the data
69         this method return the xml_id
70         @param fields: list of fields needed to create the object without id
71         @param data: the list of the data, in the same order as the field
72             ex : fields = ['firstname', 'lastname'] ; data = ['John', 'Mc donalds']
73         @param model: the openerp's model of the create/update object
74         @param table: the table where data come from in sugarcrm, no need to fit the real name of openerp name, just need to be unique
75         @param unique_name: the name of the object that we want to create/update get the id
76         @param domain_search : the domain that should find the unique existing record
77         
78         @return: the xml_id of the ressources
79     """
80     domain_search = not domain_search and [('name', 'ilike', name)] or domain_search
81     obj = sugar_obj.pool.get(model)
82     xml_id = generate_xml_id(name, table)
83
84     def mapped_id_if_exist(obj, domain, xml_id):
85         ids = obj.search(cr, uid, domain, context=context)
86         if ids:
87             return mapped_id(obj, cr, uid, model, xml_id, ids[0], context=context)
88         return False
89     
90     
91     mapped_id_if_exist(obj, domain_search, xml_id)
92     fields.append('id')
93     data.append(xml_id)
94     obj.import_data(cr, uid, fields, [data], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
95     return xml_id
96
97 def generate_xml_id(name, table):
98     """
99         @param name: name of the object, has to be unique in for a given table
100         @param table : table where the record we want generate come from
101         @return: a unique xml id for record, the xml_id will be the same given the same table and same name
102                  To be used to avoid duplication of data that don't have ids
103     """
104     sugar_instance = "sugarcrm" #TODO need to be changed we information is known in the wizard
105     name = name.replace('.', '_')
106     return sugar_instance + "_" + table + "_" + name
107
108 def get_all(sugar_obj, cr, uid, model, sugar_val, context=None):
109     models = sugar_obj.pool.get(model)
110     model_code = sugar_val[0:2]
111     all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', model_code.upper())]) 
112     output = sorted([(o.id, o.name)
113                      for o in models.browse(cr, uid, all_model_ids, context=context)],
114                     key=itemgetter(1))
115     return output
116
117 def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
118     """Get states or create new state"""
119     state_id = False
120     res_country_state_obj = sugar_obj.pool.get('res.country.state')
121     
122     state = get_all(sugar_obj,
123         cr, uid, 'res.country.state', sugar_val, context=context)
124     if state:
125         state_id = state and state[0][0]
126     else:
127         state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
128     return state_id   
129
130 def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None):
131     """Get Country or Create new country"""
132     res_country_obj = sugar_obj.pool.get('res.country')
133     country_id = False
134     country_code = sugar_country_val[0:2]
135     country = get_all(sugar_obj,
136         cr, uid, 'res.country', sugar_country_val, context=context)
137     if country:
138         country_id = country and country[0][0] 
139     else:
140         country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})  
141     return country_id
142
143 def import_partner_address(sugar_obj, cr, uid, context=None):
144     if not context:
145         context = {}
146     map_partner_address = {
147              'id': 'id',              
148              'name': ['first_name', 'last_name'],
149              'partner_id/id': 'account_id',
150             'phone': 'phone_work',
151             'mobile': 'phone_mobile',
152             'fax': 'phone_fax',
153             'function': 'title',
154             'street': 'primary_address_street',
155             'zip': 'primary_address_postalcode',
156             'city': 'primary_address_city',
157             'country_id.id': 'country_id.id',
158             'state_id.id': 'state_id.id',
159             'email': 'email',
160             'type': 'type'
161             }
162     address_obj = sugar_obj.pool.get('res.partner.address')
163     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
164     sugar_data = sugar.search(PortType, sessionid, 'Contacts')
165     for val in sugar_data:
166         val['type'] = 'contact'
167         contact_emails = sugar.contact_emails_search(PortType, context.get('username', ''), context.get('password', ''), email_address=val.get('email1'))
168         val['email'] = (','.join(map(lambda x : x, contact_emails)))
169         if val.get('primary_address_country'):
170             country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
171             state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
172             val['country_id.id'] =  country_id
173             val['state_id.id'] =  state        
174         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
175         address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
176     return True
177     
178
179 def import_users(sugar_obj, cr, uid, context=None):
180     map_user = {
181         'id' : 'id', 
182         'name': ['first_name', 'last_name'],
183         'login': 'user_name',
184         'context_lang' : 'context_lang',
185         'password' : 'password',
186         '.id' : '.id',
187         'context_department_id.id': 'context_department_id.id',
188     } 
189     
190     def get_users_department(sugar_obj, cr, uid, val, context=None):
191         department_id = False       
192         department_obj = sugar_obj.pool.get('hr.department')
193         department_ids = department_obj.search(cr, uid, [('name', '=', val)])
194         if department_ids:
195             department_id = department_ids[0]
196         elif val:
197             department_id = department_obj.create(cr, uid, {'name': val})
198         return department_id 
199     
200     if not context:
201         context = {}
202     department_id = False        
203     
204     user_obj = sugar_obj.pool.get('res.users')
205     PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
206     sugar_data = sugar.search(PortType,sessionid, 'Users')
207     for val in sugar_data:
208         user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))])
209         if user_ids: 
210             val['.id'] = str(user_ids[0])
211         else:
212             val['password'] = 'sugarcrm' #default password for all user
213         department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
214         val['context_department_id.id'] = department_id     
215         val['context_lang'] = context.get('lang','en_US')
216         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user, context)
217         #All data has to be imported separatly because they don't have the same field
218         user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
219     return True
220
221 def get_lead_status(sugar_obj, cr, uid, sugar_val,context=None):
222     if not context:
223         context = {}
224     fields = ['name', 'type']
225     name = 'lead_' + sugar_val['status']
226     data = [sugar_val['status'], 'lead']
227     return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_lead_stage', name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val['status'])], context)
228
229 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
230     if not context:
231         context = {}
232     state = False
233     state_dict = {'status': #field in the sugarcrm database
234         { #Mapping of sugarcrm stage : openerp opportunity stage
235             'New' : 'draft',
236             'Assigned':'open',
237             'In Progress': 'open',
238             'Recycled': 'cancel',
239             'Dead': 'done'
240         },}
241     state = state_dict['status'].get(sugar_val['status'], '')
242     return state
243
244 def get_user_address(sugar_obj, cr, uid, val, context=None):
245     address_obj = sugar_obj.pool.get('res.partner.address')
246     map_user_address = {
247     'name': ['first_name', 'last_name'],
248     'city': 'address_city',
249     'country_id': 'country_id',
250     'state_id': 'state_id',
251     'street': 'address_street',
252     'zip': 'address_postalcode',
253     }
254     address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') or '' +' '+ val.get('last_name'))])
255     if val.get('address_country'):
256         country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
257         state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
258         val['country_id'] =  country_id
259         val['state_id'] =  state_id
260     fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address, context)
261     dict_val = dict(zip(fields,datas))
262     if address_ids:
263         address_obj.write(cr, uid, address_ids, dict_val)
264     else:        
265         new_address_id = address_obj.create(cr,uid, dict_val)
266         return new_address_id
267     return True
268
269 def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
270         address_obj = sugar_obj.pool.get('res.partner.address')
271         new_address_id = False
272         if type == 'invoice':
273             type_address = 'billing'
274         else:
275             type_address = 'shipping'     
276     
277         map_partner_address.update({
278             'street': type_address + '_address_street',
279             'zip': type_address +'_address_postalcode',
280             'city': type_address +'_address_city',
281              'country_id': 'country_id',
282              'type': 'type',
283             })
284         val['type'] = type
285         if val.get(type_address +'_address_country'):
286             country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context)
287             state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context)
288             val['country_id'] =  country_id
289             val['state_id'] =  state
290         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
291         #Convert To list into Dictionary(Key, val). value pair.
292         dict_val = dict(zip(fields,datas))
293         new_address_id = address_obj.create(cr,uid, dict_val)
294         return new_address_id
295     
296 def get_address(sugar_obj, cr, uid, val, context=None):
297     map_partner_address={}
298     address_id=[]
299     address_obj = sugar_obj.pool.get('res.partner.address')
300     address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
301     if address_ids:
302         return address_ids 
303     else:
304         map_partner_address = {
305             'id': 'id',                    
306             'name': 'name',
307             'partner_id/id': 'account_id',
308             'phone': 'phone_office',
309             'mobile': 'phone_mobile',
310             'fax': 'phone_fax',
311             'type': 'type',
312             }
313         if val.get('billing_address_street'):
314             address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
315             
316         if val.get('shipping_address_street'):
317             address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
318         return address_id
319     return True
320
321 def import_partners(sugar_obj, cr, uid, context=None):
322     if not context:
323         context = {}
324     map_partner = {
325                 'id': 'id',
326                 'name': 'name',
327                 'website': 'website',
328                 'user_id/id': 'assigned_user_id',
329                 'ref': 'sic_code',
330                 'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
331                 'customer': 'customer',
332                 'supplier': 'supplier', 
333                 }
334     partner_obj = sugar_obj.pool.get('res.partner')
335     address_obj = sugar_obj.pool.get('res.partner.address')
336     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
337     sugar_data = sugar.search(PortType, sessionid, 'Accounts')
338     for val in sugar_data:
339         add_id = get_address(sugar_obj, cr, uid, val, context)
340         val['customer'] = '1'
341         val['supplier'] = '0'
342         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner, context)
343         partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
344         for address in  address_obj.browse(cr,uid,add_id):
345             data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
346             if data_id:
347                 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})                
348     return True
349
350 def get_category(sugar_obj, cr, uid, model, name, context=None):
351     if not context:
352         context = {}
353     fields = ['name', 'object_id']
354     data = [name, model]
355     return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)], context)
356
357 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
358     
359     alarm_dict = {'60': '1 minute before',
360                   '300': '5 minutes before',
361                   '600': '10 minutes before',
362                   '900': '15 minutes before',
363                   '1800':'30 minutes before',
364                   '3600': '1 hour before',
365      }
366     alarm_id = False
367     alarm_obj = sugar_obj.pool.get('res.alarm')
368     if alarm_dict.get(val):
369         alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
370         for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
371             alarm_id = alarm.id
372     return alarm_id 
373     
374 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
375     if not context:
376         context = {}
377     state = False
378     state_dict = {'status': #field in the sugarcrm database
379         { #Mapping of sugarcrm stage : openerp meeting stage
380             'Planned' : 'draft',
381             'Held':'open',
382             'Not Held': 'draft',
383         },}
384     state = state_dict['status'].get(val, '')
385     return state    
386
387 def get_task_state(sugar_obj, cr, uid, val, context=None):
388     if not context:
389         context = {}
390     state = False
391     state_dict = {'status': #field in the sugarcrm database
392         { #Mapping of sugarcrm stage : openerp meeting stage
393             'Completed' : 'done',
394             'Not Started':'draft',
395             'In Progress': 'open',
396             'Pending Input': 'draft',
397             'deferred': 'cancel'
398         },}
399     state = state_dict['status'].get(val, '')
400     return state    
401
402 def get_project_state(sugar_obj, cr, uid, val,context=None):
403     if not context:
404         context = {}
405     state = False
406     state_dict = {'status': #field in the sugarcrm database
407         { #Mapping of sugarcrm staus : openerp Projects state
408             'Draft' : 'draft',
409             'In Review': 'open',
410             'Published': 'close',
411         },}
412     state = state_dict['status'].get(val, '')
413     return state    
414
415 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
416     if not context:
417         context = {}
418     state = False
419     state_dict = {'status': #field in the sugarcrm database
420         { #Mapping of sugarcrm status : openerp Porject Tasks state
421              'Not Started': 'draft',
422              'In Progress': 'open',
423              'Completed': 'done',
424             'Pending Input': 'pending',
425             'Deferred': 'cancelled',
426         },}
427     state = state_dict['status'].get(val, '')
428     return state    
429
430 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
431     if not context:
432         context = {}
433     priority = False
434     priority_dict = {'priority': #field in the sugarcrm database
435         { #Mapping of sugarcrm status : openerp Porject Tasks state
436             'High': '0',
437             'Medium': '2',
438             'Low': '3'
439         },}
440     priority = priority_dict['priority'].get(val, '')
441     return priority    
442
443
444 def get_account(sugar_obj, cr, uid, val, context=None):
445     if not context:
446         context = {}
447     partner_id = False    
448     partner_address_id = False
449     partner_phone = False
450     partner_mobile = False
451     model_obj = sugar_obj.pool.get('ir.model.data')
452     address_obj = sugar_obj.pool.get('res.partner.address')
453     crm_obj = sugar_obj.pool.get('crm.lead')
454     project_obj = sugar_obj.pool.get('project.project')
455     issue_obj = sugar_obj.pool.get('project.issue')
456     if val.get('parent_type') == 'Accounts':
457         model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
458         if model_ids:
459             model = model_obj.browse(cr, uid, model_ids)[0]
460             partner_id = model.res_id
461             address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
462             if address_ids:
463                 address_id = address_obj.browse(cr, uid, address_ids[0])
464                 partner_address_id = address_id.id
465                 partner_phone = address_id.phone
466                 partner_mobile = address_id.mobile
467             
468     if val.get('parent_type') == 'Contacts':
469         model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
470         for model in model_obj.browse(cr, uid, model_ids):
471             partner_address_id = model.res_id
472             address_id = address_obj.browse(cr, uid, partner_address_id)
473             partner_phone = address_id.phone
474             partner_mobile = address_id.mobile
475             partner_id = address_id and address_id.partner_id or False
476             
477     if val.get('parent_type') == 'Opportunities':
478         model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'crm.lead')])
479         for model in model_obj.browse(cr, uid, model_ids):
480             opportunity_id = model.res_id
481             opportunity_id = crm_obj.browse(cr, uid, opportunity_id)
482             partner_id = opportunity_id.partner_id.id
483             partner_address_id =  opportunity_id.partner_address_id.id
484             partner_phone = opportunity_id.partner_address_id.phone
485             partner_mobile = opportunity_id.partner_address_id.mobile
486             
487     if val.get('parent_type') == 'Project':
488         model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.project')])
489         for model in model_obj.browse(cr, uid, model_ids):
490             proj_ids = model.res_id
491             proj_id = project_obj.browse(cr, uid, proj_ids)
492             partner_id = proj_id.partner_id.id
493             partner_address_id =  proj_id.contact_id.id
494             partner_phone = proj_id.contact_id.phone
495             partner_mobile = proj_id.contact_id.mobile
496
497     if val.get('parent_type') == 'Bugs':
498         model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.issue')])
499         for model in model_obj.browse(cr, uid, model_ids):
500             issue_ids = model.res_id
501             issue_id = issue_obj.browse(cr, uid, issue_ids)
502             partner_id = issue_id.partner_id.id
503             partner_address_id =  issue_id.partner_address_id.id
504             partner_phone = issue_id.partner_address_id.phone
505             partner_mobile = issue_id.partner_address_id.mobile                        
506                         
507     return partner_id, partner_address_id, partner_phone,partner_mobile                          
508
509 def import_documents(sugar_obj, cr, uid, context=None):
510     if not context:
511         context = {}
512     map_document = {'id' : 'id', 
513              'name': 'document_name',
514            'description': 'description',
515            'datas': 'datas',
516            'datas_fname': 'datas_fname',
517             } 
518     attach_obj = sugar_obj.pool.get('ir.attachment')
519     PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
520     sugar_data = sugar.search(PortType,sessionid, 'Documents')
521     for val in sugar_data:
522         file, filename = sugar.attachment_search(PortType, sessionid, 'DocumentRevisions', val.get('document_revision_id'))
523         val['datas'] = file
524         val['datas_fname'] = filename
525         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_document, context)
526         attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
527     return True
528
529 def import_tasks(sugar_obj, cr, uid, context=None):
530     if not context:
531         context = {}
532     map_task = {'id' : 'id',
533                 'name': 'name',
534                 'date': ['__datetime__', 'date_start'],
535                 'date_deadline' : ['__datetime__', 'date_due'],
536                 'user_id/id': 'assigned_user_id',
537                 'categ_id/id': 'categ_id/id',
538                 'partner_id/.id': 'partner_id/.id',
539                 'partner_address_id/.id': 'partner_address_id/.id',
540                 'state': 'state'
541     }
542     meeting_obj = sugar_obj.pool.get('crm.meeting')
543     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
544     categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
545     sugar_data = sugar.search(PortType, sessionid, 'Tasks')
546     for val in sugar_data:
547         partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
548         if not partner_xml_id:
549             raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
550         partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
551         val['partner_id/.id'] = partner_id
552         val['partner_address_id/.id'] = partner_address_id
553         val['categ_id/id'] = categ_id
554         val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context)
555         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task, context)
556         meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
557     return True    
558     
559 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
560     if not context:
561         context = {}
562     model_obj = sugar_obj.pool.get('ir.model.data')
563     att_obj = sugar_obj.pool.get('calendar.attendee')
564     meeting_obj = sugar_obj.pool.get('crm.meeting')
565     user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
566     for user in user_dict: 
567         user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
568         user_resource_id = model_obj.browse(cr, uid, user_model_ids)        
569         if user_resource_id:
570             user_id = user_resource_id[0].res_id 
571             attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
572             if attend_ids:
573                 attendees = attend_ids[0]
574             else:      
575                 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
576             meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
577             meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
578             if meeting_xml_id:
579                 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})       
580     return True   
581     
582 def import_meetings(sugar_obj, cr, uid, context=None):
583     if not context:
584         context = {}
585     map_meeting = {'id' : 'id',
586                     'name': 'name',
587                     'date': ['__datetime__', 'date_start'],
588                     'duration': ['duration_hours', 'duration_minutes'],
589                     'location': 'location',
590                     'alarm_id/.id': 'alarm_id/.id',
591                     'user_id/id': 'assigned_user_id',
592                     'partner_id/.id':'partner_id/.id',
593                     'partner_address_id/.id':'partner_address_id/.id',
594                     'state': 'state'
595     }
596     meeting_obj = sugar_obj.pool.get('crm.meeting')
597     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
598     sugar_data = sugar.search(PortType, sessionid, 'Meetings')
599     for val in sugar_data:
600         partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
601         val['partner_id/.id'] = partner_id
602         val['partner_address_id/.id'] = partner_address_id
603         val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
604         val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
605         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting, context)
606         meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
607         get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
608     return True    
609
610 def get_calls_state(sugar_obj, cr, uid, val,context=None):
611     if not context:
612         context = {}
613     state = False
614     state_dict = {'status': #field in the sugarcrm database
615         { #Mapping of sugarcrm stage : openerp calls stage
616             'Planned' : 'open',
617             'Held':'done',
618             'Not Held': 'pending',
619         },}
620     state = state_dict['status'].get(val, '')
621     return state   
622
623 def import_calls(sugar_obj, cr, uid, context=None):
624     if not context:
625         context = {}
626     map_calls = {'id' : 'id',
627                     'name': 'name',
628                     'date': ['__datetime__', 'date_start'],
629                     'duration': ['duration_hours', 'duration_minutes'],
630                     'user_id/id': 'assigned_user_id',
631                     'partner_id/.id': 'partner_id/.id',
632                     'partner_address_id/.id': 'partner_address_id/.id',
633                     'categ_id/id': 'categ_id/id',
634                    'state': 'state',
635                    'partner_phone': 'partner_phone',
636                    'partner_mobile': 'partner_mobile',
637                    'opportunity_id/id': 'opportunity_id/id',
638
639     }
640     phonecall_obj = sugar_obj.pool.get('crm.phonecall')
641     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
642     sugar_data = sugar.search(PortType, sessionid, 'Calls')
643     for val in sugar_data:
644         sugar_call_leads = sugar.relation_search(PortType, sessionid, 'Calls', module_id=val.get('id'), related_module='Leads', query=None, deleted=None)
645         if sugar_call_leads:
646             for call_opportunity in sugar_call_leads: 
647                 val['opportunity_id/id'] = call_opportunity 
648         categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))         
649         val['categ_id/id'] = categ_id
650         partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
651         
652         val['partner_id/.id'] = partner_id
653         val['partner_address_id/.id'] = partner_address_id
654         val['partner_phone'] = partner_phone
655         val['partner_mobile'] = partner_mobile
656         val['state'] =  get_calls_state(sugar_obj, cr, uid, val.get('status'), context)  
657         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls, context)
658         phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
659     return True
660     
661 def import_resources(sugar_obj, cr, uid, context=None):
662     if not context:
663         context = {}
664     map_resource = {'id' : 'user_hash',
665                     'name': ['first_name', 'last_name'],
666     }
667     resource_obj = sugar_obj.pool.get('resource.resource')
668     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
669     sugar_data = sugar.search(PortType, sessionid, 'Employees')
670     for val in sugar_data:
671         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
672         resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
673     return True    
674
675 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
676     if not context:
677         context = {}
678     priority = False
679     priority_dict = {'priority': #field in the sugarcrm database
680         { #Mapping of sugarcrm priority : openerp bugs priority
681             'Urgent': '1',
682             'High': '2',
683             'Medium': '3',
684             'Low': '4'
685         },}
686     priority = priority_dict['priority'].get(val, '')
687     return priority  
688
689 def get_claim_priority(sugar_obj, cr, uid, val,context=None):
690     if not context:
691         context = {}
692     priority = False
693     priority_dict = {'priority': #field in the sugarcrm database
694         { #Mapping of sugarcrm priority : openerp claims priority
695             'High': '2',
696             'Medium': '3',
697             'Low': '4'
698         },}
699     priority = priority_dict['priority'].get(val, '')
700     return priority   
701
702 def get_bug_state(sugar_obj, cr, uid, val,context=None):
703     if not context:
704         context = {}
705     state = False
706     state_dict = {'status': #field in the sugarcrm database
707         { #Mapping of sugarcrm status : openerp Bugs state
708             'New' : 'draft',
709             'Assigned':'open',
710             'Closed': 'done',
711             'Pending': 'pending',
712             'Rejected': 'cancel',
713         },}
714     state = state_dict['status'].get(val, '')
715     return state
716
717 def get_claim_state(sugar_obj, cr, uid, val,context=None):
718     if not context:
719         context = {}
720     state = False
721     state_dict = {'status': #field in the sugarcrm database
722         { #Mapping of sugarcrm status : openerp claim state
723             'New' : 'draft',
724             'Assigned':'open',
725             'Closed': 'done',
726             'Pending Input': 'pending',
727             'Rejected': 'cancel',
728             'Duplicate': 'draft',
729         },}
730     state = state_dict['status'].get(val, '')
731     return state
732     
733
734 def get_acc_contact_claim(sugar_obj, cr, uid, val, context=None):
735     if not context:
736         context = {}
737     partner_id = False    
738     partner_address_id = False
739     partner_phone = False
740     partner_email = False
741     model_obj = sugar_obj.pool.get('ir.model.data')
742     address_obj = sugar_obj.pool.get('res.partner.address')
743     model_ids = model_obj.search(cr, uid, [('name', '=', val.get('account_id')), ('model', '=', 'res.partner')])
744     if model_ids:
745         model = model_obj.browse(cr, uid, model_ids)[0]
746         partner_id = model.res_id
747         address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
748         if address_ids:
749             address_id = address_obj.browse(cr, uid, address_ids[0])
750             partner_address_id = address_id.id
751             partner_phone = address_id.phone
752             partner_mobile = address_id.email
753     return partner_id, partner_address_id, partner_phone,partner_email
754
755 def import_claims(sugar_obj, cr, uid, context=None):
756     if not context:
757         context = {}
758     map_claim = {'id' : 'id',
759                     'name': 'name',
760                     'date': ['__datetime__', 'date_entered'],
761                     'user_id/id': 'assigned_user_id',
762                     'priority':'priority',
763                     'partner_id/.id': 'partner_id/.id',
764                     'partner_address_id/.id': 'partner_address_id/.id',
765                     'partner_phone': 'partner_phone',
766                     'partner_mobile': 'partner_email',                    
767                     'description': 'description',
768                     'state': 'state',
769     }
770     claim_obj = sugar_obj.pool.get('crm.claim')
771     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
772     sugar_data = sugar.search(PortType, sessionid, 'Cases')
773     for val in sugar_data:
774         partner_id, partner_address_id, partner_phone,partner_email = get_acc_contact_claim(sugar_obj, cr, uid, val, context)
775         val['partner_id/.id'] = partner_id
776         val['partner_address_id/.id'] = partner_address_id
777         val['partner_phone'] = partner_phone
778         val['email_from'] = partner_email
779         val['priority'] = get_claim_priority(sugar_obj, cr, uid, val.get('priority'),context)
780         val['state'] = get_claim_state(sugar_obj, cr, uid, val.get('status'),context)
781         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_claim, context)
782         claim_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
783     return True    
784
785 def import_bug(sugar_obj, cr, uid, context=None):
786     if not context:
787         context = {}
788     map_resource = {'id' : 'id',
789                     'name': 'name',
790                     'project_id/.id':'project_id/.id',
791                     'categ_id/id': 'categ_id/id',
792                     'priority':'priority',
793                     'description': ['__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'],
794                     'state': 'state',
795     }
796     issue_obj = sugar_obj.pool.get('project.issue')
797     project_obj = sugar_obj.pool.get('project.project')
798     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
799     sugar_data = sugar.search(PortType, sessionid, 'Bugs')
800     for val in sugar_data:
801         project_ids = project_obj.search(cr, uid, [('name', 'like', 'sugarcrm_bugs')])
802         if project_ids:
803             project_id = project_ids[0]
804         else:
805              project_id = project_obj.create(cr, uid, {'name':'sugarcrm_bugs'})    
806         val['project_id/.id'] = project_id
807         val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
808         val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
809         val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
810         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
811         issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
812     return True    
813
814 def get_job_id(sugar_obj, cr, uid, val, context=None):
815     if not context:
816         context={}
817     fields = ['name']
818     data = [val]
819     return import_object(sugar_obj, cr, uid, fields, data, 'hr.job', 'hr_job', val, [('name', 'ilike', val)], context)
820
821 def get_campaign_id(sugar_obj, cr, uid, val, context=None):
822     if not context:
823         context={}
824     fields = ['name']
825     data = [val]
826     return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.resource.type', 'crm_campaign', val, [('name', 'ilike', val)], context)
827     
828 def get_attachment(sugar_obj, cr, uid, val, model, File, Filename, parent_type, context=None):
829     if not context:
830         context = {}
831     attach_ids = False    
832     attachment_obj = sugar_obj.pool.get('ir.attachment')
833     partner_obj = sugar_obj.pool.get('res.partner')
834     model_obj = sugar_obj.pool.get('ir.model.data')
835     mailgate_obj = sugar_obj.pool.get('mailgate.message')
836     attach_ids = attachment_obj.search(cr, uid, [('res_id','=', val.get('res_id'), ('res_model', '=', val.get('model')))])
837     if not attach_ids and Filename:
838         if parent_type == 'Accounts':
839             new_attachment_id = attachment_obj.create(cr, uid, {'name': Filename, 'datas_fname': Filename, 'datas': File, 'res_id': val.get('res_id', False),'res_model': val.get('model',False), 'partner_id': val.get('partner_id/.id')})
840         else:    
841             new_attachment_id = attachment_obj.create(cr, uid, {'name': Filename, 'datas_fname': Filename, 'datas': File, 'res_id': val.get('res_id', False),'res_model': val.get('model',False)})
842         message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
843         message_xml_id = model_obj.browse(cr, uid, message_model_ids)
844         if message_xml_id:
845           if parent_type == 'Accounts':
846                  mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)], 'partner_id': val.get('partner_id/.id')})
847           else:
848                  mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})                                              
849     return True    
850     
851 def import_history(sugar_obj, cr, uid, context=None):
852     if not context:
853         context = {}
854     map_attachment = {'id' : 'id',
855                       'name':'name',
856                       'date': ['__datetime__', 'date_entered'],
857                       'user_id/id': 'assigned_user_id',
858                       'description': ['__prettyprint__','description', 'description_html'],
859                       'res_id': 'res_id',
860                       'model': 'model',
861                       'partner_id/.id' : 'partner_id/.id',
862     }
863     mailgate_obj = sugar_obj.pool.get('mailgate.message')
864     model_obj =  sugar_obj.pool.get('ir.model.data')
865     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
866     sugar_data = sugar.search(PortType, sessionid, 'Notes')
867     for val in sugar_data:
868         File, Filename = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
869         model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
870         if model_ids:
871             model = model_obj.browse(cr, uid, model_ids)[0]
872             if model.model == 'res.partner':
873                 val['partner_id/.id'] = model.res_id
874             else:    
875                 val['res_id'] = model.res_id
876                 val['model'] = model.model
877         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment, context)   
878         mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
879         get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, Filename, val.get('parent_type'), context)
880     return True       
881     
882 def import_employees(sugar_obj, cr, uid, context=None):
883     if not context:
884         context = {}
885     map_employee = {'id' : 'id',
886                     'resource_id/.id': 'resource_id/.id',
887                     'name': ['first_name', 'last_name'],
888                     'work_phone': 'phone_work',
889                     'mobile_phone':  'phone_mobile',
890                     'user_id/name': ['first_name', 'last_name'], 
891                     'address_home_id/.id': 'address_home_id/.id',
892                     'notes': 'description',
893                     #TODO: Creation of Employee create problem.
894                  #   'coach_id/id': 'reports_to_id',
895                     'job_id/id': 'job_id/id'
896     }
897     employee_obj = sugar_obj.pool.get('hr.employee')
898     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
899     sugar_data = sugar.search(PortType, sessionid, 'Employees')
900     for val in sugar_data:
901         address_id = get_user_address(sugar_obj, cr, uid, val, context)
902         val['address_home_id/.id'] = address_id
903         model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
904         resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
905         if resource_id:
906             val['resource_id/.id'] = resource_id[0].res_id
907         val['job_id/id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
908         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee, context)
909         employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
910     return True
911
912 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
913     fields = ['shortcut', 'name', 'domain']
914     data = [salutation, salutation, domain]
915     return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context)
916
917 def import_emails(sugar_obj, cr, uid, context=None):
918     if not context:
919         context= {}
920     map_emails = {'id': 'id',
921     'name':'name',
922     'date':['__datetime__', 'date_sent'],
923     'email_from': 'from_addr_name',
924     'email_to': 'reply_to_addr',
925     'email_cc': 'cc_addrs_names',
926     'email_bcc': 'bcc_addrs_names',
927     'message_id': 'message_id',
928     'user_id/id': 'assigned_user_id',
929     'description': ['__prettyprint__', 'description', 'description_html'],
930     'res_id': 'res_id',
931     'model': 'model',
932     'partner_id/.id': 'partner_id/.id'
933     }
934     mailgate_obj = sugar_obj.pool.get('mailgate.message')
935     model_obj = sugar_obj.pool.get('ir.model.data')
936     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
937     sugar_data = sugar.search(PortType, sessionid, 'Emails')
938     for val in sugar_data:
939         model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
940         for model in model_obj.browse(cr, uid, model_ids):
941             if model.model == 'res.partner':
942                 val['partner_id/.id'] = model.res_id
943             else:    
944                 val['res_id'] = model.res_id
945                 val['model'] = model.model
946         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails, context)
947         mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
948     return True    
949     
950 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
951     if not context:
952         context={}
953     partner_id = False
954     partner_invoice_id = False        
955     model_obj = sugar_obj.pool.get('ir.model.data')
956     partner_obj = sugar_obj.pool.get('res.partner')
957     partner_address_obj = sugar_obj.pool.get('res.partner.address')
958     sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
959     for account_id in sugar_project_account:
960         model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
961         if model_ids:
962             model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
963             partner_id = partner_obj.browse(cr, uid, model_id).id
964             address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
965             partner_invoice_id = address_ids[0] 
966     return partner_id, partner_invoice_id      
967     
968 def import_projects(sugar_obj, cr, uid, context=None):
969     if not context:
970         context = {}
971     map_project = {'id': 'id',
972         'name': 'name',
973         'date_start': ['__datetime__', 'estimated_start_date'],
974         'date': ['__datetime__', 'estimated_end_date'],
975         'user_id/id': 'assigned_user_id',
976         'partner_id/.id': 'partner_id/.id',
977         'contact_id/.id': 'contact_id/.id', 
978          'state': 'state'   
979     }
980     project_obj = sugar_obj.pool.get('project.project')
981     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
982     sugar_data = sugar.search(PortType, sessionid, 'Project')
983     for val in sugar_data:
984         partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context) 
985         val['partner_id/.id'] = partner_id
986         val['contact_id/.id'] = partner_invoice_id 
987         val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
988         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project, context)
989         project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
990     return True 
991
992
993 def import_project_tasks(sugar_obj, cr, uid, context=None):
994     if not context:
995         context = {}
996     map_project_task = {'id': 'id',
997         'name': 'name',
998         'date_start': ['__datetime__', 'date_start'],
999         'date_end': ['__datetime__', 'date_finish'],
1000         'progress': 'progress',
1001         'project_id/name': 'project_name',
1002         'planned_hours': 'planned_hours',
1003         'total_hours': 'total_hours',        
1004         'priority': 'priority',
1005         'description': 'description',
1006         'user_id/id': 'assigned_user_id',
1007          'state': 'state'   
1008     }
1009     task_obj = sugar_obj.pool.get('project.task')
1010     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1011     sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
1012     for val in sugar_data:
1013         val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
1014         val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
1015         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task, context)
1016         task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1017     return True 
1018     
1019 def import_leads(sugar_obj, cr, uid, context=None):
1020     if not context:
1021         context = {}
1022     map_lead = {
1023             'id' : 'id',
1024             'name': ['first_name', 'last_name'],
1025             'contact_name': ['first_name', 'last_name'],
1026             'description': ['__prettyprint__', 'description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'],
1027             'partner_name': 'account_name',
1028             'email_from': 'email1',
1029             'phone': 'phone_work',
1030             'mobile': 'phone_mobile',
1031             'title.id': 'title.id',
1032             'function':'title',
1033             'street': 'primary_address_street',
1034             'street2': 'alt_address_street',
1035             'zip': 'primary_address_postalcode',
1036             'city':'primary_address_city',
1037             'user_id/id' : 'assigned_user_id',
1038             'stage_id/id' : 'stage_id/id',
1039             'type' : 'type',
1040             'state': 'state',
1041             'fax': 'phone_fax',
1042             'referred': 'refered_by',
1043             'optout': 'optout',
1044             'type_id/id': 'type_id/id',
1045             'country_id.id': 'country_id.id',
1046             'state_id.id': 'state_id.id'
1047             }
1048     lead_obj = sugar_obj.pool.get('crm.lead')
1049     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1050     sugar_data = sugar.search(PortType, sessionid, 'Leads')
1051     for val in sugar_data:
1052         if val.get('do_not_call') == '0':
1053             val['optout'] = '1'
1054         if val.get('opportunity_id'):
1055             continue
1056         if val.get('salutation'):
1057             title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context)
1058             val['title/id'] = title_id
1059         val['type'] = 'lead'
1060         val['type_id/id'] = get_campaign_id(sugar_obj, cr, uid, val.get('lead_source'), context)
1061         stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
1062         val['stage_id/id'] = stage_id
1063         val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
1064         if val.get('primary_address_country'):
1065             country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
1066             state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
1067             val['country_id.id'] =  country_id
1068             val['state_id.id'] =  state            
1069         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead, context)
1070         lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1071     return True
1072
1073 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
1074     if not context:
1075         context={}
1076     partner_contact_name = False 
1077     partner_contact_email = False       
1078     model_obj = sugar_obj.pool.get('ir.model.data')
1079     partner_address_obj = sugar_obj.pool.get('res.partner.address')
1080     model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
1081     model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name 
1082     sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
1083     sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
1084     sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
1085     if sugar_contact: 
1086         for contact in sugar_contact:
1087             model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
1088             if model_ids:
1089                 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
1090                 address_id = partner_address_obj.browse(cr, uid, model_id)
1091                 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
1092                 partner_contact_name = address_id.name
1093                 partner_contact_email = address_id.email
1094             else:
1095                 partner_contact_name = val.get('account_name')
1096     return partner_contact_name, partner_contact_email
1097
1098 def import_opportunities(sugar_obj, cr, uid, context=None):
1099     if not context:
1100         context = {}
1101     map_opportunity = {'id' : 'id',
1102         'name': 'name',
1103         'probability': 'probability',
1104         'partner_id/name': 'account_name',
1105         'title_action': 'next_step',
1106         'partner_address_id/name': 'partner_address_id/name',
1107         'planned_revenue': 'amount',
1108         'date_deadline': ['__datetime__', 'date_closed'],
1109         'user_id/id' : 'assigned_user_id',
1110         'stage_id/id' : 'stage_id/id',
1111         'type' : 'type',
1112         'categ_id/id': 'categ_id/id',
1113         'email_from': 'email_from'
1114     }
1115     lead_obj = sugar_obj.pool.get('crm.lead')
1116     partner_obj = sugar_obj.pool.get('res.partner')
1117     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1118     sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
1119     for val in sugar_data:
1120         partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
1121         if not partner_xml_id:
1122             raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
1123         partner_contact_name, partner_contact_email = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
1124         val['partner_address_id/name'] = partner_contact_name
1125         val['email_from'] = partner_contact_email
1126         val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))                    
1127         val['type'] = 'opportunity'
1128         val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context)
1129         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
1130         lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1131     return True
1132
1133 def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None):
1134     if not context:
1135         context = {}
1136     fields = ['name', 'type']
1137     name = 'Opportunity_' + sugar_val['sales_stage']
1138     data = [sugar_val['sales_stage'], 'Opportunity']
1139     return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_stage', name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context)
1140
1141 MAP_FIELDS = {'Opportunities':  #Object Mapping name
1142                     {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],  #Object to import before this table
1143                      'process' : import_opportunities,
1144                      },
1145               'Leads':
1146                     {'dependencies' : ['Users', 'Accounts', 'Contacts'],  #Object to import before this table
1147                      'process' : import_leads,
1148                     },
1149               'Contacts':
1150                     {'dependencies' : ['Users', 'Accounts'],  #Object to import before this table
1151                      'process' : import_partner_address,
1152                     },
1153               'Accounts':
1154                     {'dependencies' : ['Users'],  #Object to import before this table
1155                      'process' : import_partners,
1156                     },
1157               'Users': 
1158                     {'dependencies' : [],
1159                      'process' : import_users,
1160                     },
1161               'Documents': 
1162                     {'dependencies' : ['Users'],
1163                      'process' : import_documents,
1164                     },
1165               'Meetings': 
1166                     {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1167                      'process' : import_meetings,
1168                     },        
1169               'Tasks': 
1170                     {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1171                      'process' : import_tasks,
1172                     },  
1173               'Calls': 
1174                     {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities'],
1175                      'process' : import_calls,
1176                     },  
1177               'Projects': 
1178                     {'dependencies' : ['Users', 'Accounts', 'Contacts'],
1179                      'process' : import_projects,
1180                     },                        
1181               'Project Tasks': 
1182                     {'dependencies' : ['Users', 'Projects'],
1183                      'process' : import_project_tasks,
1184                     },
1185               'Bugs': 
1186                     {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
1187                      'process' : import_bug,
1188                     },                         
1189               'Claims': 
1190                     {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],
1191                      'process' : import_claims,
1192                     },                         
1193               'Emails': 
1194                     {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1195                      'process' : import_emails,
1196                     },    
1197               
1198               'Notes': 
1199                     {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1200                      'process' : import_history,
1201                     },  
1202               'Employees': 
1203                     {'dependencies' : ['Resources', 'Users'],
1204                      'process' : import_employees,
1205                     },                  
1206               'Resources': 
1207                     {'dependencies' : ['Users'],
1208                      'process' : import_resources,
1209                     },                                      
1210           }
1211
1212 class import_sugarcrm(osv.osv):
1213     """Import SugarCRM DATA"""
1214     
1215     _name = "import.sugarcrm"
1216     _description = __doc__
1217     _columns = {
1218         'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
1219         'user': fields.boolean('Users', help="If Users  are checked, SugarCRM Users data imported in OpenERP Users form"),
1220         'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
1221         'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM  Accounts data imported in OpenERP partners form"),
1222         'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
1223         'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
1224         'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
1225         'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
1226         'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
1227         'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
1228         'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
1229         'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
1230         'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
1231         'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
1232          'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
1233         'username': fields.char('User Name', size=64),
1234         'password': fields.char('Password', size=24),
1235     }
1236     _defaults = {#to be set to true, but easier for debugging
1237        'opportunity': True,
1238        'user' : True,
1239        'contact' : True,
1240        'account' : True,
1241         'employee' : True,
1242         'meeting' : True,
1243         'task' : True,
1244         'call' : True,
1245         'claim' : True,    
1246         'email' : True, 
1247         'project' : True,   
1248         'project_task': True,     
1249         'bug': True,
1250         'document': True
1251     }
1252     
1253     def get_key(self, cr, uid, ids, context=None):
1254         """Select Key as For which Module data we want import data."""
1255         if not context:
1256             context = {}
1257         key_list = []
1258         for current in self.browse(cr, uid, ids, context):
1259             if current.opportunity:
1260                 key_list.append('Opportunities')
1261             if current.user:
1262                 key_list.append('Users')
1263             if current.contact:
1264                 key_list.append('Contacts')
1265             if current.account:
1266                 key_list.append('Accounts') 
1267             if current.employee:
1268                 key_list.append('Employees')  
1269             if current.meeting:
1270                 key_list.append('Meetings')
1271             if current.task:
1272                 key_list.append('Tasks')
1273             if current.call:
1274                 key_list.append('Calls')
1275             if current.claim:
1276                 key_list.append('Claims')                
1277             if current.email:
1278                 key_list.append('Emails') 
1279             if current.project:
1280                 key_list.append('Projects')
1281             if current.project_task:
1282                 key_list.append('Project Tasks')
1283             if current.bug:
1284                 key_list.append('Bugs')
1285             if current.attachment:
1286                 key_list.append('Notes')     
1287             if current.document:
1288                 key_list.append('Documents')                                                  
1289         return key_list
1290
1291     def import_all(self, cr, uid, ids, context=None):
1292         """Import all sugarcrm data into openerp module"""
1293         if not context:
1294             context = {}
1295         keys = self.get_key(cr, uid, ids, context)
1296         imported = set() #to invoid importing 2 times the sames modules
1297         for key in keys:
1298             if not key in imported:
1299                 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1300                 MAP_FIELDS[key]['process'](self, cr, uid, context)
1301                 imported.add(key)
1302
1303         obj_model = self.pool.get('ir.model.data')
1304         model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1305         resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1306         return {
1307                 'view_type': 'form',
1308                 'view_mode': 'form',
1309                 'res_model': 'import.message',
1310                 'views': [(resource_id,'form')],
1311                 'type': 'ir.actions.act_window',
1312                 'target': 'new',
1313             }
1314
1315     def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1316         for dependency in dep:
1317             if not dependency in imported:
1318                 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1319                 dict[dependency]['process'](self, cr, uid, context)
1320                 imported.add(dependency)
1321         return True        
1322
1323 import_sugarcrm()