Skip to content

Commit

Permalink
Ticketing updates
Browse files Browse the repository at this point in the history
- Bugfix: Prevent tickets from being assigned to disabled techs
- Bugfix: Un-assign all open tickets when a tech account is disabled
- Allow auto-assignment of recurring/scheduled tickets to an agent/tech (itflow-org#901)
- Rework layout of recurring/scheduled ticket modal
  • Loading branch information
wrongecho committed Mar 17, 2024
1 parent cdd16bd commit f9a5ca1
Show file tree
Hide file tree
Showing 15 changed files with 425 additions and 210 deletions.
22 changes: 19 additions & 3 deletions ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
$client_id = intval($_GET['client_id']);
$ticket_id = intval($_GET['ticket_id']);

// Get all contacts, to allow tickets to be raised under a specific contact
$contact_sql = mysqli_query($mysqli, "SELECT contact_id, contact_name FROM contacts
WHERE contact_client_id = $client_id
AND contact_archived_at IS NULL
Expand All @@ -368,19 +369,34 @@
$response['contacts'][] = $row;
}


// Get ticket details
$ticket_sql = mysqli_query($mysqli, "SELECT * FROM scheduled_tickets
WHERE scheduled_ticket_id = $ticket_id
AND scheduled_ticket_client_id = $client_id LIMIT 1");
while ($row = mysqli_fetch_array($ticket_sql)) {
$response['ticket'][] = $row;
}

// Get assets
$asset_sql = mysqli_query($mysqli, "SELECT asset_id, asset_name FROM assets WHERE asset_client_id = $client_id AND asset_archived_at IS NULL");
while ($row = mysqli_fetch_array($asset_sql)) {
$response['assets'][] = $row;
}

// Get technicians to auto assign the ticket to
$sql_agents = mysqli_query(
$mysqli,
"SELECT users.user_id, user_name FROM users
LEFT JOIN user_settings on users.user_id = user_settings.user_id
WHERE user_role > 1
AND user_status = 1
AND user_archived_at IS NULL
ORDER BY user_name ASC"
);
while ($row = mysqli_fetch_array($sql_agents)) {
$response['agents'][] = $row;
}

echo json_encode($response);

}
Expand Down Expand Up @@ -448,9 +464,9 @@

$contact_sql = mysqli_query(
$mysqli,
"SELECT contact_id, contact_name FROM contacts
"SELECT contact_id, contact_name, contact_primary, contact_important, contact_technical FROM contacts
WHERE contacts.contact_archived_at IS NULL AND contact_client_id = $client_id
ORDER BY contact_important DESC, contact_name"
ORDER BY contact_primary DESC, contact_technical DESC, contact_important DESC, contact_name"
);

while ($row = mysqli_fetch_array($contact_sql)) {
Expand Down
10 changes: 8 additions & 2 deletions cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,16 @@
$priority = sanitizeInput($row['scheduled_ticket_priority']);
$frequency = sanitizeInput(strtolower($row['scheduled_ticket_frequency']));
$created_id = intval($row['scheduled_ticket_created_by']);
$assigned_id = intval($row['scheduled_ticket_assigned_to']);
$client_id = intval($row['scheduled_ticket_client_id']);
$contact_id = intval($row['scheduled_ticket_contact_id']);
$asset_id = intval($row['scheduled_ticket_asset_id']);

$ticket_status = 'New'; // Default
if ($assigned_id > 0) {
$ticket_status = 'Open'; // Set to open if we've auto-assigned an agent
}

// Assign this new ticket the next ticket number
$ticket_number_sql = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_ticket_next_number FROM settings WHERE company_id = 1"));
$ticket_number = intval($ticket_number_sql['config_ticket_next_number']);
Expand All @@ -262,7 +268,7 @@
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 = 'New', ticket_created_by = $created_id, ticket_contact_id = $contact_id, ticket_client_id = $client_id, ticket_asset_id = $asset_id");
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_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);

// Logging
Expand Down Expand Up @@ -316,7 +322,7 @@
if (filter_var($config_ticket_new_ticket_notification_email, FILTER_VALIDATE_EMAIL)) {

$email_subject = "ITFlow - New Recurring Ticket - $client_name: $ticket_subject";
$email_body = "Hello, <br><br>This is a notification that a recurring scheduled ticket has been raised in ITFlow. <br>Ticket: $ticket_prefix$ticket_number<br>Client: $client_name<br>Priority: $priority<br>Link: https://$config_base_url/ticket.php?ticket_id=$id <br><br>--------------------------------<br><br><b>$ticket_subject</b><br>$ticket_details";
$email_body = "Hello, <br><br>This is a notification that a recurring (scheduled) ticket has been raised in ITFlow. <br>Ticket: $ticket_prefix$ticket_number<br>Client: $client_name<br>Priority: $priority<br>Link: https://$config_base_url/ticket.php?ticket_id=$id <br><br>--------------------------------<br><br><b>$ticket_subject</b><br>$ticket_details";

$email = [
'from' => $config_ticket_from_email,
Expand Down
22 changes: 14 additions & 8 deletions database_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,7 @@
}



if (CURRENT_DATABASE_VERSION == '1.0.2') {
//Insert queries here required to update to DB version 1.0.3
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_stripe_expense_vendor` INT(11) NOT NULL DEFAULT 0 AFTER `config_stripe_account`");
Expand Down Expand Up @@ -1610,14 +1610,14 @@
if (CURRENT_DATABASE_VERSION == '1.0.6') {
// Insert queries here required to update to DB version 1.0.7
mysqli_query($mysqli, "CREATE TABLE `remember_tokens` (`remember_token_id` int(11) NOT NULL AUTO_INCREMENT,`remember_token_token` varchar(255) NOT NULL,`remember_token_user_id` int(11) NOT NULL,`remember_token_created_at` datetime NOT NULL DEFAULT current_timestamp(), PRIMARY KEY (`remember_token_id`))");

// Then, update the database to the next sequential version
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.0.7'");
}

if (CURRENT_DATABASE_VERSION == '1.0.7') {
mysqli_query($mysqli, "ALTER TABLE `user_settings` DROP `user_config_remember_me_token`");

mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.0.8'");
}

Expand All @@ -1626,13 +1626,13 @@
mysqli_query($mysqli, "ALTER TABLE `assets` DROP `asset_login_id`");
// Dropped this unused Table as we don't need many to many relationship between assets and logins
mysqli_query($mysqli, "DROP TABLE asset_logins");

mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.0.9'");
}

if (CURRENT_DATABASE_VERSION == '1.0.9') {
mysqli_query($mysqli, "ALTER TABLE `transfers` ADD `transfer_method` VARCHAR(200) DEFAULT NULL AFTER `transfer_id`");

mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.1.0'");
}

Expand All @@ -1647,10 +1647,16 @@
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.1.1'");
}

// if (CURRENT_DATABASE_VERSION == '1.1.1') {
// // Insert queries here required to update to DB version 1.1.2
if (CURRENT_DATABASE_VERSION == '1.1.1') {
mysqli_query($mysqli, "ALTER TABLE `scheduled_tickets` ADD `scheduled_ticket_assigned_to` INT(11) NOT NULL DEFAULT '0' AFTER `scheduled_ticket_created_by`");

mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.1.2'");
}

// if (CURRENT_DATABASE_VERSION == '1.1.2') {
// // Insert queries here required to update to DB version 1.1.3
// // Then, update the database to the next sequential version
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.1.2'");
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.1.3'");
// }

} else {
Expand Down
2 changes: 1 addition & 1 deletion database_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* It is used in conjunction with database_updates.php
*/

DEFINE("LATEST_DATABASE_VERSION", "1.1.1");
DEFINE("LATEST_DATABASE_VERSION", "1.1.2");

1 change: 1 addition & 0 deletions db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ CREATE TABLE `scheduled_tickets` (
`scheduled_ticket_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`scheduled_ticket_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
`scheduled_ticket_created_by` int(11) NOT NULL DEFAULT 0,
`scheduled_ticket_assigned_to` int(11) NOT NULL DEFAULT 0,
`scheduled_ticket_client_id` int(11) NOT NULL DEFAULT 0,
`scheduled_ticket_contact_id` int(11) NOT NULL DEFAULT 0,
`scheduled_ticket_asset_id` int(11) NOT NULL DEFAULT 0,
Expand Down
64 changes: 64 additions & 0 deletions js/recurring_tickets_add_modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Client selected listener
// We seem to have to use jQuery to listen for events, as the client input is a select2 component?

const clientSelectDropdown = document.getElementById("changeClientSelect"); // Define client selector

// If the client selector is disabled, we must be on client_recurring_tickets.php instead. Trigger the contact list update.
if (clientSelectDropdown.disabled) {

let client_id = $(clientSelectDropdown).find(':selected').val();

// Update the contacts dropdown list
populateContactsDropdown(client_id);

}

// Listener for client selection. Populate contact select when a client is selected
$(clientSelectDropdown).on('select2:select', function (e) {
let client_id = $(this).find(':selected').val();

// Update the contacts dropdown list
populateContactsDropdown(client_id);

// TODO: Update the assets dropdown list

});

// Populate client contact function (after a client is selected)
function populateContactsDropdown(client_id) {
// Send a GET request to ajax.php as ajax.php?get_client_contacts=true&client_id=NUM
jQuery.get(
"ajax.php",
{get_client_contacts: 'true', client_id: client_id},
function(data) {

// If we get a response from ajax.php, parse it as JSON
const response = JSON.parse(data);

// Access the data for contacts (multiple)
const contacts = response.contacts;

// Contacts dropdown
const contactSelectDropdown = document.getElementById("contactSelect");

// Clear Category dropdown
let i, L = contactSelectDropdown.options.length - 1;
for (i = L; i >= 0; i--) {
contactSelectDropdown.remove(i);
}
contactSelectDropdown[contactSelectDropdown.length] = new Option('- Contact -', '0');

// Populate dropdown
contacts.forEach(contact => {
var appendText = "";
if (contact.contact_primary == "1") {
appendText = " (Primary)";
} else if (contact.contact_technical == "1") {
appendText = " (Technical)";
}
contactSelectDropdown[contactSelectDropdown.length] = new Option(contact.contact_name + appendText, contact.contact_id);
});

}
);
}
24 changes: 24 additions & 0 deletions js/recurring_tickets_edit_modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function populateRecurringTicketEditModal(client_id, ticket_id) {
const contacts = response.contacts;
const ticket = response.ticket[0];
const assets = response.assets;
const agents = response.agents;

// Populate the scheduled ticket modal fields
document.getElementById("editHeader").innerText = " Edit Recurring ticket: " + ticket.scheduled_ticket_subject;
Expand Down Expand Up @@ -80,6 +81,29 @@ function populateRecurringTicketEditModal(client_id, ticket_id) {
assetDropdown[assetDropdown.length] = new Option(asset.asset_name, asset.asset_id);
}
});


// Agent dropdown
var agentDropdown = document.getElementById("editTicketAgent");

// Clear agent dropdown
var i, L = agentDropdown.options.length -1;
for(i = L; i >= 0; i--) {
agentDropdown.remove(i);
}
agentDropdown[agentDropdown.length] = new Option('- Agent -', '0');


// Populate dropdown
agents.forEach(agent => {
if(parseInt(agent.user_id) == parseInt(ticket.scheduled_ticket_assigned_to)){
// Selected agent
agentDropdown[agentDropdown.length] = new Option(agent.user_name, agent.user_id, true, true);
}
else{
agentDropdown[agentDropdown.length] = new Option(agent.user_name, agent.user_id);
}
});
}
);
}
5 changes: 5 additions & 0 deletions post/recurring_ticket_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@
if (isset($_POST['contact'])) {
$contact_id = intval($_POST['contact']);
}

$assigned_to = "0";
if (isset($_POST['assigned_to'])) {
$assigned_to = intval($_POST['assigned_to']);
}
8 changes: 4 additions & 4 deletions post/ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@
exit;
}

if (isset($_POST['add_scheduled_ticket']) || isset($_POST['add_recurring_ticket'])) {
if (isset($_POST['add_recurring_ticket'])) {

validateTechRole();

Expand All @@ -1334,7 +1334,7 @@
}

// Add scheduled ticket
mysqli_query($mysqli, "INSERT INTO scheduled_tickets SET scheduled_ticket_subject = '$subject', scheduled_ticket_details = '$details', scheduled_ticket_priority = '$priority', scheduled_ticket_frequency = '$frequency', scheduled_ticket_start_date = '$start_date', scheduled_ticket_next_run = '$start_date', scheduled_ticket_created_by = $session_user_id, scheduled_ticket_client_id = $client_id, scheduled_ticket_contact_id = $contact_id, scheduled_ticket_asset_id = $asset_id");
mysqli_query($mysqli, "INSERT INTO scheduled_tickets SET scheduled_ticket_subject = '$subject', scheduled_ticket_details = '$details', scheduled_ticket_priority = '$priority', scheduled_ticket_frequency = '$frequency', scheduled_ticket_start_date = '$start_date', scheduled_ticket_next_run = '$start_date', scheduled_ticket_assigned_to = $assigned_to, scheduled_ticket_created_by = $session_user_id, scheduled_ticket_client_id = $client_id, scheduled_ticket_contact_id = $contact_id, scheduled_ticket_asset_id = $asset_id");

$scheduled_ticket_id = mysqli_insert_id($mysqli);

Expand All @@ -1346,7 +1346,7 @@
header("Location: " . $_SERVER["HTTP_REFERER"]);
}

if (isset($_POST['edit_scheduled_ticket']) || isset($_POST['edit_recurring_ticket'])) {
if (isset($_POST['edit_recurring_ticket'])) {

validateTechRole();

Expand All @@ -1363,7 +1363,7 @@
}

// Edit scheduled ticket
mysqli_query($mysqli, "UPDATE scheduled_tickets SET scheduled_ticket_subject = '$subject', scheduled_ticket_details = '$details', scheduled_ticket_priority = '$priority', scheduled_ticket_frequency = '$frequency', scheduled_ticket_next_run = '$next_run_date', scheduled_ticket_asset_id = $asset_id, scheduled_ticket_contact_id = $contact_id WHERE scheduled_ticket_id = $scheduled_ticket_id");
mysqli_query($mysqli, "UPDATE scheduled_tickets SET scheduled_ticket_subject = '$subject', scheduled_ticket_details = '$details', scheduled_ticket_priority = '$priority', scheduled_ticket_frequency = '$frequency', scheduled_ticket_next_run = '$next_run_date', scheduled_ticket_assigned_to = $assigned_to, scheduled_ticket_asset_id = $asset_id, scheduled_ticket_contact_id = $contact_id WHERE scheduled_ticket_id = $scheduled_ticket_id");

// Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Recurring Ticket', log_action = 'Modify', log_description = '$session_name modified recurring ticket for $subject - $frequency', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $scheduled_ticket_id");
Expand Down
4 changes: 4 additions & 0 deletions post/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@

mysqli_query($mysqli, "UPDATE users SET user_status = 0 WHERE user_id = $user_id");

// Un-assign tickets
mysqli_query($mysqli, "UPDATE tickets SET ticket_assigned_to = 0 WHERE ticket_assigned_to = $user_id AND ticket_closed_at IS NULL");
mysqli_query($mysqli, "UPDATE scheduled_tickets SET scheduled_ticket_assigned_to = 0 WHERE scheduled_ticket_assigned_to = $user_id");

//Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'User', log_action = 'Modify', log_description = '$session_name disabled user $user_name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id, log_entity_id = $user_id");

Expand Down
Loading

0 comments on commit f9a5ca1

Please sign in to comment.