public async Task <IActionResult> Update([FromBody] BillViewModel billVmPost) { var hasPermission = await _authorizationService.AuthorizeAsync(User, "BILL", Operations.Update); if (hasPermission.Succeeded == false) { return(new BadRequestObjectResult(CommonConstants.Forbidden)); } if (ModelState.IsValid) { if (billVmPost.BillStatus == BillStatus.Completed) { List <BillDetailViewModel> billDetailVm = _billService.GetBillDetails(billVmPost.Id); foreach (var item in billDetailVm) { Product productDb = _productService.GetProductDbById(item.ProductId); productDb.ViewCount = productDb.ViewCount + item.Quantity; _productService.UpdateDb(productDb); ProductQuantity productQuantityDb = _productQuantityService.GetSingleDb(item.ProductId, item.SizeId, item.ColorId); productQuantityDb.Quantity = productQuantityDb.Quantity - item.Quantity; _productQuantityService.UpdateDb(productQuantityDb); _billService.SaveChanges(); } } if (string.IsNullOrEmpty(billVmPost.CustomerId.ToString())) { _billService.Update(billVmPost); _billService.SaveChanges(); return(new OkObjectResult(billVmPost.Id)); } else { BillStatus billStauts = billVmPost.BillStatus; if (billStauts == BillStatus.Cancelled || billStauts == BillStatus.Returned) { AppUser appUser = await _userManger.FindByIdAsync(billVmPost.CustomerId.ToString()); var totalBalance = appUser.Balance; appUser.Balance = appUser.Balance + (decimal)billVmPost.TotalMoneyOrder + (decimal)billVmPost.FeeShipping - (decimal)billVmPost.BalanceForBill - (decimal)billVmPost.TotalMoneyPayment; var result = await _userManger.UpdateAsync(appUser); if (result.Succeeded) { _billService.Update(billVmPost); _billService.SaveChanges(); return(new OkObjectResult(billVmPost.Id)); } else { return(new BadRequestObjectResult(ModelState)); } } else { _billService.Update(billVmPost); _billService.SaveChanges(); return(new OkObjectResult(billVmPost.Id)); } } } return(new BadRequestObjectResult(ModelState)); }