info('Populating analytics data from existing orders...'); // Get all orders and create revenue tracking entries $orders = Order::where('status', '!=', 'cancelled')->get(); foreach ($orders as $order) { // Parse cart data if it exists $cartData = json_decode($order->cart_data, true) ?? []; if (!empty($cartData)) { foreach ($cartData as $item) { RevenueTracking::updateOrCreate([ 'order_id' => $order->id, 'product_name' => $item['name'] ?? 'Unknown Product' ], [ 'amount' => $item['price'] ?? 0, 'cost' => ($item['price'] ?? 0) * 0.7, // 70% cost, 30% profit 'profit_margin' => 0.30, 'quantity' => $item['quantity'] ?? 1, 'created_at' => $order->created_at, 'updated_at' => $order->updated_at ]); } } else { // If no cart data, create a single entry for the order RevenueTracking::updateOrCreate([ 'order_id' => $order->id, 'product_name' => 'Order #' . $order->id ], [ 'amount' => $order->total_amount, 'cost' => $order->total_amount * 0.7, 'profit_margin' => 0.30, 'quantity' => 1, 'created_at' => $order->created_at, 'updated_at' => $order->updated_at ]); } } $this->info('Analytics data populated successfully!'); $this->info('Total revenue tracking entries: ' . RevenueTracking::count()); } }