summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIndoteknik . <it@fixcomart.co.id>2025-06-13 10:58:27 +0700
committerIndoteknik . <it@fixcomart.co.id>2025-06-13 10:58:27 +0700
commit0e7fdb8ea85c53de2c8ad5fa8674c5fbc489e45a (patch)
tree5bbafaabdebc1ae724a0031334c97ae9de1aa00e
parent9df666030d1e3a4bd1293e96122f1df8611f1cd4 (diff)
(andri) bisa ubah alamat via ubah pinpoin langsung
-rw-r--r--indoteknik_custom/models/res_partner.py164
-rw-r--r--indoteknik_custom/views/res_partner.xml4
2 files changed, 92 insertions, 76 deletions
diff --git a/indoteknik_custom/models/res_partner.py b/indoteknik_custom/models/res_partner.py
index 82aa1134..6ef5698c 100644
--- a/indoteknik_custom/models/res_partner.py
+++ b/indoteknik_custom/models/res_partner.py
@@ -188,6 +188,10 @@ class ResPartner(models.Model):
def write(self, vals):
res = super(ResPartner, self).write(vals)
+
+ for rec in self:
+ if 'latitude' in vals or 'longtitude' in vals:
+ rec._update_address_from_coords()
#
# # if 'property_payment_term_id' in vals:
# # if not self.env.user.is_accounting and vals['property_payment_term_id'] != 26:
@@ -199,6 +203,14 @@ class ResPartner(models.Model):
# # raise UserError('You name it')
#
return res
+
+ @api.model
+ def create(self, vals):
+ records = super().create(vals)
+ for rec in records:
+ if vals.get('latitude') and vals.get('longtitude'):
+ rec._update_address_from_coords()
+ return records
@api.constrains('name')
def _check_duplicate_name(self):
@@ -579,84 +591,88 @@ class ResPartner(models.Model):
result = response.json()
if result.get('results'):
location = result['results'][0]['geometry']['location']
+ formatted_address = result['results'][0].get('formatted_address', '')
+
rec.latitude = location['lat']
rec.longtitude = location['lng']
+ rec.address_map = formatted_address # ✅ Simpan alamat lengkap
else:
raise UserError("Tidak ditemukan hasil geocode untuk alamat tersebut.")
else:
raise UserError("Permintaan ke Google Maps gagal. Periksa koneksi internet atau API Key.")
- # def _update_address_from_coords(self):
- # for rec in self:
- # if rec.latitude and rec.longtitude:
- # address = self.reverse_geocode(rec.latitude, rec.longtitude)
- # if not address:
- # continue
-
- # updates = {
- # 'street': address.get('road') or '',
- # 'zip': address.get('postcode') or '',
- # 'city': address.get('city') or address.get('town') or address.get('village') or '',
- # }
-
- # # Kelurahan (vit.kelurahan)
- # if address.get('suburb'):
- # kel = self.env['vit.kelurahan'].search([
- # ('name', 'ilike', address['suburb'])
- # ], limit=1)
- # if kel:
- # updates['kelurahan_id'] = kel.id
-
- # # Kecamatan (vit.kecamatan)
- # kec_nama = address.get('district') or address.get('village')
- # if kec_nama:
- # kec = self.env['vit.kecamatan'].search([
- # ('name', 'ilike', kec_nama)
- # ], limit=1)
- # if kec:
- # updates['kecamatan_id'] = kec.id
-
- # # Kota (vit.kota)
- # kota_nama = address.get('city') or address.get('town')
- # if kota_nama:
- # kota = self.env['vit.kota'].search([
- # ('name', 'ilike', kota_nama)
- # ], limit=1)
- # if kota:
- # updates['kota_id'] = kota.id
-
- # # Provinsi (res.country.state)
- # if address.get('state'):
- # state = self.env['res.country.state'].search([
- # ('name', 'ilike', address['state'])
- # ], limit=1)
- # if state:
- # updates['state_id'] = state.id
-
- # # Negara (res.country)
- # if address.get('country_code'):
- # country = self.env['res.country'].search([
- # ('code', '=', address['country_code'].upper())
- # ], limit=1)
- # if country:
- # updates['country_id'] = country.id
-
- # rec.write(updates)
-
-
-
- # def reverse_geocode(self, lat, lon):
- # try:
- # url = f'https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat={lat}&lon={lon}'
- # headers = {
- # 'User-Agent': 'Odoo/1.0 (andrifebriyadiputra@gmail.com)', # WAJIB: ganti dengan email domain kamu
- # }
- # response = requests.get(url, headers=headers, timeout=5)
- # if response.ok:
- # data = response.json()
- # return data.get('address', {})
- # else:
- # _logger.warning("Reverse geocode failed with status %s: %s", response.status_code, response.text)
- # except Exception as e:
- # _logger.exception("Exception during reverse geocode: %s", e)
- # return {}
+ def _update_address_from_coords(self):
+ for rec in self:
+ if rec.latitude and rec.longtitude:
+ try:
+ components, formatted, parsed = rec._reverse_geocode(rec.latitude, rec.longtitude)
+ if not parsed:
+ continue
+
+ updates = {
+ 'street': parsed.get('road') or '',
+ 'zip': parsed.get('postcode') or '',
+ 'address_map': formatted or '',
+ }
+
+ state = self.env['res.country.state'].search([('name', 'ilike', parsed.get('state'))], limit=1)
+ if state:
+ updates['state_id'] = state.id
+
+ kota = self.env['vit.kota'].search([('name', 'ilike', parsed.get('city'))], limit=1)
+ if kota:
+ updates['kota_id'] = kota.id
+
+ kec = self.env['vit.kecamatan'].search([('name', 'ilike', parsed.get('district'))], limit=1)
+ if kec:
+ updates['kecamatan_id'] = kec.id
+
+ kel = self.env['vit.kelurahan'].search([('name', 'ilike', parsed.get('suburb'))], limit=1)
+ if kel:
+ updates['kelurahan_id'] = kel.id
+
+ rec.update(updates)
+
+ except Exception as e:
+ raise UserError(f"Gagal update alamat dari koordinat: {str(e)}")
+
+
+ def _reverse_geocode(self, lat, lng):
+ api_key = self.env['ir.config_parameter'].sudo().get_param('google.maps.api_key')
+ if not api_key:
+ raise UserError("API Key Google Maps belum dikonfigurasi.")
+
+ url = f'https://maps.googleapis.com/maps/api/geocode/json?latlng={lat},{lng}&key={api_key}'
+ response = requests.get(url)
+ if response.ok:
+ result = response.json()
+ if result.get('results'):
+ components = result['results'][0]['address_components']
+ formatted = result['results'][0]['formatted_address']
+ return components, formatted, self._parse_google_address(components)
+ return {}, '', {}
+
+ def _parse_google_address(self, components):
+ def get(types):
+ for comp in components:
+ if types in comp['types']:
+ return comp['long_name']
+ return ''
+
+ street_number = get('street_number')
+ route = get('route')
+ neighborhood = get('neighborhood') # Bisa jadi nama RW
+ subpremise = get('subpremise') # Bisa jadi no kamar/ruko
+
+ # Gabungkan informasi jalan
+ road = " ".join(filter(None, [route, street_number, subpremise, neighborhood]))
+
+ return {
+ 'road': road.strip(),
+ 'postcode': get('postal_code'),
+ 'state': get('administrative_area_level_1'),
+ 'city': get('administrative_area_level_2') or get('locality'),
+ 'district': get('administrative_area_level_3'),
+ 'suburb': get('administrative_area_level_4'),
+ 'formatted': get('formatted_address'),
+ }
diff --git a/indoteknik_custom/views/res_partner.xml b/indoteknik_custom/views/res_partner.xml
index bdce7c0c..f739cf14 100644
--- a/indoteknik_custom/views/res_partner.xml
+++ b/indoteknik_custom/views/res_partner.xml
@@ -75,8 +75,8 @@
</div>
<group>
<field name="address_map"/>
- <field name="latitude" readonly="1"/>
- <field name="longtitude" readonly="1"/>
+ <field name="latitude" readonly="1" force_save="1"/>
+ <field name="longtitude" readonly="1" force_save="1"/>
</group>
</page>
</xpath>