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
This commit is contained in:
Marcus Hill 2024-12-16 12:11:29 +00:00
parent fae4f1e59d
commit ac4a9b8ef2
5 changed files with 179 additions and 46 deletions

View File

@ -19,7 +19,7 @@ function populateRecurringTicketEditModal(client_id, ticket_id) {
document.getElementById("editHeader").innerText = " Edit Recurring ticket: " + ticket.scheduled_ticket_subject; document.getElementById("editHeader").innerText = " Edit Recurring ticket: " + ticket.scheduled_ticket_subject;
document.getElementById("editTicketId").value = ticket_id; document.getElementById("editTicketId").value = ticket_id;
document.getElementById("editClientId").value = client_id; document.getElementById("editClientId").value = client_id;
document.getElementById("editTicketBillable").value = ticket.scheduled_ticket_billable; document.getElementById("editTicketBillable").checked = !!parseInt(ticket.scheduled_ticket_billable);
document.getElementById("editTicketSubject").value = ticket.scheduled_ticket_subject; document.getElementById("editTicketSubject").value = ticket.scheduled_ticket_subject;
document.getElementById("editTicketNextRun").value = ticket.scheduled_ticket_next_run; document.getElementById("editTicketNextRun").value = ticket.scheduled_ticket_next_run;
tinyMCE.get('editTicketDetails').setContent(ticket.scheduled_ticket_details); tinyMCE.get('editTicketDetails').setContent(ticket.scheduled_ticket_details);

View File

@ -119,7 +119,6 @@ if (isset($_POST['add_ticket'])) {
if (filter_var($contact_email, FILTER_VALIDATE_EMAIL)) { if (filter_var($contact_email, FILTER_VALIDATE_EMAIL)) {
// Email Ticket Contact // Email Ticket Contact
// Queue Mail // Queue Mail
$data = []; $data = [];
@ -2031,9 +2030,137 @@ if (isset($_POST['edit_recurring_ticket'])) {
header("Location: " . $_SERVER["HTTP_REFERER"]); header("Location: " . $_SERVER["HTTP_REFERER"]);
} }
if (isset($_GET['force_recurring_ticket'])) {
enforceUserPermission('module_support', 2);
validateCSRFToken($_GET['csrf_token']);
$scheduled_ticket_id = intval($_GET['force_recurring_ticket']);
$sql = mysqli_query($mysqli, "SELECT * FROM scheduled_tickets WHERE scheduled_ticket_id = $scheduled_ticket_id");
if (mysqli_num_rows($sql) > 0) {
$row = mysqli_fetch_array($sql);
$schedule_id = intval($row['scheduled_ticket_id']);
$subject = sanitizeInput($row['scheduled_ticket_subject']);
$details = mysqli_real_escape_string($mysqli, $row['scheduled_ticket_details']);
$priority = sanitizeInput($row['scheduled_ticket_priority']);
$frequency = sanitizeInput(strtolower($row['scheduled_ticket_frequency']));
$billable = intval($row['scheduled_ticket_billable']);
$old_next_scheduled_date = sanitizeInput($row['scheduled_ticket_next_run']);
$created_id = intval($row['scheduled_ticket_created_by']);
$assigned_id = intval($row['scheduled_ticket_assigned_to']);
$contact_id = intval($row['scheduled_ticket_contact_id']);
$client_id = intval($row['scheduled_ticket_client_id']);
$asset_id = intval($row['scheduled_ticket_asset_id']);
$url_key = randomString(156);
$ticket_status = 1; // Default
if ($assigned_id > 0) {
$ticket_status = 2; // Set to open if we've auto-assigned an agent
}
// Sanitize Config Vars from get_settings.php and Session Vars from check_login.php
$config_ticket_prefix = sanitizeInput($config_ticket_prefix);
$config_ticket_from_name = sanitizeInput($config_ticket_from_name);
$config_ticket_from_email = sanitizeInput($config_ticket_from_email);
$config_base_url = sanitizeInput($config_base_url);
// Assign this new ticket the next ticket number & increment config_ticket_next_number by 1 (for the next ticket)
$ticket_number = $config_ticket_next_number;
$new_config_ticket_next_number = $config_ticket_next_number + 1;
mysqli_query($mysqli, "UPDATE settings SET config_ticket_next_number = $new_config_ticket_next_number WHERE company_id = 1");
// Raise the ticket
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");
$id = mysqli_insert_id($mysqli);
// Notifications
customAction('ticket_create', $id);
// Get client/contact/ticket details
$sql = mysqli_query(
$mysqli,
"SELECT client_name, contact_name, contact_email, ticket_prefix, ticket_number, ticket_priority, ticket_subject, ticket_details FROM tickets
LEFT JOIN clients ON ticket_client_id = client_id
LEFT JOIN contacts ON ticket_contact_id = contact_id
WHERE ticket_id = $id"
);
$row = mysqli_fetch_array($sql);
$contact_name = sanitizeInput($row['contact_name']);
$contact_email = sanitizeInput($row['contact_email']);
$client_name = sanitizeInput($row['client_name']);
$contact_name = sanitizeInput($row['contact_name']);
$contact_email = sanitizeInput($row['contact_email']);
$ticket_prefix = sanitizeInput($row['ticket_prefix']);
$ticket_number = intval($row['ticket_number']);
$ticket_priority = sanitizeInput($row['ticket_priority']);
$ticket_subject = sanitizeInput($row['ticket_subject']);
$ticket_details = mysqli_real_escape_string($mysqli, $row['ticket_details']);
$data = [];
// Notify client by email their ticket has been raised, if general notifications are turned on & there is a valid contact email
if (!empty($config_smtp_host) && $config_ticket_client_general_notifications == 1 && filter_var($contact_email, FILTER_VALIDATE_EMAIL)) {
$email_subject = "Ticket created - [$ticket_prefix$ticket_number] - $ticket_subject (scheduled)";
$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";
$email = [
'from' => $config_ticket_from_email,
'from_name' => $config_ticket_from_name,
'recipient' => $contact_email,
'recipient_name' => $contact_name,
'subject' => $email_subject,
'body' => $email_body
];
$data[] = $email;
}
// Add to the mail queue
addToMailQueue($mysqli, $data);
// Set the next run date (based on the scheduled date, rather than now, so things keep their schedule)
$dt_old_next_scheduled_date = new DateTime($old_next_scheduled_date);
if ($frequency == "weekly") {
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('1 week'));
} elseif ($frequency == "monthly") {
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('1 month'));
} elseif ($frequency == "quarterly") {
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('3 months'));
} elseif ($frequency == "biannually") {
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('6 months'));
} elseif ($frequency == "annually") {
$next_run = date_add($dt_old_next_scheduled_date, date_interval_create_from_date_string('12 months'));
}
// Update the run date
$next_run = $next_run->format('Y-m-d');
mysqli_query($mysqli, "UPDATE scheduled_tickets SET scheduled_ticket_next_run = '$next_run' WHERE scheduled_ticket_id = $schedule_id");
// Logging
logAction("Ticket", "Create", "$session_name force created recurring scheduled $frequency ticket - $config_ticket_prefix$ticket_number - $subject", $client_id, $id);
$_SESSION['alert_message'] = "Recurring Ticket Forced";
header("Location: " . $_SERVER["HTTP_REFERER"]);
} else {
$_SESSION['alert_type'] = "error";
$_SESSION['alert_message'] = "Recurring Ticket Force failed";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
}
if (isset($_GET['delete_recurring_ticket'])) { if (isset($_GET['delete_recurring_ticket'])) {
enforceUserPermission('module_support', 3); enforceUserPermission('module_support', 3);
validateCSRFToken($_GET['csrf_token']);
$scheduled_ticket_id = intval($_GET['delete_recurring_ticket']); $scheduled_ticket_id = intval($_GET['delete_recurring_ticket']);

View File

@ -5,7 +5,7 @@ $subject = sanitizeInput($_POST['subject']);
$priority = sanitizeInput($_POST['priority']); $priority = sanitizeInput($_POST['priority']);
$details = mysqli_real_escape_string($mysqli, $_POST['details']); $details = mysqli_real_escape_string($mysqli, $_POST['details']);
$frequency = sanitizeInput($_POST['frequency']); $frequency = sanitizeInput($_POST['frequency']);
$billable = intval($_POST['billable']); $billable = intval($_POST['billable'] ?? 0);
$asset_id = "0"; $asset_id = "0";
if (isset($_POST['asset'])) { if (isset($_POST['asset'])) {

View File

@ -10,7 +10,6 @@
<form action="post.php" method="post" autocomplete="off"> <form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="scheduled_ticket_id" id="editTicketId"> <input type="hidden" name="scheduled_ticket_id" id="editTicketId">
<input type="hidden" name="client" id="editClientId"> <input type="hidden" name="client" id="editClientId">
<input type="hidden" name="billable" value="0">
<div class="modal-body bg-white"> <div class="modal-body bg-white">
@ -74,14 +73,12 @@
</div> </div>
</div> </div>
<?php //if ($config_module_enable_accounting) { ?>
<div class="form-group" <?php if (!$config_module_enable_accounting) { echo 'style="display:none"'; } ?>> <div class="form-group" <?php if (!$config_module_enable_accounting) { echo 'style="display:none"'; } ?>>
<div class="custom-control custom-switch"> <div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" name="billable" id="editTicketBillable" value="1"> <input type="checkbox" class="custom-control-input" name="billable" id="editTicketBillable" value="1">
<label class="custom-control-label" for="editTicketBillable">Mark Billable</label> <label class="custom-control-label" for="editTicketBillable">Mark Billable</label>
</div> </div>
</div> </div>
<?php //} ?>
</div> </div>

View File

@ -59,16 +59,18 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
</div> </div>
<div class="col-md-8"> <div class="col-md-8">
<div class="dropdown float-right" id="bulkActionButton" hidden> <?php if (lookupUserPermission("module_support") >= 2) { ?>
<button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown"> <div class="dropdown float-right" id="bulkActionButton" hidden>
<i class="fas fa-fw fa-layer-group mr-2"></i>Bulk Action (<span id="selectedCount">0</span>) <button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown">
</button> <i class="fas fa-fw fa-layer-group mr-2"></i>Bulk Action (<span id="selectedCount">0</span>)
<div class="dropdown-menu">
<button class="dropdown-item text-danger text-bold" type="submit" form="bulkActions" name="bulk_delete_recurring_tickets">
<i class="fas fa-fw fa-trash mr-2"></i>Delete
</button> </button>
<div class="dropdown-menu">
<button class="dropdown-item text-danger text-bold" type="submit" form="bulkActions" name="bulk_delete_recurring_tickets">
<i class="fas fa-fw fa-trash mr-2"></i>Delete
</button>
</div>
</div> </div>
</div> <?php } ?>
</div> </div>
</div> </div>
@ -115,7 +117,9 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
Next Run Date <?php if ($sort == 'scheduled_ticket_next_run') { echo $order_icon; } ?> Next Run Date <?php if ($sort == 'scheduled_ticket_next_run') { echo $order_icon; } ?>
</a> </a>
</th> </th>
<th class="text-center">Action</th> <?php if (lookupUserPermission("module_support") >= 2) { ?>
<th class="text-center">Action</th>
<?php } ?>
</tr> </tr>
</thead> </thead>
@ -154,25 +158,30 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
<td class="text-bold"><?php echo $scheduled_ticket_next_run ?></td> <td class="text-bold"><?php echo $scheduled_ticket_next_run ?></td>
<td> <?php if (lookupUserPermission("module_support") >= 2) { ?>
<div class="dropdown dropleft text-center"> <td>
<button class="btn btn-secondary btn-sm" type="button" data-toggle="dropdown"> <div class="dropdown dropleft text-center">
<i class="fas fa-ellipsis-h"></i> <button class="btn btn-secondary btn-sm" type="button" data-toggle="dropdown">
</button> <i class="fas fa-ellipsis-h"></i>
<div class="dropdown-menu"> </button>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#editRecurringTicketModal" onclick="populateRecurringTicketEditModal(<?php echo $scheduled_ticket_client_id, ",", $scheduled_ticket_id ?>)"> <div class="dropdown-menu">
<i class="fas fa-fw fa-edit mr-2"></i>Edit <a class="dropdown-item" href="#" data-toggle="modal" data-target="#editRecurringTicketModal" onclick="populateRecurringTicketEditModal(<?php echo $scheduled_ticket_client_id, ",", $scheduled_ticket_id ?>)">
</a> <i class="fas fa-fw fa-edit mr-2"></i>Edit
<?php
if ($session_user_role == 3) { ?>
<div class="dropdown-divider"></div>
<a class="dropdown-item text-danger text-bold confirm-link" href="post.php?delete_recurring_ticket=<?php echo $scheduled_ticket_id; ?>">
<i class="fas fa-fw fa-trash mr-2"></i>Delete
</a> </a>
<?php } ?> <div class="dropdown-divider"></div>
<a class="dropdown-item" href="post.php?force_recurring_ticket=<?php echo $scheduled_ticket_id; ?>&csrf_token=<?php echo $_SESSION['csrf_token'] ?>">
<i class="fa fa-fw fa-paper-plane text-secondary mr-2"></i>Force Reoccur
</a>
<?php if (lookupUserPermission("module_support") == 3) { ?>
<div class="dropdown-divider"></div>
<a class="dropdown-item text-danger text-bold confirm-link" href="post.php?delete_recurring_ticket=<?php echo $scheduled_ticket_id; ?>&csrf_token=<?php echo $_SESSION['csrf_token'] ?>">
<i class="fas fa-fw fa-trash mr-2"></i>Delete
</a>
<?php } ?>
</div>
</div> </div>
</div> </td>
</td> <?php } ?>
</tr> </tr>