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