res_company.py
2.55 KB
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
from logging import getLogger
from odoo import (
models,
fields,
api,
)
from odoo.exceptions import ValidationError
def range_validation(val, label, first=1, last=999):
if not val:
return
try:
n = int(val)
if first <= n <= last:
return str(n)
except ValueError:
pass
raise ValidationError(f'{label} harus angka {first} - {last}')
class Company(models.Model):
_inherit = 'res.company'
kabupaten_id = fields.Many2one(
'kabupaten', ondelete='restrict',
domain="[('state_id', '=?', state_id)]")
kecamatan_id = fields.Many2one(
'kecamatan', ondelete='restrict',
domain="[('kabupaten_id', '=?', kabupaten_id)]")
kelurahan_id = fields.Many2one(
'kelurahan', ondelete='restrict',
domain="[('kecamatan_id', '=?', kecamatan_id)]")
rw = fields.Char()
rt = fields.Char()
@api.onchange('country_id')
def _onchange_country_id(self):
if self.country_id:
self.currency_id = self.country_id.currency_id
if self.country_id != self.state_id.country_id:
self.state_id = False
else:
self.state_id = False
@api.onchange('state_id')
def _onchange_state(self):
if self.state_id:
self.country_id = self.state_id.country_id
if self.state_id != self.kabupaten_id.state_id:
self.kabupaten_id = False
else:
self.kabupaten_id = False
@api.onchange('kabupaten_id')
def _onchange_kabupaten_id(self):
if self.kabupaten_id:
self.state_id = self.kabupaten_id.state_id
self.city = self.kabupaten_id.name
if self.kabupaten_id != self.kecamatan_id.kabupaten_id:
self.kecamatan_id = False
else:
self.kecamatan_id = False
self.city = False
@api.onchange('kecamatan_id')
def _onchange_kecamatan_id(self):
if self.kecamatan_id:
self.kabupaten_id = self.kecamatan_id.kabupaten_id
if self.kecamatan_id != self.kelurahan_id.kecamatan_id:
self.kelurahan_id = False
else:
self.kelurahan_id = False
@api.onchange('kelurahan_id')
def _onchange_kelurahan_id(self):
if self.kelurahan_id:
self.kecamatan_id = self.kelurahan_id.kecamatan_id
@api.onchange('rw')
def _onchange_rw(self):
self.rw = range_validation(self.rw, 'RW')
@api.onchange('rt')
def _onchange_rt(self):
self.rt = range_validation(self.rt, 'RT')