parfum/app/Livewire/Chart/ChannelRevenue.php
2025-11-11 15:58:49 +07:00

49 lines
1.1 KiB
PHP

<?php
namespace App\Livewire\Chart;
use App\Enums\OrderChannel;
use App\Models\Order;
use Livewire\Component;
class ChannelRevenue extends Component
{
public array $channels = [];
public array $revenues = [];
public array $backgroundColors = [];
public array $borderColors = [];
public function mount(array $outlets)
{
$orders = Order::select('channel', 'total')
->whereIn('outlet_id', array_keys($outlets))
->get()
->groupBy('channel');
$data = [];
foreach (OrderChannel::cases() as $channel) {
$channelOrders = $orders[$channel->value] ?? collect();
$total = $channelOrders->sum('total');
$data[] = $total;
$this->channels[] = $channel->label();
}
$this->revenues = $data;
// random colors
$count = count(OrderChannel::cases());
$this->backgroundColors = randomColors($count, 0.2);
$this->borderColors = randomColors($count, 1.0);
}
public function render()
{
return view('livewire.chart.channel-revenue');
}
}