diff --git a/api.php b/api.php index 1937dc6..c90934d 100644 --- a/api.php +++ b/api.php @@ -3242,61 +3242,34 @@ switch ($action) { } unset($tc); - // Ajastinsääntöjen tarkistus (delay_days + no_activity) - $timedRules = array_filter(dbLoadTicketRules($comp['id']), function($r) { - return $r['enabled'] && $r['delay_days'] > 0 && $r['delay_condition'] === 'no_activity'; - }); - if (!empty($timedRules)) { - foreach ($tickets as &$tc) { - if (in_array($tc['status'], ['suljettu', 'ratkaistu'])) continue; - $lastActivity = $tc['updated'] ?? $tc['created']; - $inactiveDays = (strtotime($now) - strtotime($lastActivity)) / 86400; - foreach ($timedRules as $rule) { - if ($inactiveDays < $rule['delay_days']) continue; - // Tarkista ehdot (from/to/subject) - $match = true; - if (!empty($rule['from_contains'])) { - if (stripos(($tc['from_email'] ?? '') . ' ' . ($tc['from_name'] ?? ''), $rule['from_contains']) === false) $match = false; - } - if (!empty($rule['subject_contains'])) { - if (stripos($tc['subject'] ?? '', $rule['subject_contains']) === false) $match = false; - } - if (!empty($rule['to_contains'])) { - if (stripos($tc['to_email'] ?? '', $rule['to_contains']) === false) $match = false; - } - if (!$match) continue; - // Sovella toimenpiteet - $changed = false; - if (!empty($rule['set_priority']) && ($tc['priority'] ?? '') !== $rule['set_priority']) { - $tc['priority'] = $rule['set_priority']; - $changed = true; - } - if (!empty($rule['status_set']) && ($tc['status'] ?? '') !== $rule['status_set']) { - $tc['status'] = $rule['status_set']; - $changed = true; - } - if (!empty($rule['type_set']) && ($tc['type'] ?? '') !== $rule['type_set']) { - $tc['type'] = $rule['type_set']; - $changed = true; - } - if (!empty($rule['set_tags'])) { - $ruleTags = array_map('trim', explode(',', $rule['set_tags'])); - $existingTags = $tc['tags'] ?? []; - $merged = array_values(array_unique(array_merge($existingTags, $ruleTags))); - if ($merged !== $existingTags) { - $tc['tags'] = $merged; - $changed = true; - } - } - if ($changed) { - $tc['updated'] = $now; - dbSaveTicket($comp['id'], $tc); - } - break; // Vain yksi ajastinsääntö per tiketti + // Ajastettujen sääntöjen suoritus (delay_execute_at saavutettu) + foreach ($tickets as &$tc) { + if (empty($tc['delay_rule_id']) || empty($tc['delay_execute_at'])) continue; + if ($tc['delay_execute_at'] > $now) continue; + if (in_array($tc['status'], ['suljettu', 'ratkaistu'])) { + // Suljettu/ratkaistu ennen ajastinta → peruuta + unset($tc['delay_rule_id'], $tc['delay_execute_at']); + dbSaveTicket($comp['id'], $tc); + continue; + } + // Hae sääntö ja suorita toimenpiteet + $allRules = dbLoadTicketRules($comp['id']); + $rule = null; + foreach ($allRules as $r) { if ($r['id'] === $tc['delay_rule_id']) { $rule = $r; break; } } + if ($rule) { + if (!empty($rule['status_set'])) $tc['status'] = $rule['status_set']; + if (!empty($rule['type_set'])) $tc['type'] = $rule['type_set']; + if (!empty($rule['set_priority'])) $tc['priority'] = $rule['set_priority']; + if (!empty($rule['set_tags'])) { + $ruleTags = array_map('trim', explode(',', $rule['set_tags'])); + $tc['tags'] = array_values(array_unique(array_merge($tc['tags'] ?? [], $ruleTags))); } } - unset($tc); + unset($tc['delay_rule_id'], $tc['delay_execute_at']); + $tc['updated'] = $now; + dbSaveTicket($comp['id'], $tc); } + unset($tc); // Resolve mailbox names for this company $mailboxes = dbLoadMailboxes($comp['id']); @@ -3571,12 +3544,21 @@ switch ($action) { } } if ($match) { - if (!empty($rule['status_set'])) $ticket['status'] = $rule['status_set']; - if (!empty($rule['type_set'])) $ticket['type'] = $rule['type_set']; - if (!empty($rule['set_priority'])) $ticket['priority'] = $rule['set_priority']; - if (!empty($rule['set_tags'])) { - $ruleTags = array_map('trim', explode(',', $rule['set_tags'])); - $ticket['tags'] = array_values(array_unique(array_merge($ticket['tags'], $ruleTags))); + $delayDays = intval($rule['delay_days'] ?? 0); + if ($delayDays > 0) { + // Ajastettu sääntö: älä suorita heti, tallenna ajastus tikettiin + $executeAt = date('Y-m-d', strtotime("+{$delayDays} days")) . ' 10:00:00'; + $ticket['delay_rule_id'] = $rule['id']; + $ticket['delay_execute_at'] = $executeAt; + } else { + // Välitön sääntö: suorita heti + if (!empty($rule['status_set'])) $ticket['status'] = $rule['status_set']; + if (!empty($rule['type_set'])) $ticket['type'] = $rule['type_set']; + if (!empty($rule['set_priority'])) $ticket['priority'] = $rule['set_priority']; + if (!empty($rule['set_tags'])) { + $ruleTags = array_map('trim', explode(',', $rule['set_tags'])); + $ticket['tags'] = array_values(array_unique(array_merge($ticket['tags'], $ruleTags))); + } } if (!empty($rule['auto_close_days'])) { $days = intval($rule['auto_close_days']); @@ -4024,7 +4006,6 @@ switch ($action) { 'set_tags' => trim($input['set_tags'] ?? ''), 'auto_close_days' => intval($input['auto_close_days'] ?? 0), 'delay_days' => intval($input['delay_days'] ?? 0), - 'delay_condition' => trim($input['delay_condition'] ?? ''), 'enabled' => $input['enabled'] ?? true, ]; diff --git a/index.html b/index.html index e1930cb..09ae15e 100644 --- a/index.html +++ b/index.html @@ -1293,18 +1293,17 @@
Kun ajastin on päällä, toimenpiteet suoritetaan vasta X päivän päästä klo 10:00