Phase F: Customer/Vendor Statements, Payment Terms Parser, Tax Rates

F1: GetCustomerStatementAsync/GetVendorStatementAsync on IFinancialReportService;
    StatementLineDto; CustomerStatementDto/VendorStatementDto; Statement action on
    CustomersController + VendorsController; Statement views + PDF download via
    StatementPdfHelper (QuestPDF); Statement button on Customer/Vendor Details pages.

F2: PaymentTermsParser static helper (CalculateDueDate, ParseEarlyPaymentDiscount);
    EarlyPaymentDiscountPercent/Days on Invoice entity; GetCustomerPaymentTerms AJAX
    endpoint on InvoicesController auto-populates Terms + due date on customer select;
    early payment discount notice on Invoice Create.

F3: TaxRate entity (Name/Rate/State/IsDefault/IsActive, tenant-filtered);
    IUnitOfWork.TaxRates + UnitOfWork + ApplicationDbContext; TaxRatesController
    (Index/Create/Edit/Delete/ToggleActive, CompanyAdminOnly); GetTaxRateForCustomer
    AJAX endpoint; Tax Rates in Settings gear menu.

Also fixes AddVendorCredits migration: VendorCreditApplications FKs changed from
CASCADE to NoAction to resolve SQL Server error 1785 (multiple cascade paths).
Migration: AddPaymentTermsAndTaxRates applied locally; 200/200 unit tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-10 10:55:22 -04:00
parent 1229081436
commit d3a5d827f9
27 changed files with 11492 additions and 12 deletions
@@ -0,0 +1,49 @@
using System.Text.RegularExpressions;
namespace PowderCoating.Web.Helpers;
/// <summary>
/// Parses payment terms strings (e.g., "Net 30", "2/10 Net 30", "Due on Receipt")
/// to compute due dates and extract early-payment discount terms.
/// </summary>
public static class PaymentTermsParser
{
private static readonly Regex NetDaysRegex = new(@"\bnet\s+(\d+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex EarlyDiscountRegex = new(@"(\d+(?:\.\d+)?)/(\d+)\s+net", RegexOptions.IgnoreCase | RegexOptions.Compiled);
/// <summary>
/// Returns the due date calculated from <paramref name="invoiceDate"/> and the supplied terms.
/// Returns null when the terms string cannot be parsed.
/// </summary>
public static DateTime? CalculateDueDate(string? terms, DateTime invoiceDate)
{
if (string.IsNullOrWhiteSpace(terms)) return null;
var normalized = terms.Trim().ToLowerInvariant();
if (Regex.IsMatch(normalized, @"\b(receipt|due\s*now|cod|immediate)\b"))
return invoiceDate;
var match = NetDaysRegex.Match(terms);
if (match.Success && int.TryParse(match.Groups[1].Value, out var days))
return invoiceDate.AddDays(days);
return null;
}
/// <summary>
/// Extracts early-payment discount terms from a string like "2/10 Net 30".
/// Returns (percent: 2, days: 10) for that example, or (0, 0) if not present.
/// </summary>
public static (decimal Percent, int Days) ParseEarlyPaymentDiscount(string? terms)
{
if (string.IsNullOrWhiteSpace(terms)) return (0, 0);
var match = EarlyDiscountRegex.Match(terms);
if (match.Success
&& decimal.TryParse(match.Groups[1].Value, out var percent)
&& int.TryParse(match.Groups[2].Value, out var days))
return (percent, days);
return (0, 0);
}
}
@@ -0,0 +1,111 @@
using PowderCoating.Application.DTOs.Accounting;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
namespace PowderCoating.Web.Helpers;
/// <summary>
/// Generates a QuestPDF account-activity statement. Shared by customer and vendor statement actions.
/// </summary>
public static class StatementPdfHelper
{
/// <summary>Generates and returns the raw PDF bytes for a statement.</summary>
public static byte[] Generate(
string entityName,
string companyName,
string? address,
DateTime from,
DateTime to,
decimal openingBalance,
List<StatementLineDto> lines,
decimal closingBalance,
bool isVendor)
{
var doc = Document.Create(container =>
{
container.Page(page =>
{
page.Margin(40);
page.Size(PageSizes.Letter);
page.DefaultTextStyle(t => t.FontSize(10));
page.Header().Element(h =>
{
h.Column(col =>
{
col.Item().Text(companyName).Bold().FontSize(16);
col.Item().Text($"{(isVendor ? "Vendor" : "Customer")} Statement").FontSize(12).FontColor("#555555");
col.Item().Text($"Period: {from:MMM d, yyyy} {to:MMM d, yyyy}");
col.Item().PaddingTop(4).Text(entityName).Bold();
if (!string.IsNullOrWhiteSpace(address))
col.Item().Text(address).FontColor("#555555");
});
});
page.Content().PaddingTop(16).Column(col =>
{
col.Item().Table(t =>
{
t.ColumnsDefinition(c =>
{
c.ConstantColumn(72); // Date
c.RelativeColumn(2); // Type / Ref
c.RelativeColumn(3); // Description
c.ConstantColumn(80); // Debit
c.ConstantColumn(80); // Credit
c.ConstantColumn(90); // Balance
});
static IContainer HeaderCell(IContainer c) =>
c.Background("#2c3e50").PaddingVertical(4).PaddingHorizontal(6);
t.Header(h =>
{
h.Cell().Element(HeaderCell).Text("Date").FontColor("white").Bold();
h.Cell().Element(HeaderCell).Text("Type / Ref").FontColor("white").Bold();
h.Cell().Element(HeaderCell).Text("Description").FontColor("white").Bold();
h.Cell().Element(HeaderCell).AlignRight().Text("Debit").FontColor("white").Bold();
h.Cell().Element(HeaderCell).AlignRight().Text("Credit").FontColor("white").Bold();
h.Cell().Element(HeaderCell).AlignRight().Text("Balance").FontColor("white").Bold();
});
static IContainer DataCell(IContainer c) =>
c.BorderBottom(0.5f).BorderColor("#dddddd").PaddingVertical(3).PaddingHorizontal(6);
// Opening balance row
t.Cell().Element(DataCell).Text(from.AddDays(-1).ToString("MM/dd/yy")).FontColor("#888888");
t.Cell().Element(DataCell).Text("Opening Balance").Bold();
t.Cell().Element(DataCell).Text("");
t.Cell().Element(DataCell).Text("");
t.Cell().Element(DataCell).Text("");
t.Cell().Element(DataCell).AlignRight().Text(openingBalance.ToString("C"));
foreach (var line in lines)
{
t.Cell().Element(DataCell).Text(line.Date.ToString("MM/dd/yy"));
t.Cell().Element(DataCell).Text($"{line.Type}\n{line.Reference}");
t.Cell().Element(DataCell).Text(line.Description);
t.Cell().Element(DataCell).AlignRight().Text(line.Debit.HasValue ? line.Debit.Value.ToString("C") : "");
t.Cell().Element(DataCell).AlignRight().Text(line.Credit.HasValue ? line.Credit.Value.ToString("C") : "");
t.Cell().Element(DataCell).AlignRight().Text(line.RunningBalance.ToString("C"));
}
static IContainer TotalCell(IContainer c) =>
c.Background("#f0f4f8").PaddingVertical(4).PaddingHorizontal(6);
t.Cell().ColumnSpan(5).Element(TotalCell).Text("Closing Balance").Bold();
t.Cell().Element(TotalCell).AlignRight().Text(closingBalance.ToString("C")).Bold();
});
});
page.Footer().AlignCenter().Text(t =>
{
t.Span($"Generated {DateTime.Now:MM/dd/yyyy HH:mm}").FontSize(8).FontColor("#888888");
});
});
});
return doc.GeneratePdf();
}
}