Settings > Company Details * has it (office 0); the others are the offices table, listed and changed in * the Offices section of that page (admin/modals/office/office.php, handled in * admin/post/office.php). Contact details only - name, address, phone and * email. The company name, logo, invoices, quotes and contracts stay the main * company's. * * Each agent works from one (users.user_office_id; 0 is the main one), picked * in the office's window or in Admin > Users - an invite carries it until it is * accepted (user_invites.invite_office_id). Where it shows: * - Trips: a new trip starts From the driver's office, From offers every * office, and on a new trip changing the driver moves an untouched From to * theirs (agent/modals/trip/trip_form.php). * - Ticket emails: the foot shows the address, phone and email of the office * of the agent on the ticket - the one assigned, else the one sending it * ('office' on the email, read by emailHtml in functions/email.php). * With no other office, everything is as it was. */ /** Whether the 3.9.0 update has run. */ function officesReady() { global $mysqli; static $ready = null; if ($ready === null) { if (defined('CURRENT_DATABASE_VERSION')) { $version = CURRENT_DATABASE_VERSION; } else { $result = mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1"); $version = $result && ($row = mysqli_fetch_row($result)) ? (string) $row[0] : '0'; } $ready = version_compare($version, '3.9.0', '>='); } return $ready; } /** What is read once a request - the offices, and who works where. officesForget() after a change. */ function &officesCache() { static $cache = []; return $cache; } function officesForget() { $cache = &officesCache(); $cache = []; } /** The other offices - office_id => office, by name. $archived takes in the removed ones. */ function offices($archived = false) { global $mysqli; $cache = &officesCache(); if (!isset($cache['offices'])) { $cache['offices'] = []; if (officesReady()) { $sql = mysqli_query($mysqli, "SELECT office_id, office_name, office_address, office_city, office_state, office_zip, office_country, office_phone_country_code, office_phone, office_email, office_archived_at FROM offices ORDER BY office_name"); while ($row = mysqli_fetch_assoc($sql)) { $cache['offices'][intval($row['office_id'])] = officeShape($row); } } } return $archived ? $cache['offices'] : array_filter($cache['offices'], function ($office) { return $office['archived_at'] === null; }); } /** An office in one shape - an offices row, or the company for the main one. */ function officeShape(array $row) { $office = [ 'office_id' => intval($row['office_id'] ?? 0), 'name' => (string) ($row['office_name'] ?? ''), 'address' => (string) ($row['office_address'] ?? ''), 'city' => (string) ($row['office_city'] ?? ''), 'state' => (string) ($row['office_state'] ?? ''), 'zip' => (string) ($row['office_zip'] ?? ''), 'country' => (string) ($row['office_country'] ?? ''), 'phone_country_code' => (string) ($row['office_phone_country_code'] ?? ''), 'phone' => (string) ($row['office_phone'] ?? ''), 'email' => (string) ($row['office_email'] ?? ''), 'archived_at' => $row['office_archived_at'] ?? null, ]; $office['phone_display'] = $office['phone'] !== '' ? formatPhoneNumber($office['phone'], $office['phone_country_code']) : ''; return $office; } /** The main office - the company, as Company Details has it. */ function officeMain() { $company = billingCompany(); return officeShape([ 'office_id' => 0, 'office_name' => 'Main office', 'office_address' => $company['company_address'] ?? '', 'office_city' => $company['company_city'] ?? '', 'office_state' => $company['company_state'] ?? '', 'office_zip' => $company['company_zip'] ?? '', 'office_country' => $company['company_country'] ?? '', 'office_phone_country_code' => $company['company_phone_country_code'] ?? '', 'office_phone' => $company['company_phone'] ?? '', 'office_email' => $company['company_email'] ?? '', ]); } /** An office by id (0 is the main one) - null when there is none, or it was removed (unless $archived). */ function office($office_id, $archived = false) { $office_id = intval($office_id); return $office_id === 0 ? officeMain() : (offices($archived)[$office_id] ?? null); } /** Every office, the main one first - office_id => office. */ function officesAll() { return [0 => officeMain()] + offices(); } /** An office's address on one line, without the country. */ function officeAddressLine(array $office, $separator = ' ') { return formatAddress($office['address'], $office['city'], $office['state'], $office['zip'], '', $separator); } /** The office an agent works from - 0 for the main one, and when theirs was removed. */ function officeIdForUser($user_id) { global $mysqli; $cache = &officesCache(); if (!isset($cache['users'])) { $cache['users'] = []; if (officesReady()) { $sql = mysqli_query($mysqli, "SELECT user_id, user_office_id FROM users WHERE user_office_id > 0"); while ($row = mysqli_fetch_row($sql)) { $cache['users'][intval($row[0])] = intval($row[1]); } } } $office_id = $cache['users'][intval($user_id)] ?? 0; return $office_id && isset(offices()[$office_id]) ? $office_id : 0; } /** The office an agent works from - the main one when none. */ function officeForUser($user_id) { return office(officeIdForUser($user_id)) ?? officeMain(); } /** An office picked in a form - its id when it is there (0 for the main one), else 0. */ function officeIdFrom($value) { $office_id = intval($value); return $office_id && isset(offices()[$office_id]) ? $office_id : 0; } /** Put an agent in an office (0 is the main one). */ function officeSetUser($user_id, $office_id) { global $mysqli; $user_id = intval($user_id); if (!$user_id || !officesReady()) { return; } mysqli_query($mysqli, "UPDATE users SET user_office_id = " . officeIdFrom($office_id) . " WHERE user_id = $user_id"); officesForget(); } /** The agents an office can have - user_id => row (name and avatar): not archived, no API / service accounts. */ function officeAgents() { global $mysqli; $agents = []; $sql = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users WHERE user_type = 1 AND user_archived_at IS NULL" . serviceAccountsOutSql('users.user_service_account') . " ORDER BY user_name"); while ($row = mysqli_fetch_assoc($sql)) { $agents[intval($row['user_id'])] = $row; } return $agents; } /** Who works where - office_id => [user_id, ...]; those in no other office are under 0. */ function officePeople() { $people = [0 => []]; foreach (array_keys(officeAgents()) as $user_id) { $people[officeIdForUser($user_id)][] = $user_id; } return $people; } /* * ---------- Admin > Settings > Company Details ---------- */ /** The Offices section of Company Details - the main office and the others, who works in each, and Add an office. */ function officesSection() { if (!officesReady()) { return ''; } $agents = officeAgents(); $people = officePeople(); ob_start(); ?> = settingsSectionStart('offices', 'Offices', 'Other places you work from, if you have them. Each agent works from one - their trips start there, and the emails about their tickets show its address and phone. Invoices, quotes and contracts keep the main address.') ?>
| Office | Who works there | |
|---|---|---|
|
= escapeHtml($office['name']) ?>
= escapeHtml($office['name']) ?> Above
= $contact ? implode(' · ', array_map('escapeHtml', $contact)) : 'No address yet' ?>
|
= $who ? implode(', ', $more ? array_slice($who, 0, 6) : $who) . ($more ? " and $more more" : '') : 'Nobody' ?> |