company.py
5.09 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import logging
from odoo import models, fields, api
class Company(models.Model):
_name = 'res.company'
_inherit = 'res.company'
test = fields.Char()
company_id = fields.Many2one(
'res.company', compute='_compute_address', inverse='_inverse_company',
string="Company"
)
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="Kecamatan", 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)]"
)
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. """
# todo saat install error karena
# belum ada pada partner
# if self.partner_id
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
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']
def _get_company_address_update(self, partner):
return dict((fname, partner[fname])
for fname in self._get_company_address_field_names())
# TODO @api.depends(): currently now way to formulate the dependency on the
# partner's contact address
def _compute_address(self):
for company in self.filtered(lambda company: company.partner_id):
address_data = company.partner_id.sudo().address_get(adr_pref=['contact'])
if address_data['contact']:
partner = company.partner_id.browse(address_data['contact']).sudo()
company.update(company._get_company_address_update(partner))
def _inverse_street(self):
for company in self:
company.partner_id.street = company.street
def _inverse_street2(self):
for company in self:
company.partner_id.street2 = company.street2
def _inverse_zip(self):
for company in self:
company.partner_id.zip = company.zip
def _inverse_city(self):
for company in self:
company.partner_id.city = company.city
def _inverse_state(self):
for company in self:
company.partner_id.state_id = company.state_id
def _inverse_country(self):
for company in self:
company.partner_id.country_id = company.country_id