165 lines
4.9 KiB
PHP
165 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Parking\Setup\Gate;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\MasterLokasi;
|
|
use App\Models\NamaPos;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class MasterGateController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$Gate = NamaPos::with("areaParkir")->orderBy("id")->get();
|
|
$Area = MasterLokasi::orderBy("prioritas")->orderBy("id_lokasi")->get();
|
|
|
|
return view("setup.gate.gate", compact("Gate", "Area"));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->merge([
|
|
"id" => strtoupper(trim((string) $request->input("id"))),
|
|
"nama" => trim((string) $request->input("nama")),
|
|
]);
|
|
|
|
$data = $request->validate(
|
|
[
|
|
"id" => ["required", "string", "max:10", Rule::unique("nama_pos", "id")],
|
|
"nama" => ["required", "string", "max:100"],
|
|
"jenis" => ["required", "integer", "in:0,1,2,3"],
|
|
"grace_period" => ["nullable", "integer", "in:0,1"],
|
|
"manless" => ["nullable", "integer", "in:0,1"],
|
|
"area" => ["nullable", "string", "max:10", Rule::exists("master_lokasi", "id_lokasi")],
|
|
],
|
|
[
|
|
"id.unique" => "ID pintu/pos sudah dipakai, gunakan kode lain.",
|
|
"area.exists" => "Area parkir tidak ditemukan.",
|
|
]
|
|
);
|
|
|
|
$gate = NamaPos::create([
|
|
"id" => $data["id"],
|
|
"nama" => $data["nama"],
|
|
"jenis" => $data["jenis"],
|
|
"tanggal" => now(),
|
|
"lokasi" => null,
|
|
"grace_period" => $data["grace_period"] ?? 1,
|
|
"manless" => $data["manless"] ?? 0,
|
|
"area" => $data["area"] ?? null,
|
|
]);
|
|
$gate->load("areaParkir");
|
|
|
|
return response()->json([
|
|
"success" => true,
|
|
"message" => "Pintu/pos berhasil ditambahkan.",
|
|
"data" => $gate,
|
|
], 201);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$gate = NamaPos::with("areaParkir")->find($id);
|
|
|
|
if (!$gate) {
|
|
return response()->json([
|
|
"success" => false,
|
|
"message" => "Pintu/pos tidak ditemukan.",
|
|
], 404);
|
|
}
|
|
|
|
return response()->json(["success" => true, "data" => $gate]);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
return $this->show($id);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$gate = NamaPos::find($id);
|
|
|
|
if (!$gate) {
|
|
return response()->json([
|
|
"success" => false,
|
|
"message" => "Pintu/pos tidak ditemukan.",
|
|
], 404);
|
|
}
|
|
|
|
$request->merge([
|
|
"nama" => trim((string) $request->input("nama")),
|
|
]);
|
|
|
|
$data = $request->validate(
|
|
[
|
|
"nama" => ["required", "string", "max:100"],
|
|
"jenis" => ["required", "integer", "in:0,1,2,3"],
|
|
"grace_period" => ["nullable", "integer", "in:0,1"],
|
|
"manless" => ["nullable", "integer", "in:0,1"],
|
|
"area" => ["nullable", "string", "max:10", Rule::exists("master_lokasi", "id_lokasi")],
|
|
],
|
|
[
|
|
"area.exists" => "Area parkir tidak ditemukan.",
|
|
]
|
|
);
|
|
|
|
$data["area"] = $request->filled("area") ? $request->input("area") : null;
|
|
$gate->update($data);
|
|
$gate->load("areaParkir");
|
|
|
|
return response()->json([
|
|
"success" => true,
|
|
"message" => "Pintu/pos berhasil diperbarui.",
|
|
"data" => $gate,
|
|
]);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$gate = NamaPos::find($id);
|
|
|
|
if (!$gate) {
|
|
return response()->json([
|
|
"success" => false,
|
|
"message" => "Pintu/pos tidak ditemukan.",
|
|
], 404);
|
|
}
|
|
|
|
$usedTrx = DB::table("transaksi_parkir")
|
|
->where("id_pintu_masuk", $id)
|
|
->orWhere("id_pintu_keluar", $id)
|
|
->count();
|
|
if ($usedTrx > 0) {
|
|
return response()->json([
|
|
"success" => false,
|
|
"message" => "Pintu/pos {$id} dipakai {$usedTrx} transaksi, tidak bisa dihapus.",
|
|
], 409);
|
|
}
|
|
|
|
$usedConfig = DB::table("config_pos")->where("id_pos", $id)->count()
|
|
+ DB::table("config_pos_hardware")->where("id_pos", $id)->count();
|
|
if ($usedConfig > 0) {
|
|
return response()->json([
|
|
"success" => false,
|
|
"message" => "Pintu/pos {$id} masih punya konfigurasi Pos/Hardware, hapus konfigurasinya dulu.",
|
|
], 409);
|
|
}
|
|
|
|
$gate->delete();
|
|
|
|
return response()->json([
|
|
"success" => true,
|
|
"message" => "Pintu/pos berhasil dihapus.",
|
|
]);
|
|
}
|
|
}
|