company.py
3.33 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
import logging
from odoo import models, fields, api
class Company(models.Model):
_name = 'res.company'
_inherit = 'res.company'
test = fields.Char()
district_id = fields.Many2one(
'res.district', compute='_compute_address', inverse='_inverse_district',
string="Kab/Kota", domain="[('state_id', '=?', state_id)]"
)
sub_district_id = fields.Many2one(
'res.district.sub', compute='_compute_address', inverse='_inverse_sub_district',
string="Kab/Kota", domain="[('district_id', '=?', district_id)]"
)
village_id = fields.Many2one(
'res.district.village', compute='_compute_address', inverse='_inverse_village',
string="Desa/Kelurahan", domain="[('sub_district_id', '=?', sub_district_id)]"
)
company_id = fields.Many2one(
'res.company', compute='_compute_address', inverse='_inverse_company',
string="Company"
)
def _get_company_address_field_names(self):
""" Return a list of fields coming from the address partner to match
on company address fields. Fields are labeled same on both models. """
return ['street', 'street2', 'city', 'zip', 'state_id', 'country_id',
'district_id', 'sub_district_id', 'village_id']
def _inverse_company(self):
for company in self:
company.partner_id.company_id = company.id
def _inverse_district(self):
for company in self:
company.partner_id.district_id = company.district_id
def _inverse_sub_district(self):
for company in self:
company.partner_id.sub_district_id = company.sub_district_id
def _inverse_village(self):
for company in self:
company.partner_id.village_id = company.village_id
@api.onchange('village_id')
def _onchange_village_id(self):
if self.village_id and self.village_id.sub_district_id != self.sub_district_id:
self.sub_district_id = self.village_id.sub_district_id
if self.village_id and self.village_id.zip and self.village_id.zip != self.zip:
self.zip = self.village_id.zip
@api.onchange('sub_district_id')
def _onchange_sub_district_id(self):
if self.sub_district_id and self.sub_district_id.district_id != self.district_id:
self.district_id = self.sub_district_id.district_id
if not self.sub_district_id or self.sub_district_id != self.village_id.sub_district_id:
self.village_id = False
@api.onchange('district_id')
def _onchange_district_id(self):
if self.district_id and self.district_id.state_id != self.state_id:
self.state_id = self.district_id.state_id
if not self.district_id or self.district_id != self.sub_district_id.district_id:
self.sub_district_id = False
@api.onchange('state_id')
def _onchange_state_id(self):
if self.state_id and self.state_id.country_id != self.country_id:
self.country_id = self.state_id.country_id
if not self.state_id or self.state_id != self.district_id.state_id:
self.district_id = False
@api.onchange('country_id')
def _onchange_country_id(self):
if not self.country_id or self.country_id != self.state_id.country_id:
self.state_id = False