public async Task AdjustInventoryShouldAdjustInventoryCorrectlyWhenProductWarehouseDoesntExist() { var options = new DbContextOptionsBuilder <WHMSDbContext>().UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; using var context = new WHMSDbContext(options); var warehouse = new Warehouse { Address = new Address { }, Name = "Test", }; var product = new Product { ProductName = "Test Product", }; context.Warehouses.Add(warehouse); context.Products.Add(product); await context.SaveChangesAsync(); var service = new InventoryService(context); var adjustment = new ProductAdjustmentInputModel { ProductId = product.Id, Qty = 10, WarehouseId = warehouse.Id }; await service.AdjustInventoryAsync(adjustment); var productWarehouse = context.ProductWarehouses.FirstOrDefault(); var expected = 10; Assert.NotNull(productWarehouse); Assert.Equal(expected, productWarehouse.AggregateQuantity); }
public async Task <IActionResult> AdjustInventory(ProductAdjustmentInputModel input) { if (!this.ModelState.IsValid) { return(this.RedirectToAction("ManageInventory", new { id = input.ProductId })); } if (this.productService.IsValidProductId(input.ProductId)) { await this.inventoryService.AdjustInventoryAsync(input); } return(this.Redirect("ManageInventory/" + input.ProductId)); }
public async Task ReceivePurchaseItem(int purchaseItemId) { var item = this.context.PurchaseItems.Include(x => x.Product).Include(x => x.PurchaseOrder).FirstOrDefault(x => x.Id == purchaseItemId); item.Product.LastCost = item.Cost; var physicalInventory = Math.Max(this.inventoryService.GetProductPhysicalInventory(item.ProductId), 0); item.Product.AverageCost = ((item.Product.AverageCost * physicalInventory) + (item.Cost * item.Qty)) / (physicalInventory + item.Qty); var adjustment = new ProductAdjustmentInputModel { ProductId = item.ProductId, Qty = item.Qty, WarehouseId = item.PurchaseOrder.WarehouseId, }; await this.inventoryService.AdjustInventoryAsync(adjustment); }
public async Task <bool> AdjustInventoryAsync(ProductAdjustmentInputModel input) { var productWarehouse = this.context.ProductWarehouses .FirstOrDefault( x => x.ProductId == input.ProductId && x.WarehouseId == input.WarehouseId); if (productWarehouse == null) { productWarehouse = new ProductWarehouse() { ProductId = input.ProductId, WarehouseId = input.WarehouseId }; this.context.ProductWarehouses.Add(productWarehouse); } productWarehouse.TotalPhysicalQuanitiy += input.Qty; await this.context.SaveChangesAsync(); await this.RecalculateAvailableInventoryAsync(input.ProductId); return(true); }