HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: //home/armgroup/libs/app/Charts/OrdersReportsCountChart.php
<?php

declare(strict_types=1);

namespace App\Charts;

use App\Enums\OrderStatus;
use App\Models\Order;
use Carbon\Carbon;
use Chartisan\PHP\Chartisan;
use ConsoleTVs\Charts\BaseChart;
use Illuminate\Http\Request;

class OrdersReportsCountChart extends BaseChart
{
    public ?array $middlewares = ['auth', 'permission:manage reports', 'isActive'];

    private const MONTH = 12;

    /**
     * Handles the HTTP request for the given chart.
     * It must always return an instance of Chartisan
     * and never a string or an array.
     */
    public function handler(Request $request): Chartisan
    {
        $months = $this->getPersianMonths();

        return Chartisan::build()
            ->labels($months)
            ->dataset(OrderStatus::CANCELED()->description, $this->getOrderCountPerPersianMonth($months, OrderStatus::CANCELED, $request))
            ->dataset(OrderStatus::PROCESSING()->description, $this->getOrderCountPerPersianMonth($months, OrderStatus::PROCESSING, $request))
            ->dataset(OrderStatus::SENT()->description, $this->getOrderCountPerPersianMonth($months, OrderStatus::SENT, $request))
            ->dataset(OrderStatus::RETURNED()->description, $this->getOrderCountPerPersianMonth($months, OrderStatus::RETURNED, $request))
            ->dataset(OrderStatus::COMPLETED()->description, $this->getOrderCountPerPersianMonth($months, OrderStatus::COMPLETED, $request));
    }

    private function getPersianMonths(): array
    {
        $months = [];
        for ($i = 0; $i < self::MONTH; $i++) {
            $months[] = jdate(Carbon::now()->subMonths($i))->format('%B');
        }

        return array_reverse($months);
    }

    private function getOrderCountPerPersianMonth(array $months, $status, $request): array
    {
        $data = array_fill(0, self::MONTH, 0);
        foreach ($this->getOrders($status, $request) as $order) {
            $data[array_search(jdate($order->date)->format('%B'), $months)] += $order->count;
        }

        return $data;
    }

    private function getOrders($status, $request)
    {
        $from = $request->from;
        $until = $request->until;

        return Order::when($from, function ($query) use ($from) {
            return $query->whereDate('created_at', '>=', $from);
        })->when($until, function ($query) use ($until) {
            return $query->whereDate('created_at', '<=', $until);
        })->where('status', $status)
            ->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d') as date, COUNT(*) count, status")
            ->groupByRaw('date, status')
            ->latest()
            ->get();
    }
}