Files
Distribution_GL/Repositories/FixedAssetRepository.cs
T

50 lines
2.3 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 FixedAssetRepository(AppDBContext context) : IFixedAssetRepository
{
private readonly AppDBContext _context = context;
public Task<FixedAsset?> GetByIdAsync(long assetId) =>
RepositoryHelpers.GetByIdAsync(_context.FixedAssets, assetId);
public async Task<FixedAsset?> GetByCodeAsync(string assetCode) =>
await _context.FixedAssets.FirstOrDefaultAsync(a => a.AssetCode == assetCode);
public Task<FixedAsset> AddAsync(FixedAsset asset) =>
RepositoryHelpers.AddAsync(_context.FixedAssets, asset);
public async Task<List<FixedAsset>> GetAllAsync() =>
await _context.FixedAssets.OrderBy(a => a.AssetCode).ToListAsync();
public async Task<DepreciationSchedule?> GetLatestScheduleByAssetIdAsync(long assetId) =>
await _context.DepreciationSchedules
.Where(s => s.AssetId == assetId)
.OrderByDescending(s => s.CreatedAt)
.FirstOrDefaultAsync();
public Task<DepreciationSchedule> AddScheduleAsync(DepreciationSchedule schedule) =>
RepositoryHelpers.AddAsync(_context.DepreciationSchedules, schedule);
public Task<AssetDisposal> 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<decimal> 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;
}
}
}