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.

96 lines
3.3 KiB

1 year ago
<?php
namespace App\Http\Controllers;
1 year ago
use App\Models\Kondisi_Peti;
1 year ago
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
1 year ago
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Requests\ValidasiCreateKondisiPeti;
use App\Http\Requests\ValidasiUpdateKondisiPeti;
1 year ago
use App\Imports\KondisiPetiImport;
1 year ago
class KondisiPetiController extends Controller
{
public function index()
{
$data = [
'kondisiPeti' => Kondisi_Peti::orderBy('created_at', 'desc')->get(),
1 year ago
'active' => 'menu-kondisipeti'
];
return view('dashboard.Master_Data.Manajemen_Peti.Kondisi_Peti.index', $data);
}
public function store(ValidasiCreateKondisiPeti $request)
{
// dd('oke');
try {
$currentUser = Auth::user();
$validatedData = $request->except('_token');
$validatedData['created_by'] = $currentUser->fullname; // Menggunakan nama pengguna sebagai created_by
$validatedData['updated_by'] = $currentUser->fullname; // Menggunakan nama pengguna sebagai updated_by
// dd($validatedData);
Kondisi_Peti::create($validatedData);
return redirect()->back()->with('success', 'Data Kondisi Peti Berhasil Ditambah.');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data Kondisi Peti Gagal Ditambah.');
}
}
public function update(ValidasiUpdateKondisiPeti $request, $id)
{
try {
$currentUser = Auth::user();
$kondisiPeti = Kondisi_Peti::findOrFail($id);
$validatedData = $request->except('_token');
$validatedData['updated_by'] = $currentUser->fullname;
$kondisiPeti->update($validatedData);
return redirect()->back()->with('success', 'Data Kondisi Peti Berhasil Diperbarui.');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data Kondisi Peti Gagal Diperbarui.');
}
}
public function destroy($id)
{
// dd("oke");
try {
$kondisiPeti = Kondisi_Peti::findOrFail($id);
$kondisiPeti->delete();
return redirect()->back()->with('success', 'Data kondisi peti berhasil dihapus');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data kondisi peti gagal dihapus');
}
}
1 year ago
public function importKondisiPeti(Request $request)
{
$import = new KondisiPetiImport;
try {
Excel::import($import, request()->file('file'));
if ($import->getRowCount() > 0) {
return redirect()->route('dashboard.kondisipeti.index')->with('success', 'Data Kondisi Peti berhasil diimport');
} else {
$errors = $import->getErrors();
if (!empty($errors)) {
session()->flash('error', $errors);
}
return redirect()->back()->with('import', $import)->with('error', 'Data Kondisi Peti 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())]);
}
}
1 year ago
}
1 year ago
}