49 lines
1.1 KiB
PHP
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 = generateRandomRgbaColors($count, 0.2);
|
|
$this->borderColors = generateRandomRgbaColors($count, 1.0);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.chart.channel-revenue');
|
|
}
|
|
}
|