first commit
This commit is contained in:
118
app/Http/Controllers/DashboardController.php
Normal file
118
app/Http/Controllers/DashboardController.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Consultant;
|
||||
use App\Models\DataCentre;
|
||||
use App\Models\ReportingCycle;
|
||||
use App\Models\Submission;
|
||||
use App\Models\SubmissionResult;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
if ($user->isPerunding()) {
|
||||
return $this->consultantDashboard();
|
||||
}
|
||||
|
||||
return $this->jppDashboard();
|
||||
}
|
||||
|
||||
protected function jppDashboard()
|
||||
{
|
||||
$activeCycle = ReportingCycle::aktif()->orderByDesc('renewal_year')->first();
|
||||
|
||||
$totalDataCentres = DataCentre::aktif()->count();
|
||||
$activeConsultants = Consultant::aktif()->count();
|
||||
|
||||
$submittedCount = 0;
|
||||
$notSubmittedCount = 0;
|
||||
$overdueCount = 0;
|
||||
$statusBreakdown = collect();
|
||||
|
||||
if ($activeCycle) {
|
||||
$expected = DataCentre::aktif()->whereNotNull('current_consultant_id')->count();
|
||||
$submittedCount = Submission::where('reporting_cycle_id', $activeCycle->id)
|
||||
->where('status', '!=', 'draf')->count();
|
||||
$notSubmittedCount = max(0, $expected - $submittedCount);
|
||||
|
||||
$statusBreakdown = Submission::where('reporting_cycle_id', $activeCycle->id)
|
||||
->select('status', DB::raw('count(*) as jumlah'))
|
||||
->groupBy('status')->pluck('jumlah', 'status');
|
||||
|
||||
if ($activeCycle->isOverdue()) {
|
||||
$overdueCount = $notSubmittedCount;
|
||||
}
|
||||
}
|
||||
|
||||
// Jumlah karbon A,B,C,D,E mengikut tahun pelaporan
|
||||
$carbonByYear = SubmissionResult::query()
|
||||
->join('submissions', 'submissions.id', '=', 'submission_results.submission_id')
|
||||
->join('reporting_cycles', 'reporting_cycles.id', '=', 'submissions.reporting_cycle_id')
|
||||
->whereNull('submissions.deleted_at')
|
||||
->select(
|
||||
'reporting_cycles.reporting_year',
|
||||
'submission_results.result_token',
|
||||
DB::raw('SUM(submission_results.value) as jumlah')
|
||||
)
|
||||
->groupBy('reporting_cycles.reporting_year', 'submission_results.result_token')
|
||||
->get()
|
||||
->groupBy('reporting_year');
|
||||
|
||||
// Pusat data tertinggi mengikut anggaran karbon (token A)
|
||||
$topDataCentres = SubmissionResult::query()
|
||||
->where('submission_results.result_token', 'A')
|
||||
->join('submissions', 'submissions.id', '=', 'submission_results.submission_id')
|
||||
->join('data_centres', 'data_centres.id', '=', 'submissions.data_centre_id')
|
||||
->whereNull('submissions.deleted_at')
|
||||
->select('data_centres.nama_pusat_data', DB::raw('SUM(submission_results.value) as jumlah'))
|
||||
->groupBy('data_centres.nama_pusat_data')
|
||||
->orderByDesc('jumlah')
|
||||
->limit(10)
|
||||
->get();
|
||||
|
||||
return view('dashboard.jpp', compact(
|
||||
'activeCycle', 'totalDataCentres', 'activeConsultants',
|
||||
'submittedCount', 'notSubmittedCount', 'overdueCount',
|
||||
'statusBreakdown', 'carbonByYear', 'topDataCentres'
|
||||
));
|
||||
}
|
||||
|
||||
protected function consultantDashboard()
|
||||
{
|
||||
$consultant = auth()->user()->consultant;
|
||||
|
||||
$dataCentres = collect();
|
||||
$submissions = collect();
|
||||
$pendingCount = 0;
|
||||
$correctionCount = 0;
|
||||
$activeCycle = ReportingCycle::aktif()->orderByDesc('renewal_year')->first();
|
||||
|
||||
if ($consultant) {
|
||||
$dataCentres = DataCentre::where('current_consultant_id', $consultant->id)->get();
|
||||
|
||||
$submissions = Submission::where('consultant_id', $consultant->id)
|
||||
->with(['dataCentre', 'reportingCycle'])
|
||||
->latest()->get();
|
||||
|
||||
$correctionCount = $submissions->where('status', 'pembetulan_perunding')->count();
|
||||
|
||||
if ($activeCycle) {
|
||||
$submittedDcIds = Submission::where('consultant_id', $consultant->id)
|
||||
->where('reporting_cycle_id', $activeCycle->id)
|
||||
->where('status', '!=', 'draf')
|
||||
->pluck('data_centre_id');
|
||||
$pendingCount = $dataCentres->whereNotIn('id', $submittedDcIds)->count();
|
||||
}
|
||||
}
|
||||
|
||||
return view('dashboard.consultant', compact(
|
||||
'consultant', 'dataCentres', 'submissions',
|
||||
'pendingCount', 'correctionCount', 'activeCycle'
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user