M3 Integration fixes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user