Siopas Inventory PETI for ISTW Website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

171 lines
5.3 KiB

<?php
namespace App\Http\Controllers;
use App\Models\Peti;
use Symfony\Component\Uid\Uuid;
use App\Models\Customer;
use App\Models\Transfer;
use App\Models\m_warehouse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\Transfer\ValidasiCreateTransfer;
use App\Http\Requests\Transfer\ValidasiUpdateTransfer;
class TransferController extends Controller
{
public function index()
{
$transfers = Transfer::orderBy('created_at', 'desc')->get();
$latestTransfers = $transfers->groupBy('peti_id')->map(function ($group) {
return $group->first();
});
$data = [
'transfer' => $latestTransfers,
'active' => 'menu-transfer',
];
return view('dashboard.Transfer.index', $data);
}
// public function index()
// {
// $data = [
// 'transfer' => Transfer::orderBy('created_at', 'desc')->get(),
// 'active' => 'menu-transfer',
// ];
// return view('dashboard.Transfer.index', $data);
// }
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data = [
'customer' => Customer::get(),
'peti' => Peti::get(),
// 'warehouse' => Peti::with('warehouse_id')->get(),
'warehouse' => m_warehouse::get(),
'active' => 'menu-transfer',
];
return view('dashboard.Transfer.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(ValidasiCreateTransfer $request)
{
try {
// Mendapatkan informasi pengguna yang sedang login
$currentUser = Auth::user();
// Validasi data dari request
$validatedData = $request->validated();
// Menambahkan informasi pengguna ke dalam data yang akan disimpan
$validatedData['mobile_id'] = Uuid::v4(); // Menambahkan ID Mobile
$validatedData['created_by'] = $currentUser->fullname;
$validatedData['updated_by'] = $currentUser->fullname;
// Membuat entitas Transfer
$transfer = Transfer::create($validatedData);
// dd($transfer);
// Mengupdate warehouse_id pada model Peti
Peti::where('id', $transfer->peti_id)
->update([
'warehouse_id' => $transfer->destination_warehouse,
'customer_id' => $transfer->name_customer, // tambahan update customer id
]);
return redirect()->route('dashboard.transfer.index')->with('success', 'Data Transfer Peti berhasil ditambahkan');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data Transfer Peti gagal ditransfer');
}
}
/**
* Display the specified resource.
*/
// public function show($id)
// {
// $data = [
// 'active' => 'menu-transfer',
// ];
// return view('dashboard.Transfer.show', $data);
// }
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data = [
'transfer' => Transfer::findOrFail($id),
'peti' => Peti::get(),
'customer' => Customer::get(),
'warehouse' => m_warehouse::get(),
'active' => 'menu-transfer',
];
return view('dashboard.Transfer.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(ValidasiUpdateTransfer $request, $id)
{
// dd("oke");
try {
// Mendapatkan informasi pengguna yang sedang login
$currentUser = Auth::user();
// Validasi data dari request
$validatedData = $request->validated();
// Menambahkan informasi pengguna ke dalam data yang akan disimpan
$validatedData['updated_by'] = $currentUser->fullname;
// Mencari transfer berdasarkan ID
$transfer = Transfer::find($id);
if (!$transfer) {
return redirect()->route('dashboard.transfer.index')->with('error', 'Data Transfer Peti tidak ditemukan');
}
// Memperbarui data transfer
$transfer->update($validatedData);
// Mengupdate warehouse_id pada model Peti
Peti::where('id', $transfer->peti_id)
->update([
'warehouse_id' => $transfer->destination_warehouse,
'customer_id' => $transfer->name_customer,
]);
return redirect()->route('dashboard.transfer.index')->with('success', 'Data Transfer Peti berhasil diperbaharui');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data Transfer Peti gagal diperbaharui');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
// dd("oke");
try {
$typetransfer = Transfer::findOrFail($id);
$typetransfer->delete();
return redirect()->back()->with('success', 'Data transfer peti berhasil dihapus');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data transfer peti gagal dihapus');
}
}
}