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.
148 lines
5.0 KiB
148 lines
5.0 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use App\Models\m_warehouse; |
|
use Illuminate\Http\Request; |
|
use App\Imports\WarehouseImport; |
|
use Illuminate\Support\Collection; |
|
use Illuminate\Support\Facades\Auth; |
|
use Maatwebsite\Excel\Facades\Excel; |
|
use App\Http\Requests\ValidasiCreateWarehouse; |
|
use App\Http\Requests\ValidasiUpdateWarehouse; |
|
|
|
class WarehouseController extends Controller |
|
{ |
|
/** |
|
* Display a listing of the resource. |
|
*/ |
|
public function index(Request $request) |
|
{ |
|
$perPage = $request->input('perPage', 5); |
|
|
|
$query = m_warehouse::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('name', 'like', "%$search%") |
|
->orWhere('description', 'like', "%$search%") |
|
->orWhere('address', 'like', "%$search%"); |
|
}); |
|
} |
|
|
|
if ($perPage == 'Semua') { |
|
$chunkSize = 100; |
|
$warehouses = new Collection(); |
|
$currentPage = 1; |
|
|
|
$query->chunk($chunkSize, function ($warehouseChunk) use ($warehouses, &$currentPage) { |
|
foreach ($warehouseChunk as $warehouse) { |
|
$warehouse->setAttribute('i', ($currentPage - 1) * $warehouseChunk->perPage() + 1); |
|
$warehouses->push($warehouse); |
|
$currentPage++; |
|
} |
|
}); |
|
} else { |
|
$warehouses = $query->paginate($perPage); |
|
} |
|
|
|
$data = [ |
|
'warehouse' => $warehouses, |
|
'i' => ($warehouses->currentPage() - 1) * $warehouses->perPage() + 1, |
|
'search' => $search, |
|
'active' => 'menu-warehouse', |
|
]; |
|
|
|
return view('dashboard.Master_Data.Warehouse.index', $data); |
|
} |
|
|
|
/** |
|
* Store a newly created resource in storage. |
|
*/ |
|
public function store(ValidasiCreateWarehouse $request) |
|
{ |
|
try { |
|
$currentUser = Auth::user(); |
|
|
|
$validatedData = $request->except('_token'); |
|
|
|
$validatedData['name'] = strtoupper($validatedData['name']); |
|
$validatedData['description'] = strtoupper($validatedData['description']); |
|
$validatedData['address'] = strtoupper($validatedData['address']); |
|
$validatedData['created_by'] = $currentUser->fullname; |
|
$validatedData['updated_by'] = $currentUser->fullname; |
|
|
|
m_warehouse::create($validatedData); |
|
|
|
return redirect()->back()->with('success', 'Data gudang berhasil ditambah.'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data gudang gagal ditambah.'); |
|
} |
|
} |
|
|
|
/** |
|
* Update the specified resource in storage. |
|
*/ |
|
public function update(ValidasiUpdateWarehouse $request, $id) |
|
{ |
|
try { |
|
$warehouse = m_warehouse::findOrFail($id); |
|
$warehouseData = $request->all(); |
|
$warehouseData['name'] = strtoupper($warehouseData['name']); |
|
$warehouseData['description'] = strtoupper($warehouseData['description']); |
|
$warehouseData['address'] = strtoupper($warehouseData['address']); |
|
// Menambahkan nama pengguna yang melakukan pembaruan |
|
$warehouseData['updated_by'] = Auth::user()->fullname; |
|
$warehouse->update($request->all()); |
|
|
|
|
|
return redirect()->back()->with('success', 'Data Gudang Berhasil Diperbaharui'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data Gudang Gagal Diperbaharui'); |
|
} |
|
} |
|
|
|
/** |
|
* Remove the specified resource from storage. |
|
*/ |
|
public function destroy($id) |
|
{ |
|
// dd("oke"); |
|
try { |
|
$asset = m_warehouse::findOrFail($id); |
|
$asset->delete(); |
|
return redirect()->back()->with('success', 'Data Gudang berhasil dihapus'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data Gudang gagal dihapus'); |
|
} |
|
} |
|
|
|
public function importWarehouse(Request $request) |
|
{ |
|
$import = new WarehouseImport; |
|
|
|
try { |
|
Excel::import($import, request()->file('file')); |
|
|
|
if ($import->getRowCount() > 0) { |
|
return redirect()->route('dashboard.warehouse.index')->with('success', 'Data Warehouse berhasil diimport'); |
|
} else { |
|
$errors = $import->getErrors(); |
|
|
|
if (!empty($errors)) { |
|
session()->flash('error', $errors); |
|
} |
|
|
|
return redirect()->back()->with('import', $import)->with('error', 'Data Warehouse gagal di import.'); |
|
} |
|
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) { |
|
$failures = $e->failures(); |
|
|
|
foreach ($failures as $failure) { |
|
session()->flash('error', ['Baris ' . $failure->row() => implode(', ', $failure->errors())]); |
|
} |
|
} |
|
} |
|
} |