78 lines
2.9 KiB
PHP
78 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Person;
|
|
use App\Models\Migration;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Carbon\Carbon;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function getStats()
|
|
{
|
|
// Total migrants count
|
|
$totalMigrants = Person::count();
|
|
|
|
// New migrants in the current month
|
|
$currentMonthStart = Carbon::now()->startOfMonth();
|
|
$newThisMonth = Person::where('created_at', '>=', $currentMonthStart)->count();
|
|
|
|
// Recent additions (last 30 days)
|
|
$thirtyDaysAgo = Carbon::now()->subDays(30);
|
|
$recentAdditions = Person::where('created_at', '>=', $thirtyDaysAgo)->count();
|
|
|
|
// Pending reviews - example: people with missing information
|
|
$pendingReviews = Person::whereNull('date_of_birth')
|
|
->orWhereNull('place_of_birth')
|
|
->orWhereNull('occupation')
|
|
->count();
|
|
|
|
// Incomplete records - persons missing multiple key fields
|
|
$incompleteRecords = Person::where(function($query) {
|
|
$query->whereNull('date_of_birth')
|
|
->orWhereNull('place_of_birth');
|
|
})
|
|
->where(function($query) {
|
|
$query->whereNull('occupation')
|
|
->orWhereNull('reference')
|
|
->orWhereNull('id_card_no');
|
|
})
|
|
->count();
|
|
|
|
// Find peak migration period (by year)
|
|
$peakMigrationYear = Migration::select(DB::raw('YEAR(date_of_arrival_nt) as year'), DB::raw('COUNT(*) as count'))
|
|
->whereNotNull('date_of_arrival_nt')
|
|
->groupBy(DB::raw('YEAR(date_of_arrival_nt)'))
|
|
->orderBy('count', 'desc')
|
|
->first();
|
|
|
|
// Find most common place of birth
|
|
$mostCommonOrigin = Person::select('place_of_birth', DB::raw('COUNT(*) as count'))
|
|
->whereNotNull('place_of_birth')
|
|
->where('place_of_birth', '!=', '')
|
|
->groupBy('place_of_birth')
|
|
->orderBy('count', 'desc')
|
|
->first();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'total_migrants' => $totalMigrants,
|
|
'new_this_month' => $newThisMonth,
|
|
'recent_additions' => $recentAdditions,
|
|
'pending_reviews' => $pendingReviews,
|
|
'incomplete_records' => $incompleteRecords,
|
|
'peak_migration_year' => $peakMigrationYear ? [
|
|
'year' => $peakMigrationYear->year,
|
|
'count' => $peakMigrationYear->count
|
|
] : null,
|
|
'most_common_origin' => $mostCommonOrigin ? [
|
|
'place' => $mostCommonOrigin->place_of_birth,
|
|
'count' => $mostCommonOrigin->count
|
|
] : null
|
|
]
|
|
]);
|
|
}
|
|
} |