diff --git a/app/Http/Controllers/API/v1/PeminjamanApi/PeminjamanApiController.php b/app/Http/Controllers/API/v1/PeminjamanApi/PeminjamanApiController.php index b7de3cc..6614f08 100644 --- a/app/Http/Controllers/API/v1/PeminjamanApi/PeminjamanApiController.php +++ b/app/Http/Controllers/API/v1/PeminjamanApi/PeminjamanApiController.php @@ -2,11 +2,14 @@ namespace App\Http\Controllers\API\v1\PeminjamanApi; +use App\Models\BarangMasuk; +use App\Models\asset_status; use Illuminate\Http\Request; use App\Helpers\ResponseFormatter; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use App\Http\Controllers\Controller; -use App\Models\asset_status; -use App\Models\BarangMasuk; +use Illuminate\Database\QueryException; class PeminjamanApiController extends Controller { @@ -54,54 +57,132 @@ class PeminjamanApiController extends Controller ]); } + // public function store(Request $request) + // { + // $mobile_id = $request->input('mobile_id'); + // $peti_id = $request->input('peti_id'); + // $customer_id = $request->input('customer_id'); + // $warehouse_id = $request->input('warehouse_id'); + // $exit_at = $request->input('exit_at'); + // $exit_pic = $request->input('exit_pic'); + // $exit_warehouse = $request->input('exit_warehouse'); + // $est_pengembalian = $request->input('est_pengembalian'); + // $created_by = $request->input('created_by'); + // $created_at = $request->input('created_at'); + + // $existingRecord = asset_status::where('mobile_id', $mobile_id) + // ->where('peti_id', $peti_id) + // ->first(); + + // if ($existingRecord) { + // $existingRecord->exit_at = $exit_at; + // $existingRecord->exit_pic = $exit_pic; + // $existingRecord->exit_warehouse = $exit_warehouse; + // $existingRecord->est_pengembalian = $est_pengembalian; + // $existingRecord->created_by = $created_by; + // $existingRecord->created_at = $created_at; + // $existingRecord->save(); + // return ResponseFormatter::success([ + // 'message' => 'Data peminjaman berhasil ditambahkan', + // 'peminjam' => $existingRecord + // ]); + // } else { + // $newRecord = new asset_status; + // $newRecord->mobile_id = $mobile_id; + // $newRecord->peti_id = $peti_id; + // $newRecord->customer_id = $customer_id; + // $newRecord->warehouse_id = $warehouse_id; + // $newRecord->exit_at = $exit_at; + // $newRecord->exit_pic = $exit_pic; + // $newRecord->exit_warehouse = $exit_warehouse; + // $newRecord->est_pengembalian = $est_pengembalian; + // $newRecord->created_by = $created_by; + // $newRecord->created_at = $created_at; + // $newRecord->save(); + // return ResponseFormatter::success([ + // 'message' => 'Data peminjaman berhasil ditambahkan', + // 'peminjam' => $newRecord + // ]); + // } + // } + + public function store(Request $request) { - $mobile_id = $request->input('mobile_id'); - $peti_id = $request->input('peti_id'); - $customer_id = $request->input('customer_id'); - $warehouse_id = $request->input('warehouse_id'); - $exit_at = $request->input('exit_at'); - $exit_pic = $request->input('exit_pic'); - $exit_warehouse = $request->input('exit_warehouse'); - $est_pengembalian = $request->input('est_pengembalian'); - // $status = $request->input('status'); - $created_by = $request->input('created_by'); - $created_at = $request->input('created_at'); - - $existingRecord = asset_status::where('mobile_id', $mobile_id) - ->where('peti_id', $peti_id) - ->first(); - - if ($existingRecord) { - $existingRecord->exit_at = $exit_at; - $existingRecord->exit_pic = $exit_pic; - $existingRecord->exit_warehouse = $exit_warehouse; - $existingRecord->est_pengembalian = $est_pengembalian; - // $existingRecord->status = $status; - $existingRecord->created_by = $created_by; - $existingRecord->created_at = $created_at; - $existingRecord->save(); - return ResponseFormatter::success([ - 'message' => 'Data peminjaman berhasil ditambahkan', - 'peminjam' => $existingRecord - ]); - } else { - $newRecord = new asset_status; - $newRecord->mobile_id = $mobile_id; - $newRecord->peti_id = $peti_id; - $newRecord->customer_id = $customer_id; - $newRecord->warehouse_id = $warehouse_id; - $newRecord->exit_at = $exit_at; - $newRecord->exit_pic = $exit_pic; - $newRecord->exit_warehouse = $exit_warehouse; - $newRecord->est_pengembalian = $est_pengembalian; - // $newRecord->status = $status; - $newRecord->created_by = $created_by; - $newRecord->created_at = $created_at; - $newRecord->save(); - return ResponseFormatter::success([ - 'message' => 'Data peminjaman berhasil ditambahkan', - 'peminjam' => $newRecord + try { + DB::beginTransaction(); + + $mobile_id = $request->input('mobile_id'); + $peti_id = $request->input('peti_id'); + $customer_id = $request->input('customer_id'); + $warehouse_id = $request->input('warehouse_id'); + $exit_at = $request->input('exit_at'); + $exit_pic = $request->input('exit_pic'); + $exit_warehouse = $request->input('exit_warehouse'); + $est_pengembalian = $request->input('est_pengembalian'); + $created_by = $request->input('created_by'); + $created_at = $request->input('created_at'); + + $existingRecord = asset_status::where('mobile_id', $mobile_id) + ->where('peti_id', $peti_id) + ->first(); + + if ($existingRecord) { + $data = [ + 'exit_at' => $exit_at, + 'exit_pic' => $exit_pic, + 'exit_warehouse' => $exit_warehouse, + 'est_pengembalian' => $est_pengembalian, + 'created_by' => $created_by, + 'created_at' => $created_at, + ]; + + asset_status::where('mobile_id', $mobile_id) + ->where('peti_id', $peti_id) + ->update($data); + + DB::commit(); + + return ResponseFormatter::success([ + 'message' => 'Data peminjaman berhasil diupdate', + 'peminjam' => asset_status::where('mobile_id', $mobile_id) + ->where('peti_id', $peti_id) + ->first(), + ]); + } else { + $data = [ + 'mobile_id' => $mobile_id, + 'peti_id' => $peti_id, + 'customer_id' => $customer_id, + 'warehouse_id' => $warehouse_id, + 'exit_at' => $exit_at, + 'exit_pic' => $exit_pic, + 'exit_warehouse' => $exit_warehouse, + 'est_pengembalian' => $est_pengembalian, + 'created_by' => $created_by, + 'created_at' => $created_at, + ]; + + asset_status::create($data); + + DB::commit(); + + return ResponseFormatter::success([ + 'message' => 'Data peminjaman berhasil ditambahkan', + 'peminjam' => asset_status::where('mobile_id', $mobile_id) + ->where('peti_id', $peti_id) + ->first(), + ]); + } + } catch (QueryException $e) { + DB::rollBack(); + + // Log the error for further investigation + Log::error('Error processing request: ' . $e->getMessage()); + + return ResponseFormatter::error([ + 'message' => 'Error while processing the request.', + 'error' => $e->getMessage() ]); } } diff --git a/app/Http/Controllers/API/v1/PengembalianApi/PengembalianBarangApiController.php b/app/Http/Controllers/API/v1/PengembalianApi/PengembalianBarangApiController.php index 411457b..6e1bf33 100644 --- a/app/Http/Controllers/API/v1/PengembalianApi/PengembalianBarangApiController.php +++ b/app/Http/Controllers/API/v1/PengembalianApi/PengembalianBarangApiController.php @@ -4,7 +4,10 @@ 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 { @@ -62,43 +65,104 @@ class PengembalianBarangApiController extends Controller ]); } + 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(); + 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()); - if (!$lastPetiData) { return ResponseFormatter::error([ - 'message' => 'Tidak ada data terakhir dengan peti_id, status 0 yang memenuhi kriteria', - ], 'Data Not Found', 404); + 'message' => 'Error while processing the request.', + 'error' => $e->getMessage() + ]); } + } - // 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, - ]); + // 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); + // } + // } - // 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); - } - } } diff --git a/app/Http/Controllers/API/v1/TransferApi/TransferApiController.php b/app/Http/Controllers/API/v1/TransferApi/TransferApiController.php index c31d5ef..755875e 100644 --- a/app/Http/Controllers/API/v1/TransferApi/TransferApiController.php +++ b/app/Http/Controllers/API/v1/TransferApi/TransferApiController.php @@ -3,9 +3,13 @@ namespace App\Http\Controllers\API\v1\TransferApi; use App\Models\Transfer; +use App\Models\Type_peti; 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 TransferApiController extends Controller { @@ -21,39 +25,85 @@ class TransferApiController extends Controller ]); } + // public function store(Request $request) + // { + // $mobile_id = $request->input('mobile_id'); + // $peti_id = $request->input('peti_id'); + // $name_customer = $request->input('name_customer'); + // $source_warehouse = $request->input('source_warehouse'); + // $destination_warehouse = $request->input('destination_warehouse'); + // $transfer_date = $request->input('date'); + // $created_by = $request->input('created_by'); + // $created_at = $request->input('created_at'); + + // $transfer = Transfer::create([ + // 'mobile_id' => $mobile_id, + // 'peti_id' => $peti_id, + // 'name_customer' => $name_customer, + // 'source_warehouse' => $source_warehouse, + // 'destination_warehouse' => $destination_warehouse, + // 'date' => $transfer_date, + // 'created_by' => $created_by, + // 'created_at' => $created_at + // ]); + + // if ($transfer) { + // return ResponseFormatter::success([ + // 'status' => true, + // 'message' => 'Data Transfer Peti berhasil ditambahkan', + // 'transfer_peti' => $transfer + // ]); + // } else { + // return ResponseFormatter::error([ + // 'status' => false, + // 'message' => 'Data Transfer Peti gagal ditambahkan', + // 'transfer_peti' => $transfer + // ]); + // } + // } + public function store(Request $request) { - $mobile_id = $request->input('mobile_id'); - $peti_id = $request->input('peti_id'); - $name_customer = $request->input('name_customer'); - $source_warehouse = $request->input('source_warehouse'); - $destination_warehouse = $request->input('destination_warehouse'); - $transfer_date = $request->input('date'); - $created_by = $request->input('created_by'); - $created_at = $request->input('created_at'); - - $transfer = Transfer::create([ - 'mobile_id' => $mobile_id, - 'peti_id' => $peti_id, - 'name_customer' => $name_customer, - 'source_warehouse' => $source_warehouse, - 'destination_warehouse' => $destination_warehouse, - 'date' => $transfer_date, - 'created_by' => $created_by, - 'created_at' => $created_at - ]); + try { + DB::beginTransaction(); + + $mobile_id = $request->input('mobile_id'); + $peti_id = $request->input('peti_id'); + $name_customer = $request->input('name_customer'); + $source_warehouse = $request->input('source_warehouse'); + $destination_warehouse = $request->input('destination_warehouse'); + $transfer_date = $request->input('date'); + $created_by = $request->input('created_by'); + $created_at = $request->input('created_at'); + + $transfer = Transfer::create([ + 'mobile_id' => $mobile_id, + 'peti_id' => $peti_id, + 'name_customer' => $name_customer, + 'source_warehouse' => $source_warehouse, + 'destination_warehouse' => $destination_warehouse, + 'date' => $transfer_date, + 'created_by' => $created_by, + 'created_at' => $created_at + ]); + + DB::commit(); - if ($transfer) { return ResponseFormatter::success([ 'status' => true, 'message' => 'Data Transfer Peti berhasil ditambahkan', 'transfer_peti' => $transfer ]); - } else { + } catch (QueryException $e) { + DB::rollBack(); + + // Log the error for further investigation + Log::error('Error storing Transfer data: ' . $e->getMessage()); + return ResponseFormatter::error([ 'status' => false, - 'message' => 'Data Transfer Peti gagal ditambahkan', - 'transfer_peti' => $transfer + 'message' => 'Error while processing the request.', + 'error' => $e->getMessage() ]); } }