This commit is contained in:
2022-08-04 08:39:11 +07:00
parent 4fb1723b76
commit ab94607e81
5 changed files with 198 additions and 15 deletions

143
app/Jobs/ProcessImport.php Normal file
View File

@@ -0,0 +1,143 @@
<?php
namespace App\Jobs;
use App\Exceptions\ImportRowException;
use App\Models\ImportLog;
use App\Services\ImportService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\Internal\Services\ExclusionService;
use Storage;
class ProcessImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $import_log;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(ImportLog $importLog)
{
$this->import_log = $importLog;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$file = $this->import_log->files()->where('type', 'import')->first();
$this->import_log->fill(['status' => 'progress'])->save();
$corporate = $this->import_log->importable;
$import = new ImportService();
$import->read(Storage::path($file->full_path));
$import->write(Storage::disk('public')->path('result/'.$this->import_log->file_path), 'xsls');
foreach ($import->sheetsIterator() as $sheetIndex => $sheet) {
$doc_headers_indexes = [];
foreach ($sheet->getRowIterator() as $index => $row) {
if ($index == 1) { // First Row Must be Header
foreach ($row->getCells() as $index => $cell) {
$title = $cell->getValue();
$title = preg_replace( "/\r|\n/", " ", $title );
$title = preg_replace('/\xc2\xa0/', " ", $title );
$title = rtrim($title);
$title = ltrim($title);
$doc_headers_indexes[$index] = $title;
}
// Write Header to File
$result_headers = array_merge($doc_headers_indexes, ['Ingest Code', 'Ingest Note']);
$import->addArrayToRow($result_headers);
// TODO Validate if First Row not Header
} else { // Next Row Should be Data
$row_data = [];
$row_map = [
0 => 'code',
1 => 'description',
2 => 'ip_exclusion',
3 => 'op_exclusion',
4 => 'de_exclusion',
5 => 'ma_exclusion',
6 => 'sp_exclusion',
7 => 'pre_exist_exclusion',
8 => 'op_de_exclusion',
9 => 'keterangan',
10 => 'maternity_waiting'
];
foreach ($row->getCells() as $header_index => $cell) {
if (isset($row_map[$header_index])) {
$value = $cell->getValue();
$value = preg_replace( "/\r|\n/", " ", $value );
$value = preg_replace('/\xc2\xa0/', " ", $value );
$value = rtrim($value);
$value = ltrim($value);
$row_data[$row_map[$header_index]] = $cell->getValue();
}
}
try { // Process the Row Data
if (
// empty($row_data['code']) &&
// empty($row_data['description']) &&
empty($row_data['ip_exclusion']) &&
empty($row_data['op_exclusion']) &&
empty($row_data['de_exclusion']) &&
empty($row_data['ma_exclusion']) &&
empty($row_data['sp_exclusion']) &&
empty($row_data['pre_exis_exclusion']) &&
empty($row_data['op_de_exclusion']) &&
empty($row_data['maternity_waiting'])) {
continue;
}
// Save the Row
$exclusionService = new ExclusionService();
$exclusionService->handleDiagnosisExclusionRow($corporate, $row_data);
// Write Success Result to File
$import->addArrayToRow(array_merge($row_data, [
'Ingest Code' => 200,
'Ingest Note' => 'Success',
]), $sheet->getName());
} catch (ImportRowException $e) {
// Write Data Validation Error to File
$import->addArrayToRow(array_merge($row_data, [
'Ingest Code' => $e->getCode(),
'Ingest Note' => $e->getMessage(),
]), $sheet->getName());
} catch (\Exception $e) {
throw new \Exception($e);
// Write Server Error to File
$import->addArrayToRow(array_merge($row_data, [
'Ingest Code' => 500,
'Ingest Note' => env('APP_DEBUG') ? $e->getMessage() : 'Server Error',
]), $sheet->getName());
}
}
}
break; // Only Read First Row
}
$import->reader->close();
Storage::delete('temp/'.$file_name);
$import->writer->close();
}
}

View File

@@ -12,18 +12,9 @@ class ImportLog extends Model
{
use HasFactory, SoftDeletes, Blameable;
protected static function boot()
{
parent::boot();
// protected $keyType = 'string';
static::creating(function ($model) {
try {
$model->uuid = (string) Str::orderedUuid(); // generate uuid
} catch (\Exception $e) {
abort(500, $e->getMessage());
}
});
}
// public $incrementing = false;
protected $fillable = [
'importable_type',
@@ -34,6 +25,19 @@ class ImportLog extends Model
'progress',
];
// protected static function boot()
// {
// parent::boot();
// static::creating(function ($model) {
// try {
// $model->id = (string) Str::orderedUuid(); // generate uuid
// } catch (\Exception $e) {
// abort(500, $e->getMessage());
// }
// });
// }
public function files()
{
return $this->morphMany(File::class, 'fileable');

View File

@@ -14,7 +14,7 @@ return new class extends Migration
public function up()
{
Schema::create('import_logs', function (Blueprint $table) {
$table->uuid()->primary();
$table->id();
$table->morphs('importable');
$table->string('type')->nullable();
$table->string('file_path')->nullable();

View File

@@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('jobs');
}
};

View File

@@ -6,7 +6,7 @@ import AddIcon from '@mui/icons-material/Add';
import UploadIcon from '@mui/icons-material/Upload';
import CancelIcon from '@mui/icons-material/Cancel';
// hooks
import React, { Component, useEffect, useRef, useState } from 'react';
import React, { ChangeEvent, Component, useEffect, useRef, useState } from 'react';
import useSettings from '../../../hooks/useSettings';
import { Link, useNavigate, useParams, useSearchParams } from 'react-router-dom';
// components
@@ -143,7 +143,7 @@ export default function PlanList() {
total: 0
});
const loadDataTableData = async (appliedFilter = null) => {
const loadDataTableData = async (appliedFilter : any | null = null) => {
setDataTableLoading(true);
const filter = appliedFilter ? appliedFilter : Object.fromEntries([...searchParams.entries()]);
const response = await axios.get('/corporates/'+corporate_id+'/divisions', { params: filter });
@@ -157,7 +157,7 @@ export default function PlanList() {
fontWeight: 'bold',
};
const applyFilter = async (searchFilter) => {
const applyFilter = async (searchFilter: any) => {
await loadDataTableData({ "search" : searchFilter });
setSearchParams({ "search" : searchFilter });
}