2024-12-02 01:18:34 +07:00
|
|
|
<?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;
|
2025-01-01 23:59:49 +07:00
|
|
|
use App\Models\FormUpCountry;
|
|
|
|
|
use App\Models\FormEntertaimentPresentation;
|
|
|
|
|
use App\Models\FormVehicleRunningCost;
|
|
|
|
|
use App\Models\FormMeetingSeminar;
|
|
|
|
|
use App\Models\FormOthers;
|
|
|
|
|
use App\Models\UserHasCabang;
|
2025-01-17 16:17:53 +07:00
|
|
|
use App\Models\Notifications;
|
2025-05-08 20:00:20 +07:00
|
|
|
use Carbon\Carbon;
|
2024-12-02 01:18:34 +07:00
|
|
|
|
|
|
|
|
class DashboardController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
2025-01-01 23:59:49 +07:00
|
|
|
$role = auth()->user()->getRoleNames()[0];
|
2025-05-08 20:00:20 +07:00
|
|
|
$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();
|
|
|
|
|
}
|
2025-01-23 17:46:38 +07:00
|
|
|
|
2025-01-01 23:59:49 +07:00
|
|
|
$forms = [
|
2025-01-12 13:37:29 +07:00
|
|
|
'vehicle' => FormVehicleRunningCost::whereMonth('tanggal', date('m'))
|
2025-05-08 20:00:20 +07:00
|
|
|
->whereBetween('tanggal', [$startDate, $closingDate])
|
|
|
|
|
->orderBy('tanggal', 'desc')
|
|
|
|
|
->get(),
|
2025-01-12 13:37:29 +07:00
|
|
|
'upcountry' => FormUpCountry::whereMonth('tanggal', date('m'))
|
2025-05-08 20:00:20 +07:00
|
|
|
->whereBetween('tanggal', [$startDate, $closingDate])
|
|
|
|
|
->orderBy('tanggal', 'desc')
|
|
|
|
|
->get(),
|
2025-01-12 13:37:29 +07:00
|
|
|
'entertainment' => FormEntertaimentPresentation::whereMonth('tanggal', date('m'))
|
2025-05-08 20:00:20 +07:00
|
|
|
->whereBetween('tanggal', [$startDate, $closingDate])
|
|
|
|
|
->orderBy('tanggal', 'desc')
|
|
|
|
|
->get(),
|
2025-01-12 13:37:29 +07:00
|
|
|
'meeting' => FormMeetingSeminar::whereMonth('created_at', date('m'))
|
2025-05-08 20:00:20 +07:00
|
|
|
->whereBetween('created_at', [$startDate, $closingDate])
|
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
|
->get(),
|
2025-01-12 13:37:29 +07:00
|
|
|
'others' => FormOthers::whereMonth('tanggal', date('m'))
|
2025-05-08 20:00:20 +07:00
|
|
|
->whereBetween('tanggal', [$startDate, $closingDate])
|
|
|
|
|
->orderBy('tanggal', 'desc')
|
|
|
|
|
->get(),
|
2025-01-01 23:59:49 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
|
|
|
|
$region_id = auth()->user()->getMyCabangAndRegionId()['region'];
|
|
|
|
|
|
|
|
|
|
if ($region_id) {
|
|
|
|
|
$users = UserHasCabang::whereHas('cabang', function ($query) use ($region_id) {
|
|
|
|
|
$query->where('region_id', $region_id);
|
|
|
|
|
})->pluck('user_id');
|
|
|
|
|
|
|
|
|
|
foreach ($forms as $key => $query) {
|
|
|
|
|
$forms[$key] = $query->whereIn('user_id', $users)->whereIn('status', ['Approved', 'Closed']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($role == 'Area Manager Cabang') {
|
|
|
|
|
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'];
|
|
|
|
|
|
|
|
|
|
if ($cabang_id) {
|
|
|
|
|
$users = UserHasCabang::where('cabang_id', $cabang_id)->pluck('user_id');
|
|
|
|
|
|
|
|
|
|
foreach ($forms as $key => $query) {
|
|
|
|
|
$forms[$key] = $query->whereIn('user_id', $users)->whereIn('status', ['Approved', 'Closed']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($role == 'Medical Representatif') {
|
|
|
|
|
foreach ($forms as $key => $query) {
|
|
|
|
|
$forms[$key] = $query->where('user_id', auth()->user()->id)->whereIn('status', ['Approved', 'Closed']);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
foreach ($forms as $key => $query) {
|
|
|
|
|
$forms[$key] = $query->whereIn('status', ['Approved', 'Closed']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sum the filtered queries
|
|
|
|
|
foreach ($forms as $key => $query) {
|
2025-01-02 17:59:33 +07:00
|
|
|
$forms[$key] = $query->sum('approved_total');
|
2025-01-01 23:59:49 +07:00
|
|
|
}
|
2024-12-02 01:18:34 +07:00
|
|
|
|
|
|
|
|
return view(
|
|
|
|
|
'backend.pages.dashboard.index',
|
|
|
|
|
[
|
2025-01-01 23:59:49 +07:00
|
|
|
'forms' => $forms,
|
2024-12-02 01:18:34 +07:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-01-17 16:17:53 +07:00
|
|
|
|
|
|
|
|
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
|
2025-01-19 15:22:17 +07:00
|
|
|
return response()->json([
|
|
|
|
|
'count' => $notifications->count(),
|
|
|
|
|
'data' => $notifications->take(7)->get(),
|
|
|
|
|
]);
|
2025-01-17 16:17:53 +07:00
|
|
|
}
|
|
|
|
|
|
2025-01-23 17:46:38 +07:00
|
|
|
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()
|
2025-01-17 16:17:53 +07:00
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['notification.delete']);
|
|
|
|
|
$receiver_id = auth()->user()->id;
|
|
|
|
|
|
|
|
|
|
Notifications::where('receiver_id', $receiver_id)->update(['is_read' => 1]);
|
|
|
|
|
|
2025-01-23 17:46:38 +07:00
|
|
|
session()->flash('success', 'All notifications marked as read.');
|
2025-01-17 16:17:53 +07:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2024-12-02 01:18:34 +07:00
|
|
|
}
|