public async Task <IActionResult> UpdatePurchaseDetail([FromRoute] int id, [FromBody] UpdatePurchaseDetailStatus updatePurchaseDetailStatus) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (updatePurchaseDetailStatus == null) { return(BadRequest()); } var date = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Pacific Standard Time"); var purchaseDetail = await _context.PurchaseDetail .Include(o => o.Product) .Include(o => o.Location) .FirstOrDefaultAsync(m => m.PurchaseDetailId == id); if (purchaseDetail == null) { return(BadRequest($"Invalid Purchase Detail Id : {id}")); } System.Security.Claims.ClaimsPrincipal currentUser = this.User; var userId = _userManager.GetUserId(User); if (updatePurchaseDetailStatus.OverheadCost == null) { updatePurchaseDetailStatus.OverheadCost = 0; } if (updatePurchaseDetailStatus.UnitPrice == null) { updatePurchaseDetailStatus.UnitPrice = 0; } purchaseDetail.Amount = updatePurchaseDetailStatus.Amount; purchaseDetail.UnitPrice = updatePurchaseDetailStatus.UnitPrice; purchaseDetail.OverheadCost = updatePurchaseDetailStatus.OverheadCost; purchaseDetail.TotalPrice = Math.Round(updatePurchaseDetailStatus.UnitPrice.Value * updatePurchaseDetailStatus.Amount + updatePurchaseDetailStatus.OverheadCost.Value, 2); await _context.SaveChangesAsync(); return(Ok(purchaseDetail)); }
private async Task <bool> AddToInventory(UpdatePurchaseDetailStatus updatePurchaseDetailStatus, PurchaseDetail purchaseDetail, DateTime date) { var productInventory = await _context.ProductInventory.FirstOrDefaultAsync(m => m.ProductId == purchaseDetail.ProductId && m.LocationId == updatePurchaseDetailStatus.ArrivedAtLocationId); decimal currentBalance = 0; if (productInventory != null) { currentBalance = productInventory.Balance; productInventory.Balance = productInventory.Balance + updatePurchaseDetailStatus.Amount; productInventory.ModifiedDate = date; } else { await _context.ProductInventory.AddAsync( new ProductInventory { Balance = updatePurchaseDetailStatus.Amount, BinCode = "", LocationId = updatePurchaseDetailStatus.ArrivedAtLocationId.Value, ModifiedDate = date, ProductId = purchaseDetail.ProductId }); } var productInventoryHistory = new ProductInventoryHistory { ChangedBalance = currentBalance + updatePurchaseDetailStatus.Amount, Balance = updatePurchaseDetailStatus.Amount, Notes = $"Purchase Id: {purchaseDetail.PurchaseId} arrived", BinCode = "", LocationId = updatePurchaseDetailStatus.ArrivedAtLocationId.Value, CreatedByUserId = purchaseDetail.CreatedByUserId, ModifiedDate = date, ProductId = purchaseDetail.ProductId, TransactionType = "Purchase Arrived" }; _context.ProductInventoryHistory.Add(productInventoryHistory); return(true); }
public async Task <IActionResult> PutPurchaseDetail([FromRoute] int id, [FromBody] UpdatePurchaseDetailStatus updatePurchaseDetailStatus) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (updatePurchaseDetailStatus == null || string.IsNullOrEmpty(updatePurchaseDetailStatus.PurchaseStatus)) { return(BadRequest()); } var date = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Pacific Standard Time"); var purchaseDetail = await _context.PurchaseDetail .Include(o => o.Product) .Include(o => o.Location) .FirstOrDefaultAsync(m => m.PurchaseDetailId == id); if (purchaseDetail == null) { return(BadRequest($"Invalid Purchase Detail Id : {id}")); } System.Security.Claims.ClaimsPrincipal currentUser = this.User; var userId = _userManager.GetUserId(User); if (updatePurchaseDetailStatus.OverheadCost == null) { updatePurchaseDetailStatus.OverheadCost = 0; } if (updatePurchaseDetailStatus.UnitPrice == null) { updatePurchaseDetailStatus.UnitPrice = 0; } var newPurchaseDetail = new PurchaseDetail { Amount = updatePurchaseDetailStatus.Amount, ArrivedAtLocationId = updatePurchaseDetailStatus.ArrivedAtLocationId, ArrivedDate = updatePurchaseDetailStatus.ArrivedDate, PoNumber = updatePurchaseDetailStatus.PoNumber, CreatedByUserId = userId, CreatedDate = date, EstimatedDelivery = updatePurchaseDetailStatus.EstimatedDelivery, PaidDate = updatePurchaseDetailStatus.PaidDate, ProductId = purchaseDetail.ProductId, Status = updatePurchaseDetailStatus.PurchaseStatus, UnitPrice = updatePurchaseDetailStatus.UnitPrice, OverheadCost = updatePurchaseDetailStatus.OverheadCost, TotalPrice = Math.Round(updatePurchaseDetailStatus.UnitPrice.Value * updatePurchaseDetailStatus.Amount + updatePurchaseDetailStatus.OverheadCost.Value, 2), PurchaseId = purchaseDetail.PurchaseId }; await _context.PurchaseDetail.AddAsync(newPurchaseDetail); // When purchase is marked as Arrived we should add them to inventory of the specified location if (updatePurchaseDetailStatus.PurchaseStatus == PurchaseStatus.Arrived.ToString() && updatePurchaseDetailStatus.ArrivedAtLocationId.HasValue) { var arrivedDate = purchaseDetail.ArrivedDate.HasValue ? purchaseDetail.ArrivedDate.Value : date; var done = await AddToInventory(updatePurchaseDetailStatus, purchaseDetail, arrivedDate); } // _emailSender.SendAdminReportAsync("Purchase Status Changed", $"Purchase Status changed. \n Purchase Id: {id}. \n To: {updatePurchaseDetailStatus.PurchaseStatus.ToString()}"); await _context.SaveChangesAsync(); return(Ok(newPurchaseDetail)); }