Feature: Added Send Account Statment in Client Invoices Drop Down with Date Options and options to only include paid invoices

This commit is contained in:
johnnyq
2026-08-28 14:50:15 -04:00
parent 86ba041cdc
commit 342502073e
3 changed files with 403 additions and 0 deletions

View File

@@ -178,6 +178,13 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
data-modal-url="<?= buildExportModalUrl('modals/invoice/invoice_export.php', ['client_id', 'status', 'category', 'q'], ['dtf' => $dtf, 'dtt' => $dtt]) ?>">
<i class="fa fa-fw fa-download me-2"></i>Export
</a>
<?php if ($client_url && lookupUserPermission("module_sales") >= 2 && !empty($config_smtp_provider)) { ?>
<div class="dropdown-divider"></div>
<a class="dropdown-item text-dark ajax-modal" href="#"
data-modal-url="modals/client/client_statement.php?client_id=<?= $client_id ?>">
<i class="fa fa-fw fa-file-alt me-2"></i>Send Account Statement
</a>
<?php } ?>
</div>
</div>
</div>

View File

@@ -0,0 +1,192 @@
<?php
require_once '../../../includes/modal_header.php';
enforceUserPermission('module_sales', 2);
$client_id = intval($_GET['client_id']);
enforceClientAccess();
$sql = mysqli_query($mysqli, "SELECT client_currency_code, client_name FROM clients WHERE client_id = $client_id LIMIT 1");
$row = mysqli_fetch_assoc($sql);
$client_name = escapeHtml($row['client_name']);
$client_currency_code = escapeHtml($row['client_currency_code']);
if (empty($client_currency_code)) {
$client_currency_code = $session_company_currency;
}
// What the client owes right now, shown as context so the agent knows roughly
// what is about to go out. Deliberately not filtered by the date range below -
// this is the standing balance, not a preview of the statement.
$row = mysqli_fetch_assoc(mysqli_query(
$mysqli,
"SELECT IFNULL(SUM(invoice_amount), 0) - IFNULL(SUM(amount_paid), 0) AS balance,
COUNT(invoice_id) AS invoice_count FROM invoices
LEFT JOIN (
SELECT payment_invoice_id, SUM(payment_amount) AS amount_paid FROM payments
WHERE payment_archived_at IS NULL
GROUP BY payment_invoice_id
) AS invoice_payments ON payment_invoice_id = invoice_id
WHERE invoice_client_id = $client_id
AND invoice_status NOT IN ('Draft', 'Cancelled', 'Non-Billable')"
));
$outstanding_balance = floatval($row['balance']);
// Statement recipients follow the invoice rules - a statement is a billing
// document, so the same people who would get the invoice get the statement.
$sql_contacts = mysqli_query(
$mysqli,
"SELECT contact_billing, contact_email, contact_id, contact_name, contact_primary, contact_title
FROM contacts
WHERE contact_client_id = $client_id
AND contact_archived_at IS NULL
AND contact_email IS NOT NULL
AND contact_email != ''
" . documentContactFilterSql('invoice') . "
ORDER BY contact_primary DESC, contact_billing DESC, contact_name ASC"
);
$contact_count = mysqli_num_rows($sql_contacts);
$default_contact_ids = [];
$sql_defaults = mysqli_query(
$mysqli,
"SELECT contact_id FROM contacts
WHERE contact_client_id = $client_id
AND contact_archived_at IS NULL
AND contact_email IS NOT NULL
AND contact_email != ''
" . documentDefaultContactFilterSql('invoice')
);
while ($row = mysqli_fetch_assoc($sql_defaults)) {
$default_contact_ids[] = intval($row['contact_id']);
}
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title text-white">
<i class="fa fa-fw fa-file-alt me-2"></i>Send Account Statement
<span class="text-muted ms-1"><?= $client_name ?></span>
</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'] ?>">
<input type="hidden" name="client_id" value="<?= $client_id ?>">
<div class="modal-body">
<?php if ($contact_count == 0) { ?>
<p class="text-muted mb-0">
This client has no primary or billing contact with an email address, so there is
nobody to send a statement to. Flag a contact as billing first.
</p>
<?php } else { ?>
<div class="alert alert-secondary">
<i class="fa fa-fw fa-balance-scale me-2"></i>Current outstanding balance:
<strong><?= numfmt_format_currency($currency_format, $outstanding_balance, $client_currency_code) ?></strong>
</div>
<h6 class="text-bold">Statement Options</h6>
<div class="row g-2">
<div class="col-sm-6 mb-3">
<label>From</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
<input type="date" class="form-control" name="dtf" max="2999-12-31">
</div>
<small class="text-muted">Leave blank for all history</small>
</div>
<div class="col-sm-6 mb-3">
<label>To</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
<input type="date" class="form-control" name="dtt" max="2999-12-31" value="<?= date("Y-m-d") ?>">
</div>
</div>
</div>
<div class="mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="statementIncludePaid" name="include_paid" value="1">
<label class="form-check-label" for="statementIncludePaid">
Include paid invoices
<span class="text-muted">- off means outstanding items only</span>
</label>
</div>
</div>
<hr>
<h6 class="text-bold">Email Options</h6>
<label class="mb-2">Send to <strong class="text-danger">*</strong></label>
<div class="list-group mb-0">
<?php
while ($row = mysqli_fetch_assoc($sql_contacts)) {
$contact_id = intval($row['contact_id']);
$contact_name = escapeHtml($row['contact_name']);
$contact_email = escapeHtml($row['contact_email']);
$contact_title = escapeHtml($row['contact_title']);
$contact_primary = intval($row['contact_primary']);
$contact_billing = intval($row['contact_billing']);
$contact_checked = in_array($contact_id, $default_contact_ids, true);
?>
<label class="list-group-item" for="statementContact<?= $contact_id ?>">
<input type="checkbox" class="form-check-input me-2" name="contacts[]"
id="statementContact<?= $contact_id ?>" value="<?= $contact_id ?>"
<?php if ($contact_checked) { echo "checked"; } ?>>
<strong><?= $contact_name ?></strong>
<?php if ($contact_primary == 1) { ?>
<span class="badge text-bg-primary ms-1">Primary</span>
<?php } ?>
<?php if ($contact_billing == 1) { ?>
<span class="badge text-bg-success ms-1">Billing</span>
<?php } ?>
<?php if (!empty($contact_title)) { ?>
<span class="text-muted ms-1"><?= $contact_title ?></span>
<?php } ?>
<br>
<span class="text-muted ms-4"><?= $contact_email ?></span>
</label>
<?php
}
?>
</div>
<?php } ?>
</div>
<div class="modal-footer">
<?php if ($contact_count > 0) { ?>
<button type="submit" name="send_statement" class="btn btn-primary text-bold">
<i class="fa fa-fw fa-paper-plane me-2"></i>Send Statement
</button>
<?php } ?>
<button type="button" class="btn btn-light" data-bs-dismiss="modal">
<i class="fa fa-fw fa-times me-2"></i>Cancel
</button>
</div>
</form>
<?php
require_once '../../../includes/modal_footer.php';

View File

@@ -672,6 +672,210 @@ if (isset($_POST['email_invoice'])) {
}
if (isset($_POST['send_statement'])) {
validateCSRFToken();
enforceUserPermission('module_sales', 2);
$client_id = intval($_POST['client_id']);
enforceClientAccess();
$sql = mysqli_query($mysqli, "SELECT client_currency_code, client_name FROM clients WHERE client_id = $client_id LIMIT 1");
$row = mysqli_fetch_assoc($sql);
$client_name = escapeSql($row['client_name']);
$client_currency_code = escapeSql($row['client_currency_code']);
if (empty($client_currency_code)) {
$client_currency_code = escapeSql($session_company_currency);
}
// Statement options. Emptiness has to be tested BEFORE validateDate(),
// which falls back to today rather than returning empty - running a blank
// From through it would silently produce a one-day statement.
$statement_date_to = $_POST['dtt'] ?? '';
if (empty($statement_date_to)) {
$statement_date_to = date("Y-m-d");
} else {
$statement_date_to = validateDate($statement_date_to);
}
$statement_date_to = escapeSql($statement_date_to);
$statement_date_from = $_POST['dtf'] ?? '';
if (empty($statement_date_from)) {
// Blank means the whole history, so there is no lower bound to apply
$statement_date_query = '';
$statement_period = "up to $statement_date_to";
} else {
$statement_date_from = escapeSql(validateDate($statement_date_from));
$statement_date_query = "AND invoice_date >= '$statement_date_from'";
$statement_period = "$statement_date_from to $statement_date_to";
}
$include_paid = !empty($_POST['include_paid']);
// Recipients - same picker contract as the invoice send, so the same
// client scoping applies and a tampered contact_id matches nothing
$selected_contacts = $_POST['contacts'] ?? [];
if (!is_array($selected_contacts)) {
$selected_contacts = [];
}
$selected_contact_ids = array_filter(array_unique(array_map('intval', $selected_contacts)));
if (empty($selected_contact_ids)) {
flashAlert("Select at least one contact to send to", 'error');
redirect();
}
$selected_contact_id_list = implode(',', $selected_contact_ids);
$sql_recipients = mysqli_query(
$mysqli,
"SELECT contact_email, contact_name FROM contacts
WHERE contact_id IN ($selected_contact_id_list)
AND contact_client_id = $client_id
AND contact_archived_at IS NULL
AND contact_email IS NOT NULL
AND contact_email != ''
ORDER BY contact_primary DESC, contact_billing DESC, contact_name ASC"
);
if (mysqli_num_rows($sql_recipients) == 0) {
flashAlert("None of the selected contacts have a usable email address", 'error');
redirect();
}
// The statement lines. Draft / Cancelled / Non-Billable are not money the
// client owes, so they never appear - same exclusion the Outstanding
// Balances report uses. Payments are summed in a derived table rather than
// joined directly, or an invoice with two payments would be counted twice.
$sql_statement = mysqli_query(
$mysqli,
"SELECT invoice_amount, invoice_date, invoice_due, invoice_id, invoice_number, invoice_prefix,
invoice_scope, invoice_status, invoice_url_key, IFNULL(amount_paid, 0) AS amount_paid
FROM invoices
LEFT JOIN (
SELECT payment_invoice_id, SUM(payment_amount) AS amount_paid FROM payments
WHERE payment_archived_at IS NULL
GROUP BY payment_invoice_id
) AS invoice_payments ON payment_invoice_id = invoice_id
WHERE invoice_client_id = $client_id
AND invoice_status NOT IN ('Draft', 'Cancelled', 'Non-Billable')
AND invoice_date <= '$statement_date_to'
$statement_date_query
" . ($include_paid ? '' : "AND invoice_amount - IFNULL(invoice_payments.amount_paid, 0) > 0") . "
ORDER BY invoice_date ASC, invoice_number ASC"
);
if (mysqli_num_rows($sql_statement) == 0) {
flashAlert("No invoices to report for that period - nothing sent", 'error');
redirect();
}
$sql = mysqli_query($mysqli, "SELECT company_name, company_phone, company_phone_country_code FROM companies WHERE company_id = 1");
$row = mysqli_fetch_assoc($sql);
$company_name = escapeSql($row['company_name']);
$company_phone = escapeSql(formatPhoneNumber($row['company_phone'], $row['company_phone_country_code']));
$config_invoice_from_name = escapeSql($config_invoice_from_name);
$config_invoice_from_email = escapeSql($config_invoice_from_email);
$config_base_url = escapeSql($config_base_url);
// Build the table once - it is identical for every recipient, only the
// greeting differs. Everything interpolated here is already escaped:
// addToMailQueue() writes the body into the queue raw.
$statement_rows = '';
$statement_total = 0;
$statement_line_count = 0;
while ($row = mysqli_fetch_assoc($sql_statement)) {
$invoice_id = intval($row['invoice_id']);
$invoice_prefix = escapeSql($row['invoice_prefix']);
$invoice_number = intval($row['invoice_number']);
$invoice_scope = escapeSql($row['invoice_scope']);
$invoice_status = escapeSql($row['invoice_status']);
$invoice_date = escapeSql($row['invoice_date']);
$invoice_due = escapeSql($row['invoice_due']);
$invoice_url_key = escapeSql($row['invoice_url_key']);
$invoice_amount = floatval($row['invoice_amount']);
$amount_paid = floatval($row['amount_paid']);
$invoice_balance = $invoice_amount - $amount_paid;
$statement_total += $invoice_balance;
$statement_line_count++;
$invoice_link = "https://$config_base_url/guest/guest_view_invoice.php?invoice_id=$invoice_id&url_key=$invoice_url_key";
$overdue_flag = '';
if ($invoice_balance > 0 && !empty($invoice_due) && $invoice_due < date("Y-m-d")) {
$overdue_flag = ' <span style="color:#dc3545;">(overdue)</span>';
}
$statement_rows .= '<tr>'
. '<td style="padding:6px 10px; border-bottom:1px solid #dee2e6;"><a href=\'' . $invoice_link . '\'>' . "$invoice_prefix$invoice_number" . '</a></td>'
. '<td style="padding:6px 10px; border-bottom:1px solid #dee2e6;">' . $invoice_scope . '</td>'
. '<td style="padding:6px 10px; border-bottom:1px solid #dee2e6;">' . $invoice_date . '</td>'
. '<td style="padding:6px 10px; border-bottom:1px solid #dee2e6;">' . $invoice_due . $overdue_flag . '</td>'
. '<td style="padding:6px 10px; border-bottom:1px solid #dee2e6; text-align:right;">' . numfmt_format_currency($currency_format, $invoice_amount, $client_currency_code) . '</td>'
. '<td style="padding:6px 10px; border-bottom:1px solid #dee2e6; text-align:right;">' . numfmt_format_currency($currency_format, $amount_paid, $client_currency_code) . '</td>'
. '<td style="padding:6px 10px; border-bottom:1px solid #dee2e6; text-align:right;">' . numfmt_format_currency($currency_format, $invoice_balance, $client_currency_code) . '</td>'
. '</tr>';
}
$statement_table = '<table cellpadding="0" cellspacing="0" style="border-collapse:collapse; font-size:13px;">'
. '<tr style="background:#343a40; color:#ffffff;">'
. '<th style="padding:6px 10px; text-align:left;">Invoice</th>'
. '<th style="padding:6px 10px; text-align:left;">Scope</th>'
. '<th style="padding:6px 10px; text-align:left;">Date</th>'
. '<th style="padding:6px 10px; text-align:left;">Due</th>'
. '<th style="padding:6px 10px; text-align:right;">Amount</th>'
. '<th style="padding:6px 10px; text-align:right;">Paid</th>'
. '<th style="padding:6px 10px; text-align:right;">Balance</th>'
. '</tr>'
. $statement_rows
. '<tr><td colspan="6" style="padding:8px 10px; text-align:right;"><b>Total Balance Due</b></td>'
. '<td style="padding:8px 10px; text-align:right;"><b>' . numfmt_format_currency($currency_format, $statement_total, $client_currency_code) . '</b></td></tr>'
. '</table>';
$subject = "Account Statement - $client_name";
$data = [];
$recipient_labels = [];
while ($recipient = mysqli_fetch_assoc($sql_recipients)) {
$contact_name = escapeSql($recipient['contact_name']);
$contact_email = escapeSql($recipient['contact_email']);
$body = "Hello $contact_name,<br><br>Please find your account statement for $statement_period below.<br><br>"
. $statement_table
. "<br><br>Click any invoice number above to view or pay it online.<br><br><br>--<br>$company_name - Billing<br>$config_invoice_from_email<br>$company_phone";
$data[] = [
'from' => $config_invoice_from_email,
'from_name' => $config_invoice_from_name,
'recipient' => $contact_email,
'recipient_name' => $contact_name,
'subject' => $subject,
'body' => $body
];
$recipient_labels[] = "$contact_name <$contact_email>";
logAudit("Invoice", "Statement", "$session_name emailed an account statement ($statement_period, $statement_line_count invoices) to $contact_email", $client_id);
}
addToMailQueue($data);
$recipient_count = count($recipient_labels);
flashAlert("Statement queued to $recipient_count " . ($recipient_count == 1 ? "recipient" : "recipients"));
redirect();
}
if (isExportRequest('export_invoices')) {
validateCSRFToken();