Phase E: Add Bank Reconciliation

- IsCleared + ClearedDate added to Payment, BillPayment, Expense entities
- BankReconciliation entity (account, statement date, beginning/ending balance, status)
- BankReconciliationStatus enum (InProgress, Completed)
- Migration AddBankReconciliation: new BankReconciliations table + IsCleared/ClearedDate columns
- IUnitOfWork/UnitOfWork wired with BankReconciliations repo
- BankReconciliationsController: Index, Create, Reconcile, ToggleCleared (AJAX), Complete, Report
- Reconcile view: deposit/payment checkboxes with live running balance and difference via JS
- Complete is gated: only enabled when difference == $0.00
- Nav: Bank Reconciliation added to Finance section in _Layout

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-10 00:10:38 -04:00
parent cf9dcfb4c1
commit 1229081436
15 changed files with 11111 additions and 3 deletions
@@ -0,0 +1,189 @@
@using PowderCoating.Web.Controllers
@{
ViewData["Title"] = "Reconcile";
var recon = ViewBag.Recon as PowderCoating.Core.Entities.BankReconciliation;
var deposits = ViewBag.Deposits as List<ReconciliationItem> ?? new();
var payments = ViewBag.Payments as List<ReconciliationItem> ?? new();
}
<div class="d-flex align-items-center mb-3 gap-2 flex-wrap">
<a asp-action="Index" class="btn btn-sm btn-outline-secondary">
<i class="bi bi-arrow-left me-1"></i>All Reconciliations
</a>
<h4 class="mb-0 fw-semibold ms-2">Reconcile: @recon?.Account?.Name</h4>
<span class="text-muted small">Statement date: @recon?.StatementDate.ToString("MMM d, yyyy")</span>
</div>
@if (TempData["Error"] != null)
{
<div class="alert alert-danger alert-permanent alert-dismissible fade show">
@TempData["Error"]
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
}
<div class="row g-3 mb-3">
<div class="col-md-3 text-center">
<div class="card shadow-sm py-3">
<div class="fw-bold fs-5">@recon?.BeginningBalance.ToString("C")</div>
<div class="text-muted small">Beginning Balance</div>
</div>
</div>
<div class="col-md-3 text-center">
<div class="card shadow-sm py-3">
<div class="fw-bold fs-5">@recon?.EndingBalance.ToString("C")</div>
<div class="text-muted small">Statement Ending Balance</div>
</div>
</div>
<div class="col-md-3 text-center">
<div class="card shadow-sm py-3">
<div class="fw-bold fs-5" id="clearedBalance">@recon?.BeginningBalance.ToString("C")</div>
<div class="text-muted small">Cleared Balance</div>
</div>
</div>
<div class="col-md-3 text-center">
<div class="card shadow-sm py-3">
<div class="fw-bold fs-5" id="difference">—</div>
<div class="text-muted small">Difference</div>
</div>
</div>
</div>
<div class="row g-3 mb-3">
<div class="col-md-6">
<div class="card shadow-sm">
<div class="card-header fw-semibold">Deposits / Credits</div>
<div class="card-body p-0">
<table class="table table-sm mb-0">
<thead class="table-light">
<tr><th>Date</th><th>Reference</th><th class="text-end">Amount</th><th class="text-center">Cleared</th></tr>
</thead>
<tbody>
@if (!deposits.Any())
{
<tr><td colspan="4" class="text-center text-muted py-3">No deposits found.</td></tr>
}
@foreach (var item in deposits.OrderBy(d => d.Date))
{
<tr class="recon-row" data-type="@item.EntityType" data-id="@item.EntityId"
data-amount="@item.Amount.ToString("F2")" data-direction="deposit">
<td class="small">@item.Date.ToString("MMM d")</td>
<td class="small text-truncate" style="max-width:120px" title="@item.Description">@item.Reference</td>
<td class="text-end">@item.Amount.ToString("C")</td>
<td class="text-center">
<input type="checkbox" class="form-check-input cleared-checkbox"
@(item.IsCleared ? "checked" : "") />
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card shadow-sm">
<div class="card-header fw-semibold">Payments / Debits</div>
<div class="card-body p-0">
<table class="table table-sm mb-0">
<thead class="table-light">
<tr><th>Date</th><th>Reference</th><th class="text-end">Amount</th><th class="text-center">Cleared</th></tr>
</thead>
<tbody>
@if (!payments.Any())
{
<tr><td colspan="4" class="text-center text-muted py-3">No payments found.</td></tr>
}
@foreach (var item in payments)
{
<tr class="recon-row" data-type="@item.EntityType" data-id="@item.EntityId"
data-amount="@item.Amount.ToString("F2")" data-direction="payment">
<td class="small">@item.Date.ToString("MMM d")</td>
<td class="small text-truncate" style="max-width:120px" title="@item.Description">@item.Reference</td>
<td class="text-end">@item.Amount.ToString("C")</td>
<td class="text-center">
<input type="checkbox" class="form-check-input cleared-checkbox"
@(item.IsCleared ? "checked" : "") />
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
<form asp-action="Complete" method="post" id="completeForm">
@Html.AntiForgeryToken()
<input type="hidden" name="id" value="@recon?.Id" />
<input type="hidden" name="difference" id="differenceHidden" value="9999" />
<button type="submit" class="btn btn-success" id="completeBtn" disabled>
<i class="bi bi-check-circle me-1"></i>Complete Reconciliation
</button>
<span class="ms-2 text-muted small">Complete is enabled only when difference is $0.00</span>
</form>
@section Scripts {
<script>
(function() {
const reconId = @recon?.Id;
const beginning = @recon?.BeginningBalance.ToString("F2");
const ending = @recon?.EndingBalance.ToString("F2");
let token = document.querySelector('input[name="__RequestVerificationToken"]').value;
function recalculate() {
let clearedDeposits = 0, clearedPayments = 0;
document.querySelectorAll('.recon-row').forEach(row => {
const cb = row.querySelector('.cleared-checkbox');
const amt = parseFloat(row.dataset.amount);
if (!cb.checked) return;
if (row.dataset.direction === 'deposit') clearedDeposits += amt;
else clearedPayments += amt;
});
const cleared = beginning + clearedDeposits - clearedPayments;
const difference = ending - cleared;
document.getElementById('clearedBalance').textContent =
cleared.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
const diffEl = document.getElementById('difference');
diffEl.textContent = difference.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
diffEl.className = Math.abs(difference) < 0.005 ? 'fw-bold fs-5 text-success' : 'fw-bold fs-5 text-danger';
document.getElementById('differenceHidden').value = difference.toFixed(2);
document.getElementById('completeBtn').disabled = Math.abs(difference) >= 0.005;
}
document.querySelectorAll('.cleared-checkbox').forEach(cb => {
cb.addEventListener('change', async function() {
const row = this.closest('.recon-row');
const type = row.dataset.type;
const id = row.dataset.id;
const cleared = this.checked;
try {
const resp = await fetch('/BankReconciliations/ToggleCleared', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'RequestVerificationToken': token
},
body: new URLSearchParams({
reconId, entityType: type, entityId: id, isCleared: cleared
})
});
if (!resp.ok) this.checked = !cleared; // revert on error
} catch {
this.checked = !cleared; // revert on network error
}
recalculate();
});
});
recalculate();
})();
</script>
}