[IMP]: Integrate with import_base phonecall, task, projects
[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 import sugar
23 from tools.translate import _
24 from import_base.import_framework import *
25 from import_base.mapper import *
26
27 import base64
28 import pprint
29 pp = pprint.PrettyPrinter(indent=4)
30
31 #copy old import here
32 class related_ref(dbmapper):
33     def __init__(self, type):
34         self.type = type
35         
36     def __call__(self, external_val):
37         if external_val.get('parent_type') in self.type and external_val.get('parent_id'):
38             return self.parent.xml_id_exist(external_val['parent_type'], external_val['parent_id'])
39         return ''
40
41
42 class sugar_import(import_framework):
43     TABLE_CONTACT = 'Contacts'
44     TABLE_ACCOUNT = 'Accounts'
45     TABLE_USER = 'Users'
46     TABLE_EMPLOYEE = 'Employees'
47     TABLE_RESSOURCE = "resource"
48     TABLE_OPPORTUNITY = 'Opportunities'
49     TABLE_LEAD = 'Leads'
50     TABLE_STAGE = 'crm_stage'
51     TABLE_CALL = 'Calls'
52     TABLE_MEETING = 'Meetings'
53     TABLE_TASK = 'Tasks'
54     TABLE_PROJECT = 'Project'
55     TABLE_PROJECT_TASK = 'ProjectTask'
56     TABLE_CASE = 'Cases'
57     
58     
59     def initialize(self):
60         #login
61         PortType,sessionid = sugar.login(self.context.get('username',''), self.context.get('password',''), self.context.get('url',''))
62         self.context['port'] = PortType
63         self.context['session_id'] = sessionid
64         
65     def get_data(self, table):
66         return sugar.search(self.context.get('port'), self.context.get('session_id'), table)
67     """
68         Common import method
69     """
70     def get_category(self, val, model, name):
71         fields = ['name', 'object_id']
72         data = [name, model]
73         return self.import_object(fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)])
74
75     def get_job_title(self, dict, salutation):
76         fields = ['shortcut', 'name', 'domain']
77         if salutation:
78             data = [salutation, salutation, 'Contact']
79             return self.import_object(fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)])
80
81     def get_campaign_id(self, dict, val):
82         fields = ['name']
83         data = [val]
84         return self.import_object(fields, data, 'crm.case.resource.type', 'crm_campaign', val)
85     
86     def get_all_states(self, external_val, country_id):
87         """Get states or create new state unless country_id is False"""
88         state_code = external_val[0:3] #take the tree first char
89         fields = ['country_id/id', 'name', 'code']
90         data = [country_id, external_val, state_code]
91         if country_id:
92             return self.import_object(fields, data, 'res.country.state', 'country_state', external_val) 
93         return False
94
95     def get_all_countries(self, val):
96         """Get Country, if no country match do not create anything, to avoid duplicate country code"""
97         return self.mapped_id_if_exist('res.country', [('name', 'ilike', val)], 'country', val)
98     
99     def get_float_time(self, dict, hour, min):
100         min = int(min) * 100 / 60
101         return "%s.%i" % (hour, min)
102     """
103     import Projects
104     """
105     project_state = {
106             'Draft' : 'draft',
107             'In Review': 'open',
108             'Published': 'close'
109      }
110     def import_project_account(self, val):
111         partner_id = False
112         partner_invoice_id = False        
113         model_obj = self.obj.pool.get('ir.model.data')
114         partner_obj = self.obj.pool.get('res.partner')
115         partner_address_obj = self.obj.pool.get('res.partner.address')
116         sugar_project_account = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
117         for account_id in sugar_project_account:
118             partner_id = self.get_mapped_id(self.TABLE_ACCOUNT, account_id)
119             partner_invoice_ids= partner_address_obj.search(self.cr, self.uid, [('partner_id', '=', partner_id), ('type', '=', 'invoice')])
120             if partner_invoice_ids:
121                 partner_invoice_id = partner_invoice_ids[0] 
122         return partner_id, partner_invoice_id
123            
124     def import_project(self, val):
125         partner_id, partner_invoice_id = self.import_project_account(val)    
126         val['partner_id/.id'] = partner_id
127         val['contact_id/.id'] = partner_invoice_id
128         return val
129     
130     def get_project_mapping(self):
131         return { 
132                 'model' : 'project.project',
133                 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
134                 'hook' : self.import_project,
135                 'map' : {
136                     'name': 'name',
137                     'date_start': 'estimated_start_date',
138                     'date': 'estimated_end_date',
139                     'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
140                     'partner_id/.id': 'partner_id/.id',
141                     'contact_id/.id': 'contact_id/.id',
142                     'state': map_val('status', self.project_state)
143                 }
144             }
145     
146     """
147     import Tasks
148     """
149     task_state = {
150             'Completed' : 'done',
151             'Not Started':'draft',
152             'In Progress': 'open',
153             'Pending Input': 'draft',
154             'deferred': 'cancel'
155         }
156         
157     def get_task_mapping(self):
158         return { 
159                 'model' : 'crm.meeting',
160                 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
161                 'map' : {
162                     'name': 'name',
163                     'date': 'date_start' or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
164                     'date_deadline': 'date_due' or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
165                     'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
166                     'categ_id/id': call(self.get_category, 'crm.meeting', 'Tasks'),
167                     'partner_id/id': related_ref(self.TABLE_ACCOUNT),
168                     'partner_address_id/id': related_ref(self.TABLE_CONTACT),
169                     'state': map_val('status', self.task_state)
170                 }
171             }
172        
173     """
174     import Calls
175     """     
176     call_state = {   
177             'Planned' : 'open',
178             'Held':'done',
179             'Not Held': 'pending',
180         }
181     
182     def get_calls_mapping(self):
183         return { 
184                 'model' : 'crm.phonecall',
185                 'dependencies' : [self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD],
186                 'map' : {
187                     'name': 'name',
188                     'date': 'date_start',
189                     'duration': call(self.get_float_time, value('duration_hours'), value('duration_minutes')),
190                     'user_id/id':  ref(self.TABLE_USER, 'assigned_user_id'),
191                     'partner_id/id': related_ref(self.TABLE_ACCOUNT),
192                     'partner_address_id/id': related_ref(self.TABLE_CONTACT),
193                     'categ_id/id': call(self.get_category, 'crm.phonecall', value('direction')),
194                     'opportunity_id/id': related_ref(self.TABLE_OPPORTUNITY),
195                     'description': 'description',   
196                     'state': map_val('status', self.call_state)                      
197                 }
198             }        
199         
200     """
201         import meeting
202     """
203     meeting_state = {
204             'Planned' : 'draft',
205             'Held': 'open',
206             'Not Held': 'draft', 
207         }
208     
209     def get_alarm_id(self, dict_val, val):
210         alarm_dict = {
211             '60': '1 minute before',
212             '300': '5 minutes before',
213             '600': '10 minutes before',
214             '900': '15 minutes before',
215             '1800':'30 minutes before',
216             '3600': '1 hour before',
217         }
218         return self.mapped_id_if_exist('res.alarm', [('name', 'like', alarm_dict.get(val))], 'alarm', val)
219     
220     #TODO attendees
221     def get_meeting_mapping(self):
222         return { 
223                 'model' : 'crm.meeting',
224                 'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD],
225                 'map' : {
226                     'name': 'name',
227                     'date': 'date_start',
228                     'duration': call(self.get_float_time, value('duration_hours'), value('duration_minutes')),
229                     'location': 'location',
230                     'alarm_id/id': call(self.get_alarm_id, value('reminder_time')),
231                     'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
232                     'partner_id/id': related_ref(self.TABLE_ACCOUNT),
233                     'partner_address_id/id': related_ref(self.TABLE_CONTACT),
234                     'state': map_val('status', self.meeting_state)
235                 }
236             }
237     
238     """
239         import Opportunity
240     """
241     def get_opportunity_status(self, sugar_val):
242         fields = ['name', 'type']
243         name = 'Opportunity_' + sugar_val['sales_stage']
244         data = [sugar_val['sales_stage'], 'Opportunity']
245         return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])])
246     
247     def import_opportunity_contact(self, val):
248         sugar_opportunities_contact = set(sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
249             
250         partner_contact_id = False 
251         partner_contact_email = False       
252         partner_address_obj = self.obj.pool.get('res.partner.address')
253         partner_xml_id = self.name_exist(self.TABLE_ACCOUNT, val['account_name'], 'res.partner')
254         
255         for contact in sugar_opportunities_contact:
256             address_id = self.get_mapped_id(self.TABLE_CONTACT, contact)
257             if address_id:                    
258                 address = partner_address_obj.browse(self.cr, self.uid, address_id)
259                 partner_name = address.partner_id and address.partner_id.name or False
260                 if not partner_name: #link with partner id 
261                     fields = ['partner_id/id']
262                     data = [partner_xml_id]
263                     self.import_object(fields, data, 'res.partner.address', self.TABLE_CONTACT, contact, self.DO_NOT_FIND_DOMAIN)
264                 if not partner_name or partner_name == val.get('account_name'):
265                     partner_contact_id = self.xml_id_exist(self.TABLE_CONTACT, contact)
266                     partner_contact_email = address.email
267         return partner_contact_id, partner_contact_email
268
269     def import_opp(self, val):    
270         partner_contact_name, partner_contact_email = self.import_opportunity_contact(val)
271         val['partner_address_id/id'] = partner_contact_name
272         val['email_from'] = partner_contact_email
273         return val
274     
275     def get_opp_mapping(self):
276         return {
277             'model' : 'crm.lead',
278             'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT],
279             'hook' : self.import_opp,
280             'map' :  {
281                 'name': 'name',
282                 'probability': 'probability',
283                 'partner_id/id': refbyname(self.TABLE_ACCOUNT, 'account_name', 'res.partner'),
284                 'title_action': 'next_step',
285                 'partner_address_id/id': 'partner_address_id/id',
286                 'planned_revenue': 'amount',
287                 'date_deadline': 'date_closed',
288                 'user_id/id' : ref(self.TABLE_USER, 'assigned_user_id'),
289                 'stage_id/id' : self.get_opportunity_status,
290                 'type' : const('opportunity'),
291                 'categ_id/id': call(self.get_category, 'crm.lead', value('opportunity_type')),
292                 'email_from': 'email_from',
293                 'state' : const('open'), #TODO
294             }
295         }
296     
297     """
298         import lead
299     """
300     def get_lead_status(self, sugar_val):
301         fields = ['name', 'type']
302         name = 'lead_' + sugar_val.get('status', '')
303         data = [sugar_val.get('status', ''), 'lead']
304         return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val.get('status', ''))])
305
306     lead_state = {
307         'New' : 'draft',
308         'Assigned':'open',
309         'In Progress': 'open',
310         'Recycled': 'cancel',
311         'Dead': 'done',
312         'Converted': 'done',
313     }
314
315     
316     def import_lead(self, val):
317         if val.get('opportunity_id'): #if lead is converted into opp, don't import as lead
318             return False
319         if val.get('primary_address_country'):
320             country_id = self.get_all_countries(val.get('primary_address_country'))
321             val['country_id/id'] =  country_id
322             val['state_id/id'] =  self.get_all_states(val.get('primary_address_state'), country_id)
323         return val
324     
325     def get_lead_mapping(self):
326         return {
327             'model' : 'crm.lead',
328             'hook' : self.import_lead,
329             'map' : {
330                 'name': concat('first_name', 'last_name'),
331                 'contact_name': concat('first_name', 'last_name'),
332                 'description': ppconcat('description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'),
333                 'partner_name': 'account_name',
334                 'email_from': 'email1',
335                 'phone': 'phone_work',
336                 'mobile': 'phone_mobile',
337                 'title/id': call(self.get_job_title, value('salutation')),
338                 'function':'title',
339                 'street': 'primary_address_street',
340                 'street2': 'alt_address_street',
341                 'zip': 'primary_address_postalcode',
342                 'city':'primary_address_city',
343                 'user_id/id' : ref(self.TABLE_USER, 'assigned_user_id'),
344                 'stage_id/id' : self.get_lead_status,
345                 'type' : const('lead'),
346                 'state': map_val('status', self.lead_state) ,
347                 'fax': 'phone_fax',
348                 'referred': 'refered_by',
349                 'optout': 'do_not_call',
350                 'type_id/id': call(self.get_campaign_id, value('lead_source')),
351                 'country_id/id': 'country_id/id',
352                 'state_id/id': 'state_id/id'
353                 } 
354         }
355     
356     """
357         import contact
358     """
359     def get_email(self, val):
360         return val.get('email1') + ','+ val.get('email2')
361     
362     def import_contact(self, val):
363         if val.get('primary_address_country'):
364             country_id = self.get_all_countries(val.get('primary_address_country'))
365             state = self.get_all_states(val.get('primary_address_state'), country_id)
366             val['country_id/id'] =  country_id
367             val['state_id/id'] =  state  
368         return val    
369         
370     def get_contact_mapping(self):
371         return { 
372             'model' : 'res.partner.address',
373             'dependencies' : [self.TABLE_ACCOUNT],
374             'hook' : self.import_contact,
375             'map' :  {
376                 'name': concat('first_name', 'last_name'),
377                 'partner_id/id': ref(self.TABLE_ACCOUNT,'account_id'),
378                 'phone': 'phone_work',
379                 'mobile': 'phone_mobile',
380                 'fax': 'phone_fax',
381                 'function': 'title',
382                 'street': 'primary_address_street',
383                 'zip': 'primary_address_postalcode',
384                 'city': 'primary_address_city',
385                 'country_id/id': 'country_id/id',
386                 'state_id/id': 'state_id/id',
387                 'email': self.get_email,
388                 'type': const('contact')
389             }
390         }
391     
392     """ 
393         import Account
394     """
395     def get_address_type(self, val, type):
396         if type == 'invoice':
397             type_address = 'billing'
398         else:
399             type_address = 'shipping'     
400     
401         map_partner_address = {
402             'name': 'name',
403             'phone': 'phone_office',
404             'mobile': 'phone_mobile',
405             'fax': 'phone_fax',
406             'type': 'type',
407             'street': type_address + '_address_street',
408             'zip': type_address +'_address_postalcode',
409             'city': type_address +'_address_city',
410              'country_id/id': 'country_id/id',
411              'type': 'type',
412             }
413         
414         if val.get(type_address +'_address_country'):
415             country_id = self.get_all_countries(val.get(type_address +'_address_country'))
416             state = self.get_all_states(val.get(type_address +'_address_state'), country_id)
417             val['country_id/id'] =  country_id
418             val['state_id/id'] =  state
419         val['type'] = type
420         val['id_new'] = val['id'] + '_address_' + type
421         return self.import_object_mapping(map_partner_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id_new'], self.DO_NOT_FIND_DOMAIN) 
422     
423     def get_partner_address(self, val):
424         address_id=[]
425         type_dict = {'billing_address_street' : 'invoice', 'shipping_address_street' : 'delivery'}
426         for key, type_value in type_dict.items():
427             if val.get(key):
428                 id = self.get_address_type(val, type_value)
429                 address_id.append(id)
430           
431         return ','.join(address_id)
432     
433     def get_partner_mapping(self):
434         return {
435                 'model' : 'res.partner',
436                 'dependencies' : [self.TABLE_USER],
437                 'map' : {
438                     'name': 'name',
439                     'website': 'website',
440                     'user_id/id': ref(self.TABLE_USER,'assigned_user_id'),
441                     'ref': 'sic_code',
442                     'comment': ppconcat('description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'),
443                     'customer': const('1'),
444                     'supplier': const('0'),
445                     'address/id':'address/id', 
446                     'parent_id/id_parent' : 'parent_id',
447                     'address/id' : self.get_partner_address,
448                 }
449         }
450
451     """
452         import Employee
453     """
454     def get_ressource(self, val):
455         map_resource = { 
456             'name': concat('first_name', 'last_name'),
457         }        
458         return self.import_object_mapping(map_resource, val, 'resource.resource', self.TABLE_RESSOURCE, val['id'], self.DO_NOT_FIND_DOMAIN)
459     
460     def get_job_id(self, val):
461         fields = ['name']
462         data = [val.get('title')]
463         return self.import_object(fields, data, 'hr.job', 'hr_job', val.get('title'))
464
465     def get_user_address(self, val):
466         map_user_address = {
467             'name': concat('first_name', 'last_name'),
468             'city': 'address_city',
469             'country_id/id': 'country_id/id',
470             'state_id/id': 'state_id/id',
471             'street': 'address_street',
472             'zip': 'address_postalcode',
473             'fax': 'fax',
474         }
475         
476         if val.get('address_country'):
477             country_id = self.get_all_countries(val.get('address_country'))
478             state_id = self.get_all_states(val.get('address_state'), country_id)
479             val['country_id/id'] =  country_id
480             val['state_id/id'] =  state_id
481             
482         return self.import_object_mapping(map_user_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id'], self.DO_NOT_FIND_DOMAIN)
483     
484     def get_employee_mapping(self):
485         return {
486             'model' : 'hr.employee',
487             'dependencies' : [self.TABLE_USER],
488             'map' : {
489                 'resource_id/id': self.get_ressource, 
490                 'name': concat('first_name', 'last_name'),
491                 'work_phone': 'phone_work',
492                 'mobile_phone':  'phone_mobile',
493                 'user_id/id': ref(self.TABLE_USER, 'id'), 
494                 'address_home_id/id': self.get_user_address,
495                 'notes': 'description',
496                 'job_id/id': self.get_job_id,
497             }
498      }
499     
500     """
501         import user
502     """  
503     def import_user(self, val):
504         user_obj = self.obj.pool.get('res.users')
505         user_ids = user_obj.search(self.cr, self.uid, [('login', '=', val.get('user_name'))])
506         if user_ids: 
507             val['.id'] = str(user_ids[0])
508         else:
509             val['password'] = 'sugarcrm' #default password for all user #TODO needed in documentation
510             
511         val['context_lang'] = self.context.get('lang','en_US')
512         return val
513     
514     def get_users_department(self, val):
515         dep = val.get('department')
516         fields = ['name']
517         data = [dep]
518         if not dep:
519             return False
520         return self.import_object(fields, data, 'hr.department', 'hr_department_user', dep)
521
522     def get_user_mapping(self):
523         return {
524             'model' : 'res.users',
525             'hook' : self.import_user,
526             'map' : { 
527                 'name': concat('first_name', 'last_name'),
528                 'login': 'user_name',
529                 'context_lang' : 'context_lang',
530                 'password' : 'password',
531                 '.id' : '.id',
532                 'context_department_id/id': self.get_users_department,
533             }
534         }
535
536     def get_mapping(self):
537         return {
538             self.TABLE_USER : self.get_user_mapping(),
539             self.TABLE_EMPLOYEE : self.get_employee_mapping(),
540             self.TABLE_ACCOUNT : self.get_partner_mapping(),
541             self.TABLE_CONTACT : self.get_contact_mapping(),
542             self.TABLE_LEAD : self.get_lead_mapping(),
543             self.TABLE_OPPORTUNITY : self.get_opp_mapping(),
544             self.TABLE_MEETING : self.get_meeting_mapping(),
545             self.TABLE_CALL : self.get_calls_mapping(),
546             self.TABLE_TASK : self.get_task_mapping(),
547             self.TABLE_PROJECT : self.get_project_mapping(),
548         }
549
550
551
552
553
554 class import_sugarcrm(osv.osv):
555     """Import SugarCRM DATA"""
556     
557     _name = "import.sugarcrm"
558     _description = __doc__
559     _columns = {
560         'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
561         'user': fields.boolean('Users', help="If Users  are checked, SugarCRM Users data imported in OpenERP Users form"),
562         'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
563         'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM  Accounts data imported in OpenERP partners form"),
564         'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
565         'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
566         'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
567         'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
568         'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
569         'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
570         'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
571         'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
572         'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
573         'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
574         'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
575         'username': fields.char('User Name', size=64),
576         'password': fields.char('Password', size=24),
577     }
578     _defaults = {#to be set to true, but easier for debugging
579        'opportunity': False,
580        'user' : False,
581        'contact' : False,
582        'account' : False,
583         'employee' : False,
584         'meeting' : False,
585         'task' : False,
586         'call' : False,
587         'claim' : False,    
588         'email' : False, 
589         'project' : False,   
590         'project_task': False,     
591         'bug': False,
592         'document': False
593     }
594     
595     def get_key(self, cr, uid, ids, context=None):
596         """Select Key as For which Module data we want import data."""
597         if not context:
598             context = {}
599         key_list = []
600         for current in self.browse(cr, uid, ids, context):
601             if current.opportunity:
602                 key_list.append('Leads')
603                 key_list.append('Opportunities')
604             if current.user:
605                 key_list.append('Users')
606             if current.contact:
607                 key_list.append('Contacts')
608             if current.account:
609                 key_list.append('Accounts') 
610             if current.employee:
611                 key_list.append('Employees')  
612             if current.meeting:
613                 key_list.append('Meetings')
614             if current.task:
615                 key_list.append('Tasks')
616             if current.call:
617                 key_list.append('Calls')
618             if current.claim:
619                 key_list.append('Claims')                
620             if current.email:
621                 key_list.append('Emails') 
622             if current.project:
623                 key_list.append('Project')
624             if current.project_task:
625                 key_list.append('Project Tasks')
626             if current.bug:
627                 key_list.append('Bugs')
628             if current.attachment:
629                 key_list.append('Notes')     
630             if current.document:
631                 key_list.append('Documents')                                                  
632         return key_list
633
634     def import_all(self, cr, uid, ids, context=None):
635         
636 #        """Import all sugarcrm data into openerp module"""
637         keys = self.get_key(cr, uid, ids, context)
638         imp = sugar_import(self, cr, uid, "sugarcrm", "import_sugarcrm", context)
639         imp.import_all(keys)
640         
641         obj_model = self.pool.get('ir.model.data')
642         model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
643         resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
644         return {
645                 'view_type': 'form',
646                 'view_mode': 'form',
647                 'res_model': 'import.message',
648                 'views': [(resource_id,'form')],
649                 'type': 'ir.actions.act_window',
650                 'target': 'new',
651             }
652         
653 import_sugarcrm()