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 from papyon.service.SOAPService import url_split
30 pp = pprint.PrettyPrinter(indent=4)
33 class related_ref(dbmapper):
34 def __init__(self, type):
37 def __call__(self, external_val):
38 if external_val.get('parent_type') in self.type and external_val.get('parent_id'):
39 return self.parent.xml_id_exist(external_val['parent_type'], external_val['parent_id'])
42 class sugar_import(import_framework):
44 TABLE_CONTACT = 'Contacts'
45 TABLE_ACCOUNT = 'Accounts'
47 TABLE_EMPLOYEE = 'Employees'
48 TABLE_RESSOURCE = "resource"
49 TABLE_OPPORTUNITY = 'Opportunities'
51 TABLE_STAGE = 'crm_stage'
52 TABLE_ATTENDEE = 'calendar_attendee'
54 TABLE_MEETING = 'Meetings'
56 TABLE_PROJECT = 'Project'
57 TABLE_PROJECT_TASK = 'ProjectTask'
61 TABLE_EMAIL = 'Emails'
62 TABLE_COMPAIGN = 'Campaigns'
63 TABLE_DOCUMENT = 'DocumentRevisions'
64 TABLE_HISTORY_ATTACHMNET = 'history_attachment'
68 PortType,sessionid = sugar.login(self.context.get('username',''), self.context.get('password',''), self.context.get('url',''))
70 raise osv.except_osv(_('Error !'), _('Authentication error !\nBad Username or Password or bad SugarSoap Api url !'))
71 self.context['port'] = PortType
72 self.context['session_id'] = sessionid
74 def get_data(self, table):
75 return sugar.search(self.context.get('port'), self.context.get('session_id'), table, 0, 500)
80 def get_category(self, val, model, name):
81 fields = ['name', 'object_id']
83 return self.import_object(fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)])
85 def get_job_title(self, dict, salutation):
86 fields = ['shortcut', 'name', 'domain']
88 data = [salutation, salutation, 'Contact']
89 return self.import_object(fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)])
91 def get_channel_id(self, dict, val):
96 return self.import_object(fields, data, 'res.partner.canal', 'crm_channel', val)
98 def get_all_states(self, external_val, country_id):
99 """Get states or create new state unless country_id is False"""
100 state_code = external_val[0:3] #take the tree first char
101 fields = ['country_id/id', 'name', 'code']
102 data = [country_id, external_val, state_code]
104 return self.import_object(fields, data, 'res.country.state', 'country_state', external_val)
107 def get_all_countries(self, val):
108 """Get Country, if no country match do not create anything, to avoid duplicate country code"""
109 return self.mapped_id_if_exist('res.country', [('name', 'ilike', val)], 'country', val)
111 def get_float_time(self, dict, hour, min):
112 min = int(min) * 100 / 60
113 return "%s.%i" % (hour, min)
119 def import_document(self, val):
120 File,Filename = sugar.get_document_revision_search(self.context.get('port'), self.context.get('session_id'), val.get('id'))
121 val['datas'] = base64.encodestring(File)
122 val['datas_fname'] = Filename
125 def get_document_mapping(self):
127 'model' : 'ir.attachment',
128 'dependencies' : [self.TABLE_USER],
129 'hook' : self.import_document,
130 'map' : {'name':'filename',
131 'description': ppconcat('description'),
133 'datas_fname': 'datas_fname',
142 def get_email_attachment(self, val):
143 File, Filename = sugar.attachment_search(self.context.get('port'), self.context.get('session_id'), self.TABLE_EMAIL, val.get('id'))
144 attach_xml_id = False
146 fields = ['name', 'datas', 'datas_fname','res_id', 'res_model']
147 name = 'attachment_'+ (Filename or val.get('name'))
148 datas = [Filename or val.get('name'), File, Filename, val.get('res_id'),val.get('model',False)]
149 attach_xml_id = self.import_object(fields, datas, 'ir.attachment', self.TABLE_HISTORY_ATTACHMNET, name, [('res_id', '=', val.get('res_id'), ('model', '=', val.get('model')))])
152 def import_email(self, val):
153 vals = sugar.email_search(self.context.get('port'), self.context.get('session_id'), self.TABLE_EMAIL, val.get('id'))
154 model_obj = self.obj.pool.get('ir.model.data')
156 xml_id = self.xml_id_exist(val.get('parent_type'), val.get('parent_id'))
157 model_ids = model_obj.search(self.cr, self.uid, [('name', 'like', xml_id)])
159 model = model_obj.browse(self.cr, self.uid, model_ids)[0]
160 if model.model == 'res.partner':
161 val['partner_id/.id'] = model.res_id
163 val['res_id'] = model.res_id
164 val['model'] = model.model
167 def get_email_mapping(self):
169 'model' : 'mailgate.message',
170 'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT, self.TABLE_PROJECT_TASK, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD, self.TABLE_OPPORTUNITY, self.TABLE_MEETING, self.TABLE_CALL],
171 'hook' : self.import_email,
172 'map' : {'name':'name',
173 'history' : const("1"),
175 'email_from': 'from_addr_name',
176 'email_to': 'to_addrs_names',
177 'email_cc': 'cc_addrs_names',
178 'email_bcc': 'bcc_addrs_names',
179 'message_id': 'message_id',
182 'partner_id/.id': 'partner_id/.id',
183 'attachment_ids/id': self.get_email_attachment,
184 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
185 'description': ppconcat('description', 'description_html'),
190 import History(Notes)
192 def get_note_attachment(self, val):
193 File, Filename = sugar.attachment_search(self.context.get('port'), self.context.get('session_id'), self.TABLE_NOTE, val.get('id'))
194 attach_xml_id = False
196 fields = ['name', 'datas', 'datas_fname','res_id', 'res_model']
197 name = 'attachment_'+ (Filename or val.get('name'))
198 datas = [Filename or val.get('name'), File, Filename, val.get('res_id'),val.get('model',False)]
199 attach_xml_id = self.import_object(fields, datas, 'ir.attachment', self.TABLE_HISTORY_ATTACHMNET, name, [('res_id', '=', val.get('res_id'), ('model', '=', val.get('model')))])
202 def import_history(self, val):
203 model_obj = self.obj.pool.get('ir.model.data')
204 xml_id = self.xml_id_exist(val.get('parent_type'), val.get('parent_id'))
205 model_ids = model_obj.search(self.cr, self.uid, [('name', 'like', xml_id)])
207 model = model_obj.browse(self.cr, self.uid, model_ids)[0]
208 if model.model == 'res.partner':
209 val['partner_id/.id'] = model.res_id
212 val['res_id'] = model.res_id
213 val['model'] = model.model
216 def get_history_mapping(self):
218 'model' : 'mailgate.message',
219 'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT, self.TABLE_PROJECT_TASK, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD, self.TABLE_OPPORTUNITY, self.TABLE_MEETING, self.TABLE_CALL],
220 'hook' : self.import_history,
223 'date': 'date_entered',
224 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
225 'description': ppconcat('description', 'description_html'),
228 'attachment_ids/id': self.get_note_attachment,
229 'partner_id/.id' : 'partner_id/.id',
230 'history' : 'history',
237 def get_claim_priority(self, val):
243 return priority_dict.get(val.get('priority'), '')
245 def get_contact_info_from_account(self, val):
246 partner_id = self.get_mapped_id(self.TABLE_ACCOUNT, val.get('account_id'))
247 partner_address_id = False
248 partner_phone = False
249 partner_email = False
250 partner = self.obj.pool.get('res.partner').browse(self.cr, self.uid, [partner_id])[0]
251 if partner.address and partner.address[0]:
252 address = partner.address[0]
253 partner_address_id = address.id
254 partner_phone = address.phone
255 partner_email = address.email
256 return partner_address_id, partner_phone,partner_email
258 def import_crm_claim(self, val):
259 partner_address_id, partner_phone,partner_email = self.get_contact_info_from_account(val)
260 val['partner_address_id/.id'] = partner_address_id
261 val['partner_phone'] = partner_phone
262 val['email_from'] = partner_email
265 def get_crm_claim_mapping(self):
267 'model' : 'crm.claim',
268 'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD],
269 'hook' : self.import_crm_claim,
271 'name': concat('case_number','name', delimiter='-'),
272 'date': 'date_entered',
273 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
274 'description': ppconcat('description', 'resolution', 'work_log'),
275 'partner_id/id': ref(self.TABLE_ACCOUNT, 'account_id'),
276 'partner_address_id/.id': 'partner_address_id/.id',
277 'categ_id/id': call(self.get_category, 'crm.claim', value('type')),
278 'partner_phone': 'partner_phone',
279 'email_from': 'email_from',
280 'priority': self.get_claim_priority,
281 'state': map_val('status', self.project_issue_state)
285 Import Project Issue(Bugs)
287 project_issue_state = {
291 'Pending': 'pending',
292 'Rejected': 'cancel',
295 def get_project_issue_priority(self, val):
302 return priority_dict.get(val.get('priority'), '')
304 def get_bug_project_id(self, dict, val):
307 return self.import_object(fields, data, 'project.project', 'project_issue', val)
309 def get_project_issue_mapping(self):
311 'model' : 'project.issue',
312 'dependencies' : [self.TABLE_USER],
314 'name': concat('bug_number', 'name', delimiter='-'),
315 'project_id/id': call(self.get_bug_project_id, 'sugarcrm_bugs'),
316 'categ_id/id': call(self.get_category, 'project.issue', value('type')),
317 'description': ppconcat('description', 'source', 'resolution', 'work_log', 'found_in_release', 'release_name', 'fixed_in_release_name', 'fixed_in_release'),
318 'priority': self.get_project_issue_priority,
319 'state': map_val('status', self.project_issue_state),
320 'assigned_to/id' : ref(self.TABLE_USER, 'assigned_user_id'),
327 project_task_state = {
328 'Not Started': 'draft',
329 'In Progress': 'open',
331 'Pending Input': 'pending',
332 'Deferred': 'cancelled',
335 def get_project_task_priority(self, val):
341 return priority_dict.get(val.get('priority'), '')
343 def get_project_task_mapping(self):
345 'model' : 'project.task',
346 'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT],
349 'date_start': 'date_start',
350 'date_end': 'date_finish',
351 'project_id/id': ref(self.TABLE_PROJECT, 'project_id'),
352 'planned_hours': 'estimated_effort',
353 'priority': self.get_project_task_priority,
354 'description': ppconcat('description','milestone_flag', 'project_task_id', 'task_number', 'percent_complete'),
355 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
356 'partner_id/id': 'partner_id/id',
357 'contact_id/id': 'contact_id/id',
358 'state': map_val('status', self.project_task_state)
371 def import_project_account(self, val):
373 partner_invoice_id = False
374 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)
375 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)
376 for contact_id in sugar_project_contact:
377 partner_invoice_id = self.get_mapped_id(self.TABLE_CONTACT, contact_id)
378 for account_id in sugar_project_account:
379 partner_id = self.get_mapped_id(self.TABLE_ACCOUNT, account_id)
380 return partner_id, partner_invoice_id
382 def import_project(self, val):
383 partner_id, partner_invoice_id = self.import_project_account(val)
384 val['partner_id/.id'] = partner_id
385 val['contact_id/.id'] = partner_invoice_id
388 def get_project_mapping(self):
390 'model' : 'project.project',
391 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
392 'hook' : self.import_project,
395 'date_start': 'estimated_start_date',
396 'date': 'estimated_end_date',
397 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
398 'partner_id/.id': 'partner_id/.id',
399 'contact_id/.id': 'contact_id/.id',
400 'state': map_val('status', self.project_state)
408 'Completed' : 'done',
409 'Not Started':'draft',
410 'In Progress': 'open',
411 'Pending Input': 'draft',
415 def import_task(self, val):
416 val['date'] = val.get('date_start') or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
417 val['date_deadline'] = val.get('date_due') or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
420 def get_task_mapping(self):
422 'model' : 'crm.meeting',
423 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
424 'hook' : self.import_task,
428 'date_deadline': 'date_deadline',
429 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
430 'categ_id/id': call(self.get_category, 'crm.meeting', const('Tasks')),
431 'partner_id/id': related_ref(self.TABLE_ACCOUNT),
432 'partner_address_id/id': ref(self.TABLE_CONTACT,'contact_id'),
433 'state': map_val('status', self.task_state)
440 #TODO adapt with project trunk-crm-imp
444 'Not Held': 'pending',
447 def get_calls_mapping(self):
449 'model' : 'crm.phonecall',
450 'dependencies' : [self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD],
453 'date': 'date_start',
454 'duration': call(self.get_float_time, value('duration_hours'), value('duration_minutes')),
455 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
456 'partner_id/id': related_ref(self.TABLE_ACCOUNT),
457 'partner_address_id/id': related_ref(self.TABLE_CONTACT),
458 'categ_id/id': call(self.get_category, 'crm.phonecall', value('direction')),
459 'opportunity_id/id': related_ref(self.TABLE_OPPORTUNITY),
460 'description': ppconcat('description'),
461 'state': map_val('status', self.call_state)
474 def get_attendee_id(self, cr, uid, module_name, module_id):
478 attendee_dict = sugar.user_get_attendee_list(self.context.get('port'), self.context.get('session_id'), module_name, module_id)
479 for attendee in attendee_dict:
480 user_id = self.xml_id_exist(self.TABLE_USER, attendee.get('id', False))
483 contact_id = self.xml_id_exist(self.TABLE_CONTACT, attendee.get('id', False))
484 fields = ['user_id/id', 'email', 'partner_address_id/id']
485 data = [user_id, attendee.get('email1'), contact_id]
486 attendee_xml_id = self.import_object(fields, data, 'calendar.attendee', self.TABLE_ATTENDEE, user_id or contact_id or attendee.get('email1'), ['|',('user_id', '=', attendee.get('id')),('partner_address_id','=',attendee.get('id')),('email', '=', attendee.get('email1'))])
487 attendee_id.append(attendee_xml_id)
488 return ','.join(attendee_id)
490 def get_alarm_id(self, dict_val, val):
492 '60': '1 minute before',
493 '300': '5 minutes before',
494 '600': '10 minutes before',
495 '900': '15 minutes before',
496 '1800':'30 minutes before',
497 '3600': '1 hour before',
499 return self.mapped_id_if_exist('res.alarm', [('name', 'like', alarm_dict.get(val))], 'alarm', val)
503 def import_meeting(self, val):
504 attendee_id = self.get_attendee_id(self.cr, self.uid, 'Meetings', val.get('id')) #TODO
505 val['attendee_ids/id'] = attendee_id
508 def get_meeting_mapping(self):
510 'model' : 'crm.meeting',
511 'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD, self.TABLE_TASK],
512 'hook': self.import_meeting,
515 'date': 'date_start',
516 'duration': call(self.get_float_time, value('duration_hours'), value('duration_minutes')),
517 'location': 'location',
518 'attendee_ids/id':'attendee_ids/id',
519 'alarm_id/id': call(self.get_alarm_id, value('reminder_time')),
520 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
521 'partner_id/id': related_ref(self.TABLE_ACCOUNT),
522 'partner_address_id/id': related_ref(self.TABLE_CONTACT),
523 'state': map_val('status', self.meeting_state)
531 'Need Analysis' : 'New',
532 'Closed Lost': 'Lost',
534 'Value Proposition': 'Proposition',
535 'Negotiation/Review': 'Negotiation'
538 def get_opportunity_status(self, sugar_val):
539 fields = ['name', 'type']
540 name = 'Opportunity_' + sugar_val['sales_stage']
541 data = [sugar_val['sales_stage'], 'Opportunity']
542 return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])])
544 def import_opportunity_contact(self, val):
545 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))
547 partner_contact_id = False
548 partner_contact_email = False
549 partner_address_obj = self.obj.pool.get('res.partner.address')
550 partner_xml_id = self.name_exist(self.TABLE_ACCOUNT, val['account_name'], 'res.partner')
552 for contact in sugar_opportunities_contact:
553 address_id = self.get_mapped_id(self.TABLE_CONTACT, contact)
555 address = partner_address_obj.browse(self.cr, self.uid, address_id)
556 partner_name = address.partner_id and address.partner_id.name or False
557 if not partner_name: #link with partner id
558 fields = ['partner_id/id']
559 data = [partner_xml_id]
560 self.import_object(fields, data, 'res.partner.address', self.TABLE_CONTACT, contact, self.DO_NOT_FIND_DOMAIN)
561 if not partner_name or partner_name == val.get('account_name'):
562 partner_contact_id = self.xml_id_exist(self.TABLE_CONTACT, contact)
563 partner_contact_email = address.email
564 return partner_contact_id, partner_contact_email
566 def import_opp(self, val):
567 partner_contact_id, partner_contact_email = self.import_opportunity_contact(val)
568 val['partner_address_id/id'] = partner_contact_id
569 val['email_from'] = partner_contact_email
572 def get_opp_mapping(self):
574 'model' : 'crm.lead',
575 'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT,self.TABLE_COMPAIGN],
576 'hook' : self.import_opp,
579 'probability': 'probability',
580 'partner_id/id': refbyname(self.TABLE_ACCOUNT, 'account_name', 'res.partner'),
581 'title_action': 'next_step',
582 'partner_address_id/id': 'partner_address_id/id',
583 'planned_revenue': 'amount',
584 'date_deadline': 'date_closed',
585 'user_id/id' : ref(self.TABLE_USER, 'assigned_user_id'),
586 'stage_id/id' : self.get_opportunity_status,
587 'type' : const('opportunity'),
588 'categ_id/id': call(self.get_category, 'crm.lead', value('opportunity_type')),
589 'email_from': 'email_from',
590 'state': map_val('status', self.opp_state) , #TODO
598 def get_compaign_mapping(self):
600 'model' : 'crm.case.resource.type',
609 def get_lead_status(self, sugar_val):
610 fields = ['name', 'type']
611 name = 'lead_' + sugar_val.get('status', '')
612 data = [sugar_val.get('status', ''), 'lead']
613 return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val.get('status', ''))])
618 'In Progress': 'open',
619 'Recycled': 'cancel',
624 def import_lead(self, val):
625 if val.get('opportunity_id'): #if lead is converted into opp, don't import as lead
627 if val.get('primary_address_country'):
628 country_id = self.get_all_countries(val.get('primary_address_country'))
629 val['country_id/id'] = country_id
630 val['state_id/id'] = self.get_all_states(val.get('primary_address_state'), country_id)
633 def get_lead_mapping(self):
635 'model' : 'crm.lead',
636 'dependencies' : [self.TABLE_COMPAIGN, self.TABLE_USER],
637 'hook' : self.import_lead,
639 'name': concat('first_name', 'last_name'),
640 'contact_name': concat('first_name', 'last_name'),
641 'description': ppconcat('description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'),
642 'partner_name': 'account_name',
643 'email_from': 'email1',
644 'phone': 'phone_work',
645 'mobile': 'phone_mobile',
646 'title/id': call(self.get_job_title, value('salutation')),
648 'street': 'primary_address_street',
649 'street2': 'alt_address_street',
650 'zip': 'primary_address_postalcode',
651 'city':'primary_address_city',
652 'user_id/id' : ref(self.TABLE_USER, 'assigned_user_id'),
653 'stage_id/id' : self.get_lead_status,
654 'type' : const('lead'),
655 'state': map_val('status', self.lead_state) ,
657 'referred': 'refered_by',
658 'optout': 'do_not_call',
659 'channel_id/id': call(self.get_channel_id, value('lead_source')),
660 'type_id/id': ref(self.TABLE_COMPAIGN, 'campaign_id'),
661 'country_id/id': 'country_id/id',
662 'state_id/id': 'state_id/id'
670 def get_email(self, val):
671 email_address = sugar.get_contact_by_email(self.context.get('port'), self.context.get('username'), self.context.get('password'), val.get('email1'))
673 return ','.join(email_address)
675 def import_contact(self, val):
676 if val.get('primary_address_country'):
677 country_id = self.get_all_countries(val.get('primary_address_country'))
678 state = self.get_all_states(val.get('primary_address_state'), country_id)
679 val['country_id/id'] = country_id
680 val['state_id/id'] = state
683 def get_contact_mapping(self):
685 'model' : 'res.partner.address',
686 'dependencies' : [self.TABLE_ACCOUNT],
687 'hook' : self.import_contact,
689 'name': concat('first_name', 'last_name'),
690 'partner_id/id': ref(self.TABLE_ACCOUNT,'account_id'),
691 'phone': 'phone_work',
692 'mobile': 'phone_mobile',
695 'street': 'primary_address_street',
696 'zip': 'primary_address_postalcode',
697 'city': 'primary_address_city',
698 'country_id/id': 'country_id/id',
699 'state_id/id': 'state_id/id',
700 'email': self.get_email,
701 'type': const('contact')
709 def get_address_type(self, val, type):
710 if type == 'invoice':
711 type_address = 'billing'
713 type_address = 'shipping'
715 if type == 'default':
716 map_partner_address = {
718 'type': const('default'),
722 map_partner_address = {
724 'phone': 'phone_office',
725 'mobile': 'phone_mobile',
728 'street': type_address + '_address_street',
729 'zip': type_address +'_address_postalcode',
730 'city': type_address +'_address_city',
731 'country_id/id': 'country_id/id',
735 if val.get(type_address +'_address_country'):
736 country_id = self.get_all_countries(val.get(type_address +'_address_country'))
737 state = self.get_all_states(val.get(type_address +'_address_state'), country_id)
738 val['country_id/id'] = country_id
739 val['state_id/id'] = state
742 val['id_new'] = val['id'] + '_address_' + type
743 return self.import_object_mapping(map_partner_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id_new'], self.DO_NOT_FIND_DOMAIN)
745 def get_partner_address(self, val):
747 type_dict = {'billing_address_street' : 'invoice', 'shipping_address_street' : 'delivery', 'type': 'default'}
748 for key, type_value in type_dict.items():
750 id = self.get_address_type(val, type_value)
751 address_id.append(id)
753 return ','.join(address_id)
755 def get_partner_mapping(self):
757 'model' : 'res.partner',
758 'dependencies' : [self.TABLE_USER],
761 'website': 'website',
762 'user_id/id': ref(self.TABLE_USER,'assigned_user_id'),
764 'comment': ppconcat('description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'),
765 'customer': const('1'),
766 'supplier': const('0'),
767 'address/id':'address/id',
768 'parent_id/id_parent' : 'parent_id',
769 'address/id' : self.get_partner_address,
776 def get_ressource(self, val):
778 'name': concat('first_name', 'last_name'),
780 return self.import_object_mapping(map_resource, val, 'resource.resource', self.TABLE_RESSOURCE, val['id'], self.DO_NOT_FIND_DOMAIN)
782 def get_job_id(self, val):
784 data = [val.get('title')]
785 return self.import_object(fields, data, 'hr.job', 'hr_job', val.get('title'))
787 def get_user_address(self, val):
789 'name': concat('first_name', 'last_name'),
790 'city': 'address_city',
791 'country_id/id': 'country_id/id',
792 'state_id/id': 'state_id/id',
793 'street': 'address_street',
794 'zip': 'address_postalcode',
796 'phone': 'phone_work',
797 'mobile':'phone_mobile',
801 if val.get('address_country'):
802 country_id = self.get_all_countries(val.get('address_country'))
803 state_id = self.get_all_states(val.get('address_state'), country_id)
804 val['country_id/id'] = country_id
805 val['state_id/id'] = state_id
807 return self.import_object_mapping(map_user_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id'], self.DO_NOT_FIND_DOMAIN)
809 def get_employee_mapping(self):
811 'model' : 'hr.employee',
812 'dependencies' : [self.TABLE_USER],
814 'resource_id/id': self.get_ressource,
815 'name': concat('first_name', 'last_name'),
816 'work_phone': 'phone_work',
817 'mobile_phone': 'phone_mobile',
818 'user_id/id': ref(self.TABLE_USER, 'id'),
819 'address_home_id/id': self.get_user_address,
820 'notes': ppconcat('messenger_type', 'messenger_id', 'description'),
821 'job_id/id': self.get_job_id,
822 'work_email' : 'email1',
823 'coach_id/id_parent' : 'reports_to_id',
830 def import_user(self, val):
831 user_obj = self.obj.pool.get('res.users')
832 user_ids = user_obj.search(self.cr, self.uid, [('login', '=', val.get('user_name'))])
834 val['.id'] = str(user_ids[0])
836 val['password'] = 'sugarcrm' #default password for all user #TODO needed in documentation
838 val['context_lang'] = self.context.get('lang','en_US')
841 def get_users_department(self, val):
842 dep = val.get('department')
847 return self.import_object(fields, data, 'hr.department', 'hr_department_user', dep)
849 def get_user_mapping(self):
851 'model' : 'res.users',
852 'hook' : self.import_user,
854 'name': concat('first_name', 'last_name'),
855 'login': 'user_name',
856 'context_lang' : 'context_lang',
857 'password' : 'password',
859 'context_department_id/id': self.get_users_department,
860 'user_email' : 'email1',
865 def get_mapping(self):
867 self.TABLE_USER : self.get_user_mapping(),
868 self.TABLE_EMPLOYEE : self.get_employee_mapping(),
869 self.TABLE_ACCOUNT : self.get_partner_mapping(),
870 self.TABLE_CONTACT : self.get_contact_mapping(),
871 self.TABLE_LEAD : self.get_lead_mapping(),
872 self.TABLE_OPPORTUNITY : self.get_opp_mapping(),
873 self.TABLE_MEETING : self.get_meeting_mapping(),
874 self.TABLE_CALL : self.get_calls_mapping(),
875 self.TABLE_TASK : self.get_task_mapping(),
876 self.TABLE_PROJECT : self.get_project_mapping(),
877 self.TABLE_PROJECT_TASK: self.get_project_task_mapping(),
878 self.TABLE_BUG: self.get_project_issue_mapping(),
879 self.TABLE_CASE: self.get_crm_claim_mapping(),
880 self.TABLE_NOTE: self.get_history_mapping(),
881 self.TABLE_EMAIL: self.get_email_mapping(),
882 self.TABLE_DOCUMENT: self.get_document_mapping(),
883 self.TABLE_COMPAIGN: self.get_compaign_mapping()
889 def get_email_subject(self, result):
890 return "your sugarcrm data were successfully imported at %s" % self.date_ended
892 def get_body_header(self, result):
893 return "Sugarcrm import : report of last import"
896 class import_sugarcrm(osv.osv):
897 """Import SugarCRM DATA"""
899 _name = "import.sugarcrm"
900 _description = __doc__
902 'username': fields.char('User Name', size=64, required=True),
903 'password': fields.char('Password', size=24,required=True),
904 'url' : fields.char('SugarSoap Api url:', size=264, required=True, help="Webservice's url where to get the data.\
905 example : 'http://example.com/sugarcrm/soap.php', or copy the address of your sugarcrm application http://trial.sugarcrm.com/qbquyj4802/index.php?module=Home&action=index"),
907 'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
908 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
909 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in OpenERP partners form"),
910 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
911 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings and Meeting Tasks data imported in OpenERP meetings form"),
912 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
913 'claim': fields.boolean('Cases', help="If Cases is checked, SugarCRM Cases data imported in OpenERP Claims form"),
914 'email_history': fields.boolean('Email and History',help="If Email and History is checked, SugarCRM Notes and Emails data imported in OpenERP's Related module's History with attachment"),
915 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
916 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
917 'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
918 'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
919 'email_from': fields.char('Notify End Of Import To:', size=128),
920 'instance_name': fields.char("Instance's Name", size=64, help="Prefix of SugarCRM id to differentiate xml_id of SugarCRM models datas come from different server."),
923 _defaults = {#to be set to true, but easier for debugging
924 'opportunity': False,
931 'email_history' : False,
933 'project_task': False,
936 'instance_name': 'sugarcrm',
937 'email_from': 'tfr@openerp.com',
940 'url': "http://localhost/sugarcrm/soap.php"
943 def check_url(self, url, context):
946 user = context.get('username')
947 password = context.get('password')
950 sugar.login(user, password, url)
956 def parse_valid_url(self, context=None):
959 url = context.get('url')
962 url_split = str(url).split('/')
963 while len(url_split) >= 3:
964 #3 case, soap.php is already at the end of url should be valid
966 if url_split[-1] == 'soap.php':
967 url = "/".join(url_split)
968 elif url_split[-1] == '':
969 url = "/".join(url_split) + "soap.php"
971 url = "/".join(url_split) + "/soap.php"
973 if self.check_url(url, context):
976 url_split = url_split[:-1] #take parent folder
979 def get_key(self, cr, uid, ids, context=None):
980 """Select Key as For which Module data we want import data."""
984 for current in self.browse(cr, uid, ids, context):
985 context.update({'username': current.username, 'password': current.password, 'url': current.url, 'email_user': current.email_from or False, 'instance_name': current.instance_name or False})
986 if current.opportunity:
987 key_list.append('Leads')
988 key_list.append('Opportunities')
990 key_list.append('Contacts')
992 key_list.append('Accounts')
994 key_list.append('Employees')
996 key_list.append('Meetings')
998 key_list.append('Calls')
1000 key_list.append('Cases')
1001 if current.email_history:
1002 key_list.append('Emails')
1003 key_list.append('Notes')
1005 key_list.append('Project')
1006 if current.project_task:
1007 key_list.append('ProjectTask')
1009 key_list.append('Bugs')
1010 if current.document:
1011 key_list.append('DocumentRevisions')
1015 def do_import_all(self, cr, uid, *args):
1019 context = {'username': args[4], 'password': args[5], 'url': args[3], 'instance_name': args[3]}
1020 imp = sugar_import(self, cr, uid, args[2], "import_sugarcrm", [args[1]], context)
1021 imp.set_table_list(args[0])
1025 def import_from_scheduler_all(self, cr, uid, ids, context=None):
1026 keys = self.get_key(cr, uid, ids, context)
1028 raise osv.except_osv(_('Warning !'), _('Select Module to Import.'))
1029 cron_obj = self.pool.get('ir.cron')
1030 url = self.parse_valid_url(context)
1031 args = (keys,context.get('email_user'), context.get('instance_name'), url, context.get('username'), context.get('password') )
1032 new_create_id = cron_obj.create(cr, uid, {'name': 'Import SugarCRM datas','interval_type': 'hours','interval_number': 1, 'numbercall': -1,'model': 'import.sugarcrm','function': 'do_import_all', 'args': args, 'active': False})
1034 'name': 'SugarCRM Scheduler',
1035 'view_type': 'form',
1036 'view_mode': 'form,tree',
1037 'res_model': 'ir.cron',
1038 'res_id': new_create_id,
1039 'type': 'ir.actions.act_window',
1043 def import_all(self, cr, uid, ids, context=None):
1045 # """Import all sugarcrm data into openerp module"""
1046 keys = self.get_key(cr, uid, ids, context)
1047 url = self.parse_valid_url(context)
1048 context.update({'url': url})
1049 imp = sugar_import(self, cr, uid, context.get('instance_name'), "import_sugarcrm", [context.get('email_user')], context)
1050 imp.set_table_list(keys)
1053 obj_model = self.pool.get('ir.model.data')
1054 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1055 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1057 'view_type': 'form',
1058 'view_mode': 'form',
1059 'res_model': 'import.message',
1060 'views': [(resource_id,'form')],
1061 'type': 'ir.actions.act_window',