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,139 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PowderCoating.Core.Entities;
using PowderCoating.Core.Interfaces;
using PowderCoating.Shared.Constants;
namespace PowderCoating.Web.Controllers;
/// <summary>
/// Manages named tax rates used to pre-fill the tax percent field on invoices when a taxable
/// customer is selected. Only one rate may be marked as default at a time; that default is
/// auto-applied via the GetTaxRateForCustomer AJAX endpoint on the Invoice Create form.
/// </summary>
[Authorize(Policy = AppConstants.Policies.CompanyAdminOnly)]
public class TaxRatesController : Controller
{
private readonly IUnitOfWork _unitOfWork;
private readonly ITenantContext _tenantContext;
private readonly ILogger<TaxRatesController> _logger;
public TaxRatesController(
IUnitOfWork unitOfWork,
ITenantContext tenantContext,
ILogger<TaxRatesController> logger)
{
_unitOfWork = unitOfWork;
_tenantContext = tenantContext;
_logger = logger;
}
/// <summary>Lists all tax rates for the current company.</summary>
public async Task<IActionResult> Index()
{
var rates = await _unitOfWork.TaxRates.GetAllAsync();
return View(rates.OrderBy(r => r.Name).ToList());
}
[HttpGet]
public IActionResult Create() => View(new TaxRate());
/// <summary>Creates a new tax rate. Enforces that only one rate is the default.</summary>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(TaxRate model)
{
if (!ModelState.IsValid) return View(model);
if (model.IsDefault)
await ClearOtherDefaultsAsync(0);
await _unitOfWork.TaxRates.AddAsync(model);
await _unitOfWork.CompleteAsync();
TempData["Success"] = $"Tax rate \"{model.Name}\" created.";
return RedirectToAction(nameof(Index));
}
[HttpGet]
public async Task<IActionResult> Edit(int id)
{
var rate = await _unitOfWork.TaxRates.GetByIdAsync(id);
if (rate == null) return NotFound();
return View(rate);
}
/// <summary>Updates an existing tax rate. Clears default flag on other rates when IsDefault is set.</summary>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, TaxRate model)
{
if (id != model.Id) return BadRequest();
if (!ModelState.IsValid) return View(model);
var rate = await _unitOfWork.TaxRates.GetByIdAsync(id);
if (rate == null) return NotFound();
if (model.IsDefault && !rate.IsDefault)
await ClearOtherDefaultsAsync(id);
rate.Name = model.Name;
rate.Rate = model.Rate;
rate.State = model.State;
rate.Description = model.Description;
rate.IsDefault = model.IsDefault;
rate.IsActive = model.IsActive;
await _unitOfWork.CompleteAsync();
TempData["Success"] = $"Tax rate \"{rate.Name}\" updated.";
return RedirectToAction(nameof(Index));
}
/// <summary>Toggles IsActive without a full page reload.</summary>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ToggleActive(int id)
{
var rate = await _unitOfWork.TaxRates.GetByIdAsync(id);
if (rate == null) return NotFound();
rate.IsActive = !rate.IsActive;
if (!rate.IsActive) rate.IsDefault = false;
await _unitOfWork.CompleteAsync();
return RedirectToAction(nameof(Index));
}
/// <summary>Soft-deletes a tax rate. Blocked when the rate is currently the default.</summary>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Delete(int id)
{
var rate = await _unitOfWork.TaxRates.GetByIdAsync(id);
if (rate == null) return NotFound();
if (rate.IsDefault)
{
TempData["Error"] = "Cannot delete the default tax rate. Set another rate as default first.";
return RedirectToAction(nameof(Index));
}
await _unitOfWork.TaxRates.SoftDeleteAsync(id);
await _unitOfWork.CompleteAsync();
TempData["Success"] = $"Tax rate \"{rate.Name}\" deleted.";
return RedirectToAction(nameof(Index));
}
/// <summary>
/// Clears IsDefault on all rates except the one with <paramref name="exceptId"/>.
/// Called before saving a newly-designated default to enforce the single-default invariant.
/// </summary>
private async Task ClearOtherDefaultsAsync(int exceptId)
{
var others = await _unitOfWork.TaxRates.FindAsync(r => r.IsDefault && r.Id != exceptId);
foreach (var r in others)
r.IsDefault = false;
}
}