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.

134 lines
4.0 KiB

<?php
namespace App\Http\Controllers;
use App\Models\m_warehouse;
use Illuminate\Http\Request;
use App\Imports\WarehouseImport;
use Illuminate\Support\Facades\Auth;
12 months ago
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Session;
12 months ago
use App\Http\Requests\ValidasiCreateWarehouse;
use App\Http\Requests\ValidasiUpdateWarehouse;
use Maatwebsite\Excel\Exceptions\NoTypeDetectedException;
use Illuminate\Support\Facades\Log;
class WarehouseController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$data = [
'warehouses' => m_warehouse::orderBy('created_at', 'desc')->get(),
'active' => 'menu-warehouse',
];
return view('dashboard.Master_Data.Warehouse.index', $data);
}
/**
* Show the form for creating a new resource.
*/
// public function create()
// {
// // return view('dashboard.Master_Data.Warehouse.create');
// }
/**
* Store a newly created resource in storage.
*/
public function store(ValidasiCreateWarehouse $request)
{
try {
$currentUser = Auth::user();
$validatedData = $request->except('_token');
$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.');
}
}
/**
* Display the specified resource.
*/
// public function show($id)
// {
// // dd('oke');
// // return view('dashboard.Master_Data.Warehouse.show');
// }
/**
* Show the form for editing the specified resource.
*/
// public function edit()
// {
// // dd('oke');
// // return view('dashboard.Master_Data.Warehouse.edit');
// }
/**
* Update the specified resource in storage.
*/
public function update(ValidasiUpdateWarehouse $request, $id)
{
try {
$warehouse = m_warehouse::findOrFail($id);
$warehouse->update($request->all());
// Menambahkan nama pengguna yang melakukan pembaruan
$userData['updated_by'] = Auth::user()->fullname;
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');
}
}
12 months ago
public function importWarehouse(Request $request)
{
$import = new WarehouseImport;
Excel::import($import, request()->file('file'));
// Cek apakah ada data yang berhasil diimpor
if ($import->getRowCount() > 0) {
return redirect()->route('dashboard.warehouse.index')->with('success', 'Data Warehouse berhasil diimport');
} else {
$errors = $import->getErrors();
if ($errors) {
foreach ($errors as $error) {
foreach ($error->all() as $message) {
// Tampilkan pesan kesalahan untuk setiap baris
// Simpan pesan flash dengan kategori 'error'
session()->flash('error', $message);
}
}
}
// Redirect back with import data and error message
return redirect()->back()->with('import', $import)->with('error', 'Data Import Warehouse gagal di import.');
}
12 months ago
}
}