summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xfixco_custom/models/detail_order.py119
1 files changed, 43 insertions, 76 deletions
diff --git a/fixco_custom/models/detail_order.py b/fixco_custom/models/detail_order.py
index 8da1c13..188424a 100755
--- a/fixco_custom/models/detail_order.py
+++ b/fixco_custom/models/detail_order.py
@@ -29,7 +29,10 @@ class DetailOrder(models.Model):
('failed', 'Failed'),
('already_so', 'SO Already Created'),
('processing_get_so', 'Processing Get SO'),
+ ('cancelled_so', 'Cancelled SO'),
+ ('cancelled_so_picking', 'Cancelled Picking'),
], 'Execute Status')
+
source = fields.Selection([
('webhook', 'From Webhook'),
('manual', 'Manual'),
@@ -278,8 +281,46 @@ class DetailOrder(models.Model):
self.sale_id = existing_order.id
self.execute_status = 'already_so'
return # Exit early since we don't need to create anything
+
+ # Handle cancelled orders
+ if order_status == 'CANCELLED':
+ external_order_id = json_data.get('data', [{}])[0].get('externalOrderId')
+ order_id = json_data.get('data', [{}])[0].get('orderId')
- # Only proceed with creation if order doesn't exist and status is appropriate
+ # Try to find existing SO
+ existing_order = self.env['sale.order'].search([
+ '|',
+ ('invoice_mp', '=', external_order_id),
+ ('client_order_ref', '=', order_id)
+ ], limit=1)
+
+ if existing_order:
+ if existing_order.state == 'sale':
+ # Cancel all pickings linked to this order
+ for picking in existing_order.picking_ids:
+ if picking.state not in ['cancel', 'done']:
+ picking.action_cancel()
+ self.sale_id = existing_order.id
+ self.execute_status = 'cancelled_so_picking'
+ else:
+ existing_order.action_cancel()
+ self.sale_id = existing_order.id
+ self.execute_status = 'cancelled_so'
+ else:
+ # If no existing SO, create one, then cancel
+ data = self.prepare_data_so(json_data)
+ order_lines, product_not_found = self.prepare_data_so_line(json_data)
+ data['order_line'] = order_lines
+ sale_order = self.env['sale.order'].create(data)
+
+ self.sale_id = sale_order.id
+ sale_order.order_reference = order_id
+ sale_order.address = json_data.get('data', [{}])[0].get('shippingAddressInfo', []).get('fullAddress', [])
+ sale_order.note_by_buyer = json_data.get('data', [{}])[0].get('extraInfo', []).get('noteByBuyer', [])
+
+ sale_order.action_cancel()
+ self.execute_status = 'cancelled_so_created'
+
if order_status != 'PENDING_PAYMENT':
if order_status in ('PARTIALLY_PAID', 'PAID'):
data['order_line'] = order_lines
@@ -341,78 +382,4 @@ class DetailOrder(models.Model):
percent = price_bottom / sum_bottom
real_price = percent * actual_price
- return real_price
-
-
- # check print do section
-
-
- # def get_order_id_check_print(self):
- # try:
- # if self.detail_order:
- # json_data = json.loads(self.detail_order)
- # order_id = json_data.get('data', {})[0].get('orderId')
- # if not order_id:
- # raise UserError(_("Order ID not found in JSON data"))
- # return order_id
- # raise UserError(_("No JSON data available"))
- # except json.JSONDecodeError:
- # raise UserError(_("Invalid JSON format in check_print field"))
- # except Exception as e:
- # raise UserError(_("Error extracting order ID: %s") % str(e))
-
- # def process_queue_item_check_print(self, limit=100):
- # domain = [('picking_id.state', 'not in', ['done', 'cancel'])]
- # records = self.search(domain, order='create_date asc', limit=limit)
- # for rec in records:
- # rec.execute_queue_check_print()
-
- # def execute_queue_check_print(self):
- # try:
- # order_id = self.get_order_id_check_print()
-
- # authorization = self.sign_request()
- # headers = {
- # 'Content-Type': 'application/json',
- # 'X-Advai-Country': 'ID',
- # 'Authorization': authorization
- # }
-
- # payload = {
- # "orderIds": [order_id]
- # }
-
- # url = "https://api.ginee.com/openapi/order/v1/batch-get"
-
- # response = requests.post(
- # url,
- # headers=headers,
- # data=json.dumps(payload)
- # )
-
- # if response.status_code == 200:
- # data = response.json()
- # self.detail_order = json.dumps(data)
- # detail_order = json.loads(self.detail_order)
- # if detail_order['data'][0]['printInfo']['labelPrintStatus'] == 'PRINTED': #ubah ke printed lagi nanti
- # self.picking_id.sync_qty_reserved_qty_done()
- # # self.sale_id.api_create_invoices(self.sale_id.id)
- # self.execute_status = 'done'
- # else:
- # self.write({
- # 'execute_status': 'failed',
- # 'json_ginee': json.dumps({
- # 'error': f"Request failed with status code {response.status_code}",
- # 'response': response.text
- # })
- # })
-
- # except Exception as e:
- # self.write({
- # 'execute_status': 'failed',
- # 'json_ginee': json.dumps({
- # 'error': str(e)
- # })
- # })
-
- \ No newline at end of file
+ return real_price \ No newline at end of file