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