1
1
# -*- encoding: utf-8 -*-
2
2
##############################################################################
4
# Copyright (C) 2010 OpenERP Italian Community (<http://www.openerp-italia.org>).
4
# Copyright (C) 2010 OpenERP Italian Community
5
# (<http://www.openerp-italia.org>).
6
# Copyright (C) 2010 Associazione OpenERP Italia.
8
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Affero General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
9
# it under the terms of the GNU Affero General Public License as published
10
# by the Free Software Foundation, either version 3 of the License, or
11
11
# (at your option) any later version.
13
13
# This program is distributed in the hope that it will be useful,
14
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
16
# GNU Affero General Public License for more details.
18
18
# You should have received a copy of the GNU General Public License
19
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
24
24
from osv import fields
25
25
from tools.translate import _
27
28
class res_region(osv.osv):
28
29
_name = 'res.region'
29
30
_description = 'Region'
31
'name': fields.char('Region Name', size=64, help='The full name of the region.', required=True),
33
'Region Name', size=64, help='The full name of the region.',
32
35
'country_id': fields.many2one('res.country', 'Country'),
36
40
class res_province(osv.osv):
37
41
_name = 'res.province'
38
42
_description = 'Province'
40
'name': fields.char('Province Name', size=64, help='The full name of the province.', required=True),
41
'code': fields.char('Province Code', size=2, help='The province code in two chars.',required=True),
42
'region': fields.many2one('res.region','Region'),
45
'Province Name', size=64, help='The full name of the province.',
48
'Province Code', size=2, help='The province code in two chars.',
50
'region': fields.many2one('res.region', 'Region'),
47
56
class res_city(osv.osv):
49
58
_description = 'City'
51
60
'name': fields.char('City', size=64, required=True),
52
'province_id': fields.many2one('res.province','Province'),
61
'province_id': fields.many2one('res.province', 'Province'),
53
62
'zip': fields.char('ZIP', size=5),
54
'phone_prefix': fields.char('Telephone Prefix' , size=16),
63
'phone_prefix': fields.char('Telephone Prefix', size=16),
55
64
'istat_code': fields.char('ISTAT code', size=16),
56
65
'cadaster_code': fields.char('Cadaster Code', size=16),
57
66
'web_site': fields.char('Web Site', size=64),
58
'region': fields.related('province_id','region',type='many2one', relation='res.region', string='Region', readonly=True),
67
'region': fields.related(
68
'province_id', 'region', type='many2one', relation='res.region',
69
string='Region', readonly=True),
61
73
class res_partner(osv.osv):
62
74
_inherit = 'res.partner'
69
81
def on_change_city(self, cr, uid, ids, city):
72
city_id = self.pool.get('res.city').search(cr, uid, [('name', '=', city.title())])
84
city_id = self.pool.get('res.city').search(
85
cr, uid, [('name', '=ilike', city)])
74
city_obj = self.pool.get('res.city').browse(cr, uid, city_id[0])
87
city_obj = self.pool.get('res.city').browse(
76
'province': city_obj.province_id and city_obj.province_id.id or False,
91
city_obj.province_id and city_obj.province_id.id
77
94
'region': city_obj.region and city_obj.region.id or False,
78
95
'zip': city_obj.zip,
79
'country_id': city_obj.region and city_obj.region.country_id and city_obj.region.country_id.id or False,
98
city_obj.region.country_id and
99
city_obj.region.country_id.id or False
80
101
'city': city.title(),
84
106
def _set_vals_city_data(self, cr, uid, vals):
85
if vals.has_key('city') and not vals.has_key('province') and not vals.has_key('region'):
107
if 'city' in vals and 'province' not in vals and 'region' not in vals:
87
city_obj= self.pool.get('res.city')
88
city_ids = city_obj.search(cr, uid, [('name', '=', vals['city'].title())])
109
city_obj = self.pool.get('res.city')
110
city_ids = city_obj.search(
111
cr, uid, [('name', '=ilike', vals['city'])])
90
113
city = city_obj.browse(cr, uid, city_ids[0])
91
if not vals.has_key('zip'):
114
if 'zip' not in vals:
92
115
vals['zip'] = city.zip
93
116
if city.province_id:
94
117
vals['province'] = city.province_id.id
105
128
def write(self, cr, uid, ids, vals, context=None):
106
129
vals = self._set_vals_city_data(cr, uid, vals)
107
130
return super(res_partner, self).write(cr, uid, ids, vals, context)