Phase D: Add Vendor Credits (AP cycle completion)

- VendorCredit, VendorCreditLineItem, VendorCreditApplication entities
- VendorCreditStatus enum (Open, PartiallyApplied, Applied, Voided)
- Migration AddVendorCredits: three new tables
- IUnitOfWork/UnitOfWork wired with all three repositories
- VendorCreditsController: Index (status tabs), Create, Details, Post, Apply, Void
- Post action: DR AP, CR each expense line (reverses original expense)
- Apply action: links credit to bill, updates Bill.AmountPaid and bill status
- Views: Index (summary cards + table), Create (dynamic line grid), Details (apply panel)
- Nav: Vendor Credits 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:03:14 -04:00
parent a33687f7bd
commit cf9dcfb4c1
13 changed files with 11387 additions and 3 deletions
@@ -329,6 +329,13 @@ public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDataPro
/// <summary>Individual debit/credit lines within a journal entry; soft-delete only (access controlled through parent JournalEntry).</summary>
public DbSet<JournalEntryLine> JournalEntryLines { get; set; }
/// <summary>Credit notes received from vendors (returned goods, pricing disputes); tenant-filtered with soft delete.</summary>
public DbSet<VendorCredit> VendorCredits { get; set; }
/// <summary>Expense-reversal line items on a vendor credit; soft-delete only.</summary>
public DbSet<VendorCreditLineItem> VendorCreditLineItems { get; set; }
/// <summary>Application records linking a vendor credit to a specific bill; soft-delete only.</summary>
public DbSet<VendorCreditApplication> VendorCreditApplications { get; set; }
// Job Templates
/// <summary>Reusable job templates that pre-populate job items, coats, and prep services on job creation.</summary>
public DbSet<JobTemplate> JobTemplates { get; set; }
@@ -624,6 +631,12 @@ public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDataPro
!e.IsDeleted && (IsPlatformAdmin || e.CompanyId == CurrentCompanyId));
modelBuilder.Entity<JournalEntryLine>().HasQueryFilter(e => !e.IsDeleted);
// Vendor Credits: tenant-filtered; child rows soft-delete only
modelBuilder.Entity<VendorCredit>().HasQueryFilter(e =>
!e.IsDeleted && (IsPlatformAdmin || e.CompanyId == CurrentCompanyId));
modelBuilder.Entity<VendorCreditLineItem>().HasQueryFilter(e => !e.IsDeleted);
modelBuilder.Entity<VendorCreditApplication>().HasQueryFilter(e => !e.IsDeleted);
// Purchase Orders
modelBuilder.Entity<PurchaseOrder>().HasQueryFilter(e =>
!e.IsDeleted && (IsPlatformAdmin || e.CompanyId == CurrentCompanyId));
@@ -650,6 +663,20 @@ public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDataPro
.HasForeignKey(je => je.ReversalOfId)
.OnDelete(DeleteBehavior.Restrict);
// VendorCredit → APAccount (no cascade)
modelBuilder.Entity<VendorCredit>()
.HasOne(vc => vc.APAccount)
.WithMany()
.HasForeignKey(vc => vc.APAccountId)
.OnDelete(DeleteBehavior.Restrict);
// VendorCreditLineItem → Account (nullable, no cascade)
modelBuilder.Entity<VendorCreditLineItem>()
.HasOne(li => li.Account)
.WithMany()
.HasForeignKey(li => li.AccountId)
.OnDelete(DeleteBehavior.Restrict);
// Vendor → DefaultExpenseAccount (no cascade)
modelBuilder.Entity<Vendor>()
.HasOne(s => s.DefaultExpenseAccount)
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,212 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PowderCoating.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddVendorCredits : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "VendorCredits",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CreditNumber = table.Column<string>(type: "nvarchar(max)", nullable: false),
VendorId = table.Column<int>(type: "int", nullable: false),
APAccountId = table.Column<int>(type: "int", nullable: false),
CreditDate = table.Column<DateTime>(type: "datetime2", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
Total = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
RemainingAmount = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Memo = table.Column<string>(type: "nvarchar(max)", nullable: true),
CompanyId = table.Column<int>(type: "int", nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
UpdatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
DeletedBy = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_VendorCredits", x => x.Id);
table.ForeignKey(
name: "FK_VendorCredits_Accounts_APAccountId",
column: x => x.APAccountId,
principalTable: "Accounts",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_VendorCredits_Vendors_VendorId",
column: x => x.VendorId,
principalTable: "Vendors",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "VendorCreditApplications",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
VendorCreditId = table.Column<int>(type: "int", nullable: false),
BillId = table.Column<int>(type: "int", nullable: false),
Amount = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
AppliedDate = table.Column<DateTime>(type: "datetime2", nullable: false),
CompanyId = table.Column<int>(type: "int", nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
UpdatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
DeletedBy = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_VendorCreditApplications", x => x.Id);
table.ForeignKey(
name: "FK_VendorCreditApplications_Bills_BillId",
column: x => x.BillId,
principalTable: "Bills",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_VendorCreditApplications_VendorCredits_VendorCreditId",
column: x => x.VendorCreditId,
principalTable: "VendorCredits",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "VendorCreditLineItems",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
VendorCreditId = table.Column<int>(type: "int", nullable: false),
AccountId = table.Column<int>(type: "int", nullable: true),
Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
Amount = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
CompanyId = table.Column<int>(type: "int", nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
UpdatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
DeletedBy = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_VendorCreditLineItems", x => x.Id);
table.ForeignKey(
name: "FK_VendorCreditLineItems_Accounts_AccountId",
column: x => x.AccountId,
principalTable: "Accounts",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_VendorCreditLineItems_VendorCredits_VendorCreditId",
column: x => x.VendorCreditId,
principalTable: "VendorCredits",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 1,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 3, 58, 27, 360, DateTimeKind.Utc).AddTicks(6994));
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 2,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 3, 58, 27, 360, DateTimeKind.Utc).AddTicks(7001));
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 3,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 3, 58, 27, 360, DateTimeKind.Utc).AddTicks(7003));
migrationBuilder.CreateIndex(
name: "IX_VendorCreditApplications_BillId",
table: "VendorCreditApplications",
column: "BillId");
migrationBuilder.CreateIndex(
name: "IX_VendorCreditApplications_VendorCreditId",
table: "VendorCreditApplications",
column: "VendorCreditId");
migrationBuilder.CreateIndex(
name: "IX_VendorCreditLineItems_AccountId",
table: "VendorCreditLineItems",
column: "AccountId");
migrationBuilder.CreateIndex(
name: "IX_VendorCreditLineItems_VendorCreditId",
table: "VendorCreditLineItems",
column: "VendorCreditId");
migrationBuilder.CreateIndex(
name: "IX_VendorCredits_APAccountId",
table: "VendorCredits",
column: "APAccountId");
migrationBuilder.CreateIndex(
name: "IX_VendorCredits_VendorId",
table: "VendorCredits",
column: "VendorId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "VendorCreditApplications");
migrationBuilder.DropTable(
name: "VendorCreditLineItems");
migrationBuilder.DropTable(
name: "VendorCredits");
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 1,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 3, 45, 31, 524, DateTimeKind.Utc).AddTicks(9350));
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 2,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 3, 45, 31, 524, DateTimeKind.Utc).AddTicks(9357));
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 3,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 3, 45, 31, 524, DateTimeKind.Utc).AddTicks(9359));
}
}
}
@@ -6206,7 +6206,7 @@ namespace PowderCoating.Infrastructure.Migrations
{
Id = 1,
CompanyId = 0,
CreatedAt = new DateTime(2026, 5, 10, 3, 45, 31, 524, DateTimeKind.Utc).AddTicks(9350),
CreatedAt = new DateTime(2026, 5, 10, 3, 58, 27, 360, DateTimeKind.Utc).AddTicks(6994),
Description = "Standard pricing for regular customers",
DiscountPercent = 0m,
IsActive = true,
@@ -6217,7 +6217,7 @@ namespace PowderCoating.Infrastructure.Migrations
{
Id = 2,
CompanyId = 0,
CreatedAt = new DateTime(2026, 5, 10, 3, 45, 31, 524, DateTimeKind.Utc).AddTicks(9357),
CreatedAt = new DateTime(2026, 5, 10, 3, 58, 27, 360, DateTimeKind.Utc).AddTicks(7001),
Description = "5% discount for preferred customers",
DiscountPercent = 5m,
IsActive = true,
@@ -6228,7 +6228,7 @@ namespace PowderCoating.Infrastructure.Migrations
{
Id = 3,
CompanyId = 0,
CreatedAt = new DateTime(2026, 5, 10, 3, 45, 31, 524, DateTimeKind.Utc).AddTicks(9359),
CreatedAt = new DateTime(2026, 5, 10, 3, 58, 27, 360, DateTimeKind.Utc).AddTicks(7003),
Description = "10% discount for premium customers",
DiscountPercent = 10m,
IsActive = true,
@@ -7846,6 +7846,179 @@ namespace PowderCoating.Infrastructure.Migrations
b.ToTable("Vendors");
});
modelBuilder.Entity("PowderCoating.Core.Entities.VendorCredit", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("APAccountId")
.HasColumnType("int");
b.Property<int>("CompanyId")
.HasColumnType("int");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("CreditDate")
.HasColumnType("datetime2");
b.Property<string>("CreditNumber")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime?>("DeletedAt")
.HasColumnType("datetime2");
b.Property<string>("DeletedBy")
.HasColumnType("nvarchar(max)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<string>("Memo")
.HasColumnType("nvarchar(max)");
b.Property<decimal>("RemainingAmount")
.HasColumnType("decimal(18,2)");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<decimal>("Total")
.HasColumnType("decimal(18,2)");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("datetime2");
b.Property<string>("UpdatedBy")
.HasColumnType("nvarchar(max)");
b.Property<int>("VendorId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("APAccountId");
b.HasIndex("VendorId");
b.ToTable("VendorCredits");
});
modelBuilder.Entity("PowderCoating.Core.Entities.VendorCreditApplication", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<decimal>("Amount")
.HasColumnType("decimal(18,2)");
b.Property<DateTime>("AppliedDate")
.HasColumnType("datetime2");
b.Property<int>("BillId")
.HasColumnType("int");
b.Property<int>("CompanyId")
.HasColumnType("int");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<DateTime?>("DeletedAt")
.HasColumnType("datetime2");
b.Property<string>("DeletedBy")
.HasColumnType("nvarchar(max)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("datetime2");
b.Property<string>("UpdatedBy")
.HasColumnType("nvarchar(max)");
b.Property<int>("VendorCreditId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("BillId");
b.HasIndex("VendorCreditId");
b.ToTable("VendorCreditApplications");
});
modelBuilder.Entity("PowderCoating.Core.Entities.VendorCreditLineItem", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int?>("AccountId")
.HasColumnType("int");
b.Property<decimal>("Amount")
.HasColumnType("decimal(18,2)");
b.Property<int>("CompanyId")
.HasColumnType("int");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<DateTime?>("DeletedAt")
.HasColumnType("datetime2");
b.Property<string>("DeletedBy")
.HasColumnType("nvarchar(max)");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("datetime2");
b.Property<string>("UpdatedBy")
.HasColumnType("nvarchar(max)");
b.Property<int>("VendorCreditId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("AccountId");
b.HasIndex("VendorCreditId");
b.ToTable("VendorCreditLineItems");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null)
@@ -9451,6 +9624,62 @@ namespace PowderCoating.Infrastructure.Migrations
b.Navigation("DefaultExpenseAccount");
});
modelBuilder.Entity("PowderCoating.Core.Entities.VendorCredit", b =>
{
b.HasOne("PowderCoating.Core.Entities.Account", "APAccount")
.WithMany()
.HasForeignKey("APAccountId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("PowderCoating.Core.Entities.Vendor", "Vendor")
.WithMany()
.HasForeignKey("VendorId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("APAccount");
b.Navigation("Vendor");
});
modelBuilder.Entity("PowderCoating.Core.Entities.VendorCreditApplication", b =>
{
b.HasOne("PowderCoating.Core.Entities.Bill", "Bill")
.WithMany()
.HasForeignKey("BillId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PowderCoating.Core.Entities.VendorCredit", "VendorCredit")
.WithMany("Applications")
.HasForeignKey("VendorCreditId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Bill");
b.Navigation("VendorCredit");
});
modelBuilder.Entity("PowderCoating.Core.Entities.VendorCreditLineItem", b =>
{
b.HasOne("PowderCoating.Core.Entities.Account", "Account")
.WithMany()
.HasForeignKey("AccountId")
.OnDelete(DeleteBehavior.Restrict);
b.HasOne("PowderCoating.Core.Entities.VendorCredit", "VendorCredit")
.WithMany("LineItems")
.HasForeignKey("VendorCreditId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Account");
b.Navigation("VendorCredit");
});
modelBuilder.Entity("PowderCoating.Core.Entities.Account", b =>
{
b.Navigation("BillLineItems");
@@ -9706,6 +9935,13 @@ namespace PowderCoating.Infrastructure.Migrations
b.Navigation("InventoryItems");
});
modelBuilder.Entity("PowderCoating.Core.Entities.VendorCredit", b =>
{
b.Navigation("Applications");
b.Navigation("LineItems");
});
#pragma warning restore 612, 618
}
}
@@ -146,6 +146,11 @@ public class UnitOfWork : IUnitOfWork
private IRepository<JournalEntry>? _journalEntries;
private IRepository<JournalEntryLine>? _journalEntryLines;
// Vendor Credits
private IRepository<VendorCredit>? _vendorCredits;
private IRepository<VendorCreditLineItem>? _vendorCreditLineItems;
private IRepository<VendorCreditApplication>? _vendorCreditApplications;
/// <summary>
/// Initialises the unit of work with the scoped <paramref name="context"/>.
/// The context is shared across all repositories created by this instance so that
@@ -526,6 +531,19 @@ public class UnitOfWork : IUnitOfWork
public IRepository<JournalEntryLine> JournalEntryLines =>
_journalEntryLines ??= new Repository<JournalEntryLine>(_context);
// Vendor Credits
/// <summary>Repository for <see cref="VendorCredit"/> credit notes received from vendors; tenant-filtered with soft delete.</summary>
public IRepository<VendorCredit> VendorCredits =>
_vendorCredits ??= new Repository<VendorCredit>(_context);
/// <summary>Repository for <see cref="VendorCreditLineItem"/> expense-reversal lines on a vendor credit.</summary>
public IRepository<VendorCreditLineItem> VendorCreditLineItems =>
_vendorCreditLineItems ??= new Repository<VendorCreditLineItem>(_context);
/// <summary>Repository for <see cref="VendorCreditApplication"/> records linking a vendor credit to a specific bill.</summary>
public IRepository<VendorCreditApplication> VendorCreditApplications =>
_vendorCreditApplications ??= new Repository<VendorCreditApplication>(_context);
/// <summary>
/// Flushes all pending changes in the EF Core change tracker to the database.
/// Returns the number of state entries written.