1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
21 from osv import fields, osv
23 from tools.translate import _
24 from import_base.import_framework import *
25 from import_base.mapper import *
26 from datetime import datetime
29 pp = pprint.PrettyPrinter(indent=4)
32 class related_ref(dbmapper):
33 def __init__(self, type):
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'])
42 class sugar_import(import_framework):
43 TABLE_CONTACT = 'Contacts'
44 TABLE_ACCOUNT = 'Accounts'
46 TABLE_EMPLOYEE = 'Employees'
47 TABLE_RESSOURCE = "resource"
48 TABLE_OPPORTUNITY = 'Opportunities'
50 TABLE_STAGE = 'crm_stage'
52 TABLE_MEETING = 'Meetings'
54 TABLE_PROJECT = 'Project'
55 TABLE_PROJECT_TASK = 'ProjectTask'
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
65 def get_data(self, table):
66 return sugar.search(self.context.get('port'), self.context.get('session_id'), table)
70 def get_category(self, val, model, name):
71 fields = ['name', 'object_id']
73 return self.import_object(fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)])
75 def get_job_title(self, dict, salutation):
76 fields = ['shortcut', 'name', 'domain']
78 data = [salutation, salutation, 'Contact']
79 return self.import_object(fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)])
81 def get_campaign_id(self, dict, val):
84 return self.import_object(fields, data, 'crm.case.resource.type', 'crm_campaign', val)
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]
92 return self.import_object(fields, data, 'res.country.state', 'country_state', external_val)
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)
99 def get_float_time(self, dict, hour, min):
100 min = int(min) * 100 / 60
101 return "%s.%i" % (hour, min)
107 project_task_state = {
108 'Not Started': 'draft',
109 'In Progress': 'open',
111 'Pending Input': 'pending',
112 'Deferred': 'cancelled',
115 def get_project_task_priority(self, val):
121 return priority_dict.get(val.get('priority'), '')
123 def get_project_task_mapping(self):
125 'model' : 'project.task',
126 'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT],
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)
152 def import_project_account(self, val):
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
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
172 def get_project_mapping(self):
174 'model' : 'project.project',
175 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
176 'hook' : self.import_project,
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)
192 'Completed' : 'done',
193 'Not Started':'draft',
194 'In Progress': 'open',
195 'Pending Input': 'draft',
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'),
204 def get_task_mapping(self):
206 'model' : 'crm.meeting',
207 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
208 'hook' : self.import_task,
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)
227 'Not Held': 'pending',
230 def get_calls_mapping(self):
232 'model' : 'crm.phonecall',
233 'dependencies' : [self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD],
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)
257 def get_alarm_id(self, dict_val, val):
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',
266 return self.mapped_id_if_exist('res.alarm', [('name', 'like', alarm_dict.get(val))], 'alarm', val)
269 def get_meeting_mapping(self):
271 'model' : 'crm.meeting',
272 'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD],
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)
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'])])
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))
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')
303 for contact in sugar_opportunities_contact:
304 address_id = self.get_mapped_id(self.TABLE_CONTACT, contact)
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
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
323 def get_opp_mapping(self):
325 'model' : 'crm.lead',
326 'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT],
327 'hook' : self.import_opp,
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
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', ''))])
357 'In Progress': 'open',
358 'Recycled': 'cancel',
364 def import_lead(self, val):
365 if val.get('opportunity_id'): #if lead is converted into opp, don't import as lead
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)
373 def get_lead_mapping(self):
375 'model' : 'crm.lead',
376 'hook' : self.import_lead,
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')),
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) ,
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'
407 def get_email(self, val):
408 return val.get('email1') + ','+ val.get('email2')
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
418 def get_contact_mapping(self):
420 'model' : 'res.partner.address',
421 'dependencies' : [self.TABLE_ACCOUNT],
422 'hook' : self.import_contact,
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',
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')
443 def get_address_type(self, val, type):
444 if type == 'invoice':
445 type_address = 'billing'
447 type_address = 'shipping'
449 map_partner_address = {
451 'phone': 'phone_office',
452 'mobile': 'phone_mobile',
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',
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
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)
471 def get_partner_address(self, val):
473 type_dict = {'billing_address_street' : 'invoice', 'shipping_address_street' : 'delivery'}
474 for key, type_value in type_dict.items():
476 id = self.get_address_type(val, type_value)
477 address_id.append(id)
479 return ','.join(address_id)
481 def get_partner_mapping(self):
483 'model' : 'res.partner',
484 'dependencies' : [self.TABLE_USER],
487 'website': 'website',
488 'user_id/id': ref(self.TABLE_USER,'assigned_user_id'),
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,
502 def get_ressource(self, val):
504 'name': concat('first_name', 'last_name'),
506 return self.import_object_mapping(map_resource, val, 'resource.resource', self.TABLE_RESSOURCE, val['id'], self.DO_NOT_FIND_DOMAIN)
508 def get_job_id(self, val):
510 data = [val.get('title')]
511 return self.import_object(fields, data, 'hr.job', 'hr_job', val.get('title'))
513 def get_user_address(self, val):
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',
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
530 return self.import_object_mapping(map_user_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id'], self.DO_NOT_FIND_DOMAIN)
532 def get_employee_mapping(self):
534 'model' : 'hr.employee',
535 'dependencies' : [self.TABLE_USER],
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,
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'))])
555 val['.id'] = str(user_ids[0])
557 val['password'] = 'sugarcrm' #default password for all user #TODO needed in documentation
559 val['context_lang'] = self.context.get('lang','en_US')
562 def get_users_department(self, val):
563 dep = val.get('department')
568 return self.import_object(fields, data, 'hr.department', 'hr_department_user', dep)
570 def get_user_mapping(self):
572 'model' : 'res.users',
573 'hook' : self.import_user,
575 'name': concat('first_name', 'last_name'),
576 'login': 'user_name',
577 'context_lang' : 'context_lang',
578 'password' : 'password',
580 'context_department_id/id': self.get_users_department,
584 def get_mapping(self):
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()
603 class import_sugarcrm(osv.osv):
604 """Import SugarCRM DATA"""
606 _name = "import.sugarcrm"
607 _description = __doc__
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),
627 _defaults = {#to be set to true, but easier for debugging
628 'opportunity': False,
639 'project_task': False,
644 def get_key(self, cr, uid, ids, context=None):
645 """Select Key as For which Module data we want import data."""
649 for current in self.browse(cr, uid, ids, context):
650 if current.opportunity:
651 key_list.append('Leads')
652 key_list.append('Opportunities')
654 key_list.append('Users')
656 key_list.append('Contacts')
658 key_list.append('Accounts')
660 key_list.append('Employees')
662 key_list.append('Meetings')
664 key_list.append('Tasks')
666 key_list.append('Calls')
668 key_list.append('Claims')
670 key_list.append('Emails')
672 key_list.append('Project')
673 if current.project_task:
674 key_list.append('ProjectTask')
676 key_list.append('Bugs')
677 if current.attachment:
678 key_list.append('Notes')
680 key_list.append('Documents')
683 def import_all(self, cr, uid, ids, context=None):
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)
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'])
696 'res_model': 'import.message',
697 'views': [(resource_id,'form')],
698 'type': 'ir.actions.act_window',