From 8be63dfdf32570686cbc7191865eda56926c9a03 Mon Sep 17 00:00:00 2001 From: Gunawan19621 Date: Sun, 3 Dec 2023 18:55:12 +0700 Subject: [PATCH 1/3] update import berdasarkan format --- app/Http/Controllers/CustomerController.php | 25 ++- .../Controllers/KondisiPetiController.php | 24 ++- app/Http/Controllers/PetiController.php | 107 +++++++------ .../Report/PetternLotPetiController.php | 63 +++++++- app/Http/Controllers/TypePetiController.php | 24 ++- app/Http/Controllers/WarehouseController.php | 57 +++++-- app/Imports/CustomerImport.php | 46 ++++++ app/Imports/KondisiPetiImport.php | 46 ++++++ app/Imports/PetiImport.php | 147 ++++++++++++++++-- app/Imports/TipePetiImport.php | 46 ++++++ app/Imports/WarehouseImport.php | 47 ++++++ .../Manajemen_Peti/Peti/index.blade.php | 139 ++++++----------- .../Report/Pattern_lot_peti/index.blade.php | 71 ++++++++- .../Master_Data/Warehouse/index.blade.php | 1 + .../layouts/components/alert-prompt.blade.php | 6 + 15 files changed, 663 insertions(+), 186 deletions(-) diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index 4fadcfb..5833654 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -113,7 +113,28 @@ class CustomerController extends Controller public function importCustomer(Request $request) { - Excel::import(new CustomerImport, request()->file('file')); - return redirect()->route('dashboard.customer.index')->with('success', 'Data customer berhasil Di Import'); + $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); + } + } + } + + // Redirect back with import data and error message + return redirect()->back()->with('import', $import)->with('error', 'Data Import Customer gagal di import.'); + } + // 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 bc19142..aee0687 100644 --- a/app/Http/Controllers/KondisiPetiController.php +++ b/app/Http/Controllers/KondisiPetiController.php @@ -69,7 +69,27 @@ class KondisiPetiController extends Controller public function importKondisiPeti(Request $request) { - Excel::import(new KondisiPetiImport, request()->file('file')); - return redirect()->route('dashboard.kondisipeti.index')->with('success', 'Data kondisi peti berhasil di import'); + $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.'); + } } } diff --git a/app/Http/Controllers/PetiController.php b/app/Http/Controllers/PetiController.php index 0fc7f08..9072583 100644 --- a/app/Http/Controllers/PetiController.php +++ b/app/Http/Controllers/PetiController.php @@ -24,81 +24,60 @@ class PetiController extends Controller /** * Display a listing of the resource. */ - // public function index() - // { - // $data = [ - // // 'peti' => Peti::orderBy('created_at', 'desc')->get(), - // 'peti' => Peti::orderBy('created_at', 'desc')->get(), - // 'kondisiPeti' => Kondisi_Peti::all(), - // 'active' => 'menu-peti', - // ]; - // return view('dashboard.Master_Data.Manajemen_Peti.Peti.index', $data); - // } - // public function index() - // { - // // $perPage = 10; // Jumlah data per halaman, sesuaikan dengan kebutuhan Anda - - // // $peti = Peti::orderBy('created_at', 'desc')->paginate($perPage); - // $peti = Peti::orderBy('created_at', 'desc')->get(); - // $kondisiPeti = Kondisi_Peti::all(); - - // $data = [ - // 'peti' => $peti, - // 'kondisiPeti' => $kondisiPeti, - // 'active' => 'menu-peti', - // ]; - - // return view('dashboard.Master_Data.Manajemen_Peti.Peti.index', $data); - // } public function index(Request $request) { $perPage = $request->input('perPage', 5); - $query = Peti::select(['petis.*', 'customers.code_customer', 'customers.lot_no', 'type_petis.type', 'type_petis.size_peti', 'm_warehouses.name as warehouse_name']) - ->join('customers', 'customers.id', '=', 'petis.customer_id') - ->join('type_petis', 'type_petis.id', '=', 'petis.tipe_peti_id') - ->join('m_warehouses', 'm_warehouses.id', '=', 'petis.warehouse_id') - ->join('kondisi_petis', 'kondisipeti_id', '=', 'petis.kondisipeti_id') - ->orderBy('petis.created_at', 'desc'); + $query = Peti::with(['customer', 'warehouse', 'tipe_peti', 'kondisipeti', 'transfer', 'assetStatuses']) + ->orderBy('created_at', 'desc'); + // Tambahkan logika pencarian + $search = $request->input('search') ?? ''; // Tambahkan logika pencarian $search = $request->input('search') ?? ''; if ($search) { $query->where(function ($q) use ($search) { - $q->where('petis.fix_lot', 'like', "%$search%") - ->orWhere('petis.created_by', 'like', "%$search%") - ->orWhere('customers.name', 'like', "%$search%") - ->orWhere('m_warehouses.name', 'like', "%$search%") - ->orWhere('customers.code_customer', 'like', "%$search%") - ->orWhere('type_petis.type', 'like', "%$search%") - ->orWhere('type_petis.size_peti', 'like', "%$search%") - ->orWhere('customers.lot_no', 'like', "%$search%") - ->orWhere('kondisi_petis.nama_kondisi', 'like', "%$search%") - ->orWhere('petis.packing_no', 'like', "%$search%") - ->orWhere('petis.status', 'like', "%$search%"); + $q->where('fix_lot', 'like', "%$search%") + ->orWhere('created_by', 'like', "%$search%") + ->orWhereHas('customer', function ($customerQuery) use ($search) { + $customerQuery->where('name', 'like', "%$search%") + ->orWhere('code_customer', 'like', "%$search%") + ->orWhere('lot_no', 'like', "%$search%"); + }) + ->orWhereHas('warehouse', function ($warehouseQuery) use ($search) { + $warehouseQuery->where('name', 'like', "%$search%"); + }) + ->orWhereHas('tipe_peti', function ($tipePetiQuery) use ($search) { + $tipePetiQuery->where('type', 'like', "%$search%") + ->orWhere('size_peti', 'like', "%$search%"); + }) + ->orWhereHas('kondisipeti', function ($kondisiPetiQuery) use ($search) { + $kondisiPetiQuery->where('nama_kondisi', 'like', "%$search%"); + }) + ->orWhere('packing_no', 'like', "%$search%") + ->orWhere('status', 'like', "%$search%"); }); } if ($perPage == 'Semua') { $chunkSize = 100; - $stores = new Collection(); + $petis = new Collection(); $currentPage = 1; - $query->chunk($chunkSize, function ($storesChunk) use ($stores, &$currentPage) { - foreach ($storesChunk as $store) { - $store->setAttribute('i', ($currentPage - 1) * $storesChunk->perPage() + 1); - $stores->push($store); + $query->chunk($chunkSize, function ($petiChunk) use ($petis, &$currentPage) { + foreach ($petiChunk as $peti) { + $peti->setAttribute('i', ($currentPage - 1) * $petiChunk->perPage() + 1); + $petis->push($peti); $currentPage++; } }); } else { - $stores = $query->paginate($perPage); + $petis = $query->paginate($perPage); } $data = [ - // 'peti' => Peti::orderBy('created_at', 'desc')->paginate(10), - 'peti' => $stores, + 'peti' => $petis, 'kondisiPeti' => Kondisi_Peti::all(), - 'i' => ($stores->currentPage() - 1) * $stores->perPage() + 1, + 'i' => ($petis->currentPage() - 1) * $petis->perPage() + 1, 'search' => $search, 'active' => 'menu-peti', ]; @@ -345,7 +324,27 @@ class PetiController extends Controller public function importPeti(Request $request) { - Excel::import(new PetiImport, request()->file('file')); - return redirect()->route('dashboard.peti.index')->with('success', 'Data peti berhasil di import'); + $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.'); + } } } diff --git a/app/Http/Controllers/Report/PetternLotPetiController.php b/app/Http/Controllers/Report/PetternLotPetiController.php index 5e50c9a..7ebd17f 100644 --- a/app/Http/Controllers/Report/PetternLotPetiController.php +++ b/app/Http/Controllers/Report/PetternLotPetiController.php @@ -5,16 +5,75 @@ namespace App\Http\Controllers\Report; use App\Models\Peti; use App\Models\asset_status; use Illuminate\Http\Request; +use Illuminate\Support\Collection; use App\Http\Controllers\Controller; use Maatwebsite\Excel\Facades\Excel; use App\Exports\PetternLotPetiExport; class PetternLotPetiController extends Controller { - public function index() + // public function index() + // { + // $data = [ + // 'peti' => Peti::withTrashed()->get(), + // 'active' => 'menu-Pettern_Lot_Peti' + // ]; + // return view('dashboard.Master_Data.Report.Pattern_lot_peti.index', $data); + // } + public function index(Request $request) { + $perPage = $request->input('perPage', 5); + + $query = Peti::with(['customer', 'warehouse', 'tipe_peti', 'kondisipeti', 'transfer', 'assetStatuses']) + ->orderBy('created_at', 'desc'); + // Tambahkan logika pencarian + $search = $request->input('search') ?? ''; + // Tambahkan logika pencarian + $search = $request->input('search') ?? ''; + if ($search) { + $query->where(function ($q) use ($search) { + $q->where('fix_lot', 'like', "%$search%") + ->orWhere('created_by', 'like', "%$search%") + ->orWhereHas('customer', function ($customerQuery) use ($search) { + $customerQuery->where('name', 'like', "%$search%") + ->orWhere('code_customer', 'like', "%$search%") + ->orWhere('lot_no', 'like', "%$search%"); + }) + ->orWhereHas('warehouse', function ($warehouseQuery) use ($search) { + $warehouseQuery->where('name', 'like', "%$search%"); + }) + ->orWhereHas('tipe_peti', function ($tipePetiQuery) use ($search) { + $tipePetiQuery->where('type', 'like', "%$search%") + ->orWhere('size_peti', 'like', "%$search%"); + }) + ->orWhereHas('kondisipeti', function ($kondisiPetiQuery) use ($search) { + $kondisiPetiQuery->where('nama_kondisi', 'like', "%$search%"); + }) + ->orWhere('packing_no', 'like', "%$search%") + ->orWhere('status', 'like', "%$search%"); + }); + } + + if ($perPage == 'Semua') { + $chunkSize = 100; + $petis = new Collection(); + $currentPage = 1; + + $query->chunk($chunkSize, function ($petiChunk) use ($petis, &$currentPage) { + foreach ($petiChunk as $peti) { + $peti->setAttribute('i', ($currentPage - 1) * $petiChunk->perPage() + 1); + $petis->push($peti); + $currentPage++; + } + }); + } else { + $petis = $query->paginate($perPage); + } + $data = [ - 'peti' => Peti::withTrashed()->get(), + 'peti' => $petis, + 'i' => ($petis->currentPage() - 1) * $petis->perPage() + 1, + 'search' => $search, 'active' => 'menu-Pettern_Lot_Peti' ]; return view('dashboard.Master_Data.Report.Pattern_lot_peti.index', $data); diff --git a/app/Http/Controllers/TypePetiController.php b/app/Http/Controllers/TypePetiController.php index 0efeb80..0a03c55 100644 --- a/app/Http/Controllers/TypePetiController.php +++ b/app/Http/Controllers/TypePetiController.php @@ -113,7 +113,27 @@ class TypePetiController extends Controller public function importTipePeti(Request $request) { - Excel::import(new TipePetiImport, request()->file('file')); - return redirect()->route('dashboard.typepeti.index')->with('success', 'Data tipe peti berhasil di import'); + $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); + } + } + } + + // Redirect back with import data and error message + return redirect()->back()->with('import', $import)->with('error', 'Data Import Tipe Peti gagal di import.'); + } } } diff --git a/app/Http/Controllers/WarehouseController.php b/app/Http/Controllers/WarehouseController.php index 7f021b5..4e550ea 100644 --- a/app/Http/Controllers/WarehouseController.php +++ b/app/Http/Controllers/WarehouseController.php @@ -4,11 +4,14 @@ namespace App\Http\Controllers; use App\Models\m_warehouse; use Illuminate\Http\Request; +use App\Imports\WarehouseImport; use Illuminate\Support\Facades\Auth; use Maatwebsite\Excel\Facades\Excel; +use Illuminate\Support\Facades\Session; use App\Http\Requests\ValidasiCreateWarehouse; use App\Http\Requests\ValidasiUpdateWarehouse; -use App\Imports\WarehouseImport; +use Maatwebsite\Excel\Exceptions\NoTypeDetectedException; +use Illuminate\Support\Facades\Log; class WarehouseController extends Controller { @@ -26,10 +29,10 @@ class WarehouseController extends Controller /** * Show the form for creating a new resource. */ - public function create() - { - // return view('dashboard.Master_Data.Warehouse.create'); - } + // public function create() + // { + // // return view('dashboard.Master_Data.Warehouse.create'); + // } /** * Store a newly created resource in storage. @@ -54,20 +57,20 @@ class WarehouseController extends Controller /** * Display the specified resource. */ - public function show($id) - { - // dd('oke'); - // return view('dashboard.Master_Data.Warehouse.show'); - } + // public function show($id) + // { + // // dd('oke'); + // // return view('dashboard.Master_Data.Warehouse.show'); + // } /** * Show the form for editing the specified resource. */ - public function edit() - { - // dd('oke'); - // return view('dashboard.Master_Data.Warehouse.edit'); - } + // public function edit() + // { + // // dd('oke'); + // // return view('dashboard.Master_Data.Warehouse.edit'); + // } /** * Update the specified resource in storage. @@ -104,7 +107,27 @@ class WarehouseController extends Controller public function importWarehouse(Request $request) { - Excel::import(new WarehouseImport, request()->file('file')); - return redirect()->route('dashboard.warehouse.index')->with('success', 'Data Warehouse berhasil diimport'); + $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); + } + } + } + + // Redirect back with import data and error message + return redirect()->back()->with('import', $import)->with('error', 'Data Import Warehouse gagal di import.'); + } } } diff --git a/app/Imports/CustomerImport.php b/app/Imports/CustomerImport.php index 7381f2d..497acbe 100644 --- a/app/Imports/CustomerImport.php +++ b/app/Imports/CustomerImport.php @@ -3,9 +3,11 @@ namespace App\Imports; use App\Models\Customer; +use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Maatwebsite\Excel\Concerns\ToModel; +use Maatwebsite\Excel\Events\AfterImport; use Maatwebsite\Excel\Concerns\WithHeadingRow; class CustomerImport implements ToModel, WithHeadingRow @@ -15,11 +17,27 @@ class CustomerImport implements ToModel, WithHeadingRow * * @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 + $existingCustomer = Customer::where('name', $row['name'])->first(); + + if ($existingCustomer) { + // Jika nama warehouse sudah ada, tambahkan pesan kesalahan ke dalam array $errors + $this->errors[] = new MessageBag(['Row ' . $this->rowCount => 'Data Customer sudah ada dalam database.']); + return null; // Abaikan data yang sudah ada + } + + // Increment rowCount setiap kali ada data yang diimpor + $this->rowCount++; + return new Customer([ 'name' => $row['name'], 'code_customer' => $row['code_customer'], @@ -29,4 +47,32 @@ class CustomerImport implements ToModel, WithHeadingRow 'created_by' => $user->fullname, ]); } + + 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; + } } diff --git a/app/Imports/KondisiPetiImport.php b/app/Imports/KondisiPetiImport.php index f750a04..9bc032c 100644 --- a/app/Imports/KondisiPetiImport.php +++ b/app/Imports/KondisiPetiImport.php @@ -4,8 +4,10 @@ namespace App\Imports; use App\Models\Kondisi_Peti; use Illuminate\Support\Collection; +use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\Auth; use Maatwebsite\Excel\Concerns\ToModel; +use Maatwebsite\Excel\Events\AfterImport; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\WithHeadingRow; @@ -16,15 +18,59 @@ class KondisiPetiImport implements ToModel, WithHeadingRow * * @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 + $existingKondisiPeti = Kondisi_Peti::where('nama_kondisi', $row['nama_kondisi'])->first(); + + if ($existingKondisiPeti) { + // Jika nama warehouse sudah ada, tambahkan pesan kesalahan ke dalam array $errors + $this->errors[] = new MessageBag(['Row ' . $this->rowCount => 'Data Kondisi Peti sudah ada dalam database.']); + return null; // Abaikan data yang sudah ada + } + + // Increment rowCount setiap kali ada data yang diimpor + $this->rowCount++; + return new Kondisi_Peti([ 'nama_kondisi' => $row['nama_kondisi'], 'deskripsi_kondisi' => $row['deskripsi_kondisi'], 'created_by' => $user->fullname, ]); } + + 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; + } } diff --git a/app/Imports/PetiImport.php b/app/Imports/PetiImport.php index 87ea035..32463df 100644 --- a/app/Imports/PetiImport.php +++ b/app/Imports/PetiImport.php @@ -3,35 +3,162 @@ namespace App\Imports; use App\Models\Peti; +use App\Models\Customer; +use App\Models\Type_peti; +use App\Models\m_warehouse; +use App\Models\Kondisi_Peti; use Illuminate\Support\Collection; +use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\Auth; use Maatwebsite\Excel\Concerns\ToModel; +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 { - /** - * @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(); + $existingPeti = Peti::where('fix_lot', $row['fix_lot'])->first(); + + if ($existingPeti) { + $this->errors[] = new MessageBag(['Row ' . $this->rowCount => 'Data Peti sudah ada dalam database.']); + return null; + } + + $tipePeti = Type_peti::firstOrCreate( + ['type' => $row['tipe_peti_id']], + ['size_peti' => 'Default Size', 'description' => 'Default Description'] + ); + + $customer = Customer::firstOrCreate( + ['name' => $row['customer_id']], + ['code_customer' => 'Default Code', 'lot_no' => 'Default Lot No', 'no_tlp' => 'Default No Tlp', 'address' => 'Default Address'] + ); + + $warehouse = m_warehouse::firstOrCreate( + ['name' => $row['warehouse_id']], + ['description' => 'Default Description', 'address' => 'Default Address'] + ); + + $kondisiPeti = Kondisi_Peti::firstOrCreate( + ['nama_kondisi' => $row['kondisipeti_id']], + ['deskripsi_kondisi' => 'Default Deskripsi'] + ); + + $this->rowCount++; + return new Peti([ 'created_by' => $user->fullname, - 'tipe_peti_id' => $row['tipe_peti_id'], + 'tipe_peti_id' => $tipePeti->id, 'warna' => $row['warna'], - 'customer_id' => $row['customer_id'], - 'warehouse_id' => $row['warehouse_id'], + 'customer_id' => $customer->id, + 'warehouse_id' => $warehouse->id, 'date_pembuatan' => now(), - 'kondisipeti_id' => $row['kondisipeti_id'], + 'kondisipeti_id' => $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) { + $importedData = $event->getReader()->getDelegate()->getActiveSheet()->toArray(); + $filteredData = array_filter($importedData, function ($row) { + return $row[0] !== null; + }); + + $event->getReader()->getDelegate()->getActiveSheet()->fromArray($filteredData); + }, + ]; + } + + public function getErrors() + { + return $this->errors; + } + + public function getRowCount() + { + return $this->rowCount; + } } diff --git a/app/Imports/TipePetiImport.php b/app/Imports/TipePetiImport.php index 62d3fae..4f2e412 100644 --- a/app/Imports/TipePetiImport.php +++ b/app/Imports/TipePetiImport.php @@ -4,8 +4,10 @@ namespace App\Imports; use App\Models\Type_peti; use Illuminate\Support\Collection; +use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\Auth; use Maatwebsite\Excel\Concerns\ToModel; +use Maatwebsite\Excel\Events\AfterImport; use App\Models\Type_peti as ModelsType_peti; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\WithHeadingRow; @@ -17,11 +19,27 @@ class TipePetiImport implements ToModel, WithHeadingRow * * @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 + $existingTipePeti = Type_peti::where('type', $row['type'])->first(); + + if ($existingTipePeti) { + // Jika nama warehouse sudah ada, tambahkan pesan kesalahan ke dalam array $errors + $this->errors[] = new MessageBag(['Row ' . $this->rowCount => 'Data Tipe peti sudah ada dalam database.']); + return null; // Abaikan data yang sudah ada + } + + // Increment rowCount setiap kali ada data yang diimpor + $this->rowCount++; + return new Type_peti([ 'type' => $row['type'], 'size_peti' => $row['size_peti'], @@ -29,4 +47,32 @@ class TipePetiImport implements ToModel, WithHeadingRow 'created_by' => $user->fullname, ]); } + + 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; + } } diff --git a/app/Imports/WarehouseImport.php b/app/Imports/WarehouseImport.php index c0025b2..28de7ab 100644 --- a/app/Imports/WarehouseImport.php +++ b/app/Imports/WarehouseImport.php @@ -3,9 +3,12 @@ namespace App\Imports; use App\Models\m_warehouse; +use Illuminate\Validation\Rule; use Illuminate\Support\Collection; +use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\Auth; use Maatwebsite\Excel\Concerns\ToModel; +use Maatwebsite\Excel\Events\AfterImport; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\WithHeadingRow; @@ -16,11 +19,27 @@ class WarehouseImport implements ToModel, WithHeadingRow * * @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 + $existingWarehouse = m_warehouse::where('name', $row['name'])->first(); + + if ($existingWarehouse) { + // Jika nama warehouse sudah ada, tambahkan pesan kesalahan ke dalam array $errors + $this->errors[] = new MessageBag(['Row ' . $this->rowCount => 'Data warehouse sudah ada dalam database.']); + return null; // Abaikan data yang sudah ada + } + + // Increment rowCount setiap kali ada data yang diimpor + $this->rowCount++; + + // Buat model baru jika nama warehouse belum ada return new m_warehouse([ 'name' => $row['name'], 'description' => $row['description'], @@ -28,4 +47,32 @@ class WarehouseImport implements ToModel, WithHeadingRow 'created_by' => $user->fullname, ]); } + + 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; + } } diff --git a/resources/views/dashboard/Master_Data/Manajemen_Peti/Peti/index.blade.php b/resources/views/dashboard/Master_Data/Manajemen_Peti/Peti/index.blade.php index ab53122..9491c42 100644 --- a/resources/views/dashboard/Master_Data/Manajemen_Peti/Peti/index.blade.php +++ b/resources/views/dashboard/Master_Data/Manajemen_Peti/Peti/index.blade.php @@ -19,7 +19,7 @@
Data Peti
-
+
Tambah Peti @@ -33,7 +33,7 @@
- Import Customer + Import Peti
@@ -96,90 +96,40 @@ @forelse ($peti as $data_peti) - @if ($search) - @if (stripos($data_peti->fix_lot, $search) !== false || - stripos($data_peti->created_by, $search) !== false || - stripos($data_peti->customer->name, $search) !== false || - stripos($data_peti->warehouse->name, $search) !== false || - stripos($data_peti->customer->code_customer, $search) !== false || - stripos($data_peti->tipe_peti->type, $search) !== false || - stripos($data_peti->tipe_peti->size_peti, $search) !== false || - stripos($data_peti->customer->lot_no, $search) !== false || - stripos($data_peti->kondisipeti->nama_kondisi, $search) !== false || - stripos($data_peti->packing_no, $search) !== false || - stripos($data_peti->status, $search) !== false) - - - {{ $i++ }} - - - {{ $data_peti->fix_lot }} - {{ $data_peti->created_by }} - {{ $data_peti->customer->name }} - {{ $data_peti->warehouse->name }} - {{ $data_peti->customer->code_customer }} - {{ $data_peti->tipe_peti->type }} - {{ $data_peti->tipe_peti->size_peti }} - {{ $data_peti->customer->lot_no }} - {{ $data_peti->kondisipeti->nama_kondisi }} - {{ $data_peti->packing_no }} - {{ $data_peti->status }} - - - - - - - -
- @csrf - @method('DELETE') - -
- - - @endif - @else - - - {{ $i++ }} - - {{ $data_peti->fix_lot }} - {{ $data_peti->created_by }} - {{ $data_peti->customer->name }} - {{ $data_peti->warehouse->name }} - {{ $data_peti->customer->code_customer }} - {{ $data_peti->tipe_peti->type }} - {{ $data_peti->tipe_peti->size_peti }} - {{ $data_peti->customer->lot_no }} - {{ $data_peti->kondisipeti->nama_kondisi }} - {{ $data_peti->packing_no }} - {{ $data_peti->status }} - - - - - - - -
- @csrf - @method('DELETE') - -
- - - @endif + + {{ $i++ }} + + + {{ $data_peti->fix_lot }} + {{ $data_peti->created_by }} + {{ $data_peti->customer->name }} + {{ $data_peti->warehouse->name }} + {{ $data_peti->customer->code_customer }} + {{ $data_peti->tipe_peti->type }} + {{ $data_peti->tipe_peti->size_peti }} + {{ $data_peti->customer->lot_no }} + {{ $data_peti->kondisipeti->nama_kondisi }} + {{ $data_peti->packing_no }} + {{ $data_peti->status }} + + + + + + + +
+ @csrf + @method('DELETE') + +
+ + @empty Data Kosong @@ -189,28 +139,29 @@
-
+
@if (!$peti->isEmpty()) -

Showing {{ $peti->firstItem() }} to {{ $peti->lastItem() }} of +

Showing {{ $peti->firstItem() }} to + {{ $peti->lastItem() }} of {{ $peti->total() }} entries

@endif
-
+
@if ($peti->total() > $peti->perPage())
- -
diff --git a/resources/views/dashboard/Master_Data/Report/Pattern_lot_peti/index.blade.php b/resources/views/dashboard/Master_Data/Report/Pattern_lot_peti/index.blade.php index 176c79f..86b0750 100644 --- a/resources/views/dashboard/Master_Data/Report/Pattern_lot_peti/index.blade.php +++ b/resources/views/dashboard/Master_Data/Report/Pattern_lot_peti/index.blade.php @@ -25,12 +25,46 @@
+
+
+ +
+ + + + +
+
+
+ +
+ + + + +
+
+
- +
- {{-- --}} @@ -68,6 +102,39 @@
NoUserFIX LOT Customer WH
+
+
+ + @if (!$peti->isEmpty()) +

Showing {{ $peti->firstItem() }} to + {{ $peti->lastItem() }} of + {{ $peti->total() }} entries

+ @endif +
+
+ @if ($peti->total() > $peti->perPage()) + + @endif +
+
@endsection diff --git a/resources/views/dashboard/Master_Data/Warehouse/index.blade.php b/resources/views/dashboard/Master_Data/Warehouse/index.blade.php index fb47759..7fafb01 100644 --- a/resources/views/dashboard/Master_Data/Warehouse/index.blade.php +++ b/resources/views/dashboard/Master_Data/Warehouse/index.blade.php @@ -3,6 +3,7 @@ @section('content') @include('layouts.components.alert-prompt') + @if (auth()->user()->role_id == 1)
diff --git a/resources/views/layouts/components/alert-prompt.blade.php b/resources/views/layouts/components/alert-prompt.blade.php index 60e7862..28bc3a2 100644 --- a/resources/views/layouts/components/alert-prompt.blade.php +++ b/resources/views/layouts/components/alert-prompt.blade.php @@ -14,6 +14,12 @@
@endif +@if (Session::has('error')) + +@endif + + + {{-- --}} + + - + --}} + {{-- --}} + @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 From 786f5476e68362aa0f1309015cde799d14658986 Mon Sep 17 00:00:00 2001 From: Gunawan19621 Date: Mon, 4 Dec 2023 22:03:39 +0700 Subject: [PATCH 3/3] update select menggunakan select2 --- app/Http/Controllers/DisposalController.php | 30 ++++++++ app/Http/Controllers/PeminjamanController.php | 35 ++++----- app/Http/Controllers/TransferController.php | 29 +++++++- .../views/dashboard/Disposal/create.blade.php | 58 ++++++++++++++- .../dashboard/Peminjaman/create.blade.php | 74 ++++++++++++------- .../views/dashboard/Transfer/create.blade.php | 56 +++++++++++++- routes/web.php | 2 + 7 files changed, 229 insertions(+), 55 deletions(-) diff --git a/app/Http/Controllers/DisposalController.php b/app/Http/Controllers/DisposalController.php index 0d7e07b..59c2174 100644 --- a/app/Http/Controllers/DisposalController.php +++ b/app/Http/Controllers/DisposalController.php @@ -6,7 +6,9 @@ use App\Models\Peti; use App\Models\Customer; use App\Models\Disposal; use App\Models\m_warehouse; +use Illuminate\Http\Request; use Symfony\Component\Uid\Uuid; +use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Auth; use App\Http\Requests\Disposal\ValidasiCreateDisposal; use App\Http\Requests\Disposal\ValidasiUpdateDisposal; @@ -164,4 +166,32 @@ class DisposalController extends Controller return redirect()->back()->with('error', 'Data disposal peti gagal dihapus'); } } + + /** + * Search peti by fix_lot + */ + public function autoCompleteSearch(Request $request): JsonResponse + { + $petiData = []; + $customerData = []; + + if ($request->filled('q')) { + $petiData = Peti::select("fix_lot", "id") + ->where('fix_lot', 'LIKE', '%' . $request->get('q') . '%') + ->where(function ($query) { + $query->whereHas('assetStatuses', function ($subquery) { + $subquery->where('status', '!=', 0) + ->where('created_at', '=', function ($maxQuery) { + $maxQuery->selectRaw('MAX(created_at)') + ->from('asset_statuses') + ->whereColumn('peti_id', 'petis.id'); + }); + })->orWhereDoesntHave('assetStatuses'); + }) + ->where('status', 'AKTIF') + ->get(); + } + + return response()->json(['peti' => $petiData, 'customer' => $customerData]); + } } diff --git a/app/Http/Controllers/PeminjamanController.php b/app/Http/Controllers/PeminjamanController.php index 5888e50..c3a784c 100644 --- a/app/Http/Controllers/PeminjamanController.php +++ b/app/Http/Controllers/PeminjamanController.php @@ -20,18 +20,6 @@ class PeminjamanController extends Controller /** * Display a listing of the resource. */ - // public function index() - // { - - // $data = [ - // // 'peminjaman' => asset_status::get(), - // 'peminjaman' => asset_status::orderBy('created_at', 'desc')->get(), - // 'warehouse' => m_warehouse::get(), - // 'active' => 'menu-peminjaman', - // ]; - - // return view('dashboard.Peminjaman.index', $data); - // } public function index(Request $request) { $perPage = $request->input('perPage', 5); @@ -223,19 +211,26 @@ class PeminjamanController extends Controller public function autoCompleteSearch(Request $request): JsonResponse { - $data = []; + $petiData = []; + $customerData = []; if ($request->filled('q')) { - $data = Peti::select("fix_lot", "id") + $petiData = Peti::select("fix_lot", "id") ->where('fix_lot', 'LIKE', '%' . $request->get('q') . '%') + ->where(function ($query) { + $query->whereHas('assetStatuses', function ($subquery) { + $subquery->where('status', '!=', 0) + ->where('created_at', '=', function ($maxQuery) { + $maxQuery->selectRaw('MAX(created_at)') + ->from('asset_statuses') + ->whereColumn('peti_id', 'petis.id'); + }); + })->orWhereDoesntHave('assetStatuses'); + }) + ->where('status', 'AKTIF') ->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); + return response()->json(['peti' => $petiData, 'customer' => $customerData]); } } diff --git a/app/Http/Controllers/TransferController.php b/app/Http/Controllers/TransferController.php index 462f4ff..4a35337 100644 --- a/app/Http/Controllers/TransferController.php +++ b/app/Http/Controllers/TransferController.php @@ -3,11 +3,12 @@ namespace App\Http\Controllers; use App\Models\Peti; -use Symfony\Component\Uid\Uuid; use App\Models\Customer; use App\Models\Transfer; use App\Models\m_warehouse; use Illuminate\Http\Request; +use Symfony\Component\Uid\Uuid; +use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Auth; use App\Http\Requests\Transfer\ValidasiCreateTransfer; use App\Http\Requests\Transfer\ValidasiUpdateTransfer; @@ -220,4 +221,30 @@ class TransferController extends Controller return redirect()->back()->with('error', 'Data transfer peti gagal dihapus'); } } + + // Proses select2 autocomplete + public function autoCompleteSearch(Request $request): JsonResponse + { + $petiData = []; + $customerData = []; + + if ($request->filled('q')) { + $petiData = Peti::select("fix_lot", "id") + ->where('fix_lot', 'LIKE', '%' . $request->get('q') . '%') + ->where(function ($query) { + $query->whereHas('assetStatuses', function ($subquery) { + $subquery->where('status', '!=', 0) + ->where('created_at', '=', function ($maxQuery) { + $maxQuery->selectRaw('MAX(created_at)') + ->from('asset_statuses') + ->whereColumn('peti_id', 'petis.id'); + }); + })->orWhereDoesntHave('assetStatuses'); + }) + ->where('status', 'AKTIF') + ->get(); + } + + return response()->json(['peti' => $petiData, 'customer' => $customerData]); + } } diff --git a/resources/views/dashboard/Disposal/create.blade.php b/resources/views/dashboard/Disposal/create.blade.php index 8c7254a..c3f0563 100644 --- a/resources/views/dashboard/Disposal/create.blade.php +++ b/resources/views/dashboard/Disposal/create.blade.php @@ -16,7 +16,8 @@
- + {{-- + --}} - + @foreach ($warehouse as $data) @endforeach @@ -67,6 +68,55 @@
+ + + + {{-- --}} + + @@ -222,29 +237,34 @@ customerSelect.value = customerId; }); --}} + + {{-- --}} - - @endsection diff --git a/resources/views/dashboard/Transfer/create.blade.php b/resources/views/dashboard/Transfer/create.blade.php index 1a2cd0b..abadd7e 100644 --- a/resources/views/dashboard/Transfer/create.blade.php +++ b/resources/views/dashboard/Transfer/create.blade.php @@ -16,7 +16,8 @@
- + {{-- + --}} @@ -42,7 +43,7 @@