133 lines
5.1 KiB
PHP
Executable File
133 lines
5.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Barbershop;
|
|
use App\Models\DepositMoney;
|
|
use App\Models\Expense;
|
|
use App\Models\Payroll;
|
|
use App\Models\Transaction;
|
|
use App\Models\User;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class OverviewController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$todayIncome = Transaction::query()
|
|
->barbershop()
|
|
->whereDate('created_at', now()->today())
|
|
->when(auth()->user()->role_id === 5, function ($query) {
|
|
return $query->where('user_id', auth()->id());
|
|
})
|
|
->sum('total');
|
|
$yesterdayIncome = Transaction::query()
|
|
->barbershop()
|
|
->whereDate('created_at', now()->yesterday())
|
|
->when(auth()->user()->role_id === 5, function ($query) {
|
|
return $query->where('user_id', auth()->id());
|
|
})
|
|
->sum('total');
|
|
|
|
$todayShaving = Transaction::query()
|
|
->barbershop()
|
|
->today()
|
|
->when(auth()->user()->role_id === 5, function ($query) {
|
|
return $query->where('user_id', auth()->id());
|
|
})
|
|
->count();
|
|
$yesterdayShaving = Transaction::query()
|
|
->barbershop()
|
|
->yesterday()
|
|
->when(auth()->user()->role_id === 5, function ($query) {
|
|
return $query->where('user_id', auth()->id());
|
|
})
|
|
->count();
|
|
|
|
$todayExpense = Expense::query()->barbershop()->today()->sum('amount');
|
|
$yesterdayExpense = Expense::query()->barbershop()->yesterday()->sum('amount');
|
|
|
|
$todayDepositMoney = DepositMoney::query()->barbershop()->today()->sum('amount');
|
|
$yesterdayDepositMoney = DepositMoney::query()->barbershop()->yesterday()->sum('amount');
|
|
|
|
$todayPayroll = Payroll::query()->barbershop()->today()->sum('total');
|
|
$yesterdayPayroll = Payroll::query()->barbershop()->yesterday()->sum('amount');
|
|
|
|
$todayNewCustomer = Transaction::with('customer')->whereDoesntHave('customer.transactions', function ($query) {
|
|
$query->where('id', '<>', DB::raw('transactions.id'));
|
|
})
|
|
->when(auth()->user()->role_id === 5, function ($query) {
|
|
return $query->where('user_id', auth()->id());
|
|
})
|
|
->barbershop()
|
|
->today()
|
|
->distinct('customer_id')
|
|
->count('customer_id');
|
|
|
|
$yesterdayNewCustomer = Transaction::with('customer')->whereDoesntHave('customer.transactions', function ($query) {
|
|
$query->where('id', '<>', DB::raw('transactions.id'));
|
|
})
|
|
->when(auth()->user()->role_id === 5, function ($query) {
|
|
return $query->where('user_id', auth()->id());
|
|
})
|
|
->barbershop()
|
|
->yesterday()
|
|
->distinct('customer_id')
|
|
->count('customer_id');
|
|
|
|
$topCustomersByTotal = Transaction::groupBy('customer_id')
|
|
->select(
|
|
'customer_id',
|
|
DB::raw('SUM(total) as total_transaction'),
|
|
DB::raw('COUNT(*) as transaction_count')
|
|
)
|
|
->orderBy('total_transaction', 'desc')
|
|
->when(auth()->user()->role_id === 5, function ($query) {
|
|
return $query->where('user_id', auth()->id());
|
|
})
|
|
->limit(5)
|
|
->get();
|
|
|
|
$incomeByBarber = User::with('biography')
|
|
->barbershop()
|
|
->barber()
|
|
->withSum('transactions', 'total')
|
|
->withCount('transactions');
|
|
if (in_array(auth()->user()->role_id, [1, 2, 3])) {
|
|
$incomeByBarber = $incomeByBarber->get();
|
|
} elseif (auth()->user()->role_id === 5) {
|
|
$incomeByBarber = $incomeByBarber->find(auth()->id());
|
|
}
|
|
|
|
return view('pages.admin.dashboard.overview', [
|
|
'pageTitle' => 'Ringkasan',
|
|
'todayIncome' => $todayIncome,
|
|
'yesterdayIncome' => $yesterdayIncome,
|
|
'todayShaving' => $todayShaving,
|
|
'yesterdayShaving' => $yesterdayShaving,
|
|
'todayExpense' => $todayExpense + $todayPayroll,
|
|
'todayDepositMoney' => $todayDepositMoney,
|
|
'yesterdayDepositMoney' => $yesterdayDepositMoney,
|
|
'yesterdayExpense' => $yesterdayExpense + $yesterdayPayroll,
|
|
'todayNewCustomer' => $todayNewCustomer,
|
|
'yesterdayNewCustomer' => $yesterdayNewCustomer,
|
|
'cash' => Barbershop::findOrFail(Auth::user()->barbershop_id)->cash,
|
|
'topCustomersByTotal' => $topCustomersByTotal,
|
|
'incomeByBarber' => $incomeByBarber,
|
|
]);
|
|
}
|
|
|
|
public function changeBarbershop(Barbershop $barbershop): RedirectResponse
|
|
{
|
|
$user = User::findOrFail(Auth::id());
|
|
$user->barbershop_id = $barbershop->id;
|
|
$user->save();
|
|
notify()->success('Barbershop berhasil diubah', 'Berhasil');
|
|
|
|
return redirect()->route('dashboard.overview');
|
|
}
|
|
}
|