|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\Kondisi_Peti;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
use App\Http\Requests\ValidasiCreateKondisiPeti;
|
|
|
|
use App\Http\Requests\ValidasiUpdateKondisiPeti;
|
|
|
|
use App\Imports\KondisiPetiImport;
|
|
|
|
|
|
|
|
class KondisiPetiController extends Controller
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'kondisiPeti' => Kondisi_Peti::orderBy('created_at', 'desc')->get(),
|
|
|
|
'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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function importKondisiPeti(Request $request)
|
|
|
|
{
|
|
|
|
$import = new KondisiPetiImport;
|
|
|
|
Excel::import($import, request()->file('file'));
|
|
|
|
|
|
|
|
// Cek apakah ada data yang berhasil diimpor
|
|
|
|
if ($import->getRowCount() > 0) {
|
|
|
|
return redirect()->route('dashboard.kondisipeti.index')->with('success', 'Data Kondisi Peti 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 Kondisi Peti gagal di import.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|