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
@@ -1851,6 +1851,54 @@ public class InvoicesController : Controller
return $"{prefix}{(maxNum + 1):D4}";
}
/// <summary>
/// Returns the customer's payment terms, derived due date, and early-payment discount info
/// for the Invoice Create form so JavaScript can auto-populate those fields on customer selection.
/// </summary>
[HttpGet]
public async Task<IActionResult> GetCustomerPaymentTerms(int customerId)
{
var customer = await _unitOfWork.Customers.GetByIdAsync(customerId);
if (customer == null) return NotFound();
var invoiceDate = DateTime.Today;
var dueDate = PaymentTermsParser.CalculateDueDate(customer.PaymentTerms, invoiceDate);
var (discountPercent, discountDays) = PaymentTermsParser.ParseEarlyPaymentDiscount(customer.PaymentTerms);
return Json(new
{
paymentTerms = customer.PaymentTerms,
dueDate = dueDate?.ToString("yyyy-MM-dd"),
earlyPaymentDiscountPercent = discountPercent,
earlyPaymentDiscountDays = discountDays,
isTaxExempt = customer.IsTaxExempt
});
}
/// <summary>
/// Returns the default active tax rate for the current company, or zero for tax-exempt customers.
/// Called by the Invoice Create form when the customer selection changes.
/// </summary>
[HttpGet]
public async Task<IActionResult> GetTaxRateForCustomer(int customerId)
{
var companyId = _tenantContext.GetCurrentCompanyId() ?? 0;
var customer = await _unitOfWork.Customers.GetByIdAsync(customerId);
if (customer == null) return NotFound();
if (customer.IsTaxExempt)
return Json(new { taxPercent = 0m, taxRateName = (string?)null });
var defaultRate = await _unitOfWork.TaxRates
.FirstOrDefaultAsync(r => r.IsDefault && r.IsActive && !r.IsDeleted);
return Json(new
{
taxPercent = defaultRate?.Rate ?? 0m,
taxRateName = defaultRate?.Name
});
}
/// <summary>
/// Populates ViewBag data used by both Create GET and Create POST (on validation failure re-display):
/// — Active customer list for the customer dropdown.