in agora/cerebral_simulator/src/store_simulator.py [0:0]
def generate_order(self, current_time, store, products):
"""
Generate a single order with multiple items
Args:
current_time: DateTime for the order
store: Store dictionary containing store details
products: List of available products
Returns:
list: List of order items or None if error occurs
"""
try:
current_time_str = str(current_time)
order_id = current_time.strftime('%Y%m%d%H%M%S-') + '{:03d}'.format(random.randint(1, 999))
order_items = []
# Generate random profit margins for this order
profit_choices = random.sample(range(-20, 30), 5)
# Select random products for this order (1-5 products)
selected_products = random.sample(products, random.randint(1, min(5, len(products))))
for product in selected_products:
# Generate random quantity for this product
quantity_sold = random.randint(1, 10)
inventory_key = (store["store_id"], str(product.product_id))
# Create or get inventory for this product
if inventory_key not in self.current_inventory:
product_price = round(random.uniform(product.price_range.min, product.price_range.max), 2)
self.current_inventory[inventory_key] = ProductInventory(
date_time=current_time,
store_id=store["store_id"],
product_id=str(product.product_id),
product_name=product.name,
retail_price=product_price,
in_stock=product.stock,
reorder_threshold=int(product.stock * 0.2),
last_restocked=current_time
)
product_inventory = self.current_inventory[inventory_key]
# Update inventory levels
if product_inventory.in_stock > quantity_sold:
product_inventory.in_stock -= quantity_sold
else:
# Restock if not enough inventory
product_inventory.in_stock = product.stock - quantity_sold
product_inventory.last_restocked = current_time
product_inventory.date_time = current_time
self.current_inventory[inventory_key] = product_inventory
# Calculate order item details
discount = random.choice(self.product_discount) / 100
line_item_total_price = round(product_inventory.retail_price * quantity_sold * (1 - discount), 2)
profit = round(line_item_total_price * random.choice(profit_choices) / 100, 2)
# Create order item
order_item = {
'sale_id': order_id,
'sale_date': current_time_str,
'store_id': store["store_id"],
'store_city': store["city"],
'product_id': str(product.product_id),
'product_category': product.category,
'product_name': product.name,
'price': product_inventory.retail_price,
'discount': discount,
'quantity': quantity_sold,
'item_total': line_item_total_price,
'profit': profit,
'payment_method': random.choice(self.payment_methods),
'customer_id': f'C-{random.randint(1,1000):03d}',
'register_id': random.choice(self.store_registers)
}
order_items.append(order_item)
return order_items
except Exception as e:
self.logger.error(f"Error generating order: {str(e)}")
return None