Siopas Inventory PETI for ISTW Website
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.

209 lines
7.3 KiB

12 months ago
<?php
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;
12 months ago
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
12 months ago
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Events\AfterImport;
12 months ago
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
// class PetiImport implements ToModel, WithHeadingRow
// {
// private $rowCount = 0;
// private $errors;
// 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', 'status_isi_peti'];
// 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();
// 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,
// 'updated_by' => $user->fullname,
// 'tipe_peti_id' => $tipePeti->id,
// 'warna' => $row['warna'],
// 'customer_id' => $customer->id,
// 'warehouse_id' => $warehouse->id,
// 't_warehouse_id' => $warehouse->id,
// 'date_pembuatan' => now(),
// 'kondisipeti_id' => $kondisiPeti->id,
// 'packing_no' => $row['packing_no'],
// 'fix_lot' => $row['fix_lot'],
// 'status' => $row['status'],
// 'status_isi' => $row['status_isi_peti'],
// ]);
// }
// 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;
// }
// }
12 months ago
class PetiImport implements ToModel, WithHeadingRow
{
private $rowCount = 0;
private $errors;
12 months ago
public function model(array $row)
{
// dd($row);
// Pemeriksaan apakah semua kolom yang dibutuhkan ada dalam format yang benar
$requiredColumns = ['tipe_peti', 'warna', 'nama_customer', 'nama_gudang', 'kondisi_peti', 'packing_no', 'fix_lot', 'status', 'status_isi_peti'];
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
}
}
// Validasi untuk kolom 'status_isi_peti'
$validStatusIsiPeti = ['Ada', 'Tidak Ada'];
$statusIsiPeti = ucwords(strtolower(trim($row['status_isi_peti']))); // Ubah kapitalisasi
if (!in_array($statusIsiPeti, $validStatusIsiPeti)) {
$this->errors[] = 'Nilai pada kolom status_isi_peti pada baris ' . $this->rowCount . ' tidak valid.';
return null;
}
// Set nilai status_isi berdasarkan 'Ada Isinya' atau 'Tidak Ada Isinya'
$statusIsi = ($statusIsiPeti === 'Ada') ? 1 : 0;
12 months ago
$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']],
['size_peti' => 'Default Size', 'description' => 'Default Description']
);
$customer = Customer::firstOrCreate(
['name' => $row['nama_customer']],
['code_customer' => 'Default Code', 'lot_no' => 'Default Lot No', 'no_tlp' => 'Default No Tlp', 'address' => 'Default Address']
);
$warehouse = m_warehouse::firstOrCreate(
['name' => $row['nama_gudang']],
['description' => 'Default Description', 'address' => 'Default Address']
);
$kondisiPeti = Kondisi_Peti::firstOrCreate(
['nama_kondisi' => $row['kondisi_peti']],
['deskripsi_kondisi' => 'Default Deskripsi']
);
$this->rowCount++;
12 months ago
return new Peti([
'created_by' => $user->fullname,
'updated_by' => $user->fullname,
'tipe_peti_id' => $tipePeti->id,
'warna' => strtoupper($row['warna']),
'customer_id' => $customer->id,
'warehouse_id' => $warehouse->id,
't_warehouse_id' => $warehouse->id,
12 months ago
'date_pembuatan' => now(),
'kondisipeti_id' => $kondisiPeti->id,
'packing_no' => strtoupper($row['packing_no']),
'fix_lot' => strtoupper($row['fix_lot']),
'status' => strtoupper($row['status']),
'status_isi' => $statusIsi,
12 months ago
]);
}
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;
}
12 months ago
}