{{-- 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
{{-- ===== KPI chính ===== --}}
Khách hàng mới
{{ number_format($data['customers']) }}
  • Lịch hẹn{{ number_format($schedules['all_schedules']) }}
  • Khách đến{{ number_format($schedules['become']) }}
  • Tỷ lệ đến{{ $arrivalRate }}%
Tổng số đơn hàng
{{ number_format($totalOrders) }}
  • Buổi lẻ{{ number_format($data['order_single']) }}
  • Liệu trình{{ number_format($data['order_multiple']) }}
  • Sản phẩm{{ number_format($products['orders']) }}
  • Dịch vụ{{ number_format($services['orders']) }}
  • Combo{{ number_format($comboOrders) }}
  • Nạp ví{{ number_format($wallets['orders']) }}
Tổng doanh số
{{ number_format($totalSales) }}VNĐ
  • Sản phẩm{{ number_format($products['all_total']) }}
  • Dịch vụ{{ number_format($services['all_total']) }}
  • S.phẩm & D.vụ{{ number_format($services['combo_total']) }}
  • Nạp ví{{ number_format($wallets['revenue']) }}
Thực thu
{{ number_format($actualIncome) }}VNĐ
  • Doanh thu{{ number_format($revenueOnly) }}
  • Thu nợ{{ number_format($data['is_debt']) }}
  • Nạp ví{{ number_format($wallets['payment']) }}
  • Còn nợ{{ number_format($remainingDebt) }}
{{-- ===== Nguồn tiền từ đơn hàng ===== --}}

Nguồn tiền từ đơn hàng

{{ number_format($data['payment']) }} VNĐ
@foreach($paymentSources as $src)
{{ $src['label'] }}{{ number_format($src['value']) }}
@endforeach
{{-- ===== Doanh số & doanh thu theo ngày ===== --}}

Doanh số & doanh thu theo ngày

{{-- ===== Cơ cấu (doughnut) ===== --}}
@foreach($charts as $key => $chart)

{{ $chart['title'] }}

@if(array_sum($chart['values']) > 0)
@else
Chưa có dữ liệu
@endif
@endforeach
{{-- ===== Thực thu theo tháng trong năm ===== --}}

Thực thu trong năm hiện tại