summaryrefslogtreecommitdiff
path: root/fixco_custom/models/upload_ginee.py
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2025-06-28 08:54:35 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2025-06-28 08:54:35 +0700
commitbe3af87277be6d884abbcc9f584f52a3871a6869 (patch)
treeccca579890fd93518eeb9b8ad646de0363172139 /fixco_custom/models/upload_ginee.py
parent87f6bc09d6fe91526116301375efc544f31be625 (diff)
schema SO Blibli
Diffstat (limited to 'fixco_custom/models/upload_ginee.py')
-rw-r--r--fixco_custom/models/upload_ginee.py86
1 files changed, 80 insertions, 6 deletions
diff --git a/fixco_custom/models/upload_ginee.py b/fixco_custom/models/upload_ginee.py
index c19fa03..f69e7d2 100644
--- a/fixco_custom/models/upload_ginee.py
+++ b/fixco_custom/models/upload_ginee.py
@@ -178,6 +178,48 @@ class UploadGineeLine(models.Model):
order_id = fields.Char('Order ID')
message_error = fields.Text('Error Message')
detail_order_id = fields.Many2one('detail.order', string='Detail Order')
+ is_grouped = fields.Boolean('Is Grouped', default=False)
+ group_key = fields.Char('Group Key')
+
+ def _process_grouped_blibli_orders(self, lines):
+ """Process a group of BLIBLI orders with the same invoice prefix"""
+ order_ids = [line.order_id for line in lines if line.order_id]
+
+ if not order_ids:
+ raise UserError(_('Order ID is empty for one or more records in group!'))
+
+ # Check if any of these orders already exist
+ existing_detail = self.env['detail.order'].search([
+ ('detail_order', 'ilike', order_ids[0])
+ ], limit=1)
+
+ if existing_detail:
+ return existing_detail
+
+ # Call API with all order IDs in the group
+ data = lines[0]._call_api(BATCH_GET_URI, {"orderIds": order_ids})
+
+ # Combine items from all orders in the group
+ combined_items = []
+ for order_data in data.get('data', []):
+ combined_items.extend(order_data.get('items', []))
+
+ # Create a modified json_data structure that includes all items
+ combined_json_data = {
+ 'data': [{
+ **data.get('data', [{}])[0], # Keep all original fields from first order
+ 'items': combined_items, # Combined items from all orders
+ 'shopId': data.get('data', [{}])[0].get('shopId'),
+ 'externalOrderId': ', '.join([line.invoice_marketplace for line in lines]),
+ 'orderId': ', '.join(order_ids), # Mark as grouped
+ }]
+ }
+
+ detail_order = self.env['detail.order'].create({
+ 'detail_order': json.dumps(combined_json_data, indent=4),
+ 'source': 'manual',
+ })
+ return detail_order
def _sign_request(self, uri):
"""Membuat tanda tangan sesuai format yang berhasil"""
@@ -210,32 +252,64 @@ class UploadGineeLine(models.Model):
return response.json()
def create_so_and_detail_order(self):
+ # First group BLIBLI orders by their invoice prefix
+ grouped_lines = {}
for rec in self:
+ if rec.upload_ginee_id.upload_type == 'blibli' and '-' in rec.invoice_marketplace:
+ prefix = rec.invoice_marketplace.split('-')[0]
+ if prefix not in grouped_lines:
+ grouped_lines[prefix] = []
+ grouped_lines[prefix].append(rec)
+ else:
+ # For non-BLIBLI or BLIBLI without dash, process individually
+ grouped_lines[rec.id] = [rec]
+
+ # Process each group
+ for group_key, lines in grouped_lines.items():
try:
- if not rec.order_id:
+ if len(lines) > 1:
+ # Process grouped BLIBLI orders
+ detail_order = self._process_grouped_blibli_orders(lines)
+
+ # Update all lines in the group
+ for line in lines:
+ line.update({
+ 'message_error': 'Success (grouped)',
+ 'detail_order_id': detail_order.id
+ })
+ detail_order.execute_queue_detail()
+ else:
+ # Process single line (non-grouped)
+ line = lines[0]
+ if not line.order_id:
raise UserError(_('Order ID is empty!'))
- if self.env['detail.order'].search([('detail_order', 'ilike', rec.order_id)]):
+ if self.env['detail.order'].search([('detail_order', 'ilike', line.order_id)]):
raise UserError(_(
- "Order ID %s already exists in Detail Orders") % rec.order_id)
+ "Order ID %s already exists in Detail Orders") % line.order_id)
- data = self._call_api(BATCH_GET_URI, {"orderIds": [rec.order_id]})
+ data = line._call_api(BATCH_GET_URI, {"orderIds": [line.order_id]})
detail_order = self.env['detail.order'].create({
'detail_order': json.dumps(data, indent=4),
'source': 'manual',
})
detail_order.execute_queue_detail()
- rec.update({
+ line.update({
'message_error': 'Success',
'detail_order_id': detail_order.id
})
+
except Exception as e:
- rec.message_error = str(e)
+ # Update all lines in group with error if any
+ for line in lines:
+ line.message_error = str(e)
def get_order_id(self):
for rec in self:
try:
+ if rec.order_id:
+ continue
if self.search_count([
('marketplace', '=', rec.marketplace),
('invoice_marketplace', '=', rec.invoice_marketplace),