98 lines
5.2 KiB
PHP
98 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\M3\Http\Controllers;
|
|
|
|
use Modules\M3\Http\Controllers\Controller;
|
|
use DB;
|
|
use Config;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
// use Modules\M3\Models\ICM711M;
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
|
|
class whsePerfController extends Controller
|
|
{
|
|
|
|
public static function index(Request $request) {
|
|
|
|
return view('m3::liberty-theme.pages.whsePerfRequest');
|
|
|
|
}
|
|
|
|
public static function displayTable(Request $request) {
|
|
|
|
// **********************************************************************************
|
|
// **** Changes in this section must be replicated in function exportTable below ****
|
|
// **********************************************************************************
|
|
$date_in_yr = isset($request->date_in_yr)?$request->date_in_yr:date('Y');
|
|
$date_in_mth = isset($request->date_in_mth)?$request->date_in_mth:date('m');
|
|
#$date_in = isset($request->date_in)?$request->date_in:date('Ymd');
|
|
$date_in = $date_in_yr.$date_in_mth."01";
|
|
$sqlstmt = "select AVDATE, AVANOTESCL,AVADLRCL,AVAWGTCL,AVANOTESCH,AVADLRCH,AVAWGTCH,AVANOTESOP,AVADLROP,AVAWGTOP,
|
|
AVLNOTESCL,AVLDLRCL,AVLWGTCL,AVLNOTESCH,AVLDLRCH,AVLWGTCH,AVLNOTESOP,AVLDLROP,AVLWGTOP,
|
|
AVPNOTESCL,AVPDLRCL,AVPWGTCL,AVPNOTESCH,AVPDLRCH,AVPWGTCH,AVPNOTESOP,AVPDLROP,AVPWGTOP,
|
|
AVINOTESCL,AVIDLRCL,AVIWGTCL,AVINOTESOP,AVIDLROP,AVIWGTOP
|
|
from ICM711M
|
|
where AVDATE between ".date('Ym', strtotime($date_in))."00 and ".date('Ym', strtotime($date_in))."32";
|
|
|
|
$ICM711M = DB::connection(config('m3.connection', 'pgsql'))->select($sqlstmt);
|
|
$USER_INFO = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')->selectRaw('R428US as USERPRF, R428STS as STATUS, NULL as DATE_EXPIRED, NULL as EXPIRED')->get()->all();
|
|
$power_user = DB::connection(config('m3.connection', 'pgsql'))->select("select R428PWRUSR, R428CO, R428PL, R428WH from ICM428M where R428US='".strtoUpper(m3_user())."'");
|
|
|
|
$pct_array = Config::get('arrays.whsePerf_targets');
|
|
|
|
return view('m3::liberty-theme.pages.whsePerf', compact('ICM711M','power_user','USER_INFO','date_in','pct_array'));
|
|
|
|
}
|
|
|
|
public function exportTable(Request $request) {
|
|
|
|
$date_in_yr = isset($request->date_in_yr)?$request->date_in_yr:date('Y');
|
|
$date_in_mth = isset($request->date_in_mth)?$request->date_in_mth:date('m');
|
|
#$date_in = isset($request->date_in)?$request->date_in:date('Ymd');
|
|
$date_in = $date_in_yr.$date_in_mth."01";
|
|
$sqlstmt = "select AVDATE, AVANOTESCL,AVADLRCL,AVAWGTCL,AVANOTESCH,AVADLRCH,AVAWGTCH,AVANOTESOP,AVADLROP,AVAWGTOP,
|
|
AVLNOTESCL,AVLDLRCL,AVLWGTCL,AVLNOTESCH,AVLDLRCH,AVLWGTCH,AVLNOTESOP,AVLDLROP,AVLWGTOP,
|
|
AVPNOTESCL,AVPDLRCL,AVPWGTCL,AVPNOTESCH,AVPDLRCH,AVPWGTCH,AVPNOTESOP,AVPDLROP,AVPWGTOP,
|
|
AVINOTESCL,AVIDLRCL,AVIWGTCL,AVINOTESOP,AVIDLROP,AVIWGTOP
|
|
from ICM711M
|
|
where AVDATE between ".date('Ym', strtotime($date_in))."00 and ".date('Ym', strtotime($date_in))."32";
|
|
|
|
$ICM711M = DB::connection(config('m3.connection', 'pgsql'))->select($sqlstmt);
|
|
$USER_INFO = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')->selectRaw('R428US as USERPRF, R428STS as STATUS, NULL as DATE_EXPIRED, NULL as EXPIRED')->get()->all();
|
|
$power_user = DB::connection(config('m3.connection', 'pgsql'))->select("select R428PWRUSR, R428CO, R428PL, R428WH from ICM428M where R428US='".strtoUpper(m3_user())."'");
|
|
|
|
$pct_array = Config::get('arrays.whsePerf_targets');
|
|
|
|
$html = view('m3::liberty-theme.pages.whsePerf', compact('ICM711M','power_user','USER_INFO','date_in','pct_array'))->render();
|
|
|
|
libxml_use_internal_errors(true);
|
|
$doc = new \DOMDocument();
|
|
$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); // Extract only table elements
|
|
$tables = $doc->getElementsByTagName('table');
|
|
if ($tables->length === 0) {
|
|
abort(400, 'No table found in HTML');
|
|
}
|
|
// Create new document with only the table
|
|
$newDoc = new \DOMDocument();
|
|
$newDoc->appendChild($newDoc->importNode($tables->item(0), true));
|
|
$cleanHtml = '<!DOCTYPE html><html><body>' . $newDoc->saveHTML() . '</body></html>';
|
|
libxml_clear_errors();
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'html') . '.html';
|
|
file_put_contents($tempFile, $cleanHtml);
|
|
$reader = IOFactory::createReader('Html');
|
|
$spreadsheet = $reader->load($tempFile);
|
|
$writer = new Xlsx($spreadsheet);
|
|
$fileName = 'export.xlsx';
|
|
$tempExcel = tempnam(sys_get_temp_dir(), 'excel');
|
|
$writer->save($tempExcel);
|
|
unlink($tempFile);
|
|
return response()->download($tempExcel, $fileName)->deleteFileAfterSend(true);
|
|
}
|
|
|
|
}
|