diff options
| -rw-r--r-- | fixco_custom/models/shipment_group.py | 101 |
1 files changed, 61 insertions, 40 deletions
diff --git a/fixco_custom/models/shipment_group.py b/fixco_custom/models/shipment_group.py index b4ed27b..117de0b 100644 --- a/fixco_custom/models/shipment_group.py +++ b/fixco_custom/models/shipment_group.py @@ -120,47 +120,68 @@ class ShipmentGroup(models.Model): }) def get_status(self): - for picking_line in self.picking_lines: - try: - order_id = picking_line.invoice_marketplace - - authorization = self.sign_request() - headers = { - 'Content-Type': 'application/json', - 'X-Advai-Country': 'ID', - 'Authorization': authorization - } - payload = { - "orderNumbers": [order_id], - } - url = "https://api.ginee.com/openapi/order/v2/list-order" - - response = requests.post( - url, - headers=headers, - data=json.dumps(payload) - ) + """ + Batch realtime check status order Ginee + - 1 request untuk banyak order + - Anti 429 + - Siap dipanggil sebelum validate picking + """ + # 1️⃣ Kumpulin invoice marketplace + order_map = {} + for line in self.picking_lines: + if line.invoice_marketplace: + order_map[line.invoice_marketplace] = line + + if not order_map: + return + + # 2️⃣ Prepare request + authorization = self.sign_request() + headers = { + 'Content-Type': 'application/json', + 'X-Advai-Country': 'ID', + 'Authorization': authorization + } + + payload = { + "orderNumbers": list(order_map.keys()) + } + + url = "https://api.ginee.com/openapi/order/v2/list-order" + + response = requests.post( + url, + headers=headers, + data=json.dumps(payload), + timeout=30 + ) + + if response.status_code != 200: + raise UserError(_("API request failed with status code: %s") % response.status_code) + + data = response.json() + if data.get('code') != 'SUCCESS': + raise UserError(_("API Error: %s - %s") % ( + data.get('code', 'UNKNOWN'), + data.get('message', 'No error message') + )) + + # 3️⃣ Mapping response ke picking line + contents = data.get('data', {}).get('content', []) + if not contents: + return + + for item in contents: + order_no = item.get('orderNumber') + order_status = item.get('orderStatus') + + picking_line = order_map.get(order_no) + if not picking_line: + continue + + # Simpan status + picking_line.status = order_status if order_status == 'CANCELLED' else '' - if response.status_code == 200: - data = response.json() - if data.get('code') == 'SUCCESS' and data.get('message') == 'OK': - content = data.get('data', {}).get('content', []) - - if not content: - raise UserError(_("No List Order information found in response")) - - content_info = content[0] - if content_info.get('orderStatus') == 'CANCELLED': - picking_line.status = content_info.get('orderStatus') - else: - picking_line.status = '' - else: - raise UserError(_("API Error: %s - %s") % (data.get('code', 'UNKNOWN'), data.get('message', 'No error message'))) - else: - raise UserError(_("API request failed with status code: %s") % response.status_code) - - except Exception as e: - raise UserError(_("Error: %s") % str(e)) def sign_request(self): signData = '$'.join(['POST', Request_URI]) + '$' |
