diff --git a/app/Livewire/Chart/ChannelRevenue.php b/app/Livewire/Chart/ChannelRevenue.php new file mode 100644 index 0000000..f722f0f --- /dev/null +++ b/app/Livewire/Chart/ChannelRevenue.php @@ -0,0 +1,50 @@ +user()->outlets()->pluck('name', 'outlets.id'); + + $orders = Order::select('channel', 'total') + ->whereIn('outlet_id', $outlets->keys()) + ->get() + ->groupBy('channel'); + + $data = []; + + $count = count(OrderChannel::cases()); + + foreach (OrderChannel::cases() as $channel) { + $channelOrders = $orders[$channel->value] ?? collect(); + $total = $channelOrders->sum('total'); + + $data[] = $total; + $this->channels[] = $channel->label(); + } + + $this->revenues = $data; + + $this->backgroundColors = randomColors($count, 0.2); + $this->borderColors = randomColors($count, 1.0); + } + + public function render() + { + return view('livewire.chart.channel-revenue'); + } +} diff --git a/app/Livewire/Chart/OutletExpense.php b/app/Livewire/Chart/OutletExpense.php new file mode 100644 index 0000000..af22ad0 --- /dev/null +++ b/app/Livewire/Chart/OutletExpense.php @@ -0,0 +1,51 @@ +user()->outlets()->pluck('name', 'outlets.id'); + + $expensesData = []; + + foreach ($outlets as $id => $name) { + $expenseTotal = Expense::where('outlet_id', $id)->sum('amount'); + + $purchaseTotal = Purchase::where('outlet_id', $id)->sum('total'); + + $payrollTotal = Payroll::whereHas('user.outlets', fn ($q) => $q->where('outlet_id', $id))->sum('total_salary'); + + $total = $expenseTotal + $purchaseTotal + $payrollTotal; + + $expensesData[$name] = $total; + } + + $this->outlets = array_keys($expensesData); + + $this->expenses = array_values($expensesData); + + $count = count($this->outlets); + $this->backgroundColors = randomColors($count, 0.2); + $this->borderColors = randomColors($count, 1.0); + } + + public function render() + { + return view('livewire.chart.outlet-expense'); + } +} diff --git a/app/Livewire/Chart/OutletRevenue.php b/app/Livewire/Chart/OutletRevenue.php new file mode 100644 index 0000000..41226a9 --- /dev/null +++ b/app/Livewire/Chart/OutletRevenue.php @@ -0,0 +1,41 @@ +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'); + } +} diff --git a/app/Livewire/Chart/OutletRevenueExpense.php b/app/Livewire/Chart/OutletRevenueExpense.php new file mode 100644 index 0000000..87a0052 --- /dev/null +++ b/app/Livewire/Chart/OutletRevenueExpense.php @@ -0,0 +1,48 @@ +user()->outlets()->pluck('name', 'outlets.id'); + + $revenueData = []; + + $expenseData = []; + + foreach ($outlets as $id => $name) { + $totalRevenue = Order::where('outlet_id', $id)->sum('total'); + $revenueData[$name] = $totalRevenue; + + $expenseTotal = Expense::where('outlet_id', $id)->sum('amount'); + $purchaseTotal = Purchase::where('outlet_id', $id)->sum('total'); + $payrollTotal = Payroll::whereHas('user.outlets', fn ($q) => $q->where('outlet_id', $id))->sum('total_salary'); + $expenseData[$name] = $expenseTotal + $purchaseTotal + $payrollTotal; + } + + $this->outlets = $outlets->values()->toArray(); + + $this->revenues = array_values($revenueData); + + $this->expenses = array_values($expenseData); + } + + public function render() + { + return view('livewire.chart.outlet-revenue-expense'); + } +} diff --git a/resources/views/livewire/chart/channel-revenue.blade.php b/resources/views/livewire/chart/channel-revenue.blade.php new file mode 100644 index 0000000..33a4d02 --- /dev/null +++ b/resources/views/livewire/chart/channel-revenue.blade.php @@ -0,0 +1,32 @@ +