using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using MMDAL.DTO.StockValuation; using MMDAL.Query.StockValuation; using System.Data.Common; namespace MMDAL.CustomCode.FifoLotCost; public class FifoLotCostDAL : IFifoLotCostDAL { private readonly IQueryExecutor _qe; public FifoLotCostDAL(IQueryExecutor qe) => _qe = qe; public async Task> AllocateLotsAsync( int itemId, int skuId, int storeId, int ouid, decimal issueQty, bool isLifo, LoginDTO login, CancellationToken ct) { string sql = isLifo ? FifoLotCostQB.ALLOCATE_LIFO_LOTS : FifoLotCostQB.ALLOCATE_FIFO_LOTS; var rows = await _qe.QueryAsync( login, sql, new { ItemId = itemId, SKUId = skuId, OUID = ouid, StoreId = storeId, IssueQty = issueQty }, cancellationToken: ct).ConfigureAwait(false); return rows.Select(r => new FifoAllocationDTO { SourceId = (long)r.LotId, AllocatedQuantity = (decimal)r.ConsumedQuantity, UnitCost = (decimal)r.UnitCost, AllocatedValue = (decimal)(r.ConsumedQuantity * r.UnitCost) }).ToList(); } public async Task InsertLotReceiptAsync( long lotId, int itemId, int skuId, int storeId, int ouid, decimal quantity, decimal unitCost, int objectTypeId, int objectId, long stockLedgerId, DateOnly lotDate, string? lotNumber, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, FifoLotCostQB.INSERT_LOT_RECEIPT, new { LotId = lotId, OUID = ouid, StoreId = storeId, ItemId = itemId, SKUId = skuId, LotDate = lotDate.ToDateTime(TimeOnly.MinValue), LotNumber = lotNumber, Quantity = quantity, UnitCost = unitCost, ObjectTypeId = objectTypeId, ObjectId = objectId, StockLedgerId = stockLedgerId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task InsertLotDetailInboundAsync( long lotDetailId, long lotId, long stockLedgerId, decimal quantity, decimal unitCost, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, FifoLotCostQB.INSERT_LOT_DETAIL_INBOUND, new { LotDetailId = lotDetailId, LotId = lotId, StockLedgerId = stockLedgerId, Quantity = quantity, UnitCost = unitCost }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task InsertLotDetailIssueAsync( long lotDetailId, long lotId, long issueStockLedgerId, decimal consumedQuantity, decimal unitCost, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, FifoLotCostQB.INSERT_LOT_DETAIL_ISSUE, new { LotDetailId = lotDetailId, LotId = lotId, IssueStockLedgerId = issueStockLedgerId, ConsumedQuantity = consumedQuantity, UnitCost = unitCost }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task DeleteLotDetailsForRerunAsync( int objectTypeId, int objectId, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, FifoLotCostQB.DELETE_LOT_DETAILS_FOR_RERUN, new { ObjectTypeId = objectTypeId, ObjectId = objectId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } }