68 lines
2.7 KiB
Python
68 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import models, fields, api
|
|
|
|
class AddressAddress(models.Model):
|
|
_name = 'address.address'
|
|
# loc_code = fields.Char()
|
|
_rec_name = "location_name"
|
|
_description = "Address_Add"
|
|
location_name = fields.Char(string='ឈ្មោះទីតាំង', required=True)
|
|
parent_location = fields.Many2one('address.address', string='ទីកន្លែងមេ')
|
|
children_ids = fields.One2many('address.address', 'parent_location', 'Children', copy=True)
|
|
loc_code = fields.Selection([('1', 'រាជធានី/ខេត្ត'),
|
|
('2', 'ស្រុក/ខ័ណ្ឌ/ក្រុង'),
|
|
('3', 'ឃុំ/សង្កាត់'),
|
|
('4', 'ភូមិ')], string='ជា')
|
|
|
|
# -------------------------------------------------------------------------
|
|
# Helper Methods for Portal
|
|
# -------------------------------------------------------------------------
|
|
|
|
@api.model
|
|
def get_locations_by_level(self, level_code, parent_id=None):
|
|
"""
|
|
Get locations filtered by level and optional parent
|
|
@param level_code: '1', '2', '3', or '4'
|
|
@param parent_id: ID of parent location (for levels 2-4)
|
|
@return: recordset of address.address
|
|
"""
|
|
domain = [('loc_code', '=', level_code)]
|
|
if parent_id and level_code != '1':
|
|
domain.append(('parent_location', '=', parent_id))
|
|
return self.sudo().search(domain, order='location_name')
|
|
|
|
@api.model
|
|
def get_location_path(self, location_id):
|
|
"""
|
|
Get full path string for a location
|
|
@return: "Province > District > Commune > Village"
|
|
"""
|
|
location = self.browse(location_id)
|
|
if not location.exists():
|
|
return ''
|
|
|
|
path = []
|
|
current = location
|
|
while current:
|
|
path.insert(0, current.location_name)
|
|
current = current.parent_location
|
|
|
|
return ' > '.join(path)
|
|
|
|
@api.model
|
|
def get_cascading_options(self, parent_id=None):
|
|
"""
|
|
Get all child options for cascading dropdowns
|
|
Useful for pre-loading in form context
|
|
@return: dict of {loc_code: [{id, name}]}
|
|
"""
|
|
result = {}
|
|
for code in ['2', '3', '4']: # District, Commune, Village
|
|
domain = [('loc_code', '=', code)]
|
|
if parent_id:
|
|
# Get children of this parent at any level
|
|
domain.append(('parent_location', '=', parent_id))
|
|
items = self.sudo().search(domain, order='location_name')
|
|
result[code] = [{'id': i.id, 'name': i.location_name} for i in items]
|
|
return result |