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.

297 lines
10 KiB

<?php
namespace App\Http\Controllers;
use App\Models\Peti;
use App\Models\Customer;
use App\Models\Disposal;
use App\Models\m_warehouse;
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\Disposal\ValidasiCreateDisposal;
use App\Http\Requests\Disposal\ValidasiUpdateDisposal;
class DisposalController extends Controller
{
// Halamam index
public function index(Request $request)
{
$perPage = $request->input('perPage', 5);
$query = Disposal::where('status_disposal', 'INAKTIF')
->orderBy('created_at', 'desc');
// Add search logic similar to the Transfer index controller
$search = $request->input('search') ?? '';
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('fix_lot', 'like', "%$search%")
->orWhereHas('peti', function ($petiQuery) use ($search) {
$petiQuery->where('fix_lot', 'like', "%$search%");
})
->orWhereHas('customer', function ($customerQuery) use ($search) {
$customerQuery->where('name', 'like', "%$search%");
})
->orWhere('date_disposal', 'like', "%$search%")
->orWhere('description', 'like', "%$search%");
});
}
if ($perPage == 'Semua') {
$chunkSize = 100;
$disposals = new Collection();
$currentPage = 1;
$query->chunk($chunkSize, function ($disposalChunk) use ($disposals, &$currentPage) {
foreach ($disposalChunk as $disposal) {
$disposal->setAttribute('i', ($currentPage - 1) * $disposalChunk->perPage() + 1);
$disposals->push($disposal);
$currentPage++;
}
});
} else {
$disposals = $query->paginate($perPage);
}
$data = [
'disposals' => $disposals,
'i' => ($disposals->currentPage() - 1) * $disposals->perPage() + 1,
'search' => $search,
'active' => 'menu-disposal',
];
return view('dashboard.Disposal.index', $data);
}
/**
* 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' => $petiWithStatusNotZeroOrEmptyAndActive,
'warehouse' => m_warehouse::get(),
'customer' => Customer::get(),
'active' => 'menu-disposal',
];
return view('dashboard.Disposal.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(ValidasiCreateDisposal $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;
$validatedData['description'] = strtoupper($validatedData['description']);
$validatedData['status_disposal'] = 'INAKTIF';
// Membuat entitas Disposal
$disposal = Disposal::create($validatedData);
// Mengupdate warehouse_id pada model Peti
Peti::where('id', $disposal->peti_id)
->update([
'status' => $disposal->status_disposal,
]);
return redirect()->route('dashboard.disposal.index')->with('success', 'Data Disposal Peti berhasil ditambahkan');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data Disposal Peti gagal dimasukan ke disposal');
}
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data = [
'disposal' => Disposal::findOrFail($id),
'peti' => Peti::get(),
'customer' => Customer::get(),
'warehouse' => m_warehouse::get(),
'active' => 'menu-disposal',
];
return view('dashboard.Disposal.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(ValidasiUpdateDisposal $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;
$validatedData['description'] = strtoupper($validatedData['description']);
// Mencari transfer berdasarkan ID
$disposal = Disposal::find($id);
if (!$disposal) {
return redirect()->route('dashboard.disposal.index')->with('error', 'Data Disposal Peti tidak ditemukan');
}
// dd($validatedData);
// Memperbarui data transfer
$disposal->update($validatedData);
// Mengupdate warehouse_id pada model Peti
Peti::where('id', $disposal->peti_id)
->update([
'status' => $disposal->status_disposal,
]);
return redirect()->route('dashboard.disposal.index')->with('success', 'Data Disposal Peti berhasil diperbaharui');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data Disposal Peti gagal diperbaharui');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
// dd("oke");
try {
$disposal = Disposal::findOrFail($id);
$disposal->delete();
return redirect()->back()->with('success', 'Data disposal peti berhasil dihapus');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data disposal peti gagal dihapus');
}
}
/**
* Search peti by fix_lot
*/
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,
]);
}
}