diff options
| author | Mqdd <ahmadmiqdad27@gmail.com> | 2026-01-02 20:48:52 +0700 |
|---|---|---|
| committer | Mqdd <ahmadmiqdad27@gmail.com> | 2026-01-02 20:48:52 +0700 |
| commit | 486c306380f7e8dbb57f58013c69fdce6d608e1e (patch) | |
| tree | cd5e601f8c065919bdbbd2ab53e12d040fa3db1f /fixco_custom/models/shipment_group.py | |
| parent | a67b6e3dec66e90c59db6ffac21f6a831db938ef (diff) | |
| parent | 7fdf8b3eb8cf42c7223039267faa4d22ba0ba334 (diff) | |
Merge branch 'main' of https://bitbucket.org/altafixco/fixco-addons
merge
Diffstat (limited to 'fixco_custom/models/shipment_group.py')
| -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 d83130d..2e2dffa 100644 --- a/fixco_custom/models/shipment_group.py +++ b/fixco_custom/models/shipment_group.py @@ -163,47 +163,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]) + '$' |
