Files
WMS/Modules/M3/Http/Controllers/userPickingStats4.php
T
2026-09-16 16:51:59 +01:00

318 lines
13 KiB
PHP

<?php
namespace Modules\M3\Http\Controllers;
use DB;
use Config;
use Symfony\Component\HttpFoundation\Request;
class userPickingStats4 extends Controller
{
public static function index(Request $request)
{
#$start = microtime(true);
#dd($request);
// Filter by date
if (isset($request->dates)) {
$date = $request->dates;
$starting = $date;
} else {
$starting = date("Y-m-d", strtotime("-1 days"));
}
##$min_work_hours_for_break = 4;
$min_work_hours_for_break = implode(",", Config::get('arrays.min_work_hours_for_break'));
##$ignore_picks_that_last_longer_than_this_many_hours = 8;
$ignore_picks_that_last_longer_than_this_many_hours = implode(",", Config::get('arrays.ignore_picks_that_last_longer_than_this_many_hours'));
#$user_array = Config::get('arrays.user_array');
#618
# Get Logged User CO & WH
# Get Users from ICM428M
$co = '';
$whs = '';
$ICM428M = [];
$schema = \Illuminate\Support\Facades\Schema::connection(config('m3.connection', 'pgsql'));
if ($schema->hasTable('icm428m')) {
$profile = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
->select('R428US','R428CO', 'R428WH')
->where('R428US','=',strtoUpper(m3_user()))
->first();
$co = $profile->R428CO ?? '';
$whs = $profile->R428WH ?? '';
$ICM428M = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
->selectRaw("R428US as userprf, (trim(R428FIRST) || ' ' || trim(R428FAMILY)) as name, R428EMPCDE as empno")
->where('R428STS', 'A')->where('R428EMPCDE', '!=', 0)
->where('R428CO', $co)->where('R428WH', $whs)->get()->all();
}
$user_array=array();
#while($row = odbc_fetch_object($result)) {
$x=0;
foreach($ICM428M as $key=>$row) {
// print_r($row);
$user_array[$x]['userprf']=trim($row->USERPRF);
#$user_array[$x]['name']=trim($row->R428FIRST)+' '+trim($row->R428FAMILY);
$user_array[$x]['name']=trim($row->NAME);
$user_array[$x]['empno']=$row->EMPNO;
$x++;
}
array_multisort(array_column($user_array, 'name'), SORT_ASC, $user_array);
// fill array of Lines per Hour per Monitor
$lph_per_monitor_array = Config::get('arrays_custom.lph_per_code_array');
#echo "<pre>"; print_r($user_array); echo "</pre>";
if (isset($request->user)) {
$user = $request->user;
$empno = 0;
foreach ($user_array as $key => $val) {
if ($user_array[$key]['userprf'] == $user) {
$empno = $user_array[$key]['empno'];
}
}
} else {
$empno = 0;
$user = "";
}
// get data from LBPF for user and day
$payrollCodes = "";
ksort($lph_per_monitor_array);
foreach ($lph_per_monitor_array as $key => $val) {
$payrollCodes .= $key . ",";
}
$payrollCodes = substr($payrollCodes, 0, -1);
$yr = substr($starting, 0, 4) - 2000;
$mth = substr($starting, 5, 2);
$day = substr($starting, 8, 2);
$sqlstmt = "select oppno, sum(cast (hrs as integer)+ (hrs-cast (hrs as integer))/0.6) as hrs from LBPF
where empno=$empno and lbyr=$yr and lbmo=$mth and lbdy=$day
and oppno in ($payrollCodes)
group by oppno";
#echo "$sqlstmt<br>";
$lbpf1 = DB::connection(config('m3.connection', 'pgsql'))->select($sqlstmt);
$lbpf = 0;
foreach ($lbpf1 as $key => $val) {
$lbpf += $val->HRS;
}
$sql = "select * from perfstat where dt='$starting' and a5pickcomu='$user' ";
#@dd($sql);
if (isset($request->dates) || isset($request->user)) {
$statistic4 = DB::connection(config('m3.connection', 'pgsql'))->select($sql);
} else {
$statistic4 = array();
}
#echo "<pre>"; print_r($statistic4); echo "</pre>";
#$duration = microtime(true) - $start;
// initialize arrays
$monitor_array = $totals_array = $perf_array = array();
$totals_array["value"] = 0;
$totals_array["weight"] = 0;
$totals_array["timediff"] = 0;
$totals_array["picked"] = 0;
$totals_array["entries"] = 0;
$diff = 0;
#echo "<pre>"; print_r($lbpf1); echo "</pre>";
foreach ($lbpf1 as $key => $val) {
$monitor_array[$val->OPPNO]['tot_hours'] = $val->HRS;
// Initialize these variables in case not existant in statistic4
$monitor_array[$val->OPPNO]['header'] = "Mon" . $val->OPPNO;
$monitor_array[$val->OPPNO]['timediff'] = 0;
$monitor_array[$val->OPPNO]["percent_per_monitor"] = 0;
$monitor_array[$val->OPPNO]["percent_per_activity"] = 0;
$monitor_array[$val->OPPNO]["lph_blended_per_activity"] = 0;
$monitor_array[$val->OPPNO]["picked"] = 0;
$monitor_array[$val->OPPNO]["weight"] = 0;
$monitor_array[$val->OPPNO]["lines"] = 0;
}
if (!empty($statistic4)) {
#echo "in !empty<br>";
// Initialize the Monitor array
foreach ($statistic4 as $key => $val) {
// Initialize these variables in case not existant in LBPF
$monitor_array[$val->CODE]['header'] = "Mon" . $statistic4[$key]->CODE;
$monitor_array[$val->CODE]['timediff'] = 0;
$monitor_array[$val->CODE]["percent_per_monitor"] = 0;
$monitor_array[$val->CODE]["percent_per_activity"] = 0;
$monitor_array[$val->CODE]["lph_blended_per_activity"] = 0;
$monitor_array[$val->CODE]["picked"] = 0;
$monitor_array[$val->CODE]["weight"] = 0;
$monitor_array[$val->CODE]["lines"] = 0;
}
#echo "after 1st init<br>";
// add another element (0) in monitor_array to capture the total blended lph value expected
$monitor_array[0]["lph_blended_per_activity"] = 0;
$monitor_array[0]['header'] = "0";
// cycle thru stats again, and accumulate totals at global level (totals_array) and at monitor level (monitor_array)
foreach ($statistic4 as $key => $val) {
$statistic4[$key]->WEIGHT = round($statistic4[$key]->WEIGHT);
$statistic4[$key]->A5VALUE = round($statistic4[$key]->A5VALUE);
$statistic4[$key]->A5PICKED = round($statistic4[$key]->A5PICKED);
$statistic4[$key]->A5PICKCMTS = date('Y-m-d H:i', strtotime($statistic4[$key]->A5PICKCMTS));
// Calculate difference from previous time entry in seconds and save in TIMEDIFF key of STATISTIC4 array
# $time_now=strtotime($statistic4[$key]->A5PICKCMTSADJ);
$time_now = strtotime($statistic4[$key]->A5CMTSADJ);
if ($key > 0) {
$diff = round(abs($time_now - $time_prev));
if ($diff > $ignore_picks_that_last_longer_than_this_many_hours * 60 * 60) {
$diff = -1;
}
}
$statistic4[$key]->TIMEDIFF = $diff;
if ($diff / 60 < 5) {
$statistic4[$key]->COLOUR = "";
}
if ($diff / 60 >= 5 && $diff / 60 <= 10) {
$statistic4[$key]->COLOUR = "yellow";
}
if ($diff / 60 > 10 && $diff / 60 <= 15) {
$statistic4[$key]->COLOUR = "#88ace0";
}
if ($diff / 60 > 15) {
$statistic4[$key]->COLOUR = "red";
}
// save the time for previous entry
# $time_prev=strtotime($statistic4[$key]->A5PICKCMTSADJ);
$time_prev = strtotime($statistic4[$key]->A5CMTSADJ);
// accumulate totals at monitor level
$monitor_array[$val->CODE]['timediff'] += $diff;
$monitor_array[$val->CODE]['picked'] += $statistic4[$key]->A5PICKED;
$monitor_array[$val->CODE]['weight'] += $statistic4[$key]->WEIGHT;
$monitor_array[$val->CODE]['lines']++;
// calculate totals at global level
$totals_array["entries"] = count($statistic4);
$totals_array["weight"] += $statistic4[$key]->WEIGHT;
$totals_array["value"] += $statistic4[$key]->A5VALUE;
$totals_array["timediff"] += $diff;
$totals_array["picked"] += $statistic4[$key]->A5PICKED;
}
#echo "after 2<pre>"; print_r($statistic4);
// build array for Performance with Greg's time entry (at one point we had a [0] array as well)
if (!is_null($lbpf)) {
$perf_array["tot_hours"] = (isset($lbpf) ? $lbpf : 0);
if ($perf_array["tot_hours"] <> 0) {
$perf_array["per_hour_actual"] = $totals_array["entries"] / $perf_array["tot_hours"];
} else {
$perf_array["per_hour_actual"] = 0;
}
}
// cycle through monitors array and compute percentage to allocate to each monitor (code)
ksort($monitor_array);
foreach ($monitor_array as $key => $val) {
if ($key != "0") {
if (!isset($monitor_array[$key]["tot_hours"])) {
$monitor_array[$key]["tot_hours"] = 0;
}
if ($totals_array["timediff"] <> 0) {
$monitor_array[$key]["percent_per_monitor"] = $monitor_array[$key]["timediff"] / $totals_array["timediff"];
}
if ($lbpf <> 0) {
$monitor_array[$key]["percent_per_activity"] = $monitor_array[$key]["tot_hours"] / $lbpf;
}
$monitor_array[$key]["lph_blended_per_activity"] = $monitor_array[$key]["percent_per_activity"] * (isset($lph_per_monitor_array[$key]) ? $lph_per_monitor_array[$key] : 0);
$monitor_array[0]["lph_blended_per_activity"] += $monitor_array[$key]["lph_blended_per_activity"];
}
}
if ($monitor_array[0]["lph_blended_per_activity"] <> 0) {
$perf_array["performance_activity"] = $perf_array["per_hour_actual"] / $monitor_array[0]["lph_blended_per_activity"];
}
}
#echo "<pre>";
#print_r($request);
#print_r($perf_array); print_r($monitor_array);
// return encoded json if input field named format is json
if ($request->format == "json") {
if (!array_key_exists("performance_activity", $perf_array)) {
$performance = 0;
} else {
$performance = number_format($perf_array["performance_activity"] * 100, 0);
}
if (!array_key_exists("0", $monitor_array)) {
$blendedTargetLPH = 0;
} else {
$blendedTargetLPH = number_format($monitor_array[0]["lph_blended_per_activity"], 1);
}
if (!array_key_exists("per_hour_actual", $perf_array)) {
$actualLPH = 0;
} else {
$actualLPH = number_format($perf_array["per_hour_actual"], 1);
}
if (!array_key_exists("tot_hours", $perf_array)) {
$grossHours = 0;
} else {
$grossHours = $perf_array["tot_hours"];
}
$json_data = array(
"day" => $starting,
"performance" => $performance,
"blendedTargetLPH" => $blendedTargetLPH,
"actualLPH" => $actualLPH,
"grossHours" => $grossHours
);
#echo "<pre>"; print_r($json_data);
return json_encode($json_data);
} else {
// display webpage of statistic version 4
$request->ports = Config::get('arrays.env_ports');
$no_data = "N";
if (count($statistic4) == 0 && $_SERVER['REQUEST_URI'] != "/userPickingStats4") {
$no_data = "Y";
}
#error_log("Time required to xyz: " . $duration . "\n", 3, "/temp/v4.log");
return view(
'liberty-theme.pages.userPickingStats4',
compact('statistic4', 'starting', 'user', 'user_array', 'monitor_array', 'totals_array', 'perf_array', 'request', 'no_data')
);
}
}
}