Browse Source

update import validasi jika datanya salah

master
Gunawan19621 1 year ago
parent
commit
685cbac44f
  1. 36
      app/Http/Controllers/CustomerController.php
  2. 31
      app/Http/Controllers/KondisiPetiController.php
  3. 20
      app/Http/Controllers/PeminjamanController.php
  4. 58
      app/Http/Controllers/PetiController.php
  5. 35
      app/Http/Controllers/TypePetiController.php
  6. 35
      app/Http/Controllers/WarehouseController.php
  7. 10
      app/Imports/CustomerImport.php
  8. 10
      app/Imports/KondisiPetiImport.php
  9. 81
      app/Imports/PetiImport.php
  10. 10
      app/Imports/TipePetiImport.php
  11. 9
      app/Imports/WarehouseImport.php
  12. 127
      resources/views/dashboard/Peminjaman/create.blade.php
  13. 1
      routes/web.php

36
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');
}
}

31
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())]);
}
}
}
}

20
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);
}
}

58
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())]);
}
}
}
}

35
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())]);
}
}
}
}

35
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())]);
}
}
}
}

10
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();

10
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();

81
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();

10
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();

9
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();

127
resources/views/dashboard/Peminjaman/create.blade.php

@ -2,7 +2,8 @@
@section('title', 'Tambah Peminjaman')
@section('content')
@include('layouts.components.alert-prompt')
<div class="card shadow mb-4">
<!-- Cara Lama -->
{{-- <div class="card shadow mb-4">
<div class="card-header py-3">
<div class="row">
<div class="col-6">
@ -15,6 +16,7 @@
id="peminjamanForm">
@csrf
<div class="form-group">
<label for="peti_id" class="col-form-label">Pilih Detail Peti: <span
class="text-danger">*</span></label>
<select class="form-control" name="peti_id" id="peti_id" required>
@ -68,8 +70,103 @@
</div>
</form>
</div>
</div> --}}
<!-- Cara Baru -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<div class="row">
<div class="col-6">
<h5 class="m-0 font-weight-bold text-primary mt-2">Tambah Peminjaman</h5>
</div>
</div>
</div>
<div class="card-body">
<form action="{{ route('dashboard.peminjaman.store') }}" method="POST" enctype="multipart/form-data"
id="peminjamanForm">
@csrf
<div class="form-group">
<label for="peti_id" class="col-form-label">Pilih Detail Peti: <span
class="text-danger">*</span></label>
<select class="form-control" id="search" name="peti_id"></select>
{{-- <label for="customer_id" class="col-form-label">Customer:<span class="text-danger">*</span></label>
<select class="form-control" name="customer_id" type="text" id="customer_id" required>
<option disabled selected>Pilih Nama Customer</option>
@foreach ($customer as $data)
<option value="{{ $data->id }}">{{ $data->name }}</option>
@endforeach
</select> --}}
<label for="customer_id" class="col-form-label">Customer:<span class="text-danger">*</span></label>
<select class="form-control" name="customer_id" type="text" id="customer_id" required>
<option disabled selected>Pilih Nama Customer</option>
@foreach ($customer as $data)
<option value="{{ $data->id }}">{{ $data->name }}</option>
@endforeach
</select>
<label for="exit_at" class="col-form-label">Tanggal Peminjaman: <span
class="text-danger">*</span></label>
<input class="form-control" name="exit_at" type="date" id="exit_at" value="{{ old('exit_at') }}"
required>
<label for="est_pengembalian" class="col-form-label">Estimasi Tanggal Pengembalian: <span
class="text-danger">*</span></label>
<input class="form-control" name="est_pengembalian" type="date" id="est_pengembalian" required>
<label for="warehouse_id" class="col-form-label">Asal Gudang:</label>
<select class="form-control" name="warehouse_id" type="text" id="warehouse_id" required>
<option disabled selected>Pilih Asal Gudang</option>
@foreach ($warehouse as $data)
<option value="{{ $data->id }}">{{ $data->name }}</option>
@endforeach
</select>
<label for="exit_warehouse" class="col-form-label">Tujuan Gudang: <span
class="text-danger">*</span></label>
<select class="form-control" name="exit_warehouse" type="text" id="exit_warehouse" required>
<option disabled selected>Pilih Tujuan Gudang</option>
@foreach ($warehouse as $data)
<option value="{{ $data->id }}">{{ $data->name }}</option>
@endforeach
</select>
</div>
<div class="modal-footer d-flex justify-content-center">
<a href="{{ route('dashboard.peminjaman.index') }}" class="btn btn-secondary">Kembali</a>
<button type="submit" class="btn btn-primary" id="submitButton">Simpan</button>
</div>
</form>
</div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
{{-- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"> --}}
<script type="text/javascript">
var path = "{{ route('dashboard.peminjaman.autoCompleteSearch') }}";
$('#search').select2({
placeholder: 'Pilih Detail Peti',
ajax: {
url: path,
dataType: 'json',
delay: 250,
processResults: function(data) {
return {
results: $.map(data, function(item) {
return {
text: item.fix_lot,
id: item.id
}
})
};
},
cache: true
}
});
</script>
<!-- auto disable form pada saat sudah di simpan-->
<script>
document.getElementById('peminjamanForm').addEventListener('submit', function() {
@ -100,7 +197,7 @@
</script>
<!-- otomatis asal gudang dan Customer berdasarkan detail peti -->
<script>
{{-- <script>
// Mendapatkan elemen select detail peti
const petiSelect = document.getElementById('peti_id');
@ -124,6 +221,30 @@
// Memilih customer berdasarkan data-customer-id
customerSelect.value = customerId;
});
</script>
</script> --}}
{{-- <script>
// Mendapatkan elemen select detail peti
const petiSelect = $('#search');
// Mendapatkan elemen select customer
const customerSelect = $('#customer_id');
// Mendapatkan elemen select asal gudang
const warehouseSelect = $('#warehouse_id');
// Menambahkan event listener ke select detail peti
petiSelect.on('select2:select', function(e) {
// Mendapatkan data tambahan dari opsi yang dipilih
const warehouseId = e.params.data.warehouseId;
const customerId = e.params.data.customerId;
// Memilih asal gudang berdasarkan data-warehouse-id
warehouseSelect.val(warehouseId).trigger('change');
// Memilih customer berdasarkan data-customer-id
customerSelect.val(customerId).trigger('change');
});
</script> --}}
@endsection

1
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

Loading…
Cancel
Save