From 17cfcf44b2a136f2f0848f9ca0d0549f3b15a08f Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Tue, 22 Sep 2026 11:56:38 +0100 Subject: [PATCH] M3 Integration fixes --- .../Controllers/ExecSummaryController.php | 292 ++++++++-------- .../Controllers/ExecSummaryLineController.php | 330 ++++++++---------- .../M3GetPDFFromCacheController.php | 312 ++++++++--------- 3 files changed, 449 insertions(+), 485 deletions(-) diff --git a/Modules/M3/Http/Controllers/ExecSummaryController.php b/Modules/M3/Http/Controllers/ExecSummaryController.php index 8260749d..66c0f906 100755 --- a/Modules/M3/Http/Controllers/ExecSummaryController.php +++ b/Modules/M3/Http/Controllers/ExecSummaryController.php @@ -9,155 +9,155 @@ use Str; class ExecSummaryController extends Controller { - private const WAREHOUSE_IN_CLAUSE = "' and a5whs in ("; - public static function index(Request $request) { - $iC670M = collect(); - $wHDDA = ''; - $cOMPANY = '110'; - $pLANT = '144'; - $wHS = '322'; - - try { - // Ajax Call - if ($request->ajax()) { - $wHDDA = isset($request->WHDDA) ? $request->WHDDA : []; - $wHArr = []; - foreach ($wHDDA as $WHDDAV) { - $cO = substr($WHDDAV, 0, 3); - $wHArr[] = substr($WHDDAV, 6, 3); - } - - $reportPeriod = isset($request->reportPeriod) ? $request->reportPeriod : ''; // monthly , quarter , year , daily - // Current - $checkSales = isset($request->checkSales) ? $request->checkSales : ''; // on - $checkWeight = isset($request->checkWeight) ? $request->checkWeight : ''; // on - $checkLines = isset($request->checkLines) ? $request->checkLines : ''; // on - - if (! empty($wHArr) && $cO != '' && $reportPeriod != '') { - switch ($reportPeriod) { - case 'year': - // Set Dates based on reportPeriod type - $firstDay = isset($request->REPORTDATE) ? \DateTime::createFromFormat('Y-m', $request->REPORTDATE) : ''; - $reportDate = $firstDay->modify('first day of January')->format('Ymd'); - - $lastDay = isset($request->TOREPORTDATE) ? \DateTime::createFromFormat('Y-m', trim($request->TOREPORTDATE)) : ''; - $toreportDate = $lastDay->modify('last day of December')->format('Ymd'); - - // Build SQL - $selectContent = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDate and ayapr8N002<=$toreportDate ) - ) and a5co='" . $cO . "' and a5whs in(" . implode(',', $wHArr) . ') - ) - select substr(ayapr8N002,1,4) as years,count(*) as lines,sum(a5rsvqty) as reserveqty,sum(a5packed) as packed,sum(a5lnwght*a5packed/a5rsvqty) as weight,sum(dollars) as dollars,a5whs as whs from tempfile - group by substr(ayapr8N002,1,4),a5whs order by 1 '; - break; - - case 'quarter': - // Set Dates based on reportPeriod type - $firstDay = isset($request->REPORTDATE) ? \DateTime::createFromFormat('Y-m', $request->REPORTDATE) : ''; - $reportDate = $firstDay->modify('first day of January')->format('Ymd'); - - $lastDay = isset($request->TOREPORTDATE) ? \DateTime::createFromFormat('Y-m', trim($request->TOREPORTDATE)) : ''; - $toreportDate = $lastDay->modify('last day of December')->format('Ymd'); - - // Build SQL - $selectContent = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDate and ayapr8N002<=$toreportDate ) - ) and a5co='" . $cO . self::WAREHOUSE_IN_CLAUSE . implode(',', $wHArr) . ") - ) - select substr(ayapr8N002,3,2) ||'-' || 'Q' || EXTRACT(QUARTER FROM TO_DATE(CAST(ayapr8n002 AS TEXT),'YYYYMMDD')) as years,count(*) as lines, - sum(a5rsvqty) as reserveqty, sum(a5packed) as packed,sum (a5lnwght*a5packed/a5rsvqty) as weight,sum(dollars) as dollars, a5whs as whs - from tempfile - group by substr(ayapr8N002,3,2) ||'-' || 'Q' || EXTRACT(QUARTER FROM TO_DATE(CAST(ayapr8n002 AS TEXT),'YYYYMMDD')),a5whs order by 1 "; - break; - - case 'monthly': - // Set Dates based on reportPeriod type - $firstDay = isset($request->REPORTDATE) ? \DateTime::createFromFormat('Y-m', $request->REPORTDATE) : ''; - $reportDate = $firstDay->modify('first day of this month')->format('Ymd'); - - $lastDay = isset($request->TOREPORTDATE) ? \DateTime::createFromFormat('Y-m', trim($request->TOREPORTDATE)) : ''; - $toreportDate = $lastDay->modify('last day of this month')->format('Ymd'); - - // Build SQL - $selectContent = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDate and ayapr8N002<=$toreportDate ) - ) and a5co='" . $cO . self::WAREHOUSE_IN_CLAUSE . implode(',', $wHArr) . ") - ) - select substr(ayapr8N002,1,4) || '-' || substr(ayapr8N002,5,2) as years,count(*) as lines,sum(a5rsvqty) as reserveqty,sum(a5packed) as packed, - sum(a5lnwght*a5packed/a5rsvqty) as weight,sum(dollars) as dollars, a5whs as whs - from tempfile - group by substr(ayapr8N002,1,4) || '-' || substr(ayapr8N002,5,2),a5whs order by 1 "; - break; - - case 'custom': - // Set Dates based on reportPeriod type - $firstDay = isset($request->REPORTDATE) ? \DateTime::createFromFormat('Y-m', $request->REPORTDATE) : ''; - $reportDate = $firstDay->modify('first day of this month')->format('Ymd'); - - $toreportDate = $firstDay->modify('last day of this month')->format('Ymd'); - - // Build SQL - $selectContent = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDate and ayapr8N002<=$toreportDate ) - ) and a5co='" . $cO . self::WAREHOUSE_IN_CLAUSE . implode(',', $wHArr) . ') - ) - select substr(ayapr8N002,7,2) as years,count(*) as lines,sum(a5rsvqty) as reserveqty,sum(a5packed) as packed, - sum(a5lnwght*a5packed/a5rsvqty) as weight, sum(dollars) as dollars, a5whs as whs - from tempfile - group by substr(ayapr8N002,7,2),a5whs order by 1 '; - break; - - default: - $selectContent = ''; - break; - } - - $execSummaryRes = DB::connection(config('m3.connection', 'pgsql'))->select($selectContent); - } else { - $execSummaryRes = collect(); - } - - return response()->json(['success' => true, 'reportPeriod' => $reportPeriod, 'result' => $execSummaryRes, 'checkSales' => $checkSales, - 'checkWeight' => $checkWeight, 'checkLines' => $checkLines]); - } - - // Init Page - $sHUSER = m3User() !== '' ? Str::upper(trim(m3User())) : ''; - if ($sHUSER != '') { - // Get User CO & WH from Table ICM428M - $iCM428M = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m') - ->select('R428US', 'R428CO', 'R428WH', 'R428PL') - ->where('R428US', '=', $sHUSER) - ->first(); - - $cOMPANY = isset($iCM428M->R428CO) ? $iCM428M->R428CO : '110'; // Company - $pLANT = isset($iCM428M->R428PL) ? $iCM428M->R428PL : '144'; // Plant - $wHS = isset($iCM428M->R428WH) ? $iCM428M->R428WH : '322'; // WareHouse - } - $wHDDA = $cOMPANY . $pLANT . $wHS; - // dump($wHDDA); - // Warehouse Table - $iC670M = DB::connection(config('m3.connection', 'pgsql'))->table('ic670m')->get(); - } catch (\Throwable $th) { - Log::error($th); + if ($request->ajax()) { + return self::ajaxResponse($request); } - return view('m3::liberty-theme.pages.execSummary', ['IC670M' => $iC670M, 'WHDDA' => $wHDDA]); - // ,'ICM688M2',,'COMPANY','PLANT','WHS','reportDate' + return self::pageResponse(); + } + + private static function ajaxResponse(Request $request) + { + [$company, $warehouses] = self::warehouseSelection((array) $request->input('WHDDA', [])); + $reportPeriod = (string) $request->input('reportPeriod', ''); + $result = collect(); + + try { + if ($company !== '' && $warehouses !== [] && $reportPeriod !== '') { + $sql = self::summarySql($reportPeriod, $request, $company, $warehouses); + $result = $sql === '' ? collect() : DB::connection(config('m3.connection', 'pgsql'))->select($sql); + } + } catch (\Throwable $throwable) { + Log::error($throwable); + } + + return response()->json([ + 'success' => true, + 'reportPeriod' => $reportPeriod, + 'result' => $result, + 'checkSales' => $request->input('checkSales', ''), + 'checkWeight' => $request->input('checkWeight', ''), + 'checkLines' => $request->input('checkLines', ''), + ]); + } + + private static function pageResponse() + { + $warehouses = collect(); + $company = '110'; + $plant = '144'; + $warehouse = '322'; + + try { + $profile = self::currentUserProfile(); + if ($profile !== null) { + $company = $profile->R428CO ?? $company; + $plant = $profile->R428PL ?? $plant; + $warehouse = $profile->R428WH ?? $warehouse; + } + $warehouses = DB::connection(config('m3.connection', 'pgsql'))->table('ic670m')->get(); + } catch (\Throwable $throwable) { + Log::error($throwable); + } + + return view('m3::liberty-theme.pages.execSummary', [ + 'IC670M' => $warehouses, + 'WHDDA' => $company . $plant . $warehouse, + ]); + } + + private static function currentUserProfile(): ?object + { + $user = m3User() !== '' ? Str::upper(trim(m3User())) : ''; + if ($user === '') { + return null; + } + + return DB::connection(config('m3.connection', 'pgsql'))->table('icm428m') + ->select('R428US', 'R428CO', 'R428WH', 'R428PL') + ->where('R428US', $user) + ->first(); + } + + private static function warehouseSelection(array $selection): array + { + $company = ''; + $warehouses = []; + foreach ($selection as $value) { + $company = substr((string) $value, 0, 3); + $warehouses[] = substr((string) $value, 6, 3); + } + + return [$company, $warehouses]; + } + + private static function summarySql(string $period, Request $request, string $company, array $warehouses): string + { + $range = self::dateRange($period, $request); + if ($range === null) { + return ''; + } + + [$from, $to] = $range; + $warehouseList = implode(',', $warehouses); + $groupExpression = match ($period) { + 'year' => 'substr(ayapr8N002,1,4)', + 'quarter' => "substr(ayapr8N002,3,2) ||'-' || 'Q' || EXTRACT(QUARTER FROM TO_DATE(CAST(ayapr8n002 AS TEXT),'YYYYMMDD'))", + 'monthly' => "substr(ayapr8N002,1,4) || '-' || substr(ayapr8N002,5,2)", + 'custom' => 'substr(ayapr8N002,7,2)', + default => '', + }; + if ($groupExpression === '') { + return ''; + } + + return "with tempfile as ( + select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, + (select max(ayapr8N002) from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) as ayapr8N002 + from icm688m2 where a5macpkr in ( + select distinct aymacpkr from icm695m1 + where AYSTATUS in ('Z','z') and ayapr8N002>=$from and ayapr8N002<=$to + ) and a5co='$company' and a5whs in($warehouseList) + ) + select $groupExpression as years,count(*) as lines,sum(a5rsvqty) as reserveqty, + sum(a5packed) as packed,sum(a5lnwght*a5packed/a5rsvqty) as weight, + sum(dollars) as dollars,a5whs as whs + from tempfile group by $groupExpression,a5whs order by 1"; + } + + private static function dateRange(string $period, Request $request): ?array + { + $from = self::monthDate($request->input('REPORTDATE')); + if ($from === null) { + return null; + } + + if ($period === 'custom') { + return [(clone $from)->modify('first day of this month')->format('Ymd'), (clone $from)->modify('last day of this month')->format('Ymd')]; + } + + $to = self::monthDate($request->input('TOREPORTDATE')); + if ($to === null) { + return null; + } + + return match ($period) { + 'year', 'quarter' => [(clone $from)->modify('first day of January')->format('Ymd'), (clone $to)->modify('last day of December')->format('Ymd')], + 'monthly' => [(clone $from)->modify('first day of this month')->format('Ymd'), (clone $to)->modify('last day of this month')->format('Ymd')], + default => null, + }; + } + + private static function monthDate(mixed $value): ?\DateTime + { + if (! is_string($value) || trim($value) === '') { + return null; + } + + $date = \DateTime::createFromFormat('Y-m', trim($value)); + + return $date instanceof \DateTime ? $date : null; } } diff --git a/Modules/M3/Http/Controllers/ExecSummaryLineController.php b/Modules/M3/Http/Controllers/ExecSummaryLineController.php index 0b82a612..4ffc0eb3 100755 --- a/Modules/M3/Http/Controllers/ExecSummaryLineController.php +++ b/Modules/M3/Http/Controllers/ExecSummaryLineController.php @@ -19,186 +19,158 @@ class ExecSummaryLineController extends Controller public static function index(Request $request) { - try { - // Ajax Call - if ($request->ajax()) { - $wHDDA = isset($request->WHDDA) ? $request->WHDDA : []; - $wHArr = []; - foreach ($wHDDA as $WHDDAV) { - $cO = substr($WHDDAV, 0, 3); - $wHArr[] = substr($WHDDAV, 6, 3); - } - - $reportPeriod = isset($request->reportPeriod) ? $request->reportPeriod : ''; // monthly , quarter , year , daily - // Current - $checkSales = isset($request->checkSales) ? $request->checkSales : ''; // on - $checkWeight = isset($request->checkWeight) ? $request->checkWeight : ''; // on - $checkLines = isset($request->checkLines) ? $request->checkLines : ''; // on - - if (! empty($wHArr) && $cO != '' && $reportPeriod != '') { - switch ($reportPeriod) { - case 'year': - // Set Dates based on reportPeriod type - $firstDay = isset($request->REPORTDATE) ? \DateTime::createFromFormat('Y-m', $request->REPORTDATE) : ''; - $reportDate = $firstDay->modify('first day of January')->format('Ymd'); - - $lastDay = isset($request->TOREPORTDATE) ? \DateTime::createFromFormat('Y-m', trim($request->TOREPORTDATE)) : ''; - $toreportDate = $lastDay->modify('last day of December')->format('Ymd'); - - // Build SQL - $selectContent = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDate and ayapr8N002<=$toreportDate ) - ) and a5co='" . $cO . "' and a5whs in(" . implode(',', $wHArr) . ') - ) - select substr(ayapr8N002,1,4) as years,count(*) as lines,sum(a5rsvqty) as reserveqty,sum(a5packed) as packed,sum(a5lnwght*a5packed/a5rsvqty) as weight,sum(dollars) as dollars,a5whs as whs from tempfile - group by substr(ayapr8N002,1,4),a5whs order by 1 '; - - $execSummaryResLY = []; - break; - - case 'quarter': - // Set Dates based on reportPeriod type - $firstDay = isset($request->REPORTDATE) ? \DateTime::createFromFormat('Y-m', $request->REPORTDATE) : ''; - $reportDate = $firstDay->modify('first day of January')->format('Ymd'); - - $lastDay = isset($request->TOREPORTDATE) ? \DateTime::createFromFormat('Y-m', trim($request->TOREPORTDATE)) : ''; - $toreportDate = $lastDay->modify('last day of December')->format('Ymd'); - // Build SQL - $selectContent = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDate and ayapr8N002<=$toreportDate ) - ) and a5co='" . $cO . self::WAREHOUSE_IN_CLAUSE . implode(',', $wHArr) . ") - ) - select 'Q' || EXTRACT(QUARTER FROM TO_DATE(CAST(ayapr8n002 AS TEXT),'YYYYMMDD')) ||'-' || substr(ayapr8N002,3,2) as years,count(*) as lines, - sum(a5rsvqty) as reserveqty, sum(a5packed) as packed,sum (a5lnwght*a5packed/a5rsvqty) as weight,sum(dollars) as dollars, a5whs as whs - from tempfile - group by 'Q' || EXTRACT(QUARTER FROM TO_DATE(CAST(ayapr8n002 AS TEXT),'YYYYMMDD')) ||'-' || substr(ayapr8N002,3,2),a5whs order by 1 "; - - $execSummaryResLY = []; - - break; - - case 'monthly': - // Set Dates based on reportPeriod type - $firstDay = isset($request->REPORTDATE) ? \DateTime::createFromFormat('Y-m', $request->REPORTDATE) : null; - - $lastDay = (clone $firstDay); - $reportDate = $firstDay->modify('-11 months')->modify(self::FIRST_DAY_THIS_MONTH)->format('Ymd'); // - $toreportDate = $lastDay ? $lastDay->modify(self::LAST_DAY_THIS_MONTH)->format('Ymd') : ''; - // Last Year Dates - // Same period, but for Last Year (LY) - $reportDateLY = (clone $firstDay)->modify(self::ONE_YEAR_AGO)->modify(self::FIRST_DAY_THIS_MONTH)->format('Ymd'); - $toreportDateLY = (clone $lastDay)->modify(self::ONE_YEAR_AGO)->modify(self::LAST_DAY_THIS_MONTH)->format('Ymd'); - - // Build SQL - $selectContent = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDate and ayapr8N002<=$toreportDate ) - ) and a5co='" . $cO . self::WAREHOUSE_IN_CLAUSE . implode(',', $wHArr) . ") - ) - select substr(ayapr8N002,1,4) || '-' || substr(ayapr8N002,5,2) as years,count(*) as lines,sum(a5rsvqty) as reserveqty,sum(a5packed) as packed, - sum(a5lnwght*a5packed/a5rsvqty) as weight,sum(dollars) as dollars, a5whs as whs - from tempfile - group by substr(ayapr8N002,1,4) || '-' || substr(ayapr8N002,5,2),a5whs order by 1 "; - - $selectContentLY = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDateLY and ayapr8N002<=$toreportDateLY ) - ) and a5co='" . $cO . self::WAREHOUSE_IN_CLAUSE . implode(',', $wHArr) . ") - ) - select substr(ayapr8N002,1,4) || '-' || substr(ayapr8N002,5,2) as years,count(*) as lines,sum(a5rsvqty) as reserveqty,sum(a5packed) as packed, - sum(a5lnwght*a5packed/a5rsvqty) as weight,sum(dollars) as dollars, a5whs as whs - from tempfile - group by substr(ayapr8N002,1,4) || '-' || substr(ayapr8N002,5,2),a5whs order by 1 "; - - $execSummaryResLY = DB::connection(config('m3.connection', 'pgsql'))->select($selectContentLY); - - break; - - case 'custom': - // Set Dates based on reportPeriod type - $firstDay = isset($request->REPORTDATE) ? \DateTime::createFromFormat('Y-m', $request->REPORTDATE) : ''; - $reportDate = $firstDay->modify(self::FIRST_DAY_THIS_MONTH)->format('Ymd'); - $toreportDate = $firstDay->modify(self::LAST_DAY_THIS_MONTH)->format('Ymd'); - - // Last Year Dates - $reportDateLY = (clone $firstDay)->modify(self::ONE_YEAR_AGO)->modify(self::FIRST_DAY_THIS_MONTH)->format('Ymd'); - $toreportDateLY = (clone $firstDay)->modify(self::ONE_YEAR_AGO)->modify(self::LAST_DAY_THIS_MONTH)->format('Ymd'); - - // Build SQL - $selectContent = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDate and ayapr8N002<=$toreportDate ) - ) and a5co='" . $cO . self::WAREHOUSE_IN_CLAUSE . implode(',', $wHArr) . ') - ) - select substr(ayapr8N002,7,2) as years,count(*) as lines,sum(a5rsvqty) as reserveqty,sum(a5packed) as packed, - sum(a5lnwght*a5packed/a5rsvqty) as weight, sum(dollars) as dollars, a5whs as whs - from tempfile - group by substr(ayapr8N002,7,2),a5whs order by 1 '; - - $selectContentLY = "with tempfile as ( - select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, - (select max(ayapr8N002) as ayapr8N002 from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) - from icm688m2 where a5macpkr in ( - select distinct aymacpkr from icm695m1 where (AYSTATUS in ('Z','z') and ayapr8N002>=$reportDateLY and ayapr8N002<=$toreportDateLY ) - ) and a5co='" . $cO . self::WAREHOUSE_IN_CLAUSE . implode(',', $wHArr) . ') - ) - select substr(ayapr8N002,7,2) as years,count(*) as lines,sum(a5rsvqty) as reserveqty,sum(a5packed) as packed, - sum(a5lnwght*a5packed/a5rsvqty) as weight, sum(dollars) as dollars, a5whs as whs - from tempfile - group by substr(ayapr8N002,7,2),a5whs order by 1 '; - - $execSummaryResLY = DB::connection(config('m3.connection', 'pgsql'))->select($selectContentLY); - break; - - default: - $selectContent = ''; - break; - } - - $execSummaryRes = DB::connection(config('m3.connection', 'pgsql'))->select($selectContent); - } else { - $execSummaryRes = collect(); - $execSummaryResLY = collect(); - } - - return response()->json(['success' => true, 'reportPeriod' => $reportPeriod, 'result' => $execSummaryRes, 'resultLY' => $execSummaryResLY, - 'checkSales' => $checkSales, 'checkWeight' => $checkWeight, 'checkLines' => $checkLines]); - } - - // Init Page - $sHUSER = m3User() !== '' ? Str::upper(trim(m3User())) : ''; - if ($sHUSER != '') { - // Get User CO & WH from Table ICM428M - $iCM428M = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m') - ->select('R428US', 'R428CO', 'R428WH', 'R428PL') - ->where('R428US', '=', $sHUSER) - ->first(); - - $cOMPANY = isset($iCM428M->R428CO) ? $iCM428M->R428CO : '110'; // Company - $pLANT = isset($iCM428M->R428PL) ? $iCM428M->R428PL : '144'; // Plant - $wHS = isset($iCM428M->R428WH) ? $iCM428M->R428WH : '322'; // WareHouse - } - $wHDDA = $cOMPANY . $pLANT . $wHS; - // dump($wHDDA); - // Warehouse Table - $iC670M = DB::connection(config('m3.connection', 'pgsql'))->table('ic670m')->get(); - } catch (\Throwable $th) { - Log::error($th); + if ($request->ajax()) { + return self::ajaxResponse($request); } - return view('m3::liberty-theme.pages.execSummaryLine', ['IC670M' => $iC670M, 'WHDDA' => $wHDDA]); - // ,'ICM688M2',,'COMPANY','PLANT','WHS','reportDate' + return self::pageResponse(); + } + + private static function ajaxResponse(Request $request) + { + [$company, $warehouses] = self::warehouseSelection((array) $request->input('WHDDA', [])); + $period = (string) $request->input('reportPeriod', ''); + $result = collect(); + $lastYear = collect(); + + try { + if ($company !== '' && $warehouses !== [] && $period !== '') { + [$sql, $lastYearSql] = self::periodSql($period, $request, $company, $warehouses); + $result = $sql === '' ? collect() : DB::connection(config('m3.connection', 'pgsql'))->select($sql); + $lastYear = $lastYearSql === '' ? [] : DB::connection(config('m3.connection', 'pgsql'))->select($lastYearSql); + } + } catch (\Throwable $throwable) { + Log::error($throwable); + } + + return response()->json([ + 'success' => true, 'reportPeriod' => $period, 'result' => $result, 'resultLY' => $lastYear, + 'checkSales' => $request->input('checkSales', ''), 'checkWeight' => $request->input('checkWeight', ''), + 'checkLines' => $request->input('checkLines', ''), + ]); + } + + private static function pageResponse() + { + $company = '110'; + $plant = '144'; + $warehouse = '322'; + $warehouses = collect(); + try { + $user = m3User() !== '' ? Str::upper(trim(m3User())) : ''; + if ($user !== '') { + $profile = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m') + ->select('R428US', 'R428CO', 'R428WH', 'R428PL')->where('R428US', $user)->first(); + $company = $profile->R428CO ?? $company; + $plant = $profile->R428PL ?? $plant; + $warehouse = $profile->R428WH ?? $warehouse; + } + $warehouses = DB::connection(config('m3.connection', 'pgsql'))->table('ic670m')->get(); + } catch (\Throwable $throwable) { + Log::error($throwable); + } + + return view('m3::liberty-theme.pages.execSummaryLine', ['IC670M' => $warehouses, 'WHDDA' => $company . $plant . $warehouse]); + } + + private static function warehouseSelection(array $selection): array + { + $company = ''; + $warehouses = []; + foreach ($selection as $value) { + $company = substr((string) $value, 0, 3); + $warehouses[] = substr((string) $value, 6, 3); + } + + return [$company, $warehouses]; + } + + private static function periodSql(string $period, Request $request, string $company, array $warehouses): array + { + return match ($period) { + 'year' => self::yearSql($request, $company, $warehouses), + 'quarter' => self::quarterSql($request, $company, $warehouses), + 'monthly' => self::monthlySql($request, $company, $warehouses), + 'custom' => self::customSql($request, $company, $warehouses), + default => ['', ''], + }; + } + + private static function yearSql(Request $request, string $company, array $warehouses): array + { + $range = self::range($request, 'first day of January', 'last day of December'); + return $range === null ? ['', ''] : [self::summarySql($range[0], $range[1], $company, $warehouses, 'substr(ayapr8N002,1,4)'), '']; + } + + private static function quarterSql(Request $request, string $company, array $warehouses): array + { + $range = self::range($request, 'first day of January', 'last day of December'); + $group = "'Q' || EXTRACT(QUARTER FROM TO_DATE(CAST(ayapr8n002 AS TEXT),'YYYYMMDD')) ||'-' || substr(ayapr8N002,3,2)"; + return $range === null ? ['', ''] : [self::summarySql($range[0], $range[1], $company, $warehouses, $group), '']; + } + + private static function monthlySql(Request $request, string $company, array $warehouses): array + { + $date = self::monthDate($request->input('REPORTDATE')); + if ($date === null) { + return ['', '']; + } + $end = clone $date; + $from = (clone $date)->modify('-11 months')->modify(self::FIRST_DAY_THIS_MONTH)->format('Ymd'); + $to = $end->modify(self::LAST_DAY_THIS_MONTH)->format('Ymd'); + $lastFrom = (clone $date)->modify(self::ONE_YEAR_AGO)->modify('-11 months')->modify(self::FIRST_DAY_THIS_MONTH)->format('Ymd'); + $lastTo = (clone $date)->modify(self::ONE_YEAR_AGO)->modify(self::LAST_DAY_THIS_MONTH)->format('Ymd'); + $group = "substr(ayapr8N002,1,4) || '-' || substr(ayapr8N002,5,2)"; + + return [self::summarySql($from, $to, $company, $warehouses, $group), self::summarySql($lastFrom, $lastTo, $company, $warehouses, $group)]; + } + + private static function customSql(Request $request, string $company, array $warehouses): array + { + $date = self::monthDate($request->input('REPORTDATE')); + if ($date === null) { + return ['', '']; + } + $from = (clone $date)->modify(self::FIRST_DAY_THIS_MONTH)->format('Ymd'); + $to = (clone $date)->modify(self::LAST_DAY_THIS_MONTH)->format('Ymd'); + $lastFrom = (clone $date)->modify(self::ONE_YEAR_AGO)->modify(self::FIRST_DAY_THIS_MONTH)->format('Ymd'); + $lastTo = (clone $date)->modify(self::ONE_YEAR_AGO)->modify(self::LAST_DAY_THIS_MONTH)->format('Ymd'); + $group = 'substr(ayapr8N002,7,2)'; + + return [self::summarySql($from, $to, $company, $warehouses, $group), self::summarySql($lastFrom, $lastTo, $company, $warehouses, $group)]; + } + + private static function range(Request $request, string $fromModifier, string $toModifier): ?array + { + $from = self::monthDate($request->input('REPORTDATE')); + $to = self::monthDate($request->input('TOREPORTDATE')); + if ($from === null || $to === null) { + return null; + } + + return [(clone $from)->modify($fromModifier)->format('Ymd'), (clone $to)->modify($toModifier)->format('Ymd')]; + } + + private static function monthDate(mixed $value): ?\DateTime + { + if (! is_string($value) || trim($value) === '') { + return null; + } + $date = \DateTime::createFromFormat('Y-m', trim($value)); + return $date instanceof \DateTime ? $date : null; + } + + private static function summarySql(string $from, string $to, string $company, array $warehouses, string $group): string + { + $warehouseList = implode(',', $warehouses); + return "with tempfile as ( + select a5rsvqty,a5packed,a5slunpr*a5packed as dollars,a5lnwght,a5whs, + (select max(ayapr8N002) from icm695m1 where a5macpkr=aymacpkr fetch first 1 rows only) as ayapr8N002 + from icm688m2 where a5macpkr in ( + select distinct aymacpkr from icm695m1 where AYSTATUS in ('Z','z') and ayapr8N002>=$from and ayapr8N002<=$to + ) and a5co='$company' and a5whs in($warehouseList) + ) select $group as years,count(*) as lines,sum(a5rsvqty) as reserveqty,sum(a5packed) as packed, + sum(a5lnwght*a5packed/a5rsvqty) as weight,sum(dollars) as dollars,a5whs as whs + from tempfile group by $group,a5whs order by 1"; } } diff --git a/Modules/M3/Http/Controllers/M3GetPDFFromCacheController.php b/Modules/M3/Http/Controllers/M3GetPDFFromCacheController.php index c15bb4c2..0b2bf35b 100755 --- a/Modules/M3/Http/Controllers/M3GetPDFFromCacheController.php +++ b/Modules/M3/Http/Controllers/M3GetPDFFromCacheController.php @@ -12,169 +12,161 @@ class M3GetPDFFromCacheController extends Controller { public static function index(Request $request) { - // Parse PDF file and build necessary objects. + $state = self::initialState(); - $aggno = $aggmsg = ''; - $iC670M = $pDFArray = $carrier = $trackingno = $iCM688M1 = []; - $aYMACPKR = $wHDDA = $cOMPANY = $pLANT = $wHS = ''; - - // PDF File PATH - $pdfFilePath = Config::get('arrays.PdfPath'); try { - // Initialise Vars - $pDFArray = $trackingno = $carrier = $iCM688M1 = []; - - $sHUSER = m3User() !== '' ? Str::upper(trim(m3User())) : ''; - - if ($sHUSER != '') { - // Get User CO & WH - // Get user from ICM428M - $iCM428M = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m') - ->select('R428US', 'R428CO', 'R428WH', 'R428PL') - ->where('R428US', '=', $sHUSER) - ->first(); - - $cOMPANY = isset($iCM428M->R428CO) ? $iCM428M->R428CO : ''; // Company - $pLANT = isset($iCM428M->R428PL) ? $iCM428M->R428PL : ''; // Plant - $wHS = isset($iCM428M->R428WH) ? $iCM428M->R428WH : ''; // WareHouse - } - - // Warehouse Table - $iC670M = DB::connection(config('m3.connection', 'pgsql'))->table('ic670m')->get(); - // ->where('WDCO',$userCO)->where('WDPL',$userPL)->where('WDWHS',$userWH) - - // Get URL PARAMS - $urlParams = count($request->all()) > 0 ? $request->all() : ''; - // dump($urlParams); - $aYMACPKR = isset($urlParams['MACPKR']) ? trim($urlParams['MACPKR']) : ''; // Delivery note number - $dOCTYPE = isset($urlParams['DOCTYPE']) ? trim($urlParams['DOCTYPE']) : ''; // Document type (PackingList, CarrierLabel, ContentLabel) - $wHDDA = $cOMPANY . $pLANT . $wHS; - - // Check Doctype fisrt - $docTypes = $dOCTYPE != '' ? [$dOCTYPE] : ['PackingList', 'ContentLabel', 'CarrierLabel', 'GSLabel']; // document type (PackingList, CarrierLabel, ContentLabel) - - if ($urlParams != '') { - // dump($urlParams); - // document number (ie the internal box number, the pick list number) - // Key Combination : COMPANY WHS A4MACPKR DOCTYPE DOCNUM - // Document type (PackingList, CarrierLabel, ContentLabel) - - // Get Aggregation Number if it exists - $iCM695M1Agg = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1') - ->select('AYTRUCK') - ->where('AYCO', $cOMPANY) - ->where('AYWHS', $wHS) - ->where('AYMACPKR', $aYMACPKR) - ->first(); - $aggno = isset($iCM695M1Agg->AYTRUCK) ? trim($iCM695M1Agg->AYTRUCK) : ''; - - if ($aggno != '' && $aggno != '0') { - - $aggmsg = "This delivery note is part of aggregation $aggno "; - $iCM688M1 = DB::connection(config('m3.connection', 'pgsql'))->select("select A4MACPKR from ICM688M1 where A4SHPAGGCD='$aggno'"); - - // 654 add 'AYSPR15A01', 'AYSPR30A01', 'AYTRUCK' carrier and tracking# and agg# - $iCM695M1 = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1') - ->select('AYCO', 'AYWHS', 'AYMACPKR', 'AYINTBOX', 'AYSPR15A01', 'AYSPR30A01', 'AYTRUCK') - ->distinct() - ->where('AYCO', $cOMPANY) - ->where('AYWHS', $wHS) - // ->where('AYMACPKR',$aYMACPKR) - ->where('AYTRUCK', $aggno) - ->orderBy('AYMACPKR') - ->get(); - } else { - // 654 add 'AYSPR15A01', 'AYSPR30A01', 'AYTRUCK' carrier and tracking# and agg# - $iCM695M1 = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1') - ->select('AYCO', 'AYWHS', 'AYMACPKR', 'AYINTBOX', 'AYSPR15A01', 'AYSPR30A01', 'AYTRUCK') - ->distinct() - ->where('AYCO', $cOMPANY) - ->where('AYWHS', $wHS) - ->where('AYMACPKR', $aYMACPKR) - ->orderBy('AYMACPKR') - ->get(); - } - - if (count($iCM695M1) > 0) { - $tmpCacheArr = $carrier = $trackingno = []; - // Loop the Results from $ICM595M1TMP - foreach ($iCM695M1 as $ICM695M1R) { - $wHS = trim($ICM695M1R->AYWHS); - $cOMPANY = trim($ICM695M1R->AYCO); - $aYMACPKR = trim($ICM695M1R->AYMACPKR); - $dOCNUM = trim($ICM695M1R->AYINTBOX); - // 654 - $carrier[] = trim($ICM695M1R->AYSPR15A01); - $trackingno[] = trim($ICM695M1R->AYSPR30A01); - $aggno = trim($ICM695M1R->AYTRUCK); - - // Make Key and Test Validity - if ($aYMACPKR != '' && $cOMPANY != '' && $wHS != '') { - $contentLabel = ''; - - // Get Unique Keys - foreach ($docTypes as $value) { - // Check Parcel vs LTL - if ($value == 'ContentLabel') { - $dOCNUMT = $dOCNUM; - $contentLabel = ! is_numeric($dOCNUM) ? 'LTL' : ''; - } - - if ($value == 'CarrierLabel') { - $dOCNUMT = $dOCNUM; - - // Keep Solon Logic to read LTL - $dOCNUMT2 = $contentLabel == 'LTL' ? $aYMACPKR : $dOCNUM; - if ($dOCNUMT2 != $dOCNUMT) { - $pDFKey2 = $value . '/' . $value . '_' . $aYMACPKR . '_' . $dOCNUMT2 . '_' . $cOMPANY . '_' . $wHS; - $tmpCacheArr[] = $pDFKey2; - } - } - - if ($value == 'PackingList') { - $dOCNUMT = $aYMACPKR; - } - - // Get GS1 Labels - if ($value == 'GSLabel') { - $pDFKey = $value . '/' . $value . '_' . $aYMACPKR . '_' . $cOMPANY . '_' . $wHS . '_' . $dOCNUM; - - // $pDFKey = $value.'/'.$value.'_'.$aYMACPKR.'_'.$cOMPANY.'_'.$wHS; - // } - // else{ - // $pDFKey = $value.'/'.$value.'_'.$aYMACPKR.'_'.$cOMPANY.'_'.$wHS.'_'.$dOCNUM; - // } - } else { - $pDFKey = $value . '/' . $value . '_' . $aYMACPKR . '_' . $dOCNUMT . '_' . $cOMPANY . '_' . $wHS; - } - $tmpCacheArr[] = $pDFKey; - } - } - } - - // Get unique values from array $pDFArray - $tmpCacheArr = collect($tmpCacheArr)->unique(); - foreach ($tmpCacheArr as $value) { - - $pdfFileName = $pdfFilePath . $value . '.pdf'; - if (File::exists($pdfFileName)) { - - $pdfFile = File::get($pdfFileName); - // Transform content to base64 - // $base64PDF = $pdfFile; - $base64PDF = base64_encode($pdfFile); - $pDFArray[] = $base64PDF; - } - } - } else { - // Nothing Found - } - } - } catch (\Throwable $th) { - report($th); + self::loadUserContext($state); + self::loadDocuments($request, $state); + } catch (\Throwable $throwable) { + report($throwable); } - return view('m3::liberty-theme.pages.M3GetPDF', ['IC670M' => $iC670M, 'PDFArray' => $pDFArray, 'AYMACPKR' => $aYMACPKR, 'WHDDA' => $wHDDA, 'aggmsg' => $aggmsg, 'carrier' => $carrier, 'trackingno' => $trackingno, 'ICM688M1' => $iCM688M1, 'COMPANY' => $cOMPANY, 'PLANT' => $pLANT, 'WHS' => $wHS]); - // compact('IC670M','ViewTitle') + return view('m3::liberty-theme.pages.M3GetPDF', self::viewData($state)); + } + + private static function initialState(): array + { + return [ + 'aggmsg' => '', 'IC670M' => collect(), 'PDFArray' => [], 'carrier' => [], + 'trackingno' => [], 'ICM688M1' => [], 'AYMACPKR' => '', 'WHDDA' => '', + 'COMPANY' => '', 'PLANT' => '', 'WHS' => '', + ]; + } + + private static function loadUserContext(array &$state): void + { + $user = m3User() !== '' ? Str::upper(trim(m3User())) : ''; + if ($user !== '') { + $profile = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m') + ->select('R428US', 'R428CO', 'R428WH', 'R428PL')->where('R428US', $user)->first(); + $state['COMPANY'] = $profile->R428CO ?? ''; + $state['PLANT'] = $profile->R428PL ?? ''; + $state['WHS'] = $profile->R428WH ?? ''; + } + + $state['WHDDA'] = $state['COMPANY'] . $state['PLANT'] . $state['WHS']; + $state['IC670M'] = DB::connection(config('m3.connection', 'pgsql'))->table('ic670m')->get(); + } + + private static function loadDocuments(Request $request, array &$state): void + { + $params = $request->all(); + if ($params === []) { + return; + } + + $state['AYMACPKR'] = trim((string) ($params['MACPKR'] ?? '')); + $documentType = trim((string) ($params['DOCTYPE'] ?? '')); + $documentTypes = $documentType !== '' ? [$documentType] : ['PackingList', 'ContentLabel', 'CarrierLabel', 'GSLabel']; + $aggregation = self::aggregationNumber($state); + $rows = self::documentRows($state, $aggregation); + if ($rows->isEmpty()) { + return; + } + + $keys = self::cacheKeys($rows, $documentTypes, $state); + $state['PDFArray'] = self::readCachedPdfs($keys); + } + + private static function aggregationNumber(array &$state): string + { + $row = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1') + ->select('AYTRUCK')->where('AYCO', $state['COMPANY'])->where('AYWHS', $state['WHS']) + ->where('AYMACPKR', $state['AYMACPKR'])->first(); + $aggregation = trim((string) ($row->AYTRUCK ?? '')); + if ($aggregation !== '' && $aggregation !== '0') { + $state['aggmsg'] = "This delivery note is part of aggregation $aggregation "; + $state['ICM688M1'] = DB::connection(config('m3.connection', 'pgsql')) + ->table('icm688m1')->select('A4MACPKR')->where('A4SHPAGGCD', $aggregation)->get()->all(); + } + + return $aggregation; + } + + private static function documentRows(array $state, string $aggregation) + { + $query = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1') + ->select('AYCO', 'AYWHS', 'AYMACPKR', 'AYINTBOX', 'AYSPR15A01', 'AYSPR30A01', 'AYTRUCK') + ->distinct()->where('AYCO', $state['COMPANY'])->where('AYWHS', $state['WHS']); + + $aggregation !== '' && $aggregation !== '0' + ? $query->where('AYTRUCK', $aggregation) + : $query->where('AYMACPKR', $state['AYMACPKR']); + + return $query->orderBy('AYMACPKR')->get(); + } + + private static function cacheKeys(iterable $rows, array $documentTypes, array &$state): array + { + $keys = []; + foreach ($rows as $row) { + self::appendRowKeys($keys, $row, $documentTypes, $state); + } + + return array_values(array_unique($keys)); + } + + private static function appendRowKeys(array &$keys, object $row, array $documentTypes, array &$state): void + { + $warehouse = trim((string) $row->AYWHS); + $company = trim((string) $row->AYCO); + $picker = trim((string) $row->AYMACPKR); + $documentNumber = trim((string) $row->AYINTBOX); + $state['WHS'] = $warehouse; + $state['COMPANY'] = $company; + $state['AYMACPKR'] = $picker; + $state['carrier'][] = trim((string) $row->AYSPR15A01); + $state['trackingno'][] = trim((string) $row->AYSPR30A01); + + if ($picker === '' || $company === '' || $warehouse === '') { + return; + } + + $contentLabel = ''; + foreach ($documentTypes as $type) { + [$key, $extraKey, $contentLabel] = self::documentKey($type, $picker, $documentNumber, $company, $warehouse, $contentLabel); + if ($extraKey !== null) { + $keys[] = $extraKey; + } + $keys[] = $key; + } + } + + private static function documentKey(string $type, string $picker, string $number, string $company, string $warehouse, string $contentLabel): array + { + $documentNumber = $number; + $extraKey = null; + if ($type === 'ContentLabel') { + $contentLabel = is_numeric($number) ? '' : 'LTL'; + } elseif ($type === 'PackingList') { + $documentNumber = $picker; + } elseif ($type === 'CarrierLabel' && $contentLabel === 'LTL') { + $extraKey = "$type/{$type}_{$picker}_{$picker}_{$company}_{$warehouse}"; + } + + $key = $type === 'GSLabel' + ? "$type/{$type}_{$picker}_{$company}_{$warehouse}_{$number}" + : "$type/{$type}_{$picker}_{$documentNumber}_{$company}_{$warehouse}"; + + return [$key, $extraKey, $contentLabel]; + } + + private static function readCachedPdfs(array $keys): array + { + $result = []; + $path = (string) Config::get('arrays.PdfPath'); + foreach ($keys as $key) { + $fileName = $path . $key . '.pdf'; + if (File::exists($fileName)) { + $result[] = base64_encode(File::get($fileName)); + } + } + + return $result; + } + + private static function viewData(array $state): array + { + return $state; } }