From e9f38f540f92d6ef98b0153cbf17ce064932ad60 Mon Sep 17 00:00:00 2001 From: AndriFP Date: Tue, 15 Apr 2025 16:47:04 +0700 Subject: fix: add minimun delivery_amt + alert --- indoteknik_custom/models/sale_order.py | 80 ++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index c83ffd61..36feb70b 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -315,8 +315,12 @@ class SaleOrder(models.Model): "sale_order_id": self.id, }) self.shipping_option_id = shipping_option.id - + def action_estimate_shipping(self): + # Pengecekan Minimum Delivery Amount + if self.delivery_amt < 5000: + raise UserError("Estimasi ongkos kirim belum mencapai jumlah minimum untuk pengiriman") + if self.carrier_id.id in [1, 151]: self.action_indoteknik_estimate_shipping() return @@ -366,10 +370,65 @@ class SaleOrder(models.Model): self.shipping_option_id = self.env["shipping.option"].search([('sale_order_id', '=', self.id)], limit=1).id - self.message_post(body=f"Estimasi Ongkos Kirim: Rp{self.delivery_amt}
Detail Lain:
{'
'.join([f'Service: {s[0]}, Description: {s[1]}, ETD: {s[2]} hari, Cost: Rp {s[3]}' for s in shipping_options])}") + # Jika perlu, tambahkan log atau tindakan lebih lanjut setelah peringatan else: raise UserError("Gagal mendapatkan estimasi ongkir.") + + # def action_estimate_shipping(self): + # if self.carrier_id.id in [1, 151]: + # self.action_indoteknik_estimate_shipping() + # return + + # total_weight = 0 + # missing_weight_products = [] + + # for line in self.order_line: + # if line.weight > 0: + # total_weight += line.weight * line.product_uom_qty + # line.product_id.weight = line.weight + # else: + # missing_weight_products.append(line.product_id.name) + + # if missing_weight_products: + # product_names = '
'.join(missing_weight_products) + # self.message_post(body=f"Produk berikut tidak memiliki berat:
{product_names}") + + # if total_weight == 0: + # raise UserError("Tidak dapat mengestimasi ongkir tanpa berat yang valid.") + + # destination_subsdistrict_id = self.real_shipping_id.kecamatan_id.rajaongkir_id + # if not destination_subsdistrict_id: + # raise UserError("Gagal mendapatkan ID kota tujuan.") + + # result = self._call_rajaongkir_api(total_weight, destination_subsdistrict_id) + # if result: + # shipping_options = [] + # for courier in result['rajaongkir']['results']: + # for cost_detail in courier['costs']: + # service = cost_detail['service'] + # description = cost_detail['description'] + # etd = cost_detail['cost'][0]['etd'] + # value = cost_detail['cost'][0]['value'] + # shipping_options.append((service, description, etd, value, courier['code'])) + + # self.env["shipping.option"].search([('sale_order_id', '=', self.id)]).unlink() + + # for service, description, etd, value, provider in shipping_options: + # self.env["shipping.option"].create({ + # "name": service, + # "price": value, + # "provider": provider, + # "etd": etd, + # "sale_order_id": self.id, + # }) + + # self.shipping_option_id = self.env["shipping.option"].search([('sale_order_id', '=', self.id)], limit=1).id + + # self.message_post(body=f"Estimasi Ongkos Kirim: Rp{self.delivery_amt}
Detail Lain:
{'
'.join([f'Service: {s[0]}, Description: {s[1]}, ETD: {s[2]} hari, Cost: Rp {s[3]}' for s in shipping_options])}") + # else: + # raise UserError("Gagal mendapatkan estimasi ongkir.") + def _call_rajaongkir_api(self, total_weight, destination_subsdistrict_id): url = 'https://pro.rajaongkir.com/api/cost' headers = { @@ -670,13 +729,26 @@ class SaleOrder(models.Model): raise UserError('Email yang anda input kurang valid') # @api.constrains('delivery_amt', 'carrier_id', 'shipping_cost_covered') + def _validate_delivery_amt(self): + if self.delivery_amt < 5000: + raise UserError("Estimasi Ongkos Kirim belum memenuhi jumlah minimum untuk pengiriman.") + if self.delivery_amt < 1: - if(self.carrier_id.id == 1 or self.shipping_cost_covered == 'indoteknik') and not self.env.context.get('active_id', []): - if(self.carrier_id.id == 1): + if (self.carrier_id.id == 1 or self.shipping_cost_covered == 'indoteknik') and not self.env.context.get('active_id', []): + if self.carrier_id.id == 1: raise UserError('Untuk Kurir Indoteknik Delivery, Estimasi Ongkos Kirim Harus di isi') else: raise UserError('Untuk Shipping Covered Indoteknik, Estimasi Ongkos Kirim Harus di isi') + + + # def _validate_delivery_amt(self): + # if self.delivery_amt < 1: + # if(self.carrier_id.id == 1 or self.shipping_cost_covered == 'indoteknik') and not self.env.context.get('active_id', []): + # if(self.carrier_id.id == 1): + # raise UserError('Untuk Kurir Indoteknik Delivery, Estimasi Ongkos Kirim Harus di isi') + # else: + # raise UserError('Untuk Shipping Covered Indoteknik, Estimasi Ongkos Kirim Harus di isi') def override_allow_create_invoice(self): if not self.env.user.is_accounting: -- cgit v1.2.3 From 2dc56ffaf7e2e6d703eac32fd1213cf84b684915 Mon Sep 17 00:00:00 2001 From: AndriFP Date: Wed, 16 Apr 2025 10:25:39 +0700 Subject: (andri)add log note after estimate shipping success --- indoteknik_custom/models/sale_order.py | 129 +++++++++++++-------------------- indoteknik_custom/views/sale_order.xml | 1 + 2 files changed, 52 insertions(+), 78 deletions(-) diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 36feb70b..cc7b9851 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -315,12 +315,22 @@ class SaleOrder(models.Model): "sale_order_id": self.id, }) self.shipping_option_id = shipping_option.id + self.message_post( + body=( + f"Estimasi pengiriman Indoteknik berhasil:
" + f"Layanan: {shipping_option.name}
" + f"ETD: {shipping_option.etd}
" + f"Biaya: Rp {shipping_option.price:,}
" + f"Provider: {shipping_option.provider}" + ), + message_type="comment", + ) def action_estimate_shipping(self): # Pengecekan Minimum Delivery Amount if self.delivery_amt < 5000: raise UserError("Estimasi ongkos kirim belum mencapai jumlah minimum untuk pengiriman") - + if self.carrier_id.id in [1, 151]: self.action_indoteknik_estimate_shipping() return @@ -347,87 +357,50 @@ class SaleOrder(models.Model): raise UserError("Gagal mendapatkan ID kota tujuan.") result = self._call_rajaongkir_api(total_weight, destination_subsdistrict_id) - if result: - shipping_options = [] - for courier in result['rajaongkir']['results']: - for cost_detail in courier['costs']: - service = cost_detail['service'] - description = cost_detail['description'] - etd = cost_detail['cost'][0]['etd'] - value = cost_detail['cost'][0]['value'] - shipping_options.append((service, description, etd, value, courier['code'])) - - self.env["shipping.option"].search([('sale_order_id', '=', self.id)]).unlink() - - for service, description, etd, value, provider in shipping_options: - self.env["shipping.option"].create({ - "name": service, - "price": value, - "provider": provider, - "etd": etd, - "sale_order_id": self.id, - }) - - self.shipping_option_id = self.env["shipping.option"].search([('sale_order_id', '=', self.id)], limit=1).id - # Jika perlu, tambahkan log atau tindakan lebih lanjut setelah peringatan + if not result: + raise UserError("Estimasi Ongkir gagal sebab pilihan kurir tidak terdaftar di RajaOngkir") + + shipping_options = [] + for courier in result['rajaongkir']['results']: + for cost_detail in courier['costs']: + service = cost_detail['service'] + description = cost_detail['description'] + etd = cost_detail['cost'][0]['etd'] + value = cost_detail['cost'][0]['value'] + shipping_options.append((service, description, etd, value, courier['code'])) + + # Hapus opsi shipping lama + self.env["shipping.option"].search([('sale_order_id', '=', self.id)]).unlink() + + # Simpan opsi shipping baru + for service, description, etd, value, provider in shipping_options: + self.env["shipping.option"].create({ + "name": service, + "price": value, + "provider": provider, + "etd": etd, + "sale_order_id": self.id, + }) + + # Set opsi shipping default + first_option = self.env["shipping.option"].search([('sale_order_id', '=', self.id)], limit=1) + if first_option: + self.shipping_option_id = first_option.id + message = ( + f"Estimasi pengiriman berhasil:
" + f"Layanan: {first_option.name}
" + f"Deskripsi: {dict(first_option._fields['name'].selection).get(first_option.name, first_option.name)}
" + f"ETD: {first_option.etd} hari
" + f"Biaya: Rp {first_option.price:,}" + ) + self.message_post( + body=message, + message_type="comment", + ) else: raise UserError("Gagal mendapatkan estimasi ongkir.") - - # def action_estimate_shipping(self): - # if self.carrier_id.id in [1, 151]: - # self.action_indoteknik_estimate_shipping() - # return - - # total_weight = 0 - # missing_weight_products = [] - - # for line in self.order_line: - # if line.weight > 0: - # total_weight += line.weight * line.product_uom_qty - # line.product_id.weight = line.weight - # else: - # missing_weight_products.append(line.product_id.name) - - # if missing_weight_products: - # product_names = '
'.join(missing_weight_products) - # self.message_post(body=f"Produk berikut tidak memiliki berat:
{product_names}") - - # if total_weight == 0: - # raise UserError("Tidak dapat mengestimasi ongkir tanpa berat yang valid.") - - # destination_subsdistrict_id = self.real_shipping_id.kecamatan_id.rajaongkir_id - # if not destination_subsdistrict_id: - # raise UserError("Gagal mendapatkan ID kota tujuan.") - - # result = self._call_rajaongkir_api(total_weight, destination_subsdistrict_id) - # if result: - # shipping_options = [] - # for courier in result['rajaongkir']['results']: - # for cost_detail in courier['costs']: - # service = cost_detail['service'] - # description = cost_detail['description'] - # etd = cost_detail['cost'][0]['etd'] - # value = cost_detail['cost'][0]['value'] - # shipping_options.append((service, description, etd, value, courier['code'])) - - # self.env["shipping.option"].search([('sale_order_id', '=', self.id)]).unlink() - - # for service, description, etd, value, provider in shipping_options: - # self.env["shipping.option"].create({ - # "name": service, - # "price": value, - # "provider": provider, - # "etd": etd, - # "sale_order_id": self.id, - # }) - - # self.shipping_option_id = self.env["shipping.option"].search([('sale_order_id', '=', self.id)], limit=1).id - - # self.message_post(body=f"Estimasi Ongkos Kirim: Rp{self.delivery_amt}
Detail Lain:
{'
'.join([f'Service: {s[0]}, Description: {s[1]}, ETD: {s[2]} hari, Cost: Rp {s[3]}' for s in shipping_options])}") - # else: - # raise UserError("Gagal mendapatkan estimasi ongkir.") def _call_rajaongkir_api(self, total_weight, destination_subsdistrict_id): url = 'https://pro.rajaongkir.com/api/cost' diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml index 2c64181e..f1b72af3 100755 --- a/indoteknik_custom/views/sale_order.xml +++ b/indoteknik_custom/views/sale_order.xml @@ -94,6 +94,7 @@ type="object" />