PageRenderTime 63ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Samba.Services/DataCreationService.cs

https://code.google.com/p/sambapos/
C# | 302 lines | 249 code | 53 blank | 0 comment | 20 complexity | fbd0970fa4846183bb93a9ac570542a9 MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using Samba.Domain.Foundation;
  8. using Samba.Domain.Models.Menus;
  9. using Samba.Domain.Models.Settings;
  10. using Samba.Domain.Models.Tables;
  11. using Samba.Domain.Models.Tickets;
  12. using Samba.Domain.Models.Users;
  13. using Samba.Infrastructure.Data;
  14. using Samba.Infrastructure.Settings;
  15. using Samba.Localization.Properties;
  16. using Samba.Persistance.Data;
  17. namespace Samba.Services
  18. {
  19. public class DataCreationService
  20. {
  21. private readonly IWorkspace _workspace;
  22. public DataCreationService()
  23. {
  24. _workspace = WorkspaceFactory.Create();
  25. }
  26. private bool ShouldCreateData()
  27. {
  28. return _workspace.Count<User>() == 0;
  29. }
  30. public void CreateData()
  31. {
  32. CreateDefaultCurrenciesIfNeeded();
  33. if (!ShouldCreateData()) return;
  34. var screen = new ScreenMenu();
  35. _workspace.Add(screen);
  36. var ticketNumerator = new Numerator { Name = Resources.TicketNumerator };
  37. _workspace.Add(ticketNumerator);
  38. var orderNumerator = new Numerator { Name = Resources.OrderNumerator };
  39. _workspace.Add(orderNumerator);
  40. _workspace.CommitChanges();
  41. var department = new Department
  42. {
  43. Name = Resources.Restaurant,
  44. ScreenMenuId = screen.Id,
  45. TicketNumerator = ticketNumerator,
  46. OrderNumerator = orderNumerator,
  47. IsAlaCarte = true
  48. };
  49. _workspace.Add(department);
  50. var role = new UserRole("Admin") { IsAdmin = true, DepartmentId = 1 };
  51. _workspace.Add(role);
  52. var u = new User("Administrator", "1234") { UserRole = role };
  53. _workspace.Add(u);
  54. var ticketTemplate = new PrinterTemplate();
  55. ticketTemplate.Name = Resources.TicketTemplate;
  56. ticketTemplate.HeaderTemplate = Resources.TicketTemplateHeaderValue;
  57. ticketTemplate.LineTemplate = Resources.TicketTempleteLineTemplateValue;
  58. ticketTemplate.GiftLineTemplate = Resources.TicketTemplateGiftedLineTemplateValue;
  59. ticketTemplate.FooterTemplate = Resources.TicketTemplateFooterValue;
  60. var kitchenTemplate = new PrinterTemplate();
  61. kitchenTemplate.Name = Resources.KitchenOrderTemplate;
  62. kitchenTemplate.HeaderTemplate = Resources.KitchenTemplateHeaderValue;
  63. kitchenTemplate.LineTemplate = Resources.KitchenTemplateLineTemplateValue;
  64. kitchenTemplate.GiftLineTemplate = Resources.KitchenTemplateLineTemplateValue;
  65. kitchenTemplate.VoidedLineTemplate = Resources.KitchenTemplateVoidedLineTemplateValue;
  66. kitchenTemplate.FooterTemplate = "<F>-";
  67. var invoiceTemplate = new PrinterTemplate();
  68. invoiceTemplate.Name = Resources.InvoicePrinterTemplate;
  69. invoiceTemplate.HeaderTemplate = Resources.InvoiceTemplateHeaderValue;
  70. invoiceTemplate.LineTemplate = Resources.InvoiceTemplateLineTemplateValue;
  71. invoiceTemplate.VoidedLineTemplate = "";
  72. invoiceTemplate.FooterTemplate = "<F>-";
  73. _workspace.Add(ticketTemplate);
  74. _workspace.Add(kitchenTemplate);
  75. _workspace.Add(invoiceTemplate);
  76. var printer1 = new Printer { Name = Resources.TicketPrinter };
  77. var printer2 = new Printer { Name = Resources.KitchenPrinter };
  78. var printer3 = new Printer { Name = Resources.InvoicePrinter };
  79. _workspace.Add(printer1);
  80. _workspace.Add(printer2);
  81. _workspace.Add(printer3);
  82. var t = new Terminal
  83. {
  84. IsDefault = true,
  85. Name = Resources.Server,
  86. SlipReportPrinter = printer1,
  87. };
  88. var pm1 = new PrinterMap { Printer = printer1, PrinterTemplate = ticketTemplate };
  89. _workspace.Add(pm1);
  90. var pj1 = new PrintJob
  91. {
  92. Name = Resources.PrintBill,
  93. ButtonText = Resources.PrintBill,
  94. LocksTicket = true,
  95. Order = 0,
  96. UseFromPaymentScreen = true,
  97. UseFromTerminal = true,
  98. UseFromPos = true,
  99. WhatToPrint = (int)WhatToPrintTypes.Everything,
  100. WhenToPrint = (int)WhenToPrintTypes.Manual
  101. };
  102. pj1.PrinterMaps.Add(pm1);
  103. _workspace.Add(pj1);
  104. var pm2 = new PrinterMap { Printer = printer2, PrinterTemplate = kitchenTemplate };
  105. var pj2 = new PrintJob
  106. {
  107. Name = Resources.PrintOrdersToKitchenPrinter,
  108. ButtonText = "",
  109. Order = 1,
  110. WhatToPrint = (int)WhatToPrintTypes.NewLines,
  111. WhenToPrint = (int)WhenToPrintTypes.NewLinesAdded
  112. };
  113. pj2.PrinterMaps.Add(pm2);
  114. _workspace.Add(pj2);
  115. t.PrintJobs.Add(pj1);
  116. t.PrintJobs.Add(pj2);
  117. _workspace.Add(t);
  118. ImportMenus(screen);
  119. ImportTables(department);
  120. _workspace.CommitChanges();
  121. _workspace.Dispose();
  122. }
  123. private void ImportTables(Department department)
  124. {
  125. var fileName = string.Format("{0}/Imports/table{1}.txt", LocalSettings.AppPath, "_" + LocalSettings.CurrentLanguage);
  126. if (!File.Exists(fileName))
  127. fileName = string.Format("{0}/Imports/table.txt", LocalSettings.AppPath);
  128. if (!File.Exists(fileName)) return;
  129. var lines = File.ReadAllLines(fileName);
  130. var items = BatchCreateTables(lines, _workspace);
  131. _workspace.CommitChanges();
  132. var screen = new TableScreen { Name = Resources.AllTables, ColumnCount = 8 };
  133. _workspace.Add(screen);
  134. foreach (var table in items)
  135. screen.AddScreenItem(table);
  136. _workspace.CommitChanges();
  137. department.TableScreenId = screen.Id;
  138. }
  139. private void ImportMenus(ScreenMenu screenMenu)
  140. {
  141. var fileName = string.Format("{0}/Imports/menu{1}.txt", LocalSettings.AppPath, "_" + LocalSettings.CurrentLanguage);
  142. if (!File.Exists(fileName))
  143. fileName = string.Format("{0}/Imports/menu.txt", LocalSettings.AppPath);
  144. if (!File.Exists(fileName)) return;
  145. var lines = File.ReadAllLines(fileName, Encoding.UTF8);
  146. var items = BatchCreateMenuItems(lines, _workspace);
  147. _workspace.CommitChanges();
  148. var groupCodes = items.Select(x => x.GroupCode).Distinct().Where(x => !string.IsNullOrEmpty(x));
  149. foreach (var groupCode in groupCodes)
  150. {
  151. var code = groupCode;
  152. screenMenu.AddCategory(code);
  153. screenMenu.AddItemsToCategory(groupCode, items.Where(x => x.GroupCode == code).ToList());
  154. }
  155. }
  156. public IEnumerable<Table> BatchCreateTables(string[] values, IWorkspace workspace)
  157. {
  158. IList<Table> result = new List<Table>();
  159. if (values.Length > 0)
  160. {
  161. var currentCategory = Resources.Common;
  162. foreach (var value in values)
  163. {
  164. if (value.StartsWith("#"))
  165. {
  166. currentCategory = value.Trim('#', ' ');
  167. }
  168. else
  169. {
  170. var tableName = value;
  171. var count = Dao.Count<Table>(y => y.Name == tableName.Trim());
  172. if (count == 0)
  173. {
  174. var table = new Table { Name = value.Trim(), Category = currentCategory };
  175. if (result.Count(x => x.Name.ToLower() == table.Name.ToLower()) == 0)
  176. {
  177. result.Add(table);
  178. workspace.Add(table);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. return result;
  185. }
  186. public IEnumerable<MenuItem> BatchCreateMenuItems(string[] values, IWorkspace workspace)
  187. {
  188. var ds = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
  189. IList<MenuItem> result = new List<MenuItem>();
  190. if (values.Length > 0)
  191. {
  192. var currentCategory = Resources.Common;
  193. foreach (var item in values)
  194. {
  195. if (item.StartsWith("#"))
  196. {
  197. currentCategory = item.Trim('#', ' ');
  198. }
  199. else if (item.Contains(" "))
  200. {
  201. IList<string> parts = new List<string>(item.Split(' '));
  202. var price = ConvertToDecimal(parts[parts.Count - 1], ds);
  203. parts.RemoveAt(parts.Count - 1);
  204. var itemName = string.Join(" ", parts.ToArray());
  205. var mi = MenuItem.Create();
  206. mi.Name = itemName;
  207. mi.Portions[0].Price.Amount = price;
  208. mi.GroupCode = currentCategory;
  209. workspace.Add(mi);
  210. workspace.Add(mi.Portions[0]);
  211. result.Add(mi);
  212. }
  213. }
  214. }
  215. return result;
  216. }
  217. public IEnumerable<Reason> BatchCreateReasons(string[] values, int reasonType, IWorkspace workspace)
  218. {
  219. IList<Reason> result = new List<Reason>();
  220. if (values.Length > 0)
  221. {
  222. foreach (var reason in values.Select(value => new Reason { Name = value, ReasonType = reasonType }))
  223. {
  224. workspace.Add(reason);
  225. result.Add(reason);
  226. }
  227. }
  228. return result;
  229. }
  230. private static decimal ConvertToDecimal(string priceStr, string decimalSeperator)
  231. {
  232. try
  233. {
  234. priceStr = priceStr.Replace(".", decimalSeperator);
  235. priceStr = priceStr.Replace(",", decimalSeperator);
  236. var price = Convert.ToDecimal(priceStr);
  237. return price;
  238. }
  239. catch (Exception)
  240. {
  241. return 0;
  242. }
  243. }
  244. private static void CreateDefaultCurrenciesIfNeeded()
  245. {
  246. LocalSettings.DefaultCurrencyFormat = "C";
  247. }
  248. }
  249. }