Files
itflow/agent/reports/outstanding_balances.php
johnnyq cd7ebc0a5f Security: enforce per-user client restrictions on client, financial and bulk handlers and per-client reports
Agents restricted from a client could still edit, archive, restore or delete it, run bulk actions on it (edits, tags, archive, email, tickets) and add credits. Every selected client is now checked before anything is written.

Expense, trip and recurring expense edits now check the client the record already belongs to, not just the submitted one. Recurring expense delete, revenue edit/delete and task approval handlers gained the same check.

Per-client reports and the dashboard payment/ticket lists now hide clients the agent is denied. Company-wide totals are unchanged.
2026-10-08 16:17:46 -04:00

86 lines
2.7 KiB
PHP

<?php
require_once "includes/inc_all_reports.php";
enforceUserPermission('module_financial');
?>
<div class="card card-dark">
<div class="card-header py-2">
<h3 class="card-title mt-2"><i class="fas fa-fw fa-exclamation-triangle me-2"></i>Clients with a Balance</h3>
<div class="card-tools">
<button type="button" class="btn btn-primary d-print-none" onclick="window.print();"><i class="fas fa-fw fa-print me-2"></i>Print</button>
</div>
</div>
<div class="card-body">
<?php
$sql_clients = mysqli_query($mysqli, "
SELECT
clients.client_id,
clients.client_name,
IFNULL(SUM(invoices.invoice_amount), 0) - IFNULL(SUM(payments.payment_amount), 0) AS balance
FROM
clients
LEFT JOIN
invoices
ON
clients.client_id = invoices.invoice_client_id
AND invoices.invoice_status != 'Draft'
AND invoices.invoice_status != 'Cancelled'
AND invoice_status != 'Non-Billable'
LEFT JOIN
(SELECT
payment_invoice_id,
SUM(payment_amount) as payment_amount
FROM payments
GROUP BY payment_invoice_id) as payments
ON
invoices.invoice_id = payments.payment_invoice_id
WHERE 1=1 " . clientScopeSql('clients.client_id') . "
GROUP BY
clients.client_id,
clients.client_name
HAVING
balance > 0
ORDER BY
balance DESC
");
?>
<div class="table-responsive-sm">
<table class="table table-striped">
<thead>
<tr>
<th>Client</th>
<th class="text-end">Balance</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($sql_clients)) {
$client_id = intval($row['client_id']);
$client_name = escapeHtml($row['client_name']);
$balance = floatval($row['balance']);
?>
<tr>
<td><a href="../../agent/invoices.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
<td class="text-end"><?= numfmt_format_currency($currency_format, $balance, $session_company_currency) ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php
require_once "../../includes/footer.php";