CRUD area & gate, statistik pengunjung per pintu, laporan okupansi area
This commit is contained in:
@@ -3,86 +3,162 @@
|
||||
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
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$Gate = NamaPos::with("areaParkir")->orderBy("id")->get();
|
||||
$Area = MasterLokasi::orderBy("prioritas")->orderBy("id_lokasi")->get();
|
||||
|
||||
$Gate = NamaPos::orderBy('id')->get();
|
||||
return view ('setup.gate.gate', compact('Gate'));
|
||||
return view("setup.gate.gate", compact("Gate", "Area"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
return $this->show($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
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,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
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.",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user