|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API\v1\PengembalianApi;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Helpers\ResponseFormatter;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
|
|
|
|
class PengembalianBarangApiController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$peminjaman = \App\Models\asset_status::with([
|
|
|
|
'peti.customer:id,name,code_customer,lot_no,nip,no_hp,tgl_lahir,jenis_kelamin,agama,address',
|
|
|
|
'peti.tipe_peti:id,type,size_peti,description',
|
|
|
|
'warehouse',
|
|
|
|
'warehouseEnter'
|
|
|
|
])->get();
|
|
|
|
|
|
|
|
return ResponseFormatter::success([
|
|
|
|
'message' => 'Data peminjaman berhasil diambil',
|
|
|
|
'asset_status' => $peminjaman,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$pengembalian = \App\Models\asset_status::with([
|
|
|
|
'peti.customer:id,name,code_customer,lot_no,nip,no_hp,tgl_lahir,jenis_kelamin,agama,address',
|
|
|
|
'peti.tipe_peti:id,type,size_peti,description',
|
|
|
|
'warehouse',
|
|
|
|
'warehouseEnter'
|
|
|
|
])
|
|
|
|
->where('updated_by', '=', null)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return ResponseFormatter::success([
|
|
|
|
'message' => 'Data peminjaman berhasil diambil',
|
|
|
|
'asset_status' => $pengembalian,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$pengembalian = \App\Models\asset_status::with([
|
|
|
|
'peti.customer:id,name,code_customer,lot_no,nip,no_hp,tgl_lahir,jenis_kelamin,agama,address',
|
|
|
|
'peti.tipe_peti:id,type,size_peti,description',
|
|
|
|
'warehouse',
|
|
|
|
'warehouseEnter'
|
|
|
|
])->find($id);
|
|
|
|
|
|
|
|
if (!$pengembalian) {
|
|
|
|
return ResponseFormatter::error([
|
|
|
|
'message' => 'Data pengembalian tidak ditemukan',
|
|
|
|
], 'Data Not Found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResponseFormatter::success([
|
|
|
|
'message' => 'Data pengembalian berhasil diambil',
|
|
|
|
'asset_status' => $pengembalian,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function update(Request $request)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
$petiId = $request->peti_id;
|
|
|
|
|
|
|
|
$lastPetiData = \App\Models\asset_status::where('peti_id', $petiId)
|
|
|
|
->where('status', 0)
|
|
|
|
->orderBy('id', 'desc')
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if (!$lastPetiData) {
|
|
|
|
DB::rollBack();
|
|
|
|
|
|
|
|
return ResponseFormatter::error([
|
|
|
|
'message' => 'Tidak ada data terakhir dengan peti_id, status 0 yang memenuhi kriteria',
|
|
|
|
], 'Data Not Found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lastPetiData->status == 0) {
|
|
|
|
$lastPetiData->update([
|
|
|
|
'enter_at' => $request->enter_at,
|
|
|
|
'enter_pic' => $request->enter_pic,
|
|
|
|
'enter_warehouse' => $request->enter_warehouse,
|
|
|
|
'kondisi_peti_id' => $request->kondisi_peti_id,
|
|
|
|
'updated_by' => $request->updated_by,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Ubah status menjadi 1 setelah pembaruan berhasil
|
|
|
|
$lastPetiData->update(['status' => 1]);
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
|
|
|
|
return ResponseFormatter::success([
|
|
|
|
'message' => 'Data pengembalian berhasil diupdate',
|
|
|
|
'asset_status' => $lastPetiData
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
DB::rollBack();
|
|
|
|
|
|
|
|
return ResponseFormatter::error([
|
|
|
|
'message' => 'Data tidak dapat diupdate karena status bukan 0',
|
|
|
|
], 'Invalid Status', 400);
|
|
|
|
}
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
|
|
|
|
// Log the error for further investigation
|
|
|
|
Log::error('Error updating data: ' . $e->getMessage());
|
|
|
|
|
|
|
|
return ResponseFormatter::error([
|
|
|
|
'message' => 'Error while processing the request.',
|
|
|
|
'error' => $e->getMessage()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// public function update(Request $request)
|
|
|
|
// {
|
|
|
|
// $petiId = $request->peti_id;
|
|
|
|
|
|
|
|
// // Cari data terakhir dengan kondisi peti_id dan status 0
|
|
|
|
// $lastPetiData = \App\Models\asset_status::where('peti_id', $petiId)
|
|
|
|
// ->where('status', 0) // Hanya data dengan status 0
|
|
|
|
// ->orderBy('id', 'desc')
|
|
|
|
// ->first();
|
|
|
|
|
|
|
|
// if (!$lastPetiData) {
|
|
|
|
// return ResponseFormatter::error([
|
|
|
|
// 'message' => 'Tidak ada data terakhir dengan peti_id, status 0 yang memenuhi kriteria',
|
|
|
|
// ], 'Data Not Found', 404);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Lakukan pembaruan hanya jika status-nya 0
|
|
|
|
// if ($lastPetiData->status == 0) {
|
|
|
|
// $lastPetiData->update([
|
|
|
|
// 'enter_at' => $request->enter_at,
|
|
|
|
// 'enter_pic' => $request->enter_pic,
|
|
|
|
// 'enter_warehouse' => $request->enter_warehouse,
|
|
|
|
// 'kondisi_peti_id' => $request->kondisi_peti_id,
|
|
|
|
// 'updated_by' => $request->updated_by,
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// // Ubah status menjadi 1 setelah pembaruan berhasil
|
|
|
|
// $lastPetiData->update(['status' => 1]);
|
|
|
|
|
|
|
|
// return ResponseFormatter::success([
|
|
|
|
// 'message' => 'Data pengembalian berhasil diupdate',
|
|
|
|
// 'asset_status' => $lastPetiData
|
|
|
|
// ]);
|
|
|
|
// } else {
|
|
|
|
// return ResponseFormatter::error([
|
|
|
|
// 'message' => 'Data tidak dapat diupdate karena status bukan 0',
|
|
|
|
// ], 'Invalid Status', 400);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
}
|