You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
monitor-doc/app/Imports/DocumentImport.php

99 lines
3.5 KiB
PHP

<?php
namespace App\Imports;
use App\Models\Category;
use App\Models\Company;
use App\Models\Document;
use App\Models\Type;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class DocumentImport implements ToCollection, WithHeadingRow
{
/**
* @param Collection $collection
*/
public function collection(Collection $rows)
{
$no = Document::count();
$documents = collect();
foreach($rows as $row) {
$company = Company::where('name', 'like', $row['nama_perusahaan'])->first();
$type = Type::where('name', 'like', $row['jenis'])->first();
$cat = Category::where('name', 'like', $row['kategori'])->first();
if ($company == null) {
$company = Company::first();
}
if ($type == null) {
$type = Type::first();
}
if ($cat == null) {
$cat = Category::first();
}
$no += 1;
try {
$tanggal_terbit = Carbon::createFromFormat('d-m-Y',$row['tanggal_terbit']);
$tanggal_jatuh_tempo = Carbon::createFromFormat('d-m-Y',$row['tanggal_jatuh_tempo']);
} catch(\Exception $e) {
try {
$tanggal_terbit = Carbon::createFromFormat('d/m/Y',$row['tanggal_terbit']);
$tanggal_jatuh_tempo = Carbon::createFromFormat('d/m/Y',$row['tanggal_jatuh_tempo']);
} catch (\Exception $e) {
try {
$tanggal_terbit = Carbon::createFromFormat('m/d/Y',$row['tanggal_terbit']);
$tanggal_jatuh_tempo = Carbon::createFromFormat('m/d/Y',$row['tanggal_jatuh_tempo']);
} catch (\Exception $e) {
try {
$tanggal_terbit = Carbon::createFromFormat('m-d-Y',$row['tanggal_terbit']);
$tanggal_jatuh_tempo = Carbon::createFromFormat('m-d-Y',$row['tanggal_jatuh_tempo']);
} catch(\Exception $e) {
try {
$tanggal_terbit = Carbon::parse($row['tanggal_terbit']);
$tanggal_jatuh_tempo = Carbon::parse($row['tanggal_jatuh_tempo']);
} catch (\Exception $e) {}
}
}
}
}
$documents->add([
"no" => $no,
"no_doc" => $row['no'],
"name" => $row['nama'],
"publisher" => $row['penerbit'],
"description" => $row['keterangan'],
"publish_date" => $tanggal_terbit,
"due_date" => $tanggal_jatuh_tempo,
"status" => $row['status'] == 'Ya' ? Document::STATUS_YES : Document::STATUS_NO,
"type" => $row['tipe'] == 'Tetap' ? Document::TYPE_TETAP : Document::TYPE_TIDAK_TETAP,
"document" => '',
"company_id" => $company->id,
"type_id" => $type->id,
"category_id" => $cat->id,
"user_id" => auth()->user()->id,
"created_at" => now()
]);
}
if ($documents->count() > 0) {
Document::insert($documents->toArray());
}
}
public function chunkSize(): int
{
return 1000;
}
}