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.

249 lines
10 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;
1 year ago
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
1 year ago
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\ValidasiUpdatePengembalian;
1 year ago
class PengembalianController extends Controller
{
public function index(Request $request)
1 year ago
{
$perPage = $request->input('perPage', 5);
$query = asset_status::with(['customer', 'warehouse', 'warehouseEnter', 'kondisi_peti'])
->where('status', '=', 1)
->join(DB::raw('(SELECT peti_id, MAX(updated_at) as latest_update FROM asset_statuses GROUP BY peti_id) as latest_peti'), function ($join) {
$join->on('asset_statuses.peti_id', '=', 'latest_peti.peti_id')
->on('asset_statuses.updated_at', '=', 'latest_peti.latest_update');
})
->orderBy('asset_statuses.created_at', 'desc');
// 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%");
}) // sudah cari fix lot
->orWhereHas('customer', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%");
}) // sudah cari nama customer
->orWhere(function ($dateQuery) use ($search) {
try {
// Format tanggal yang diharapkan dari input pengguna
$formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d');
// Cek kesamaan tanggal
$dateQuery->whereDate('exit_at', $formattedDate);
} catch (\Exception $e) {
Log::error('Error parsing date: ' . $e->getMessage());
}
})
->orWhere(function ($dateQuery) use ($search) {
try {
// Format tanggal yang diharapkan dari input pengguna
$formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d');
// Cek kesamaan tanggal
$dateQuery->whereDate('est_pengembalian', $formattedDate);
} catch (\Exception $e) {
Log::error('Error parsing date: ' . $e->getMessage());
}
}) // sudah cari est pengembalian (tanggal pengembalian)
->orWhere('exit_pic', 'like', "%$search%") // sudah cari exit pic (nama peminjam)
->orWhereHas('warehouse', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%")
->orWhere('address', 'like', "%$search%");
}) // sudah cari warehouse (lokasi peminjaman)
->orWhere(function ($dateQuery) use ($search) {
try {
// Format tanggal yang diharapkan dari input pengguna
$formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d');
// Cek kesamaan tanggal
$dateQuery->whereDate('enter_at', $formattedDate);
} catch (\Exception $e) {
Log::error('Error parsing date: ' . $e->getMessage());
}
}) // sudah cari enter at (tanggal pengembalian)
->orWhere('enter_pic', 'like', "%$search%") // sudah cari enter pic (nama pengembali)
->orWhereHas('warehouseEnter', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%");
}) // sudah cari warehouse enter (lokasi pengembalian)
->orWhereHas('kondisi_peti', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('nama_kondisi', 'like', "%$search%");
}) // sudah cari kondisi peti
->orWhere(function ($statusQuery) use ($search) {
if (strtolower($search) === 'return') {
$statusQuery->where('status', 1);
} else {
$statusQuery->where('status', 'like', "%$search%");
}
}); // sudah cari status
});
}
// Periksa apakah perPage adalah 'Semua'
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() + $store->getQueueableId());
$stores->push($store);
$currentPage++;
}
});
} else {
// Hitung halaman berdasarkan parameter halaman yang diberikan atau default ke halaman pertama
$page = $request->input('page') ?? 1;
// Paginasi dengan parameter halaman
$stores = $query->paginate($perPage, ['*'], 'page', $page);
// Hitung nomor urutan untuk setiap item di halaman
$stores->each(function ($store) use ($page, $perPage) {
$store->setAttribute('i', ($page - 1) * $perPage + $store->getQueueableId());
});
}
$data = [
'peminjaman' => $stores,
'warehouse' => m_warehouse::get(),
'i' => ($stores->currentPage() - 1) * $stores->perPage() + 1,
'search' => $search,
'active' => 'menu-pengembalian',
];
return view('dashboard.Pengembalian.index', $data);
1 year ago
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show($id)
{
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
1 year ago
{
$data = [
'peti' => Peti::get(),
'peminjaman' => asset_status::findOrFail($id),
'warehouse' => m_warehouse::get(),
'kondisiPeti' => Kondisi_Peti::get(),
'customer' => Customer::get(),
'active' => 'menu-pengembalian',
];
return view('dashboard.Pengembalian.edit', $data);
1 year ago
}
/**
* Update the specified resource in storage.
*/
public function update(ValidasiUpdatePengembalian $request, $id)
1 year ago
{
// dd($request);
1 year ago
try {
// Mendapatkan informasi pengguna yang sedang login
$currentUser = Auth::user();
// Validasi data dari request
$validatedData = $request->validated();
1 year ago
$peminjaman = asset_status::findOrFail($id);
$validatedData['updated_by'] = $currentUser->fullname;
$validatedData['enter_pic'] = $currentUser->fullname;
$validatedData['status'] = 1;
$peminjaman = asset_status::find($id);
if (!$peminjaman) {
return redirect()->route('dashboard.peminjaman.index')->with('error', 'Proses Pengembalian Peti tidak ditemukan');
}
// dd($validatedData);
$peminjaman->update($validatedData);
// dd($peminjaman);
// Mengupdate warehouse_id pada model Peti
Peti::where('id', $peminjaman->peti_id)
->update([
't_warehouse_id' => $peminjaman->enter_warehouse,
// 'customer_id' => $transfer->name_customer,
'updated_by' => $currentUser->fullname,
]);
return redirect()->route('dashboard.pengembalian.index')->with('success', 'Data peminjaman berhasil diperbaharui');
1 year ago
} catch (\Throwable $th) {
// Tampilkan pesan kesalahan untuk debugging
return redirect()->back()->with('error', 'Data peminjaman gagal diperbaharui: ' . $th->getMessage());
1 year ago
}
}
// public function update(ValidasiUpdatePengembalian $request, $id)
// {
// dd($request);
// try {
// $peminjaman = asset_status::findOrFail($id);
// // Update atribut-atribut yang diperlukan
// $peminjaman->enter_pic = Auth::user()->fullname;
// $peminjaman->updated_by = Auth::user()->fullname;
// $peminjaman->peti_id = $request->input('peti_id');
// $peminjaman->exit_at = $request->input('exit_at');
// $peminjaman->est_pengembalian = $request->input('est_pengembalian');
// $peminjaman->exit_warehouse = $request->input('exit_warehouse');
// $peminjaman->enter_at = $request->input('enter_at');
// $peminjaman->enter_warehouse = $request->input('enter_warehouse');
// $peminjaman->kondisi_peti_id = $request->input('kondisi_peti_id');
// // Mengatur status menjadi 1 secara otomatis
// $peminjaman->status = 1;
// // dd($peminjaman);
// $peminjaman->save();
// return redirect()->route('dashboard.pengembalian.index')->with('success', 'Data peminjaman berhasil diperbaharui');
// } catch (\Throwable $th) {
// // Tampilkan pesan kesalahan untuk debugging
// return redirect()->back()->with('error', 'Data peminjaman gagal diperbaharui: ' . $th->getMessage());
// }
// }
1 year ago
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
// dd("oke");
1 year ago
}
}