summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models
diff options
context:
space:
mode:
authorIndoteknik . <it@fixcomart.co.id>2025-06-11 09:50:05 +0700
committerIndoteknik . <it@fixcomart.co.id>2025-06-11 09:50:05 +0700
commit771cd3b9f5c0a6594b6e276bc47e3599d6c751e4 (patch)
treed0e5bcc7d1514ced3b0f66839d2a19f68fe57112 /indoteknik_custom/models
parent361b55a0a8b9837ddf4fec369b66e5d8dd628fbf (diff)
(andri) add validasi tidak bisa pinpoint ketika alamat yang diisikan belum lengkap
Diffstat (limited to 'indoteknik_custom/models')
-rw-r--r--indoteknik_custom/models/res_partner.py62
1 files changed, 38 insertions, 24 deletions
diff --git a/indoteknik_custom/models/res_partner.py b/indoteknik_custom/models/res_partner.py
index b8bdfe22..a15398c7 100644
--- a/indoteknik_custom/models/res_partner.py
+++ b/indoteknik_custom/models/res_partner.py
@@ -537,32 +537,42 @@ class ResPartner(models.Model):
def geocode_address(self):
for rec in self:
- # Ambil nama dari relasi (Many2one) atau gunakan nilai default
- kelurahan = rec.kelurahan_id.name if rec.kelurahan_id else ''
- kecamatan = rec.kecamatan_id.name if rec.kecamatan_id else ''
- kota = rec.kota_id.name if rec.kota_id else ''
- zip_code = rec.zip or ''
- state = rec.state_id.name if rec.state_id else ''
- country = rec.country_id.name if rec.country_id else ''
- street = rec.street or ''
-
- # Susun alamat lengkap sesuai urutan lokal
- address = ', '.join(filter(None, [
- street,
- kelurahan,
- kecamatan,
- kota,
- zip_code,
- state,
- country,
- ]))
-
- if not address:
- continue
-
+ # Daftar field penting
+ required_fields = {
+ 'Alamat Jalan (street)': rec.street,
+ 'Kelurahan': rec.kelurahan_id.name if rec.kelurahan_id else '',
+ 'Kecamatan': rec.kecamatan_id.name if rec.kecamatan_id else '',
+ 'Kota': rec.kota_id.name if rec.kota_id else '',
+ 'Kode Pos': rec.zip,
+ 'Provinsi': rec.state_id.name if rec.state_id else '',
+ 'Negara': rec.country_id.name if rec.country_id else '',
+ }
+
+ # Cek jika ada yang kosong
+ missing = [label for label, val in required_fields.items() if not val]
+ if missing:
+ raise UserError(
+ "Alamat tidak lengkap. Mohon lengkapi field berikut:\n- " + "\n- ".join(missing)
+ )
+
+ # Susun alamat lengkap
+ address = ', '.join([
+ required_fields['Alamat Jalan (street)'],
+ required_fields['Kelurahan'],
+ required_fields['Kecamatan'],
+ required_fields['Kota'],
+ required_fields['Kode Pos'],
+ required_fields['Provinsi'],
+ required_fields['Negara'],
+ ])
+
+ # Ambil API Key
api_key = self.env['ir.config_parameter'].sudo().get_param('google.maps.api_key')
- url = f'https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={api_key}'
+ if not api_key:
+ raise UserError("API Key Google Maps belum dikonfigurasi. Silakan isi melalui Settings.")
+ # Request ke Google Maps
+ url = f'https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={api_key}'
response = requests.get(url)
if response.ok:
@@ -571,3 +581,7 @@ class ResPartner(models.Model):
location = result['results'][0]['geometry']['location']
rec.latitude = location['lat']
rec.longtitude = location['lng']
+ else:
+ raise UserError("Tidak ditemukan hasil geocode untuk alamat tersebut.")
+ else:
+ raise UserError("Permintaan ke Google Maps gagal. Periksa koneksi internet atau API Key.")