Admin Labor Costs holds each agent's cost per hour from a date with a company default, and Client Profitability adds labor, parts, profit, margin and break-even hours for flat fees, with a Paid or Invoiced basis; database 3.8.0 adds user_cost_rates and config_labor_cost_default; 26.11.9 changelog entries

This commit is contained in:
johnnyq
2026-11-17 19:23:22 -05:00
parent ee5f7ff48a
commit 46a2dfe96c
11 changed files with 793 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
<?php
/*
* ITFlow - Database update to version 3.8.0 (from 3.7.9)
* Included by admin/database_updates.php - do not access directly
*
* Labor costs (functions/labor_cost.php): what an hour of each agent costs the
* company - wage, payroll taxes and benefits - from a date on. A new rate is a
* new row, so a past period keeps the rate it had. Client Profitability costs
* the hours on each client's tickets at them.
* user_cost_rates
* settings.config_labor_cost_default for agents with no rate - NULL leaves their time uncosted
*
* Re-runnable: the table and the column are added only where they aren't there yet.
*/
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
$itflow_column = function ($table, $column) use ($mysqli) {
$result = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column' LIMIT 1");
return $result && mysqli_num_rows($result) > 0;
};
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `user_cost_rates` (
`user_cost_rate_id` int(11) NOT NULL AUTO_INCREMENT,
`user_cost_rate_user_id` int(11) NOT NULL,
`user_cost_rate_from` date NOT NULL,
`user_cost_rate_hourly` decimal(15,2) NOT NULL DEFAULT 0.00,
`user_cost_rate_created_by` int(11) NOT NULL DEFAULT 0,
`user_cost_rate_created_at` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`user_cost_rate_id`),
UNIQUE KEY `user_cost_rate_user_from` (`user_cost_rate_user_id`, `user_cost_rate_from`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
if (!$itflow_column('settings', 'config_labor_cost_default')) {
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_labor_cost_default` decimal(15,2) DEFAULT NULL AFTER `config_default_hourly_rate`");
}

View File

@@ -86,6 +86,12 @@
<p>Payment Methods</p>
</a>
</li>
<li class="nav-item">
<a href="/admin/labor_costs.php" class="nav-link <?= (basename($_SERVER['PHP_SELF']) == 'labor_costs.php' ? 'active' : '') ?>">
<i class="nav-icon fas fa-user-tag"></i>
<p>Labor Costs</p>
</a>
</li>
<?php } ?>
<?php if ($config_module_enable_ticketing) { ?>
<li class="nav-item">

186
admin/labor_costs.php Normal file
View File

@@ -0,0 +1,186 @@
<?php
/*
* Admin > Labor Costs - what an hour of each agent costs the company (functions/labor_cost.php),
* with the date each rate counts from, and the company default for agents with none. Client
* Profitability costs the hours on each client's tickets at them. An agent's rates are in their
* window (modals/labor_cost/labor_cost_rates.php); the handlers are admin/post/labor_cost.php.
* The audit log names the agent, never the amount.
*/
// Default Column Sortby Filter
$sort = "user_name";
$order = "ASC";
require_once "includes/inc_all_admin.php";
if (!laborCostReady()) { ?>
<div class="alert alert-warning">Labor costs need the database update - <a href="update.php" class="alert-link">Maintenance &gt; Update</a>.</div>
<?php require_once "../includes/footer.php";
return;
}
$views = [
'' => ['label' => 'Agents', 'icon' => 'fa-user', 'title' => 'Active agents'],
'archived' => ['label' => 'Archived', 'icon' => 'fa-archive', 'title' => 'Archived agents with a rate - their time in earlier periods is still costed'],
];
$show = (string) ($_GET['show'] ?? '');
$show = isset($views[$show]) ? $show : '';
$rates = laborCostRates();
$default = laborCostDefault();
$today = date('Y-m-d');
// Active agents, and archived ones that have a rate - then searched, sorted and paged here
$search = mb_strtolower(trim(stripslashes((string) ($q ?? ''))));
$rows = [];
$view_counts = ['' => 0, 'archived' => 0];
$result = mysqli_query($mysqli, "SELECT user_id, user_name, user_email, user_avatar, user_archived_at FROM users WHERE user_type = 1
AND (user_archived_at IS NULL OR user_id IN (SELECT user_cost_rate_user_id FROM user_cost_rates))");
while ($row = mysqli_fetch_assoc($result)) {
if ($search !== '' && mb_strpos(mb_strtolower($row['user_name'] . ' ' . $row['user_email']), $search) === false) {
continue;
}
$view = $row['user_archived_at'] === null ? '' : 'archived';
$view_counts[$view]++;
if ($view !== $show) {
continue;
}
$user_id = intval($row['user_id']);
$list = $rates[$user_id] ?? [];
$row['now'] = laborCostRateOn($rates, $user_id, $today, null);
$row['since'] = '';
foreach ($list as [$from]) {
if ($from <= $today || $row['since'] === '') {
$row['since'] = $from;
}
}
$row['rate_count'] = count($list);
$rows[] = $row;
}
$sortable_columns = [
'user_name' => function ($row) { return mb_strtolower($row['user_name']); },
'now' => function ($row) { return $row['now'] ?? -1; },
'since' => function ($row) { return $row['since']; },
];
if (!isset($sortable_columns[$sort])) {
$sort = 'user_name';
}
$key = $sortable_columns[$sort];
usort($rows, function ($a, $b) use ($key, $order) {
$cmp = $key($a) <=> $key($b);
if ($cmp === 0) {
$cmp = strcasecmp($a['user_name'], $b['user_name']);
}
return $order === 'DESC' ? -$cmp : $cmp;
});
$num_rows = [count($rows)];
$rows = array_slice($rows, $record_from, $record_to);
$sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) {
return '<a class="text-reset text-decoration-none" href="?' . escapeHtml($url_query_strings_sort) . '&amp;sort=' . $column . '&amp;order=' . $disp . '">'
. $label . ($sort === $column ? " $order_icon" : '') . '</a>';
};
$pill_url = function ($view) {
return '?' . http_build_query(array_filter(['q' => $_GET['q'] ?? '', 'show' => $view]));
};
$money = function ($amount) use ($currency_format, $session_company_currency) {
return escapeHtml(numfmt_format_currency($currency_format, $amount, $session_company_currency));
};
?>
<div class="card" data-labor-costs>
<div class="card-header bg-dark py-2">
<h3 class="card-title mt-2"><i class="fas fa-fw fa-user-tag me-2"></i>Labor Costs</h3>
<div class="card-tools">
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="modals/labor_cost/labor_cost_default.php" data-labor-default-button>
<i class="fas fa-fw fa-users"></i><span class="d-none d-lg-inline ms-2">Company default: <?= $default === null ? 'none' : $money($default) . ' / h' ?></span>
</button>
</div>
</div>
<div class="card-header py-3">
<p class="small text-body-secondary mb-3">
What an hour of each agent costs the company - their wage, payroll taxes and benefits. <a href="/agent/reports/client_profitability.php">Client Profitability</a>
costs the hours on each client's tickets at these rates. A new rate counts from its date, so earlier periods keep the rate they had; an agent's first rate also
covers the time before it. Agents with no rate take the company default. Only admins see this page.
</p>
<form autocomplete="off">
<?php if ($show !== '') { ?><input type="hidden" name="show" value="<?= escapeHtml($show) ?>"><?php } ?>
<div class="row g-2 align-items-center">
<div class="col-lg-5 col-md-6">
<div class="input-group">
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search agents..." title="Name or email">
<button class="btn btn-dark" title="Search"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
</form>
<nav class="nav nav-pills itflow-asset-tabs mt-2">
<?php foreach ($views as $view_key => $view) { ?>
<a class="nav-link <?= $show === (string) $view_key ? 'active' : '' ?>" href="<?= escapeHtml($pill_url($view_key)) ?>" title="<?= escapeHtml($view['title']) ?>"><i class="fas fa-fw <?= $view['icon'] ?> me-1"></i><?= escapeHtml($view['label']) ?> <span class="badge rounded-pill ms-1"><?= intval($view_counts[$view_key]) ?></span></a>
<?php } ?>
</nav>
</div>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-nowrap <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
<tr>
<th class="ps-3"><?= $sort_link('user_name', 'Agent') ?></th>
<th><?= $sort_link('now', 'Cost per hour now') ?></th>
<th class="d-none d-md-table-cell"><?= $sort_link('since', 'Since') ?></th>
<th class="d-none d-sm-table-cell">Rates</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $row) {
$user_id = intval($row['user_id']);
$rates_url = "modals/labor_cost/labor_cost_rates.php?user_id=$user_id";
?>
<tr data-labor-agent="<?= $user_id ?>">
<td class="ps-3">
<div class="d-flex align-items-center gap-3">
<span class="d-none d-sm-inline-flex"><?= userAvatar($user_id, $row['user_name'], $row['user_avatar']) ?></span>
<div class="min-w-0">
<a class="ajax-modal fw-semibold text-body" href="#" data-modal-url="<?= $rates_url ?>" title="Rates for <?= escapeHtml($row['user_name']) ?>"><?= escapeHtml($row['user_name']) ?></a>
<div class="small text-body-secondary text-truncate"><?= escapeHtml($row['user_email']) ?></div>
</div>
</div>
</td>
<td data-labor-now>
<?php if ($row['now'] !== null) { ?>
<span class="fw-semibold"><?= $money($row['now']) ?></span>
<?php } elseif ($default !== null) { ?>
<span class="text-body-secondary">Default, <?= $money($default) ?></span>
<?php } else { ?>
<span class="text-warning-emphasis"><i class="fas fa-fw fa-exclamation-triangle me-1"></i>Not set</span>
<?php } ?>
</td>
<td class="d-none d-md-table-cell text-nowrap text-body-secondary"><?= $row['since'] !== '' ? escapeHtml(date('M j, Y', strtotime($row['since']))) : '-' ?></td>
<td class="d-none d-sm-table-cell text-body-secondary"><?= $row['rate_count'] ? number_format($row['rate_count']) : '<span class="text-body-tertiary">None</span>' ?></td>
<td class="text-end pe-3">
<button type="button" class="btn btn-sm btn-light border ajax-modal" data-modal-url="<?= $rates_url ?>"><i class="fas fa-fw fa-dollar-sign"></i><span class="d-none d-md-inline ms-1"><?= $row['rate_count'] ? 'Rates' : 'Set a rate' ?></span></button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php
$empty_state = [
'thing' => 'agents',
'icon' => 'fa-user-tag',
'filtered' => $q !== '',
'archived' => $show === 'archived',
'views' => ['show'],
'text' => 'Every active agent is listed here, to give a cost per hour.',
];
require_once "../includes/filter_footer.php";
?>
</div>
<?php
require_once "../includes/footer.php";

View File

@@ -0,0 +1,37 @@
<?php
/*
* Admin > Labor Costs - the company default: the cost per hour of an agent with no rate of their
* own (admin/post/labor_cost.php, functions/labor_cost.php). Empty leaves their time uncosted.
*/
require_once '../../includes/modal_header.php';
$default = laborCostDefault();
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fas fa-fw fa-users me-2"></i>Company default</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<div class="modal-body">
<p class="small text-body-secondary">The cost per hour of an agent with no rate of their own - a blended figure for your team, say. Leave it empty and their time costs nothing in Client Profitability, which says how many hours that is.</p>
<label class="form-label" for="laborCostDefault">Cost per hour</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-dollar-sign"></i></span>
<input type="text" inputmode="decimal" class="form-control" name="labor_cost_default" id="laborCostDefault" placeholder="None" maxlength="12" value="<?= $default === null ? '' : escapeHtml(number_format($default, 2, '.', '')) ?>" autofocus>
</div>
</div>
<div class="modal-footer">
<button type="submit" name="edit_labor_cost_default" class="btn btn-primary text-bold"><i class="fas fa-fw fa-check me-2"></i>Save</button>
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
</div>
</form>
<?php
require_once "../../../includes/modal_footer.php";

View File

@@ -0,0 +1,84 @@
<?php
/*
* Admin > Labor Costs - one agent's rates: what an hour of them costs from each date, a new one,
* and taking one away (admin/post/labor_cost.php). functions/labor_cost.php.
*/
require_once '../../includes/modal_header.php';
$user_id = intval($_GET['user_id'] ?? 0);
$agent = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT user_id, user_name, user_archived_at FROM users WHERE user_id = $user_id AND user_type = 1"));
$list = $agent && laborCostReady() ? array_reverse(laborCostRates()[$user_id] ?? []) : [];
$default = laborCostDefault();
$money = function ($amount) use ($currency_format, $session_company_currency) {
return escapeHtml(numfmt_format_currency($currency_format, $amount, $session_company_currency));
};
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fas fa-fw fa-user-tag me-2"></i><?= $agent ? escapeHtml($agent['user_name']) : 'Labor cost' ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<?php if (!$agent || !laborCostReady()) { ?>
<div class="modal-body">
<div class="alert alert-danger mb-0"><?= laborCostReady() ? 'That agent wasn\'t found.' : 'Labor costs need the database update - Maintenance &gt; Update.' ?></div>
</div>
<?php } else { ?>
<form action="post.php" method="post" autocomplete="off" data-labor-rate-form>
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<input type="hidden" name="user_id" value="<?= $user_id ?>">
<div class="modal-body">
<p class="small text-body-secondary">What an hour of <?= escapeHtml($agent['user_name']) ?> costs the company - wage, payroll taxes and benefits. A new rate counts from its date; time before it keeps the rate it had.</p>
<h6 class="mb-2">Rates</h6>
<?php if ($list) { ?>
<ul class="list-group mb-3" data-labor-rates>
<?php foreach ($list as $i => [$from, $hourly, $rate_id]) { ?>
<li class="list-group-item d-flex align-items-center gap-2" data-labor-rate="<?= intval($rate_id) ?>">
<span class="fw-semibold"><?= $money($hourly) ?> / h</span>
<span class="text-body-secondary small">from <?= escapeHtml(date('M j, Y', strtotime($from))) ?><?= $i === count($list) - 1 ? ', and before' : '' ?></span>
<a class="btn btn-sm btn-link text-danger ms-auto confirm-link" href="post.php?delete_labor_cost_rate=<?= intval($rate_id) ?>&amp;csrf_token=<?= $_SESSION['csrf_token'] ?>"
data-confirm-title="Take this rate away?" data-confirm-text="<?= escapeHtml($agent['user_name']) ?>'s time is costed at the rate before it<?= count($list) === 1 ? ', or the company default' : '' ?>." data-confirm-button="Take it away" title="Take this rate away">
<i class="fas fa-fw fa-times"></i>
</a>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<p class="small mb-3 <?= $default === null ? 'text-warning-emphasis' : 'text-body-secondary' ?>" data-labor-rates-none>
None yet - <?= $default === null ? 'their time costs nothing in Client Profitability, as there\'s no company default either.' : 'their time is costed at the company default, ' . $money($default) . ' an hour.' ?>
</p>
<?php } ?>
<h6 class="mb-2"><?= $list ? 'A new rate' : 'Their rate' ?></h6>
<div class="row g-3">
<div class="col-sm-6">
<label class="form-label" for="laborCostHourly">Cost per hour <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-dollar-sign"></i></span>
<input type="text" inputmode="decimal" class="form-control" name="hourly" id="laborCostHourly" placeholder="0.00" maxlength="12" required autofocus>
</div>
</div>
<div class="col-sm-6">
<label class="form-label" for="laborCostFrom">Counts from <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-calendar-day"></i></span>
<input type="date" class="form-control" name="from" id="laborCostFrom" value="<?= escapeHtml($list ? date('Y-m-d') : date('Y-01-01')) ?>" max="2999-12-31" required>
</div>
</div>
</div>
<div class="form-text">A rate on a date that already has one replaces it.</div>
</div>
<div class="modal-footer">
<button type="submit" name="add_labor_cost_rate" class="btn btn-primary text-bold"><i class="fas fa-fw fa-check me-2"></i>Save the rate</button>
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
</div>
</form>
<?php } ?>
<?php
require_once "../../../includes/modal_footer.php";

70
admin/post/labor_cost.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
/*
* Admin > Labor Costs (admin/labor_costs.php, functions/labor_cost.php). The audit log names the
* agent, never the amount - it's close to their pay.
*/
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
if (isset($_POST['edit_labor_cost_default'])) {
validateCSRFToken();
if (!laborCostReady()) {
flashAlert("Labor costs need the database update - Maintenance > Update.", 'error');
redirect();
}
[$amount, $problem] = laborCostParse($_POST['labor_cost_default'] ?? '');
if ($problem !== '') {
flashAlert($problem, 'error');
redirect();
}
mysqli_query($mysqli, "UPDATE settings SET config_labor_cost_default = " . ($amount === null ? 'NULL' : $amount) . " WHERE company_id = 1");
logAudit("Settings", "Edit", "$session_name changed the company default labor cost");
flashAlert($amount === null ? "No company default - agents with no rate cost nothing." : "Company default saved.");
redirect();
}
if (isset($_POST['add_labor_cost_rate'])) {
validateCSRFToken();
$user_id = intval($_POST['user_id'] ?? 0);
$problem = laborCostRateSave($user_id, $_POST['from'] ?? '', $_POST['hourly'] ?? '', $session_user_id);
if ($problem !== '') {
flashAlert(escapeHtml($problem), 'error');
redirect();
}
$agent_name = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT user_name FROM users WHERE user_id = $user_id"))[0] ?? "agent $user_id");
logAudit("Settings", "Edit", "$session_name set a labor cost for " . escapeSql($agent_name), 0, $user_id);
flashAlert("Labor cost saved for " . escapeHtml($agent_name) . ".");
redirect();
}
if (isset($_GET['delete_labor_cost_rate'])) {
validateCSRFToken();
$user_id = laborCostRateDelete(intval($_GET['delete_labor_cost_rate']));
if ($user_id) {
$agent_name = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT user_name FROM users WHERE user_id = $user_id"))[0] ?? "agent $user_id");
logAudit("Settings", "Edit", "$session_name took away a labor cost of " . escapeSql($agent_name), 0, $user_id);
flashAlert("Rate taken away.");
}
redirect();
}