DataTplOrderInvoice.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Globalization;
  13. using GrapeCity.Documents.Word;
  14.  
  15. namespace DsWordWeb.Demos
  16. {
  17. // This data template sample prints an invoice containing the list
  18. // of products in the purchase order.
  19. // The invoice data is generated for a randomly selected
  20. // order from the sample DsNWind database.
  21. public class DataTplOrderInvoice
  22. {
  23. public GcWordDocument CreateDocx(int _ = 0)
  24. {
  25. using var ds = new DataSet();
  26. // Load the sample database, fetch a random supplier
  27. // and a random order from that supplier:
  28. ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"));
  29.  
  30. // Database tables used by the invoice:
  31. var dtSuppliers = ds.Tables["Suppliers"];
  32. var dtOrders = ds.Tables["OrdersCustomersEmployees"];
  33. var dtOrdersDetails = ds.Tables["EmployeesProductsOrders"];
  34.  
  35. // Collect order data:
  36. var random = Util.NewRandom();
  37.  
  38. var fetchedIndex = random.Next(dtSuppliers.Select().Count());
  39. var supplier =
  40. dtSuppliers.Select()
  41. .Skip(fetchedIndex).Take(1)
  42. .Select(it => new
  43. {
  44. SupplierID = Convert.ToInt32(it["SupplierID"]),
  45. CompanyName = it["CompanyName"].ToString(),
  46. ContactName = it["ContactName"].ToString(),
  47. ContactTitle = it["ContactTitle"].ToString(),
  48. Address = it["Address"].ToString(),
  49. City = it["City"].ToString(),
  50. Region = it["Region"].ToString(),
  51. PostalCode = it["PostalCode"].ToString(),
  52. Country = it["Country"].ToString(),
  53. Phone = it["Phone"].ToString(),
  54. Fax = it["Fax"].ToString(),
  55. HomePage = it["HomePage"].ToString()
  56. }).FirstOrDefault();
  57.  
  58. fetchedIndex = random.Next(dtOrders.Select().Count());
  59. var order = dtOrders.Select()
  60. .Skip(fetchedIndex).Take(1)
  61. .Select(it => new
  62. {
  63. OrderDate = it["OrderDate"],
  64. OrderID = Convert.ToInt32(it["OrderID"]),
  65. CompanyName = it["CompanyName"].ToString(),
  66. Name = $"{it["FirstName"]} {it["LastName"]}",
  67. Address = $"{it["ShipAddress"]},\n{it["ShipCity"]} {it["ShipRegion"]} {it["ShipPostalCode"]} {it["ShipCountry"]}",
  68. Email = GetEmail(it["FirstName"].ToString(), it["LastName"].ToString(), it["CompanyName"].ToString()),
  69. }).FirstOrDefault();
  70.  
  71. var orderDetails = dtOrdersDetails.Select()
  72. .Select(it => new
  73. {
  74. OrderID = Convert.ToInt32(it["OrderID"]),
  75. ProductName = it["ProductName"].ToString(),
  76. UnitPrice = Convert.ToDecimal(it["UnitPrice"]),
  77. Quantity = Convert.ToDecimal(it["Quantity"]),
  78. Total = Convert.ToDecimal(it["UnitPrice"]) * Convert.ToDecimal(it["Quantity"]),
  79. })
  80. .Where(it => it.OrderID == order.OrderID)
  81. .OrderBy(it => it.ProductName).ToList();
  82.  
  83. // Finally, prep the integrated data source for the template:
  84. var data = new
  85. {
  86. o = order,
  87. ps = orderDetails,
  88. total = orderDetails.Sum(od_ => od_.Total),
  89. };
  90.  
  91. // Load the template DOCX, add the data source and process the template:
  92. var doc = new GcWordDocument();
  93. doc.Load(Path.Combine("Resources", "WordDocs", "InvoiceTemplate.docx"));
  94. doc.DataTemplate.DataSources.Add("ds", data);
  95. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  96.  
  97. // Done:
  98. return doc;
  99. }
  100.  
  101. // Generate a sample email address from other data:
  102. private string GetEmail(string firstName, string lastName, string companyName)
  103. {
  104. var x = new string(companyName.ToLower().Where(c_ => char.IsLetterOrDigit(c_)).ToArray());
  105. return $"{firstName.ToLower()}.{char.ToLower(lastName[0])}@{x}.com";
  106. }
  107.  
  108. public static List<string[]> GetSampleParamsList()
  109. {
  110. return new List<string[]>()
  111. {
  112. new string[] { "@data-templates/Order Invoice", "Generate the invoice for a random order fetched from a database", null },
  113. new string[] { "@use-data-tpl/Order Invoice", "Generate the invoice for a random order fetched from a database", null },
  114. };
  115. }
  116. }
  117. }
  118.