overhauled workorder module
This commit is contained in:
@@ -20,7 +20,9 @@ class WorkorderBaseController extends TTCrud
|
||||
['value' => 'problem_solved', 'text' => 'Problem gelöst', 'icon' => 'fas fa-check-circle text-success'],
|
||||
['value' => 'documented', 'text' => 'Dokumentiert', 'icon' => 'fas fa-file-alt text-success'],
|
||||
['value' => 'completed', 'text' => 'Abgeschlossen', 'icon' => 'fas fa-check-double text-secondary'],
|
||||
['value' => 'charged', 'text' => 'Verrechnet', 'icon' => 'fas fa-euro-sign text-purple'],
|
||||
['value' => 'cancelled', 'text' => 'Abgebrochen', 'icon' => 'fas fa-ban text-danger'],
|
||||
['value' => 'archived', 'text' => 'Archiviert', 'icon' => 'fas fa-archive text-muted'],
|
||||
]]
|
||||
];
|
||||
|
||||
@@ -50,6 +52,7 @@ class WorkorderBaseController extends TTCrud
|
||||
$journals = WorkorderJournalModel::getAll(['workorderId' => intval($this->request->workorderId)], null, 0, ['key' => 'create', 'order' => 'DESC']);
|
||||
|
||||
$tenantConfig = $this->getTenantConfigFromWorkorder((int)$this->request->workorderId);
|
||||
$translationMap = [];
|
||||
if ($tenantConfig && !empty($tenantConfig->documentationTypes)) {
|
||||
$customTypes = json_decode($tenantConfig->documentationTypes, true);
|
||||
$customMap = array_column($customTypes, 'text', 'value');
|
||||
@@ -136,4 +139,107 @@ class WorkorderBaseController extends TTCrud
|
||||
|
||||
return WorkorderTenantConfigModel::getFirst(['addressId' => $network->owner_id]) ?? null;
|
||||
}
|
||||
|
||||
//region BACKGROUND TASKS
|
||||
/**
|
||||
* Creates new workorders from preorders based on tenant configurations.
|
||||
* Runs at most once every 5 minutes to avoid performance issues.
|
||||
*/
|
||||
protected function createWorkordersFromPreorders()
|
||||
{
|
||||
$lockFile = TEMP_DIR . "/task_create_workorders.lock";
|
||||
if (file_exists($lockFile) && (time() - intval(file_get_contents($lockFile))) < 300) {
|
||||
return; // Run only every 5 minutes
|
||||
}
|
||||
|
||||
$configs = WorkorderTenantConfigModel::getAll();
|
||||
foreach ($configs as $config) {
|
||||
$filters = json_decode($config->workorderCreationFilters, true);
|
||||
if (empty($filters)) continue;
|
||||
|
||||
$networks = NetworkModel::search(['owner_id' => $config->addressId]);
|
||||
if (empty($networks)) continue;
|
||||
|
||||
$tenantCampaigns = array_map(fn($n) => $n->id, PreordercampaignModel::getAll(['network_id' => array_map(fn($n) => $n->id, $networks)]));
|
||||
if (empty($tenantCampaigns)) continue;
|
||||
|
||||
$filters['preordercampaign_id'] = $tenantCampaigns;
|
||||
$newPreorders = PreorderModel::searchActive($filters);
|
||||
|
||||
foreach ($newPreorders as $preorder) {
|
||||
if (!WorkorderModel::getFirst(['preorderId' => $preorder->id])) {
|
||||
WorkorderModel::create([
|
||||
'preorderId' => $preorder->id, 'clusterId' => $preorder->preordercampaign_id,
|
||||
'status' => 'new', 'create' => time(), 'createBy' => 0 // System User
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
file_put_contents($lockFile, time());
|
||||
}
|
||||
|
||||
/**
|
||||
* Archives workorders that are no longer considered active based on tenant configurations.
|
||||
* Runs at most once every 5 minutes to avoid performance issues.
|
||||
*/
|
||||
protected function archiveWorkorders()
|
||||
{
|
||||
$lockFile = TEMP_DIR . "/task_archive_workorders.lock";
|
||||
if (file_exists($lockFile) && (time() - intval(file_get_contents($lockFile))) < 300) {
|
||||
return; // Run only every 5 minutes
|
||||
}
|
||||
|
||||
$configs = WorkorderTenantConfigModel::getAll();
|
||||
foreach ($configs as $config) {
|
||||
$activeFilters = json_decode($config->workorderActiveFilters, true);
|
||||
if (empty($activeFilters)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$networks = NetworkModel::search(['owner_id' => $config->addressId]);
|
||||
if (empty($networks)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tenantCampaignIds = array_column(PreordercampaignModel::getAll(['network_id' => array_column($networks, 'id')]), 'id');
|
||||
if (empty($tenantCampaignIds)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$activeFilters['preordercampaign_id'] = $tenantCampaignIds;
|
||||
|
||||
$activePreorderIds = array_column(PreorderModel::searchActive($activeFilters), 'id');
|
||||
$activePreorderIdsSet = array_flip($activePreorderIds);
|
||||
|
||||
$statusesToCheck = ['new', 'assigned', 'scheduled', 'correction_requested', 'intervention_required', 'civil_engineering_required', 'civil_engineering_completed', 'problem_solved'];
|
||||
|
||||
$allTenantPreorders = PreorderModel::getAll(['preordercampaign_id' => $tenantCampaignIds]);
|
||||
if(empty($allTenantPreorders)) continue;
|
||||
|
||||
$allTenantPreorderIds = array_column($allTenantPreorders, 'id');
|
||||
|
||||
$workordersToCheck = WorkorderModel::getAll([
|
||||
'status' => $statusesToCheck,
|
||||
'preorderId' => $allTenantPreorderIds
|
||||
]);
|
||||
|
||||
foreach ($workordersToCheck as $workorder) {
|
||||
if (!isset($activePreorderIdsSet[$workorder->preorderId])) {
|
||||
$oldStatus = $workorder->status;
|
||||
$workorder->status = 'archived';
|
||||
WorkorderModel::update((array)$workorder);
|
||||
|
||||
WorkorderJournalModel::create([
|
||||
'workorderId' => $workorder->id,
|
||||
'text' => 'Arbeitsauftrag wurde automatisch archiviert, da die zugehörige Vorbestellung nicht mehr den Aktiv-Kriterien entspricht.',
|
||||
'statusChange' => $this->getStatusText($oldStatus) . " -> " . $this->getStatusText('archived'),
|
||||
'create' => time(),
|
||||
'createBy' => 1, // System user
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
file_put_contents($lockFile, time());
|
||||
}
|
||||
//endregion
|
||||
}
|
||||
Reference in New Issue
Block a user