125 lines
4.2 KiB
PHP
125 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Backend;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Admin;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
use App\Models\FormUpCountry;
|
|
use App\Models\FormEntertaimentPresentation;
|
|
use App\Models\FormVehicleRunningCost;
|
|
use App\Models\FormMeetingSeminar;
|
|
use App\Models\FormOthers;
|
|
use App\Models\UserHasCabang;
|
|
use App\Models\Notifications;
|
|
use Carbon\Carbon;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$role = auth()->user()->getRoleNames()[0];
|
|
$startDay = (int) env('STARTING_DATE', 11);
|
|
$closingDay = (int) env('CLOSING_DATE', 10);
|
|
|
|
// Referensi waktu sekarang
|
|
$now = Carbon::now();
|
|
|
|
// Tentukan periode
|
|
if ($now->day >= $startDay) {
|
|
// Mulai dari tanggal START bulan ini
|
|
$startDate = Carbon::create($now->year, $now->month, $startDay)->startOfDay();
|
|
// Sampai tanggal CLOSING bulan depan
|
|
$closingDate = Carbon::create($now->copy()->addMonth()->year, $now->copy()->addMonth()->month, $closingDay)->endOfDay();
|
|
} else {
|
|
// Mulai dari tanggal START bulan lalu
|
|
$startDate = Carbon::create($now->copy()->subMonth()->year, $now->copy()->subMonth()->month, $startDay)->startOfDay();
|
|
// Sampai tanggal CLOSING bulan ini
|
|
$closingDate = Carbon::create($now->year, $now->month, $closingDay)->endOfDay();
|
|
}
|
|
|
|
$formQueries = [
|
|
'vehicle' => FormVehicleRunningCost::query()
|
|
->whereMonth('tanggal', date('m'))
|
|
->whereBetween('tanggal', [$startDate, $closingDate]),
|
|
'upcountry' => FormUpCountry::query()
|
|
->whereMonth('tanggal', date('m'))
|
|
->whereBetween('tanggal', [$startDate, $closingDate]),
|
|
'entertainment' => FormEntertaimentPresentation::query()
|
|
->whereMonth('tanggal', date('m'))
|
|
->whereBetween('tanggal', [$startDate, $closingDate]),
|
|
'meeting' => FormMeetingSeminar::query()
|
|
->whereMonth('created_at', date('m'))
|
|
->whereBetween('created_at', [$startDate, $closingDate]),
|
|
'others' => FormOthers::query()
|
|
->whereMonth('tanggal', date('m'))
|
|
->whereBetween('tanggal', [$startDate, $closingDate]),
|
|
];
|
|
|
|
$approvedStatuses = [
|
|
// 'Approved',
|
|
// 'Approved 1',
|
|
// 'Approved 2',
|
|
// 'Final Approved',
|
|
// 'Final Approve',
|
|
'Closed',
|
|
];
|
|
|
|
$forms = [];
|
|
|
|
foreach ($formQueries as $key => $query) {
|
|
$forms[$key] = $query
|
|
->whereIn('status', $approvedStatuses)
|
|
->whereNotNull('final_approved_at')
|
|
->sum('approved_total');
|
|
}
|
|
|
|
return view(
|
|
'backend.pages.dashboard.index',
|
|
[
|
|
'forms' => $forms,
|
|
]
|
|
);
|
|
}
|
|
|
|
public function notifications()
|
|
{
|
|
$this->checkAuthorization(auth()->user(), ['notification.view']);
|
|
|
|
// Mulai dengan query dasar
|
|
$notifications = Notifications::orderBy('created_at', 'desc')
|
|
->where('receiver_id', auth()->user()->id)
|
|
->where('is_read', 0);
|
|
|
|
// Ambil data dan kembalikan sebagai JSON
|
|
return response()->json([
|
|
'count' => $notifications->count(),
|
|
'data' => $notifications->take(7)->get(),
|
|
]);
|
|
}
|
|
|
|
public function mark_read($id)
|
|
{
|
|
$this->checkAuthorization(auth()->user(), ['notification.delete']);
|
|
$receiver_id = auth()->user()->id;
|
|
|
|
Notifications::where('receiver_id', $receiver_id)->where('id', $id)->update(['is_read' => 1]);
|
|
|
|
return response()->json(['message' => 'Notification marked as read.']);
|
|
}
|
|
|
|
public function mark_all_read()
|
|
{
|
|
$this->checkAuthorization(auth()->user(), ['notification.delete']);
|
|
$receiver_id = auth()->user()->id;
|
|
|
|
Notifications::where('receiver_id', $receiver_id)->update(['is_read' => 1]);
|
|
|
|
session()->flash('success', 'All notifications marked as read.');
|
|
return redirect()->back();
|
|
}
|
|
}
|