partner.py 3.23 KB
from odoo import fields, models, api

ADDRESS_FIELDS = (
    'street', 'street2', 'zip', 'city', 'state_id', 'country_id',
    'village_id', 'sub_district_id', 'district_id'
)


class ResPartner(models.Model):
    _inherit = 'res.partner'
    # company_id = fields.Many2one('res.company', string="Company",
    #                              default=lambda self: self.env.company.id
    #                              if not self.company_id else False
    #                              )
    #
    # country_id = fields.Many2one('res.country', string="Country",
    #                              default=lambda self: self.env.company.country_id
    #                              if not self.country_id else False
    #                              )
    # state_id = fields.Many2one('res.country.state', string="State",
    #                            default=lambda self: self.env.company.state_id
    #                            if not self.state_id else False
    #                            )
    #
    district_id = fields.Many2one('res.district', string="Kabupaten/Kota",
                                  ondelete='restrict', domain="[('state_id', '=?', state_id)]"
                                  # default=lambda self: self.env.company.district_id
                                  # if not self.district_id else False
                                  )

    sub_district_id = fields.Many2one('res.district.sub', string="Kecamatan",
                                      ondelete='restrict', domain="[('district_id', '=?', district_id)]"

                                      )
    village_id = fields.Many2one('res.district.village', string="Desa/Kelurahan",
                                 ondelete='restrict', domain="[('sub_district_id', '=?', sub_district_id)]"
                                 )

    @api.model
    def _address_fields(self):
        """Returns the list of address fields that are synced from the parent."""
        return list(ADDRESS_FIELDS)

    @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 not self.state_id or self.state_id != self.district_id.state_id:
            self.district_id = False

# from . import company