407952a31c191ad41bfc4ba182e4b7f90c0eab8a
[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
25 class import_sugarcrm(osv.osv):
26      """Import SugarCRM DATA"""
27
28      _name = "import.sugarcrm"
29      _description = __doc__
30      _columns = {
31         'mod_name':fields.selection([
32             ('lead','Leads'),
33             ('opportunity','Opportunities'),
34             ('accounts','Accounts'),
35             ('contact','Contacts'),
36         ],'Module Name', help="Module Name is used to specify which Module data want to Import"),
37      }
38
39
40      def import_data(self, cr, uid, ids,context=None):
41        if not context:
42         context={}
43        lead_pool = self.pool.get("crm.lead")
44        for current in self.browse(cr, uid, ids):
45         if current.mod_name == 'lead' or current.mod_name == 'opportunity':
46           module_name = 'crm'
47
48         elif current.mod_name == "accounts":
49           module_name = 'account'
50         ids = self.pool.get("ir.module.module").search(cr, uid, [("name", '=',
51            module_name), ('state', '=', 'installed')])
52         if not ids:
53            raise osv.except_osv(_('Error !'), _('Please  Install %s Module') % (module_name))
54
55         if current.mod_name == 'lead':
56            sugar_name = "Leads"
57         elif current.mod_name == 'opportunity':
58            sugar_name="Opportunities"
59         elif current.mod_name == 'accounts':
60            sugar_name="Accounts"
61         elif current.mod_name == 'contact':
62            sugar_name="Contacts"
63
64        sugar_data = sugar.test(sugar_name)
65        for sugar_val in sugar_data:
66            if sugar_name == "Leads":
67                vals = {'name':sugar_val.get('first_name', ''),
68                        'user_id':sugar_val.get('created_by',''),
69                        'partner_name': sugar_val.get('first_name','')+''+ sugar_val.get('last_name',''),
70                        'email_from': sugar_val.get('email1',''),
71                        'phone': sugar_val.get('phone_work',''),
72                        'mobile': sugar_val.get('phone_mobile',''),
73                        'write_date':sugar_val.get('date_modified',''),
74                        'function':sugar_val.get('title',''),
75                        'street': sugar_val.get('primary_address_street',''),
76                        'zip': sugar_val.get('primary_address_postalcode',''),
77                        'city':sugar_val.get('primary_address_city',''),
78                        }
79                lead_pool.create(cr, uid, vals)
80
81        return {}
82
83 import_sugarcrm()
84
85
86
87
88