//Save CashTransaction public ActionResult Select(int id) { var cashTransaction = new CashTransaction(); var cashTransactionItems = new CashTransactionItem(); double totalPrice = 0; var temps = _context.TempCarts.ToList(); foreach (var c in temps) { ViewData["products"] = c; totalPrice = totalPrice + (c.ProductPrice * c.Quantity); } var count = _context.CashTransactions.Count(); var cashId = Convert.ToString(DateTime.Today.Year) + "0" + Convert.ToString(count + 1) + "00" + Convert.ToString(DateTime.Today.Day); var selectCustomer = _context.Customers.SingleOrDefault(c => c.Id == id); var cashTransacItem = new CashTransactionItem(); if (selectCustomer.Id != 0) { foreach (var g in temps) { cashTransacItem.CashTransactionId = Convert.ToInt16(cashId); cashTransacItem.ProductId = g.ProductId; cashTransacItem.Quantity = g.Quantity; cashTransacItem.ProductPrice = g.ProductPrice; _context.CashTransactionItems.Add(cashTransacItem); } cashTransaction.Id = Convert.ToInt16(cashId); cashTransaction.CustomerId = selectCustomer.Id; cashTransaction.OriginalTotalAmount = totalPrice; cashTransaction.TotalDiscountedAmount = 0; cashTransaction.TotalAmount = cashTransaction.OriginalTotalAmount - cashTransaction.TotalDiscountedAmount; cashTransaction.Remarks = ""; cashTransaction.OR = 0; _context.CashTransactions.Add(cashTransaction); } _context.SaveChanges(); return(RedirectToAction("CashTransactionSummary" /*, cashId*/)); }
public ActionResult Save(CashTransaction cashTransaction) { var cashTransactionItem = new CashTransactionItem(); foreach (var item in _context.TempCarts.ToList()) { cashTransactionItem.ProductId = item.ProductId; cashTransactionItem.CashTransactionId = cashTransaction.Id; cashTransactionItem.ProductPrice = item.ProductPrice; cashTransactionItem.Quantity = item.Quantity; _context.CashTransactionItems.Add(cashTransactionItem); //minus product stock to available for selling _context.Products.SingleOrDefault(p => p.Id == item.ProductId).AvailableForSelling = _context.Products.SingleOrDefault(p => p.Id == item.ProductId).AvailableForSelling - item.Quantity; } var cash = _context.CashTransactions.SingleOrDefault(c => c.Id == cashTransaction.Id); cash.CustomerId = cashTransaction.CustomerId; cash.CashTransactionDate = DateTime.Now; cash.OR = cashTransaction.OR; cash.TotalAmount = cash.OriginalTotalAmount - cash.TotalDiscountedAmount; cash.TotalDiscountedAmount = cashTransaction.TotalDiscountedAmount; cash.Remarks = cashTransaction.Remarks; var temp = _context.TempCarts; foreach (var d in temp.ToList()) { _context.TempCarts.Remove(_context.TempCarts.SingleOrDefault(c => c.Id == d.Id)); } _context.SaveChanges(); return(RedirectToAction("Index")); }