diff options
| author | Indoteknik . <it@fixcomart.co.id> | 2025-06-11 09:50:05 +0700 |
|---|---|---|
| committer | Indoteknik . <it@fixcomart.co.id> | 2025-06-11 09:50:05 +0700 |
| commit | 771cd3b9f5c0a6594b6e276bc47e3599d6c751e4 (patch) | |
| tree | d0e5bcc7d1514ced3b0f66839d2a19f68fe57112 | |
| parent | 361b55a0a8b9837ddf4fec369b66e5d8dd628fbf (diff) | |
(andri) add validasi tidak bisa pinpoint ketika alamat yang diisikan belum lengkap
| -rw-r--r-- | indoteknik_custom/models/res_partner.py | 62 | ||||
| -rw-r--r-- | indoteknik_custom/views/res_partner.xml | 4 |
2 files changed, 40 insertions, 26 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.") diff --git a/indoteknik_custom/views/res_partner.xml b/indoteknik_custom/views/res_partner.xml index b7834fb4..6acfeaa0 100644 --- a/indoteknik_custom/views/res_partner.xml +++ b/indoteknik_custom/views/res_partner.xml @@ -66,9 +66,9 @@ <field name="leadtime"/> </group> <xpath expr="//notebook/page[@name='contact_addresses']" position="before"> - <page string="Lokasi Peta" name="map_location" autofocus="autofocus"> + <page string="Pin Point Location" name="map_location" autofocus="autofocus"> <group> - <button name="geocode_address" type="object" string="Dapatkan PinPoint Lokasi" class="btn btn-primary"/> + <button name="geocode_address" type="object" string="Get Pin Point Location" class="btn btn-primary"/> </group> <div style="margin: 16px 0;"> <field name="map_view" widget="openstreetmap" nolabel="1" style="width: 100%;"/> |
