Phase G: Add Budgeting and Year-End Close

Budgeting:
- Budget + BudgetLine entities with Jan–Dec monthly columns per GL account
- BudgetsController: Index, Create, Edit, SetDefault, Copy, Delete
- Copy action rolls a budget forward to a new fiscal year
- Budget vs. Actual report (BudgetVsActual): compares monthly budget amounts to
  real P&L by calling GetProfitAndLossAsync once per month; variance shown as
  favorable/unfavorable; year + budget selectors in header
- Views: Budgets/Index, Create, Edit with inline annual totals via budget-edit.js
- Nav link + report card on Landing

Year-End Close:
- YearEndClose entity records each closed year + JE reference for audit trail
- AccountsController.YearEndClose GET (history + form) + CloseYear POST
- Close zeroes all Revenue and Expense/COGS account balances into Retained Earnings
  via IAccountBalanceService and posts a supporting JE dated Dec 31
- Idempotency: rejects attempt to close an already-closed year
- Pre-close checklist in view to guide the workflow
- Nav link under Finance

Migration AddBudgetsAndYearEndClose applied

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-10 13:01:56 -04:00
parent fde24b09c9
commit 4fd9c52aaf
19 changed files with 12527 additions and 3 deletions
@@ -343,6 +343,13 @@ public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDataPro
/// <summary>One record per asset per period for each depreciation posting; soft-delete only.</summary>
public DbSet<FixedAssetDepreciationEntry> FixedAssetDepreciationEntries { get; set; }
/// <summary>Named annual budgets with monthly amounts per GL account; tenant-filtered with soft delete.</summary>
public DbSet<Budget> Budgets { get; set; }
/// <summary>One row per account per Budget; contains JanDec decimal columns.</summary>
public DbSet<BudgetLine> BudgetLines { get; set; }
/// <summary>Audit trail of completed year-end closes; tenant-filtered with soft delete.</summary>
public DbSet<YearEndClose> YearEndCloses { 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>
@@ -687,6 +694,27 @@ public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDataPro
.HasForeignKey(e => e.JournalEntryId)
.OnDelete(DeleteBehavior.NoAction);
// Budgets: tenant-filtered; BudgetLines soft-delete only
modelBuilder.Entity<Budget>().HasQueryFilter(e =>
!e.IsDeleted && (IsPlatformAdmin || e.CompanyId == CurrentCompanyId));
modelBuilder.Entity<BudgetLine>().HasQueryFilter(e => !e.IsDeleted);
// BudgetLine → Account: Restrict delete so removing an account doesn't cascade into budget data
modelBuilder.Entity<BudgetLine>()
.HasOne(bl => bl.Account)
.WithMany()
.HasForeignKey(bl => bl.AccountId)
.OnDelete(DeleteBehavior.Restrict);
// YearEndClose: tenant-filtered; links to a specific JE
modelBuilder.Entity<YearEndClose>().HasQueryFilter(e =>
!e.IsDeleted && (IsPlatformAdmin || e.CompanyId == CurrentCompanyId));
modelBuilder.Entity<YearEndClose>()
.HasOne(y => y.JournalEntry)
.WithMany()
.HasForeignKey(y => y.JournalEntryId)
.OnDelete(DeleteBehavior.Restrict);
// Vendor Credits: tenant-filtered; child rows soft-delete only
modelBuilder.Entity<VendorCredit>().HasQueryFilter(e =>
!e.IsDeleted && (IsPlatformAdmin || e.CompanyId == CurrentCompanyId));
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,185 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PowderCoating.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddBudgetsAndYearEndClose : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Budgets",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
FiscalYear = table.Column<int>(type: "int", nullable: false),
Notes = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDefault = table.Column<bool>(type: "bit", 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_Budgets", x => x.Id);
});
migrationBuilder.CreateTable(
name: "YearEndCloses",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ClosedYear = table.Column<int>(type: "int", nullable: false),
ClosedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
ClosedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
JournalEntryId = table.Column<int>(type: "int", 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_YearEndCloses", x => x.Id);
table.ForeignKey(
name: "FK_YearEndCloses_JournalEntries_JournalEntryId",
column: x => x.JournalEntryId,
principalTable: "JournalEntries",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "BudgetLines",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
BudgetId = table.Column<int>(type: "int", nullable: false),
AccountId = table.Column<int>(type: "int", nullable: false),
Jan = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Feb = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Mar = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Apr = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
May = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Jun = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Jul = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Aug = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Sep = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Oct = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Nov = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Dec = 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_BudgetLines", x => x.Id);
table.ForeignKey(
name: "FK_BudgetLines_Accounts_AccountId",
column: x => x.AccountId,
principalTable: "Accounts",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_BudgetLines_Budgets_BudgetId",
column: x => x.BudgetId,
principalTable: "Budgets",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 1,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 16, 55, 8, 322, DateTimeKind.Utc).AddTicks(966));
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 2,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 16, 55, 8, 322, DateTimeKind.Utc).AddTicks(974));
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 3,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 16, 55, 8, 322, DateTimeKind.Utc).AddTicks(976));
migrationBuilder.CreateIndex(
name: "IX_BudgetLines_AccountId",
table: "BudgetLines",
column: "AccountId");
migrationBuilder.CreateIndex(
name: "IX_BudgetLines_BudgetId",
table: "BudgetLines",
column: "BudgetId");
migrationBuilder.CreateIndex(
name: "IX_YearEndCloses_JournalEntryId",
table: "YearEndCloses",
column: "JournalEntryId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BudgetLines");
migrationBuilder.DropTable(
name: "YearEndCloses");
migrationBuilder.DropTable(
name: "Budgets");
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 1,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 16, 0, 49, 626, DateTimeKind.Utc).AddTicks(4004));
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 2,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 16, 0, 49, 626, DateTimeKind.Utc).AddTicks(4009));
migrationBuilder.UpdateData(
table: "PricingTiers",
keyColumn: "Id",
keyValue: 3,
column: "CreatedAt",
value: new DateTime(2026, 5, 10, 16, 0, 49, 626, DateTimeKind.Utc).AddTicks(4011));
}
}
}
@@ -1269,6 +1269,139 @@ namespace PowderCoating.Infrastructure.Migrations
b.ToTable("BillPayments");
});
modelBuilder.Entity("PowderCoating.Core.Entities.Budget", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
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<int>("FiscalYear")
.HasColumnType("int");
b.Property<bool>("IsDefault")
.HasColumnType("bit");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Notes")
.HasColumnType("nvarchar(max)");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("datetime2");
b.Property<string>("UpdatedBy")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Budgets");
});
modelBuilder.Entity("PowderCoating.Core.Entities.BudgetLine", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("AccountId")
.HasColumnType("int");
b.Property<decimal>("Apr")
.HasColumnType("decimal(18,2)");
b.Property<decimal>("Aug")
.HasColumnType("decimal(18,2)");
b.Property<int>("BudgetId")
.HasColumnType("int");
b.Property<int>("CompanyId")
.HasColumnType("int");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Dec")
.HasColumnType("decimal(18,2)");
b.Property<DateTime?>("DeletedAt")
.HasColumnType("datetime2");
b.Property<string>("DeletedBy")
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Feb")
.HasColumnType("decimal(18,2)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<decimal>("Jan")
.HasColumnType("decimal(18,2)");
b.Property<decimal>("Jul")
.HasColumnType("decimal(18,2)");
b.Property<decimal>("Jun")
.HasColumnType("decimal(18,2)");
b.Property<decimal>("Mar")
.HasColumnType("decimal(18,2)");
b.Property<decimal>("May")
.HasColumnType("decimal(18,2)");
b.Property<decimal>("Nov")
.HasColumnType("decimal(18,2)");
b.Property<decimal>("Oct")
.HasColumnType("decimal(18,2)");
b.Property<decimal>("Sep")
.HasColumnType("decimal(18,2)");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("datetime2");
b.Property<string>("UpdatedBy")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.HasIndex("AccountId");
b.HasIndex("BudgetId");
b.ToTable("BudgetLines");
});
modelBuilder.Entity("PowderCoating.Core.Entities.BugReport", b =>
{
b.Property<int>("Id")
@@ -6432,7 +6565,7 @@ namespace PowderCoating.Infrastructure.Migrations
{
Id = 1,
CompanyId = 0,
CreatedAt = new DateTime(2026, 5, 10, 16, 0, 49, 626, DateTimeKind.Utc).AddTicks(4004),
CreatedAt = new DateTime(2026, 5, 10, 16, 55, 8, 322, DateTimeKind.Utc).AddTicks(966),
Description = "Standard pricing for regular customers",
DiscountPercent = 0m,
IsActive = true,
@@ -6443,7 +6576,7 @@ namespace PowderCoating.Infrastructure.Migrations
{
Id = 2,
CompanyId = 0,
CreatedAt = new DateTime(2026, 5, 10, 16, 0, 49, 626, DateTimeKind.Utc).AddTicks(4009),
CreatedAt = new DateTime(2026, 5, 10, 16, 55, 8, 322, DateTimeKind.Utc).AddTicks(974),
Description = "5% discount for preferred customers",
DiscountPercent = 5m,
IsActive = true,
@@ -6454,7 +6587,7 @@ namespace PowderCoating.Infrastructure.Migrations
{
Id = 3,
CompanyId = 0,
CreatedAt = new DateTime(2026, 5, 10, 16, 0, 49, 626, DateTimeKind.Utc).AddTicks(4011),
CreatedAt = new DateTime(2026, 5, 10, 16, 55, 8, 322, DateTimeKind.Utc).AddTicks(976),
Description = "10% discount for premium customers",
DiscountPercent = 10m,
IsActive = true,
@@ -8376,6 +8509,57 @@ namespace PowderCoating.Infrastructure.Migrations
b.ToTable("VendorCreditLineItems");
});
modelBuilder.Entity("PowderCoating.Core.Entities.YearEndClose", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("ClosedAt")
.HasColumnType("datetime2");
b.Property<string>("ClosedBy")
.HasColumnType("nvarchar(max)");
b.Property<int>("ClosedYear")
.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<int>("JournalEntryId")
.HasColumnType("int");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("datetime2");
b.Property<string>("UpdatedBy")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.HasIndex("JournalEntryId");
b.ToTable("YearEndCloses");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null)
@@ -8589,6 +8773,25 @@ namespace PowderCoating.Infrastructure.Migrations
b.Navigation("Vendor");
});
modelBuilder.Entity("PowderCoating.Core.Entities.BudgetLine", b =>
{
b.HasOne("PowderCoating.Core.Entities.Account", "Account")
.WithMany()
.HasForeignKey("AccountId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("PowderCoating.Core.Entities.Budget", "Budget")
.WithMany("Lines")
.HasForeignKey("BudgetId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Account");
b.Navigation("Budget");
});
modelBuilder.Entity("PowderCoating.Core.Entities.BugReportAttachment", b =>
{
b.HasOne("PowderCoating.Core.Entities.BugReport", "BugReport")
@@ -10090,6 +10293,17 @@ namespace PowderCoating.Infrastructure.Migrations
b.Navigation("VendorCredit");
});
modelBuilder.Entity("PowderCoating.Core.Entities.YearEndClose", b =>
{
b.HasOne("PowderCoating.Core.Entities.JournalEntry", "JournalEntry")
.WithMany()
.HasForeignKey("JournalEntryId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("JournalEntry");
});
modelBuilder.Entity("PowderCoating.Core.Entities.Account", b =>
{
b.Navigation("BillLineItems");
@@ -10134,6 +10348,11 @@ namespace PowderCoating.Infrastructure.Migrations
b.Navigation("Payments");
});
modelBuilder.Entity("PowderCoating.Core.Entities.Budget", b =>
{
b.Navigation("Lines");
});
modelBuilder.Entity("PowderCoating.Core.Entities.BugReport", b =>
{
b.Navigation("Attachments");
@@ -161,6 +161,9 @@ public class UnitOfWork : IUnitOfWork
private IRepository<RecurringTemplate>? _recurringTemplates;
private IRepository<FixedAsset>? _fixedAssets;
private IRepository<FixedAssetDepreciationEntry>? _fixedAssetDepreciationEntries;
private IRepository<Budget>? _budgets;
private IRepository<BudgetLine>? _budgetLines;
private IRepository<YearEndClose>? _yearEndCloses;
/// <summary>
/// Initialises the unit of work with the scoped <paramref name="context"/>.
@@ -573,6 +576,12 @@ public class UnitOfWork : IUnitOfWork
_fixedAssets ??= new Repository<FixedAsset>(_context);
public IRepository<FixedAssetDepreciationEntry> FixedAssetDepreciationEntries =>
_fixedAssetDepreciationEntries ??= new Repository<FixedAssetDepreciationEntry>(_context);
public IRepository<Budget> Budgets =>
_budgets ??= new Repository<Budget>(_context);
public IRepository<BudgetLine> BudgetLines =>
_budgetLines ??= new Repository<BudgetLine>(_context);
public IRepository<YearEndClose> YearEndCloses =>
_yearEndCloses ??= new Repository<YearEndClose>(_context);
/// <summary>
/// Flushes all pending changes in the EF Core change tracker to the database.