From 6d222cdfb56df09e61cd3add3c3fb328bd9adc7b Mon Sep 17 00:00:00 2001 From: "Indoteknik ." Date: Fri, 20 Jun 2025 14:20:57 +0700 Subject: (andri) patch json agar odoo menerima response kosong dari biteship --- indoteknik_api/controllers/api_v1/stock_picking.py | 58 ++++++++++++++-------- indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/patch/__init__.py | 1 + indoteknik_custom/models/patch/http_override.py | 46 +++++++++++++++++ 4 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 indoteknik_custom/models/patch/__init__.py create mode 100644 indoteknik_custom/models/patch/http_override.py diff --git a/indoteknik_api/controllers/api_v1/stock_picking.py b/indoteknik_api/controllers/api_v1/stock_picking.py index c5a4f7ed..0926bd26 100644 --- a/indoteknik_api/controllers/api_v1/stock_picking.py +++ b/indoteknik_api/controllers/api_v1/stock_picking.py @@ -1,10 +1,12 @@ from .. import controller from odoo import http -from odoo.http import request +from odoo.http import request, Response from pytz import timezone from datetime import datetime import json +import logging +_logger = logging.getLogger(__name__) class StockPicking(controller.Controller): prefix = '/api/v1/' @@ -143,26 +145,40 @@ class StockPicking(controller.Controller): 'name': picking_data.name }) - # @http.route(prefix + 'webhook/biteship', type='json', auth='public', methods=['POST'], csrf=False) - # def udpate_status_from_bitehsip(self, **kw): - # try: - # if not request.jsonrequest: - # return "ok" - - # data = request.jsonrequest # Ambil data JSON dari request - # event = data.get('event') - - # # Handle Event Berdasarkan Jenisnya - # if event == "order.status": - # self.process_order_status(data) - # elif event == "order.price": - # self.process_order_price(data) - # elif event == "order.waybill_id": - # self.process_order_waybill(data) - - # return {'success': True, 'message': f'Webhook {event} received'} - # except Exception as e: - # return {'success': False, 'message': str(e)} + + @http.route(prefix + 'webhook/biteship', type='json', auth='public', methods=['POST'], csrf=False) + def update_status_from_biteship(self, **kw): + _logger.info("Biteship Webhook: Request received at controller start (type='json').") + + try: + # Karena type='json', Odoo secara otomatis akan mem-parsing JSON untuk Anda. + # 'data' akan berisi dictionary Python dari payload JSON Biteship. + data = request.jsonrequest + + # Log ini akan menunjukkan payload yang diterima (sudah dalam bentuk dict) + _logger.info(f"Biteship Webhook: Parsed JSON data from request.jsonrequest: {json.dumps(data)}") + + event = data.get('event') + if event: + _logger.info(f"Biteship Webhook: Processing event: {event}") + if event == "order.status": + self.process_order_status(data) + elif event == "order.price": + self.process_order_price(data) + elif event == "order.waybill_id": + self.process_order_waybill(data) + # Tambahkan logika untuk event lain jika ada + else: + _logger.info("Biteship Webhook: No specific event in payload. Likely an installation/verification ping or unknown event type.") + + # Untuk route type='json', Anda cukup mengembalikan dictionary Python. + # Odoo akan secara otomatis mengonversinya menjadi respons JSON yang valid. + return {'status': 'ok'} + + except Exception as e: + _logger.error(f"Biteship Webhook: Unhandled error during processing: {e}", exc_info=True) + # Untuk error, kembalikan dictionary error juga, Odoo akan mengonversinya ke JSON + return {'status': 'error', 'message': str(e)} # @http.route(prefix + 'webhook/biteship', auth='public', methods=['POST'], csrf=False) # def udpate_status_from_bitehsip(self, **kw): diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 605d1016..094ac69e 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -151,3 +151,4 @@ from . import account_payment_register from . import stock_inventory from . import sale_order_delay from . import approval_invoice_date +from . import patch diff --git a/indoteknik_custom/models/patch/__init__.py b/indoteknik_custom/models/patch/__init__.py new file mode 100644 index 00000000..051b6537 --- /dev/null +++ b/indoteknik_custom/models/patch/__init__.py @@ -0,0 +1 @@ +from . import http_override \ No newline at end of file diff --git a/indoteknik_custom/models/patch/http_override.py b/indoteknik_custom/models/patch/http_override.py new file mode 100644 index 00000000..e1978edb --- /dev/null +++ b/indoteknik_custom/models/patch/http_override.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +import odoo.http +import json +import logging +from werkzeug.exceptions import BadRequest +import functools + +_logger = logging.getLogger(__name__) + +class CustomJsonRequest(odoo.http.JsonRequest): + def __init__(self, httprequest): + super(odoo.http.JsonRequest, self).__init__(httprequest) + + self.params = {} + request_data_raw = self.httprequest.get_data().decode(self.httprequest.charset) + + self.jsonrequest = {} + if request_data_raw.strip(): + try: + self.jsonrequest = json.loads(request_data_raw) + except ValueError: + msg = 'Invalid JSON data: %r' % (request_data_raw,) + _logger.info('%s: %s (Handled by CustomJsonRequest)', self.httprequest.path, msg) + raise BadRequest(msg) + else: + _logger.info("CustomJsonRequest: Received empty or whitespace-only JSON body. Treating as empty JSON for webhook.") + + self.params = dict(self.jsonrequest.get("params", {})) + self.context = self.params.pop('context', dict(self.session.context)) + + +_original_get_request = odoo.http.Root.get_request + +@functools.wraps(_original_get_request) +def _get_request_override(self, httprequest): + _logger.info("--- DEBUG: !!! _get_request_override IS CALLED !!! ---") + _logger.info(f"--- DEBUG: Request Mimetype: {httprequest.mimetype}, Path: {httprequest.path} ---") + + if httprequest.mimetype in ("application/json", "application/json-rpc"): + _logger.debug("Odoo HTTP: Using CustomJsonRequest for mimetype: %s", httprequest.mimetype) + return CustomJsonRequest(httprequest) + else: + _logger.debug("Odoo HTTP: Using original get_request for mimetype: %s", httprequest.mimetype) + return _original_get_request(self, httprequest) + +odoo.http.Root.get_request = _get_request_override \ No newline at end of file -- cgit v1.2.3 From 6e8591a6bd28c4faafc08eb9c539fe24bdecf419 Mon Sep 17 00:00:00 2001 From: "Indoteknik ." Date: Fri, 20 Jun 2025 16:06:00 +0700 Subject: (andri) tracking webhook aktif dan menggantikan peran button sebelumnya --- indoteknik_api/controllers/api_v1/stock_picking.py | 33 +++- indoteknik_custom/models/__init__.py | 2 +- indoteknik_custom/models/stock_picking.py | 167 ++++++++++++++------- indoteknik_custom/views/stock_picking.xml | 4 +- 4 files changed, 141 insertions(+), 65 deletions(-) diff --git a/indoteknik_api/controllers/api_v1/stock_picking.py b/indoteknik_api/controllers/api_v1/stock_picking.py index 0926bd26..09d0c585 100644 --- a/indoteknik_api/controllers/api_v1/stock_picking.py +++ b/indoteknik_api/controllers/api_v1/stock_picking.py @@ -184,13 +184,34 @@ class StockPicking(controller.Controller): # def udpate_status_from_bitehsip(self, **kw): # return "ok" + # def process_order_status(self, data): + # picking_model = request.env['stock.picking'].sudo().search([('biteship_id', '=', data.get('order_id'))], + # limit=1) + # if data.get('status') == 'picked': + # picking_model.write({'driver_departure_date': datetime.utcnow()}) + # elif data.get('status') == 'delivered': + # picking_model.write({'driver_arrival_date': datetime.utcnow()}) + def process_order_status(self, data): - picking_model = request.env['stock.picking'].sudo().search([('biteship_id', '=', data.get('order_id'))], - limit=1) - if data.get('status') == 'picked': - picking_model.write({'driver_departure_date': datetime.utcnow()}) - elif data.get('status') == 'delivered': - picking_model.write({'driver_arrival_date': datetime.utcnow()}) + picking = request.env['stock.picking'].sudo().search([ + ('biteship_id', '=', data.get('order_id')) + ], limit=1) + + if not picking: + _logger.warning(f"[Webhook] Tidak ditemukan picking untuk order_id {data.get('order_id')}") + return + + status = data.get('status') + timestamp = data.get('updated_at') or datetime.utcnow().isoformat() + + description = picking._get_biteship_status_description(status, { + "courier": {"company": data.get("courier_company", "")}, + "destination": {"contact_name": picking.partner_id.name or ""} + }) + + picking.log_biteship_event_from_webhook(status, timestamp, description) + + def process_order_price(self, data): picking_model = request.env['stock.picking'].sudo().search([('biteship_id', '=', data.get('order_id'))], diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 094ac69e..3f538e25 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -151,4 +151,4 @@ from . import account_payment_register from . import stock_inventory from . import sale_order_delay from . import approval_invoice_date -from . import patch +# from . import patch diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py index eabef37c..7bb881c2 100644 --- a/indoteknik_custom/models/stock_picking.py +++ b/indoteknik_custom/models/stock_picking.py @@ -1721,15 +1721,15 @@ class StockPicking(models.Model): response = requests.get(_biteship_url + '/trackings/' + self.biteship_tracking_id, headers=headers, json=manifests) result = response.json() - description = { - 'confirmed' : 'Indoteknik telah melakukan permintaan pick-up', - 'allocated' : 'Kurir akan melakukan pick-up pesanan', - 'picking_up' : 'Kurir sedang dalam perjalanan menuju lokasi pick-up', - 'picked' : 'Pesanan sudah di pick-up kurir '+result.get("courier", {}).get("company", ""), - 'on_hold' : 'Pesanan ditahan sementara karena masalah pengiriman', - 'dropping_off' : 'Kurir sudah ditugaskan dan pesanan akan segera diantar ke pembeli', - 'delivered' : f'Pesanan telah sampai dan diterima oleh {result.get("destination", {}).get("contact_name", "")}' - } + # description = { + # 'confirmed' : 'Indoteknik telah melakukan permintaan pick-up', + # 'allocated' : 'Kurir akan melakukan pick-up pesanan', + # 'picking_up' : 'Kurir sedang dalam perjalanan menuju lokasi pick-up', + # 'picked' : 'Pesanan sudah di pick-up kurir '+result.get("courier", {}).get("company", ""), + # 'on_hold' : 'Pesanan ditahan sementara karena masalah pengiriman', + # 'dropping_off' : 'Kurir sudah ditugaskan dan pesanan akan segera diantar ke pembeli', + # 'delivered' : f'Pesanan telah sampai dan diterima oleh {result.get("destination", {}).get("contact_name", "")}' + # } if (result.get('success') == True): history = result.get("history", []) status = result.get("status", "") @@ -1738,7 +1738,7 @@ class StockPicking(models.Model): manifests.append({ "status": re.sub(r'[^a-zA-Z0-9\s]', ' ', entry["status"]).lower().capitalize(), "datetime": self._convert_to_local_time(entry["updated_at"]), - "description": description[entry["status"]], + "description": self._get_biteship_status_description(entry["status"], result), }) return { @@ -1754,53 +1754,108 @@ class StockPicking(models.Model): _logger.error(f"Error fetching Biteship order for picking {self.id}: {str(e)}") return { 'error': str(e) } - def action_sync_biteship_tracking(self): - for picking in self: - if not picking.biteship_id: - raise UserError("Tracking Biteship tidak tersedia.") - - histori = picking.get_manifest_biteship() - updated_fields = {} - seen_logs = set() - - manifests = sorted(histori.get("manifests", []), key=lambda m: m.get("datetime") or "") - - for manifest in manifests: - status = manifest.get("status", "").lower() - dt_str = manifest.get("datetime") - desc = manifest.get("description") - dt = False - - try: - dt = picking._convert_to_utc_datetime(dt_str) - _logger.info(f"[Biteship Sync] Berhasil parse datetime: {dt_str} -> {dt}") - except Exception as e: - _logger.warning(f"[Biteship Sync] Gagal parse datetime: {e}") - continue + # def action_sync_biteship_tracking(self): + # for picking in self: + # if not picking.biteship_id: + # raise UserError("Tracking Biteship tidak tersedia.") + + # histori = picking.get_manifest_biteship() + # updated_fields = {} + # seen_logs = set() + + # manifests = sorted(histori.get("manifests", []), key=lambda m: m.get("datetime") or "") + + # for manifest in manifests: + # status = manifest.get("status", "").lower() + # dt_str = manifest.get("datetime") + # desc = manifest.get("description") + # dt = False + + # try: + # dt = picking._convert_to_utc_datetime(dt_str) + # _logger.info(f"[Biteship Sync] Berhasil parse datetime: {dt_str} -> {dt}") + # except Exception as e: + # _logger.warning(f"[Biteship Sync] Gagal parse datetime: {e}") + # continue + + # # Update tanggal ke field (pastikan naive datetime UTC) + # if status == "picked" and dt and not picking.driver_departure_date: + # updated_fields["driver_departure_date"] = fields.Datetime.to_string(dt) + + # if status == "delivered" and dt and not picking.driver_arrival_date: + # updated_fields["driver_arrival_date"] = fields.Datetime.to_string(dt) + + # # Buat log unik dengan waktu lokal Asia/Jakarta + # if dt and desc: + # try: + # dt_local = parser.parse(dt_str).replace(tzinfo=None) + # except Exception as e: + # _logger.warning(f"[Biteship Sync] Gagal parse dt_str untuk log: {e}") + # dt_local = dt # fallback + + # desc_clean = ' '.join(desc.strip().split()) + # log_line = f"[TRACKING] {status} - {dt_local.strftime('%d %b %Y %H:%M')}: {desc_clean}" + # if not picking._has_existing_log(log_line): + # picking.message_post(body=log_line) + # seen_logs.add(log_line) + + # if updated_fields: + # picking.write(updated_fields) + + def _get_biteship_status_description(self, status, data=None): + + data = data or {} + + courier = data.get("courier", {}).get("company", "") + contact_name = data.get("destination", {}).get("contact_name", "") + + description_map = { + 'confirmed': 'Indoteknik telah melakukan permintaan pick-up', + 'allocated': 'Kurir akan melakukan pick-up pesanan', + 'picking_up': 'Kurir sedang dalam perjalanan menuju lokasi pick-up', + 'picked': f'Pesanan sudah di pick-up kurir {courier}', + 'on_hold': 'Pesanan ditahan sementara karena masalah pengiriman', + 'dropping_off': 'Kurir sudah ditugaskan dan pesanan akan segera diantar ke pembeli', + 'delivered': f'Pesanan telah sampai dan diterima oleh {contact_name}', + 'cancelled': 'Pesanan dibatalkan oleh sistem atau pengguna', + } + + return description_map.get(status, f"Status '{status}' diterima dari Biteship") + + + def log_biteship_event_from_webhook(self, status, timestamp, description): + self.ensure_one() + updated_fields = {} + + try: + dt = self._convert_to_utc_datetime(timestamp) + except Exception as e: + _logger.warning(f"[Webhook] Gagal konversi waktu: {e}") + dt = datetime.utcnow() + + if status == "picked" and not self.driver_departure_date: + updated_fields["driver_departure_date"] = fields.Datetime.to_string(dt) + if status == "delivered" and not self.driver_arrival_date: + updated_fields["driver_arrival_date"] = fields.Datetime.to_string(dt) + + # Update shipping_status + shipping_status = self._map_status_biteship(status) + if shipping_status and self.shipping_status != shipping_status: + updated_fields["shipping_status"] = shipping_status + + # Log ke chatter + try: + dt_local = parser.parse(timestamp).replace(tzinfo=None) + except Exception: + dt_local = dt + + log_line = f"[TRACKING] {status} - {dt_local.strftime('%d %b %Y %H:%M')}: {description.strip()}" + if not self._has_existing_log(log_line): + self.message_post(body=log_line) + + if updated_fields: + self.write(updated_fields) - # Update tanggal ke field (pastikan naive datetime UTC) - if status == "picked" and dt and not picking.driver_departure_date: - updated_fields["driver_departure_date"] = fields.Datetime.to_string(dt) - - if status == "delivered" and dt and not picking.driver_arrival_date: - updated_fields["driver_arrival_date"] = fields.Datetime.to_string(dt) - - # Buat log unik dengan waktu lokal Asia/Jakarta - if dt and desc: - try: - dt_local = parser.parse(dt_str).replace(tzinfo=None) - except Exception as e: - _logger.warning(f"[Biteship Sync] Gagal parse dt_str untuk log: {e}") - dt_local = dt # fallback - - desc_clean = ' '.join(desc.strip().split()) - log_line = f"[TRACKING] {status} - {dt_local.strftime('%d %b %Y %H:%M')}: {desc_clean}" - if not picking._has_existing_log(log_line): - picking.message_post(body=log_line) - seen_logs.add(log_line) - - if updated_fields: - picking.write(updated_fields) def _has_existing_log(self, log_line): self.ensure_one() diff --git a/indoteknik_custom/views/stock_picking.xml b/indoteknik_custom/views/stock_picking.xml index c088e00c..22e6cf60 100644 --- a/indoteknik_custom/views/stock_picking.xml +++ b/indoteknik_custom/views/stock_picking.xml @@ -64,12 +64,12 @@ string="Biteship" type="object" /> -