public async Task <ActionResult <StockBatchDetailView> > CreateStockBatch([FromBody] NewStockBatchDto newBatch) { try { if (newBatch == null) { return(StatusCode(400)); } if (!ModelState.IsValid) { return(new InvalidInputResponse(ModelState)); } var result = await _Mediator.Send(newBatch); var createdBatch = await _Mediator.Send(new GetStockBatchDetailViewQuery() { Id = result }); return(StatusCode(201, createdBatch)); } catch (NotFoundException e) { return(StatusCode(404, e.Message)); } catch (BelowRequiredMinimumItemException e) { ModelState.AddModelError("Storage", e.Message); return(new InvalidInputResponse(ModelState)); } catch (InequalMasterDetailQuantityException e) { ModelState.AddModelError("Storage", e.Message); return(new InvalidInputResponse(ModelState)); } }
public async Task <Unit> Handle(InventoryAdjustmentDto request, CancellationToken cancellationToken) { var item = await _database.Item.FindAsync(request.ItemId); if (item == null) { throw new NotFoundException(nameof(Item), request.ItemId); } var totalInventory = await _database.StockBatchStorage .Include(a => a.Batch) .Include(b => b.WriteOffDetail) .Where(storage => storage.Batch.ItemId == request.ItemId) .ToListAsync(); var quantity = totalInventory .Where(b => b.Batch.Status.Trim().ToUpper() == "RECIEVED") .GroupBy(d => d.Batch.ItemId) .Sum(i => i.Sum(f => f.Quantity - f.WriteOffDetail.Sum(s => s.Quantity))); if (quantity > request.NewQuantity) { Console.WriteLine("its a write off"); } if (quantity < request.NewQuantity) { var lastLot = await _database.StockBatch .FirstOrDefaultAsync(lot => lot.ItemId == request.ItemId); Console.WriteLine("its a new Lot"); var addedAmount = request.NewQuantity - quantity; NewStockBatchDto newLot = new NewStockBatchDto() { Quantity = addedAmount, ItemId = request.ItemId, AvailableFrom = DateTime.Now, Status = "Recieved", UnitCost = lastLot.UnitCost, Source = "System Generated", StorageLocation = new List <NewStockBatchStorageDto> () { new NewStockBatchStorageDto() { StorageId = item.DefaultStorageId, Quantity = addedAmount } }, }; var result = await _Mediator.Send(newLot); } return(Unit.Value); }