//public ActionResult AddItem(int? id) //{ // TempProduct temp = db.TempProducts.Find(id); // if (temp.Quantity >= 1) // { // temp.QtyPurchased = temp.QtyPurchased + 1; // temp.Quantity = temp.Quantity - 1; // db.Entry(temp).State = EntityState.Modified; // db.SaveChanges(); // } // else // { // ViewBag.Message = "Insufficient Quntity"; // } // AllClassViewModel viewModel = new AllClassViewModel // { // TempProducts = db.TempProducts.ToList() // }; // return View("ExchangeView", viewModel); //} public ActionResult RemItem(int?id) { SellingHistory sellingHistory = db.SellingHistories.Find(id); Billing bill = db.Billings.Find(sellingHistory.BillingId); Product product = db.Products.Find(sellingHistory.ProductId); if (sellingHistory.Quantity <= 1) { sellingHistory.Quantity = 1; AllClassViewModel viewModel1 = new AllClassViewModel { Billing = bill, Products = db.Products.ToList(), SellingHistories = db.SellingHistories.Where(m => m.BillingId == bill.Id) }; return(View("ExchangeView", viewModel1)); } product.Quantity = product.Quantity + 1; sellingHistory.Quantity = sellingHistory.Quantity - 1; db.Entry(sellingHistory).State = EntityState.Modified; db.Entry(product).State = EntityState.Modified; db.SaveChanges(); AllClassViewModel viewModel = new AllClassViewModel { Billing = bill, Products = db.Products.ToList(), SellingHistories = db.SellingHistories.Where(m => m.BillingId == bill.Id) }; return(View("ExchangeView", viewModel)); }
public ActionResult PrintBill(AllClassViewModel allClass) { bool flag = false; int id = 0; foreach (var item in db.Customers.ToList()) { if (allClass.Customer.PhoneNo.Equals(item.PhoneNo)) { flag = true; id = item.Id; break; } } if (!flag) { Customer customer = new Customer() { Address = allClass.Customer.Address, Name = allClass.Customer.Name, PhoneNo = allClass.Customer.PhoneNo }; db.Customers.Add(customer); db.SaveChanges(); foreach (var item in db.Customers.ToList()) { if (item.PhoneNo == allClass.Customer.PhoneNo) { id = item.Id; break; } } } int totalAmount = 0; foreach (var item in db.TempProducts.ToList()) { totalAmount = totalAmount + (item.Price) * (item.QtyPurchased); } Billing bill = new Billing() { CustomerId = id, AmountPaid = allClass.Billing.AmountPaid, Date = allClass.Billing.Date, DueDate = allClass.Billing.DueDate, AmountDue = totalAmount - allClass.Billing.AmountPaid //enter amount due here }; db.Billings.Add(bill); db.SaveChanges(); var lastbills = db.Billings.ToList(); Billing lastbill = lastbills.Last(); int lastId = lastbill.Id; SellingHistory history = new SellingHistory() { }; foreach (var item in db.TempProducts.ToList()) { foreach (var item1 in db.Products.ToList()) { if (item1.BarCodeId == item.BarCodeId) { history.BillingId = lastId; history.ProductId = item1.Id; history.Quantity = item.QtyPurchased; db.SellingHistories.Add(history); item1.Quantity = item1.Quantity - item.QtyPurchased; db.Entry(item1).State = EntityState.Modified; db.SaveChanges(); } } } foreach (var item in db.TempProducts.ToList()) { db.TempProducts.Remove(item); db.SaveChanges(); } return(RedirectToAction("FinalBillPrint", lastbill)); }