Skip to content

Commit ac4a9b8

Browse files
committed
Recurring tickets
- Allow forcing recurring tickets to run (e.g. cron broke, or you want to do a task early) - Use new permissions system for recurring UI - Bugfix: Ticket billable status wasn't saving/displaying on the edit modal
1 parent fae4f1e commit ac4a9b8

File tree

5 files changed

+178
-45
lines changed

5 files changed

+178
-45
lines changed

js/recurring_tickets_edit_modal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function populateRecurringTicketEditModal(client_id, ticket_id) {
1919
document.getElementById("editHeader").innerText = " Edit Recurring ticket: " + ticket.scheduled_ticket_subject;
2020
document.getElementById("editTicketId").value = ticket_id;
2121
document.getElementById("editClientId").value = client_id;
22-
document.getElementById("editTicketBillable").value = ticket.scheduled_ticket_billable;
22+
document.getElementById("editTicketBillable").checked = !!parseInt(ticket.scheduled_ticket_billable);
2323
document.getElementById("editTicketSubject").value = ticket.scheduled_ticket_subject;
2424
document.getElementById("editTicketNextRun").value = ticket.scheduled_ticket_next_run;
2525
tinyMCE.get('editTicketDetails').setContent(ticket.scheduled_ticket_details);

post/user/ticket.php

Lines changed: 141 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,15 @@
109109
$company_name = sanitizeInput($row['company_name']);
110110
$company_phone = sanitizeInput(formatPhoneNumber($row['company_phone']));
111111

112-
112+
113113
// EMAILING
114-
114+
115115
$subject = "Ticket created [$ticket_prefix$ticket_number] - $ticket_subject";
116116
$body = "<i style=\'color: #808080\'>##- Please type your reply above this line -##</i><br><br>Hello $contact_name,<br><br>A ticket regarding \"$ticket_subject\" has been created for you.<br><br>--------------------------------<br>$ticket_details--------------------------------<br><br>Ticket: $ticket_prefix$ticket_number<br>Subject: $ticket_subject<br>Status: Open<br>Portal: <a href=\'https://$config_base_url/guest_view_ticket.php?ticket_id=$ticket_id&url_key=$url_key\'>View ticket</a><br><br>--<br>$company_name - Support<br>$config_ticket_from_email<br>$company_phone";
117117

118118
// Verify contact email is valid
119119
if (filter_var($contact_email, FILTER_VALIDATE_EMAIL)) {
120120

121-
122121

123122
// Email Ticket Contact
124123
// Queue Mail
@@ -184,7 +183,7 @@
184183
$asset_id = intval($_POST['asset']);
185184
$location_id = intval($_POST['location']);
186185
$project_id = intval($_POST['project']);
187-
186+
188187
mysqli_query($mysqli, "UPDATE tickets SET ticket_category = $category_id, ticket_subject = '$ticket_subject', ticket_priority = '$ticket_priority', ticket_billable = $billable, ticket_details = '$details', ticket_vendor_ticket_number = '$vendor_ticket_number', ticket_contact_id = $contact_id, ticket_vendor_id = $vendor_id, ticket_location_id = $location_id, ticket_asset_id = $asset_id, ticket_project_id = $project_id WHERE ticket_id = $ticket_id");
189188

190189
// Get contact/ticket details after update for logging / email purposes
@@ -208,7 +207,7 @@
208207
$client_id = intval($row['ticket_client_id']);
209208

210209
// Notify new contact if selected
211-
if ($notify && !empty($config_smtp_host)) {
210+
if ($notify && !empty($config_smtp_host)) {
212211

213212
// Get Company Name Phone Number and Sanitize for Email Sending
214213
$sql = mysqli_query($mysqli, "SELECT company_name, company_phone FROM companies WHERE company_id = 1");
@@ -466,7 +465,7 @@
466465
$watcher_email = sanitizeInput($row['watcher_email']);
467466
$client_id = intval($row['ticket_client_id']);
468467
$ticket_id = intval($row['ticket_id']);
469-
468+
470469
mysqli_query($mysqli, "DELETE FROM ticket_watchers WHERE watcher_id = $watcher_id");
471470

472471
// History
@@ -795,7 +794,7 @@
795794

796795
// Assign Tech to Selected Tickets
797796
if (isset($_POST['ticket_ids'])) {
798-
797+
799798
// Get a Ticket Count
800799
$ticket_count = count($_POST['ticket_ids']);
801800

@@ -884,7 +883,7 @@
884883

885884
}
886885
} // End For Each Ticket ID Loop
887-
886+
888887
mysqli_query($mysqli, "UPDATE tickets SET ticket_updated_at = NOW() WHERE ticket_id = $merge_into_ticket_id");
889888

890889
$_SESSION['alert_message'] = "<strong>$ticket_count</strong> tickets merged into <strong>$ticket_prefix$merge_into_ticket_number</strong>";
@@ -932,7 +931,7 @@
932931
mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 4, ticket_resolved_at = NOW() WHERE ticket_id = $ticket_id");
933932

934933
mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = '$details', ticket_reply_type = '$ticket_reply_type', ticket_reply_time_worked = '$ticket_reply_time_worked', ticket_reply_by = $session_user_id, ticket_reply_ticket_id = $ticket_id");
935-
934+
936935
// Logging
937936
logAction("Ticket", "Resolve", "$session_name resolved $ticket_prefix$ticket_number - $ticket_subject", $client_id, $ticket_id);
938937

@@ -962,7 +961,7 @@
962961
$company_name = sanitizeInput($row['company_name']);
963962
$company_phone = sanitizeInput(formatPhoneNumber($row['company_phone']));
964963

965-
964+
966965
// EMAIL
967966
$subject = "Ticket resolved - [$ticket_prefix$ticket_number] - $ticket_subject | (pending closure)";
968967
$body = "<i style=\'color: #808080\'>##- Please type your reply above this line -##</i><br><br>Hello $contact_name,<br><br>Your ticket regarding \"$ticket_subject\" has been marked as solved and is pending closure.<br><br>$details<br><br> If your request/issue is resolved, you can simply ignore this email. If you need further assistance, please reply or <a href=\'https://$config_base_url/guest_view_ticket.php?ticket_id=$ticket_id&url_key=$url_key\'>re-open</a> to let us know! <br><br>Ticket: $ticket_prefix$ticket_number<br>Subject: $ticket_subject<br>Portal: https://$base_url/portal/ticket.php?id=$ticket_id<br><br>--<br>$company_name - Support<br>$config_ticket_from_email<br>$company_phone";
@@ -972,7 +971,7 @@
972971

973972
$data = [];
974973

975-
974+
976975

977976
// Email Ticket Contact
978977
// Queue Mail
@@ -1071,7 +1070,7 @@
10711070

10721071
// Logging
10731072
logAction("Ticket", "Resolved", "$session_name resolved Ticket $ticket_prefix$ticket_number", $client_id, $ticket_id);
1074-
1073+
10751074
customAction('ticket_resolve', $ticket_id);
10761075
}
10771076

@@ -1203,7 +1202,7 @@
12031202

12041203

12051204
} // End For Each Ticket ID Loop
1206-
1205+
12071206
$_SESSION['alert_message'] = "<strong>$ticket_count</strong> Tickets added to Project <strong>$project_name</strong>";
12081207

12091208
}
@@ -1916,7 +1915,7 @@
19161915

19171916
// Add internal note to ticket, and link to invoice in database
19181917
mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = 'Created invoice <a href=\"invoice.php?invoice_id=$invoice_id\">$config_invoice_prefix$invoice_number</a> for this ticket.', ticket_reply_type = 'Internal', ticket_reply_time_worked = '00:01:00', ticket_reply_by = $session_user_id, ticket_reply_ticket_id = $ticket_id");
1919-
1918+
19201919
mysqli_query($mysqli, "UPDATE tickets SET ticket_invoice_id = $invoice_id WHERE ticket_id = $ticket_id");
19211920

19221921
// Logging
@@ -2031,9 +2030,137 @@
20312030
header("Location: " . $_SERVER["HTTP_REFERER"]);
20322031
}
20332032

2033+
if (isset($_GET['force_recurring_ticket'])) {
2034+
2035+
enforceUserPermission('module_support', 2);
2036+
validateCSRFToken($_GET['csrf_token']);
2037+
2038+
$scheduled_ticket_id = intval($_GET['force_recurring_ticket']);
2039+
2040+
$sql = mysqli_query($mysqli, "SELECT * FROM scheduled_tickets WHERE scheduled_ticket_id = $scheduled_ticket_id");
2041+
2042+
if (mysqli_num_rows($sql) > 0) {
2043+
$row = mysqli_fetch_array($sql);
2044+
$schedule_id = intval($row['scheduled_ticket_id']);
2045+
$subject = sanitizeInput($row['scheduled_ticket_subject']);
2046+
$details = mysqli_real_escape_string($mysqli, $row['scheduled_ticket_details']);
2047+
$priority = sanitizeInput($row['scheduled_ticket_priority']);
2048+
$frequency = sanitizeInput(strtolower($row['scheduled_ticket_frequency']));
2049+
$billable = intval($row['scheduled_ticket_billable']);
2050+
$old_next_scheduled_date = sanitizeInput($row['scheduled_ticket_next_run']);
2051+
$created_id = intval($row['scheduled_ticket_created_by']);
2052+
$assigned_id = intval($row['scheduled_ticket_assigned_to']);
2053+
$contact_id = intval($row['scheduled_ticket_contact_id']);
2054+
$client_id = intval($row['scheduled_ticket_client_id']);
2055+
$asset_id = intval($row['scheduled_ticket_asset_id']);
2056+
$url_key = randomString(156);
2057+
2058+
$ticket_status = 1; // Default
2059+
if ($assigned_id > 0) {
2060+
$ticket_status = 2; // Set to open if we've auto-assigned an agent
2061+
}
2062+
2063+
// Sanitize Config Vars from get_settings.php and Session Vars from check_login.php
2064+
$config_ticket_prefix = sanitizeInput($config_ticket_prefix);
2065+
$config_ticket_from_name = sanitizeInput($config_ticket_from_name);
2066+
$config_ticket_from_email = sanitizeInput($config_ticket_from_email);
2067+
$config_base_url = sanitizeInput($config_base_url);
2068+
2069+
// Assign this new ticket the next ticket number & increment config_ticket_next_number by 1 (for the next ticket)
2070+
$ticket_number = $config_ticket_next_number;
2071+
$new_config_ticket_next_number = $config_ticket_next_number + 1;
2072+
mysqli_query($mysqli, "UPDATE settings SET config_ticket_next_number = $new_config_ticket_next_number WHERE company_id = 1");
2073+
2074+
// Raise the ticket
2075+
mysqli_query($mysqli, "INSERT INTO tickets SET ticket_prefix = '$config_ticket_prefix', ticket_number = $ticket_number, ticket_subject = '$subject', ticket_details = '$details', ticket_priority = '$priority', ticket_status = '$ticket_status', ticket_billable = $billable, ticket_url_key = '$url_key', ticket_created_by = $created_id, ticket_assigned_to = $assigned_id, ticket_contact_id = $contact_id, ticket_client_id = $client_id, ticket_asset_id = $asset_id");
2076+
$id = mysqli_insert_id($mysqli);
2077+
2078+
// Notifications
2079+
2080+
customAction('ticket_create', $id);
2081+
2082+
// Get client/contact/ticket details
2083+
$sql = mysqli_query(
2084+
$mysqli,
2085+
"SELECT client_name, contact_name, contact_email, ticket_prefix, ticket_number, ticket_priority, ticket_subject, ticket_details FROM tickets
2086+
LEFT JOIN clients ON ticket_client_id = client_id
2087+
LEFT JOIN contacts ON ticket_contact_id = contact_id
2088+
WHERE ticket_id = $id"
2089+
);
2090+
$row = mysqli_fetch_array($sql);
2091+
2092+
$contact_name = sanitizeInput($row['contact_name']);
2093+
$contact_email = sanitizeInput($row['contact_email']);
2094+
$client_name = sanitizeInput($row['client_name']);
2095+
$contact_name = sanitizeInput($row['contact_name']);
2096+
$contact_email = sanitizeInput($row['contact_email']);
2097+
$ticket_prefix = sanitizeInput($row['ticket_prefix']);
2098+
$ticket_number = intval($row['ticket_number']);
2099+
$ticket_priority = sanitizeInput($row['ticket_priority']);
2100+
$ticket_subject = sanitizeInput($row['ticket_subject']);
2101+
$ticket_details = mysqli_real_escape_string($mysqli, $row['ticket_details']);
2102+
2103+
$data = [];
2104+
2105+
// Notify client by email their ticket has been raised, if general notifications are turned on & there is a valid contact email
2106+
if (!empty($config_smtp_host) && $config_ticket_client_general_notifications == 1 && filter_var($contact_email, FILTER_VALIDATE_EMAIL)) {
2107+
2108+
$email_subject = "Ticket created - [$ticket_prefix$ticket_number] - $ticket_subject (scheduled)";
2109+
$email_body = "<i style=\'color: #808080\'>##- Please type your reply above this line -##</i><br><br>Hello $contact_name,<br><br>A ticket regarding \"$ticket_subject\" has been automatically created for you.<br><br>--------------------------------<br>$ticket_details--------------------------------<br><br>Ticket: $ticket_prefix$ticket_number<br>Subject: $ticket_subject<br>Status: Open<br>Portal: https://$config_base_url/portal/ticket.php?id=$id<br><br>--<br>$company_name - Support<br>$config_ticket_from_email<br>$company_phone";
2110+
2111+
$email = [
2112+
'from' => $config_ticket_from_email,
2113+
'from_name' => $config_ticket_from_name,
2114+
'recipient' => $contact_email,
2115+
'recipient_name' => $contact_name,
2116+
'subject' => $email_subject,
2117+
'body' => $email_body
2118+
];
2119+
2120+
$data[] = $email;
2121+
2122+
}
2123+
2124+
// Add to the mail queue
2125+
addToMailQueue($mysqli, $data);
2126+
2127+
// Set the next run date (based on the scheduled date, rather than now, so things keep their schedule)
2128+
$dt_old_next_scheduled_date = new DateTime($old_next_scheduled_date);
2129+
if ($frequency == "weekly") {
2130+
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('1 week'));
2131+
} elseif ($frequency == "monthly") {
2132+
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('1 month'));
2133+
} elseif ($frequency == "quarterly") {
2134+
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('3 months'));
2135+
} elseif ($frequency == "biannually") {
2136+
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('6 months'));
2137+
} elseif ($frequency == "annually") {
2138+
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('12 months'));
2139+
}
2140+
2141+
// Update the run date
2142+
$next_run = $next_run->format('Y-m-d');
2143+
mysqli_query($mysqli, "UPDATE scheduled_tickets SET scheduled_ticket_next_run = '$next_run' WHERE scheduled_ticket_id = $schedule_id");
2144+
2145+
// Logging
2146+
logAction("Ticket", "Create", "$session_name force created recurring scheduled $frequency ticket - $config_ticket_prefix$ticket_number - $subject", $client_id, $id);
2147+
2148+
$_SESSION['alert_message'] = "Recurring Ticket Forced";
2149+
2150+
header("Location: " . $_SERVER["HTTP_REFERER"]);
2151+
2152+
} else {
2153+
$_SESSION['alert_type'] = "error";
2154+
$_SESSION['alert_message'] = "Recurring Ticket Force failed";
2155+
header("Location: " . $_SERVER["HTTP_REFERER"]);
2156+
}
2157+
2158+
}
2159+
20342160
if (isset($_GET['delete_recurring_ticket'])) {
20352161

20362162
enforceUserPermission('module_support', 3);
2163+
validateCSRFToken($_GET['csrf_token']);
20372164

20382165
$scheduled_ticket_id = intval($_GET['delete_recurring_ticket']);
20392166

post/user/ticket_recurring_model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
$priority = sanitizeInput($_POST['priority']);
66
$details = mysqli_real_escape_string($mysqli, $_POST['details']);
77
$frequency = sanitizeInput($_POST['frequency']);
8-
$billable = intval($_POST['billable']);
8+
$billable = intval($_POST['billable'] ?? 0);
99

1010
$asset_id = "0";
1111
if (isset($_POST['asset'])) {

recurring_ticket_edit_modal.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<form action="post.php" method="post" autocomplete="off">
1111
<input type="hidden" name="scheduled_ticket_id" id="editTicketId">
1212
<input type="hidden" name="client" id="editClientId">
13-
<input type="hidden" name="billable" value="0">
1413

1514
<div class="modal-body bg-white">
1615

@@ -74,14 +73,12 @@
7473
</div>
7574
</div>
7675

77-
<?php //if ($config_module_enable_accounting) { ?>
7876
<div class="form-group" <?php if (!$config_module_enable_accounting) { echo 'style="display:none"'; } ?>>
7977
<div class="custom-control custom-switch">
8078
<input type="checkbox" class="custom-control-input" name="billable" id="editTicketBillable" value="1">
8179
<label class="custom-control-label" for="editTicketBillable">Mark Billable</label>
8280
</div>
8381
</div>
84-
<?php //} ?>
8582

8683
</div>
8784

0 commit comments

Comments
 (0)