using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using MMDAL.Query.StockValuation; using System.Data.Common; namespace MMDAL.CustomCode.StockLedgerCost; public class StockLedgerCostDAL : IStockLedgerCostDAL { private readonly IQueryExecutor _qe; public StockLedgerCostDAL(IQueryExecutor qe) => _qe = qe; public async Task ApplyAverageCostNonTransferAsync( int objectTypeId, int objectId, int ouid, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, StockLedgerCostQB.APPLY_AVERAGE_COST_NON_TRANSFER, new { ObjectTypeId = objectTypeId, ObjectId = objectId, OUID = ouid }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task ApplyAverageCostTransferAsync( int objectTypeId, int objectId, int ouid, int fromStoreId, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, StockLedgerCostQB.APPLY_AVERAGE_COST_TRANSFER, new { ObjectTypeId = objectTypeId, ObjectId = objectId, OUID = ouid, FromStoreId = fromStoreId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task UpdateAverageValueNonTransferAsync( int objectTypeId, int objectId, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, StockLedgerCostQB.UPDATE_AVERAGE_VALUE_NON_TRANSFER, new { ObjectTypeId = objectTypeId, ObjectId = objectId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task UpdateAverageValueTransferFromStoreAsync( int objectTypeId, int objectId, int fromStoreId, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, StockLedgerCostQB.UPDATE_AVERAGE_VALUE_TRANSFER_FROMSTORE, new { ObjectTypeId = objectTypeId, ObjectId = objectId, FromStoreId = fromStoreId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task ApplyCostAffectingStorewiseAsync( int objectTypeId, int objectId, bool isStockTransfer, int toStoreId, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, StockLedgerCostQB.APPLY_COST_AFFECTING_STOREWISE, new { ObjectTypeId = objectTypeId, ObjectId = objectId, IsStockTransfer = isStockTransfer ? 0 : 1, ToStoreId = toStoreId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task ApplyCostAffectingConsolidatedAsync( int objectTypeId, int objectId, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync( login, StockLedgerCostQB.APPLY_COST_AFFECTING_CONSOLIDATED, new { ObjectTypeId = objectTypeId, ObjectId = objectId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } public async Task GetIsStorewiseAsync(int ouid, LoginDTO login, CancellationToken ct) { return await _qe.ExecuteScalarAsync( login, StockLedgerCostQB.GET_STOREWISE_SETTING, new { OUID = ouid }, cancellationToken: ct).ConfigureAwait(false); } public async Task RunPerpetualRerunBatchAsync( int ouid, DateOnly fromDate, DateOnly toDate, int documentId, bool isStorewise, LoginDTO login, CancellationToken ct) { // All three steps plus cleanup execute in one connection scope so that // #tmpavg and #tmpresolved temp tables are visible across steps. var steps = new (string Sql, object Param)[] { ( StockLedgerCostQB.PERPET_RERUN_STEP1_CALC_AVG, new { OUID = ouid, FromDate = fromDate.ToDateTime(TimeOnly.MinValue), ToDate = toDate.ToDateTime(TimeOnly.MaxValue), IsStorewiseValuation = isStorewise ? 1 : 0 } ), ( StockLedgerCostQB.PERPET_RERUN_STEP2_JOIN_TO_LEDGER, new { OUID = ouid, FromDate = fromDate.ToDateTime(TimeOnly.MinValue), ToDate = toDate.ToDateTime(TimeOnly.MaxValue), IsStorewiseValuation = isStorewise ? 1 : 0, DocumentId = documentId } ), ( StockLedgerCostQB.PERPET_RERUN_STEP3_UPDATE_LEDGER, new { } ), ( StockLedgerCostQB.PERPET_RERUN_DROP_TEMP_1, new { } ), ( StockLedgerCostQB.PERPET_RERUN_DROP_TEMP_2, new { } ) }; await _qe.ExecuteInTransactionAsync(login, steps).ConfigureAwait(false); } }