public async Task AdjustStock(StockAdjustmentType stockAdjustmentType, long warehouseId, long productId, decimal unitCost, decimal quantity, DateTime?expiredDate, string note) { if (quantity <= 0) { return; } var wareHouse = await _wareHouseService.GetWareHouseById(warehouseId); if (!_wareHouseService.IsActiveWareHouse(wareHouse)) { throw new ServiceException(new ErrorMessage[] { new() { Message = "Warehouse is not active" } });
public async Task <StockTransfer> AddStockTransfer(StockTransfer stockTransfer) { Guard.Argument(stockTransfer, nameof(stockTransfer)).NotNull(); stockTransfer.Created = stockTransfer.Updated = DateTimeOffset.UtcNow; await ValidateStockTransfer(stockTransfer); await using var transaction = await _unitOfWork.GetTransaction(); try { var fromWareHouse = await _wareHouseService.GetWareHouseById(stockTransfer.FromWareHouseId); if (!_wareHouseService.IsActiveWareHouse(fromWareHouse)) { throw new ServiceException(new ErrorMessage[] { new ErrorMessage() { Message = "From warehouse is not a active one" } }); } var toWareHouse = await _wareHouseService.GetWareHouseById(stockTransfer.ToWareHouseId); if (!_wareHouseService.IsActiveWareHouse(toWareHouse)) { throw new ServiceException(new ErrorMessage[] { new ErrorMessage() { Message = "To warehouse is not a active one" } }); } if (stockTransfer.ToWareHouseId == stockTransfer.FromWareHouseId) { throw new ServiceException(new ErrorMessage[] { new ErrorMessage() { Message = "From and To Warehouses should not be same" } }); } _unitOfWork.StockTransferRepository.Insert(stockTransfer); await _unitOfWork.SaveChanges(); foreach (var stockTransferStockTransferItem in stockTransfer.StockTransferItems) { await _stockService.AdjustStock(StockAdjustmentType.Out, stockTransfer.FromWareHouseId, stockTransferStockTransferItem.ProductId, stockTransferStockTransferItem.UnitCost, stockTransferStockTransferItem.Quantity, stockTransferStockTransferItem.ExpiredDate, $"Stock Transfer - {stockTransfer.StockTransferNumber}"); await _stockService.AdjustStock(StockAdjustmentType.In, stockTransfer.ToWareHouseId, stockTransferStockTransferItem.ProductId, stockTransferStockTransferItem.UnitCost, stockTransferStockTransferItem.Quantity, stockTransferStockTransferItem.ExpiredDate, $"Stock Transfer - {stockTransfer.StockTransferNumber}"); } await transaction.CommitAsync(); return(stockTransfer); } catch { await transaction.RollbackAsync(); throw; } }