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