diff --git a/add_scheduled_ticket_modal.php b/add_scheduled_ticket_modal.php new file mode 100644 index 00000000..36779fe8 --- /dev/null +++ b/add_scheduled_ticket_modal.php @@ -0,0 +1,158 @@ + \ No newline at end of file diff --git a/client_tickets.php b/client_tickets.php index a5209a36..1fac7f86 100644 --- a/client_tickets.php +++ b/client_tickets.php @@ -55,6 +55,7 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli,"SELECT FOUND_ROWS()"));

Tickets

+
@@ -195,4 +196,7 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli,"SELECT FOUND_ROWS()"));
- + diff --git a/cron.php b/cron.php index f213e623..ba6179ed 100644 --- a/cron.php +++ b/cron.php @@ -32,6 +32,10 @@ while($row = mysqli_fetch_array($sql_companies)){ $config_recurring_auto_send_invoice = $row['config_recurring_auto_send_invoice']; $config_base_url = $row['config_base_url']; + //Tickets + $config_ticket_prefix = $row['config_ticket_prefix']; + $config_ticket_next_number = $row['config_ticket_next_number']; + if($config_enable_cron == 1){ //GET ALERTS @@ -286,6 +290,62 @@ while($row = mysqli_fetch_array($sql_companies)){ //Send Alert to inform Cron was run mysqli_query($mysqli,"INSERT INTO alerts SET alert_type = 'Cron', alert_message = 'Cron.php successfully executed', alert_date = NOW(), company_id = $company_id"); } //End Cron Check + + // Scheduled tickets + // This should really be somewhere else above, but I don't want to break anything. Please move as appropriate. + + // Get date now, and calculate tomorrow's date (presuming this is being run at 11 PM) + $now = new DateTime(); + $tomorrow = date_add($now, date_interval_create_from_date_string('1 day')); + $tomorrow_text = $tomorrow->format('Y-m-d'); + + // Get scheduled tickets for tomorrow + $sql_scheduled_tickets = mysqli_query($mysqli, "SELECT * FROM scheduled_tickets WHERE scheduled_ticket_next_run = '$tomorrow_text'"); + + if(mysqli_num_rows($sql_scheduled_tickets) > 0){ + while($row = mysqli_fetch_array($sql_scheduled_tickets)){ + $schedule_id = $row['scheduled_ticket_id']; + $subject = $row['scheduled_ticket_subject']; + $details = $row['scheduled_ticket_details']; + $priority = $row['scheduled_ticket_priority']; + $frequency = strtolower($row['scheduled_ticket_frequency']); + $created_id = $row['scheduled_ticket_created_by']; + $client_id = $row['scheduled_ticket_client_id']; + $contact_id = $row['scheduled_ticket_contact_id']; + $asset_id = $row['scheduled_ticket_asset_id']; + $company_id = $row['company_id']; + + //Get the next Ticket Number and add 1 for the new ticket number + $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 = '$company_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 = 'Open', ticket_created_at = NOW(), ticket_created_by = $created_id, ticket_contact_id = $contact_id, ticket_client_id = $client_id, ticket_asset_id = $asset_id, company_id = $company_id"); + + //Logging + mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'System created scheduled $frequency ticket - $subject', log_created_at = NOW(), log_client_id = $client_id, company_id = $company_id, log_user_id = $created_id"); + + //Set the next run date + if($frequency == "weekly"){ + //NOTE: We seemingly have to initialize a new datetime for each loop. + //Otherwise it stacks the dates of $now / $tomorrow, e.g. by the third scheduled ticket it will schedule the next run for three weeks/months out instead of one + //This isn't clean but it works + //TODO: Refactor this + $now = new DateTime(); + $next_run = date_add($now, date_interval_create_from_date_string('1 week 1 day')); + } + elseif($frequency == "monthly"){ + $now = new DateTime(); + $next_run = date_add($now, date_interval_create_from_date_string('1 month 1 day')); + } + + //Update the run date + $next_run = $next_run->format('Y-m-d'); + $a = mysqli_query($mysqli, "UPDATE scheduled_tickets SET scheduled_ticket_next_run = '$next_run' WHERE scheduled_ticket_id = '$schedule_id'"); + + } + } + } //End Company Loop through ?> diff --git a/db.sql b/db.sql index 0f2f5bcc..9b3abb81 100644 --- a/db.sql +++ b/db.sql @@ -852,6 +852,31 @@ CREATE TABLE `roles` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `scheduled_tickets` +-- + +DROP TABLE IF EXISTS `scheduled_tickets`; +CREATE TABLE IF NOT EXISTS `scheduled_tickets` ( + `scheduled_ticket_id` int(11) NOT NULL AUTO_INCREMENT, + `scheduled_ticket_category` varchar(200) DEFAULT NULL, + `scheduled_ticket_subject` varchar(200) NOT NULL, + `scheduled_ticket_details` longtext NOT NULL, + `scheduled_ticket_priority` varchar(200) DEFAULT NULL, + `scheduled_ticket_frequency` varchar(10) NOT NULL, + `scheduled_ticket_start_date` date NOT NULL, + `scheduled_ticket_next_run` date NOT NULL, + `scheduled_ticket_created_at` datetime NOT NULL, + `scheduled_ticket_updated_at` datetime DEFAULT NULL, + `scheduled_ticket_created_by` int(11) NOT NULL, + `scheduled_ticket_client_id` int(11) DEFAULT NULL, + `scheduled_ticket_contact_id` int(11) DEFAULT NULL, + `scheduled_ticket_asset_id` int(11) DEFAULT NULL, + `company_id` int(11) NOT NULL, + PRIMARY KEY (`scheduled_ticket_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +COMMIT; + -- -- Table structure for table `settings` -- diff --git a/post.php b/post.php index 2c4a076a..29fdc542 100644 --- a/post.php +++ b/post.php @@ -4863,6 +4863,34 @@ if(isset($_POST['add_ticket'])){ } +if(isset($_POST['add_scheduled_ticket'])){ + $client_id = intval($_POST['client']); + $contact = intval($_POST['contact']); + $subject = trim(strip_tags(mysqli_real_escape_string($mysqli,$_POST['subject']))); + $priority = trim(strip_tags(mysqli_real_escape_string($mysqli,$_POST['priority']))); + $details = trim(mysqli_real_escape_string($mysqli,$_POST['details'])); + $asset_id = intval($_POST['asset']); + $frequency = trim(mysqli_real_escape_string($mysqli,$_POST['frequency'])); + $start_date = mysqli_real_escape_string($mysqli,$_POST['start_date']); + + if($client_id > 0 AND $contact == 0){ + $sql = mysqli_query($mysqli,"SELECT primary_contact FROM clients WHERE client_id = $client_id AND company_id = $session_company_id"); + $row = mysqli_fetch_array($sql); + $contact = $row['primary_contact']; + } + + // 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_at = NOW(), scheduled_ticket_created_by = '$session_user_id', scheduled_ticket_client_id = '$client_id', scheduled_ticket_contact_id = '$contact', scheduled_ticket_asset_id = '$asset_id', company_id = '$session_company_id'"); + + //Logging + mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'Created scheduled ticket for $subject $frequency starting $start_date', log_created_at = NOW(), log_client_id = $client_id, company_id = $session_company_id, log_user_id = $session_user_id"); + + $_SESSION['alert_message'] = "Scheduled ticket created."; + + header("Location: " . $_SERVER["HTTP_REFERER"]); + +} + if(isset($_POST['edit_ticket'])){ $ticket_id = intval($_POST['ticket_id']);