1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 TeMPO Consulting, MSF
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
##############################################################################
23
from osv import fields
26
class project_addresses(osv.osv_memory):
27
_name = 'base.setup.company'
28
_inherit = 'base.setup.company'
30
def _get_all_states(self, cr, uid, context=None):
31
return super(project_addresses, self)._get_all_states(cr, uid, context=context)
33
def _get_all_countries(self, cr, uid, context=None):
34
return super(project_addresses, self)._get_all_countries(cr, uid, context=context)
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),
57
def default_get(self, cr, uid, fields, context=None):
59
Get the current address of the main partner and fill the form
61
res = super(project_addresses, self).default_get(cr, uid, fields, context=context)
63
if not 'company_id' in 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)
72
res['partner_name'] = company.instance_id.instance
73
res['name'] = company.instance_id.instance
74
res['second_time'] = True
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']:
82
res[field] = address[field].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']:
90
res['ship_%s' % field] = address[field].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']:
98
res['bill_%s' % field] = address[field].id
102
def execute(self, cr, uid, ids, context=None):
104
Create project's addresses
109
res = super(project_addresses, self).execute(cr, 1, ids, context=context)
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 '
118
self.pool.get('res.users').write(cr, uid, [uid], {'view': 'extended'}, context=context)
120
company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
121
address_obj = self.pool.get('res.partner.address')
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 = {
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),
138
ship_address = address_obj.search(cr, uid, [('type', '=', 'delivery'), ('partner_id', '=', company.partner_id.id)], context=context)
140
address_obj.write(cr, uid, ship_address[0], ship_address_data, context=context)
142
address_obj.create(cr, uid, dict(ship_address_data, partner_id=int(company.partner_id)),
145
ship_address = address_obj.search(cr, uid, [('type', '=', 'delivery'), ('partner_id', '=', company.partner_id.id)], context=context)
147
address_obj.unlink(cr, uid, ship_address[0], context=context)
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 = {
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),
164
bill_address = address_obj.search(cr, uid, [('type', '=', 'invoice'), ('partner_id', '=', company.partner_id.id)], context=context)
166
address_obj.write(cr, uid, bill_address[0], bill_address_data, context=context)
168
address_obj.create(cr, uid, dict(bill_address_data, partner_id=int(company.partner_id)),
171
bill_address = address_obj.search(cr, uid, [('type', '=', 'invoice'), ('partner_id', '=', company.partner_id.id)], context=context)
173
address_obj.unlink(cr, uid, bill_address[0], context=context)
175
if company.instance_id:
176
self.pool.get('res.company').write(cr, 1, [company.id], {'name': company.instance_id.instance}, context=context)
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)
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')
187
instance_name = user_obj.browse(cr, uid, uid, context=context).company_id.partner_id.name
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)