getDashboardStats($month); // Sales stats — query td_order and td_invoice directly $month_start = $month . '-01'; $month_end = date('Y-m-t', strtotime($month_start)); // Total orders this month (excluding cancelled) $sth = $pdo2->prepare( "SELECT COUNT(*), COALESCE(SUM(grand_total), 0) FROM td_order WHERE company_id = :company_id AND status != -1 AND order_date BETWEEN :start AND :end" ); $sth->execute([':company_id' => $company_id, ':start' => $month_start, ':end' => $month_end]); [$total_orders, $total_revenue] = $sth->fetch(PDO::FETCH_NUM); // Unpaid invoices (status=0 draft + status=1 issued + status=3 overdue) $sth = $pdo2->prepare( "SELECT COUNT(*) FROM td_invoice WHERE company_id = :company_id AND doc_type = 'invoice' AND status IN (0, 1, 3)" ); $sth->execute([':company_id' => $company_id]); $unpaid_invoices = (int)$sth->fetchColumn(); // Pending returns (status=0 draft) $sth = $pdo2->prepare( "SELECT COUNT(*) FROM td_return WHERE company_id = :company_id AND status = 0" ); $sth->execute([':company_id' => $company_id]); $pending_returns = (int)$sth->fetchColumn(); $answer['output'] = [ 'total_in' => $stats['total_in'], 'total_out' => $stats['total_out'], 'active_products' => $stats['active_products'], 'low_stock' => $report->getDashboardLowStockCount(), 'total_orders' => (int)$total_orders, 'total_revenue' => (float)$total_revenue, 'unpaid_invoices' => $unpaid_invoices, 'pending_returns' => $pending_returns, 'stock_movement' => $report->getStockMovementChart(12), 'most_moved' => $report->getMostMovedProducts($month), 'low_stock_items' => $report->getLowStockItems(), 'recent_activity' => $report->getRecentActivity(10), 'months' => $report->getAvailableMonths(), ]; $answer['success'] = 1; exit(json_encode($answer)); ?>