public ActionResult AddOrderToDB(FormCollection collection) { var name = collection["Name"]; var email = collection["Email"]; var model = GetCartDetails(out double total); var order = CreateOrder(model); var client = new Client { Name = name, Email = email, }; db.Client.Add(client); order.Client = client; db.Order.Add(order); db.SaveChanges(); string directoryPath = "~/ReportTemplates/"; string fileName = $"Receipt_Order{order.ID}.docx"; string templateFile = "ReceiptTemplate.docx"; string receiptFileName = Server.MapPath(directoryPath + fileName); string templateFileName = Server.MapPath(directoryPath + templateFile); ReceiptManager.CreateReceipt(order, templateFileName, receiptFileName); Response.ContentType = "Application/msword"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); Response.TransmitFile(receiptFileName); Response.End(); string virtualPath = Request.MapPath(directoryPath + fileName); System.IO.File.Delete(virtualPath); Session["Cart"] = null; return(RedirectToAction("Index", "Home")); }
protected void btnSave_Click(object sender, EventArgs e) { //建立資料model及ReceiptManager的物件 var manager = new ReceiptManager(); var model = new ReceiptModel(); //建立5個變數來存取各個輸入值 string inputRecNo = this.txtReceiptNumber.Text.Trim(); string inputDate = this.lbDate.Text.Trim(); string dplCompany = this.dplCompany.SelectedValue; string inputAmount = this.txtAmount.Text.Trim(); string dplRE = this.dplRE.SelectedValue; //檢查輸入值(發票號碼、日期、金額) if (string.IsNullOrEmpty(inputRecNo) || string.IsNullOrEmpty(inputAmount) || string.Equals(inputDate, "請選擇日期")) { //日期沒輸入會變成紅色 if (string.Equals(inputDate, "請選擇日期")) { this.lbDate.ForeColor = System.Drawing.Color.Red; } this.lblMsg.Text = "請填入完整的發票資料"; return; } else if (ReceiptDetailHelper.checkReceiptNumber(inputRecNo) != string.Empty) { this.lblMsg.Text = "請填入正確的發票格式"; return; } else if (ReceiptDetailHelper.checkAmount(inputAmount) != string.Empty) { this.lblMsg.Text = "金額只能填入數字"; return; } //上面檢查都通過後,將輸入值存入資料model model.ReceiptNumber = inputRecNo; model.Date = DateTime.Parse(inputDate); model.Company = dplCompany; model.Amount = decimal.Parse(inputAmount); model.Revenue_Expense = (Revenue_Expense)Enum.Parse(typeof(Revenue_Expense), dplRE); //分成更新模式及新增模式 //更新模式下將資料model更新至資料庫 //新增模式下將資料model存入資料庫 if (ReceiptDetailHelper.isUpdateMode()) { manager.UpdateReceipt(model); this.lblMsg.Text = "發票更新成功"; } else { manager.CreateReceipt(model); this.lblMsg.Text = "發票新增成功"; } }
public IActionResult Create(ReceiptDetailsViewModel viewModel) { if (ModelState.IsValid) { var userID = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); var restaurant = _restaurantManager.GetRestaurantByUserID(userID); var tax = restaurant.Tax; var orderID = viewModel.OrderID; var order = _orderManager.GetOrderByID(orderID); var receipt = _receiptManager.CreateReceipt(order, userID, tax); return(RedirectToAction(nameof(Index))); } return(View(viewModel)); }