" . stripslashes($name) . " created - its contract wasn't added: " . escapeHtml($contract_error) . " Add it from the client's Contracts.", 'warning');
} else {
flashAlert("Client " . stripslashes($name) . " created" . ($contract_id ? ", with contract " . escapeHtml($contract_name) . "" : ''));
}
redirect();
}
if (isset($_POST['edit_client'])) {
validateCSRFToken();
enforceUserPermission('module_client', 2);
require_once 'client_model.php';
$client_id = intval($_POST['client_id']);
enforceClientAccess();
// Its tax as it was, for what the form leaves out
$tax_set_sql = '';
if ($tax_input !== null) {
$tax_current = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT client_default_tax_id, client_tax_exempt, client_tax_exempt_number, client_tax_exempt_expires
FROM clients WHERE client_id = $client_id LIMIT 1")) ?: [];
$tax_set_sql = ", " . taxClientSetSql($tax_input, $tax_current);
}
// A referral typed in that isn't one yet is made one; one that is takes its own spelling
$referral = escapeSql(categoryEnsure('Referral', $_POST['referral'] ?? ''));
// Update client
mysqli_query($mysqli, "UPDATE clients SET client_name = '$name', client_type = '$type', client_website = '$website', client_referral = '$referral'" . ($reminders !== null ? ", client_reminders = $reminders" : '') . ($card_fee_exempt !== null ? ", client_card_fee_exempt = $card_fee_exempt" : '') . ", client_tax_id_number = '$tax_id_number', client_lead = $lead, client_abbreviation = '$abbreviation', client_notes = '$notes'$tax_set_sql WHERE client_id = $client_id");
// Tags - client tags only, and an admin can type a new one in (functions/tags.php)
tagSetForRecord('client', $client_id, (array) ($_POST['tags'] ?? []));
logAudit("Client", "Edit", "$session_name edited client $name", $client_id, $client_id);
// Another contract, started from a template picked on the Billing tab (functions/contract.php)
[$contract_id, $contract_name, $contract_error] = contractFromClientForm($client_id, $_POST);
if ($contract_error !== '') {
flashAlert("Client " . stripslashes($name) . " updated - the contract wasn't added: " . escapeHtml($contract_error), 'warning');
} else {
flashAlert("Client " . stripslashes($name) . " updated" . ($contract_id ? ", with new contract " . escapeHtml($contract_name) . "" : ''));
}
redirect();
}
if (isset($_POST['set_client_portal_signup'])) {
// Let people with an address at one of the client's Domains sign up for the portal, or stop it (functions/portal_invite.php)
validateCSRFToken();
enforceUserPermission('module_client', 2);
$client_id = intval($_POST['client_id'] ?? 0);
enforceClientAccess();
$on = !empty($_POST['portal_signup']);
$client = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT client_name, client_archived_at FROM clients WHERE client_id = $client_id LIMIT 1"));
if (!$client || !portalInvitesReady() || ($on && $client['client_archived_at'])) {
flashAlert("Signing up can't be changed for this client", 'error');
redirect();
}
$domains = portalSignupDomains($client_id);
if ($on && !$domains) {
flashAlert("Add the client's email domain under Domains first - people sign up with an address at one of them", 'error');
redirect();
}
if (!portalSignupSet($client_id, $on)) {
redirect();
}
$client_name = escapeSql($client['client_name']);
logAudit("Client", "Edit", "$session_name " . ($on ? 'let people at ' . escapeSql(implode(', ', $domains)) . " sign up for the client portal for $client_name" : "stopped people signing up for the client portal for $client_name"), $client_id, $client_id);
if ($on) {
flashAlert('People with an address at ' . escapeHtml(implode(', ', $domains)) . ' can sign up now' . (portalSignupOn() ? '' : ' - once signing up is turned on in Admin > Settings > Client portal'));
} else {
flashAlert('Signing up is off for ' . escapeHtml($client['client_name']) . ' - invite people instead');
}
redirect();
}
if (isset($_GET['archive_client'])) {
validateCSRFToken();
enforceUserPermission('module_client', 2);
$client_id = intval($_GET['archive_client']);
enforceClientAccess();
// Archive client
mysqli_query($mysqli, "UPDATE clients SET client_archived_at = NOW() WHERE client_id = $client_id");
// Stop recurring invoices
$sql_recurring_invoices = mysqli_query($mysqli, "SELECT recurring_invoice_id FROM recurring_invoices WHERE recurring_invoice_client_id = $client_id AND recurring_invoice_status = 1");
while ($row = mysqli_fetch_assoc($sql_recurring_invoices)) {
$recurring_invoice_id = intval($row['recurring_invoice_id']);
mysqli_query($mysqli,"UPDATE recurring_invoices SET recurring_invoice_status = 0 WHERE recurring_invoice_id = $recurring_invoice_id AND recurring_invoice_client_id = $client_id");
mysqli_query($mysqli,"INSERT INTO history SET history_status = 0, history_description = 'Recurring Invoice inactive as client archived', history_recurring_invoice_id = $recurring_invoice_id");
}
// Get Client Name
$client_name = escapeSql(getFieldById('clients', $client_id, 'client_name'));
// Nothing more is signed on their contracts - a request still out, or waiting on our countersignature, is withdrawn
$contracts_withdrawn = contractSignatureWithdrawStale($client_id);
logAudit("Client", "Archive", "$session_name archived client $client_name" . ($contracts_withdrawn ? " - $contracts_withdrawn contract signing request" . ($contracts_withdrawn === 1 ? '' : 's') . " withdrawn" : ''), $client_id, $client_id);
flashAlert("Client " . stripslashes($client_name) . " archived" . ($contracts_withdrawn ? " - contract signing requests to them were withdrawn" : ''), 'error');
redirect();
}
if (isset($_GET['restore_client'])) {
validateCSRFToken();
enforceUserPermission('module_client', 2);
$client_id = intval($_GET['restore_client']);
enforceClientAccess();
// Get Client Name
$client_name = escapeSql(getFieldById('clients', $client_id, 'client_name'));
mysqli_query($mysqli, "UPDATE clients SET client_archived_at = NULL WHERE client_id = $client_id");
logAudit("Client", "Restore", "$session_name restored client $client_name", $client_id);
flashAlert("Client " . stripslashes($client_name) . " restored");
redirect();
}
if (isset($_GET['delete_client'])) {
validateCSRFToken();
enforceUserPermission('module_client', 3);
$client_id = intval($_GET['delete_client']);
enforceClientAccess();
// Get Client Name
$client_name = escapeSql(getFieldById('clients', $client_id, 'client_name'));
// Their payments and other income go with them - not once any has cleared on a reconciled statement
$lock = reconcileClientLock($client_id);
if ($lock) {
flashAlert(escapeHtml('Money from ' . stripslashes($client_name) . " cleared on the {$lock['account_name']} statement to " . date('M j, Y', strtotime($lock['reconciliation_statement_date'])) . ' - keep the client archived instead, or undo that reconciliation to delete them'), 'error');
redirect();
}
// Delete Associations
// Delete Client Data
mysqli_query($mysqli, "DELETE FROM certificates WHERE certificate_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM documents WHERE document_client_id = $client_id");
// Delete Contacts
mysqli_query($mysqli, "DELETE FROM contacts WHERE contact_client_id = $client_id");
// Delete Assets
mysqli_query($mysqli, "DELETE FROM assets WHERE asset_client_id = $client_id");
// Delete Racks (their items go with them)
mysqli_query($mysqli, "DELETE FROM racks WHERE rack_client_id = $client_id");
// Delete Projects - their tickets are the client's, and go below
mysqli_query($mysqli, "DELETE FROM projects WHERE project_client_id = $client_id");
// Delete Domains and associated records
mysqli_query($mysqli, "DELETE FROM domains WHERE domain_client_id = $client_id");
mysqli_query($mysqli, "DELETE calendar_event_attendees FROM calendar_event_attendees JOIN calendar_events ON event_id = attendee_event_id WHERE event_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM calendar_events WHERE event_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM files WHERE file_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM folders WHERE folder_client_id = $client_id");
//Delete Invoices and Invoice Referencing data
$sql = mysqli_query($mysqli, "SELECT invoice_id FROM invoices WHERE invoice_client_id = $client_id");
while($row = mysqli_fetch_assoc($sql)) {
$invoice_id = $row['invoice_id'];
mysqli_query($mysqli, "DELETE FROM invoice_items WHERE item_invoice_id = $invoice_id");
mysqli_query($mysqli, "DELETE FROM payments WHERE payment_invoice_id = $invoice_id");
mysqli_query($mysqli, "DELETE FROM history WHERE history_invoice_id = $invoice_id");
}
mysqli_query($mysqli, "DELETE FROM invoices WHERE invoice_client_id = $client_id");
// Delete Locations and location tags
mysqli_query($mysqli, "DELETE FROM locations WHERE location_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM credentials WHERE credential_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM logs WHERE log_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM networks WHERE network_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM notifications WHERE notification_client_id = $client_id");
//Delete Quote and related items
$sql = mysqli_query($mysqli, "SELECT quote_id FROM quotes WHERE quote_client_id = $client_id");
while($row = mysqli_fetch_assoc($sql)) {
$quote_id = $row['quote_id'];
mysqli_query($mysqli, "DELETE FROM quote_items WHERE item_quote_id = $quote_id");
}
mysqli_query($mysqli, "DELETE FROM quotes WHERE quote_client_id = $client_id");
// Delete Recurring Invoices and associated items
$sql = mysqli_query($mysqli, "SELECT recurring_invoice_id FROM recurring_invoices WHERE recurring_invoice_client_id = $client_id");
while($row = mysqli_fetch_assoc($sql)) {
$recurring_invoice_id = $row['recurring_invoice_id'];
mysqli_query($mysqli, "DELETE FROM recurring_invoice_items WHERE item_recurring_invoice_id = $recurring_invoice_id");
}
mysqli_query($mysqli, "DELETE FROM recurring_invoices WHERE recurring_invoice_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM revenues WHERE revenue_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM recurring_tickets WHERE recurring_ticket_client_id = $client_id");
// Delete Services
mysqli_query($mysqli, "DELETE FROM services WHERE service_client_id = $client_id");
// Delete Shared Items
mysqli_query($mysqli, "DELETE FROM shared_items WHERE item_client_id = $client_id");
// Delete Software
mysqli_query($mysqli, "DELETE FROM software WHERE software_client_id = $client_id");
// Delete tickets and related data
$sql = mysqli_query($mysqli, "SELECT ticket_id FROM tickets WHERE ticket_client_id = $client_id");
while($row = mysqli_fetch_assoc($sql)) {
$ticket_id = $row['ticket_id'];
mysqli_query($mysqli, "DELETE FROM ticket_replies WHERE ticket_reply_ticket_id = $ticket_id");
mysqli_query($mysqli, "DELETE FROM ticket_views WHERE view_ticket_id = $ticket_id");
}
mysqli_query($mysqli, "DELETE FROM tickets WHERE ticket_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM trips WHERE trip_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM contracts WHERE contract_client_id = $client_id");
mysqli_query($mysqli, "DELETE FROM vendors WHERE vendor_client_id = $client_id");
//Delete Client Files
removeDirectory("../uploads/clients/$client_id");
//Finally Remove the Client
mysqli_query($mysqli, "DELETE FROM clients WHERE client_id = $client_id");
// Don't leave the company's own-client designation pointing at a client that is gone
mysqli_query($mysqli, "UPDATE settings SET config_internal_client_id = 0 WHERE config_internal_client_id = $client_id");
logAudit("Client", "Delete", "$session_name deleted Client $client_name and all associated data");
flashAlert("Client " . stripslashes($client_name) . " deleted, with everything that was theirs", 'error');
redirect('clients.php');
}
if (isExportRequest('export_clients')) {
validateCSRFToken();
enforceUserPermission('module_client');
$format = resolveExportFormat($_POST['export_clients']);
// Filters inherited from the clients page - mirrors agent/clients.php
$filter_summary = [];
// Leads Filter
if (isset($_POST['leads']) && $_POST['leads'] == 1) {
$leads_query = "AND client_lead = 1";
$filter_summary['Showing'] = 'Leads';
$export_label = 'Leads';
} else {
$leads_query = "AND client_lead = 0";
$export_label = 'Clients';
}
// Tags Filter
if (isset($_POST['tags']) && is_array($_POST['tags']) && !empty($_POST['tags'])) {
$tag_filter = implode(",", array_map('intval', $_POST['tags']));
$tag_query = "AND tags.tag_id IN ($tag_filter)";
$tag_names = [];
$sql_tags = mysqli_query($mysqli, "SELECT tag_name FROM tags WHERE tag_id IN ($tag_filter) ORDER BY tag_name ASC");
while ($tag_row = mysqli_fetch_assoc($sql_tags)) {
$tag_names[] = $tag_row['tag_name'];
}
$filter_summary['Tags'] = implode(', ', $tag_names);
} else {
// Default - any
$tag_query = '';
}
// Industry Filter
if (!empty($_POST['industry'])) {
$industry_query = "AND (clients.client_type = '" . escapeSql($_POST['industry']) . "')";
$filter_summary['Industry'] = $_POST['industry'];
} else {
// Default - any
$industry_query = '';
}
// Referral Filter
if (!empty($_POST['referral'])) {
$referral_query = "AND (clients.client_referral = '" . escapeSql($_POST['referral']) . "')";
$filter_summary['Referral'] = $_POST['referral'];
} else {
// Default - any
$referral_query = '';
}
// Archived Filter
if (isset($_POST['archived']) && $_POST['archived'] == 1) {
$archive_query = "client_archived_at IS NOT NULL";
$filter_summary['Archived'] = 'Archived only';
} else {
$archive_query = "client_archived_at IS NULL";
}
// Date Filter - all-time sentinels from filter_header.php are left in place,
// they match everything anyway
$dtf = escapeSql(!empty($_POST['dtf']) ? $_POST['dtf'] : '1970-01-01');
$dtt = escapeSql(!empty($_POST['dtt']) ? $_POST['dtt'] : '2099-12-31');
$date_range = formatExportDateRange($dtf, $dtt);
if ($date_range) {
$filter_summary['Created'] = $date_range;
}
// Search Filter
$q = escapeSql($_POST['q'] ?? '');
$phone_query = preg_replace('/\D/', '', $q);
if (!empty($q)) {
$filter_summary['Search'] = $_POST['q'];
}
// Get records from database - same shape as the clients page list query
$sql = mysqli_query(
$mysqli,
"SELECT clients.*, contacts.*, locations.*
FROM clients
LEFT JOIN contacts ON clients.client_id = contacts.contact_client_id AND contact_primary = 1
LEFT JOIN locations ON clients.client_id = locations.location_client_id AND location_primary = 1
LEFT JOIN client_tags ON client_tags.client_id = clients.client_id
LEFT JOIN tags ON tags.tag_id = client_tags.tag_id
WHERE (client_name LIKE '%$q%' OR client_abbreviation LIKE '%$q%' OR client_type LIKE '%$q%' OR client_referral LIKE '%$q%'
OR contact_email LIKE '%$q%' OR contact_name LIKE '%$q%' OR contact_phone LIKE '%$phone_query%'
OR contact_mobile LIKE '%$phone_query%' OR location_address LIKE '%$q%'
OR location_city LIKE '%$q%' OR location_state LIKE '%$q%' OR location_zip LIKE '%$q%' OR location_country LIKE '%$q%'
OR tag_name LIKE '%$q%' OR client_tax_id_number LIKE '%$q%')
AND $archive_query
AND DATE(client_created_at) BETWEEN '$dtf' AND '$dtt'
$leads_query
" . clientScopeSql('clients.client_id') . "
$tag_query
$industry_query
$referral_query
GROUP BY client_id
ORDER BY client_name ASC"
);
$num_rows = mysqli_num_rows($sql);
guardExportHasRows($num_rows, 'clients');
if ($num_rows > 0) {
guardExportPdfRowCount($format, $num_rows);
$export = beginExport('clients', $format, "$session_company_name-$export_label", $export_label, summarizeExportFilters($filter_summary));
while ($row = mysqli_fetch_assoc($sql)) {
$tax_client = taxClient($row['client_id']);
$row['client_tax_rate'] = intval($tax_client['default_id']) ? taxLabel(taxRow($tax_client['default_id'])) : '';
$row['client_tax_exempt_display'] = $tax_client['exempt'] ? 'Yes' : '';
$row['client_tax_exempt_number'] = $tax_client['exempt'] ? $tax_client['number'] : '';
$row['client_tax_exempt_expires'] = $tax_client['exempt'] ? $tax_client['expires'] : '';
addExportRow($export, $row);
}
finishExport($export);
logAudit("Client", "Export", "$session_name exported $num_rows client(s) to a " . strtoupper($format) . " file");
}
exit;
}
if (isset($_POST["import_clients_csv"])) {
validateCSRFToken();
enforceUserPermission('module_client', 2);
// Read by its headings - any order, any left out (functions/import.php)
[$rows, $ignored, $error] = importCsvRows('clients');
if ($error !== '') {
flashAlert(escapeHtml($error), 'error');
redirect();
}
$row_count = $duplicate_count = $no_name_count = $terms_count = 0;
$notes = ['dates left empty (use YYYY-MM-DD)' => 0, 'tax rates not found' => 0, 'currencies not a 3-letter code' => 0];
// A phone's dial code and number - "+1 (412) 555-0100" as the exports write it (functions/import.php)
$phone = function ($value, $column) {
[$code, $number] = importPhone($value);
return "{$column}_country_code = '" . escapeSql($code) . "', $column = '" . escapeSql(mb_substr($number, 0, 200)) . "'";
};
foreach ($rows as $row) {
$in = function ($key) use ($row) {
return (string) ($row[$key] ?? '');
};
if ($in('client_name') === '') {
$no_name_count++;
continue;
}
$name = escapeSql(mb_substr($in('client_name'), 0, 200));
if (mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM clients WHERE client_name = '$name'")) > 0) {
$duplicate_count++;
continue;
}
$industry = escapeSql(mb_substr($in('client_type'), 0, 200));
// A referral that isn't one yet is made one; one that is takes its own spelling
$referral = escapeSql(categoryEnsure('Referral', $in('client_referral')));
$website = escapeSql(mb_substr(preg_replace("(^https?://)", "", $in('client_website')), 0, 200));
$lead = importYes($in('client_lead')) ? 1 : 0;
$abbreviation = escapeSql(mb_substr($in('client_abbreviation') !== '' ? $in('client_abbreviation') : shortenClientName($in('client_name')), 0, 6));
$currency = strtoupper($in('client_currency_code'));
if ($currency !== '' && !preg_match('/^[A-Z]{3}$/', $currency)) {
$notes['currencies not a 3-letter code']++;
$currency = '';
}
$currency = escapeSql($currency !== '' ? $currency : $session_company_currency);
$tax_id_number = escapeSql(mb_substr($in('client_tax_id_number'), 0, 255));
$client_notes = escapeSql($in('client_notes'));
// Its tax rate and exemption, as the Billing tab sets them (functions/tax.php)
$tax_input = [];
if ($in('client_tax_rate') !== '') {
$tax_input['default_tax_id'] = importTaxId($in('client_tax_rate'));
$notes['tax rates not found'] += $tax_input['default_tax_id'] ? 0 : 1;
}
if (importYes($in('client_tax_exempt'))) {
$expires = importDate($in('client_tax_exempt_expires'));
$notes['dates left empty (use YYYY-MM-DD)'] += $expires === null ? 1 : 0;
$tax_input += ['tax_exempt' => true, 'tax_exempt_number' => $in('client_tax_exempt_number'), 'tax_exempt_expires' => (string) $expires];
}
mysqli_query($mysqli, "INSERT INTO clients SET client_name = '$name', client_type = '$industry', client_website = '$website', client_referral = '$referral', client_lead = $lead, client_currency_code = '$currency', client_tax_id_number = '$tax_id_number', client_abbreviation = '$abbreviation', client_notes = '$client_notes'"
. ($tax_input ? ', ' . taxClientSetSql($tax_input) : '') . billingClientLegacySql());
$client_id = mysqli_insert_id($mysqli);
if (!file_exists("../uploads/clients/$client_id")) {
mkdir("../uploads/clients/$client_id");
file_put_contents("../uploads/clients/$client_id/index.php", "");
}
// Its primary location and contact - when the row has them
if ($in('location_name') . $in('location_phone') . $in('location_address') . $in('location_city') . $in('location_state') . $in('location_zip') . $in('location_country') !== '') {
$location_name = escapeSql(mb_substr($in('location_name') !== '' ? $in('location_name') : 'Primary', 0, 200));
mysqli_query($mysqli, "INSERT INTO locations SET location_name = '$location_name', location_address = '" . escapeSql($in('location_address')) . "', location_city = '" . escapeSql($in('location_city')) . "',
location_state = '" . escapeSql($in('location_state')) . "', location_zip = '" . escapeSql($in('location_zip')) . "', location_country = '" . escapeSql($in('location_country')) . "',
" . $phone($in('location_phone'), 'location_phone') . ", location_primary = 1, location_client_id = $client_id");
}
if ($in('contact_name') . $in('contact_email') . $in('contact_phone') . $in('contact_mobile') !== '') {
mysqli_query($mysqli, "INSERT INTO contacts SET contact_name = '" . escapeSql($in('contact_name') !== '' ? $in('contact_name') : $in('contact_email')) . "', contact_title = '" . escapeSql($in('contact_title')) . "',
" . $phone($in('contact_phone'), 'contact_phone') . ", contact_extension = '" . preg_replace('/\D/', '', $in('contact_extension')) . "', " . $phone($in('contact_mobile'), 'contact_mobile') . ",
contact_email = '" . escapeSql($in('contact_email')) . "', contact_primary = 1, contact_important = 1, contact_client_id = $client_id");
}
if ($in('client_tags') !== '') {
tagSetForRecord('client', $client_id, importTagIds('client', $in('client_tags')));
}
// An hourly rate or payment terms other than the defaults become the client's Standard terms contract (functions/contract.php)
$hourly_rate = $in('hourly_rate') !== '' ? floatval(str_replace([',', '$'], '', $in('hourly_rate'))) : null;
$payment_terms = is_numeric($in('payment_terms')) ? max(0, min(365, intval($in('payment_terms')))) : null;
if (function_exists('contractsReady') && contractsReady() && contractStandardTermsAdd($client_id, $hourly_rate, $payment_terms, [], null, 'Made by the client import from its Hourly Rate and Payment Terms.')) {
$terms_count++;
}
$row_count++;
}
$left_out = importNotesText($notes + ['rows with no Client Name' => $no_name_count]);
logAudit("Client", "Import", "$session_name imported $row_count client(s) via CSV file, $duplicate_count duplicate(s) found" . ($left_out !== '' ? " - $left_out" : ''));
flashAlert("$row_count Client(s) added, $duplicate_count duplicate(s) found"
. ($terms_count ? ", $terms_count with a Standard terms contract for their own rate or payment terms" : '')
. ($left_out !== '' ? ' - ' . escapeHtml($left_out) : '')
. ($ignored ? '. Columns not imported: ' . escapeHtml(implode(', ', array_slice($ignored, 0, 8)) . (count($ignored) > 8 ? ' and ' . (count($ignored) - 8) . ' more' : '')) : ''),
$left_out !== '' || $ignored ? 'warning' : 'success');
redirect();
}
if (isset($_GET['download_clients_csv_template'])) {
enforceUserPermission('module_client');
importTemplateDownload('clients', 'Clients-Template.csv');
}
if (isset($_POST['bulk_add_client_ticket'])) {
validateCSRFToken();
enforceUserPermission('module_support', 2);
// Every client picked, checked before any ticket is made
$client_ids = array_values(array_unique(array_filter(array_map('intval', (array) ($_POST['client_ids'] ?? [])))));
foreach ($client_ids as $client_id) {
enforceClientAccess($client_id);
}
$assigned_to = ticketAgentId($_POST['bulk_assigned_to'] ?? 0);
$ticket_status = $assigned_to ? TICKET_STATUS_OPEN : TICKET_STATUS_NEW;
$subject_input = trim((string) ($_POST['bulk_subject'] ?? ''));
$priority = escapeSql(ticketPriority($_POST['bulk_priority'] ?? ''));
$category_id = ticketCategoryId($_POST['bulk_category'] ?? 0);
$details = mysqli_real_escape_string($mysqli, (string) ($_POST['bulk_details'] ?? ''));
$project_id = intval($_POST['bulk_project'] ?? 0);
$use_primary_contact = !empty($_POST['use_primary_contact']);
$ticket_template_id = intval($_POST['bulk_ticket_template_id'] ?? 0);
$billable = intval($_POST['bulk_billable'] ?? 0) ? 1 : 0;
$ticket_prefix = escapeSql($config_ticket_prefix);
// A template fills in what was left empty, and what it asks for is answered once for them all
$template = ticketTemplateRow($ticket_template_id);
$fill_in_answers = null;
if ($template) {
$subject_input = $subject_input !== '' ? $subject_input : (string) $template['ticket_template_subject'];
$details = trim((string) ($_POST['bulk_details'] ?? '')) !== '' ? $details : mysqli_real_escape_string($mysqli, (string) $template['ticket_template_details']);
$fill_in_answers = ticketFillInAnswers(ticketTemplateFillInsFor($ticket_template_id), $_POST['fill_ins'] ?? [], $fill_in_error);
if ($fill_in_answers === null) {
flashAlert($fill_in_error, 'error');
redirect();
}
}
if ($subject_input === '') {
flashAlert('The tickets need a subject.', 'error');
redirect();
}
$subject = escapeSql(mb_substr($subject_input, 0, 500));
$ticket_ids = [];
foreach ($client_ids as $client_id) {
if (!getFieldById('clients', $client_id, 'client_name')) {
continue;
}
// Each client's primary contact, when asked; a shared project only when it is internal
$contact_id = 0;
if ($use_primary_contact) {
$primary = mysqli_fetch_row(mysqli_query($mysqli, "SELECT contact_id FROM contacts WHERE contact_client_id = $client_id AND contact_primary = 1 AND contact_archived_at IS NULL LIMIT 1"));
$contact_id = $primary ? intval($primary[0]) : 0;
}
$ticket_project_id = ticketClientOwnedId('project', $project_id, $client_id);
// 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);
$url_key = randomString(32);
mysqli_query($mysqli, "INSERT INTO tickets SET ticket_prefix = '$ticket_prefix', ticket_number = $ticket_number, ticket_source = 'Agent', ticket_category = $category_id,
ticket_subject = '$subject', ticket_details = '$details', ticket_priority = '$priority', ticket_billable = $billable, ticket_status = $ticket_status,
ticket_created_by = $session_user_id, ticket_assigned_to = $assigned_to, ticket_contact_id = $contact_id, ticket_url_key = '$url_key',
ticket_client_id = $client_id, ticket_invoice_id = 0, ticket_project_id = $ticket_project_id");
$ticket_id = mysqli_insert_id($mysqli);
applyTicketSla($ticket_id);
// Tasks typed in, then the template's - its tags and due time too, and [Client] filled in
foreach ((array) ($_POST['tasks'] ?? []) as $task) {
$task_name = escapeSql(mb_substr(trim((string) $task), 0, 255));
if ($task_name !== '') {
mysqli_query($mysqli, "INSERT INTO tasks SET task_name = '$task_name', task_ticket_id = $ticket_id");
}
}
if ($template) {
ticketTemplateFinish($ticket_id, $ticket_template_id, true, true, $fill_in_answers);
}
logAudit("Ticket", "Create", "$session_name created ticket $ticket_prefix$ticket_number - $subject", $client_id, $ticket_id);
triggerCustomAction('ticket_create', $ticket_id);
$ticket_ids[] = $ticket_id;
}
$count = count($ticket_ids);
// The agent they went to hears once, about all of them
if ($ticket_ids && $assigned_to) {
ticketMailAssigned($assigned_to, $ticket_ids, stripslashes($session_name));
}
logAudit("Ticket", "Bulk Create", "$session_name created $count ticket(s) for the clients picked");
flashAlert("Created $count ticket" . ($count === 1 ? '' : 's') . " for the clients picked");
redirect();
}
if (isset($_POST['bulk_edit_client_industry'])) {
validateCSRFToken();
enforceUserPermission('module_client', 2);
$industry = escapeSql($_POST['bulk_industry']);
if (isset($_POST['client_ids'])) {
// Check every selected client before changing any of them
foreach ($_POST['client_ids'] as $client_id) {
enforceClientAccess(intval($client_id));
}
$count = count($_POST['client_ids']);
foreach($_POST['client_ids'] as $client_id) {
$client_id = intval($client_id);
$sql = mysqli_query($mysqli,"SELECT client_name FROM clients WHERE client_id = $client_id");
$row = mysqli_fetch_assoc($sql);
$client_name = escapeSql($row['client_name']);
mysqli_query($mysqli,"UPDATE clients SET client_type = '$industry' WHERE client_id = $client_id");
logAudit("Client", "Edit", "$session_name set Industry to $industry for $client_name", $client_id);
}
logAudit("Client", "Bulk Edit", "$session_name set the industry $industry for $count client(s)", $client_id);
flashAlert("Industry set to " . stripslashes($industry) . " on $count client" . ($count === 1 ? '' : 's'));
}
redirect();
}
if (isset($_POST['bulk_edit_client_referral'])) {
validateCSRFToken();
enforceUserPermission('module_client', 2);
$referral = escapeSql($_POST['bulk_referral']);
if (isset($_POST['client_ids'])) {
// Check every selected client before changing any of them
foreach ($_POST['client_ids'] as $client_id) {
enforceClientAccess(intval($client_id));
}
$count = count($_POST['client_ids']);
foreach($_POST['client_ids'] as $client_id) {
$client_id = intval($client_id);
$sql = mysqli_query($mysqli,"SELECT client_name FROM clients WHERE client_id = $client_id");
$row = mysqli_fetch_assoc($sql);
$client_name = escapeSql($row['client_name']);
mysqli_query($mysqli,"UPDATE clients SET client_referral = '$referral' WHERE client_id = $client_id");
logAudit("Client", "Edit", "$session_name set Referral to $referral for $client_name", $client_id);
}
logAudit("Client", "Bulk Edit", "$session_name set the referral $referral for $count client(s)", $client_id);
flashAlert("Referral set to " . stripslashes($referral) . " on $count client" . ($count === 1 ? '' : 's'));
}
redirect();
}
if (isset($_POST['bulk_add_client_contract'])) {
// A contract from a template for each client ticked - active from the day picked (functions/contract.php)
validateCSRFToken();
enforceUserPermission('module_client', 2);
enforceUserPermission('module_sales', 2);
$client_ids = array_values(array_unique(array_filter(array_map('intval', (array) ($_POST['client_ids'] ?? [])))));
$template = contractTemplateRow(intval($_POST['contract_template_id'] ?? 0));
if (!contractClientPickReady() || !$template || !empty($template['contract_template_archived_at'])) {
flashAlert("Pick a contract template.", 'error');
redirect();
}
// Check every selected client before starting any
foreach ($client_ids as $client_id) {
enforceClientAccess($client_id);
}
$made = 0;
$not_made = [];
foreach ($client_ids as $client_id) {
[$contract_id, $contract_name, $contract_error] = contractFromClientForm($client_id, ['contract_template_id' => intval($template['contract_template_id']), 'contract_start' => (string) ($_POST['contract_start'] ?? '')]);
if ($contract_id) {
$made++;
} else {
$not_made[] = getFieldById('clients', $client_id, 'client_name') . ($contract_error !== '' ? " ($contract_error)" : '');
}
}
if ($made) {
logAudit("Contract", "Bulk Create", "$session_name started contract " . escapeSql($template['contract_template_name']) . " from its template for $made client(s)", 0);
}
flashAlert("Contract " . escapeHtml($template['contract_template_name']) . " started for $made client" . ($made === 1 ? '' : 's')
. ($not_made ? " - not for " . escapeHtml(implode(', ', $not_made)) : ''), $not_made ? 'warning' : 'success');
redirect();
}
if (isset($_POST['bulk_send_client_email']) && isset($_POST['client_ids'])) {
validateCSRFToken();
enforceUserPermission('module_client', 1);
$client_ids = array_map('intval', $_POST['client_ids']);
$count = count($client_ids);
// Check every selected client before queuing anything
foreach ($client_ids as $client_id) {
enforceClientAccess($client_id);
}
// Build contact type filters
$filters = [];
if (!empty($_POST['primary_contacts'])) {
$filters[] = "contact_primary = 1";
}
if (!empty($_POST['important_contacts'])) {
$filters[] = "contact_important = 1";
}
if (!empty($_POST['billing_contacts'])) {
$filters[] = "contact_billing = 1";
}
if (!empty($_POST['technical_contacts'])) {
$filters[] = "contact_technical = 1";
}
$contact_filter_query = '';
if (!empty($filters)) {
$contact_filter_query = ' AND (' . implode(' OR ', $filters) . ')';
}
// Prepare client ID list for SQL
$client_ids_str = implode(',', $client_ids);
// The clients' contacts that match, not archived - emailBulkSend() sends each address once
$result = mysqli_query($mysqli, "SELECT contact_email, contact_name, client_name FROM contacts
JOIN clients ON client_id = contact_client_id
WHERE contact_client_id IN ($client_ids_str) AND contact_archived_at IS NULL
$contact_filter_query
ORDER BY client_name ASC, contact_primary DESC, contact_name ASC");
$recipients = [];
$company_name = (string) (billingCompany()['company_name'] ?? '');
while ($row = mysqli_fetch_assoc($result)) {
$recipients[] = ['email' => trim((string) $row['contact_email']), 'name' => (string) $row['contact_name'],
'footnote' => "You received this as a contact for {$row['client_name']} with $company_name."];
}
emailBulkPost($recipients, count($recipients));
}
if (isset($_POST['bulk_archive_clients'])) {
validateCSRFToken();
enforceUserPermission('module_client', 2);
if (isset($_POST['client_ids'])) {
// Check every selected client before changing any of them
foreach ($_POST['client_ids'] as $client_id) {
enforceClientAccess(intval($client_id));
}
$count = 0;
foreach ($_POST['client_ids'] as $client_id) {
$client_id = intval($client_id);
$sql = mysqli_query($mysqli,"SELECT client_name FROM clients WHERE client_id = $client_id");
$row = mysqli_fetch_assoc($sql);
$client_name = escapeSql($row['client_name']);
mysqli_query($mysqli,"UPDATE clients SET client_archived_at = NOW() WHERE client_id = $client_id");
$contracts_withdrawn = contractSignatureWithdrawStale($client_id);
logAudit("Client", "Archive", "$session_name archived $client_name" . ($contracts_withdrawn ? " - $contracts_withdrawn contract signing request" . ($contracts_withdrawn === 1 ? '' : 's') . " withdrawn" : ''), $client_id);
$count++;
}
logAudit("Client", "Bulk Archive", "$session_name archived $count clients", $client_id);
flashAlert("Archived $count client(s)", 'error');
}
redirect();
}
if (isset($_POST['bulk_unarchive_clients'])) {
validateCSRFToken();
enforceUserPermission('module_client', 2);
if (isset($_POST['client_ids'])) {
// Check every selected client before changing any of them
foreach ($_POST['client_ids'] as $client_id) {
enforceClientAccess(intval($client_id));
}
$count = count($_POST['client_ids']);
foreach ($_POST['client_ids'] as $client_id) {
$client_id = intval($client_id);
$sql = mysqli_query($mysqli,"SELECT client_name FROM clients WHERE client_id = $client_id");
$row = mysqli_fetch_assoc($sql);
$client_name = escapeSql($row['client_name']);
mysqli_query($mysqli,"UPDATE clients SET client_archived_at = NULL WHERE client_id = $client_id");
logAudit("Client", "Restore", "$session_name restored $client_name", $client_id);
}
logAudit("Client", "Bulk Restore", "$session_name restored $count client(s)", $client_id);
flashAlert("You restored $count client(s)");
}
redirect();
}
/*
* Export a client (functions/client_export.php): the documentation PDF, or
* everything as a zip. Client access is the floor; each section then needs
* its own module, so a role missing one goes without that section, and
* passwords only go out locked with a password the agent sets.
*/
if (isset($_POST['export_client_pdf']) || isset($_POST['export_client_archive'])) {
validateCSRFToken();
enforceUserPermission('module_client');
$client_id = intval($_POST['client_id'] ?? 0);
enforceClientAccess();
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT client_name FROM clients WHERE client_id = $client_id"));
if (!$row) {
flashAlert('That client no longer exists.', 'error');
redirect('clients.php');
}
$client_name = $row['client_name'];
$kind = isset($_POST['export_client_archive']) ? 'archive' : 'pdf';
$options = clientExportOptions($_POST, $kind);
if ($error = clientExportCheck($options, $_POST)) {
flashAlert($error, 'error');
redirect();
}
// A big client's files take a while to gather; the request is the export
set_time_limit(0);
$logged = escapeSql(clientExportSummary($options)) . ($options['secrets'] ? ', including passwords and 2FA secrets' : '');
if ($kind === 'pdf') {
try {
$pdf = clientExportPdf($client_id, $options);
} catch (Exception $exception) {
error_log('ITFlow client export PDF: ' . $exception->getMessage());
flashAlert('The PDF could not be made. Try again with fewer sections, or use the full export.', 'error');
redirect();
}
logAudit("Client", "Export", "$session_name exported the documentation PDF for " . escapeSql($client_name) . " ($logged)", $client_id, $client_id);
clientExportSendHeaders(clientExportFileName($client_name, 'IT Documentation', 'pdf'), 'application/pdf', strlen($pdf));
echo $pdf;
exit;
}
$zip_path = clientExportArchive($client_id, $options, $error);
if (!$zip_path) {
flashAlert($error, 'error');
redirect();
}
logAudit("Client", "Export", "$session_name exported all data for " . escapeSql($client_name) . " ($logged" . ($options['archived'] ? ', archived records' : '')
. ($options['internal'] ? ', internal notes' : '') . ")", $client_id, $client_id);
clientExportSendHeaders(clientExportFileName($client_name, 'Export', 'zip'), 'application/zip', filesize($zip_path));
while (ob_get_level()) {
ob_end_clean();
}
readfile($zip_path);
@unlink($zip_path);
exit;
}