summaryrefslogtreecommitdiff
path: root/fixco_custom/models/detail_order.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/detail_order.py
parent87f6bc09d6fe91526116301375efc544f31be625 (diff)
schema SO Blibli
Diffstat (limited to 'fixco_custom/models/detail_order.py')
-rwxr-xr-xfixco_custom/models/detail_order.py62
1 files changed, 51 insertions, 11 deletions
diff --git a/fixco_custom/models/detail_order.py b/fixco_custom/models/detail_order.py
index b0940a8..f0c0760 100755
--- a/fixco_custom/models/detail_order.py
+++ b/fixco_custom/models/detail_order.py
@@ -38,6 +38,8 @@ class DetailOrder(models.Model):
picking_id = fields.Many2one('stock.picking', 'Picking')
invoice_id = fields.Many2one('account.move', 'Invoice')
message_error = fields.Text('Message Error')
+ is_grouped_order = fields.Boolean('Is Grouped Order', default=False)
+ original_order_ids = fields.Char('Original Order IDs')
# get detail order section
@@ -161,37 +163,74 @@ class DetailOrder(models.Model):
'invoice_mp': json_data.get('data', {})[0].get('externalOrderId'),
}
return data
+
+ def _combine_order_items(self, items):
+ """Combine quantities of the same products from multiple orders"""
+ product_quantities = {}
+ for item in items:
+ key = item.get('masterSku')
+ if key in product_quantities:
+ product_quantities[key]['quantity'] += item.get('quantity', 0)
+ product_quantities[key]['actualPrice'] += item.get('actualPrice', 0)
+ else:
+ product_quantities[key] = {
+ 'quantity': item.get('quantity', 0),
+ 'actualPrice': item.get('actualPrice', 0),
+ 'productName': item.get('productName'),
+ 'masterSkuType': item.get('masterSkuType'),
+ 'item_data': item # Keep original item data
+ }
+ return product_quantities
def prepare_data_so_line(self, json_data):
order_lines = []
- items = json_data.get('data', [{}])[0].get('items', [])
product_not_found = False
- for item in items:
+ # Get all items (already combined if grouped)
+ items = json_data.get('data', [{}])[0].get('items', [])
+
+ # Combine quantities of the same products
+ product_quantities = self._combine_order_items(items)
+
+ # Process the combined items
+ for sku, combined_item in product_quantities.items():
+ item = combined_item['item_data']
product = self.env['product.product'].search(
- [('default_code', '=', item.get('masterSku'))],
+ [('default_code', '=', sku)],
limit=1
)
if product and item.get('masterSkuType') == 'BUNDLE':
order_lines.append((0, 0, {
'display_type': 'line_note',
- 'name': f"Bundle: {item.get('productName')}, Qty: {item.get('quantity')}, Master SKU: {item.get('masterSku')}",
+ 'name': f"Bundle: {item.get('productName')}, Qty: {combined_item['quantity']}, Master SKU: {sku}",
'product_uom_qty': 0,
'price_unit': 0,
}))
bundling_lines = self.env['bundling.line'].search([('product_id', '=', product.id)])
bundling_variant_ids = bundling_lines.mapped('variant_id').ids
- sale_pricelist = self.env['product.pricelist.item'].search([('product_id', 'in', bundling_variant_ids), ('pricelist_id', '=', 17)])
+ sale_pricelist = self.env['product.pricelist.item'].search([
+ ('product_id', 'in', bundling_variant_ids),
+ ('pricelist_id', '=', 17)
+ ])
price_bundling_bottom = sum(item.fixed_price for item in sale_pricelist)
+
for bline in bundling_lines:
- bottom_price = self.env['product.pricelist.item'].search([('product_id', '=', bline.variant_id.id), ('pricelist_id', '=', 17)], limit=1)
+ bottom_price = self.env['product.pricelist.item'].search([
+ ('product_id', '=', bline.variant_id.id),
+ ('pricelist_id', '=', 17)
+ ], limit=1)
price = bottom_price.fixed_price
- price_unit = self.prorate_price_bundling(bline.variant_id,price_bundling_bottom,price,actual_price=item.get('actualPrice'))
+ price_unit = self.prorate_price_bundling(
+ bline.variant_id,
+ price_bundling_bottom,
+ price,
+ actual_price=combined_item['actualPrice']/combined_item['quantity'] # Use average price
+ )
order_lines.append((0, 0, {
'product_id': bline.variant_id.id if bline.variant_id else product.id,
- 'product_uom_qty': bline.product_uom_qty * item.get('quantity') if bline.product_uom_qty else item.get('quantity'),
+ 'product_uom_qty': bline.product_uom_qty * combined_item['quantity'],
'price_unit': price_unit,
'name': f"{bline.variant_id.display_name} (Bundle Component)" if bline.variant_id.display_name else product.name,
}))
@@ -204,14 +243,15 @@ class DetailOrder(models.Model):
}))
continue
+ # Regular product line
line_data = {
'product_id': product.id if product else 5792,
- 'product_uom_qty': item.get('quantity'),
- 'price_unit': item.get('actualPrice'),
+ 'product_uom_qty': combined_item['quantity'],
+ 'price_unit': combined_item['actualPrice'] / combined_item['quantity'], # Average price
}
if not product:
- line_data['name'] = f"{item.get('masterSku')} ({item.get('productName')})"
+ line_data['name'] = f"{sku} ({combined_item['productName']})"
product_not_found = True
order_lines.append((0, 0, line_data))