Browse Source

update API Peminjaman,pengembalian dan transfer

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

177
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,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) public function store(Request $request)
{ {
$mobile_id = $request->input('mobile_id'); try {
$peti_id = $request->input('peti_id'); DB::beginTransaction();
$customer_id = $request->input('customer_id');
$warehouse_id = $request->input('warehouse_id'); $mobile_id = $request->input('mobile_id');
$exit_at = $request->input('exit_at'); $peti_id = $request->input('peti_id');
$exit_pic = $request->input('exit_pic'); $customer_id = $request->input('customer_id');
$exit_warehouse = $request->input('exit_warehouse'); $warehouse_id = $request->input('warehouse_id');
$est_pengembalian = $request->input('est_pengembalian'); $exit_at = $request->input('exit_at');
// $status = $request->input('status'); $exit_pic = $request->input('exit_pic');
$created_by = $request->input('created_by'); $exit_warehouse = $request->input('exit_warehouse');
$created_at = $request->input('created_at'); $est_pengembalian = $request->input('est_pengembalian');
$created_by = $request->input('created_by');
$existingRecord = asset_status::where('mobile_id', $mobile_id) $created_at = $request->input('created_at');
->where('peti_id', $peti_id)
->first(); $existingRecord = asset_status::where('mobile_id', $mobile_id)
->where('peti_id', $peti_id)
if ($existingRecord) { ->first();
$existingRecord->exit_at = $exit_at;
$existingRecord->exit_pic = $exit_pic; if ($existingRecord) {
$existingRecord->exit_warehouse = $exit_warehouse; $data = [
$existingRecord->est_pengembalian = $est_pengembalian; 'exit_at' => $exit_at,
// $existingRecord->status = $status; 'exit_pic' => $exit_pic,
$existingRecord->created_by = $created_by; 'exit_warehouse' => $exit_warehouse,
$existingRecord->created_at = $created_at; 'est_pengembalian' => $est_pengembalian,
$existingRecord->save(); 'created_by' => $created_by,
return ResponseFormatter::success([ 'created_at' => $created_at,
'message' => 'Data peminjaman berhasil ditambahkan', ];
'peminjam' => $existingRecord
]); asset_status::where('mobile_id', $mobile_id)
} else { ->where('peti_id', $peti_id)
$newRecord = new asset_status; ->update($data);
$newRecord->mobile_id = $mobile_id;
$newRecord->peti_id = $peti_id; DB::commit();
$newRecord->customer_id = $customer_id;
$newRecord->warehouse_id = $warehouse_id; return ResponseFormatter::success([
$newRecord->exit_at = $exit_at; 'message' => 'Data peminjaman berhasil diupdate',
$newRecord->exit_pic = $exit_pic; 'peminjam' => asset_status::where('mobile_id', $mobile_id)
$newRecord->exit_warehouse = $exit_warehouse; ->where('peti_id', $peti_id)
$newRecord->est_pengembalian = $est_pengembalian; ->first(),
// $newRecord->status = $status; ]);
$newRecord->created_by = $created_by; } else {
$newRecord->created_at = $created_at; $data = [
$newRecord->save(); 'mobile_id' => $mobile_id,
return ResponseFormatter::success([ 'peti_id' => $peti_id,
'message' => 'Data peminjaman berhasil ditambahkan', 'customer_id' => $customer_id,
'peminjam' => $newRecord '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()
]); ]);
} }
} }

126
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,43 +65,104 @@ class PengembalianBarangApiController extends Controller
]); ]);
} }
public function update(Request $request) public function update(Request $request)
{ {
$petiId = $request->peti_id; try {
DB::beginTransaction();
// Cari data terakhir dengan kondisi peti_id dan status 0
$lastPetiData = \App\Models\asset_status::where('peti_id', $petiId) $petiId = $request->peti_id;
->where('status', 0) // Hanya data dengan status 0
->orderBy('id', 'desc') $lastPetiData = \App\Models\asset_status::where('peti_id', $petiId)
->first(); ->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([ return ResponseFormatter::error([
'message' => 'Tidak ada data terakhir dengan peti_id, status 0 yang memenuhi kriteria', 'message' => 'Error while processing the request.',
], 'Data Not Found', 404); 'error' => $e->getMessage()
]);
} }
}
// Lakukan pembaruan hanya jika status-nya 0 // public function update(Request $request)
if ($lastPetiData->status == 0) { // {
$lastPetiData->update([ // $petiId = $request->peti_id;
'enter_at' => $request->enter_at,
'enter_pic' => $request->enter_pic, // // Cari data terakhir dengan kondisi peti_id dan status 0
'enter_warehouse' => $request->enter_warehouse, // $lastPetiData = \App\Models\asset_status::where('peti_id', $petiId)
'kondisi_peti_id' => $request->kondisi_peti_id, // ->where('status', 0) // Hanya data dengan status 0
'updated_by' => $request->updated_by, // ->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);
}
}
} }

96
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,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) public function store(Request $request)
{ {
$mobile_id = $request->input('mobile_id'); try {
$peti_id = $request->input('peti_id'); DB::beginTransaction();
$name_customer = $request->input('name_customer');
$source_warehouse = $request->input('source_warehouse'); $mobile_id = $request->input('mobile_id');
$destination_warehouse = $request->input('destination_warehouse'); $peti_id = $request->input('peti_id');
$transfer_date = $request->input('date'); $name_customer = $request->input('name_customer');
$created_by = $request->input('created_by'); $source_warehouse = $request->input('source_warehouse');
$created_at = $request->input('created_at'); $destination_warehouse = $request->input('destination_warehouse');
$transfer_date = $request->input('date');
$transfer = Transfer::create([ $created_by = $request->input('created_by');
'mobile_id' => $mobile_id, $created_at = $request->input('created_at');
'peti_id' => $peti_id,
'name_customer' => $name_customer, $transfer = Transfer::create([
'source_warehouse' => $source_warehouse, 'mobile_id' => $mobile_id,
'destination_warehouse' => $destination_warehouse, 'peti_id' => $peti_id,
'date' => $transfer_date, 'name_customer' => $name_customer,
'created_by' => $created_by, 'source_warehouse' => $source_warehouse,
'created_at' => $created_at 'destination_warehouse' => $destination_warehouse,
]); 'date' => $transfer_date,
'created_by' => $created_by,
'created_at' => $created_at
]);
DB::commit();
if ($transfer) {
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