~unifield-team/unifield-wm/us-671-homere

« back to all changes in this revision

Viewing changes to unifield_setup/installer/project_addresses.py

UF-359 [ADD] Account override module integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2011 TeMPO Consulting, MSF
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
 
 
22
 
from osv import osv
23
 
from osv import fields
24
 
 
25
 
 
26
 
class project_addresses(osv.osv_memory):
27
 
    _name = 'base.setup.company'
28
 
    _inherit = 'base.setup.company'
29
 
    
30
 
    def _get_all_states(self, cr, uid, context=None):
31
 
        return super(project_addresses, self)._get_all_states(cr, uid, context=context)
32
 
    
33
 
    def _get_all_countries(self, cr, uid, context=None):
34
 
        return super(project_addresses, self)._get_all_countries(cr, uid, context=context)
35
 
    
36
 
    _columns = {
37
 
        'second_time': fields.boolean('Config. Wizard launched for the second time'),
38
 
        'partner_name': fields.char(size=64, string='Partner name'),
39
 
        'ship_street':fields.char('Street', size=128),
40
 
        'ship_street2':fields.char('Street 2', size=128),
41
 
        'ship_zip':fields.char('Zip Code', size=24),
42
 
        'ship_city':fields.char('City', size=128),
43
 
        'ship_state_id':fields.selection(_get_all_states, 'Fed. State'),
44
 
        'ship_country_id':fields.selection(_get_all_countries, 'Country'),
45
 
        'ship_email':fields.char('E-mail', size=64),
46
 
        'ship_phone':fields.char('Phone', size=64),
47
 
        'bill_street':fields.char('Street', size=128),
48
 
        'bill_street2':fields.char('Street 2', size=128),
49
 
        'bill_zip':fields.char('Zip Code', size=24),
50
 
        'bill_city':fields.char('City', size=128),
51
 
        'bill_state_id':fields.selection(_get_all_states, 'Fed. State'),
52
 
        'bill_country_id':fields.selection(_get_all_countries, 'Country'),
53
 
        'bill_email':fields.char('E-mail', size=64),
54
 
        'bill_phone':fields.char('Phone', size=64),
55
 
    }
56
 
    
57
 
    def default_get(self, cr, uid, fields, context=None):
58
 
        '''
59
 
        Get the current address of the main partner and fill the form
60
 
        '''
61
 
        res = super(project_addresses, self).default_get(cr, uid, fields, context=context)
62
 
        
63
 
        if not 'company_id' in res:
64
 
            return res
65
 
        company = self.pool.get('res.company').browse(cr, uid, res['company_id'], context=context)
66
 
        company_id = company.partner_id.id
67
 
        addresses = self.pool.get('res.partner').address_get(cr, uid, company_id, ['invoice', 'delivery', 'default'])
68
 
        default_id = addresses.get('default', False)
69
 
        delivery_id = addresses.get('delivery', False) != default_id and addresses.get('delivery', False)
70
 
        bill_id = addresses.get('invoice', False) != default_id and addresses.get('invoice', False)
71
 
 
72
 
        res['partner_name'] = company.instance_id.instance
73
 
        res['name'] = company.instance_id.instance
74
 
        res['second_time'] = True
75
 
 
76
 
        if default_id:
77
 
            address = self.pool.get('res.partner.address').browse(cr, uid, default_id, context=context)
78
 
            for field in ['street','street2','zip','city','email','phone']:
79
 
                res[field] = address[field]
80
 
            for field in ['country_id','state_id']:
81
 
                if address[field]:
82
 
                    res[field] = address[field].id
83
 
                    
84
 
        if delivery_id:
85
 
            address = self.pool.get('res.partner.address').browse(cr, uid, delivery_id, context=context)
86
 
            for field in ['street','street2','zip','city','email','phone']:
87
 
                res['ship_%s' % field] = address[field]
88
 
            for field in ['country_id','state_id']:
89
 
                if address[field]:
90
 
                    res['ship_%s' % field] = address[field].id
91
 
                    
92
 
        if bill_id:
93
 
            address = self.pool.get('res.partner.address').browse(cr, uid, bill_id, context=context)
94
 
            for field in ['street','street2','zip','city','email','phone']:
95
 
                res['bill_%s' % field] = address[field]
96
 
            for field in ['country_id','state_id']:
97
 
                if address[field]:
98
 
                    res['bill_%s' % field] = address[field].id
99
 
        
100
 
        return res
101
 
    
102
 
    def execute(self, cr, uid, ids, context=None):
103
 
        '''
104
 
        Create project's addresses
105
 
        '''
106
 
        if not context:
107
 
            context = {}
108
 
 
109
 
        res = super(project_addresses, self).execute(cr, 1, ids, context=context)
110
 
        
111
 
        assert len(ids) == 1, "We should only get one object from the form"
112
 
        payload = self.browse(cr, uid, ids[0], context=context)
113
 
        if not getattr(payload, 'company_id', None):
114
 
            raise ValueError('Case where no default main company is setup ' 
115
 
                             'not handled yet')
116
 
 
117
 
        # TODO: quick fix
118
 
        self.pool.get('res.users').write(cr, uid, [uid], {'view': 'extended'}, context=context)
119
 
 
120
 
        company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
121
 
        address_obj = self.pool.get('res.partner.address')
122
 
 
123
 
        if payload.ship_street or payload.ship_street2 or payload.ship_zip or payload.ship_city \
124
 
           or payload.ship_email or payload.ship_phone or payload.ship_country_id:
125
 
            ship_address_data = {
126
 
                'type': 'delivery',
127
 
                'name':company.instance_id.instance,
128
 
                'street':payload.ship_street,
129
 
                'street2':payload.ship_street2,
130
 
                'zip':payload.ship_zip,
131
 
                'city':payload.ship_city,
132
 
                'email':payload.ship_email,
133
 
                'phone':payload.ship_phone,
134
 
                'country_id':int(payload.ship_country_id),
135
 
                'state_id':int(payload.ship_state_id),
136
 
            }
137
 
    
138
 
            ship_address = address_obj.search(cr, uid, [('type', '=', 'delivery'), ('partner_id', '=', company.partner_id.id)], context=context)
139
 
            if ship_address:
140
 
                address_obj.write(cr, uid, ship_address[0], ship_address_data, context=context)
141
 
            else:
142
 
                address_obj.create(cr, uid, dict(ship_address_data, partner_id=int(company.partner_id)),
143
 
                        context=context)
144
 
        else:
145
 
            ship_address = address_obj.search(cr, uid, [('type', '=', 'delivery'), ('partner_id', '=', company.partner_id.id)], context=context)
146
 
            if ship_address:
147
 
                address_obj.unlink(cr, uid, ship_address[0], context=context)
148
 
                
149
 
        if payload.bill_street or payload.bill_street2 or payload.bill_zip or payload.bill_city \
150
 
           or payload.bill_email or payload.bill_phone or payload.bill_country_id:    
151
 
            bill_address_data = {
152
 
                'type': 'invoice',
153
 
                'name':company.instance_id.instance,
154
 
                'street':payload.bill_street,
155
 
                'street2':payload.bill_street2,
156
 
                'zip':payload.bill_zip,
157
 
                'city':payload.bill_city,
158
 
                'email':payload.bill_email,
159
 
                'phone':payload.bill_phone,
160
 
                'country_id':int(payload.bill_country_id),
161
 
                'state_id':int(payload.bill_state_id),
162
 
            }
163
 
    
164
 
            bill_address = address_obj.search(cr, uid, [('type', '=', 'invoice'), ('partner_id', '=', company.partner_id.id)], context=context)
165
 
            if bill_address:
166
 
                address_obj.write(cr, uid, bill_address[0], bill_address_data, context=context)
167
 
            else:
168
 
                address_obj.create(cr, uid, dict(bill_address_data, partner_id=int(company.partner_id)),
169
 
                    context=context)
170
 
        else:
171
 
            bill_address = address_obj.search(cr, uid, [('type', '=', 'invoice'), ('partner_id', '=', company.partner_id.id)], context=context)
172
 
            if bill_address:
173
 
                address_obj.unlink(cr, uid, bill_address[0], context=context)
174
 
        
175
 
        if company.instance_id:
176
 
            self.pool.get('res.company').write(cr, 1, [company.id], {'name': company.instance_id.instance}, context=context)
177
 
 
178
 
            c = context.copy()
179
 
            c.update({'from_config': True})
180
 
            self.pool.get('res.partner').write(cr, uid, [company.partner_id.id], {'name': company.instance_id.instance,
181
 
                                                                                  'partner_type': 'internal'}, context=c)
182
 
 
183
 
        data_obj = self.pool.get('ir.model.data')
184
 
        warehouse_obj = self.pool.get('stock.warehouse')
185
 
        user_obj = self.pool.get('res.users')
186
 
 
187
 
        instance_name = user_obj.browse(cr, uid, uid, context=context).company_id.partner_id.name
188
 
 
189
 
        # Rename the warehouse with the name of the company
190
 
        warehouse_id = data_obj.get_object_reference(cr, uid, 'stock', 'warehouse0')[1]
191
 
        warehouse_obj.write(cr, uid, [warehouse_id], {'name': 'MSF %s' % instance_name}, context=context)
192
 
 
193
 
        return res
194
 
    
195
 
project_addresses()