Labor Costs, functions/labor_cost.php), each * agent's hours are costed at their rate, the products sold at their cost, * and profit, margin and break-even hours for flat monthly fees follow. */ require_once "includes/inc_report.php"; reportEnforce('client_profitability'); $period = reportPeriod('last_12'); $target = isset($_GET['target']) && $_GET['target'] !== '' ? round(max(0, floatval($_GET['target'])), 2) : round(floatval($config_default_hourly_rate), 2); $with_tickets = reportFeatureOn('ticketing'); $clients = []; $client = function ($row) use (&$clients) { $id = intval($row['client_id']); if (!isset($clients[$id])) { $clients[$id] = ['client_id' => $id, 'client' => $row['client_name'], 'abbr' => $row['client_abbreviation'] ?? '', 'income' => 0.0, 'costs' => 0.0, 'seconds' => 0, 'billable_seconds' => 0, 'tickets' => 0, 'mrr' => 0.0]; } return $id; }; $scope = clientScopeSql('client_id'); // Money in $sql = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation, SUM(amount) AS v FROM ( SELECT invoice_client_id AS client_id, payment_amount AS amount FROM payments JOIN invoices ON invoice_id = payment_invoice_id WHERE " . reportRangeSql('payment_date', $period) . " UNION ALL SELECT revenue_client_id, revenue_amount FROM revenues WHERE revenue_client_id > 0 AND " . dashboardRevenueSql() . " AND " . reportRangeSql('revenue_date', $period) . " ) AS income JOIN clients USING (client_id) WHERE 1 = 1 $scope GROUP BY client_id, client_name, client_abbreviation"); while ($row = mysqli_fetch_assoc($sql)) { $clients[$client($row)]['income'] = floatval($row['v']); } // Money out on their behalf $sql = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation, SUM(expense_amount) AS v FROM expenses JOIN clients ON expense_client_id = client_id WHERE expense_vendor_id > 0 AND " . reportRangeSql('expense_date', $period) . " $scope GROUP BY client_id, client_name, client_abbreviation"); while ($row = mysqli_fetch_assoc($sql)) { $clients[$client($row)]['costs'] = floatval($row['v']); } // Time spent, and tickets raised if ($with_tickets) { $sql = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation, SUM(TIME_TO_SEC(ticket_reply_time_worked)) AS s, SUM(CASE WHEN ticket_billable = 1 THEN TIME_TO_SEC(ticket_reply_time_worked) ELSE 0 END) AS b FROM ticket_replies JOIN tickets ON ticket_reply_ticket_id = ticket_id JOIN clients ON ticket_client_id = client_id WHERE ticket_reply_archived_at IS NULL AND ticket_reply_time_worked IS NOT NULL AND " . reportRangeSql('ticket_reply_created_at', $period) . " $scope GROUP BY client_id, client_name, client_abbreviation"); while ($row = mysqli_fetch_assoc($sql)) { $id = $client($row); $clients[$id]['seconds'] = intval($row['s']); $clients[$id]['billable_seconds'] = intval($row['b']); } $sql = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation, COUNT(ticket_id) AS n FROM tickets JOIN clients ON ticket_client_id = client_id WHERE " . reportRangeSql('ticket_created_at', $period) . " $scope GROUP BY client_id, client_name, client_abbreviation"); while ($row = mysqli_fetch_assoc($sql)) { if (isset($clients[intval($row['client_id'])])) { $clients[intval($row['client_id'])]['tickets'] = intval($row['n']); } } } // What they are on now, every month $sql = mysqli_query($mysqli, "SELECT recurring_invoice_client_id AS client_id, SUM(" . recurringInvoiceMonthlySql() . ") AS v FROM recurring_invoices WHERE recurring_invoice_status = 1 AND recurring_invoice_archived_at IS NULL GROUP BY recurring_invoice_client_id"); while ($row = mysqli_fetch_assoc($sql)) { if (isset($clients[intval($row['client_id'])])) { $clients[intval($row['client_id'])]['mrr'] = floatval($row['v']); } } // Labor and parts, and income as invoiced when that's picked (functions/labor_cost.php) laborProfitAdd($clients, $client, $period, $with_tickets); $with_labor = laborCostReady(); $basis = laborProfitBasis(); foreach ($clients as &$row) { $row['net'] = $row['income'] - $row['costs']; $row['hours'] = $row['seconds'] / 3600; $row['rate'] = $row['seconds'] > 0 ? $row['net'] / ($row['seconds'] / 3600) : null; $row['flag'] = ''; if ($row['seconds'] >= 1800 && $row['net'] <= 0) { $row['flag'] = 'unpaid'; } elseif ($row['rate'] !== null && $target > 0 && $row['rate'] < $target && $row['seconds'] >= 1800) { $row['flag'] = 'below'; } } unset($row); laborProfitFinish($clients, $period); // Lowest rate first - the ones to look at - then those with no time logged usort($clients, function ($a, $b) { if ($a['rate'] === null || $b['rate'] === null) { return ($a['rate'] === null) <=> ($b['rate'] === null) ?: $b['net'] <=> $a['net']; } return $a['rate'] <=> $b['rate']; }); $columns = [ 'client' => ['label' => 'Client', 'html' => function ($row) { return reportClientCell($row['client_id'], $row['client'], $row['abbr']); }], 'income' => ['label' => 'Income', 'type' => 'money'], 'costs' => ['label' => 'Direct costs', 'type' => 'money', 'title' => 'Expenses filed against the client', 'html' => function ($row) use ($period) { return $row['costs'] > 0 ? '' . reportMoney($row['costs']) . '' : '–'; }], 'net' => ['label' => 'Net', 'type' => 'money', 'class' => 'fw-semibold'], ]; if ($with_labor) { $zero = '–'; $columns['labor'] = ['label' => 'Labor cost', 'type' => 'money', 'title' => 'Hours on their tickets at each agent\'s cost per hour (Admin > Labor Costs)', 'html' => function ($row) use ($zero) { $html = $row['labor'] > 0 ? reportMoney($row['labor']) : $zero; if ($row['uncosted_seconds'] > 0) { $html .= ' '; } return $html; }]; $columns['parts'] = ['label' => 'Parts cost', 'type' => 'money', 'title' => 'What the products on their invoices in the period cost you', 'html' => function ($row) use ($zero) { return $row['parts'] > 0 ? reportMoney($row['parts']) : $zero; }]; $columns['profit'] = ['label' => 'Profit', 'type' => 'money', 'class' => 'fw-semibold', 'title' => 'Net less labor and parts', 'html' => function ($row) { return '' . reportMoney($row['profit']) . ''; }]; $columns['margin'] = ['label' => 'Margin', 'type' => 'percent', 'title' => 'Profit as a share of income', 'html' => function ($row) use ($zero) { return $row['margin'] === null ? $zero : '' . reportPercent($row['margin']) . ''; }]; } if ($with_tickets) { $columns['seconds'] = ['label' => 'Hours', 'type' => 'hours', 'title' => 'Time logged on their tickets in the period']; $columns['billable_share'] = ['label' => 'Billable', 'type' => 'percent', 'title' => 'Share of the hours on billable tickets', 'class' => 'text-body-secondary']; $columns['rate'] = ['label' => 'Per hour', 'type' => 'money', 'title' => 'Net divided by hours - your effective hourly rate', 'html' => function ($row) use ($target) { if ($row['rate'] === null) { return 'No time'; } $tone = $row['flag'] ? 'text-danger fw-semibold' : ($target > 0 && $row['rate'] >= $target ? 'text-success-emphasis fw-semibold' : 'fw-semibold'); return '' . reportMoney($row['rate']) . ''; }]; $columns['tickets'] = ['label' => 'Tickets', 'type' => 'count']; } $columns['mrr'] = ['label' => 'Recurring / month', 'type' => 'money', 'html' => function ($row) { return $row['mrr'] > 0 ? reportMoney($row['mrr']) : '–'; }]; if ($with_labor && $with_tickets) { $columns['hours_month'] = ['label' => 'Hours / month', 'type' => 'decimal', 'decimals' => 1, 'title' => 'Hours on their tickets, a month on average over the period']; $columns['break_even'] = ['label' => 'Break-even h / month', 'type' => 'decimal', 'decimals' => 1, 'title' => 'Hours a month their recurring fee pays for: the fee, less parts and direct costs a month, over the cost per hour of the agents who work for them', 'html' => function ($row) { if ($row['break_even'] === null) { return '–'; } $text = number_format($row['break_even'], 1); if ($row['over_break_even']) { return '' . $text . ' Over'; } return '' . $text . ''; }]; } $columns['flag'] = ['label' => '', 'sort' => false, 'csv_label' => 'Flag', 'csv' => function ($row) { return ['unpaid' => 'Time but no income', 'below' => 'Below target'][$row['flag']] ?? ''; }, 'html' => function ($row) { if ($row['flag'] === 'unpaid') { return 'Time, no income'; } if ($row['flag'] === 'below') { return 'Below target'; } return ''; }]; foreach ($clients as &$row) { $row['billable_share'] = reportShare($row['billable_seconds'], $row['seconds']); } unset($row); $income = array_sum(array_column($clients, 'income')); $costs = array_sum(array_column($clients, 'costs')); $seconds = array_sum(array_column($clients, 'seconds')); $overall_rate = $seconds ? ($income - $costs) / ($seconds / 3600) : null; $totals = ['client' => 'Total', 'income' => $income, 'costs' => $costs, 'net' => $income - $costs, 'seconds' => $seconds, 'rate' => $overall_rate, 'tickets' => array_sum(array_column($clients, 'tickets')), 'mrr' => array_sum(array_column($clients, 'mrr')), 'billable_share' => reportShare(array_sum(array_column($clients, 'billable_seconds')), $seconds)]; if ($with_labor) { $totals += ['labor' => array_sum(array_column($clients, 'labor')), 'parts' => array_sum(array_column($clients, 'parts')), 'profit' => array_sum(array_column($clients, 'profit')), 'uncosted_seconds' => array_sum(array_column($clients, 'uncosted_seconds')), 'hours_month' => array_sum(array_column($clients, 'hours_month')), 'break_even' => null]; $totals['margin'] = reportShare($totals['profit'], $income, 1); } if (reportExporting()) { reportCsv('Client Profitability ' . $period['start'] . ' to ' . $period['end'], $columns, $clients, $totals); } require_once "includes/inc_report_layout.php"; $flagged = array_filter($clients, function ($row) { return $row['flag'] !== ''; }); $chart_rows = array_values(array_filter($clients, function ($row) { return $row['rate'] !== null && $row['seconds'] >= 1800; })); usort($chart_rows, function ($a, $b) { return $b['seconds'] <=> $a['seconds']; }); $chart_rows = array_slice($chart_rows, 0, 15); usort($chart_rows, function ($a, $b) { return $a['rate'] <=> $b['rate']; }); $target_control = '
' . '' . '' . '
'; // Income as paid - money in, as the report always counted it - or as invoiced if ($with_labor) { $target_control .= '
' . reportSegmentControl('basis', $basis, LABOR_PROFIT_BASES, 'd-inline-block') . '
'; } ?>
$period, 'years' => reportPastYears("SELECT DISTINCT YEAR(payment_date) AS y FROM payments"), 'controls' => $target_control, ]); ?> 'Income from clients', 'value' => reportMoney($income), 'sub' => $costs > 0 ? escapeHtml(reportMoney($costs)) . ' in direct costs' : 'No direct costs filed'], ['label' => 'Hours worked', 'value' => $with_tickets ? reportHours($seconds) : '-', 'sub' => $with_tickets ? escapeHtml(reportPercent(reportShare(array_sum(array_column($clients, 'billable_seconds')), $seconds))) . ' on billable tickets' : 'Ticketing is off'], ['label' => 'Earned per hour', 'value' => $overall_rate === null ? '-' : reportMoney($overall_rate), 'tone' => $overall_rate !== null && $target > 0 ? ($overall_rate >= $target ? 'success' : 'danger') : '', 'sub' => $target > 0 ? 'Target ' . reportMoney($target) . '' : 'Enter a target above'], ['label' => 'Clients to look at', 'value' => reportCount(count($flagged)), 'tone' => $flagged ? 'danger' : 'success', 'sub' => 'Below target, or time with no income'], ]) ?>
'Labor cost', 'value' => reportMoney($totals['labor']), 'sub' => $uncosted > 0 ? '' . escapeHtml(reportHours($uncosted)) . ' h with no cost per hour' : 'Hours at each agent\'s cost'], ['label' => 'Parts cost', 'value' => reportMoney($totals['parts']), 'sub' => 'On invoices dated in the period'], ['label' => 'Profit', 'value' => reportMoney($totals['profit']), 'tone' => $totals['profit'] < 0 ? 'danger' : 'success', 'sub' => $totals['margin'] === null ? '' : escapeHtml(reportPercent($totals['margin'])) . ' margin, ' . strtolower(LABOR_PROFIT_BASES[$basis]) . ''], ['label' => 'Losing money', 'value' => reportCount(count($losing)), 'tone' => $losing ? 'danger' : 'success', 'sub' => 'Clients with a profit below 0'], ['label' => 'Over break-even', 'value' => reportCount(count($over)), 'tone' => $over ? 'danger' : 'success', 'sub' => 'More hours than their fee pays for'], ]) ?>
No labor costs are set, so time costs nothing here. Set each agent\'s cost per hour.' : 'An admin sets them in Admin > Labor Costs.' ?>
0) { ?>
h were logged by agents with no cost per hour, and there's no company default - that time costs nothing here. Labor Costs' : '' ?>
'bar', 'horizontal' => true, 'labels' => array_map(function ($r) { return $r['client']; }, $chart_rows), 'series' => [['label' => 'Per hour', 'data' => array_map(function ($r) { return round($r['rate'], 2); }, $chart_rows), 'tones' => array_map(function ($r) use ($target) { return $r['flag'] ? 'danger' : ($target > 0 ? 'success' : 'accent'); }, $chart_rows)]], 'format' => 'money', 'legend' => false, ], '', 'What each client earned per hour of work') ?>
'No income or time logged for any client in this period.', 'totals' => $totals]) ?>