// Order product from cart // Get: Branch/Order/Makeorder public ActionResult Makeorder() { // Init the cart list var cart = Session["cart"] as List <ProductVM> ?? new List <ProductVM>(); // Set current time var now = preUni.getCurrentTime(); using (Db db = new Db()) { //Get User dto var userdto = db.Users.Find(User.Identity.Name); var orderSeqs = db.OrderSeqs.FirstOrDefault(x => x.BranchCode == userdto.BranchCode); string orderNo = now.Year.ToString() + now.Month.ToString("d2") + userdto.BranchCode.ToString("d2") + orderSeqs.OrderSeq.ToString("d4"); var orderdto = new OrderDTO { OrderNo = orderNo, UserId = userdto.UserId, BranchCode = userdto.BranchCode, CreatedAt = now }; // Add to OrderDTO and save orderSeqs.OrderSeq++; if (orderSeqs.OrderSeq > 9999) { orderSeqs.OrderSeq = 1; } db.Orders.Add(orderdto); db.SaveChanges(); decimal grandTotal = 0; // Loop items in cart foreach (var item in cart) { if (item.Quantity == 0 && item.TeachersCopyQty == 0) { continue; } var orderDetailsDTO = new OrderDetailsDTO { OrderId = orderdto.OrderId, UserId = userdto.UserId, ProductId = item.ProductId, Quantity = item.Quantity, TeachersCopyQty = item.TeachersCopyQty, Week = preUni.productToWeek(item.ProductId, now), Status = 0, Price = item.Total, }; db.OrderDetails.Add(orderDetailsDTO); db.SaveChanges(); grandTotal += item.Total; } orderdto.GrandTotal = grandTotal; db.SaveChanges(); #region Generate Mail var preUniOrder = new preUniOrder(orderdto.OrderId); string subject = "Order Confirmation Notification_" + userdto.BranchName + "_OrderNo: " + orderdto.OrderNo; sendMail.send("*****@*****.**", subject, preUniOrder.ConfirmMail()); //sendMail.send(userdto.EmailAddress, subject, preUniOrder.ConfirmMail()); string invoiceSubject = "Order Invoice Notification_" + userdto.BranchName + "_OrderNo: " + orderdto.OrderNo; string invoiceBody = @"<div style=""max-width:800px;"">" + preUniOrder.MakeInvoice() + "</div>"; //sendMail.send(userdto.EmailAddress, invoiceSubject, invoiceBody); sendMail.send("*****@*****.**", invoiceSubject, invoiceBody); #endregion preUni.logging(User.Identity.Name, "New order : " + orderdto.OrderNo); Session["cart"] = null; } // Redirect return(Redirect("~/Branch/Order/OrderStatement")); }
public ActionResult MakeEditedOrder(ICollection <OrderDetailVM> model) { // Check model state if (!ModelState.IsValid) { TempData["FM"] = "Order make error - input data invalid"; return(RedirectToAction("OrderStatement")); } //Set time var now = preUni.getCurrentTime(); using (Db db = new Db()){ //Get user dto var userdto = db.Users.Find(model.FirstOrDefault().UserId); int branchCode = userdto.BranchCode; //Set Order Number var orderSeqs = db.OrderSeqs.FirstOrDefault(x => x.BranchCode == branchCode); string orderNo = now.Year.ToString() + now.Month.ToString("d2") + branchCode.ToString("d2") + orderSeqs.OrderSeq.ToString("d4"); //Make New Order var newOrder = new OrderDTO() { OrderNo = orderNo, BranchCode = branchCode, CreatedAt = now, UserId = User.Identity.Name }; orderSeqs.OrderSeq++; db.Orders.Add(newOrder); db.SaveChanges(); //Get order id int orderId = newOrder.OrderId; decimal grandTotal = 0; //Loop items in model foreach (var item in model) { if (item.Quantity == 0 && item.TeachersQty == 0) { continue; } var unitPrice = db.Products.Find(item.ProductId).Price; var price = unitPrice * item.Quantity; grandTotal += price; var newOrderDetail = new OrderDetailsDTO() { OrderId = orderId, UserId = User.Identity.Name, ProductId = item.ProductId, Quantity = item.Quantity, Status = 0, TeachersCopyQty = item.TeachersQty, Week = preUni.productToWeek(item.ProductId, now), Price = price }; db.OrderDetails.Add(newOrderDetail); newOrder.GrandTotal = grandTotal; } #region Generate Mail var preUniOrder = new preUniOrder(newOrder.OrderId); string subject = "Order Confirmation Notification_" + userdto.BranchName + "_OrderNo: " + newOrder.OrderNo; sendMail.send("*****@*****.**", subject, preUniOrder.ConfirmMail()); //sendMail.send(userdto.EmailAddress, subject, preUniOrder.ConfirmMail()); string invoiceSubject = "Order Invoice Notification_" + userdto.BranchName + "_OrderNo: " + newOrder.OrderNo; string invoiceBody = @"<div style=""max-width:800px;"">" + preUniOrder.MakeInvoice() + "</div>"; //sendMail.send(userdto.EmailAddress, invoiceSubject, invoiceBody); sendMail.send("*****@*****.**", invoiceSubject, invoiceBody); #endregion preUni.logging(User.Identity.Name, "make editted order : " + newOrder.OrderNo); } return(RedirectToAction("OrderStatement")); }