78 lines
2.9 KiB
PHP
78 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Barbershop;
|
|
use App\Models\Expense;
|
|
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())->sum('total');
|
|
$yesterdayIncome = Transaction::query()->barbershop()->whereDate('created_at', now()->yesterday())->sum('total');
|
|
|
|
$todayShaving = Transaction::query()->barbershop()->today()->count();
|
|
$yesterdayShaving = Transaction::query()->barbershop()->yesterday()->count();
|
|
|
|
$todayExpense = Expense::query()->barbershop()->today()->sum('amount');
|
|
$yesterdayExpense = Expense::query()->barbershop()->yesterday()->sum('amount');
|
|
|
|
$todayNewCustomer = Transaction::with('customer')->whereDoesntHave('customer.transactions', function ($query) {
|
|
$query->where('id', '<>', DB::raw('transactions.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'));
|
|
})
|
|
->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')
|
|
->limit(5)
|
|
->get();
|
|
|
|
return view('pages.admin.dashboard.overview', [
|
|
'pageTitle' => 'Ringkasan',
|
|
'todayIncome' => $todayIncome,
|
|
'yesterdayIncome' => $yesterdayIncome,
|
|
'todayShaving' => $todayShaving,
|
|
'yesterdayShaving' => $yesterdayShaving,
|
|
'todayExpense' => $todayExpense,
|
|
'yesterdayExpense' => $yesterdayExpense,
|
|
'todayNewCustomer' => $todayNewCustomer,
|
|
'yesterdayNewCustomer' => $yesterdayNewCustomer,
|
|
'cash' => Barbershop::findOrFail(Auth::user()->barbershop_id)->cash,
|
|
'topCustomersByTotal' => $topCustomersByTotal,
|
|
]);
|
|
}
|
|
|
|
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');
|
|
}
|
|
}
|