summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2024-09-30 17:06:18 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2024-09-30 17:06:18 +0700
commitce74fd588cb8bf7cca770e0ac9717321e6b70ebe (patch)
tree0d73ba5296f038bba14b8d4fecf53d689f68a48d
parent3b83868fbdb29e8f5208035e5166a13e5d24f382 (diff)
estimated price shipping
-rwxr-xr-xindoteknik_custom/models/sale_order.py84
-rw-r--r--indoteknik_custom/models/sale_order_line.py2
-rwxr-xr-xindoteknik_custom/views/product_template.xml2
3 files changed, 69 insertions, 19 deletions
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py
index d0a34007..8f48e898 100755
--- a/indoteknik_custom/models/sale_order.py
+++ b/indoteknik_custom/models/sale_order.py
@@ -137,49 +137,100 @@ class SaleOrder(models.Model):
total_weight = 0
missing_weight_products = []
- # Menghitung total berat dari Sale Order Line
for line in self.order_line:
if line.weight:
- total_weight += line.weight * line.product_uom_qty
- line.product_id.weight = line.weight
+ total_weight += line.weight
else:
missing_weight_products.append(line.product_id.name)
- # Menampilkan pesan jika ada produk tanpa berat
- if missing_weight_products:
- self.message_post(body="Warning: Beberapa produk tidak memiliki berat: %s" % ', '.join(missing_weight_products))
-
if total_weight == 0:
raise UserError("Tidak dapat mengestimasi ongkir tanpa berat yang valid.")
- # Panggil API Raja Ongkir untuk mendapatkan estimasi ongkir
- result = self._call_rajaongkir_api(total_weight)
+ # Mendapatkan city_id berdasarkan nama kota
+ origin_city_name = self.warehouse_id.partner_id.kota_id.name
+ destination_city_name = self.real_shipping_id.kota_id.name
+
+ origin_city_id = self._get_city_id_by_name(origin_city_name)
+ destination_city_id = self._get_city_id_by_name(destination_city_name)
+
+ if not origin_city_id or not destination_city_id:
+ raise UserError("Gagal mendapatkan ID kota asal atau tujuan.")
+
+ result = self._call_rajaongkir_api(total_weight, origin_city_id, destination_city_id)
if result:
estimated_cost = result['rajaongkir']['results'][0]['costs'][0]['cost'][0]['value']
- # Memasukkan hasil estimasi ke field delivery_amt
self.delivery_amt = estimated_cost
self.message_post(body=f"Estimasi Ongkos Kirim: {estimated_cost}")
else:
raise UserError("Gagal mendapatkan estimasi ongkir.")
- def _call_rajaongkir_api(self, total_weight):
+ def _call_rajaongkir_api(self, total_weight, origin_city_id, destination_city_id):
url = 'https://pro.rajaongkir.com/api/cost'
headers = {
'key': '7ac9883688da043b50cc32f0e3070bb6',
}
courier = self.carrier_id.name.lower()
- origin = self.partner_shipping_id.city_id.state_id.code
- destination = self.partner_shipping_id.city_id.state_id.code
+
data = {
- 'origin': origin, # Contoh ID kota asal (Yogyakarta)
- 'destination': destination, # Contoh ID kota tujuan (Jakarta)
- 'weight': int(total_weight * 1000), # Menggunakan berat dalam gram
+ 'origin': int(origin_city_id),
+ 'originType': 'city',
+ 'destination': int(destination_city_id),
+ 'destinationType': 'city',
+ 'weight': int(total_weight * 1000),
'courier': courier,
}
+
response = requests.post(url, headers=headers, data=data)
if response.status_code == 200:
return response.json()
return None
+
+ def _normalize_city_name(self, city_name):
+ # Ubah nama kota menjadi huruf kecil
+ city_name = city_name.lower()
+
+ # Hilangkan prefiks "kabupaten" atau "kota" jika ada
+ if city_name.startswith('kabupaten'):
+ city_name = city_name.replace('kabupaten', '').strip()
+ elif city_name.startswith('kota'):
+ city_name = city_name.replace('kota', '').strip()
+
+ # Hilangkan spasi yang berlebihan
+ city_name = " ".join(city_name.split())
+
+ return city_name
+
+ def _get_city_id_by_name(self, city_name):
+ url = 'https://pro.rajaongkir.com/api/city'
+ headers = {
+ 'key': '7ac9883688da043b50cc32f0e3070bb6',
+ }
+
+ # Normalisasi nama kota sebelum melakukan pencarian
+ normalized_city_name = self._normalize_city_name(city_name)
+
+ response = requests.get(url, headers=headers)
+ if response.status_code == 200:
+ city_data = response.json()
+ for city in city_data['rajaongkir']['results']:
+ if city['city_name'].lower() == normalized_city_name:
+ return city['city_id']
+ return None
+
+ def _get_subdistrict_id_by_name(self, city_id, subdistrict_name):
+ url = f'https://pro.rajaongkir.com/api/subdistrict?city={city_id}'
+ headers = {
+ 'key': '7ac9883688da043b50cc32f0e3070bb6',
+ }
+
+ response = requests.get(url, headers=headers)
+ if response.status_code == 200:
+ subdistrict_data = response.json()
+ for subdistrict in subdistrict_data['rajaongkir']['results']:
+ if subdistrict['subdistrict_name'].lower() == subdistrict_name.lower():
+ return subdistrict['subdistrict_id']
+ return None
+
def _compute_type_promotion(self):
for rec in self:
@@ -189,7 +240,6 @@ class SaleOrder(models.Model):
if line_program.promotion_type:
promotion_types.append(dict(line_program._fields['promotion_type'].selection).get(line_program.promotion_type))
- # Removing duplicates by converting to a set, then back to a list
rec.type_promotion = ', '.join(sorted(set(promotion_types)))
def _compute_purchase_delivery_amount(self):
diff --git a/indoteknik_custom/models/sale_order_line.py b/indoteknik_custom/models/sale_order_line.py
index 0ea6a2cc..d1dcd0af 100644
--- a/indoteknik_custom/models/sale_order_line.py
+++ b/indoteknik_custom/models/sale_order_line.py
@@ -245,7 +245,6 @@ class SaleOrderLine(models.Model):
# query, limit=1, order='count_trx_po desc, count_trx_po_vendor desc')
price, taxes, vendor_id = self._get_purchase_price(line.product_id)
line.vendor_id = vendor_id
- line.weight = line.product_id.weight
line.tax_id = line.order_id.sales_tax_id
# price, taxes = line._get_valid_purchase_price(purchase_price)
line.purchase_price = price
@@ -259,6 +258,7 @@ class SaleOrderLine(models.Model):
('(' + attribute_values_str + ')' if attribute_values_str else '') + ' ' + \
(line.product_id.short_spesification if line.product_id.short_spesification else '')
line.name = line_name
+ line.weight = line.product_id.weight
def compute_delivery_amt_line(self):
for line in self:
diff --git a/indoteknik_custom/views/product_template.xml b/indoteknik_custom/views/product_template.xml
index b6155eea..a77b99de 100755
--- a/indoteknik_custom/views/product_template.xml
+++ b/indoteknik_custom/views/product_template.xml
@@ -22,7 +22,7 @@
<field name="last_update_solr" readonly="1" />
</field>
<field name="public_categ_ids" position="attributes">
- <attribute name="required">1</attribute>
+ <attribute name="required">0</attribute>
</field>
<field name="public_categ_ids" position="attributes">
<attribute name="options">{'no_create': True}</attribute>