Refactor: centralize accounting helpers, status constants, and query deduplication
- AccountingDropdownHelper: wired into BillsController and ExpensesController, replacing 35-40 lines of duplicated DB queries per controller - AppConstants.StatusCodes: added Job.* and Quote.* constants to replace all magic status strings across Jobs, Quotes, Appointments, OvenScheduler, AiQuickQuote, QuoteApproval, and AccountingDropdownHelper - AccountingRules: extracted IsNormalDebitBalance into shared Infrastructure helper; removed duplicate private method from AccountBalanceService and LedgerService (~50 lines deleted) - AccountDataExportController: extracted 9 Fetch*Async methods (superset of includes) so Add*Sheet and Build*Csv no longer duplicate DB queries; each entity is queried once regardless of whether XLSX or CSV format is requested - BillsController.Create and ExpensesController.Create wrapped in ExecuteInTransactionAsync; blob uploads moved after commit to keep financial data atomic and prevent orphaned blobs from rolling back - Number generators (Appointments, CreditMemo, OvenBatch) fixed from full-table GetAllAsync to prefix-filtered FindAsync Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -306,7 +306,7 @@ public class LedgerService : ILedgerService
|
||||
|
||||
// Derive normal-debit-balance flag from AccountSubType (more authoritative than AccountType,
|
||||
// since users could misconfigure AccountType while SubType is picked from a constrained list).
|
||||
bool normalDebitBalance = IsNormalDebitBalance(account.AccountSubType);
|
||||
bool normalDebitBalance = AccountingRules.IsNormalDebitBalance(account.AccountSubType);
|
||||
|
||||
// Compute the balance before the selected period
|
||||
decimal priorBalance = await ComputePriorBalanceAsync(account, fromDate, to.Date, normalDebitBalance);
|
||||
@@ -338,36 +338,6 @@ public class LedgerService : ILedgerService
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns <c>true</c> if the account sub-type has a normal debit balance (Assets, Expenses, COGS),
|
||||
/// <c>false</c> for normal credit balance (Liabilities, Equity, Revenue).
|
||||
/// <see cref="AccountSubType"/> is used rather than <see cref="PowderCoating.Core.Enums.AccountType"/>
|
||||
/// because sub-type is constrained to a known set of values and cannot be misconfigured by a user,
|
||||
/// whereas <c>AccountType</c> is a broader category that a user might set incorrectly.
|
||||
/// Expense enum values are ≥ 50 by convention, allowing a catch-all range match.
|
||||
/// </summary>
|
||||
private static bool IsNormalDebitBalance(AccountSubType subType) => subType switch
|
||||
{
|
||||
// Asset subtypes → normal debit balance
|
||||
AccountSubType.Cash
|
||||
or AccountSubType.Checking
|
||||
or AccountSubType.Savings
|
||||
or AccountSubType.AccountsReceivable
|
||||
or AccountSubType.Inventory
|
||||
or AccountSubType.FixedAsset
|
||||
or AccountSubType.OtherCurrentAsset
|
||||
or AccountSubType.OtherAsset => true,
|
||||
|
||||
// COGS → normal debit balance
|
||||
AccountSubType.CostOfGoodsSold => true,
|
||||
|
||||
// Expense subtypes (enum values ≥ 50) → normal debit balance
|
||||
var st when (int)st >= 50 => true,
|
||||
|
||||
// Liability subtypes (AP, CreditCard, etc.), Equity, Revenue → normal credit balance
|
||||
_ => false
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Computes the account balance on the day immediately before <paramref name="beforeDate"/>
|
||||
/// by summing all activity prior to that date across every transaction source and adding
|
||||
@@ -375,7 +345,7 @@ public class LedgerService : ILedgerService
|
||||
/// date is on or before <paramref name="periodEnd"/> — a future-dated opening balance (e.g.
|
||||
/// from a mid-year chart-of-accounts migration) should not pollute earlier period reports.
|
||||
/// A null <c>OpeningBalanceDate</c> means the balance predates all transactions and always applies.
|
||||
/// The sign convention follows <see cref="IsNormalDebitBalance"/>: debits increase debit-normal
|
||||
/// The sign convention follows <see cref="AccountingRules.IsNormalDebitBalance"/>: debits increase debit-normal
|
||||
/// accounts and credits increase credit-normal accounts.
|
||||
/// </summary>
|
||||
private async Task<decimal> ComputePriorBalanceAsync(
|
||||
|
||||
Reference in New Issue
Block a user