Files
expense/app/Http/Controllers/Backend/DashboardController.php
T

143 lines
5.7 KiB
PHP
Raw Normal View History

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-01-01 23:59:49 +07:00
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-01-14 17:25:26 +07:00
$closing_date = env('CLOSING_DATE');
2025-01-01 23:59:49 +07:00
$forms = [
2025-01-12 13:37:29 +07:00
'vehicle' => FormVehicleRunningCost::whereMonth('tanggal', date('m'))
->whereYear('tanggal', date('Y'))
2025-01-14 17:25:26 +07:00
->where('tanggal', '<=', date('Y-m-'.$closing_date))
->whereRaw('DAY(CURDATE()) <= '.$closing_date)
2025-01-12 13:37:29 +07:00
->orderBy('tanggal', 'desc')
->get(),
'upcountry' => FormUpCountry::whereMonth('tanggal', date('m'))
->whereYear('tanggal', date('Y'))
2025-01-14 17:25:26 +07:00
->where('tanggal', '<=', date('Y-m-'.$closing_date))
->whereRaw('DAY(CURDATE()) <= '.$closing_date)
2025-01-12 13:37:29 +07:00
->orderBy('tanggal', 'desc')
->get(),
'entertainment' => FormEntertaimentPresentation::whereMonth('tanggal', date('m'))
->whereYear('tanggal', date('Y'))
2025-01-14 17:25:26 +07:00
->where('tanggal', '<=', date('Y-m-'.$closing_date))
->whereRaw('DAY(CURDATE()) <= '.$closing_date)
2025-01-12 13:37:29 +07:00
->orderBy('tanggal', 'desc')
->get(),
'meeting' => FormMeetingSeminar::whereMonth('created_at', date('m'))
->whereYear('created_at', date('Y'))
2025-01-14 17:25:26 +07:00
->where('created_at', '<=', date('Y-m-'.$closing_date))
->whereRaw('DAY(CURDATE()) <= '.$closing_date)
2025-01-12 13:37:29 +07:00
->orderBy('created_at', 'desc')
->get(),
'others' => FormOthers::whereMonth('tanggal', date('m'))
->whereYear('tanggal', date('Y'))
2025-01-14 17:25:26 +07:00
->where('tanggal', '<=', date('Y-m-'.$closing_date))
->whereRaw('DAY(CURDATE()) <= '.$closing_date)
2025-01-12 13:37:29 +07:00
->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']);
$role = auth()->user()->getRoleNames()[0];
// Mulai dengan query dasar
$notifications = Notifications::orderBy('created_at', 'desc')
->where('receiver_id', auth()->user()->id)
->where('is_read', 0);
// Tambahkan filter berdasarkan role
if ($role == 'Admin Region') {
$notifications->where('type', 'new_expense');
} elseif ($role == 'Marketing Operational Manager Region') {
$notifications->where('type', 'approve2_expense');
} elseif ($role == 'Area Manager Cabang' || $role == 'Marketing Information System') {
$notifications->where('type', 'approve_expense');
} elseif ($role == 'Head of Sales Marketing') {
$notifications->where('type', 'approve2_expense');
} else {
// Jika tidak ada role yang sesuai, kembalikan koleksi kosong
return response()->json([]);
}
// Ambil data dan kembalikan sebagai JSON
return response()->json($notifications->limit(6)->get());
}
public function mark_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', 'Notifications marked as read.');
return redirect()->back();
}
2024-12-02 01:18:34 +07:00
}