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 FixedAssetRepository(AppDBContext context) : IFixedAssetRepository { private readonly AppDBContext _context = context; public Task GetByIdAsync(long assetId) => RepositoryHelpers.GetByIdAsync(_context.FixedAssets, assetId); public async Task GetByCodeAsync(string assetCode) => await _context.FixedAssets.FirstOrDefaultAsync(a => a.AssetCode == assetCode); public Task AddAsync(FixedAsset asset) => RepositoryHelpers.AddAsync(_context.FixedAssets, asset); public async Task> GetAllAsync() => await _context.FixedAssets.OrderBy(a => a.AssetCode).ToListAsync(); public async Task GetLatestScheduleByAssetIdAsync(long assetId) => await _context.DepreciationSchedules .Where(s => s.AssetId == assetId) .OrderByDescending(s => s.CreatedAt) .FirstOrDefaultAsync(); public Task AddScheduleAsync(DepreciationSchedule schedule) => RepositoryHelpers.AddAsync(_context.DepreciationSchedules, schedule); public Task AddDisposalAsync(AssetDisposal disposal) => RepositoryHelpers.AddAsync(_context.AssetDisposals, disposal); // Accounting_System_Design_LKAS.md Section 11.5 - the only non-cash add-back this system // currently tracks. ROUND(...,10) guards the same decimal-overflow class of bug fixed // elsewhere in the report queries (03_Progress_Tracker.md's 2026-07-21 bugfix entry). public async Task GetDepreciationAddBackAsync(DateOnly periodStart, DateOnly periodEnd) { return await _context.DepreciationSchedules .Where(ds => ds.Journal != null && ds.Journal.IsPosted && ds.Journal.EntryDate >= periodStart && ds.Journal.EntryDate <= periodEnd) .SumAsync(ds => (decimal?)ds.DepreciationAmount) ?? 0m; } } }