using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using MMDAL.DTO.StockValuation; using MMDAL.Query.StockValuation; using System.Data.Common; namespace MMDAL.CustomCode.FifoLayerCost; public class FifoLayerCostDAL : IFifoLayerCostDAL { private readonly IQueryExecutor _qe; public FifoLayerCostDAL(IQueryExecutor qe) => _qe = qe; public async Task CreateCostLayerAsync( long costLayerId, int itemId, int skuId, int storeId, int ouid, decimal quantity, decimal unitCost, int objectTypeId, int objectId, long stockLedgerId, DateOnly layerDate, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, FifoLayerCostQB.CREATE_COST_LAYER, new { CostLayerId = costLayerId, OUID = ouid, StoreId = storeId, ItemId = itemId, SKUId = skuId, LayerDate = layerDate.ToDateTime(TimeOnly.MinValue), Quantity = quantity, UnitCost = unitCost, ObjectTypeId = objectTypeId, ObjectId = objectId, StockLedgerId = stockLedgerId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task> AllocateLayersAsync( int itemId, int skuId, int storeId, int ouid, decimal issueQty, bool isLifo, LoginDTO login, CancellationToken ct) { string sql = isLifo ? FifoLayerCostQB.ALLOCATE_LIFO_LAYERS : FifoLayerCostQB.ALLOCATE_FIFO_LAYERS; 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.CostLayerId, AllocatedQuantity = (decimal)r.ConsumedQuantity, UnitCost = (decimal)r.UnitCost, AllocatedValue = (decimal)(r.ConsumedQuantity * r.UnitCost) }).ToList(); } public async Task ConsumeLayerAsync( long costLayerId, decimal consumedQty, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, FifoLayerCostQB.CONSUME_LAYER, new { CostLayerId = costLayerId, ConsumedQty = consumedQty }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task InsertLayerConsumptionAsync( long costLayerDetailId, long costLayerId, long issueStockLedgerId, decimal consumedQuantity, decimal unitCost, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, FifoLayerCostQB.INSERT_LAYER_CONSUMPTION, new { CostLayerDetailId = costLayerDetailId, CostLayerId = costLayerId, IssueStockLedgerId = issueStockLedgerId, ConsumedQuantity = consumedQuantity, UnitCost = unitCost }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task> GetOpenCostLayersAsync( int itemId, int skuId, int storeId, int ouid, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, FifoLayerCostQB.GET_OPEN_COST_LAYERS, new { ItemId = itemId, SKUId = skuId, OUID = ouid, StoreId = storeId }, cancellationToken: ct).ConfigureAwait(false); return result.ToList(); } }