M3 Security Fixes for pass 28
This commit is contained in:
@@ -4,7 +4,7 @@ namespace Modules\M3\Http\Controllers;
|
||||
|
||||
use DateTime;
|
||||
use DB;
|
||||
use Illuminate\Support\Facades\Http; // added this for curl call to v4
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -13,133 +13,128 @@ class userPickingStats5 extends Controller
|
||||
{
|
||||
public static function index(Request $request)
|
||||
{
|
||||
[$userArray, $co, $whs] = self::loadUsers();
|
||||
[$starting, $ending, $days] = self::dateRange($request);
|
||||
$user = $request->user;
|
||||
$statistics = [];
|
||||
$totals = self::emptyTotals();
|
||||
|
||||
// 618
|
||||
// Get Logged User CO & WH. Legacy M3 profile data may not yet exist in PostgreSQL.
|
||||
$co = '';
|
||||
$whs = '';
|
||||
$user_array = [];
|
||||
try {
|
||||
if (Schema::connection(config('m3.connection', 'pgsql'))->hasTable('icm428m')) {
|
||||
$profile = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
|
||||
->select('R428US', 'R428CO', 'R428WH')
|
||||
->where('R428US', strtoupper((string) m3_user()))
|
||||
->first();
|
||||
$co = trim((string) ($profile->R428CO ?? ''));
|
||||
$whs = trim((string) ($profile->R428WH ?? ''));
|
||||
|
||||
if ($co !== '' && $whs !== '') {
|
||||
$rows = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
|
||||
->selectRaw("R428US as userprf, (trim(R428FIRST) || ' ' || trim(R428FAMILY)) as name, R428EMPCDE as empno")
|
||||
->where('R428EMPCDE', '!=', 0)
|
||||
->where('R428CO', $co)
|
||||
->where('R428WH', $whs)
|
||||
->get();
|
||||
foreach ($rows as $row) {
|
||||
$user_array[] = [
|
||||
'userprf' => trim((string) $row->userprf),
|
||||
'name' => trim((string) $row->name),
|
||||
'empno' => $row->empno,
|
||||
];
|
||||
}
|
||||
array_multisort(array_column($user_array, 'name'), SORT_ASC, $user_array);
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('M3 picking user list unavailable', ['exception' => $e->getMessage()]);
|
||||
$user_array = [];
|
||||
if (isset($request->dates)) {
|
||||
[$statistics, $totals] = self::collectDailyStatistics($request, $starting, $days, $totals);
|
||||
}
|
||||
|
||||
// Filter by date
|
||||
if ($request->format === 'json') {
|
||||
return response()->json($statistics);
|
||||
}
|
||||
|
||||
$statistic5 = $statistics;
|
||||
$totals_array = $totals;
|
||||
$user_array = $userArray;
|
||||
|
||||
return view('m3::liberty-theme.pages.userPickingStats5', compact(
|
||||
'statistic5', 'totals_array', 'user_array', 'user', 'starting', 'ending', 'request'
|
||||
));
|
||||
}
|
||||
|
||||
private static function loadUsers(): array
|
||||
{
|
||||
$users = [];
|
||||
$co = '';
|
||||
$whs = '';
|
||||
|
||||
try {
|
||||
$connection = config('m3.connection', 'pgsql');
|
||||
if (! Schema::connection($connection)->hasTable('icm428m')) {
|
||||
return [$users, $co, $whs];
|
||||
}
|
||||
|
||||
$profile = DB::connection($connection)->table('icm428m')
|
||||
->select('R428CO', 'R428WH')
|
||||
->where('R428US', strtoupper((string) m3_user()))
|
||||
->first();
|
||||
$co = trim((string) ($profile->R428CO ?? ''));
|
||||
$whs = trim((string) ($profile->R428WH ?? ''));
|
||||
if ($co === '' || $whs === '') {
|
||||
return [$users, $co, $whs];
|
||||
}
|
||||
|
||||
$rows = DB::connection($connection)->table('icm428m')
|
||||
->selectRaw("R428US as userprf, (trim(R428FIRST) || ' ' || trim(R428FAMILY)) as name, R428EMPCDE as empno")
|
||||
->where('R428EMPCDE', '!=', 0)->where('R428CO', $co)->where('R428WH', $whs)->get();
|
||||
foreach ($rows as $row) {
|
||||
$users[] = ['userprf' => trim((string) $row->userprf), 'name' => trim((string) $row->name), 'empno' => $row->empno];
|
||||
}
|
||||
array_multisort(array_column($users, 'name'), SORT_ASC, $users);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('M3 picking user list unavailable', ['exception' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
return [$users, $co, $whs];
|
||||
}
|
||||
|
||||
private static function dateRange(Request $request): array
|
||||
{
|
||||
if (isset($request->dates)) {
|
||||
$date = explode(' - ', $request->dates);
|
||||
$start = str_replace([' ', '-'], '', $date[0]);
|
||||
$end = str_replace([' ', '-'], '', $date[1]);
|
||||
$starting = $date[0];
|
||||
$ending = $date[1];
|
||||
$dates = explode(' - ', (string) $request->dates);
|
||||
$starting = $dates[0] ?? date('Y-m-d');
|
||||
$ending = $dates[1] ?? $starting;
|
||||
} else {
|
||||
$start = date('Ymd', strtotime('-5 days'));
|
||||
$end = date('Ymd');
|
||||
$starting = date('Y-m-d', strtotime('-5 days'));
|
||||
// $starting = "2021-11-01";
|
||||
$ending = date('Y-m-d');
|
||||
}
|
||||
|
||||
// calculate day difference between 2 dates
|
||||
$str_date = new DateTime($starting);
|
||||
$end_date = new DateTime($ending);
|
||||
$interval = $str_date->diff($end_date);
|
||||
$days = $interval->format('%a'); // retrieve number of days %a
|
||||
// echo "diff=$days<br>";
|
||||
$days = (int) (new DateTime($starting))->diff(new DateTime($ending))->format('%a');
|
||||
return [$starting, $ending, $days];
|
||||
}
|
||||
|
||||
$user = $request->user;
|
||||
private static function emptyTotals(): array
|
||||
{
|
||||
return ['performance' => 0, 'blendedTargetLPH' => 0, 'actualLPH' => 0, 'grossHours' => 0];
|
||||
}
|
||||
|
||||
// retrieve session user and pw, also the HTTP server host address and port
|
||||
$username = $request->header('php-auth-user');
|
||||
$pw = $request->header('php-auth-pw');
|
||||
$http_host = $request->server('HTTP_HOST');
|
||||
$urlStats4 = "http://$http_host/userPickingStats4";
|
||||
private static function collectDailyStatistics(Request $request, string $starting, int $days, array $totals): array
|
||||
{
|
||||
$statistics = [];
|
||||
$count = 0;
|
||||
$url = 'http://' . $request->server('HTTP_HOST') . '/userPickingStats4';
|
||||
|
||||
// initialize arrays
|
||||
$statistic5 = $totals_array = [];
|
||||
// initialize totals
|
||||
$totals_array['performance'] = 0;
|
||||
$totals_array['blendedTargetLPH'] = 0;
|
||||
$totals_array['actualLPH'] = 0;
|
||||
$totals_array['grossHours'] = 0;
|
||||
$y = 0;
|
||||
|
||||
// loop for the days difference and passing proper date to v4 to get json encoded response
|
||||
if (isset($request->dates)) {
|
||||
for ($x = 0; $x <= $days; $x++) {
|
||||
|
||||
$next_date = date('Y-m-d', strtotime($starting . "+$x days"));
|
||||
|
||||
$parameters = ['dates' => $next_date, 'user' => $request->user, 'format' => 'json'];
|
||||
// echo "<pre>"; print_r($parameters); echo "$urlStats4</pre>";
|
||||
|
||||
$response = Http::withBasicAuth($username, $pw)->get($urlStats4, $parameters);
|
||||
// echo $response;
|
||||
$statusCode = $response->status();
|
||||
|
||||
// echo "$statusCode json return=".$response->getBody()." <br>";
|
||||
|
||||
$statistic5[$x] = json_decode($response->getBody(), true);
|
||||
|
||||
$totals_array['performance'] += isset($statistic5[$x]['performance']) ? $statistic5[$x]['performance'] : 0;
|
||||
$totals_array['blendedTargetLPH'] += isset($statistic5[$x]['blendedTargetLPH']) ? $statistic5[$x]['blendedTargetLPH'] : 0;
|
||||
$totals_array['actualLPH'] += isset($statistic5[$x]['actualLPH']) ? $statistic5[$x]['actualLPH'] : 0;
|
||||
$totals_array['grossHours'] += isset($statistic5[$x]['grossHours']) ? $statistic5[$x]['grossHours'] : 0;
|
||||
|
||||
if (! isset($statistic5[$x]['day'])) {
|
||||
$statistic5[$x]['day'] = $next_date;
|
||||
}
|
||||
if (isset($statistic5[$x]['performance'])) {
|
||||
if ($statistic5[$x]['performance'] > 0) {
|
||||
$y++;
|
||||
}
|
||||
} else {
|
||||
$statistic5[$x]['performance'] = 0;
|
||||
}
|
||||
for ($day = 0; $day <= $days; $day++) {
|
||||
$date = date('Y-m-d', strtotime($starting . "+$day days"));
|
||||
$statistics[$day] = self::fetchStatistic($request, $url, $date);
|
||||
self::addTotals($totals, $statistics[$day]);
|
||||
if (($statistics[$day]['performance'] ?? 0) > 0) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($y > 0) {
|
||||
$totals_array['performance'] /= $y;
|
||||
$totals_array['blendedTargetLPH'] /= $y;
|
||||
$totals_array['actualLPH'] /= $y;
|
||||
$totals_array['grossHours'] /= $y;
|
||||
if ($count > 0) {
|
||||
foreach (array_keys($totals) as $key) {
|
||||
$totals[$key] /= $count;
|
||||
}
|
||||
}
|
||||
|
||||
if ($request->format == 'json') {
|
||||
echo '<pre>';
|
||||
print_r($statistic5);
|
||||
echo '</pre>';
|
||||
} else {
|
||||
// dd($statistic5);
|
||||
// if($statistic5[1]!=null)
|
||||
return view('m3::liberty-theme.pages.userPickingStats5', compact('statistic5', 'totals_array', 'user_array', 'user', 'starting', 'ending', 'request'));
|
||||
}
|
||||
return [$statistics, $totals];
|
||||
}
|
||||
|
||||
private static function fetchStatistic(Request $request, string $url, string $date): array
|
||||
{
|
||||
try {
|
||||
$response = Http::withBasicAuth((string) $request->header('php-auth-user'), (string) $request->header('php-auth-pw'))
|
||||
->get($url, ['dates' => $date, 'user' => $request->user, 'format' => 'json']);
|
||||
$data = $response->successful() ? (json_decode($response->body(), true) ?: []) : [];
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('M3 picking statistics unavailable', ['date' => $date, 'exception' => $e->getMessage()]);
|
||||
$data = [];
|
||||
}
|
||||
$data['day'] = $data['day'] ?? $date;
|
||||
$data['performance'] = $data['performance'] ?? 0;
|
||||
return $data;
|
||||
}
|
||||
|
||||
private static function addTotals(array &$totals, array $statistic): void
|
||||
{
|
||||
foreach (array_keys($totals) as $key) {
|
||||
$totals[$key] += (float) ($statistic[$key] ?? 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user