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.
 
 
 
 
 
 

319 lines
12 KiB

<?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\Log;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\ValidasiCreatePeminjaman;
use App\Http\Requests\ValidasiUpdatePeminjaman;
class PeminjamanController extends Controller
{
/**
* Halaman index
*/
public function index(Request $request)
{
$perPage = $request->input('perPage', 5);
$query = asset_status::with(['customer', 'warehouseId', 'warehouse', 'warehouseEnter', 'kondisi_peti'])
->where('status', '=', 0)
->orderBy('created_at', 'desc');
//logika pencarian
$search = $request->input('search') ?? '';
if ($search) {
$searchArray = explode(' - ', $search);
if (count($searchArray) === 2) {
// Case: DPM - B100X63
list($codeCustomer, $typePeti) = $searchArray;
$query->whereHas('peti.customer', function ($customerQuery) use ($codeCustomer) {
$customerQuery->where('code_customer', 'like', "%$codeCustomer%");
})->whereHas('peti.tipe_peti', function ($tipePetiQuery) use ($typePeti) {
$tipePetiQuery->where('type', 'like', "%$typePeti%");
});
} else {
// Case: Other scenarios
$query->where(function ($q) use ($search) {
$q->where('mobile_id', 'like', "%$search%")
->orWhereHas('peti', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('fix_lot', 'like', "%$search%");
})
->orWhereHas('customer', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%");
})
->orWhereHas('peti.customer', function ($customerQuery) use ($search) {
$customerQuery->where('code_customer', 'like', "%$search%");
})
->orWhereHas('peti.tipe_peti', function ($tipePetiQuery) use ($search) {
$tipePetiQuery->where('type', 'like', "%$search%");
})
->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());
}
})
->orWhere('exit_pic', 'like', "%$search%")
->orWhereHas('warehouse', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%")
->orWhere('address', '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(),
'i' => ($stores->currentPage() - 1) * $stores->perPage() + 1,
'search' => $search,
'active' => 'menu-peminjaman',
];
return view('dashboard.Peminjaman.index', $data);
}
/**
* Halaman tambah data
*/
public function create()
{
$data = [
'peminjaman' => asset_status::get(),
'warehouse' => m_warehouse::get(),
'customer' => Customer::get(),
'existingPeti' => asset_status::pluck('peti_id')->toArray(),
'active' => 'menu-peminjaman',
];
return view('dashboard.Peminjaman.create', $data);
}
/**
* Halaman proses tambah data
*/
public function store(ValidasiCreatePeminjaman $request)
{
// dd($request->all());
// dd($request);
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
// dd($validatedData);
asset_status::create($validatedData);
return redirect()->route('dashboard.peminjaman.index')->with('success', 'Data peminjaman berhasil ditambah.');
} catch (\Throwable $th) {
dd($th->getMessage()); // Tampilkan pesan kesalahan
return redirect()->back()->with('error', 'Data peminjaman gagal ditambah.');
}
}
/**
* Halaman edit data
*/
public function edit($id)
{
$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);
}
/**
* Halaman proses edit data
*/
public function update(ValidasiUpdatePeminjaman $request, $id)
{
// dd($request->all());
// dd($request);
try {
$peminjaman = asset_status::findOrFail($id);
$peminjaman['updated_by'] = Auth::user()->fullname; // Menambahkan ID pengguna sebagai updated_by
$peminjaman->update($request->all());
return redirect()->route('dashboard.peminjaman.index')->with('success', 'Data peminjaman berhasil diperbaharui');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data peminjaman gagal diperbaharui');
}
}
/**
* proses deleted
*/
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');
}
}
/**
* Halaman proses pengembalian
*/
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);
}
// proses search peti
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]);
}
// proses search Customer
public function searchCustomers(Request $request): JsonResponse
{
$query = $request->get('q');
$customers = Customer::select('id', 'name')
->where('name', 'like', "%$query%")
->get()
->toArray();
return response()->json(['customers' => $customers]);
}
public function searchWarehouses(Request $request): JsonResponse
{
$query = $request->get('q');
$warehouses = m_warehouse::select('id', 'name')
->where('name', 'like', "%$query%")
->get()
->toArray();
return response()->json(['warehouses' => $warehouses]);
}
// proses auto select customer dan warehouse berdasarkan peti id
public function getCustomerAndWarehouseByPeti(Request $request): JsonResponse
{
$petiId = $request->get('peti_id');
// Fetch customer data and warehouse based on the selected peti
$peti = Peti::with(['customer', 'warehouse'])
->where('id', $petiId)
->first();
$customerData = [];
$selectedCustomer = null;
$warehouseData = [];
$selectedWarehouse = null;
if ($peti && $peti->customer) {
// Extract customer data from the related relationship
$customerData[] = [
'id' => $peti->customer->id,
'name' => $peti->customer->name,
// Add any other fields you want to include
];
// Set the selectedCustomer to the customer id
$selectedCustomer = $peti->customer->id;
}
if ($peti && $peti->warehouse) {
// Extract warehouse data from the related relationship
$warehouseData[] = [
'id' => $peti->warehouse->id,
'name' => $peti->warehouse->name,
// Add any other fields you want to include
];
// Set the selectedWarehouse to the warehouse id
$selectedWarehouse = $peti->warehouse->id;
}
return response()->json([
'customers' => $customerData,
'selectedCustomer' => $selectedCustomer,
'warehouses' => $warehouseData,
'selectedWarehouse' => $selectedWarehouse,
]);
}
}