|
|
|
<?php
|
|
|
|
|
|
|
|
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 Illuminate\Database\QueryException;
|
|
|
|
|
|
|
|
class PeminjamanApiController extends Controller
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$peminjaman = \App\Models\asset_status::with([
|
|
|
|
'peti.customer:id,name,code_customer,lot_no,address',
|
|
|
|
// 'peti.customer',
|
|
|
|
'peti.tipe_peti:id,type,size_peti,description',
|
|
|
|
'warehouse',
|
|
|
|
'warehouseEnter'
|
|
|
|
])->get();
|
|
|
|
|
|
|
|
// Menghapus atribut yang terkait dengan "enter"
|
|
|
|
foreach ($peminjaman as $peminjamanItem) {
|
|
|
|
unset($peminjamanItem->enter_at);
|
|
|
|
unset($peminjamanItem->enter_pic);
|
|
|
|
unset($peminjamanItem->enter_warehouse);
|
|
|
|
unset($peminjamanItem->kondisi_peti);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResponseFormatter::success([
|
|
|
|
'message' => 'Data peminjaman berhasil diambil',
|
|
|
|
'asset_status' => $peminjaman,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getWarehouseEnter()
|
|
|
|
{
|
|
|
|
$warehouse = \App\Models\m_warehouse::get();
|
|
|
|
|
|
|
|
return ResponseFormatter::success([
|
|
|
|
'message' => 'Data warehouse berhasil diambil',
|
|
|
|
'warehouse' => $warehouse,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$peminjaman = asset_status::with(['peti.customer', 'peti.tipe_peti', 'warehouse'])->find($id);
|
|
|
|
|
|
|
|
return ResponseFormatter::success([
|
|
|
|
'message' => 'Data peminjaman berhasil diambil ID',
|
|
|
|
'asset_status' => $peminjaman
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|