Browse Source

update API Peminjaman,pengembalian dan transfer

master
unknown 1 year ago
parent
commit
1ac81b6cad
  1. 135
      app/Http/Controllers/API/v1/PeminjamanApi/PeminjamanApiController.php
  2. 70
      app/Http/Controllers/API/v1/PengembalianApi/PengembalianBarangApiController.php
  3. 58
      app/Http/Controllers/API/v1/TransferApi/TransferApiController.php

135
app/Http/Controllers/API/v1/PeminjamanApi/PeminjamanApiController.php

@ -2,11 +2,14 @@
namespace App\Http\Controllers\API\v1\PeminjamanApi; namespace App\Http\Controllers\API\v1\PeminjamanApi;
use App\Models\BarangMasuk;
use App\Models\asset_status;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Helpers\ResponseFormatter; use App\Helpers\ResponseFormatter;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\asset_status; use Illuminate\Database\QueryException;
use App\Models\BarangMasuk;
class PeminjamanApiController extends Controller class PeminjamanApiController extends Controller
{ {
@ -54,8 +57,61 @@ 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) public function store(Request $request)
{ {
try {
DB::beginTransaction();
$mobile_id = $request->input('mobile_id'); $mobile_id = $request->input('mobile_id');
$peti_id = $request->input('peti_id'); $peti_id = $request->input('peti_id');
$customer_id = $request->input('customer_id'); $customer_id = $request->input('customer_id');
@ -64,7 +120,6 @@ class PeminjamanApiController extends Controller
$exit_pic = $request->input('exit_pic'); $exit_pic = $request->input('exit_pic');
$exit_warehouse = $request->input('exit_warehouse'); $exit_warehouse = $request->input('exit_warehouse');
$est_pengembalian = $request->input('est_pengembalian'); $est_pengembalian = $request->input('est_pengembalian');
// $status = $request->input('status');
$created_by = $request->input('created_by'); $created_by = $request->input('created_by');
$created_at = $request->input('created_at'); $created_at = $request->input('created_at');
@ -73,35 +128,61 @@ class PeminjamanApiController extends Controller
->first(); ->first();
if ($existingRecord) { if ($existingRecord) {
$existingRecord->exit_at = $exit_at; $data = [
$existingRecord->exit_pic = $exit_pic; 'exit_at' => $exit_at,
$existingRecord->exit_warehouse = $exit_warehouse; 'exit_pic' => $exit_pic,
$existingRecord->est_pengembalian = $est_pengembalian; 'exit_warehouse' => $exit_warehouse,
// $existingRecord->status = $status; 'est_pengembalian' => $est_pengembalian,
$existingRecord->created_by = $created_by; 'created_by' => $created_by,
$existingRecord->created_at = $created_at; 'created_at' => $created_at,
$existingRecord->save(); ];
asset_status::where('mobile_id', $mobile_id)
->where('peti_id', $peti_id)
->update($data);
DB::commit();
return ResponseFormatter::success([ return ResponseFormatter::success([
'message' => 'Data peminjaman berhasil ditambahkan', 'message' => 'Data peminjaman berhasil diupdate',
'peminjam' => $existingRecord 'peminjam' => asset_status::where('mobile_id', $mobile_id)
->where('peti_id', $peti_id)
->first(),
]); ]);
} else { } else {
$newRecord = new asset_status; $data = [
$newRecord->mobile_id = $mobile_id; 'mobile_id' => $mobile_id,
$newRecord->peti_id = $peti_id; 'peti_id' => $peti_id,
$newRecord->customer_id = $customer_id; 'customer_id' => $customer_id,
$newRecord->warehouse_id = $warehouse_id; 'warehouse_id' => $warehouse_id,
$newRecord->exit_at = $exit_at; 'exit_at' => $exit_at,
$newRecord->exit_pic = $exit_pic; 'exit_pic' => $exit_pic,
$newRecord->exit_warehouse = $exit_warehouse; 'exit_warehouse' => $exit_warehouse,
$newRecord->est_pengembalian = $est_pengembalian; 'est_pengembalian' => $est_pengembalian,
// $newRecord->status = $status; 'created_by' => $created_by,
$newRecord->created_by = $created_by; 'created_at' => $created_at,
$newRecord->created_at = $created_at; ];
$newRecord->save();
asset_status::create($data);
DB::commit();
return ResponseFormatter::success([ return ResponseFormatter::success([
'message' => 'Data peminjaman berhasil ditambahkan', 'message' => 'Data peminjaman berhasil ditambahkan',
'peminjam' => $newRecord '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()
]); ]);
} }
} }

70
app/Http/Controllers/API/v1/PengembalianApi/PengembalianBarangApiController.php

@ -4,7 +4,10 @@ namespace App\Http\Controllers\API\v1\PengembalianApi;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Helpers\ResponseFormatter; use App\Helpers\ResponseFormatter;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Database\QueryException;
class PengembalianBarangApiController extends Controller class PengembalianBarangApiController extends Controller
{ {
@ -62,23 +65,27 @@ class PengembalianBarangApiController extends Controller
]); ]);
} }
public function update(Request $request) public function update(Request $request)
{ {
try {
DB::beginTransaction();
$petiId = $request->peti_id; $petiId = $request->peti_id;
// Cari data terakhir dengan kondisi peti_id dan status 0
$lastPetiData = \App\Models\asset_status::where('peti_id', $petiId) $lastPetiData = \App\Models\asset_status::where('peti_id', $petiId)
->where('status', 0) // Hanya data dengan status 0 ->where('status', 0)
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->first(); ->first();
if (!$lastPetiData) { if (!$lastPetiData) {
DB::rollBack();
return ResponseFormatter::error([ return ResponseFormatter::error([
'message' => 'Tidak ada data terakhir dengan peti_id, status 0 yang memenuhi kriteria', 'message' => 'Tidak ada data terakhir dengan peti_id, status 0 yang memenuhi kriteria',
], 'Data Not Found', 404); ], 'Data Not Found', 404);
} }
// Lakukan pembaruan hanya jika status-nya 0
if ($lastPetiData->status == 0) { if ($lastPetiData->status == 0) {
$lastPetiData->update([ $lastPetiData->update([
'enter_at' => $request->enter_at, 'enter_at' => $request->enter_at,
@ -91,14 +98,71 @@ class PengembalianBarangApiController extends Controller
// Ubah status menjadi 1 setelah pembaruan berhasil // Ubah status menjadi 1 setelah pembaruan berhasil
$lastPetiData->update(['status' => 1]); $lastPetiData->update(['status' => 1]);
DB::commit();
return ResponseFormatter::success([ return ResponseFormatter::success([
'message' => 'Data pengembalian berhasil diupdate', 'message' => 'Data pengembalian berhasil diupdate',
'asset_status' => $lastPetiData 'asset_status' => $lastPetiData
]); ]);
} else { } else {
DB::rollBack();
return ResponseFormatter::error([ return ResponseFormatter::error([
'message' => 'Data tidak dapat diupdate karena status bukan 0', 'message' => 'Data tidak dapat diupdate karena status bukan 0',
], 'Invalid Status', 400); ], '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);
// }
// }
} }

58
app/Http/Controllers/API/v1/TransferApi/TransferApiController.php

@ -3,9 +3,13 @@
namespace App\Http\Controllers\API\v1\TransferApi; namespace App\Http\Controllers\API\v1\TransferApi;
use App\Models\Transfer; use App\Models\Transfer;
use App\Models\Type_peti;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Helpers\ResponseFormatter; use App\Helpers\ResponseFormatter;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Database\QueryException;
class TransferApiController extends Controller class TransferApiController extends Controller
{ {
@ -21,8 +25,48 @@ 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) public function store(Request $request)
{ {
try {
DB::beginTransaction();
$mobile_id = $request->input('mobile_id'); $mobile_id = $request->input('mobile_id');
$peti_id = $request->input('peti_id'); $peti_id = $request->input('peti_id');
$name_customer = $request->input('name_customer'); $name_customer = $request->input('name_customer');
@ -43,17 +87,23 @@ class TransferApiController extends Controller
'created_at' => $created_at 'created_at' => $created_at
]); ]);
if ($transfer) { DB::commit();
return ResponseFormatter::success([ return ResponseFormatter::success([
'status' => true, 'status' => true,
'message' => 'Data Transfer Peti berhasil ditambahkan', 'message' => 'Data Transfer Peti berhasil ditambahkan',
'transfer_peti' => $transfer '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([ return ResponseFormatter::error([
'status' => false, 'status' => false,
'message' => 'Data Transfer Peti gagal ditambahkan', 'message' => 'Error while processing the request.',
'transfer_peti' => $transfer 'error' => $e->getMessage()
]); ]);
} }
} }

Loading…
Cancel
Save