181 lines
4.9 KiB
PHP
181 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\lokers;
|
|
use App\Models\penitipan;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Carbon\Carbon;
|
|
|
|
class LokersController extends Controller
|
|
{
|
|
|
|
//function get user
|
|
public function index()
|
|
{
|
|
$loker = Lokers::all();
|
|
|
|
return response()->json([
|
|
'message' => 'Data user berhasil diambil',
|
|
'data' => $loker
|
|
]);
|
|
}
|
|
|
|
public function getByNomorLoker($nomor_loker)
|
|
{
|
|
$loker = Lokers::where('nomor_loker', $nomor_loker)->first();
|
|
|
|
if ($loker) {
|
|
return response()->json($loker);
|
|
} else {
|
|
return response()->json([
|
|
'message' => 'Loker tidak ditemukan'
|
|
], 404);
|
|
}
|
|
}
|
|
|
|
//tambah loker
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'nomor_loker' => 'required|string',
|
|
'status' => 'required|in:kosong,digunakan,pending,servis'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['errors' => $validator->errors()], 422);
|
|
}
|
|
|
|
$loker = lokers::create([
|
|
'nomor_loker' => $request->nomor_loker,
|
|
'status' => $request->status
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Loker berhasil ditambahkan',
|
|
'data' => $loker],
|
|
200);
|
|
}
|
|
// hapus loker berdasarkan id
|
|
public function destroy($id)
|
|
{
|
|
$loker = Lokers::find($id);
|
|
|
|
if (!$loker) {
|
|
return response()->json([
|
|
'message' => 'Loker tidak ditemukan'
|
|
], 404);
|
|
}
|
|
|
|
$loker->delete();
|
|
|
|
return response()->json([
|
|
'message' => 'Loker berhasil dihapus'
|
|
], 200);
|
|
}
|
|
|
|
public function updateStatus(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'status' => 'required|in:kosong,digunakan,pending,servis'
|
|
]);
|
|
|
|
$loker = Lokers::find($id);
|
|
|
|
if (!$loker) {
|
|
return response()->json(['message' => 'Loker tidak ditemukan'], 404);
|
|
}
|
|
|
|
$prevStatus = $loker->status;
|
|
$loker->status = $request->status;
|
|
$loker->save();
|
|
|
|
// Temukan penitipan aktif yang berhubungan
|
|
$penitipan = Penitipan::where('loker_id', $loker->id)
|
|
->whereNull('waktu_selesai')
|
|
->latest()
|
|
->first();
|
|
|
|
if ($penitipan) {
|
|
// Jika status berubah ke 'digunakan' dan waktu_mulai belum ada
|
|
if ($request->status === 'digunakan' && !$penitipan->waktu_mulai) {
|
|
$penitipan->waktu_mulai = now();
|
|
$penitipan->save();
|
|
}
|
|
|
|
// Jika status berubah ke 'kosong' dan sebelumnya 'digunakan'
|
|
if ($request->status === 'kosong' && $prevStatus === 'digunakan') {
|
|
$penitipan->waktu_selesai = now();
|
|
|
|
// Parsing waktu selesai dan mulai ke Carbon
|
|
$end = Carbon::parse($penitipan->waktu_mulai);
|
|
$start = Carbon::parse($penitipan->waktu_selesai);
|
|
|
|
// Hitung durasi dalam detik
|
|
$durasiDetik = $end->diffInSeconds($start);
|
|
|
|
// Hitung blok per 30 detik
|
|
$blok = ceil($durasiDetik / 30);
|
|
$biaya = $blok * 1000;
|
|
|
|
// Simpan durasi dan biaya
|
|
$penitipan->durasi_menit = $durasiDetik; // kamu mau isi dengan jumlah detik (bukan menit)
|
|
$penitipan->biaya = $biaya;
|
|
|
|
$penitipan->save();
|
|
}
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => 'Status loker berhasil diperbarui',
|
|
'data' => $loker
|
|
], 200);
|
|
}
|
|
|
|
|
|
public function today()
|
|
{
|
|
$tanggal = Carbon::today()->toDateString();
|
|
|
|
$penitipanList = Penitipan::whereDate('waktu_selesai', $tanggal)->get();
|
|
|
|
$total = 0;
|
|
foreach ($penitipanList as $item) {
|
|
$total += $item->biaya ?? 0;
|
|
}
|
|
|
|
return response()->json([
|
|
'tanggal' => $tanggal,
|
|
'total_penghasilan' => $total,
|
|
'jumlah_transaksi' => $penitipanList->count(),
|
|
'detail' => $penitipanList,
|
|
]);
|
|
}
|
|
|
|
public function countByDate(Request $request)
|
|
{
|
|
$request->validate([
|
|
'tanggal' => 'required|date',
|
|
]);
|
|
|
|
$tanggal = Carbon::parse($request->tanggal)->toDateString();
|
|
|
|
$penitipanList = Penitipan::whereDate('waktu_selesai', $tanggal)->get();
|
|
|
|
$total = 0;
|
|
foreach ($penitipanList as $item) {
|
|
$total += $item->biaya ?? 0; // jika biaya null, anggap 0
|
|
}
|
|
|
|
return response()->json([
|
|
'tanggal' => $tanggal,
|
|
'total_penghasilan' => $total,
|
|
'jumlah_transaksi' => $penitipanList->count(),
|
|
'detail' => $penitipanList,
|
|
]);
|
|
}
|
|
|
|
}
|