{{-- Nội dung báo cáo thống kê doanh thu (/statistics) - được nạp lại bằng AJAX mỗi lần lọc (#gridForm). Bố cục: 4 thẻ KPI -> nguồn tiền -> biểu đồ theo ngày -> lưới biểu đồ cơ cấu (doughnut) -> thực thu theo tháng. Biểu đồ dùng Chart.js (public/js/chart.min.js, nạp 1 lần ở statistics/index.blade.php). --}} @php // ===== Số liệu tổng hợp (giữ nguyên công thức như giao diện cũ) ===== $totalOrders = $data['orders'] + $wallets['orders']; $comboOrders = $data['orders'] - $products['orders'] - $services['orders']; $totalSales = $data['all_total'] + $wallets['revenue']; $actualIncome = $data['payment'] + $wallets['payment'] - $wallets['used']; $revenueOnly = $data['payment'] - $wallets['used'] - $data['is_debt']; $remainingDebt = $data['all_total'] - $data['gross_revenue']; $arrivalRate = !empty($schedules['all_schedules']) ? round($schedules['become'] / $schedules['all_schedules'] * 100, 1) : 0; $paymentSources = [ ['label' => 'Tiền mặt', 'value' => (int) $list_payment['money'], 'icon' => 'fas fa-money-bill-wave'], ['label' => 'Thẻ', 'value' => (int) $list_payment['card'], 'icon' => 'far fa-credit-card'], ['label' => 'Chuyển khoản', 'value' => (int) $list_payment['CK'], 'icon' => 'fas fa-university'], ['label' => 'Tiêu từ ví', 'value' => (int) $wallets['used'], 'icon' => 'fas fa-wallet'], ]; $paymentMax = max(1, max(array_column($paymentSources, 'value'))); // ===== Dữ liệu biểu đồ (label/value) truyền sang JS bằng @json để không vỡ chuỗi có dấu nháy ===== $pairs = function ($rows, $labelKey, $valueKey) { $out = ['labels' => [], 'values' => []]; foreach ($rows as $row) { // data_get đọc được cả array, stdClass lẫn Eloquent Model ((array) $model trả về thuộc tính protected, không phải cột) $out['labels'][] = (string) data_get($row, $labelKey, ''); $out['values'][] = (float) data_get($row, $valueKey, 0); } return $out; }; $charts = [ 'source' => array_merge(['title' => 'Doanh thu theo nguồn'], $pairs($statusRevenues, 'name', 'revenue')), 'status' => array_merge(['title' => 'Khách hàng mới theo trạng thái', 'unit' => 'KH'], $pairs($data['statusCustomer'], 'name', 'total')), 'orderType'=> ['title' => 'Doanh thu theo loại đơn', 'labels' => ['Sản phẩm', 'Dịch vụ', 'Sản phẩm & Dịch vụ'], 'values' => [(float) $products['gross_revenue'], (float) $services['gross_revenue'], (float) $services['combo_gross']]], 'gender' => array_merge(['title' => 'Doanh thu theo giới tính'], $pairs($revenue_gender ?: [], 'name', 'all_total')), 'customer' => ['title' => 'Thực thu theo loại khách hàng', 'labels' => ['Khách hàng mới', 'Khách hàng cũ'], 'values' => [(float) $revenue['revenueNew'], (float) $revenue['revenueOld']]], ]; if (count($data['category_product'])) { $charts['product'] = array_merge(['title' => 'Top 5 sản phẩm bán chạy', 'unit' => 'SP'], $pairs($data['category_product'], 'name', 'total')); } if (count($revenue_locale)) { $charts['locale'] = array_merge(['title' => 'Top 5 tỉnh doanh thu cao nhất'], $pairs($revenue_locale, 'name', 'total')); } $daily = ['labels' => [], 'sales' => [], 'income' => []]; foreach ($data['revenue_month'] as $item) { $daily['labels'][] = substr($item->payment_date, -2); $daily['sales'][] = (float) ($item->order_month + $item->wallet_month); $daily['income'][] = (float) ($item->payment_revenue + $item->payment_wallet_month); } $yearly = ['labels' => [], 'values' => []]; foreach ($revenue_year as $item) { $yearly['labels'][] = 'Tháng ' . $item->month; $yearly['values'][] = (float) $item->all_total; } $chartIcons = ['source' => 'fas fa-bullhorn', 'status' => 'fas fa-user-tag', 'orderType' => 'fas fa-boxes', 'product' => 'fas fa-star', 'locale' => 'fas fa-map-marker-alt', 'gender' => 'fas fa-venus-mars', 'customer' => 'fas fa-user-friends']; @endphp