~apem/openerp/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-

from osv import fields, osv
from mail.mail_message import to_email
from tools.translate import _

class import_transition(osv.osv):
    _name = 'apem.imports'
    
    def _lang_get(self, cr, uid, context=None):
        obj = self.pool.get('res.lang')
        ids = obj.search(cr, uid, [], context=context)
        res = obj.read(cr, uid, ids, ['code', 'name'], context)
        return [(r['code'], r['name']) for r in res] + [('','')]
    
    _columns = {
        'state': fields.selection([ ('done','Done'), ('todo','To Do'), ('ignore','Ignore')], 'Conversion state'),
                
        'lead_name': fields.char('Name', size=64),
        'description': fields.text('Notes'),
        'lead_email_cc': fields.text('Global CC', size=252 , help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"),
        'salesman': fields.many2one('res.users', 'Salesman'),
        'salesteam': fields.many2one('crm.case.section', 'Sales Team', \
                        select=True, help='When sending mails, the default email address is taken from the sales team.'),
        'partner_name': fields.char("Customer Name", size=64,help='The name of the future partner that will be created while converting the lead into opportunity', select=1),
        'partner_website': fields.char('Website',size=64, help="Website of Partner."),
        'partner_categories': fields.many2many('res.partner.category', 'res_partner_category_gm_rel', 'apem_import_id', 'category_id', 'Categories'),
        'partner_payment_term': fields.property(
            'account.payment.term',
            type='many2one',
            relation='account.payment.term',
            string ='Payment Term',
            view_load=True,
            help="This payment term will be used instead of the default one for the current partner"),
        'partner_credit_limit': fields.float(string='Credit Limit'),
        'activity': fields.many2one('res.partner.activity', "Activity"),
        'market': fields.many2one('res.partner.market', "Market"),
        'source': fields.many2one('crm.case.channel', 'Contact source', help="First communication source (mail, direct, phone, ...)"),
        'contact_name': fields.char('Contact Name', size=64, select=1),
        'contact_title': fields.many2one('res.partner.title','Title'),
        'contact_function': fields.char('Function', size=128),
        'contact_street': fields.char('Street', size=128),
        'contact_street2': fields.char('Street2', size=128),
        'contact_zip': fields.char('Zip', change_default=True, size=24),
        'contact_city': fields.char('City', size=128),
        'contact_country': fields.many2one('res.country', 'Country'),
        'contact_email': fields.char('E-Mail', size=240),
        'contact_phone': fields.char('Phone', size=64),
        'lang': fields.selection(_lang_get, 'Language', help="If the selected language is loaded in the system, all documents related to this partner will be printed in this language. If not, it will be english."),
                }
    
    _default = {
                'state': lambda *args: 'todo'
                }
    
    def set_igonred(self, cr, uid, ids, context=None):
        return self.write(cr,uid,ids,{'state':'ignore'},context=context)
    
    def set_todo(self, cr, uid, ids, context=None):
        return self.write(cr,uid,ids,{'state':'todo'},context=context)
    
    def set_done(self, cr, uid, ids, context=None):
        return self.write(cr,uid,ids,{'state':'done'},context=context)
    
    def create_lead(self, cr, uid, ids, context=None):
        res = False
        self.set_done(cr,uid,ids,context=context)

        for cur_id in ids:            
            '''creates the lead and builds the redirection to its details'''
            res = {
                'name':_("Lead created"),
                'view_mode': 'form',
                'view_type': 'form',
                'res_id' : self.action_create_lead(cr, uid, cur_id, context=context),
                'res_model': 'crm.lead',
                'type': 'ir.actions.act_window'
            }
            
        # redirects to the last lead created
        return res
    
    def action_create_lead(self, cr, uid, id, context=None):
        lead = self.pool.get('crm.lead')
        curEntry = self.browse(cr,uid,id,context=context)
        
        return lead.create(cr, uid, {
                'name': curEntry.lead_name,
                'email_cc': curEntry.lead_email_cc,
                'section_id': curEntry.salesteam.id or False,
                'user_id': curEntry.salesman.id,
                'description': curEntry.description,
                'partner_name': curEntry.partner_name,
                'partner_activity': curEntry.activity.id,
                'partner_market': curEntry.market.id,
                'channel_id': curEntry.source.id,
                'contact_name': curEntry.contact_name,
                'email_from': curEntry.contact_email,
                'phone': curEntry.contact_phone,
                'title': curEntry.contact_title and curEntry.contact_title.id or False,
                'function': curEntry.contact_function,
                'street': curEntry.contact_street,
                'street2': curEntry.contact_street2,
                'zip': curEntry.contact_zip,
                'city': curEntry.contact_city,
                'country_id': curEntry.contact_country and curEntry.contact_country.id or False,
                'type': 'lead',
                'stage_id': 2,  #'new' state
                'state': 'draft'
            }, context)
    
    def create_contact(self, cr, uid, ids, context={}):
        address = self.pool.get('res.partner.address')
        self.set_done(cr,uid,ids,context=context)
        
        entries = self.browse(cr, uid, ids, context=context)
        
        for curEntry in entries:
            '''Creates or retrieve the partner'''
            curPartner = self.get_partner(cr, uid, curEntry, context=context)
            
            '''Creates or retrieve the contact'''
            curEmail = curEntry.contact_email and to_email(curEntry.contact_email)[0]
            if curEmail:
                same_contacts = address.search(cr, uid, [('email', '=', curEmail), ('partner_id', '=', curPartner)], context=context)
                if len(same_contacts) > 0:
                    '''Raise an exception informing the user that the contact wasn't created'''
                    raise osv.except_osv(_('Contact not added'),
                        _('A contact with the given email already exists.'))
                        
            '''creates the contact and redirect to its page'''
            return {
                'name':_("Contact created"),
                'view_mode': 'form',
                'view_type': 'form',
                'res_id' : self.action_create_contact(cr, uid, curEntry, curPartner, context=context),
                'res_model': 'res.partner.address',
                'type': 'ir.actions.act_window'
            }
        return False
    
    def action_create_contact(self, cr, uid, curEntry, curPartner, context=None):
        address = self.pool.get('res.partner.address')
        return address.create(cr, uid, {
                'partner_id': curPartner,
                'name': curEntry.contact_name,
                'phone': curEntry.contact_phone,
                'email': curEntry.contact_email and to_email(curEntry.contact_email)[0],
                'title': curEntry.contact_title and curEntry.contact_title.id or False,
                'function': curEntry.contact_function,
                'street': curEntry.contact_street,
                'street2': curEntry.contact_street2,
                'zip': curEntry.contact_zip,
                'city': curEntry.contact_city,
                'country_id': curEntry.contact_country and curEntry.contact_country.id or False,
                'lang': curEntry.lang,
                'type': 'other'
            }, context)
        
    def get_partner(self, cr, uid, curEntry, context=None):
        partner = self.pool.get('res.partner')
        same_Partner = partner.search(cr, uid, [('name', '=', curEntry.partner_name or curEntry.contact_name or curEntry.lead_name)], context=context)
        
        if len(same_Partner) > 0:
             return same_Partner[0]
        return partner.create(cr, uid, {
                'name': curEntry.partner_name or curEntry.contact_name or curEntry.lead_name,
                #'ref': curEntry.lead_name,
                'user_id': curEntry.salesman.id,
                'comment': curEntry.description,
                'section_id': curEntry.salesteam.id or False,
                'address': [],
                'activity': curEntry.activity.id,
                'market': curEntry.market.id,
                'source': curEntry.source.id
            }, context)