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.
132 lines
5.0 KiB
132 lines
5.0 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use App\Models\Kondisi_Peti; |
|
use Illuminate\Http\Request; |
|
use App\Imports\KondisiPetiImport; |
|
use Illuminate\Support\Collection; |
|
use Illuminate\Support\Facades\Auth; |
|
use Maatwebsite\Excel\Facades\Excel; |
|
use App\Http\Requests\ValidasiCreateKondisiPeti; |
|
use App\Http\Requests\ValidasiUpdateKondisiPeti; |
|
|
|
class KondisiPetiController extends Controller |
|
{ |
|
public function index(Request $request) |
|
{ |
|
$perPage = $request->input('perPage', 5); |
|
|
|
$query = Kondisi_Peti::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('nama_kondisi', 'like', "%$search%") |
|
->orWhere('deskripsi_kondisi', 'like', "%$search%"); |
|
}); |
|
} |
|
|
|
if ($perPage == 'Semua') { |
|
$chunkSize = 100; |
|
$kondisiPetis = new Collection(); |
|
$currentPage = 1; |
|
|
|
$query->chunk($chunkSize, function ($kondisiPetiChunk) use ($kondisiPetis, &$currentPage) { |
|
foreach ($kondisiPetiChunk as $kondisiPeti) { |
|
$kondisiPeti->setAttribute('i', ($currentPage - 1) * $kondisiPetiChunk->perPage() + 1); |
|
$kondisiPetis->push($kondisiPeti); |
|
$currentPage++; |
|
} |
|
}); |
|
} else { |
|
$kondisiPetis = $query->paginate($perPage); |
|
} |
|
|
|
$data = [ |
|
'kondisiPetis' => $kondisiPetis, |
|
'i' => ($kondisiPetis->currentPage() - 1) * $kondisiPetis->perPage() + 1, |
|
'search' => $search, |
|
'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['nama_kondisi'] = strtoupper($validatedData['nama_kondisi']); // Menggunakan nama pengguna sebagai created_by |
|
$validatedData['deskripsi_kondisi'] = strtoupper($validatedData['deskripsi_kondisi']); // Menggunakan nama pengguna sebagai created_by |
|
$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['nama_kondisi'] = strtoupper($validatedData['nama_kondisi']); // Menggunakan nama pengguna sebagai created_by |
|
$validatedData['deskripsi_kondisi'] = strtoupper($validatedData['deskripsi_kondisi']); |
|
$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; |
|
|
|
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())]); |
|
} |
|
} |
|
} |
|
}
|
|
|