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.

97 lines
3.3 KiB

1 year ago
<?php
namespace App\Http\Controllers;
12 months ago
use App\Models\Kondisi_Peti;
1 year ago
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
12 months ago
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Requests\ValidasiCreateKondisiPeti;
use App\Http\Requests\ValidasiUpdateKondisiPeti;
12 months 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');
// dd($request);
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');
}
}
12 months 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())]);
}
}
12 months ago
}
1 year ago
}