diff --git a/ajax.php b/ajax.php index 98003749..d52ad331 100644 --- a/ajax.php +++ b/ajax.php @@ -344,6 +344,45 @@ if (isset($_GET['quote_get_json_details'])) { } +/* + * Returns sorted list of active clients + */ +if (isset($_GET['get_active_clients'])) { + + $client_sql = mysqli_query( + $mysqli, + "SELECT client_id, client_name FROM clients + WHERE client_archived_at IS NULL + ORDER BY client_accessed_at DESC" + ); + + while ($row = mysqli_fetch_array($client_sql)) { + $response['clients'][] = $row; + } + + echo json_encode($response); +} + +/* + * Returns ordered list of active contacts for a specified client + */ +if (isset($_GET['get_client_contacts'])) { + $client_id = intval($_GET['client_id']); + + $contact_sql = mysqli_query( + $mysqli, + "SELECT contact_id, contact_name FROM contacts + WHERE contacts.contact_archived_at IS NULL AND contact_client_id = $client_id + ORDER BY contact_important DESC, contact_name" + ); + + while ($row = mysqli_fetch_array($contact_sql)) { + $response['contacts'][] = $row; + } + + echo json_encode($response); +} + /* * Dynamic TOTP for client login page * When provided with a TOTP secret, returns a 6-digit code diff --git a/database_updates.php b/database_updates.php index a4105ee2..b16095fc 100644 --- a/database_updates.php +++ b/database_updates.php @@ -865,15 +865,15 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) { mysqli_query($mysqli, "ALTER TABLE `client_tags` CHANGE `client_id` `client_tags_client_id` INT NOT NULL"); mysqli_query($mysqli, "ALTER TABLE `client_tags` CHANGE `tag_id` `client_tags_tag_id` INT NOT NULL"); - // Then, update the database to the next sequential version - mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.4.4'"); + // Then, update the database to the next sequential version + mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.4.4'"); } if (CURRENT_DATABASE_VERSION == '0.4.4') { - // Insert queries here required to update to DB version 0.4.5 + // Insert queries here required to update to DB version 0.4.5 mysqli_query($mysqli, "ALTER TABLE `client_tags` CHANGE `client_tags_client_id` `client_tag_client_id` INT NOT NULL"); mysqli_query($mysqli, "ALTER TABLE `client_tags` CHANGE `client_tags_tag_id` `client_tag_tag_id` INT NOT NULL"); - // Then, update the database to the next sequential version + // Then, update the database to the next sequential version mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.4.5'"); } @@ -940,11 +940,17 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) { mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.4.8'"); } -//if (CURRENT_DATABASE_VERSION == '0.4.8') { - // Insert queries here required to update to DB version 0.4.9 + if (CURRENT_DATABASE_VERSION == '0.4.8') { + mysqli_query($mysqli, "ALTER TABLE `tickets` ADD `ticket_source` VARCHAR(255) NULL DEFAULT NULL AFTER `ticket_number`"); + + mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.4.9'"); + } + + //if (CURRENT_DATABASE_VERSION == '0.4.9') { + // Insert queries here required to update to DB version 0.5.0 // Then, update the database to the next sequential version - // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.4.9'"); + // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.5.0'"); //} } else { diff --git a/database_version.php b/database_version.php index a02019fa..69296470 100644 --- a/database_version.php +++ b/database_version.php @@ -5,4 +5,4 @@ * It is used in conjunction with database_updates.php */ -DEFINE("LATEST_DATABASE_VERSION", "0.4.8"); +DEFINE("LATEST_DATABASE_VERSION", "0.4.9"); diff --git a/db.sql b/db.sql index 58a2d9e8..c06529e8 100644 --- a/db.sql +++ b/db.sql @@ -1280,6 +1280,7 @@ CREATE TABLE `tickets` ( `ticket_id` int(11) NOT NULL AUTO_INCREMENT, `ticket_prefix` varchar(200) DEFAULT NULL, `ticket_number` int(11) NOT NULL, + `ticket_source` varchar(255) DEFAULT NULL, `ticket_category` varchar(200) DEFAULT NULL, `ticket_subject` varchar(200) NOT NULL, `ticket_details` longtext NOT NULL, diff --git a/js/ticket_change_client.js b/js/ticket_change_client.js new file mode 100644 index 00000000..c3b553bc --- /dev/null +++ b/js/ticket_change_client.js @@ -0,0 +1,94 @@ +/* + * LISTENERS + */ + +// Modal loaded listener - populate client select +const changeClientModalLoad = document.getElementById('clientChangeTicketModalLoad'); +changeClientModalLoad.addEventListener('click', function() { + populateChangeClientModal_Clients(); +}) + +// Client selected listener - populate contact select +// We seem to have to use jQuery to listen for events, as the client input is a select2 component? +const clientSelectDropdown = document.getElementById("changeClientSelect"); +$(clientSelectDropdown).on('select2:select', function (e) { + let client_id = $(this).find(':selected').val(); + populateChangeClientModal_Contacts(client_id); +}); + + +/* + * FUNCTIONS + */ + +// Populate client list function +function populateChangeClientModal_Clients() { + + // Get current client ID + let current_client_id = document.getElementById("client_id").value; + + // Send a GET request to ajax.php as ajax.php?get_active_clients=true + jQuery.get( + "ajax.php", + {get_active_clients: 'true'}, + function(data) { + + // If we get a response from ajax.php, parse it as JSON + const response = JSON.parse(data); + + // Access the data for clients (multiple) + const clients = response.clients; + + // Client dropdown already defined in listeners as clientSelectDropdown + + // Clear dropdown + let i, L = clientSelectDropdown.options.length - 1; + for (i = L; i >= 0; i--) { + clientSelectDropdown.remove(i); + } + clientSelectDropdown[clientSelectDropdown.length] = new Option('- Client -', '0'); + + // Populate dropdown + clients.forEach(client => { + if (parseInt(current_client_id) !== parseInt(client.client_id)) { + // Show clients returned (excluding the current client ID - we can't change a ticket client to itself) + clientSelectDropdown[clientSelectDropdown.length] = new Option(client.client_name, client.client_id); + } + }); + + } + ); +} + +// Populate client contact function (after a client is selected) +function populateChangeClientModal_Contacts(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("changeContactSelect"); + + // 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 => { + contactSelectDropdown[contactSelectDropdown.length] = new Option(contact.contact_name, contact.contact_id); + }); + + } + ); +} diff --git a/post.php b/post.php index 97f8be90..3b0b236d 100644 --- a/post.php +++ b/post.php @@ -6529,6 +6529,29 @@ if(isset($_POST['merge_ticket'])){ } +if(isset($_POST['change_client_ticket'])){ + + validateTechRole(); + + $ticket_id = intval($_POST['ticket_id']); + $client_id = intval($_POST['new_client_id']); + $contact_id = intval($_POST['new_contact_id']); + + // Set any/all existing replies to internal + mysqli_query($mysqli, "UPDATE ticket_replies SET ticket_reply_type = 'Internal' WHERE ticket_reply_ticket_id = $ticket_id"); + + // Update ticket client & contact + mysqli_query($mysqli, "UPDATE tickets SET ticket_client_id = $client_id, ticket_contact_id = $contact_id WHERE ticket_id = $ticket_id LIMIT 1"); + + //Logging + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket Reply', log_action = 'Modify', log_description = '$session_name modified ticket - client changed', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $ticket_id"); + + $_SESSION['alert_message'] = "Ticket client updated"; + + header("Location: " . $_SERVER["HTTP_REFERER"]); + +} + if(isset($_GET['close_ticket'])){ validateTechRole(); diff --git a/ticket.php b/ticket.php index 7cefb9ca..8aa1586c 100644 --- a/ticket.php +++ b/ticket.php @@ -119,11 +119,13 @@ if (isset($_GET['ticket_id'])) { $location_zip = htmlentities($row['location_zip']); $location_phone = formatPhoneNumber($row['location_phone']); + // REMOVING - doesn't work properly now that a ticket might be created by an agent or client + // Moving to ticket_source in future //Ticket Created By - $ticket_created_by = intval($row['ticket_created_by']); - $ticket_created_by_sql = mysqli_query($mysqli, "SELECT user_name FROM users WHERE user_id = $ticket_created_by"); - $row = mysqli_fetch_array($ticket_created_by_sql); - $ticket_created_by_display = htmlentities($row['user_name']); + //$ticket_created_by = intval($row['ticket_created_by']); + //$ticket_created_by_sql = mysqli_query($mysqli, "SELECT user_name FROM users WHERE user_id = $ticket_created_by"); + //$row = mysqli_fetch_array($ticket_created_by_sql); + //$ticket_created_by_display = htmlentities($row['user_name']); if ($contact_id) { //Get Contact Ticket Stats @@ -233,6 +235,9 @@ if (isset($_GET['ticket_id'])) { Merge + + Change Client + @@ -265,7 +270,7 @@ if (isset($_GET['ticket_id'])) {
- +
@@ -468,7 +473,7 @@ if (isset($_GET['ticket_id'])) {
Created:
Updated:
-
Created by:
+ +
+ + + + + +