Add Diagnosis Exclusion

This commit is contained in:
2022-08-03 11:24:09 +07:00
parent 8c78fd3d84
commit f72a641f56
27 changed files with 20708 additions and 3 deletions

View File

@@ -0,0 +1,82 @@
<?php
namespace Modules\Internal\Http\Controllers\Api;
use App\Models\Icd;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class DiagnosisController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
{
$diagnosis = Icd::withTrashed()->filter($request->toArray())->paginate();
return $diagnosis;
}
/**
* Show the form for creating a new resource.
* @return Renderable
*/
public function create()
{
return view('internal::create');
}
/**
* Store a newly created resource in storage.
* @param Request $request
* @return Renderable
*/
public function store(Request $request)
{
//
}
/**
* Show the specified resource.
* @param int $id
* @return Renderable
*/
public function show($id)
{
return view('internal::show');
}
/**
* Show the form for editing the specified resource.
* @param int $id
* @return Renderable
*/
public function edit($id)
{
return view('internal::edit');
}
/**
* Update the specified resource in storage.
* @param Request $request
* @param int $id
* @return Renderable
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
* @param int $id
* @return Renderable
*/
public function destroy($id)
{
//
}
}

View File

@@ -0,0 +1,220 @@
<?php
namespace Modules\Internal\Http\Controllers\Api;
use App\Exceptions\ImportRowException;
use App\Models\Corporate;
use App\Models\Exclusion;
use App\Models\Icd;
use App\Models\ImportLog;
use App\Services\ImportService;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Modules\Internal\Services\ExclusionService;
use Modules\Internal\Transformers\DiagnosisExclusionResource;
class DiagnosisExclusionController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request, $corporate_id)
{
$exclusions = Exclusion::query()
->where('corporate_id', $corporate_id)
->with(['exclusionable', 'rules'])
->filter($request->toArray())
->paginate();
return response()->json(DiagnosisExclusionResource::collection($exclusions)->response()->getData(true));
}
/**
* Show the form for creating a new resource.
* @return Renderable
*/
public function create()
{
return view('internal::create');
}
/**
* Store a newly created resource in storage.
* @param Request $request
* @return Renderable
*/
public function store(Request $request)
{
//
}
/**
* Show the specified resource.
* @param int $id
* @return Renderable
*/
public function show($id)
{
return view('internal::show');
}
/**
* Show the form for editing the specified resource.
* @param int $id
* @return Renderable
*/
public function edit($id)
{
return view('internal::edit');
}
/**
* Update the specified resource in storage.
* @param Request $request
* @param int $id
* @return Renderable
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
* @param int $id
* @return Renderable
*/
public function destroy($id)
{
//
}
public function import(Request $request, $corporate_id)
{
$request->validate([
'file' => 'required|file|mimes:xls,xlsx,csv,txt',
]);
// dd($request->toArray());
$file_name = now()->getPreciseTimestamp(3).'-'.$request->file('file')->getClientOriginalName();
$file = $request->file('file')->storeAs('temp', $file_name);
$corporate = Corporate::findOrFail($corporate_id);
$importLog = $corporate->importLogs()->create([
'type' => 'diagnosis-exclusions',
'file_path' => $file,
'status' => 'pending',
'progress' => 0,
]);
$import = new ImportService();
$import->read(Storage::path('temp/'.$file_name));
$import->write(Storage::disk('public')->path('temp/result-'.$file_name), '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();
return [
// 'total_successed_row' => $imported_plan_data,
// 'total_failed_row' => count($failed_plan_data),
// 'failed_row' => $failed_plan_data,
'result_file' => [
'url' => Storage::disk('public')->url('temp/result-'.$file_name),
'name' => 'result-'.$file_name,
]
];
}
}

View File

@@ -6,6 +6,8 @@ use Modules\Internal\Http\Controllers\Api\BenefitController;
use Modules\Internal\Http\Controllers\Api\CorporateBenefitController;
use Modules\Internal\Http\Controllers\Api\CorporateController;
use Modules\Internal\Http\Controllers\Api\CorporatePlanController;
use Modules\Internal\Http\Controllers\Api\DiagnosisController;
use Modules\Internal\Http\Controllers\Api\DiagnosisExclusionController;
use Modules\Internal\Http\Controllers\Api\DivisionController;
use Modules\Internal\Http\Controllers\Api\MemberController;
use Modules\Internal\Http\Controllers\Api\PlanController;
@@ -63,5 +65,16 @@ Route::prefix('internal')->group(function () {
Route::get('corporates/{corporate_id}/members', [MemberController::class, 'index']);
Route::post('corporates/{corporate_id}/members/import', [MemberController::class, 'import']);
Route::get('corporates/{corporate_id}/diagnosis-exclusions', [DiagnosisExclusionController::class, 'index']);
Route::post('corporates/{corporate_id}/diagnosis-exclusions/import', [DiagnosisExclusionController::class, 'import']);
// Route::get('corporates/{corporate_id}/diagnosis-exclusions', [DiagnosisExclusionController::class, 'index']);
// Route::get('corporates/{corporate_id}/diagnosis-exclusions/import', [DiagnosisExclusionController::class, 'import']);
Route::get('master/diagnosis', [DiagnosisController::class, 'index']);
});
// Route::get('something', [DiagnosisExclusionController::class, 'index']);
});

View File

@@ -0,0 +1,220 @@
<?php
namespace Modules\Internal\Services;
use App\Exceptions\ImportRowException;
use App\Models\Benefit;
use App\Models\Corporate;
use App\Models\Icd;
use App\Models\Plan;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
class ExclusionService
{
protected function validateDiagnosisExclusionRow($row)
{
// if (empty($row['service_code'])) {
// throw new ImportRowException(__('plan.RECORD_TYPE_REQUIRED'), 0, null, $row);
// }
}
public function handleDiagnosisExclusionRow(Corporate $corporate, $row)
{
try {
$this->validateDiagnosisExclusionRow($row);
if (!empty($row['ip_exclusion'])) {
$excl_array = explode('|', $row['ip_exclusion']);
if ($excl_array[0] == '3') {
$icd = Icd::where('code', $row['code'])->first();
$exclusion = $icd->exclusions()->create([
'corporate_id' => $corporate->id,
'service_code' => 'OP',
'type' => 'diagnosis'
]);
if (!empty($excl_array[1])) { //msc
$msc = explode(',', $excl_array[1]);
collect($msc)->each(function($m) use ($exclusion) {
$exclusion->rules()->create([
'name' => 'msc',
'values' => $m
]);
});
}
if (!empty($excl_array[2])) { //genders
$genders = explode(',', $excl_array[2]);
collect($genders)->each(function($gender) use ($exclusion) {
$exclusion->rules()->create([
'name' => 'gender',
'values' => $gender
]);
});
}
if (!empty($excl_array[3])) { //min_age
$exclusion->rules()->create([
'name' => 'min_age',
'values' => $excl_array[3]
]);
}
if (!empty($excl_array[4])) { //max_age
$exclusion->rules()->create([
'name' => 'max_age',
'values' => $excl_array[4]
]);
}
if (!empty($excl_array[5])) { //plans
$exclusion->rules()->create([
'name' => 'plan',
'values' => $excl_array[5]
]);
}
}
}
if (!empty($row['op_exclusion'])) {
$excl_array = explode('|', $row['op_exclusion']);
if ($excl_array[0] == '3') {
$icd = Icd::where('code', $row['code'])->first();
if (!$icd) {
throw new ImportRowException(__('icd.NOT_FOUND'), 0, NULL, $row);
}
$exclusion = $icd->exclusions()->create([
'corporate_id' => $corporate->id,
'service_code' => 'OP',
'type' => 'diagnosis'
]);
if (!empty($excl_array[1])) { //msc
$msc = explode(',', $excl_array[1]);
collect($msc)->each(function($m) use ($exclusion) {
$exclusion->rules()->create([
'name' => 'msc',
'values' => $m
]);
});
}
if (!empty($excl_array[2])) { //genders
$genders = explode(',', $excl_array[2]);
collect($genders)->each(function($gender) use ($exclusion) {
$exclusion->rules()->create([
'name' => 'gender',
'values' => $gender
]);
});
}
if (!empty($excl_array[3])) { //min_age
$exclusion->rules()->create([
'name' => 'min_age',
'values' => $excl_array[3]
]);
}
if (!empty($excl_array[4])) { //max_age
$exclusion->rules()->create([
'name' => 'max_age',
'values' => $excl_array[4]
]);
}
if (!empty($excl_array[5])) { //plans
$exclusion->rules()->create([
'name' => 'plan',
'values' => $excl_array[5]
]);
}
}
}
if (!empty($row['de_exclusion'])) {
$excl_array = explode('|', $row['de_exclusion']);
if ($excl_array[0] == '3') {
$icd = Icd::where('code', $row['code'])->first();
$exclusion = $icd->exclusions()->create([
'corporate_id' => $corporate->id,
'service_code' => 'OP',
'type' => 'diagnosis'
]);
if (!empty($excl_array[1])) { //msc
$msc = explode(',', $excl_array[1]);
collect($msc)->each(function($m) use ($exclusion) {
$exclusion->rules()->create([
'name' => 'msc',
'values' => $m
]);
});
}
if (!empty($excl_array[2])) { //genders
$genders = explode(',', $excl_array[2]);
collect($genders)->each(function($gender) use ($exclusion) {
$exclusion->rules()->create([
'name' => 'gender',
'values' => $gender
]);
});
}
if (!empty($excl_array[3])) { //min_age
$exclusion->rules()->create([
'name' => 'min_age',
'values' => $excl_array[3]
]);
}
if (!empty($excl_array[4])) { //max_age
$exclusion->rules()->create([
'name' => 'max_age',
'values' => $excl_array[4]
]);
}
if (!empty($excl_array[5])) { //plans
$exclusion->rules()->create([
'name' => 'plan',
'values' => $excl_array[5]
]);
}
}
}
if (!empty($row['ma_exclusion'])) {
$excl_array = explode('|', $row['ma_exclusion']);
dd($excl_array);
if ($excl_array[0]) {
}
}
if (!empty($row['sp_exclusion'])) {
$excl_array = explode('|', $row['sp_exclusion']);
dd($excl_array);
if ($excl_array[0]) {
}
}
if (!empty($row['pre_exist_exclusion'])) {
$excl_array = explode('|', $row['pre_exist_exclusion']);
dd($excl_array);
if ($excl_array[0]) {
}
}
if (!empty($row['op_de_exclusion'])) {
$excl_array = explode('|', $row['op_de_exclusion']);
dd($excl_array);
if ($excl_array[0]) {
}
}
if (!empty($row['maternity_waiting'])) {
$excl_array = explode('|', $row['maternity_waiting']);
dd($excl_array);
if ($excl_array[0]) {
}
}
return true;
} catch (\Exception $e) {
throw $e;
}
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Modules\Internal\Transformers;
use Illuminate\Http\Resources\Json\JsonResource;
class DiagnosisExclusionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'code' => $this->exclusionable->code,
'name' => $this->exclusionable->name,
'diagnosis_type' => $this->exclusionable->type,
'service_code' => $this->service_code,
'type' => $this->type,
'rules' => $this->rules->mapToGroups(function ($item, $key) {
return [$item['name'] => $item['values']];
})
];
}
}