public static string GetOrderPdfPath(PdfOrderModel model) { model.ReportPath = GetReportPath(); var htmlUrl = GetOrderHtmlUrl(model); if (string.IsNullOrEmpty(htmlUrl)) { return(string.Empty); } /* page settings */ var converter = new HtmlToPdf(); converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; converter.Options.MarginTop = 24; converter.Options.MarginLeft = 10; converter.Options.MarginRight = 10; converter.Options.MarginBottom = 0; /* footer settings */ converter.Options.DisplayFooter = true; converter.Footer.DisplayOnFirstPage = true; converter.Footer.DisplayOnOddPages = true; converter.Footer.DisplayOnEvenPages = true; converter.Footer.Height = 24; converter.Footer.Add(new PdfTextSection(0, -10, "Page: {page_number} of {total_pages}", new Font("Arial", 6)) { HorizontalAlign = PdfTextHorizontalAlign.Center }); /* new pdf */ var pdfPath = string.Format(@"{0}\{1}.pdf", model.ReportPath, model.FileName); var fullPath = string.Format(@"{0}\{1}", MapPath, pdfPath); var doc = converter.ConvertUrl(htmlUrl); doc.Save(fullPath); doc.Close(); return(fullPath); }
public string TestGetOrderPdfPath() { var model = new PdfOrderModel { RootUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority), TemplatePath = System.Configuration.ConfigurationManager.AppSettings["Template_JobOrder"], FileName = string.Format("joborder{0:mmssfffff}", DateTime.Now), HeadContact = "HeadContact", HeadDeliverTo = "HeadDeliverTo", HeadOrderTo = "HeadOrderTo", HeadOrderDate = "HeadOrderDate", HeadRefNumber = "HeadOrderNumber", HeadAccountManager = "HeadAccountManager", SumSubTotal = "$555.50", SumGst = "$100.00", SumAmount = "$655.50", OrderDetailList = new List <PdfOrderDetailModel> { new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Page1-End", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, /* Page1 = 26 */ new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, new PdfOrderDetailModel { StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90" }, new PdfOrderDetailModel { StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60" }, new PdfOrderDetailModel { StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50" }, new PdfOrderDetailModel { StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10" }, new PdfOrderDetailModel { StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40" }, //new PdfOrderDetailModel{StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90"}, //new PdfOrderDetailModel{StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60"}, //new PdfOrderDetailModel{StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50"}, //new PdfOrderDetailModel{StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10"}, //new PdfOrderDetailModel{StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40"}, //new PdfOrderDetailModel{StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90"}, //new PdfOrderDetailModel{StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60"}, /* Page2 = 38 */ //new PdfOrderDetailModel{StockCode = "Page2-End", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50"}, //new PdfOrderDetailModel{StockCode = "Product-A", Description = "Description-A", Qty = "$10.00", UnitPrice = "$1.01", LineTotal = "$10.10"}, //new PdfOrderDetailModel{StockCode = "Product-B", Description = "Description-B", Qty = "$20.00", UnitPrice = "$2.02", LineTotal = "$40.40"}, //new PdfOrderDetailModel{StockCode = "Product-C", Description = "Description-C", Qty = "$30.00", UnitPrice = "$3.03", LineTotal = "$90.90"}, //new PdfOrderDetailModel{StockCode = "Product-D", Description = "Description-D", Qty = "$40.00", UnitPrice = "$4.04", LineTotal = "$161.60"}, //new PdfOrderDetailModel{StockCode = "Product-E", Description = "Description-E", Qty = "$50.00", UnitPrice = "$5.05", LineTotal = "$252.50"}, } }; return(Common.PdfHelper.GetOrderPdfPath(model)); }
public async Task CreateOrderPdfAndSendEmail(string rootUrl, int managerId, OrderInfoModel model, Order order, string name, string isTripleEmail = null) { try { var manager = await _managerRepository.GetById(managerId); var orderAddr = model.ToOrderAddrModel(); var pdfModel = new PdfOrderModel() { RootUrl = rootUrl, TemplatePath = System.Configuration.ConfigurationManager.AppSettings["Template_JobOrder"], FileName = string.Format("order{0}", order.OrderCode), HeadContact = "PO Box 1111" + "<br>" + "Burswood WA 6100 " + "<br>" + "T (08) 9470 7300" + "<br>" + "F (08) 9362 6210" + "<br>" + "<br>" + "E [email protected]" + "<br>" + "www.datanet.com.au" + "<br>" + "<br>" + "ABN: 56 123 914 616", HeadDeliverTo = order.ContactName + (!string.IsNullOrEmpty(order.ContactName) ?"<br>":"") + order.CompanyName + (!string.IsNullOrEmpty(order.CompanyName) ? "<br>" : "") + orderAddr?.Addr1 + (!string.IsNullOrEmpty(orderAddr?.Addr1) ? "<br>" : "") + orderAddr?.Addr2 + (!string.IsNullOrEmpty(orderAddr?.Addr2) ? "<br>" : "") + orderAddr?.Addr3 + (!string.IsNullOrEmpty(orderAddr?.Addr3) ? "<br>" : "") + orderAddr?.State + " " + orderAddr?.PostCode, HeadOrderTo = order.CompanyName, HeadOrderDate = order.CreateTime?.AddHours(model.TimeZone).ToString("dd'/'MM'/'yyyy"), HeadRefNumber = order.OrderCode, HeadAccountManager = order.ManagerName + "<br>" + manager?.Phone + "<br>" + "<a href='mailto:" + manager?.Email + "'>" + manager?.Email + "</a>", SumSubTotal = (order.Amount - order.GST)?.ToString("c2", CulturesHelper.Australia), SumGst = order.GST?.ToString("c2", CulturesHelper.Australia), SumAmount = order.Amount?.ToString("c2", CulturesHelper.Australia), OrderDetailList = new List <PdfOrderDetailModel>() { } }; foreach (var productItem in model.OrderProducts) { var product = await _productRepository.GetById(productItem.ProductId); pdfModel.OrderDetailList.Add(new PdfOrderDetailModel() { StockCode = product.Code, Description = product.ShortDesc, Qty = productItem.Quantity.ToString() + ".00", UnitPrice = productItem.Price?.ToString("c2", CulturesHelper.Australia), LineTotal = ((productItem.Quantity * productItem.Price)?.ToString("c2", CulturesHelper.Australia)) }); } pdfModel.OrderDetailList.Add(new PdfOrderDetailModel() { StockCode = "DELIVERY", Description = "Delivery", Qty = "1.00", UnitPrice = model.DeliveryCharge.ToString("c2", CulturesHelper.Australia), LineTotal = model.DeliveryCharge.ToString("c2", CulturesHelper.Australia) }); order.PDFPath = PdfHelper.GetOrderPdfPath(pdfModel); await _orderRepositroy.AddOrUpdate(order); var sendEmailResult = await SendEmailById(order.Id, rootUrl, isTripleEmail); if (sendEmailResult.Status != 0) { throw new Exception(sendEmailResult.Message); } } catch (Exception ex) { //add order history var history = new OrderHistory { Id = 0, CreateTime = DateTime.UtcNow, CreateBy = name, TableName = "", FieldName = "", OldValue = "", NewValue = "", Note = Constants.OrderHistoryAddOrder, Operator = Constants.OrderHistoryErrorOperator, OrderId = order.Id }; await _orderHistoryRepository.AddOrUpdate(history); Logger.WriteErrorLogForOrder("OrderService", "CreateOrderPdfAndSendEmail", ex, order.Id); } }
private static string GetOrderHtmlUrl(PdfOrderModel model) { var htmlUrl = string.Format(@"{0}/{1}", model.RootUrl, model.TemplatePath); var request = (HttpWebRequest)WebRequest.Create(htmlUrl); var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { return(string.Empty); } var responseStream = response.GetResponseStream(); if (responseStream == null) { return(string.Empty); } var readStream = response.CharacterSet == null ? new StreamReader(responseStream) : new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet)); var content = readStream.ReadToEnd(); /* Value Replacement */ content = content.Replace("{{head_contact}}", model.HeadContact); content = content.Replace("{{head_order_date}}", model.HeadOrderDate); content = content.Replace("{{head_ref_number}}", model.HeadRefNumber); content = content.Replace("{{head_account_manager}}", model.HeadAccountManager); content = content.Replace("{{head_order_to}}", model.HeadOrderTo); content = content.Replace("{{head_deliver_to}}", model.HeadDeliverTo); content = content.Replace("{{sum_sub_total}}", model.SumSubTotal); content = content.Replace("{{sum_gst}}", model.SumGst); content = content.Replace("{{sum_amount}}", model.SumAmount); #region Build Order Table var needManualClosed = false; var totalCount = model.OrderDetailList.Count; const string rowPageBreak = @"<div class='row'><div style='page-break-before: always'></div></div>"; const string tableHead = @"<div class='row'><div class='col-xs-12 table_right_angled' style='padding-top: 2px;'> <table class='table table-bordered right_angled'> <thead> <tr> <th style='width: 250px;'>STOCKCODE</th> <th>DESCRIPTION</th> <th style='width: 80px;'>QTY</th> <th style='width: 120px;'>UNIT PRICE</th> <th style='width: 120px;'>LINE TOTAL</th> </tr> </thead> <tbody>" ; const string tableRow = @"<tr><td>{0}</td><td>{1}</td><td class='text-right'>{2}</td><td class='text-right'>{3}</td><td class='text-right'>{4}</td></tr>"; const string tableFoot = @"</tbody></table></div></div>"; var sbDetailList = new StringBuilder(tableHead); foreach (var item in model.OrderDetailList) { sbDetailList.AppendLine(string.Format(tableRow, item.StockCode, item.Description, item.Qty, item.UnitPrice, item.LineTotal)); var currentNumber = model.OrderDetailList.IndexOf(item) + 1; var otherPageRemainder = (currentNumber - 26) % 38; #region Page Break /* Bank Panel */ var pageBreakBank = totalCount >= 20 && totalCount <= 25 || otherPageRemainder >= 32 && otherPageRemainder <= 37 ? rowPageBreak : string.Empty; content = content.Replace("{{page_break_bank}}", pageBreakBank); /* Terms Panel */ var pageBreakTerm = totalCount >= 17 && totalCount <= 19 || otherPageRemainder >= 29 && otherPageRemainder <= 31 ? rowPageBreak : string.Empty; content = content.Replace("{{page_break_term}}", pageBreakTerm); #endregion needManualClosed = currentNumber == 26 || otherPageRemainder == 0; if (!needManualClosed) { continue; } sbDetailList.AppendLine(tableFoot); sbDetailList.AppendLine(rowPageBreak); if (currentNumber >= totalCount) { continue; } sbDetailList.AppendLine(tableHead); } if (!needManualClosed) { sbDetailList.AppendLine(tableFoot); } #endregion content = content.Replace("{{table_details}}", sbDetailList.ToString()); var htmlPath = string.Format(@"{0}\{1}.html", model.ReportPath, model.FileName); var fullPath = string.Format(@"{0}\{1}", MapPath, htmlPath); File.WriteAllText(fullPath, content); readStream.Close(); response.Close(); return(string.Format(@"{0}/{1}", model.RootUrl, htmlPath)); }