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'])
41 class sugar_import(import_framework):
42 TABLE_CONTACT = 'Contacts'
43 TABLE_ACCOUNT = 'Accounts'
45 TABLE_EMPLOYEE = 'Employees'
46 TABLE_RESSOURCE = "resource"
47 TABLE_OPPORTUNITY = 'Opportunities'
49 TABLE_STAGE = 'crm_stage'
50 TABLE_ATTENDEE = 'calendar_attendee'
52 TABLE_MEETING = 'Meetings'
54 TABLE_PROJECT = 'Project'
55 TABLE_PROJECT_TASK = 'ProjectTask'
59 TABLE_EMAIL = 'Emails'
60 TABLE_DOCUMENT = 'DocumentRevisions'
61 TABLE_COMPAIGN = 'Campaigns'
62 TABLE_HISTORY_ATTACHMNET = 'history_attachment'
66 PortType,sessionid = sugar.login(self.context.get('username',''), self.context.get('password',''), self.context.get('url',''))
68 raise osv.except_osv(_('Error !'), _('Authentication error !\nBad Username or Password !'))
69 self.context['port'] = PortType
70 self.context['session_id'] = sessionid
72 def get_data(self, table):
74 return sugar.search(self.context.get('port'), self.context.get('session_id'), table)
79 def get_category(self, val, model, name):
80 fields = ['name', 'object_id']
82 return self.import_object(fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)])
84 def get_job_title(self, dict, salutation):
85 fields = ['shortcut', 'name', 'domain']
87 data = [salutation, salutation, 'Contact']
88 return self.import_object(fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)])
90 def get_channel_id(self, dict, val):
93 return self.import_object(fields, data, 'res.partner.canal', 'crm_channel', val)
95 def get_all_states(self, external_val, country_id):
96 """Get states or create new state unless country_id is False"""
97 state_code = external_val[0:3] #take the tree first char
98 fields = ['country_id/id', 'name', 'code']
99 data = [country_id, external_val, state_code]
101 return self.import_object(fields, data, 'res.country.state', 'country_state', external_val)
104 def get_all_countries(self, val):
105 """Get Country, if no country match do not create anything, to avoid duplicate country code"""
106 return self.mapped_id_if_exist('res.country', [('name', 'ilike', val)], 'country', val)
108 def get_float_time(self, dict, hour, min):
109 min = int(min) * 100 / 60
110 return "%s.%i" % (hour, min)
112 def get_attachment(self, val):
113 File, Filename = sugar.attachment_search(self.context.get('port'), self.context.get('session_id'), self.TABLE_NOTE, val.get('id'))
114 attach_xml_id = False
115 attachment_obj = self.obj.pool.get('ir.attachment')
116 model_obj = self.obj.pool.get('ir.model.data')
117 mailgate_obj = self.obj.pool.get('mailgate.message')
119 fields = ['name', 'datas', 'datas_fname','res_id', 'res_model']
120 name = 'attachment_'+ (Filename or val.get('name'))
121 datas = [Filename or val.get('name'), File, Filename, val.get('res_id'),val.get('model',False)]
122 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')))])
128 #THIS IS A JOKE ? NOT FUNNY
129 def import_document(self, val):
130 filepath = '/home/openerp/Public/sugarcrm/cache/upload/'+ val.get('id')
131 f = open(filepath, "r")
134 val['datas'] = base64.encodestring(datas)
135 val['datas_fname'] = val.get('filename')
138 def get_document_mapping(self):
140 'model' : 'ir.attachment',
141 'dependencies' : [self.TABLE_USER],
142 'hook' : self.import_document,
143 'map' : {'name':'filename',
144 'description': ppconcat('description'),
146 'datas_fname': 'datas_fname',
154 def import_email(self, val):
156 model_obj = self.obj.pool.get('ir.model.data')
157 xml_id = self.xml_id_exist(val.get('parent_type'), val.get('parent_id'))
158 model_ids = model_obj.search(self.cr, self.uid, [('name', 'like', xml_id)])
160 print 'model_id', xml_id, model_ids
161 model = model_obj.browse(self.cr, self.uid, model_ids)[0]
162 if model.model == 'res.partner':
163 val['partner_id/.id'] = model.res_id
165 val['res_id'] = model.res_id
166 val['model'] = model.model
169 def get_email_mapping(self):
171 'model' : 'mailgate.message',
172 '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],
173 'hook' : self.import_email,
174 'map' : {'name':'name',
176 'email_from': 'from_addr_name',
177 'email_to': 'reply_to_addr',
178 'email_cc': 'cc_addrs_names',
179 'email_bcc': 'bcc_addrs_names',
180 'message_id': 'message_id',
183 'partner_id/.id': 'partner_id/.id',
184 'attachment_ids/id': self.get_attachment,
185 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
186 'description': ppconcat('description', 'description_html'),
191 import History(Notes)
194 def import_history(self, val):
195 model_obj = self.obj.pool.get('ir.model.data')
196 xml_id = self.xml_id_exist(val.get('parent_type'), val.get('parent_id'))
197 model_ids = model_obj.search(self.cr, self.uid, [('name', 'like', xml_id)])
199 model = model_obj.browse(self.cr, self.uid, model_ids)[0]
200 if model.model == 'res.partner':
201 val['partner_id/.id'] = model.res_id
203 val['res_id'] = model.res_id
204 val['model'] = model.model
207 def get_history_mapping(self):
209 'model' : 'mailgate.message',
210 '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],
211 'hook' : self.import_history,
214 'date': 'date_entered',
215 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
216 'description': ppconcat('description', 'description_html'),
219 'attachment_ids/id': self.get_attachment,
220 'partner_id/.id' : 'partner_id/.id',
227 def get_claim_priority(self, val):
233 return priority_dict.get(val.get('priority'), '')
235 def get_contact_info_from_account(self, val):
236 partner_id = self.get_mapped_id(self.TABLE_ACCOUNT, val.get('account_id'))
237 partner_address_id = False
238 partner_phone = False
239 partner_email = False
240 partner = self.obj.pool.get('res.partner').browse(self.cr, self.uid, [partner_id])[0]
241 if partner.address and partner.address[0]:
242 address = partner.address[0]
243 partner_address_id = address.id
244 partner_phone = address.phone
245 partner_email = address.email
246 return partner_address_id, partner_phone,partner_email
248 def import_crm_claim(self, val):
249 partner_address_id, partner_phone,partner_email = self.get_contact_info_from_account(val)
250 val['partner_address_id/.id'] = partner_address_id
251 val['partner_address_id/.id'] = partner_address_id
252 val['partner_phone'] = partner_phone
253 val['email_from'] = partner_email
256 def get_crm_claim_mapping(self):
258 'model' : 'crm.claim',
259 'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD],
260 'hook' : self.import_crm_claim,
263 'date': 'date_entered',
264 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
265 'description': ppconcat('description'),
266 'partner_id/id': ref(self.TABLE_ACCOUNT, 'account_id'),
267 'partner_address_id/.id': 'partner_address_id/.id',
268 'partner_phone': 'partner_phone',
269 'email_from': 'email_from',
270 'priority': self.get_claim_priority,
271 'state': map_val('status', self.project_issue_state)
275 Import Project Issue(Bugs)
277 project_issue_state = {
281 'Pending': 'pending',
282 'Rejected': 'cancel',
285 def get_project_issue_priority(self, val):
292 return priority_dict.get(val.get('priority'), '')
294 def get_bug_project_id(self, dict, val):
297 return self.import_object(fields, data, 'project.project', 'project_issue', val)
299 def get_project_issue_mapping(self):
301 'model' : 'project.issue',
302 'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT, self.TABLE_PROJECT_TASK],
305 'project_id/id': call(self.get_bug_project_id, 'sugarcrm_bugs'),
306 'categ_id/id': call(self.get_category, 'project.issue', value('type')),
307 'description': ppconcat('description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'),
308 'priority': self.get_project_issue_priority,
309 'state': map_val('status', self.project_issue_state)
316 project_task_state = {
317 'Not Started': 'draft',
318 'In Progress': 'open',
320 'Pending Input': 'pending',
321 'Deferred': 'cancelled',
324 def get_project_task_priority(self, val):
330 return priority_dict.get(val.get('priority'), '')
332 def get_project_task_mapping(self):
334 'model' : 'project.task',
335 'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT],
338 'date_start': 'date_start',
339 'date_end': 'date_finish',
340 'progress': 'progress',
341 'project_id/id': ref(self.TABLE_PROJECT, 'project_id'),
342 'planned_hours': 'planned_hours',
343 'total_hours': 'total_hours',
344 'priority': self.get_project_task_priority,
345 'description': ppconcat('description','milestone_flag', 'project_task_id', 'task_number'),
346 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
347 'partner_id/id': 'partner_id/id',
348 'contact_id/id': 'contact_id/id',
349 'state': map_val('status', self.project_task_state)
361 def import_project_account(self, val):
363 partner_invoice_id = False
364 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)
365 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)
366 for contact_id in sugar_project_contact:
367 partner_invoice_id = self.get_mapped_id(self.TABLE_CONTACT, contact_id)
368 for account_id in sugar_project_account:
369 partner_id = self.get_mapped_id(self.TABLE_ACCOUNT, account_id)
370 return partner_id, partner_invoice_id
372 def import_project(self, val):
373 partner_id, partner_invoice_id = self.import_project_account(val)
374 val['partner_id/.id'] = partner_id
375 val['contact_id/.id'] = partner_invoice_id
378 def get_project_mapping(self):
380 'model' : 'project.project',
381 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
382 'hook' : self.import_project,
385 'date_start': 'estimated_start_date',
386 'date': 'estimated_end_date',
387 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
388 'partner_id/.id': 'partner_id/.id',
389 'contact_id/.id': 'contact_id/.id',
390 'state': map_val('status', self.project_state)
398 'Completed' : 'done',
399 'Not Started':'draft',
400 'In Progress': 'open',
401 'Pending Input': 'draft',
405 def import_task(self, val):
406 val['date'] = val.get('date_start') or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
407 val['date_deadline'] = val.get('date_due') or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
410 def get_task_mapping(self):
412 'model' : 'crm.meeting',
413 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
414 'hook' : self.import_task,
418 'date_deadline': 'date_deadline',
419 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
420 'categ_id/id': call(self.get_category, 'crm.meeting', const('Tasks')),
421 'partner_id/id': related_ref(self.TABLE_ACCOUNT),
422 'partner_address_id/id': ref(self.TABLE_CONTACT,'contact_id'),
423 'state': map_val('status', self.task_state)
433 'Not Held': 'pending',
436 def get_calls_mapping(self):
438 'model' : 'crm.phonecall',
439 'dependencies' : [self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD],
442 'date': 'date_start',
443 'duration': call(self.get_float_time, value('duration_hours'), value('duration_minutes')),
444 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
445 'partner_id/id': related_ref(self.TABLE_ACCOUNT),
446 'partner_address_id/id': related_ref(self.TABLE_CONTACT),
447 'categ_id/id': call(self.get_category, 'crm.phonecall', value('direction')),
448 'opportunity_id/id': related_ref(self.TABLE_OPPORTUNITY),
449 'description': ppconcat('description'),
450 'state': map_val('status', self.call_state)
463 def get_attendee_id(self, cr, uid, module_name, module_id):
467 attendee_dict = sugar.user_get_attendee_list(self.context.get('port'), self.context.get('session_id'), module_name, module_id)
468 for attendee in attendee_dict:
469 user_id = self.xml_id_exist(self.TABLE_USER, attendee.get('id', False))
473 contact_id = self.xml_id_exist(self.TABLE_CONTACT, attendee.get('id', False))
474 fields = ['user_id/id', 'email', 'partner_address_id/id']
475 data = [user_id or False, attendee.get('email1'), contact_id]
476 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'))])
477 attendee_id.append(attendee_xml_id)
478 return ','.join(attendee_id)
480 def get_alarm_id(self, dict_val, val):
482 '60': '1 minute before',
483 '300': '5 minutes before',
484 '600': '10 minutes before',
485 '900': '15 minutes before',
486 '1800':'30 minutes before',
487 '3600': '1 hour before',
489 return self.mapped_id_if_exist('res.alarm', [('name', 'like', alarm_dict.get(val))], 'alarm', val)
492 def import_meeting(self, val):
493 attendee_id = self.get_attendee_id(self.cr, self.uid, 'Meetings', val.get('id')) #TODO
494 val['attendee_ids/id'] = attendee_id
497 def get_meeting_mapping(self):
499 'model' : 'crm.meeting',
500 'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD],
501 'hook': self.import_meeting,
504 'date': 'date_start',
505 'duration': call(self.get_float_time, value('duration_hours'), value('duration_minutes')),
506 'location': 'location',
507 'attendee_ids/id':'attendee_ids/id',
508 'alarm_id/id': call(self.get_alarm_id, value('reminder_time')),
509 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
510 'partner_id/id': related_ref(self.TABLE_ACCOUNT),
511 'partner_address_id/id': related_ref(self.TABLE_CONTACT),
512 'state': map_val('status', self.meeting_state)
520 'Need Analysis' : 'New',
521 'Closed Lost': 'Lost',
523 'Value Proposition': 'Proposition',
524 'Negotiation/Review': 'Negotiation'
527 def get_opportunity_status(self, sugar_val):
528 fields = ['name', 'type']
529 name = 'Opportunity_' + sugar_val['sales_stage']
530 data = [sugar_val['sales_stage'], 'Opportunity']
531 return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])])
533 def import_opportunity_contact(self, val):
534 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))
536 partner_contact_id = False
537 partner_contact_email = False
538 partner_address_obj = self.obj.pool.get('res.partner.address')
539 partner_xml_id = self.name_exist(self.TABLE_ACCOUNT, val['account_name'], 'res.partner')
541 for contact in sugar_opportunities_contact:
542 address_id = self.get_mapped_id(self.TABLE_CONTACT, contact)
544 address = partner_address_obj.browse(self.cr, self.uid, address_id)
545 partner_name = address.partner_id and address.partner_id.name or False
546 if not partner_name: #link with partner id
547 fields = ['partner_id/id']
548 data = [partner_xml_id]
549 self.import_object(fields, data, 'res.partner.address', self.TABLE_CONTACT, contact, self.DO_NOT_FIND_DOMAIN)
550 if not partner_name or partner_name == val.get('account_name'):
551 partner_contact_id = self.xml_id_exist(self.TABLE_CONTACT, contact)
552 partner_contact_email = address.email
553 return partner_contact_id, partner_contact_email
555 def import_opp(self, val):
556 partner_contact_name, partner_contact_email = self.import_opportunity_contact(val)
557 val['partner_address_id/id'] = partner_contact_name
558 val['email_from'] = partner_contact_email
561 def get_opp_mapping(self):
563 'model' : 'crm.lead',
564 'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT,self.TABLE_COMPAIGN],
565 'hook' : self.import_opp,
568 'probability': 'probability',
569 'partner_id/id': refbyname(self.TABLE_ACCOUNT, 'account_name', 'res.partner'),
570 'title_action': 'next_step',
571 'partner_address_id/id': 'partner_address_id/id',
572 'planned_revenue': 'amount',
573 'date_deadline': 'date_closed',
574 'user_id/id' : ref(self.TABLE_USER, 'assigned_user_id'),
575 'stage_id/id' : self.get_opportunity_status,
576 'type' : const('opportunity'),
577 'categ_id/id': call(self.get_category, 'crm.lead', value('opportunity_type')),
578 'email_from': 'email_from',
579 'state': map_val('status', self.opp_state) , #TODO
587 def get_compaign_mapping(self):
589 'model' : 'crm.case.resource.type',
598 def get_lead_status(self, sugar_val):
599 fields = ['name', 'type']
600 name = 'lead_' + sugar_val.get('status', '')
601 data = [sugar_val.get('status', ''), 'lead']
602 return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val.get('status', ''))])
607 'In Progress': 'open',
608 'Recycled': 'cancel',
614 def import_lead(self, val):
615 if val.get('opportunity_id'): #if lead is converted into opp, don't import as lead
617 if val.get('primary_address_country'):
618 country_id = self.get_all_countries(val.get('primary_address_country'))
619 val['country_id/id'] = country_id
620 val['state_id/id'] = self.get_all_states(val.get('primary_address_state'), country_id)
623 def get_lead_mapping(self):
625 'model' : 'crm.lead',
626 'dependencies' : [self.TABLE_COMPAIGN, self.TABLE_USER],
627 'hook' : self.import_lead,
629 'name': concat('first_name', 'last_name'),
630 'contact_name': concat('first_name', 'last_name'),
631 'description': ppconcat('description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'),
632 'partner_name': 'account_name',
633 'email_from': 'email1',
634 'phone': 'phone_work',
635 'mobile': 'phone_mobile',
636 'title/id': call(self.get_job_title, value('salutation')),
638 'street': 'primary_address_street',
639 'street2': 'alt_address_street',
640 'zip': 'primary_address_postalcode',
641 'city':'primary_address_city',
642 'user_id/id' : ref(self.TABLE_USER, 'assigned_user_id'),
643 'stage_id/id' : self.get_lead_status,
644 'type' : const('lead'),
645 'state': map_val('status', self.lead_state) ,
647 'referred': 'refered_by',
648 'optout': 'do_not_call',
649 'channel_id/id': call(self.get_channel_id, value('lead_source')),
650 'type_id/id': ref(self.TABLE_COMPAIGN, 'campaign_id'),
651 'country_id/id': 'country_id/id',
652 'state_id/id': 'state_id/id'
659 def import_contact(self, val):
660 if val.get('primary_address_country'):
661 country_id = self.get_all_countries(val.get('primary_address_country'))
662 state = self.get_all_states(val.get('primary_address_state'), country_id)
663 val['country_id/id'] = country_id
664 val['state_id/id'] = state
667 def get_contact_mapping(self):
669 'model' : 'res.partner.address',
670 'dependencies' : [self.TABLE_ACCOUNT],
671 'hook' : self.import_contact,
673 'name': concat('first_name', 'last_name'),
674 'partner_id/id': ref(self.TABLE_ACCOUNT,'account_id'),
675 'phone': 'phone_work',
676 'mobile': 'phone_mobile',
679 'street': 'primary_address_street',
680 'zip': 'primary_address_postalcode',
681 'city': 'primary_address_city',
682 'country_id/id': 'country_id/id',
683 'state_id/id': 'state_id/id',
684 'email': concat('email1', 'email2', delimiter=','),
685 'type': const('contact')
692 def get_address_type(self, val, type):
693 if type == 'invoice':
694 type_address = 'billing'
696 type_address = 'shipping'
698 map_partner_address = {
700 'phone': 'phone_office',
701 'mobile': 'phone_mobile',
704 'street': type_address + '_address_street',
705 'zip': type_address +'_address_postalcode',
706 'city': type_address +'_address_city',
707 'country_id/id': 'country_id/id',
711 if val.get(type_address +'_address_country'):
712 country_id = self.get_all_countries(val.get(type_address +'_address_country'))
713 state = self.get_all_states(val.get(type_address +'_address_state'), country_id)
714 val['country_id/id'] = country_id
715 val['state_id/id'] = state
717 val['id_new'] = val['id'] + '_address_' + type
718 return self.import_object_mapping(map_partner_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id_new'], self.DO_NOT_FIND_DOMAIN)
720 def get_partner_address(self, val):
722 type_dict = {'billing_address_street' : 'invoice', 'shipping_address_street' : 'delivery'}
723 for key, type_value in type_dict.items():
725 id = self.get_address_type(val, type_value)
726 address_id.append(id)
728 return ','.join(address_id)
730 def get_partner_mapping(self):
732 'model' : 'res.partner',
733 'dependencies' : [self.TABLE_USER],
736 'website': 'website',
737 'user_id/id': ref(self.TABLE_USER,'assigned_user_id'),
739 'comment': ppconcat('description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'),
740 'customer': const('1'),
741 'supplier': const('0'),
742 'address/id':'address/id',
743 'parent_id/id_parent' : 'parent_id',
744 'address/id' : self.get_partner_address,
751 def get_ressource(self, val):
753 'name': concat('first_name', 'last_name'),
755 return self.import_object_mapping(map_resource, val, 'resource.resource', self.TABLE_RESSOURCE, val['id'], self.DO_NOT_FIND_DOMAIN)
757 def get_job_id(self, val):
759 data = [val.get('title')]
760 return self.import_object(fields, data, 'hr.job', 'hr_job', val.get('title'))
762 def get_user_address(self, val):
764 'name': concat('first_name', 'last_name'),
765 'city': 'address_city',
766 'country_id/id': 'country_id/id',
767 'state_id/id': 'state_id/id',
768 'street': 'address_street',
769 'zip': 'address_postalcode',
771 'phone': 'phone_work',
772 'mobile':'phone_mobile',
776 if val.get('address_country'):
777 country_id = self.get_all_countries(val.get('address_country'))
778 state_id = self.get_all_states(val.get('address_state'), country_id)
779 val['country_id/id'] = country_id
780 val['state_id/id'] = state_id
782 return self.import_object_mapping(map_user_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id'], self.DO_NOT_FIND_DOMAIN)
784 def get_employee_mapping(self):
786 'model' : 'hr.employee',
787 'dependencies' : [self.TABLE_USER],
789 'resource_id/id': self.get_ressource,
790 'name': concat('first_name', 'last_name'),
791 'work_phone': 'phone_work',
792 'mobile_phone': 'phone_mobile',
793 'user_id/id': ref(self.TABLE_USER, 'id'),
794 'address_home_id/id': self.get_user_address,
795 'notes': ppconcat('messenger_type', 'messenger_id', 'description'),
796 'job_id/id': self.get_job_id,
797 'work_email' : 'email1',
798 'coach_id/id_parent' : 'reports_to_id',
805 def import_user(self, val):
806 user_obj = self.obj.pool.get('res.users')
807 user_ids = user_obj.search(self.cr, self.uid, [('login', '=', val.get('user_name'))])
809 val['.id'] = str(user_ids[0])
811 val['password'] = 'sugarcrm' #default password for all user #TODO needed in documentation
813 val['context_lang'] = self.context.get('lang','en_US')
816 def get_users_department(self, val):
817 dep = val.get('department')
822 return self.import_object(fields, data, 'hr.department', 'hr_department_user', dep)
824 def get_user_mapping(self):
826 'model' : 'res.users',
827 'hook' : self.import_user,
829 'name': concat('first_name', 'last_name'),
830 'login': 'user_name',
831 'context_lang' : 'context_lang',
832 'password' : 'password',
834 'context_department_id/id': self.get_users_department,
835 'user_email' : 'email1',
840 def get_mapping(self):
842 self.TABLE_USER : self.get_user_mapping(),
843 self.TABLE_EMPLOYEE : self.get_employee_mapping(),
844 self.TABLE_ACCOUNT : self.get_partner_mapping(),
845 self.TABLE_CONTACT : self.get_contact_mapping(),
846 self.TABLE_LEAD : self.get_lead_mapping(),
847 self.TABLE_OPPORTUNITY : self.get_opp_mapping(),
848 self.TABLE_MEETING : self.get_meeting_mapping(),
849 self.TABLE_CALL : self.get_calls_mapping(),
850 self.TABLE_TASK : self.get_task_mapping(),
851 self.TABLE_PROJECT : self.get_project_mapping(),
852 self.TABLE_PROJECT_TASK: self.get_project_task_mapping(),
853 self.TABLE_BUG: self.get_project_issue_mapping(),
854 self.TABLE_CASE: self.get_crm_claim_mapping(),
855 self.TABLE_NOTE: self.get_history_mapping(),
856 self.TABLE_EMAIL: self.get_email_mapping(),
857 self.TABLE_DOCUMENT: self.get_document_mapping(),
858 self.TABLE_COMPAIGN: self.get_compaign_mapping()
865 def get_email_subject(self, result):
866 return "your sugarcrm data were successfully imported at %s" % self.date_ended
868 def get_body_header(self, result):
869 return "Sugarcrm import : report of last import"
872 class import_sugarcrm(osv.osv):
873 """Import SugarCRM DATA"""
875 _name = "import.sugarcrm"
876 _description = __doc__
879 'username': fields.char('User Name', size=64, required=True),
880 'password': fields.char('Password', size=24,required=True),
881 'url' : fields.char('Service', size=264, required=True, help="Connection with Sugarcrm Using Soap Protocol Services and For that Path should be 'http://localhost/sugarcrm/soap.php' Format."),
883 'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
884 'user': fields.boolean('Users', help="If Users are checked, SugarCRM Users data imported in OpenERP Users form"),
885 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
886 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in OpenERP partners form"),
887 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
888 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
889 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
890 'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
891 'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
892 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
893 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
894 'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
895 'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
896 'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
897 'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
898 'email_from': fields.char('Notify End Of Import To:', size=128),
899 '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."),
902 _defaults = {#to be set to true, but easier for debugging
903 'opportunity': False,
914 'project_task': False,
917 'instance_name': 'sugarcrm',
918 'email_from': 'tfr@openerp.com',
921 'url': "http://localhost/sugarcrm/soap.php"
924 def get_key(self, cr, uid, ids, context=None):
925 """Select Key as For which Module data we want import data."""
929 for current in self.browse(cr, uid, ids, context):
930 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})
931 if current.opportunity:
932 key_list.append('Leads')
933 key_list.append('Opportunities')
935 key_list.append('Users')
937 key_list.append('Contacts')
939 key_list.append('Accounts')
941 key_list.append('Employees')
943 key_list.append('Meetings')
945 key_list.append('Tasks')
947 key_list.append('Calls')
949 key_list.append('Cases')
951 key_list.append('Emails')
953 key_list.append('Project')
954 if current.project_task:
955 key_list.append('ProjectTask')
957 key_list.append('Bugs')
958 if current.attachment:
959 key_list.append('Notes')
961 key_list.append('DocumentRevisions')
965 def do_import_all(self, cr, uid, *args):
969 print 'args of schedule method', args
970 context = {'username': args[4], 'password': args[5], 'url': args[3], 'instance_name': args[3]}
972 imp = sugar_import(self, cr, uid, args[2], "import_sugarcrm", [args[1]], context)
973 imp.set_table_list(args[0])
977 def import_from_scheduler_all(self, cr, uid, ids, context=None):
978 keys = self.get_key(cr, uid, ids, context)
980 raise osv.except_osv(_('Warning !'), _('Select Module to Import.'))
981 cron_obj = self.pool.get('ir.cron')
982 args = (keys,context.get('email_user'), context.get('instance_name'), context.get('url'), context.get('username'), context.get('password') )
983 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})
985 'name': 'SugarCRM Scheduler',
987 'view_mode': 'form,tree',
988 'res_model': 'ir.cron',
989 'res_id': new_create_id,
990 'type': 'ir.actions.act_window',
993 def import_all(self, cr, uid, ids, context=None):
995 # """Import all sugarcrm data into openerp module"""
996 keys = self.get_key(cr, uid, ids, context)
997 imp = sugar_import(self, cr, uid, context.get('instance_name'), "import_sugarcrm", [context.get('email_user')], context)
998 imp.set_table_list(keys)
1001 obj_model = self.pool.get('ir.model.data')
1002 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1003 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1005 'view_type': 'form',
1006 'view_mode': 'form',
1007 'res_model': 'import.message',
1008 'views': [(resource_id,'form')],
1009 'type': 'ir.actions.act_window',