public ActionResult AddPayment(SmallBusinessPaymentViewModel viewModel) { var repository = new SmallBusinessRepository(); var smallBusiness = repository.GetById(viewModel.SmallBusinessId); repository.Dispose(); if (smallBusiness == null) { return(HttpNotFound()); } if (smallBusiness.SellingPrice < smallBusiness.PaymentReceived + viewModel.PaidAmount) { ViewBag.Title = "Add Small Business Payment"; TempData["ErrorMessage"] = "Total Payment Received exceeded selling price"; return(View(viewModel)); } using (repository = new SmallBusinessRepository()) { repository.ReceivePayment(viewModel); } TempData["SuccessMessage"] = "Payment Added successfully."; return(RedirectToAction("Index")); }
internal void ReceivePayment(SmallBusinessPaymentViewModel viewModel) { var smallBusiness = _context.SmallBusinesses .Where(x => x.Id == viewModel.SmallBusinessId) .Include(x => x.Installments) .Include(x => x.Payments) .Include(x => x.InvolvedSavingAccounts) .SingleOrDefault(); if (smallBusiness != null && (smallBusiness.SellingPrice >= smallBusiness.PaymentReceived + viewModel.PaidAmount) && viewModel.PaidAmount > 0) { smallBusiness.PaymentReceived += viewModel.PaidAmount; var tempPaymentAmount = smallBusiness.PaymentReceived; bool allPaid = true; foreach (var item in smallBusiness.Installments.OrderBy(x => x.DueDate)) { if (tempPaymentAmount < item.Amount) { smallBusiness.PaymentDueDate = item.DueDate; allPaid = false; break; } tempPaymentAmount -= item.Amount; } if (allPaid) { smallBusiness.PaymentDueDate = null; } var oSmallBusinessPayment = new SmallBusinessPayment(); oSmallBusinessPayment.Amount = viewModel.PaidAmount; var individualAmount = viewModel.PaidAmount / (decimal)smallBusiness.InvolvedSavingAccounts.Count(); oSmallBusinessPayment.Transactions = smallBusiness.InvolvedSavingAccounts .Select(x => new Transaction { SavingAccountId = x.SavingAccountId, Amount = individualAmount, Description = string.Format("Small Business Payment:- Product:{0:N2}, Received: {1:N2} , Total Received:{2:N2} of {3:N2}, Total accounts involved:{4}", smallBusiness.Product, viewModel.PaidAmount, smallBusiness.PaymentReceived, smallBusiness.SellingPrice, smallBusiness.InvolvedSavingAccounts.Count), TransactionDate = DateTime.Today, Type = TransactionType.Small_Business_Payment, TransactionSide = TransactionSide.Credit }) .ToList(); smallBusiness.Payments.Add(oSmallBusinessPayment); _context.SaveChanges(); } }
public SmallBusinessPaymentViewModel GetPaymentViewModelById(int id) { SmallBusinessPaymentViewModel smallBusinessPaymentViewModel = null; var smallBusiness = _context.SmallBusinesses.Where(x => x.Id == id).Include(x => x.Installments).SingleOrDefault(); if (smallBusiness != null) { smallBusinessPaymentViewModel = new SmallBusinessPaymentViewModel { Product = smallBusiness.Product, SmallBusinessId = smallBusiness.Id, CustomerName = smallBusiness.CustomerName, SellingPrice = smallBusiness.SellingPrice, PaymentReceived = smallBusiness.PaymentReceived }; decimal dueAmount = smallBusiness.Installments.Where(x => x.DueDate <= DateTime.Today).Sum(x => x.Amount); smallBusinessPaymentViewModel.PaymentDue = smallBusiness.PaymentReceived >= dueAmount ? 0 : dueAmount - smallBusiness.PaymentReceived; } return(smallBusinessPaymentViewModel); }