company.py 5.05 KB
import logging

from odoo import models, fields, api

_logger = logging.getLogger(__name__)


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_local_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

    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):
        res = dict((fname, partner[fname])
                   for fname in self._get_local_company_address_field_names())
        return res

    # 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