158 lines
6.8 KiB
PHP
158 lines
6.8 KiB
PHP
<?php
|
|
namespace App\DevExtreme;
|
|
class DataSourceLoader {
|
|
public static function Load($dbSet, $params, $module = null) {
|
|
$result = NULL;
|
|
if (isset($dbSet) && get_class($dbSet) == "App\DevExtreme\DbSet" && isset($params) && is_array($params)) {
|
|
// Check if distinctColumn parameter is provided (for header filter unique values)
|
|
// distinctColumn can come as string (from GET) or already parsed
|
|
$distinctColumn = null;
|
|
if (isset($params["distinctColumn"])) {
|
|
$distinctColumn = $params["distinctColumn"];
|
|
// If it's a JSON string, decode it
|
|
if (is_string($distinctColumn) && (substr($distinctColumn, 0, 1) === '[' || substr($distinctColumn, 0, 1) === '{')) {
|
|
$decoded = json_decode($distinctColumn, true);
|
|
if ($decoded !== null) {
|
|
$distinctColumn = $decoded;
|
|
}
|
|
}
|
|
// Log for debugging
|
|
error_log("DataSourceLoader: distinctColumn parameter received: " . var_export($distinctColumn, true) . " (type: " . gettype($distinctColumn) . ")");
|
|
}
|
|
|
|
if (!empty($distinctColumn) && is_string($distinctColumn)) {
|
|
|
|
// Apply filters first
|
|
$dbSet->Filter(Utils::GetItemValueOrDefault($params, "filter"));
|
|
|
|
if ($module === 'weldlog-employer-view' && $dbSet->getTableName() === 'weld_logs') {
|
|
self::ApplyEmployerViewFilters($dbSet);
|
|
}
|
|
|
|
// Get distinct values
|
|
error_log("DataSourceLoader: Getting distinct values for column: " . $distinctColumn . " in table: " . $dbSet->getTableName());
|
|
$distinctValues = $dbSet->GetDistinctValues($distinctColumn);
|
|
if ($dbSet->GetLastError() !== NULL) {
|
|
error_log("GetDistinctValues error: " . $dbSet->GetLastError());
|
|
// Return error response instead of NULL
|
|
$result = array();
|
|
$result["data"] = array();
|
|
$result["hasBlanks"] = false;
|
|
$result["error"] = $dbSet->GetLastError();
|
|
return $result;
|
|
}
|
|
error_log("DataSourceLoader: Found " . count($distinctValues) . " distinct values");
|
|
|
|
// Check for blanks
|
|
$hasBlanks = $dbSet->HasBlanks($distinctColumn);
|
|
if ($dbSet->GetLastError() !== NULL) {
|
|
error_log("HasBlanks error: " . $dbSet->GetLastError());
|
|
// Continue even if HasBlanks fails
|
|
}
|
|
|
|
// Format response for header filter
|
|
$result = array();
|
|
$result["data"] = array_map(function($value) {
|
|
return array("value" => $value, "text" => $value);
|
|
}, $distinctValues);
|
|
$result["hasBlanks"] = $hasBlanks;
|
|
|
|
return $result;
|
|
}
|
|
|
|
// Normal data loading
|
|
$dbSet->Select(Utils::GetItemValueOrDefault($params, "select"))
|
|
->Filter(Utils::GetItemValueOrDefault($params, "filter"));
|
|
|
|
if ($module === 'weldlog-employer-view' && $dbSet->getTableName() === 'weld_logs') {
|
|
self::ApplyEmployerViewFilters($dbSet);
|
|
}
|
|
$totalSummary = $dbSet->GetTotalSummary(Utils::GetItemValueOrDefault($params, "totalSummary"),
|
|
Utils::GetItemValueOrDefault($params, "filter"));
|
|
if ($dbSet->GetLastError() !== NULL) {
|
|
error_log("GetTotalSummary error: " . $dbSet->GetLastError());
|
|
return $result;
|
|
}
|
|
$totalCount = (isset($params["requireTotalCount"]) && $params["requireTotalCount"] === true)
|
|
? $dbSet->GetCount() : NULL;
|
|
if ($dbSet->GetLastError() !== NULL) {
|
|
return $result;
|
|
}
|
|
|
|
$dbSet->Sort(Utils::GetItemValueOrDefault($params, "sort"));
|
|
$groupCount = NULL;
|
|
$skip = Utils::GetItemValueOrDefault($params, "skip");
|
|
$take = Utils::GetItemValueOrDefault($params, "take");
|
|
if (isset($params["group"])) {
|
|
$groupExpression = $params["group"];
|
|
$groupSummary = Utils::GetItemValueOrDefault($params, "groupSummary");
|
|
$dbSet->Group($groupExpression, $groupSummary, $skip, $take);
|
|
if (isset($params["requireGroupCount"]) && $params["requireGroupCount"] === true) {
|
|
$groupCount = $dbSet->GetGroupCount();
|
|
}
|
|
}
|
|
else {
|
|
$dbSet->SkipTake($skip, $take);
|
|
}
|
|
$result = array();
|
|
$result["data"] = $dbSet->AsArray();
|
|
if ($dbSet->GetLastError() !== NULL) {
|
|
return $result;
|
|
}
|
|
if (isset($totalCount)) {
|
|
$result["totalCount"] = $totalCount;
|
|
}
|
|
if (isset($totalSummary)) {
|
|
$result["summary"] = $totalSummary;
|
|
}
|
|
if (isset($groupCount)) {
|
|
$result["groupCount"] = $groupCount;
|
|
}
|
|
}
|
|
else {
|
|
throw new \Exception("Invalid params");
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Apply employer view specific filters to weld_logs table
|
|
* Filters:
|
|
* 1. Exclude records with repair_status = 'Repair' in repair_logs
|
|
* 2. Exclude records with NDT results other than 'Accept / Годен' or empty/null
|
|
*/
|
|
private static function ApplyEmployerViewFilters($dbSet) {
|
|
$weldLogsTable = $dbSet->getTableName();
|
|
|
|
$repairFilter = "NOT EXISTS (
|
|
SELECT 1 FROM repair_logs
|
|
WHERE repair_logs.iso_number = {$weldLogsTable}.iso_number
|
|
AND repair_logs.new_joint_no = {$weldLogsTable}.no_of_the_joint_as_per_as_built_survey
|
|
AND repair_logs.repair_status = 'Repair'
|
|
)";
|
|
|
|
$ndtFields = [
|
|
'vt_result',
|
|
'rt_result',
|
|
'ut_result',
|
|
'pt_result',
|
|
'mt_result',
|
|
'pmi_result',
|
|
'ferrite_result',
|
|
'ht_result'
|
|
];
|
|
|
|
$ndtFilters = [];
|
|
foreach ($ndtFields as $field) {
|
|
$ndtFilters[] = "({$weldLogsTable}.{$field} IN ('Accept / Годен', '') OR {$weldLogsTable}.{$field} IS NULL)";
|
|
}
|
|
|
|
$ndtFilter = implode(' AND ', $ndtFilters);
|
|
|
|
$combinedFilter = "({$repairFilter}) AND ({$ndtFilter})";
|
|
|
|
$dbSet->AddCustomWhere($combinedFilter);
|
|
}
|
|
|
|
}
|