parfum/app/Livewire/Chart/OutletRevenue.php
2025-11-11 15:35:47 +07:00

42 lines
1.0 KiB
PHP

<?php
namespace App\Livewire\Chart;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class OutletRevenue extends Component
{
public array $outlets = [];
public array $revenues = [];
public array $backgroundColors = [];
public array $borderColors = [];
public function mount()
{
$outlets = auth()->user()->outlets()->pluck('name', 'outlets.id');
$revenues = DB::table('orders')
->select('outlet_id', DB::raw('SUM(total) as total_revenue'))
->whereIn('outlet_id', $outlets->keys())
->groupBy('outlet_id')
->pluck('total_revenue', 'outlet_id');
$this->outlets = $outlets->values()->toArray();
$this->revenues = $outlets->map(fn ($name, $id) => $revenues[$id] ?? 0)->values()->toArray();
$count = count($this->outlets);
$this->backgroundColors = randomColors($count, 0.2);
$this->borderColors = randomColors($count, 1.0);
}
public function render()
{
return view('livewire.chart.outlet-revenue');
}
}