mirror of
https://github.com/itflow-org/itflow
synced 2026-11-13 07:57:25 +00:00
71 lines
2.8 KiB
PHP
71 lines
2.8 KiB
PHP
<?php
|
|
|
|
require_once '../validate_api_key.php';
|
|
|
|
require_once '../require_post_method.php';
|
|
|
|
// Ticket-related settings
|
|
require_once "../../../includes/load_global_settings.php";
|
|
|
|
$sql = mysqli_query($mysqli, "SELECT company_name, company_phone FROM companies WHERE company_id = 1");
|
|
$row = mysqli_fetch_assoc($sql);
|
|
$company_name = $row['company_name'];
|
|
$company_phone = formatPhoneNumber($row['company_phone']);
|
|
|
|
// Parse Info
|
|
$ticket_row = false; // Creation, not an update
|
|
require_once 'ticket_model.php';
|
|
|
|
// Default
|
|
$insert_id = false;
|
|
|
|
// A client that exists, or none (internal work)
|
|
$client_exists = !$client_id || mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM clients WHERE client_id = " . intval($client_id)));
|
|
|
|
if (!empty($subject) && $client_exists) {
|
|
|
|
$client_id = intval($client_id);
|
|
$ticket_prefix = escapeSql($config_ticket_prefix);
|
|
|
|
// If no contact is selected automatically choose the primary contact for the client (if client set)
|
|
if ($contact == 0 && $client_id != 0) {
|
|
$sql = mysqli_query($mysqli,"SELECT contact_id FROM contacts WHERE contact_client_id = $client_id AND contact_primary = 1");
|
|
$row = mysqli_fetch_assoc($sql);
|
|
$contact = intval($row['contact_id']);
|
|
}
|
|
|
|
$ticket_status = 1; // Default
|
|
if ($assigned_to > 0) {
|
|
$ticket_status = 2; // Set to open if we've auto-assigned an agent
|
|
}
|
|
|
|
// Atomically increment and get the new ticket number
|
|
mysqli_query($mysqli, "
|
|
UPDATE settings
|
|
SET
|
|
config_ticket_next_number = LAST_INSERT_ID(config_ticket_next_number),
|
|
config_ticket_next_number = config_ticket_next_number + 1
|
|
WHERE company_id = 1
|
|
");
|
|
|
|
$ticket_number = mysqli_insert_id($mysqli);
|
|
|
|
// Insert ticket
|
|
$url_key = randomString(32);
|
|
$insert_sql = mysqli_query($mysqli,"INSERT INTO tickets SET ticket_prefix = '$ticket_prefix', ticket_number = $ticket_number, ticket_source = 'API', ticket_subject = '$subject', ticket_details = '$details', ticket_priority = '$priority', ticket_status = $ticket_status, ticket_billable = $billable, ticket_vendor_ticket_number = '$vendor_ticket_number', ticket_vendor_id = $vendor_id, ticket_created_by = 0, ticket_assigned_to = $assigned_to, ticket_contact_id = $contact, ticket_asset_id = $asset, ticket_url_key = '$url_key', ticket_client_id = $client_id");
|
|
|
|
// Check insert & get insert ID
|
|
if ($insert_sql) {
|
|
$insert_id = mysqli_insert_id($mysqli);
|
|
applyTicketSla($insert_id);
|
|
|
|
// Logging
|
|
logAudit("Ticket", "Create", "Created ticket $ticket_prefix$ticket_number $subject via API ($api_key_name)", $client_id, $insert_id);
|
|
logAudit("API", "Success", "Created ticket $ticket_prefix$ticket_number $subject via API ($api_key_name)", $client_id);
|
|
}
|
|
|
|
}
|
|
|
|
// Output
|
|
require_once '../create_output.php';
|