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.

238 lines
8.7 KiB

1 year ago
<?php
namespace App\Http\Controllers;
use App\Models\Peti;
use App\Models\Customer;
use App\Models\m_warehouse;
use App\Models\asset_status;
use App\Models\Kondisi_Peti;
use Illuminate\Http\Request;
use Symfony\Component\Uid\Uuid;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\ValidasiCreatePeminjaman;
use App\Http\Requests\ValidasiUpdatePeminjaman;
1 year ago
class PeminjamanController extends Controller
{
1 year ago
/**
* Display a listing of the resource.
*/
public function index(Request $request)
1 year ago
{
$perPage = $request->input('perPage', 5);
$query = asset_status::with(['customer', 'warehouseId', 'warehouse', 'warehouseEnter', 'kondisi_peti'])
->where('status', '=', 0)
->orderBy('created_at', 'desc');
// Tambahkan logika pencarian
$search = $request->input('search') ?? '';
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('mobile_id', 'like', "%$search%")
->orWhereHas('peti', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('fix_lot', 'like', "%$search%");
})
->orWhere('exit_at', 'like', "%$search%")
->orWhere('est_pengembalian', 'like', "%$search%")
->orWhere('exit_pic', 'like', "%$search%")
->orWhereHas('customer', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%");
})
->orWhereHas('warehouse', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%")
->orWhere('address', 'like', "%$search%");
})
->orWhere('enter_at', 'like', "%$search%")
->orWhere('enter_pic', 'like', "%$search%")
->orWhereHas('warehouseEnter', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%");
})
->orWhereHas('kondisi_peti', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('nama_kondisi', 'like', "%$search%");
})
->orWhere('status', 'like', "%$search%")
->orWhere('created_by', 'like', "%$search%")
->orWhere('updated_by', 'like', "%$search%");
});
}
if ($perPage == 'Semua') {
$chunkSize = 100;
$stores = new Collection();
$currentPage = 1;
$query->chunk($chunkSize, function ($storesChunk) use ($stores, &$currentPage) {
foreach ($storesChunk as $store) {
$store->setAttribute('i', ($currentPage - 1) * $storesChunk->perPage() + 1);
$stores->push($store);
$currentPage++;
}
});
} else {
$stores = $query->paginate($perPage);
}
$data = [
'peminjaman' => $stores,
'warehouse' => m_warehouse::get(),
'search' => $search,
'active' => 'menu-peminjaman',
];
return view('dashboard.Peminjaman.index', $data);
1 year ago
}
1 year ago
/**
* Show the form for creating a new resource.
*/
public function create()
{
$petiWithStatusNotZeroOrEmptyAndActive = Peti::where(function ($query) {
$query->whereHas('assetStatuses', function ($subquery) {
$subquery->where('status', '!=', 0)
->where('created_at', '=', function ($maxQuery) {
$maxQuery->selectRaw('MAX(created_at)')
->from('asset_statuses')
->whereColumn('peti_id', 'petis.id');
});
})->orWhereDoesntHave('assetStatuses');
})->where('status', 'AKTIF')->get();
$data = [
// 'peti' => Peti::all(),
'peminjaman' => asset_status::get(),
'warehouse' => m_warehouse::get(),
'peti_block' => $petiWithStatusNotZeroOrEmptyAndActive,
'customer' => Customer::get(),
'existingPeti' => asset_status::pluck('peti_id')->toArray(),
'active' => 'menu-peminjaman',
];
return view('dashboard.Peminjaman.create', $data);
1 year ago
}
/**
* Store a newly created resource in storage.
*/
public function store(ValidasiCreatePeminjaman $request)
1 year ago
{
// dd($request->all());
// dd($request);
1 year ago
try {
$currentUser = Auth::user();
$validatedData = $request->except('_token');
$validatedData['exit_pic'] = $currentUser->fullname; // Menambahkan ID pengguna sebagai created_by
$validatedData['mobile_id'] = Uuid::v4(); // Menambahkan ID Mobile
$validatedData['created_by'] = $currentUser->fullname; // Menambahkan ID pengguna sebagai created_by
$validatedData['updated_by'] = $currentUser->fullname; // Menambahkan ID pengguna sebagai updated_by
1 year ago
// dd($validatedData);
asset_status::create($validatedData);
return redirect()->route('dashboard.peminjaman.index')->with('success', 'Data peminjaman berhasil ditambah.');
1 year ago
} catch (\Throwable $th) {
dd($th->getMessage()); // Tampilkan pesan kesalahan
1 year ago
return redirect()->back()->with('error', 'Data peminjaman gagal ditambah.');
}
}
/**
* Display the specified resource.
*/
public function show($id)
{
// dd('oke');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
1 year ago
{
$data = [
'peti' => Peti::all(),
'peminjaman' => asset_status::find($id),
'warehouse' => m_warehouse::get(),
'customer' => Customer::get(),
'active' => 'menu-peminjaman',
];
return view('dashboard.Peminjaman.edit', $data);
1 year ago
}
/**
* Update the specified resource in storage.
*/
public function update(ValidasiUpdatePeminjaman $request, $id)
1 year ago
{
// dd($request->all());
1 year ago
// dd($request);
try {
$peminjaman = asset_status::findOrFail($id);
$peminjaman['updated_by'] = Auth::user()->fullname; // Menambahkan ID pengguna sebagai updated_by
1 year ago
$peminjaman->update($request->all());
return redirect()->route('dashboard.peminjaman.index')->with('success', 'Data peminjaman berhasil diperbaharui');
1 year ago
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data peminjaman gagal diperbaharui');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$peminjaman = asset_status::findOrFail($id);
$peminjaman->delete();
return redirect()->back()->with('success', 'Data peminjaman berhasil dihapus');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data peminjaman gagal dihapus');
}
1 year ago
}
/**
* Show the form for editing the specified resource.
*/
public function pengembalian($id)
{
// dd('oke');
$data = [
'peti' => Peti::get(),
'peminjaman' => asset_status::findOrFail($id),
'warehouse' => m_warehouse::get(),
'kondisiPeti' => Kondisi_Peti::get(),
'active' => 'menu-pengembalian',
];
return view('dashboard.Peminjaman.pengembalian', $data);
}
public function autoCompleteSearch(Request $request): JsonResponse
{
$petiData = [];
$customerData = [];
if ($request->filled('q')) {
$petiData = Peti::select("fix_lot", "id")
->where('fix_lot', 'LIKE', '%' . $request->get('q') . '%')
->where(function ($query) {
$query->whereHas('assetStatuses', function ($subquery) {
$subquery->where('status', '!=', 0)
->where('created_at', '=', function ($maxQuery) {
$maxQuery->selectRaw('MAX(created_at)')
->from('asset_statuses')
->whereColumn('peti_id', 'petis.id');
});
})->orWhereDoesntHave('assetStatuses');
})
->where('status', 'AKTIF')
->get();
}
return response()->json(['peti' => $petiData, 'customer' => $customerData]);
}
1 year ago
}