summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2024-09-30 14:04:23 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2024-09-30 14:04:23 +0700
commite48193b793216a4ab82d88753ae144c08c3ddfaf (patch)
treee995192bb00ee5a5b3ffcb200040b464d4ec6779
parent0d1bf664d93ff02a42b1962fcf034690b105b8a9 (diff)
estimated shipping price
-rwxr-xr-xindoteknik_custom/models/sale_order.py48
-rw-r--r--indoteknik_custom/models/sale_order_line.py2
-rwxr-xr-xindoteknik_custom/views/sale_order.xml2
3 files changed, 52 insertions, 0 deletions
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py
index 3a28bc54..798b9109 100755
--- a/indoteknik_custom/models/sale_order.py
+++ b/indoteknik_custom/models/sale_order.py
@@ -131,6 +131,54 @@ class SaleOrder(models.Model):
payment_term_id = fields.Many2one(
'account.payment.term', string='Payment Terms', check_company=True, # Unrequired company
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", tracking=True)
+
+ def action_estimate_shipping(self):
+ 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
+ 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)
+ 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):
+ 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
+ 'courier': courier,
+ }
+ response = requests.post(url, headers=headers, data=data)
+ if response.status_code == 200:
+ return response.json()
+ return None
def _compute_type_promotion(self):
for rec in self:
diff --git a/indoteknik_custom/models/sale_order_line.py b/indoteknik_custom/models/sale_order_line.py
index 50438dbe..0ea6a2cc 100644
--- a/indoteknik_custom/models/sale_order_line.py
+++ b/indoteknik_custom/models/sale_order_line.py
@@ -31,6 +31,7 @@ class SaleOrderLine(models.Model):
qty_reserved = fields.Float(string='Qty Reserved', compute='_compute_qty_reserved')
reserved_from = fields.Char(string='Reserved From', copy=False)
item_percent_margin_without_deduction = fields.Float('%Margin', compute='_compute_item_margin_without_deduction')
+ weight = fields.Float(string='Weight')
@api.constrains('note_procurement')
def note_procurement_to_apo(self):
@@ -244,6 +245,7 @@ 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
diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml
index f2789254..5ef399c7 100755
--- a/indoteknik_custom/views/sale_order.xml
+++ b/indoteknik_custom/views/sale_order.xml
@@ -84,6 +84,7 @@
string="Override Create Invoice"
type="object"
/>
+ <button string="Estimate Shipping" type="object" name="action_estimate_shipping"/>
</field>
<field name="partner_shipping_id" position="after">
<field name="real_shipping_id"/>
@@ -130,6 +131,7 @@
<field name="note" optional="hide"/>
<field name="note_procurement" optional="hide"/>
<field name="vendor_subtotal" optional="hide"/>
+ <field name="weight" optional="hide"/>
<field name="amount_voucher_disc" string="Voucher" readonly="1" optional="hide"/>
<field name="order_promotion_id" string="Promotion" readonly="1" optional="hide"/>
</xpath>