Files

65 lines
3.1 KiB
C#

using General_Ledger_Service.Infra;
using General_Ledger_Service.Interfaces.Repositories;
using General_Ledger_Service.Models;
using Microsoft.EntityFrameworkCore;
namespace General_Ledger_Service.Repositories
{
public class TaxRepository(AppDBContext context) : ITaxRepository
{
private readonly AppDBContext _context = context;
public async Task<TaxType?> GetTaxTypeByCodeAsync(string code) =>
await _context.TaxTypes.FirstOrDefaultAsync(t => t.Code == code);
public async Task<List<TaxType>> GetAllTaxTypesAsync() =>
await _context.TaxTypes.OrderBy(t => t.Code).ToListAsync();
public async Task<TaxCode?> GetEffectiveTaxCodeByCodeAsync(string code, DateOnly txnDate) =>
await _context.TaxCodes.FirstOrDefaultAsync(tc =>
tc.Code == code &&
tc.EffectiveFrom <= txnDate &&
(tc.EffectiveTo == null || tc.EffectiveTo >= txnDate));
public async Task<TaxCode?> GetByCodeAsync(string code) =>
await _context.TaxCodes.FirstOrDefaultAsync(tc => tc.Code == code);
public Task<TaxCode> AddTaxCodeAsync(TaxCode taxCode) =>
RepositoryHelpers.AddAsync(_context.TaxCodes, taxCode);
public async Task<List<TaxCode>> GetAllTaxCodesAsync() =>
await _context.TaxCodes.OrderBy(tc => tc.Code).ToListAsync();
// Accounting_System_Design_LKAS.md Section 11.7 - Non-Deductible Expenses. Expense is
// debit-normal, so base_credit - base_debit is negative for a normal balance; ABS() here
// (rather than in the Service layer) since this figure is always presented as a positive
// "Add:" line, same convention ReportService applies for the P&L's Expense-side sections.
public async Task<decimal> GetNonDeductibleExpensesAsync(DateOnly periodStart, DateOnly periodEnd)
{
var net = await _context.JournalEntryLines
.Where(l => l.Journal.IsPosted
&& l.Journal.JournalType != "CLOSING"
&& l.Journal.EntryDate >= periodStart
&& l.Journal.EntryDate <= periodEnd
&& l.Account.AccountTypeId == 5
&& !l.Account.IsTaxDeductible)
.SumAsync(l => (decimal?)(l.BaseCredit - l.BaseDebit)) ?? 0m;
return Math.Abs(net);
}
// Reused for APIT/WHT/quarterly-tax-payment credit accounts (Section 11.7, steps 8-10) -
// each is a plain credit-normal balance sum against one configured accounts.account_id.
public async Task<decimal> GetAccountCreditActivityAsync(long accountId, DateOnly periodStart, DateOnly periodEnd)
{
return await _context.JournalEntryLines
.Where(l => l.Journal.IsPosted
&& l.Journal.JournalType != "CLOSING"
&& l.Journal.EntryDate >= periodStart
&& l.Journal.EntryDate <= periodEnd
&& l.AccountId == accountId)
.SumAsync(l => (decimal?)(l.BaseCredit - l.BaseDebit)) ?? 0m;
}
}
}