mirror of
https://github.com/itflow-org/itflow
synced 2026-11-20 03:17:24 +00:00
Client portal stage 2: billing in the portal - invoices and quotes open in the portal with PDF download and print, Pay invoices pays several invoices as one Stripe card payment with a saved card or a new card on a checkout page (one payment per invoice under a shared reference, booked once under a lock, one receipt and one paid notification), quotes accepted or declined with the contact's name and a note plus signed copy or PO upload, payments list with printable receipts, statement with aging and the shared statement PDF, recurring invoice page with autopay, Cards and autopay on one page, quotes waiting and past-due invoices on home, Ask us links prefill a ticket subject; shared billingQuoteAnswer and billingQuoteAddFiles used by the guest links too, new functions/billing_pay.php, Stripe-aware CSP for card pages with js/portal_stripe.js, nightly Stripe fee reconciled once per payment reference, Stripe refund limited to the payment's own amount, old add_payment_by_provider removed
This commit is contained in:
@@ -1,162 +1,130 @@
|
||||
<?php
|
||||
/*
|
||||
* Client Portal
|
||||
* Account Statement
|
||||
* Statement - what is owed and how late it is (current, then 1-30, 31-60,
|
||||
* 61-90 and over 90 days past due), with the invoices behind it; or
|
||||
* everything from the last twelve months, paid ones too. The PDF is the same
|
||||
* statement the agents email (billingStatement() in
|
||||
* functions/billing_email.php).
|
||||
*
|
||||
* SCOPING: every query on this page is keyed to $session_client_id, which
|
||||
* check_login.php sets from the session. Nothing here reads a client id from
|
||||
* the request, so there is no id for a contact to tamper with - and
|
||||
* enforceContactCan('accounting') keeps it to primary and billing contacts,
|
||||
* matching invoices.php and quotes.php.
|
||||
* SCOPING: billingStatement() is given $session_client_id from the session -
|
||||
* nothing here reads a client id from the request.
|
||||
*/
|
||||
|
||||
header("Content-Security-Policy: default-src 'self'");
|
||||
|
||||
require_once "includes/inc_all.php";
|
||||
|
||||
portalBillingGate();
|
||||
|
||||
enforceContactCan('accounting');
|
||||
|
||||
/*
|
||||
* Amounts render in the client's own currency, matching the guest invoice view
|
||||
* and the statement an agent emails. This page and its PDF used to render in
|
||||
* the company currency, so the same invoice carried a different symbol
|
||||
* depending on where you read it.
|
||||
*/
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT client_currency_code FROM clients WHERE client_id = $session_client_id LIMIT 1"));
|
||||
$statement_currency_code = escapeHtml($row['client_currency_code']);
|
||||
if (empty($statement_currency_code)) {
|
||||
$statement_currency_code = $session_company_currency;
|
||||
}
|
||||
|
||||
/*
|
||||
* Payments are summed in a derived table rather than joined directly, or an
|
||||
* invoice with two payments against it would be counted twice.
|
||||
*
|
||||
* Draft / Cancelled / Non-Billable are not money owed, and the balance test
|
||||
* drops anything fully paid, so Paid invoices fall out without naming them.
|
||||
*/
|
||||
$statement_sql = mysqli_query(
|
||||
$mysqli,
|
||||
"SELECT invoice_amount, invoice_date, invoice_due, invoice_id, invoice_number, invoice_prefix,
|
||||
invoice_scope, 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 = $session_client_id
|
||||
AND invoice_status NOT IN ('Draft', 'Cancelled', 'Non-Billable')
|
||||
AND invoice_amount - IFNULL(invoice_payments.amount_paid, 0) > 0
|
||||
ORDER BY invoice_date ASC, invoice_number ASC"
|
||||
);
|
||||
|
||||
$statement_count = mysqli_num_rows($statement_sql);
|
||||
$statement_total = 0;
|
||||
|
||||
$with_paid = ($_GET['view'] ?? '') === 'year';
|
||||
$statement = billingStatement($session_client_id, $with_paid ? date('Y-m-d', strtotime('-12 months')) : '', '', $with_paid);
|
||||
$currency = $statement['currency'];
|
||||
$cards = billingStripeProvider() !== null;
|
||||
$money = function ($amount) use ($currency) {
|
||||
return escapeHtml(billingMoney($amount, $currency));
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h3 class="mb-0">Account Statement</h3>
|
||||
<?php if ($statement_count > 0) { ?>
|
||||
<a class="btn btn-primary" href="post.php?export_statement_pdf=1&csrf_token=<?= $_SESSION['csrf_token'] ?>">
|
||||
<i class="fa fa-fw fa-download me-2"></i>Download PDF
|
||||
</a>
|
||||
<?php } ?>
|
||||
<div class="itflow-portal-page-head">
|
||||
<div>
|
||||
<h1>Statement</h1>
|
||||
<p><?= escapeHtml($session_client_name) ?> · as of <?= escapeHtml(date('M j, Y')) ?></p>
|
||||
</div>
|
||||
<div class="d-flex flex-wrap gap-2">
|
||||
<?php if ($statement['invoices']) { ?>
|
||||
<a class="btn btn-outline-secondary" href="post.php?export_statement_pdf=1<?= $with_paid ? '&paid=1' : '' ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>"><i class="fas fa-file-pdf me-2"></i>Download PDF</a>
|
||||
<?php } ?>
|
||||
<?php if ($statement['total'] > 0 && $cards) { ?>
|
||||
<a class="btn btn-primary" href="unpaid_invoices.php"><i class="fas fa-credit-card me-2"></i>Pay invoices</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="row g-3 mb-4">
|
||||
<div class="col-6 col-lg-3">
|
||||
<div class="itflow-portal-stat">
|
||||
<span class="itflow-portal-stat-icon"><i class="fas fa-file-invoice-dollar"></i></span>
|
||||
<span>
|
||||
<span class="itflow-portal-stat-value d-block"><?= $money($statement['total']) ?></span>
|
||||
<span class="itflow-portal-stat-label">Total due</span>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-6 col-lg-3">
|
||||
<div class="itflow-portal-stat<?= $statement['overdue'] > 0 ? ' itflow-portal-stat-danger' : '' ?>">
|
||||
<span class="itflow-portal-stat-icon"><i class="fas fa-exclamation-circle"></i></span>
|
||||
<span>
|
||||
<span class="itflow-portal-stat-value d-block"><?= $money($statement['overdue']) ?></span>
|
||||
<span class="itflow-portal-stat-label">Past due</span>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-lg-6">
|
||||
<div class="itflow-portal-aging" aria-label="How late">
|
||||
<?php foreach ($statement['aging'] as $bucket => $amount) { ?>
|
||||
<div class="<?= $amount > 0 && $bucket !== 'Current' ? 'is-late' : '' ?>">
|
||||
<span><?= escapeHtml($bucket) ?></span>
|
||||
<strong class="itflow-money"><?= $money($amount) ?></strong>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-10">
|
||||
<nav class="itflow-portal-tabs" aria-label="Statement">
|
||||
<a class="<?= !$with_paid ? 'active' : '' ?>" href="statement.php"><i class="fas fa-hourglass-half"></i>Open invoices</a>
|
||||
<a class="<?= $with_paid ? 'active' : '' ?>" href="statement.php?view=year"><i class="fas fa-calendar-alt"></i>Last 12 months</a>
|
||||
</nav>
|
||||
|
||||
<?php if ($statement_count == 0) { ?>
|
||||
|
||||
<?= portalEmptyState('There is nothing outstanding on this account.', 'fa-check', 'success') ?>
|
||||
|
||||
<?php } else { ?>
|
||||
|
||||
<table class="table table-bordered border border-dark">
|
||||
<thead class="table-dark">
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<th>Scope</th>
|
||||
<th>Date</th>
|
||||
<th>Due</th>
|
||||
<th class="text-end">Amount</th>
|
||||
<th class="text-end">Paid</th>
|
||||
<th class="text-end">Balance</th>
|
||||
</tr>
|
||||
<section class="itflow-portal-panel">
|
||||
<?php if (!$statement['invoices']) { ?>
|
||||
<?= $with_paid
|
||||
? emptyStateHtml(['title' => 'No invoices in the last 12 months', 'text' => '', 'icon' => 'fa-file-alt', 'filtered' => false])
|
||||
: emptyStateHtml(['title' => 'Nothing outstanding', 'text' => 'You are all paid up - thank you.', 'icon' => 'fa-check', 'filtered' => false]) ?>
|
||||
<?php } else { ?>
|
||||
<div class="table-responsive">
|
||||
<table class="table align-middle mb-0 itflow-portal-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Invoice</th>
|
||||
<th class="d-none d-md-table-cell">Date</th>
|
||||
<th>Due</th>
|
||||
<th class="text-end d-none d-md-table-cell">Amount</th>
|
||||
<th class="text-end d-none d-md-table-cell">Paid</th>
|
||||
<th class="text-end">Balance</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
<?php
|
||||
|
||||
while ($row = mysqli_fetch_assoc($statement_sql)) {
|
||||
$invoice_id = intval($row['invoice_id']);
|
||||
$invoice_prefix = escapeHtml($row['invoice_prefix']);
|
||||
$invoice_number = intval($row['invoice_number']);
|
||||
$invoice_scope = escapeHtml($row['invoice_scope']);
|
||||
$invoice_date = escapeHtml($row['invoice_date']);
|
||||
$invoice_due = escapeHtml($row['invoice_due']);
|
||||
$invoice_url_key = escapeHtml($row['invoice_url_key']);
|
||||
$invoice_amount = floatval($row['invoice_amount']);
|
||||
$amount_paid = floatval($row['amount_paid']);
|
||||
$invoice_balance = $invoice_amount - $amount_paid;
|
||||
|
||||
$statement_total = $statement_total + $invoice_balance;
|
||||
|
||||
if (empty($invoice_scope)) {
|
||||
$invoice_scope_display = "-";
|
||||
} else {
|
||||
$invoice_scope_display = $invoice_scope;
|
||||
}
|
||||
|
||||
// Same one-day grace as invoices.php, so the two pages agree
|
||||
// on what counts as late
|
||||
if (strtotime($invoice_due) + 86400 < time()) {
|
||||
$overdue_color = "text-danger fw-bold";
|
||||
} else {
|
||||
$overdue_color = "";
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<a target="_blank" href="//<?= $config_base_url ?>/guest/guest_view_invoice.php?invoice_id=<?= "$invoice_id&url_key=$invoice_url_key" ?>">
|
||||
<?= "$invoice_prefix$invoice_number" ?>
|
||||
</a>
|
||||
</td>
|
||||
<td><?= $invoice_scope_display ?></td>
|
||||
<td><?= $invoice_date ?></td>
|
||||
<td class="<?= $overdue_color ?>"><?= $invoice_due ?></td>
|
||||
<td class="text-end"><?= numfmt_format_currency($currency_format, $invoice_amount, $statement_currency_code) ?></td>
|
||||
<td class="text-end"><?= numfmt_format_currency($currency_format, $amount_paid, $statement_currency_code) ?></td>
|
||||
<td class="text-end fw-bold"><?= numfmt_format_currency($currency_format, $invoice_balance, $statement_currency_code) ?></td>
|
||||
</tr>
|
||||
|
||||
<?php
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<?php foreach ($statement['invoices'] as $row) {
|
||||
$late = $row['balance'] > 0 && $row['days'] > 0; ?>
|
||||
<tr>
|
||||
<td>
|
||||
<a class="fw-semibold" href="invoice.php?id=<?= intval($row['invoice_id']) ?>"><?= escapeHtml($row['invoice_prefix'] . $row['invoice_number']) ?></a>
|
||||
<?php if (trim((string) $row['invoice_scope']) !== '') { ?><div class="small text-body-secondary text-truncate"><?= escapeHtml($row['invoice_scope']) ?></div><?php } ?>
|
||||
</td>
|
||||
<td class="text-nowrap d-none d-md-table-cell"><?= escapeHtml(date('M j, Y', strtotime($row['invoice_date']))) ?></td>
|
||||
<td class="text-nowrap<?= $late ? ' text-danger-emphasis' : '' ?>">
|
||||
<?= escapeHtml(date('M j, Y', strtotime($row['invoice_due']))) ?>
|
||||
<?php if ($late) { ?><div class="small"><?= intval($row['days']) ?> <?= intval($row['days']) === 1 ? 'day' : 'days' ?> late</div><?php } ?>
|
||||
</td>
|
||||
<td class="text-end itflow-money d-none d-md-table-cell"><?= $money($row['invoice_amount']) ?></td>
|
||||
<td class="text-end itflow-money d-none d-md-table-cell"><?= $money($row['paid']) ?></td>
|
||||
<td class="text-end itflow-money fw-semibold"><?= $row['balance'] > 0 ? $money($row['balance']) : '<span class="text-success-emphasis">Paid</span>' ?></td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<th colspan="6" class="text-end">Total Balance Due</th>
|
||||
<th class="text-end"><?= numfmt_format_currency($currency_format, $statement_total, $statement_currency_code) ?></th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="d-none d-md-table-cell" colspan="3"></td>
|
||||
<th colspan="2" class="text-end">Total due</th>
|
||||
<th class="text-end itflow-money"><?= $money($statement['total']) ?></th>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
|
||||
<?php } ?>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<?php } ?>
|
||||
</section>
|
||||
|
||||
<?php
|
||||
require_once "includes/footer.php";
|
||||
|
||||
Reference in New Issue
Block a user