summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/sale_order.py
diff options
context:
space:
mode:
authorit-fixcomart <it@fixcomart.co.id>2024-09-30 15:24:37 +0700
committerit-fixcomart <it@fixcomart.co.id>2024-09-30 15:24:37 +0700
commitcb81068b8b695eaf8de5c0ba6b0951bae436626e (patch)
treefd4ce5250bc2e56e91e4ce394365438854909b42 /indoteknik_custom/models/sale_order.py
parent90a29846ae6008b3152d495f2e83a5d00cfdd438 (diff)
parent3b83868fbdb29e8f5208035e5166a13e5d24f382 (diff)
Merge branch 'production' into iman/switch-account
Diffstat (limited to 'indoteknik_custom/models/sale_order.py')
-rwxr-xr-xindoteknik_custom/models/sale_order.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py
index 3a28bc54..d0a34007 100755
--- a/indoteknik_custom/models/sale_order.py
+++ b/indoteknik_custom/models/sale_order.py
@@ -111,6 +111,7 @@ class SaleOrder(models.Model):
date_driver_departure = fields.Datetime(string='Departure Date', compute='_compute_date_kirim', copy=False)
note_website = fields.Char(string="Note Website")
use_button = fields.Boolean(string='Using Calculate Selling Price', copy=False)
+ unreserve_id = fields.Many2one('stock.picking', 'Unreserve Picking')
voucher_shipping_id = fields.Many2one(comodel_name='voucher', string='Voucher Shipping', copy=False)
margin_after_delivery_purchase = fields.Float(string='Margin After Delivery Purchase', compute='_compute_margin_after_delivery_purchase')
percent_margin_after_delivery_purchase = fields.Float(string='% Margin After Delivery Purchase', compute='_compute_margin_after_delivery_purchase')
@@ -131,6 +132,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: