From 685cbac44ff7e7465afd96ca8744391edcce0e79 Mon Sep 17 00:00:00 2001 From: Gunawan19621 Date: Mon, 4 Dec 2023 10:18:23 +0700 Subject: [PATCH] update import validasi jika datanya salah --- app/Http/Controllers/CustomerController.php | 36 ++--- .../Controllers/KondisiPetiController.php | 31 ++--- app/Http/Controllers/PeminjamanController.php | 20 +++ app/Http/Controllers/PetiController.php | 58 +++++--- app/Http/Controllers/TypePetiController.php | 35 ++--- app/Http/Controllers/WarehouseController.php | 35 ++--- app/Imports/CustomerImport.php | 10 ++ app/Imports/KondisiPetiImport.php | 10 ++ app/Imports/PetiImport.php | 81 ++--------- app/Imports/TipePetiImport.php | 10 ++ app/Imports/WarehouseImport.php | 9 ++ .../dashboard/Peminjaman/create.blade.php | 127 +++++++++++++++++- routes/web.php | 1 + 13 files changed, 306 insertions(+), 157 deletions(-) diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index 5833654..b3ed763 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -114,27 +114,27 @@ class CustomerController extends Controller public function importCustomer(Request $request) { $import = new CustomerImport; - Excel::import($import, request()->file('file')); - - // Cek apakah ada data yang berhasil diimpor - if ($import->getRowCount() > 0) { - return redirect()->route('dashboard.customer.index')->with('success', 'Data Customer 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); - } + + try { + Excel::import($import, request()->file('file')); + + if ($import->getRowCount() > 0) { + return redirect()->route('dashboard.customer.index')->with('success', 'Data Customer berhasil diimport'); + } else { + $errors = $import->getErrors(); + + if (!empty($errors)) { + session()->flash('error', $errors); } + + return redirect()->back()->with('import', $import)->with('error', 'Data Customer gagal di import.'); } + } catch (\Maatwebsite\Excel\Validators\ValidationException $e) { + $failures = $e->failures(); - // Redirect back with import data and error message - return redirect()->back()->with('import', $import)->with('error', 'Data Import Customer gagal di import.'); + foreach ($failures as $failure) { + session()->flash('error', ['Baris ' . $failure->row() => implode(', ', $failure->errors())]); + } } - // return redirect()->route('dashboard.customer.index')->with('success', 'Data customer berhasil Di Import'); } } diff --git a/app/Http/Controllers/KondisiPetiController.php b/app/Http/Controllers/KondisiPetiController.php index aee0687..d5faa94 100644 --- a/app/Http/Controllers/KondisiPetiController.php +++ b/app/Http/Controllers/KondisiPetiController.php @@ -70,26 +70,27 @@ class KondisiPetiController extends Controller 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(); + 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 ($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); - } + 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(); - // Redirect back with import data and error message - return redirect()->back()->with('import', $import)->with('error', 'Data Import Kondisi Peti gagal di import.'); + foreach ($failures as $failure) { + session()->flash('error', ['Baris ' . $failure->row() => implode(', ', $failure->errors())]); + } } } } diff --git a/app/Http/Controllers/PeminjamanController.php b/app/Http/Controllers/PeminjamanController.php index 24e81e5..5888e50 100644 --- a/app/Http/Controllers/PeminjamanController.php +++ b/app/Http/Controllers/PeminjamanController.php @@ -9,6 +9,7 @@ use App\Models\asset_status; use App\Models\Kondisi_Peti; use Illuminate\Http\Request; use Symfony\Component\Uid\Uuid; +use Illuminate\Http\JsonResponse; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use App\Http\Requests\ValidasiCreatePeminjaman; @@ -36,6 +37,7 @@ class PeminjamanController extends Controller $perPage = $request->input('perPage', 5); $query = asset_status::with(['customer', 'warehouseId', 'warehouse', 'warehouseEnter', 'kondisi_peti']) + ->where('status', '=', 0) ->orderBy('created_at', 'desc'); // Tambahkan logika pencarian $search = $request->input('search') ?? ''; @@ -218,4 +220,22 @@ class PeminjamanController extends Controller ]; return view('dashboard.Peminjaman.pengembalian', $data); } + + public function autoCompleteSearch(Request $request): JsonResponse + { + $data = []; + + if ($request->filled('q')) { + $data = Peti::select("fix_lot", "id") + ->where('fix_lot', 'LIKE', '%' . $request->get('q') . '%') + ->get(); + } + // if ($request->filled('q')) { + // $data = Peti::select("fix_lot", "id", "warehouse_id as warehouseId", "customer_id as customerId") + // ->where('fix_lot', 'LIKE', '%' . $request->get('q') . '%') + // ->get(); + // } + + return response()->json($data); + } } diff --git a/app/Http/Controllers/PetiController.php b/app/Http/Controllers/PetiController.php index 9072583..d69a6ca 100644 --- a/app/Http/Controllers/PetiController.php +++ b/app/Http/Controllers/PetiController.php @@ -322,29 +322,55 @@ class PetiController extends Controller ); } + // public function importPeti(Request $request) + // { + // $import = new PetiImport; + // Excel::import($import, request()->file('file')); + + // // Cek apakah ada data yang berhasil diimpor + // if ($import->getRowCount() > 0) { + // return redirect()->route('dashboard.peti.index')->with('success', 'Data 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 Peti gagal di import.'); + // } + // } public function importPeti(Request $request) { $import = new PetiImport; - Excel::import($import, request()->file('file')); - // Cek apakah ada data yang berhasil diimpor - if ($import->getRowCount() > 0) { - return redirect()->route('dashboard.peti.index')->with('success', 'Data 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); - } + try { + Excel::import($import, request()->file('file')); + + if ($import->getRowCount() > 0) { + return redirect()->route('dashboard.peti.index')->with('success', 'Data Peti berhasil diimport'); + } else { + $errors = $import->getErrors(); + + if (!empty($errors)) { + session()->flash('error', $errors); } + + return redirect()->back()->with('import', $import)->with('error', 'Data Peti gagal di import.'); } + } catch (\Maatwebsite\Excel\Validators\ValidationException $e) { + $failures = $e->failures(); - // Redirect back with import data and error message - return redirect()->back()->with('import', $import)->with('error', 'Data Import Peti gagal di import.'); + foreach ($failures as $failure) { + session()->flash('error', ['Baris ' . $failure->row() => implode(', ', $failure->errors())]); + } } } } diff --git a/app/Http/Controllers/TypePetiController.php b/app/Http/Controllers/TypePetiController.php index 0a03c55..56bb5fa 100644 --- a/app/Http/Controllers/TypePetiController.php +++ b/app/Http/Controllers/TypePetiController.php @@ -114,26 +114,27 @@ class TypePetiController extends Controller public function importTipePeti(Request $request) { $import = new TipePetiImport; - Excel::import($import, request()->file('file')); - - // Cek apakah ada data yang berhasil diimpor - if ($import->getRowCount() > 0) { - return redirect()->route('dashboard.typepeti.index')->with('success', 'Data Tipe 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); - } + + try { + Excel::import($import, request()->file('file')); + + if ($import->getRowCount() > 0) { + return redirect()->route('dashboard.typepeti.index')->with('success', 'Data Tipe Peti berhasil diimport'); + } else { + $errors = $import->getErrors(); + + if (!empty($errors)) { + session()->flash('error', $errors); } + + return redirect()->back()->with('import', $import)->with('error', 'Data Tipe Peti gagal di import.'); } + } catch (\Maatwebsite\Excel\Validators\ValidationException $e) { + $failures = $e->failures(); - // Redirect back with import data and error message - return redirect()->back()->with('import', $import)->with('error', 'Data Import Tipe Peti gagal di import.'); + foreach ($failures as $failure) { + session()->flash('error', ['Baris ' . $failure->row() => implode(', ', $failure->errors())]); + } } } } diff --git a/app/Http/Controllers/WarehouseController.php b/app/Http/Controllers/WarehouseController.php index 4e550ea..c4c6964 100644 --- a/app/Http/Controllers/WarehouseController.php +++ b/app/Http/Controllers/WarehouseController.php @@ -108,26 +108,27 @@ class WarehouseController extends Controller 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); - } + + try { + Excel::import($import, request()->file('file')); + + if ($import->getRowCount() > 0) { + return redirect()->route('dashboard.warehouse.index')->with('success', 'Data Warehouse berhasil diimport'); + } else { + $errors = $import->getErrors(); + + if (!empty($errors)) { + session()->flash('error', $errors); } + + return redirect()->back()->with('import', $import)->with('error', 'Data Warehouse gagal di import.'); } + } catch (\Maatwebsite\Excel\Validators\ValidationException $e) { + $failures = $e->failures(); - // Redirect back with import data and error message - return redirect()->back()->with('import', $import)->with('error', 'Data Import Warehouse gagal di import.'); + foreach ($failures as $failure) { + session()->flash('error', ['Baris ' . $failure->row() => implode(', ', $failure->errors())]); + } } } } diff --git a/app/Imports/CustomerImport.php b/app/Imports/CustomerImport.php index 497acbe..06f1ecc 100644 --- a/app/Imports/CustomerImport.php +++ b/app/Imports/CustomerImport.php @@ -23,6 +23,16 @@ class CustomerImport implements ToModel, WithHeadingRow public function model(array $row) { + // Pemeriksaan apakah semua kolom yang dibutuhkan ada dalam format yang benar + $requiredColumns = ['name', 'code_customer', 'lot_no', 'no_tlp', 'address']; + + foreach ($requiredColumns as $column) { + if (!array_key_exists($column, $row) || empty($row[$column])) { + $this->errors[] = 'Kolom ' . $column . ' pada baris ' . $this->rowCount . ' tidak boleh kosong.'; + return null; // Abaikan data dengan kolom yang tidak sesuai + } + } + // Mendapatkan informasi pengguna yang sedang login $user = Auth::user(); diff --git a/app/Imports/KondisiPetiImport.php b/app/Imports/KondisiPetiImport.php index 9bc032c..e287574 100644 --- a/app/Imports/KondisiPetiImport.php +++ b/app/Imports/KondisiPetiImport.php @@ -24,6 +24,16 @@ class KondisiPetiImport implements ToModel, WithHeadingRow public function model(array $row) { + // Pemeriksaan apakah semua kolom yang dibutuhkan ada dalam format yang benar + $requiredColumns = ['nama_kondisi', 'deskripsi_kondisi']; + + foreach ($requiredColumns as $column) { + if (!array_key_exists($column, $row) || empty($row[$column])) { + $this->errors[] = 'Kolom ' . $column . ' pada baris ' . $this->rowCount . ' tidak boleh kosong.'; + return null; // Abaikan data dengan kolom yang tidak sesuai + } + } + // Mendapatkan informasi pengguna yang sedang login $user = Auth::user(); diff --git a/app/Imports/PetiImport.php b/app/Imports/PetiImport.php index 32463df..4555bc5 100644 --- a/app/Imports/PetiImport.php +++ b/app/Imports/PetiImport.php @@ -15,77 +15,6 @@ use Maatwebsite\Excel\Events\AfterImport; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\WithHeadingRow; -// class PetiImport implements ToModel, WithHeadingRow -// { -// /** -// * @param array $row -// * -// * @return \Illuminate\Database\Eloquent\Model|null -// */ - -// private $rowCount = 0; -// private $errors; - -// public function model(array $row) -// { -// // Mendapatkan informasi pengguna yang sedang login -// $user = Auth::user(); - -// // Cek apakah nama warehouse sudah ada dalam database -// $existingPeti = Peti::where('fix_lot', $row['fix_lot'])->first(); - -// if ($existingPeti) { -// // Jika nama warehouse sudah ada, tambahkan pesan kesalahan ke dalam array $errors -// $this->errors[] = new MessageBag(['Row ' . $this->rowCount => 'Data Peti sudah ada dalam database.']); -// return null; // Abaikan data yang sudah ada -// } - -// // Increment rowCount setiap kali ada data yang diimpor -// $this->rowCount++; - -// return new Peti([ -// 'created_by' => $user->fullname, -// 'tipe_peti_id' => $row['tipe_peti_id'], -// 'warna' => $row['warna'], -// 'customer_id' => $row['customer_id'], -// 'warehouse_id' => $row['warehouse_id'], -// 'date_pembuatan' => now(), -// 'kondisipeti_id' => $row['kondisipeti_id'], -// 'packing_no' => $row['packing_no'], -// 'fix_lot' => $row['fix_lot'], -// 'status' => $row['status'], -// ]); -// } - -// public function registerEvents(): array -// { -// return [ -// AfterImport::class => function (AfterImport $event) { -// // Setelah import selesai, kita bisa mengakses semua data yang diimpor -// $importedData = $event->getReader()->getDelegate()->getActiveSheet()->toArray(); - -// // Lakukan pemeriksaan apakah ada data yang bernilai null dan hapus dari hasil import -// $filteredData = array_filter($importedData, function ($row) { -// return $row[0] !== null; // Jika nilai kolom pertama tidak null, pertahankan baris tersebut -// }); - -// // Update hasil import dengan data yang telah difilter -// $event->getReader()->getDelegate()->getActiveSheet()->fromArray($filteredData); -// }, -// ]; -// } - -// public function getErrors() -// { -// return $this->errors; -// } - -// public function getRowCount() -// { -// return $this->rowCount; -// } -// } - class PetiImport implements ToModel, WithHeadingRow { private $rowCount = 0; @@ -93,6 +22,16 @@ class PetiImport implements ToModel, WithHeadingRow public function model(array $row) { + // Pemeriksaan apakah semua kolom yang dibutuhkan ada dalam format yang benar + $requiredColumns = ['tipe_peti_id', 'warna', 'customer_id', 'warehouse_id', 'kondisipeti_id', 'packing_no', 'fix_lot', 'status']; + + foreach ($requiredColumns as $column) { + if (!array_key_exists($column, $row) || empty($row[$column])) { + $this->errors[] = 'Kolom ' . $column . ' pada baris ' . $this->rowCount . ' tidak boleh kosong.'; + return null; // Abaikan data dengan kolom yang tidak sesuai + } + } + $user = Auth::user(); $existingPeti = Peti::where('fix_lot', $row['fix_lot'])->first(); diff --git a/app/Imports/TipePetiImport.php b/app/Imports/TipePetiImport.php index 4f2e412..26a9598 100644 --- a/app/Imports/TipePetiImport.php +++ b/app/Imports/TipePetiImport.php @@ -25,6 +25,16 @@ class TipePetiImport implements ToModel, WithHeadingRow public function model(array $row) { + // Pemeriksaan apakah semua kolom yang dibutuhkan ada dalam format yang benar + $requiredColumns = ['type', 'size_peti', 'description']; + + foreach ($requiredColumns as $column) { + if (!array_key_exists($column, $row) || empty($row[$column])) { + $this->errors[] = 'Kolom ' . $column . ' pada baris ' . $this->rowCount . ' tidak boleh kosong.'; + return null; // Abaikan data dengan kolom yang tidak sesuai + } + } + // Mendapatkan informasi pengguna yang sedang login $user = Auth::user(); diff --git a/app/Imports/WarehouseImport.php b/app/Imports/WarehouseImport.php index 28de7ab..d7b8d48 100644 --- a/app/Imports/WarehouseImport.php +++ b/app/Imports/WarehouseImport.php @@ -24,6 +24,15 @@ class WarehouseImport implements ToModel, WithHeadingRow public function model(array $row) { + $requiredColumns = ['name', 'description', 'address']; + + foreach ($requiredColumns as $column) { + if (!array_key_exists($column, $row) || empty($row[$column])) { + $this->errors[] = 'Kolom ' . $column . ' pada baris ' . $this->rowCount . ' tidak boleh kosong.'; + return null; // Abaikan data dengan kolom yang tidak sesuai + } + } + // Mendapatkan informasi pengguna yang sedang login $user = Auth::user(); diff --git a/resources/views/dashboard/Peminjaman/create.blade.php b/resources/views/dashboard/Peminjaman/create.blade.php index a827d7d..f854e45 100644 --- a/resources/views/dashboard/Peminjaman/create.blade.php +++ b/resources/views/dashboard/Peminjaman/create.blade.php @@ -2,7 +2,8 @@ @section('title', 'Tambah Peminjaman') @section('content') @include('layouts.components.alert-prompt') -
+ + {{--
@@ -15,6 +16,7 @@ id="peminjamanForm"> @csrf
+ + + {{-- + --}} + + + + + + + + + + + + + + +
+ + +
+ + + + {{-- --}} + + - + --}} + {{-- --}} + @endsection diff --git a/routes/web.php b/routes/web.php index 37c4477..3adf893 100644 --- a/routes/web.php +++ b/routes/web.php @@ -64,6 +64,7 @@ Route::prefix('dashboard')->name('dashboard.')->middleware(['auth'])->group(func Route::put('peminjaman/{id}', 'update')->name('peminjaman.update'); Route::delete('peminjaman/delete/{id}', 'destroy')->name('peminjaman.destroy'); Route::get('peminjaman/{id}/pengembalian', 'pengembalian')->name('peminjaman.pengembalian'); + Route::get('peminjaman/Search', 'autoCompleteSearch')->name('peminjaman.autoCompleteSearch'); }); //Halaman Pengembalian