first
This commit is contained in:
114
app/Http/Controllers/RateMasController.php
Normal file
114
app/Http/Controllers/RateMasController.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class RateMasController extends Controller
|
||||
{
|
||||
private const TABLES = [
|
||||
'2013_ratemas' => 'RateMas 2013',
|
||||
'2014_ratemas' => 'RateMas 2014',
|
||||
'2015_ratemas' => 'RateMas 2015',
|
||||
'2016_ratemas' => 'RateMas 2016',
|
||||
'2017_ratemas' => 'RateMas 2017',
|
||||
'2018_ratemas' => 'RateMas 2018',
|
||||
];
|
||||
|
||||
public function index(): View
|
||||
{
|
||||
return view('ratemas.index', [
|
||||
'tables' => $this->availableTables(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function lookup(Request $request): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'noakaun' => ['required', 'string', 'max:100'],
|
||||
'year' => ['required', 'integer', 'min:1900', 'max:2100'],
|
||||
]);
|
||||
|
||||
return redirect()->route('ratemas.show', [
|
||||
'table' => $validated['year'].'_ratemas',
|
||||
'noakaun' => $validated['noakaun'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function search(string $table): View
|
||||
{
|
||||
$this->abortIfInvalidTable($table);
|
||||
|
||||
return view('ratemas.search', [
|
||||
'table' => $table,
|
||||
'title' => $this->tableTitle($table),
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Request $request, string $table): View
|
||||
{
|
||||
$this->abortIfInvalidTable($table);
|
||||
|
||||
$validated = $request->validate([
|
||||
'noakaun' => ['required', 'string', 'max:100'],
|
||||
]);
|
||||
|
||||
$record = DB::table($table)
|
||||
->where('no_akaun', $validated['noakaun'])
|
||||
->first();
|
||||
|
||||
abort_if($record === null, 404, 'Rekod no akaun tidak dijumpai.');
|
||||
|
||||
$years = array_map(
|
||||
fn (string $tableName): string => substr($tableName, 0, 4),
|
||||
array_keys($this->availableTables())
|
||||
);
|
||||
$currentYear = substr($table, 0, 4);
|
||||
$currentIndex = array_search($currentYear, $years, true);
|
||||
|
||||
return view('ratemas.show', [
|
||||
'record' => $record,
|
||||
'table' => $table,
|
||||
'title' => $this->tableTitle($table),
|
||||
'currentYear' => $currentYear,
|
||||
'years' => $years,
|
||||
'previousYear' => $currentIndex > 0 ? $years[$currentIndex - 1] : null,
|
||||
'nextYear' => $currentIndex < count($years) - 1 ? $years[$currentIndex + 1] : null,
|
||||
'noakaun' => $validated['noakaun'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function abortIfInvalidTable(string $table): void
|
||||
{
|
||||
abort_unless(preg_match('/^\d{4}_ratemas$/', $table) === 1 && Schema::hasTable($table), 404);
|
||||
}
|
||||
|
||||
private function tableTitle(string $table): string
|
||||
{
|
||||
return $this->availableTables()[$table] ?? 'RateMas '.substr($table, 0, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function availableTables(): array
|
||||
{
|
||||
$tables = self::TABLES;
|
||||
|
||||
foreach (Schema::getTables() as $table) {
|
||||
$name = $table['name'] ?? $table['table'] ?? null;
|
||||
|
||||
if (is_string($name) && preg_match('/^\d{4}_ratemas$/', $name) === 1) {
|
||||
$tables[$name] = 'RateMas '.substr($name, 0, 4);
|
||||
}
|
||||
}
|
||||
|
||||
ksort($tables);
|
||||
|
||||
return $tables;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user