mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
100 lines
3.4 KiB
PHP
100 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Imports;
|
|
|
|
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
|
use App\Classes\Modules\Imports\Services\GenericImport;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Models\PermitsReminder;
|
|
use Carbon\Carbon;
|
|
use DateTime;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
|
|
|
class ImportPermitsReminderController
|
|
{
|
|
/**
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function import(Request $request)
|
|
{
|
|
$object = new DocumentObject('', $request->input('files'), '', ApprovalStatus::APPROVED, 'imports');
|
|
$file = json_decode($object->getFiles()[0])->file_info->original->file;
|
|
$file = storage_path('app/documents/' . $file);
|
|
|
|
$import = new GenericImport();
|
|
Excel::import($import, $file);
|
|
$excelRows = $import->rows->toArray();
|
|
$successRows = [];
|
|
$returnArray = []; // Initialize the return array
|
|
|
|
foreach ($excelRows as $row) {
|
|
if (is_null($row['expiry_date']) && is_null($row['reminder_date'])) {
|
|
continue;
|
|
}
|
|
|
|
$row['expiry_date'] = $this->changeExcelDate($row['expiry_date']);
|
|
$row['reminder_date'] = $this->changeExcelDate($row['reminder_date']);
|
|
|
|
$validator = Validator::make($row, [
|
|
'model' => 'required',
|
|
'expiry_date' => 'required|date|after_or_equal:today',
|
|
'reminder_date' => 'required|date|after_or_equal:today',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
$row['status'] = 'failed';
|
|
$row['message'] = $validator->errors()->all();
|
|
$returnArray[] = $row;
|
|
} else {
|
|
$existingRecord = PermitsReminder::where('model', $row['model'])->first();
|
|
|
|
if ($existingRecord) {
|
|
$existingRecord->update([
|
|
'expiry_date' => $row['expiry_date'],
|
|
'reminder_date' => $row['reminder_date'],
|
|
]);
|
|
|
|
$row['status'] = 'Updated';
|
|
$row['message'] = 'Record has been updated';
|
|
$returnArray[] = $row;
|
|
} else {
|
|
// Add to successful rows for batch insertion
|
|
$row['created_at'] = Carbon::now();
|
|
$row['updated_at'] = Carbon::now();
|
|
$successRows[] = $row;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Insert only if there are new successful rows
|
|
if (!empty($successRows)) {
|
|
PermitsReminder::insert($successRows);
|
|
}
|
|
|
|
$successCount = count($successRows);
|
|
|
|
return response()->json([
|
|
'completedRows' => $successCount,
|
|
'failedRows' => count($returnArray),
|
|
'remark' => "Successful Imported {$successCount} data.",
|
|
'data' => $returnArray
|
|
]);
|
|
}
|
|
|
|
public function changeExcelDate($date)
|
|
{
|
|
$formatedDate = DateTime::createFromFormat('d/m/Y', $date);
|
|
if ($formatedDate instanceof DateTime) {
|
|
return $formatedDate->format('Y-m-d');
|
|
}
|
|
|
|
$unixTime = (($date - 25569) * 86400);
|
|
|
|
return (new DateTime("@$unixTime"))->format('Y-m-d');
|
|
}
|
|
}
|