Commit 3df476aa authored by Sarun Mungthanya's avatar Sarun Mungthanya
Browse files

issue tis

parent 553ca807
...@@ -46,7 +46,7 @@ class MasterFileController extends Controller ...@@ -46,7 +46,7 @@ class MasterFileController extends Controller
if ($request->createPatch) { if ($request->createPatch) {
$sqlPackAll = $this->readTxt($filePath, $formatData, $fileFormat, $request->limit, $request->noLimit); $sqlPackAll = $this->readTxt($filePath, $formatData, $fileFormat, $request->limit, $request->noLimit);
$this->processPatches($sqlPackAll, $fileFormat, $request->genWithSmartUpdate); $this->processPatches($sqlPackAll, $fileFormat, $request->genWithSmartUpdate ,$request->noLimit);
$message .= 'Patch master file created successfully. '; $message .= 'Patch master file created successfully. ';
} }
...@@ -95,50 +95,106 @@ class MasterFileController extends Controller ...@@ -95,50 +95,106 @@ class MasterFileController extends Controller
]); ]);
} }
private function processPatches($sqlPackAll, $fileFormat, $genWithSmartUpdate) function processPatches($sqlPackAll, $fileFormat, $genSpnSmartUpdateDecode ,$noLimit)
{ {
$uid = auth()->id(); $uid = auth()->user()->id;
$pLevel = "Critical";
$nameFormat = $fileFormat->name ?? 'Unknown Format'; $nameFormat = $fileFormat->name ?? 'Unknown Format';
$level = "Critical"; $pDesc = $nameFormat;
$date = Carbon::now(); $day = Date('j');
$month = Date('n');
foreach ($sqlPackAll as $index => $sqlPack) { $year = Date('Y');
$patchCode = $this->generatePatchCode($sqlPack, $genWithSmartUpdate); $pApproveDate = date("Y-m-d h:i:s");
$this->savePatch($patchCode, $nameFormat, $level, $uid, $date); $partPatch = '';
} $countSql = count($sqlPackAll);
$part = 1;
foreach ($sqlPackAll as $i__ => $sqlPack) {
if ($countSql != 1 && !$noLimit) {
$partPatch = 'Part ' . $part++;
} }
$tis620_encoded = iconv('UTF-8', 'TIS-620', var_export($sqlPack, true));
$ALL64 = base64_encode(gzcompress($tis620_encoded));
private function generatePatchCode($sqlPack, $genWithSmartUpdate)
{
$all64 = base64_encode(gzcompress(iconv('UTF-8', 'TIS-620', var_export($sqlPack, true))));
$patchCode = "\$ALL64=\"{$all64}\";\n";
if ($genWithSmartUpdate) { $patchCode = '$ALL64="' . $ALL64 . '";' . "\n";
$patchCode .= "\$ALLVAR=gzuncompress(spnsmartupdatedecode(\$ALL64));\n";
if ($genSpnSmartUpdateDecode) {
$updatedecode = '$ALLVAR=gzuncompress(spnsmartupdatedecode($ALL64));' . "\n";
} else { } else {
$patchCode .= "\$ALLVAR=gzuncompress(base64_decode(\$ALL64));\n"; $updatedecode = '$ALLVAR=gzuncompress(base64_decode($ALL64));' . "\n";
} }
$patchCode .= $updatedecode;
$patchCode .= "eval('\$SQL='.".'$ALLVAR.";");'."\n";
$patchCode .= $this->generatePatchCode($countSql , $i__);
if ($noLimit && ($i__ == $countSql - 1)) {
$this->savePatch($patchCode, $nameFormat, $day, $month, $year, $partPatch, $pLevel, $pDesc, $uid, $pApproveDate);
} elseif (!$noLimit) {
$this->savePatch($patchCode, $nameFormat, $day, $month, $year, $partPatch, $pLevel, $pDesc, $uid, $pApproveDate);
}
}
}
private function generatePatchCode($countSql , $i__)
{
$patchCode = '';
$patchCode .= '$insert=$SQL["insert"];' . "\n";
$patchCode .= '$update=$SQL["update"];' . "\n";
$patchCode .= '$select=$SQL["select"];' . "\n";
$patchCode .= '$select2=$SQL["select2"];' . "\n";
$patchCode .= '$DATABASE=$SQL["DATABASE"];' . "\n";
$patchCode .= '$CountSQL_=count($insert);' . "\n";
$patchCode .= '$succ="0";' . "\n";
$patchCode .= '$succ2="0";' . "\n";
$patchCode .= 'for($i_=0;$i_<$CountSQL_;$i_++){' . "\n";
$patchCode .= ' $INSERT_ID="";' . "\n";
$patchCode .= ' if($DBTYPE=="MYSQL"){' . "\n";
$patchCode .= ' $sql=$insert[$i_]." ON DUPLICATE KEY UPDATE ".$update[$i_];' . "\n";
$patchCode .= ' }' . "\n";
$patchCode .= ' if($DBTYPE=="MSSQL"){' . "\n";
$patchCode .= ' $sql="IF EXISTS (".$select[$i_].") BEGIN UPDATE $DATABASE SET ".$update[$i_].$select2[$i_]." END ELSE ".$insert[$i_];' . "\n";
$patchCode .= ' }' . "\n";
$patchCode .= ' exec_query($sql);' . "\n";
$patchCode .= ' $succ++;' . "\n";
$patchCode .= ' if($INSERT_ID!=""){$succ2++;}' . "\n";
$patchCode .= '}' . "\n";
$patchCode .= 'unset($ALL64);' . "\n";
$patchCode .= 'unset($ALLVAR);' . "\n";
$patchCode .= 'unset($SQL);' . "\n";
$patchCode .= 'unset($insert);' . "\n";
$patchCode .= 'unset($update);' . "\n";
$patchCode .= 'unset($select);' . "\n";
$patchCode .= 'unset($select2);' . "\n";
$patchCode .= '$PRINT_ .= date("h:i:s")." Process '.($i__+1).' / ' . $countSql . ' Successful Update $succ data(s) (Added = $succ2) from $CountSQL_.</br>";' . "\n";
return $patchCode; return $patchCode;
} }
private function savePatch($patchCode, $nameFormat, $level, $uid, $date) private function savePatch($patchCode, $nameformat, $day, $month, $year, $partPatch, $PLEVEL, $PDESC, $UID, $PAPPROVEDATE)
{ {
DB::table('conf_smartupdate')->insert([ $patchCode .= '$PRINTOUT=$PRINT_;' . "\n";
'PATCHNAME' => "Update Master File {$nameFormat} " . $date->toFormattedDateString(), $patchCode .= '$PATCH_STATUS="OK";' . "\n";
'PDATE' => $date, // $patchCode = str_replace("'", "''", $patchCode);
'PLEVEL' => $level,
$data = [
'PATCHNAME' => "Update Master File $nameformat วันที่ $day เดือน $month ปี $year $partPatch",
'PDATE' => Carbon::now(),
'PLEVEL' => $PLEVEL,
'PCODE' => 'SHIPPINGNET', 'PCODE' => 'SHIPPINGNET',
'PDESC' => $nameFormat, 'MAJOR_VERSION' => 'ALL',
'POWNER' => $uid, 'PDESC' => $PDESC,
'POWNER' => $UID,
'PTYPE' => 'UPDATE MASTER', 'PTYPE' => 'UPDATE MASTER',
'PATCHCODE' => iconv("UTF-8", "TIS-620", $patchCode), 'PAPPROVEDATE' => $PAPPROVEDATE,
'PAPPROVEDATE' => now(), 'PATCHCODE' => iconv("utf-8", "tis-620" ,$patchCode),
'MAJOR_VERTSION' => 'ALL', 'UNINSTALL' => '$DONE=1;',
'PATCHCODE_SERVER' => '$DONE=1;', 'PATCHCODE_SERVER' => '$DONE=1;',
'UNINSTALL' => '$DONE=1;' ];
]); DB::table('conf_smartupdate')->insert($data);
} }
private function readTxt($filePath, $formatData, $ac, $limit = 10000 , $noLimit = false) private function readTxt($filePath, $formatData, $ac, $limit = 10000 , $noLimit = false)
...@@ -152,13 +208,13 @@ class MasterFileController extends Controller ...@@ -152,13 +208,13 @@ class MasterFileController extends Controller
while (!feof($fileHandle)) { while (!feof($fileHandle)) {
$sqlPack = []; $sqlPack = [];
$text = fgets($fileHandle); $text = fgets($fileHandle);
try { // try {
$text = iconv('TIS-620', 'UTF-8', $text); // $text = iconv('TIS-620', 'UTF-8', $text);
} catch (\Throwable $th) { // } catch (\Throwable $th) {
$encoding = mb_detect_encoding($text, ['UTF-8', 'ISO-8859-1', 'ASCII'], true); $encoding = mb_detect_encoding($text, ['UTF-8', 'ISO-8859-1', 'ASCII' ,'TIS-620'], true);
$text = iconv($encoding, 'UTF-8', $text); $text = iconv($encoding, 'UTF-8', $text);
} // }
$X = $Y = $Z = 0; $X = $Y = $Z = 0;
......
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