HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/posscale/public_html/printmanager/app/Filament/Widgets/OrdersLast12MonthsChart.php
<?php

namespace App\Filament\Widgets;

use App\Models\Order;
use Carbon\Carbon;
use Filament\Widgets\ChartWidget;

class OrdersLast12MonthsChart extends ChartWidget
{
    protected static ?string $heading = 'Orders - Last 12 Months';

    protected static ?int $sort = 6;

    


    protected function getData(): array
    {
        $end = Carbon::now()->endOfMonth();
        $start = (clone $end)->subMonths(11)->startOfMonth();

        $orders = Order::query()
            ->selectRaw('DATE_FORMAT(order_date, "%Y-%m") as ym, COUNT(*) as total')
            ->whereBetween('order_date', [$start, $end])
            ->groupByRaw('DATE_FORMAT(order_date, "%Y-%m")')
            ->orderByRaw('DATE_FORMAT(order_date, "%Y-%m")')
            ->pluck('total', 'ym');

        $labels = [];
        $data = [];

        $period = new \DatePeriod(
            $start,
            new \DateInterval('P1M'),
            (clone $end)->addMonth()->startOfMonth(),
        );

        /** @var Carbon $date */
        foreach ($period as $date) {
            $key = $date->format('Y-m');

            $labels[] = $date->format('M Y');
            $data[] = (int) ($orders[$key] ?? 0);
        }

        return [
            'datasets' => [
                [
                    'label' => 'Orders',
                    'data' => $data,
                    'borderColor' => '#000', // Tailwind blue-500
                    'backgroundColor' => 'rgba(0, 0, 0, 0.2)',
                    'fill' => true,
                    'tension' => 0.4,
                ],
            ],
            'labels' => $labels,
        ];
    }

    protected function getType(): string
    {
        return 'line';
    }


     /**
     * @return int | string | array<string, int | null>
     */
    public function getColumnSpan(): int | string | array
    {
        return 3;
    }
}