diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d2d5347a..26306f12a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ Update from Maintenance > Update — Update ITFlow hands the job to cron, which ### New Features & Updates - API: products can be updated through the API (`products/update`). Only the fields you send are changed, and the key's user needs edit access to Sales, the same as editing a product in ITFlow. +- API: a ticket can be raised from a ticket template (`tickets/create`). Send `ticket_template_id` and the ticket gets what the template sets up - its subject and details unless you send your own, its category, priority, billing, agent, tags, due time and tasks - with the template's fill-ins answered from `ticket_fill_ins`. Send `ticket_notify` too and people hear about it as when it is raised in ITFlow: the contact gets the Ticket Created email, and the new-ticket address and the agent it is assigned to are told. The answer gives the ticket's number, as TCK-93, and the link its contact opens it with. A form on your website can open a ticket with the steps to work through already on it. +- Canned Responses: on a ticket raised from a ticket template, the template's answers fill in too. A response that says [Hostname] or [Employee Name] gets that ticket's answer as it goes in, as [Contact] and [Ticket] do - so one response can serve every ticket from the template. An answer left blank stays in brackets for you to fill in. - API keys: agents are notified in ITFlow a week before an API key expires, so an integration doesn't stop without warning. - Payment Providers: Stripe's fee on a card payment is now recorded as an expense about 15 minutes after the payment, instead of the next morning. A new Payment Provider Fees job in Maintenance > Cron does it, and you can run it more often there. Record fees now still works any time. - Payment Providers: clients can pay the card fee. Turn on Clients pay the card fee on Stripe in Admin > Payment Providers and enter what Stripe charges you, as 2.9% plus 0.30. Each card payment is then made bigger by Stripe's fee on it, so you get the full invoice. The fee goes on the invoice as a Card Fee line, with no tax, when the payment goes through. Clients see it before they pay: on Pay invoices and the checkout page in the client portal, on the page an invoice's link opens, and next to autopay. It applies to payments in the client portal, through an invoice's link, by autopay, and when you charge a saved card, where the Charge Saved Card window shows it too. One payment for several invoices has one fee, shared over them. A client can be let off it with No card fee on their Billing tab. Refunding a card payment from ITFlow takes its Card Fee line back off the invoice. It is off until you turn it on. Card surcharges have rules where you and your clients are - in the US, for one, the card networks cap them and don't allow them on debit cards - so check them before turning it on. @@ -31,6 +33,7 @@ Update from Maintenance > Update — Update ITFlow hands the job to cron, which - Transactions: the list, its Transfers pill, Reconcile and the transactions export load faster again. On a test with eight years of books, the Transfers pill went from about a third of a second in 26.11.2 to under a fifth. - Expenses: billable expenses waiting to go on an invoice slowed down every page, because the side menu counts them and each one was checked against every expense. With a few hundred waiting, that was about a third of a second on every page. The invoice page, Add Expenses and an expense's quick look had the same delay. All of them are quick again. - Transactions: on All accounts, Money in and Money out counted every transfer between your own accounts twice - once out of one account and once into the other - so both were far too high. A Stripe payout to the bank, a check deposit or paying off a card all added to both. They now leave transfers between your accounts out, as the dashboard and Income already did. The Transfers pill still lists them, and one account on its own still counts them as money in and out of that account. The Difference was always right. +- API: adding a contact or a location through the API (`contacts/create`, `locations/create`) no longer writes a PHP warning to the server's error log for each field left out. - App Logs: the email parser logged an error every minute while email-to-ticket parsing was switched off, burying the errors that matter. It now just stops, and Maintenance > Cron says why. - Phone numbers: outside North America, a number shown with its country code kept the 0 - or the 8 in Russia - that is only dialled within the country, as "+49 0172 1234567" for a German mobile, wherever it showed: ITFlow's pages, the client portal (a contact's own profile, and the Contact us details), emails, PDFs and exports. Click-to-call dialled it that way too, and so failed. Numbers are now written the way each country writes them: "+49 172 1234567", and "0172 1234567" without the country code. This covers every country, not only the ones ITFlow had its own rules for. A German landline such as Munich's is split after its area code ("+49 89 1234567"), Italy keeps the 0 its landlines are dialled with from abroad ("+39 06 1234 5678"), and South African numbers read "+27 82 123 4567". A number typed with or without that leading 0, or with its country code at the front, shows and dials the same. North American numbers look as they did. @@ -66,6 +69,8 @@ Update from Maintenance > Update — Update ITFlow hands the job to cron, which - Locks: `getAgentUserLimit()` returns `managedSeatLimit()` when the manager sets one, and `getActiveAgentUserCount()` leaves out `managedSupportUserId()`. `managedApplyState()` writes the manager's white-label key to `config_whitelabel_key` and takes it out again when the manager stops setting it, and Settings > Modules doesn't save over it. `admin/update.php` hides Update ITFlow and `queue_update` refuses on a managed instance. `managedRequired()` reads `$config_manager_required`. - `cron/ticket_email_parser.php` no longer writes an App Logs error when cron or parsing is switched off - `cronJobStop()` already records why, and with parsing off it was an error a minute, which `managedReport()` counts. The audit log knows the Managed Instance and Management types and the Connect, Disconnect, Remove, Release, Code, Mode and Support actions. - Shared links have a new type, `Export` (IT documentation). `agent/ajax.php` share_create makes its PDF with `clientExportOptions()`, `clientExportCheck()` and `clientExportPdf()`, as the download does, and hands it to `shareCreate()` as `export` (pdf, name, secret). The link seals the PDF's name, size, SHA-256 and whether it's password-locked into `item_payload`, and `shareExportStore()` writes the PDF to `uploads/shares/export-.bin` with AES-256-GCM under the link's content key - iv, tag, ciphertext - in a folder `backupHardenStorageDir()` keeps from the web, so the disk and the database alone open nothing. `guest/guest_download_file.php` serves File or Export, the PDF through `shareExportRead()`; `guest/share_ajax.php` describes it from `shareExportSize()` and the payload; `js/guest_share.js` downloads it as it does a file. `shareEnd()` and `shareSweep()` delete the file with the payload, and `sharePrune()` deletes any left without a link that can still open it (a client deleted with its links). `shareNeedsProtection($type, $secret)` takes whether this link holds passwords, `shareCanSee()` and `shareCanMake()` need `module_client` for it, and it's logged as a Client Export too. No database change - `item_type` is a varchar. `clientExportSectionPicker()` takes an id prefix and the sections to start ticked; Export Data's Share a link passes the ticked ones as `sections[]`. +- `api/v1/tickets/create.php` takes `ticket_template_id`, `ticket_fill_ins` (a JSON object of fill-in names and answers, checked with `ticketFillInAnswers()`) and `ticket_notify`. A missing or archived template, `ticket_fill_ins` that isn't an object and a required fill-in left out are a 400 with the reason in `message`. From a template, `ticket_subject`, `ticket_details`, `ticket_priority`, `ticket_billable` and `ticket_assigned_to` not sent come from it, `ticket_category` is its category, and `ticketTemplateFinish($ticket_id, $template_id, true, true, $answers, ...)` adds its tags, due time, tasks and fill-in answers and fills its placeholders. `ticket_notify` = 1 calls `ticketMailCreated()` and `ticketMailColleaguesNew()` when `config_ticket_client_general_notifications` is on, `ticketMailNewTicketAddress($ticket_id, 'api')`, and for an assigned ticket an app notification and `ticketMailAssigned()`. The answer adds `ticket_reference`, `ticket_url` (`ticketEmailUrl()`) and, from a template, `ticket_tasks`. `ticketAgentEmailContent()`'s `new` takes how `api` ("raised a ticket through the API"). A request without the three fields is raised as before, with the two new fields in its answer. +- `cannedResponseValues()` adds the ticket's fill-in answers (`ticketFillInsStored('ticket', $ticket_id)`, as `ticketFillInText()` words them), answered ones only and never over a placeholder of the same name, so `cannedResponsePlaceholders()` and `cannedResponseFill()` - the picker, the `/` search and Update & Reply - fill them in. `includes/canned_response_form.php` says so under the placeholder buttons. ## [26.11.2] diff --git a/api/v1/contacts/contact_model.php b/api/v1/contacts/contact_model.php index 076006154..dcd3aa035 100644 --- a/api/v1/contacts/contact_model.php +++ b/api/v1/contacts/contact_model.php @@ -2,6 +2,10 @@ define('number_regex', '/[^0-9]/'); // Variable assignment from POST (or: blank/from DB is updating) + +// create.php has no row to fall back on +$contact_row = $contact_row ?? null; + if (isset($_POST['contact_name'])) { $name = escapeSql($_POST['contact_name']); } elseif ($contact_row) { diff --git a/api/v1/locations/location_model.php b/api/v1/locations/location_model.php index f7246f2a8..c084d166d 100644 --- a/api/v1/locations/location_model.php +++ b/api/v1/locations/location_model.php @@ -2,6 +2,9 @@ // Variable assignment from POST (or: blank/from DB is updating) +// create.php has no row to fall back on +$location_row = $location_row ?? null; + if (isset($_POST['location_name'])) { $name = escapeSql($_POST['location_name']); } elseif ($location_row) { diff --git a/api/v1/tickets/create.php b/api/v1/tickets/create.php index 08f99e94f..66661538d 100644 --- a/api/v1/tickets/create.php +++ b/api/v1/tickets/create.php @@ -1,5 +1,16 @@ Ticket Templates): the template's subject and details for those not + * sent, its category, priority, billing and agent for those not sent, its + * tags, due time and tasks, and its fill-ins answered from ticket_fill_ins + * ({"Name": "answer"}). ticket_notify = 1 tells people as a ticket raised in + * ITFlow does - the contact and watchers (when ticket emails to clients are + * on), the new-ticket address and the agent it is assigned to. The answer has + * the ticket's number as people see it and where its contact opens it. + */ + require_once '../validate_api_key.php'; require_once '../require_post_method.php'; @@ -19,6 +30,52 @@ require_once 'ticket_model.php'; // Default $insert_id = false; +/* A request that can't be raised as sent - 400 and why, in plain text */ +$ticket_refuse = function ($message) use (&$return_arr) { + $return_arr['success'] = "False"; + $return_arr['message'] = html_entity_decode(strip_tags($message), ENT_QUOTES | ENT_HTML5, 'UTF-8'); + header("HTTP/1.1 400 Bad Request"); + echo json_encode($return_arr); + exit(); +}; + +// From a template - what it sets, for whatever the request leaves out, and its fill-ins answered +$template = null; +$fill_in_answers = null; +if (!empty($_POST['ticket_template_id'])) { + $template = ticketTemplateRow($_POST['ticket_template_id']); + if (!$template || !empty($template['ticket_template_archived_at'])) { + $ticket_refuse("There is no ticket template " . intval($_POST['ticket_template_id']) . " - it may have been archived. See Admin > Ticket Templates."); + } + $posted_fill_ins = $_POST['ticket_fill_ins'] ?? []; + if (!is_array($posted_fill_ins)) { + $ticket_refuse("ticket_fill_ins is a JSON object of the template's fill-ins and their answers - {\"Name\": \"answer\"}."); + } + $fill_in_answers = ticketFillInAnswers(ticketTemplateFillInsFor($template['ticket_template_id']), $posted_fill_ins, $fill_in_error); + if ($fill_in_answers === null) { + $ticket_refuse($fill_in_error); + } + + if (!isset($_POST['ticket_subject'])) { + $template_subject = trim((string) $template['ticket_template_subject']) !== '' ? (string) $template['ticket_template_subject'] : (string) $template['ticket_template_name']; + $subject = escapeSql(mb_substr($template_subject, 0, 500)); + } + if (!isset($_POST['ticket_details'])) { + $details = mysqli_real_escape_string($mysqli, (string) $template['ticket_template_details']); + } + if (!isset($_POST['ticket_priority'])) { + $priority = escapeSql(ticketPriority($template['ticket_template_priority'] ?? '')); + } + if (!isset($_POST['ticket_billable']) && $template['ticket_template_billable'] !== null) { + $billable = intval($template['ticket_template_billable']) ? 1 : 0; + } + if (!isset($_POST['ticket_assigned_to'])) { + $assigned_to = ticketAgentId($template['ticket_template_assigned_to']); + } +} +$category_id = $template ? ticketCategoryId($template['ticket_template_category_id']) : 0; +$notify = intval($_POST['ticket_notify'] ?? 0) === 1; + // 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))); @@ -31,7 +88,7 @@ if (!empty($subject) && $client_exists) { 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']); + $contact = intval($row['contact_id'] ?? 0); } $ticket_status = 1; // Default @@ -52,15 +109,43 @@ if (!empty($subject) && $client_exists) { // 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"); + $insert_sql = mysqli_query($mysqli,"INSERT INTO tickets SET ticket_prefix = '$ticket_prefix', ticket_number = $ticket_number, ticket_source = 'API', ticket_category = $category_id, 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); + // Raised from a template - its tags, due time and tasks, its fill-ins kept and its placeholders filled in + if ($template) { + ticketTemplateFinish($insert_id, $template['ticket_template_id'], true, true, $fill_in_answers, "Raised by the API ($api_key_name) from template " . $template['ticket_template_name']); + } + + // Who hears about it, as when it's raised in ITFlow + if ($notify) { + if (intval($config_ticket_client_general_notifications) === 1) { + ticketMailCreated($insert_id, 'Ticket Created'); + ticketMailColleaguesNew($insert_id); + } + ticketMailNewTicketAddress($insert_id, 'api'); + if ($assigned_to) { + $new_row = ticketRow($insert_id); + mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Ticket', notification = '" . escapeSql("Ticket " . ticketReference($new_row) . " - " . $new_row['ticket_subject'] . " has been assigned to you by the API ($api_key_name)") . "', + notification_action = '" . escapeSql('/agent/' . ticketUrl($insert_id, $client_id)) . "', notification_client_id = $client_id, notification_user_id = " . intval($assigned_to)); + ticketMailAssigned($assigned_to, [$insert_id], html_entity_decode($api_key_name, ENT_QUOTES, 'UTF-8')); + } + } + + // The number people see, and where its contact opens it + $new_row = ticketRow($insert_id); + $return_arr['ticket_reference'] = ticketReference($new_row); + $return_arr['ticket_url'] = ticketEmailUrl($new_row); + if ($template) { + $return_arr['ticket_tasks'] = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM tasks WHERE task_ticket_id = " . intval($insert_id)))[0]); + } + // Logging - logAudit("Ticket", "Create", "Created ticket $ticket_prefix$ticket_number $subject via API ($api_key_name)", $client_id, $insert_id); + logAudit("Ticket", "Create", "Created ticket $ticket_prefix$ticket_number $subject via API ($api_key_name)" . ($template ? " from template " . escapeSql($template['ticket_template_name']) : ''), $client_id, $insert_id); logAudit("API", "Success", "Created ticket $ticket_prefix$ticket_number $subject via API ($api_key_name)", $client_id); } diff --git a/functions/canned_response.php b/functions/canned_response.php index 7b690053e..c06b65a6f 100644 --- a/functions/canned_response.php +++ b/functions/canned_response.php @@ -329,7 +329,13 @@ function cannedResponsesForTicket($category_id, $user_id) { return cannedResponsePickerGroups([$category_id], $user_id); } -/* What the placeholders become on a ticket for $agent_name replying - lowercase name => value, as ticketPlaceholderFill() takes them; [] for no ticket. Raw. */ +/* + * What the placeholders become on a ticket for $agent_name replying - lowercase + * name => value, as ticketPlaceholderFill() takes them; [] for no ticket. Raw. + * A ticket raised from a template fills in its fill-ins' answers too - [Hostname] + * on one whose template asked for it - where they don't share a name with one of + * the placeholders above. One not answered stays for the agent to fill in. + */ function cannedResponseValues($ticket_id, $agent_name) { global $mysqli; $values = ticketPlaceholderValues($ticket_id); @@ -341,6 +347,13 @@ function cannedResponseValues($ticket_id, $agent_name) { $values['first name'] = $contact !== '' ? preg_split('/\s+/u', $contact)[0] : ''; $values['subject'] = (string) ($row['ticket_subject'] ?? ''); $values['agent'] = (string) $agent_name; + foreach (ticketFillInsStored('ticket', $ticket_id) as $answer) { + $key = strtolower($answer['name']); + $text = ticketFillInText($answer); + if ($text !== '' && !array_key_exists($key, $values)) { + $values[$key] = $text; + } + } return $values; } diff --git a/functions/ticket.php b/functions/ticket.php index e693152e7..db252eeb5 100644 --- a/functions/ticket.php +++ b/functions/ticket.php @@ -848,7 +848,7 @@ function ticketMailAgent($user_id) { * What an email to an agent about a ticket says - emailHtml()'s fields * (functions/email.php), all but who it is to and why. $row is ticketRow(). * $kind and its $opts: - * new it was raised - how: portal, email or scheduled + * new it was raised - how: portal, email, api or scheduled * replied the client replied - how: portal or email, message (the reply, HTML), by (who) * assigned it was assigned to them - by (who) * mentioned an internal note mentions them - by (who), message (the note, HTML), reply_id (the note, to open) @@ -866,7 +866,7 @@ function ticketAgentEmailContent(array $row, $kind, array $opts = []) { 'button' => ['label' => 'Open in ITFlow', 'url' => ticketAgentUrl($row)], ]; if ($kind === 'new') { - $how = ['portal' => ' in the client portal', 'email' => ' by email', 'scheduled' => ', as scheduled'][$opts['how'] ?? ''] ?? ''; + $how = ['portal' => ' in the client portal', 'email' => ' by email', 'api' => ' through the API', 'scheduled' => ', as scheduled'][$opts['how'] ?? ''] ?? ''; $who = ($opts['how'] ?? '') === 'scheduled' ? 'A recurring ticket was raised' : (trim((string) ($row['contact_name'] ?? '')) !== '' ? $row['contact_name'] . ' raised a ticket' : 'A ticket was raised'); $email['kind'] = 'New ticket'; $email['intro'] = [($opts['how'] ?? '') === 'scheduled' ? "$who for $client, as scheduled." : "$who$how for $client."]; diff --git a/includes/canned_response_form.php b/includes/canned_response_form.php index ae46fa47c..de9b0eb6c 100644 --- a/includes/canned_response_form.php +++ b/includes/canned_response_form.php @@ -75,7 +75,7 @@ $files = !empty($r['canned_response_id']) ? cannedResponseFiles($r['canned_respo -
Anything else in square brackets - [Date of visit] - stays as it is, and the agent is asked about it before sending.
+
On a ticket raised from a template, its answers fill in too - [Hostname] on a ticket whose template asked for one. Anything else in square brackets - [Date of visit] - stays as it is, and the agent is asked about it before sending.