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