Commit 549d68bd authored by Thidaporn Laisan's avatar Thidaporn Laisan
Browse files

update upload file

parent 847d61cc
<?php
namespace App\Http\Livewire\Pages\Exchangerate;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Storage;
use App\Models\CenterConfExchangerate;
use App\Models\CenterConfExchangerateExport;
use App\Models\ConfSmartupdate;
use Illuminate\Support\Facades\DB;
class ExchangerateUpload extends Component
{
use WithFileUploads;
public $file;
public $createPatch = false;
public $updatePatch = false;
public $sendToEec = false;
public function upload()
{
$this->validate([
'file' => 'required|mimes:csv,txt|max:2048',
]);
$filePath = $this->file->store('uploads');
if ($this->sendToEec) {
$resultEEC = $this->sendMasterFileToEcc($filePath);
if ($resultEEC['http_code'] == 200) {
session()->flash('message', 'Send MasterFile To EEC successfully!');
} else {
session()->flash('message', 'Failed to send MasterFile To EEC.');
}
}
if ($this->createPatch && $this->updatePatch) {
session()->flash('message', 'กรุณาเลือกอย่างใดอย่างหนึ่ง Create หรือ Update.');
return;
}
if ($this->createPatch || $this->updatePatch) {
$this->processFileForPatch($filePath);
}
}
private function sendMasterFileToEcc($filePath)
{
$url = env('URL_MASTERFILE_EEC');
$postData = [
'type' => 'exchange-rate',
'file' => curl_file_create(Storage::path($filePath)),
'timestamp' => now()->format('Y-m-d')
];
$curl = curl_init();
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => [
"cache-control: no-cache",
"content-type: multipart/form-data"
],
];
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$err = curl_error($curl);
$info = curl_getinfo($curl);
curl_close($curl);
return $info;
}
private function processFileForPatch($filePath)
{
$fileContent = Storage::get($filePath);
$lines = explode("\n", $fileContent);
$needChkDup = true;
$array_cur = [];
$Loop_pathcode = '';
foreach ($lines as $line) {
$CUR = substr($line, 0, 3);
if ($CUR != '') {
if (!in_array($CUR, $array_cur, true)) {
$array_cur[] = $CUR;
}
$DATE1 = substr($line, 3, 8);
$d1 = substr($DATE1, 0, 2);
$m1 = substr($DATE1, 2, 2);
$y1 = substr($DATE1, 4, 4);
$DATE1 = "$y1-$m1-$d1";
$DATE2 = substr($line, 11, 8);
$d2 = substr($DATE2, 0, 2);
$m2 = substr($DATE2, 2, 2);
$y2 = substr($DATE2, 4, 4);
$DATE2 = "$y2-$m2-$d2";
$EXRATE = trim(substr($line, 19, 13));
$IMRATE = trim(substr($line, 32, 13));
$FACT = trim(substr($line, 45, 7));
$AMMENDDATE = now()->format('Ymd') + 543;
if ($needChkDup && $this->createPatch) {
$latestRecord = CenterConfExchangerate::selectRaw('MONTH(MAX(exdate)) as month, YEAR(MAX(exdate)) as year')
->join('center_conf_exchangerate_export', function($join) {
$join->on('center_conf_exchangerate.currency', '=', 'center_conf_exchangerate_export.currency')
->on('center_conf_exchangerate.exdate', '=', 'center_conf_exchangerate_export.exdate')
->on('center_conf_exchangerate.finishdate', '=', 'center_conf_exchangerate_export.finishdate');
})->first();
$chk = ConfSmartupdate::whereMonth('PDATE', $m2)
->whereYear('PDATE', $y2)
->where('PTYPE', 'UPDATE MASTER NEW')
->exists();
if ($chk) {
session()->flash('message', 'ไม่สามารถเพิ่ม Patch เดือน '.$m2.' ปี '.$y2.' ได้เนื่องจากได้มีการเพิ่มข้อมูลไว้ในระบบเรียบร้อยแล้ว.');
return;
}
$needChkDup = false;
}
CenterConfExchangerate::updateOrCreate(
[
'currency' => $CUR,
'exdate' => $DATE1,
'finishdate' => $DATE2
],
[
'baht' => $IMRATE,
'rate' => $FACT,
'amenddate' => $AMMENDDATE
]
);
CenterConfExchangerateExport::updateOrCreate(
[
'currency' => $CUR,
'exdate' => $DATE1,
'finishdate' => $DATE2
],
[
'baht' => $EXRATE,
'rate' => $FACT,
'amenddate' => $AMMENDDATE
]
);
if ($this->updatePatch) {
$y2 = str_pad($y2, 4, '0', STR_PAD_LEFT);
$m2 = str_pad($m2, 2, '0', STR_PAD_LEFT);
$UPDATECODE_update = $y2 . $m2;
$countExist = CenterConfExchangerate::where('exdate', $DATE1)
->where('currency', $CUR)
->count();
if ($countExist != 0) {
CenterConfExchangerate::where('currency', $CUR)
->where('exdate', $DATE1)
->update(['finishdate' => $DATE2]);
}
$countExistEx = CenterConfExchangerateExport::where('exdate', $DATE1)
->where('currency', $CUR)
->count();
if ($countExistEx != 0) {
CenterConfExchangerateExport::where('currency', $CUR)
->where('exdate', $DATE1)
->update(['finishdate' => $DATE2]);
}
$countExist = CenterConfExchangerate::where('exdate', $DATE1)
->where('currency', $CUR)
->count();
if ($countExist == 0) {
CenterConfExchangerate::create([
'currency' => $CUR,
'exdate' => $DATE1,
'finishdate' => $DATE2,
'rate' => $FACT,
'baht' => $IMRATE,
'amenddate' => $UPDATECODE_update
]);
CenterConfExchangerateExport::create([
'currency' => $CUR,
'exdate' => $DATE1,
'finishdate' => $DATE2,
'rate' => $FACT,
'baht' => $EXRATE,
'amenddate' => $UPDATECODE_update
]);
}
}
}
}
//GEN Patch
$ins_addMonth = $m2;
$ins_addYear = $y2;
$ins_addDay = $d1;
if ($this->updatePatch) {
$PDESC = "(UPDATE) อัตราแลกเปลี่ยนของขาเข้า และขาออก เดือน ". $this->monthToThai($m2) ."ปี $y2";
$PTYPE = "Specific Customer";
$PDATE = now()->format('Y-m-d H:i:s');
$PLEVEL = "Critical";
$PCODE = "";
$MAJOR_VERSION = 'All';
$POWNER = auth()->user()->id;
$PATCHCODE = $Loop_pathcode.' $PATCH_STATUS="OK";';
$PATCHCODE = str_replace("'", "\'", $PATCHCODE);
$UNINSTALL='$b=1';
$next_PID = DB::table('information_schema.tables')
->where('table_name', 'conf_smartupdate')
->value('AUTO_INCREMENT');
// Using Eloquent to get the data
$patchFiles = DB::table('tab_patch_file')->where('ptid', $next_PID)->get();
$PATCHCODE_SERVER = "$a=1;\n";
foreach ($patchFiles as $index => $file) {
$PATCHCODE_SERVER .= "\$file_name_{$index}=\"{$file->file_name}\";\n";
$PATCHCODE_SERVER .= "\$file_data_{$index}=\"{$file->file_data}\";\n";
}
$PATCHCODE_SERVER .= "\$max=count(\$fid);";
$PATCHCODE_SERVER = str_replace("'", "''", $PATCHCODE_SERVER);
$PATCHCODE_SERVER = str_replace("\\", "\\\\", $PATCHCODE_SERVER);
$arr_cur_str = "[".implode(",",$array_cur)."]";
$PATCHNAME="Update : Exchange Rate Import & Export $m2 / $y2 ".$arr_cur_str;
ConfSmartupdate::create([
'PATCHNAME' => $PATCHNAME,
'PDATE' => $PDATE,
'PLEVEL' => $PLEVEL,
'PCODE' => $PCODE,
'MAJOR_VERSION' => $MAJOR_VERSION,
'PDESC' => $PDESC,
'POWNER' => $POWNER,
'PTYPE' => $PTYPE,
'PATCHCODE' => $PATCHCODE,
'UNINSTALL' => $UNINSTALL,
'PATCHCODE_SERVER' => $PATCHCODE_SERVER,
'PAPPROVEDATE' => now()
]);
session()->flash('message', 'อัพเดท Patch เดือน '.$m2.' ปี '.$y2.' ในระบบเรียบร้อยแล้ว.');
}
if ($this->createPatch) {
$PDESC = "อัตราแลกเปลี่ยนของขาเข้า และขาออก เดือน ". $this->monthToThai($ins_addMonth) ."ปี $ins_addYear";
$PTYPE = "UPDATE MASTER NEW";
$PDATE = "$ins_addYear-$ins_addMonth-$ins_addDay 00:00:00";
$PLEVEL = "AUTO";
$PCODE = "SHIPPINGNET";
$MAJOR_VERSION = 'All';
$POWNER = auth()->user()->id;
$PATCHCODE = Storage::get($this->findHTX('patchcode_new.txt'));
$PATCHCODE = str_replace("'", "''", $PATCHCODE);
$UNINSTALL = Storage::get($this->findHTX('uninstall_new.txt'));
$UNINSTALL = str_replace("'", "''", $UNINSTALL);
$i=0;
$PATCHCODE_SERVER = '';
$filePath = $this->findHTX('patchcode_server_new.txt');
$fileContent = Storage::get($filePath);
foreach (explode("\n", $fileContent) as $tmp) {
if ($i==0) {
$PATCHCODE_SERVER .= '$MONTH="'.$ins_addYear.'-'.$ins_addMonth.'-%";';
} else if ($i==1) {
$PATCHCODE_SERVER .= '$UPDATECODE='."'".$ins_addYear.$ins_addMonth."'".';';
} else {
$PATCHCODE_SERVER .= $tmp;
}
$i++;
}
$PATCHCODE_SERVER = str_replace("'", "''", $PATCHCODE_SERVER);
$PATCHNAME = "Exchange Rate Import & Export $ins_addMonth / $ins_addYear";
ConfSmartupdate::create([
'PATCHNAME' => $PATCHNAME,
'PDATE' => $PDATE,
'PLEVEL' => $PLEVEL,
'PCODE' => $PCODE,
'MAJOR_VERSION' => $MAJOR_VERSION,
'PDESC' => $PDESC,
'POWNER' => $POWNER,
'PTYPE' => $PTYPE,
'PATCHCODE' => $PATCHCODE,
'UNINSTALL' => $UNINSTALL,
'PATCHCODE_SERVER' => $PATCHCODE_SERVER,
'PAPPROVEDATE' => now()
]);
session()->flash('message', 'สร้าง Patch เดือน '.$ins_addMonth.' ปี '.$ins_addYear.' ในระบบเรียบร้อยแล้ว.');
}
}
private function monthToThai($m)
{
$thai_n = ["มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม"];
return $thai_n[$m-1];
}
private function findHTX($filename)
{
// Implement findHTX logic based on your requirements
return $filename;
}
public function render()
{
return view('livewire.pages.exchangerate.exchangerate-upload');
}
}
?>
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
@elseif ($currentContent === 'Parameter') @elseif ($currentContent === 'Parameter')
<livewire:pages.parameter.parameter-index /> <livewire:pages.parameter.parameter-index />
@elseif ($currentContent === 'Exchangerate') @elseif ($currentContent === 'Exchangerate')
<livewire:pages.exchangerate.exchangerate-index /> <livewire:pages.exchangerate.exchangerate-index />
@else @else
@livewire('code-comparer') @livewire('code-comparer')
@endif @endif
......
...@@ -8,6 +8,27 @@ ...@@ -8,6 +8,27 @@
.table-responsive { .table-responsive {
overflow-x: auto; overflow-x: auto;
} }
.form-upload {
display: inline-block;
margin-left: 1rem;
}
.form-upload input[type="file"] {
display: inline-block;
margin-right: 0.5rem;
padding: 0.5rem;
border-radius: 0.25rem;
}
.form-upload label {
margin-left: 0.5rem;
margin-right: 1rem;
}
.form-upload button {
margin-top: 0.5rem;
}
</style> </style>
<main class="m-2"> <main class="m-2">
@if ($action === 'list') @if ($action === 'list')
...@@ -31,8 +52,23 @@ ...@@ -31,8 +52,23 @@
<div class="px-2 ml-4"> <div class="px-2 ml-4">
<button type="button" class="py-2 px-3 bg-stone-700 rounded-lg text-white" <button type="button" class="py-2 px-3 bg-stone-700 rounded-lg text-white"
wire:click="showexchangerateAddForm">Add</button> wire:click="showexchangerateAddForm">Add</button>
<button type="button" class="py-2 px-3 bg-stone-700 rounded-lg text-white" <button type="button" class="py-2 px-3 bg-stone-700 rounded-lg text-white"
onclick="confirmDeleteSelected()">Delete</button> onclick="confirmDeleteSelected()">Delete</button>
<!-- File Upload Form -->
<form wire:submit.prevent="upload" class="form-upload">
<input type="file" wire:model="file">
@error('file') <span class="error">{{ $message }}</span> @enderror
<label>
<input type="checkbox" wire:model="createPatch"> <b>Create Patch</b>
</label>
<label>
<input type="checkbox" wire:model="updatepatch"> <b>Update Patch</b>
</label>
<label>
<input type="checkbox" wire:model="sendToEec"> <b>Send To EEC</b>
</label>
<button type="submit" class="py-2 px-3 bg-stone-700 rounded-lg text-white">Upload</button>
</form>
</div> </div>
<div class="inline-flex flex-initial"> <div class="inline-flex flex-initial">
<div x-data="{ isInputActive: true }"> <div x-data="{ isInputActive: true }">
...@@ -58,18 +94,16 @@ ...@@ -58,18 +94,16 @@
@endforeach @endforeach
</select> </select>
</span> </span>
<button type="button" class="bg-stone-700 text-white px-4 py-2 rounded" <button type="button" class="bg-stone-700 text-white px-4 py-2 rounded"
wire:click="search">Search</button> wire:click="search">Search</button>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<div class="mx-3 mt-3 px-4"> <div class="mx-3 mt-3 px-4">
<div class="is-scrollbar-hidden min-w-full table-responsive" x-data="pages.tables.initExample1"> <div class="is-scrollbar-hidden min-w-full table-responsive" x-data="pages.tables.initExample1">
<table class="is-hoverable table w-full text-left"> <table class="is-hoverable table w-full text-left">
<thead> <thead>
<tr> <tr>
<th <th
class="whitespace-nowrap rounded-tl-lg bg-slate-200 px-4 py-3 font-semibold uppercase text-slate-800 dark:bg-navy-800 dark:text-navy-100 lg:px-5"> class="whitespace-nowrap rounded-tl-lg bg-slate-200 px-4 py-3 font-semibold uppercase text-slate-800 dark:bg-navy-800 dark:text-navy-100 lg:px-5">
...@@ -106,7 +140,7 @@ ...@@ -106,7 +140,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach ($results as $exc) @foreach ($results as $exc)
<tr class="border-y border-transparent border-b-slate-200 dark:border-b-navy-500"> <tr class="border-y border-transparent border-b-slate-200 dark:border-b-navy-500">
<td class="whitespace-nowrap px-4 py-3 sm:px-5"> <td class="whitespace-nowrap px-4 py-3 sm:px-5">
<label class="inline-flex items-center space-x-2"> <label class="inline-flex items-center space-x-2">
...@@ -147,10 +181,9 @@ ...@@ -147,10 +181,9 @@
@elseif($action === 'add') @elseif($action === 'add')
<livewire:pages.exchangerate.exchangerate-create /> <livewire:pages.exchangerate.exchangerate-create />
@elseif($action === 'edit') @elseif($action === 'edit')
@livewire('pages.exchangerate.exchangerate-edit', ['editPid' => $editPid]) @livewire('pages.exchangerate.exchangerate-edit', ['editPid' => $editPid])
@endif @endif
</main> </main>
@vite(['resources/js/app.js', 'resources/js/sweetalert.js']) @vite(['resources/js/app.js', 'resources/js/sweetalert.js'])
<script src="{{ asset('js/confirmDelete.js') }}"></script> <script src="{{ asset('js/confirmDelete.js') }}"></script>
</div> </div>
<!-- <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script> -->
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment