V6Client.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.Dxf;
  13. using PRSDesktop.Integrations.V6;
  14. namespace PRSDesktop;
  15. public class V6Client : MicrosoftSQLClient
  16. {
  17. public V6Settings Settings { get; private set; }
  18. public V6Client()
  19. {
  20. Client.Save(new V6Usage(),"");
  21. Settings = new GlobalConfiguration<V6Settings>().Load();
  22. }
  23. protected override string GetConnectionString() => Settings.AsConnectionString();
  24. public V6Project? GetProject(string? reference)
  25. {
  26. V6Project? project = null;
  27. if (V6Project.ParseReference(reference, out int number, out string variation))
  28. {
  29. var _query = CheckQuery(Settings.QuoteSQL, V6Project.SQL, number, variation);
  30. var _table = Query(_query,"quote");
  31. return _table.Rows.Count > 0
  32. ? DataRowToProject(_table.Rows[0])
  33. : null;
  34. }
  35. return project;
  36. }
  37. public IEnumerable<V6Project> GetProjects()
  38. {
  39. try
  40. {
  41. List<V6Project> _projects = new();
  42. if (!IsConnected)
  43. return _projects;
  44. var _quotes = Query(Settings.QuoteSQL,"quotes");
  45. foreach (DataRow _row in _quotes.Rows)
  46. _projects.Add(DataRowToProject(_row));
  47. return _projects;
  48. }
  49. catch (Exception e)
  50. {
  51. Console.WriteLine(e);
  52. throw;
  53. }
  54. }
  55. private V6Project DataRowToProject(DataRow row)
  56. {
  57. var _quote = new V6Project()
  58. {
  59. ID = GetInteger(row,nameof(V6Project.ID)),
  60. Revision = GetInteger(row, nameof(V6Project.Revision)),
  61. Number = GetInteger(row, nameof(V6Project.Number)),
  62. Variation = GetString(row,nameof(V6Project.Variation)),
  63. ClientID = GetString(row,nameof(V6Project.ClientID)),
  64. ClientName = GetString(row,nameof(V6Project.ClientName)),
  65. Title = GetString(row,nameof(V6Project.Title)),
  66. SellPrice = GetDouble(row,nameof(V6Project.SellPrice)),
  67. Street = GetString(row,nameof(V6Project.Street)),
  68. City = GetString(row,nameof(V6Project.City)),
  69. State = GetString(row,nameof(V6Project.State)),
  70. PostCode = GetString(row,nameof(V6Project.PostCode)),
  71. };
  72. return _quote;
  73. }
  74. private static string CheckQuery(string query, string fallback, int number, string variation, IEnumerable<int>? quoteitems = null)
  75. {
  76. string _basefilter = quoteitems == null
  77. ? $"q.quote_num = '{number}' and q.quote_num_suff = '{variation}' and q.quote_vers = (select max(quote_vers) from quote where quote_id = q.quote_id) "
  78. : $"qi.quote_item_id in ({string.Join(",",quoteitems)}) ";
  79. var result = string.IsNullOrWhiteSpace(query)
  80. ? fallback
  81. : query;
  82. return result.Replace("where ", $"where {_basefilter} and ", StringComparison.CurrentCultureIgnoreCase);
  83. }
  84. public List<V6Elevation> GetElevations(V6Project project)
  85. {
  86. List<V6Elevation> _result = new();
  87. try
  88. {
  89. var _query = CheckQuery(Settings.ElevationSQL, V6Elevation.SQL, project.Number, project.Variation);
  90. var _table = Query(_query, "items");
  91. _result.AddRange(_table.Rows.OfType<DataRow>().Select(DataRowToElevation));
  92. return _result;
  93. }
  94. catch (Exception e)
  95. {
  96. Console.WriteLine(e);
  97. throw;
  98. }
  99. }
  100. private V6Elevation DataRowToElevation(DataRow row)
  101. {
  102. var _result = new V6Elevation()
  103. {
  104. ID = GetInteger(row, nameof(V6Elevation.ID)),
  105. Description = GetString(row,nameof(V6Elevation.Description)),
  106. Quantity = (int)GetDouble(row,nameof(V6Elevation.Quantity))
  107. };
  108. return _result;
  109. }
  110. public V6Drawings? GetDrawingSet(V6Project project, int itemnumber)
  111. {
  112. try
  113. {
  114. var _query = CheckQuery(Settings.DrawingsSQL, V6Drawings.SQL, project.Number, project.Variation, [itemnumber]);
  115. var _table = Query(_query, "drawings");
  116. return _table.Rows.Count > 0
  117. ? DataRowToDrawings(_table.Rows[0])
  118. : null;
  119. }
  120. catch (Exception e)
  121. {
  122. Console.WriteLine(e);
  123. throw;
  124. }
  125. }
  126. private V6Drawings DataRowToDrawings(DataRow row)
  127. {
  128. return new V6Drawings()
  129. {
  130. BinaryData = GetBinary(row, nameof(V6Drawings.BinaryData))
  131. };
  132. }
  133. public V6BOM GetBOM(V6Project project, IEnumerable<int>? quoteitems)
  134. {
  135. var result = new V6BOM();
  136. var itemnumbers = quoteitems?.ToArray();
  137. result.Finishes = GetFinishes(project, itemnumbers);
  138. result.Profiles = GetProfiles(project, itemnumbers);
  139. result.Gaskets = GetGaskets(project, itemnumbers);
  140. result.Components = GetComponents(project, itemnumbers);
  141. result.Glass = GetGlass(project, itemnumbers);
  142. result.Labour = GetLabour(project, itemnumbers);
  143. return result;
  144. }
  145. public List<V6Labour> GetLabour(V6Project project, IEnumerable<int>? quoteitems = null)
  146. {
  147. var _result = new List<V6Labour>();
  148. try
  149. {
  150. string _query = CheckQuery(Settings.LabourSQL, V6Labour.SQL, project.Number, project.Variation, quoteitems);
  151. var _table = Query(_query,"labour");
  152. foreach (DataRow _row in _table.Rows)
  153. {
  154. var _labour = DataRowToLabour(_row);
  155. _result.Add(_labour);
  156. }
  157. return _result;
  158. }
  159. catch (Exception e)
  160. {
  161. Logger.Send(LogType.Error,"",e.Message);
  162. }
  163. return _result;
  164. }
  165. private V6Labour DataRowToLabour(DataRow row)
  166. {
  167. var _labour = new V6Labour();
  168. _labour.Code = GetString(row, nameof(V6Labour.Code));
  169. _labour.Description = GetString(row, nameof(V6Labour.Description));
  170. _labour.Quantity = GetDouble(row, nameof(V6Labour.Quantity));
  171. _labour.Cost = GetDouble(row, nameof(V6Labour.Cost));
  172. return _labour;
  173. }
  174. public List<V6Finish> GetFinishes(V6Project project, IEnumerable<int>? quoteitems = null)
  175. {
  176. var _result = new List<V6Finish>();
  177. try
  178. {
  179. string _query = CheckQuery(Settings.FinishSQL, V6Finish.SQL, project.Number, project.Variation, quoteitems);
  180. var _table = Query(_query,"finish");
  181. foreach (DataRow _row in _table.Rows)
  182. {
  183. var _finish = DataRowToFinish(_row);
  184. _result.Add(_finish);
  185. }
  186. }
  187. catch (Exception e)
  188. {
  189. Logger.Send(LogType.Error,"",e.Message);
  190. }
  191. return _result;
  192. }
  193. private V6Finish DataRowToFinish(DataRow row)
  194. {
  195. var _result = new V6Finish();
  196. _result.Code = GetString(row, nameof(V6Profile.Code));
  197. _result.Description = GetString(row, nameof(V6Profile.Description));
  198. return _result;
  199. }
  200. public List<V6Profile> GetProfiles(V6Project project, IEnumerable<int>? quoteitems = null)
  201. {
  202. var _result = new List<V6Profile>();
  203. try
  204. {
  205. string _query = CheckQuery(Settings.ProfileSQL, V6Profile.SQL, project.Number, project.Variation, quoteitems);
  206. var _table = Query(_query,"profile");
  207. foreach (DataRow _row in _table.Rows)
  208. {
  209. var _profile = DataRowToProfile(_row);
  210. _result.Add(_profile);
  211. }
  212. return _result;
  213. }
  214. catch (Exception e)
  215. {
  216. Logger.Send(LogType.Error,"",e.Message);
  217. }
  218. return _result;
  219. }
  220. private V6Profile DataRowToProfile(DataRow row)
  221. {
  222. var _result = new V6Profile();
  223. _result.Code = GetString(row, nameof(V6Profile.Code));
  224. _result.Description = GetString(row, nameof(V6Profile.Description));
  225. _result.Length = GetDouble(row, nameof(V6Profile.Length));
  226. _result.Quantity = Math.Ceiling(GetDouble(row,nameof(V6Profile.Quantity)) / (_result.Length.IsEffectivelyEqual(0.0) ? 1.0 : _result.Length));
  227. _result.Cost = GetDouble(row, nameof(V6Profile.Cost));
  228. _result.Finish = GetString(row, nameof(V6Profile.Finish));
  229. return _result;
  230. }
  231. public List<V6Gasket> GetGaskets(V6Project project, IEnumerable<int>? quoteitems = null)
  232. {
  233. var _result = new List<V6Gasket>();
  234. try
  235. {
  236. string _query = CheckQuery(Settings.GasketSQL, V6Gasket.SQL, project.Number, project.Variation, quoteitems);
  237. var _table = Query(_query,"gasket");
  238. foreach (DataRow _row in _table.Rows)
  239. {
  240. var _gasket = DataRowToGasket(_row);
  241. _result.Add(_gasket);
  242. }
  243. return _result;
  244. }
  245. catch (Exception e)
  246. {
  247. Logger.Send(LogType.Error,"",e.Message);
  248. }
  249. return _result;
  250. }
  251. private V6Gasket DataRowToGasket(DataRow row)
  252. {
  253. var _result = new V6Gasket();
  254. _result.Code = GetString(row, nameof(V6Gasket.Code));
  255. _result.Description = GetString(row, nameof(V6Gasket.Description));
  256. _result.Length = GetDouble(row, nameof(V6Gasket.Length));
  257. _result.Quantity = Math.Ceiling(GetDouble(row,nameof(V6Profile.Quantity)) / (_result.Length.IsEffectivelyEqual(0.0) ? 1.0 : _result.Length));
  258. _result.Cost = GetDouble(row, nameof(V6Profile.Cost));
  259. return _result;
  260. }
  261. public List<V6Component> GetComponents(V6Project project, IEnumerable<int>? quoteitems = null)
  262. {
  263. var _result = new List<V6Component>();
  264. try
  265. {
  266. string _query = CheckQuery(Settings.ComponentSQL, V6Component.SQL, project.Number, project.Variation, quoteitems);
  267. var _table = Query(_query, "sundries");
  268. foreach (DataRow _row in _table.Rows)
  269. {
  270. var _sundry = DataRowToComponent(_row);
  271. _result.Add(_sundry);
  272. }
  273. }
  274. catch (Exception e)
  275. {
  276. Logger.Send(LogType.Error, "", e.Message);
  277. }
  278. return _result;
  279. }
  280. private V6Component DataRowToComponent(DataRow row)
  281. {
  282. var _result = new V6Component();
  283. _result.Code = GetString(row, nameof(V6Component.Code));
  284. _result.Description = GetString(row, nameof(V6Component.Description));
  285. _result.PackSize = GetDouble(row, nameof(V6Component.PackSize));
  286. _result.Quantity = GetDouble(row, nameof(V6Component.Quantity));
  287. _result.Cost = GetDouble(row, nameof(V6Component.Cost));
  288. return _result;
  289. }
  290. public List<V6Glass> GetGlass(V6Project project, IEnumerable<int>? quoteitems = null)
  291. {
  292. var _result = new List<V6Glass>();
  293. try
  294. {
  295. string _query = CheckQuery(Settings.GlassSQL, V6Glass.SQL, project.Number, project.Variation, quoteitems);
  296. var _table = Query(_query,"glass");
  297. foreach (DataRow _row in _table.Rows)
  298. {
  299. _result.Add(DataRowToGlass(_row));
  300. }
  301. }
  302. catch (Exception e)
  303. {
  304. Logger.Send(LogType.Error, "", e.Message);
  305. }
  306. return _result;
  307. }
  308. private V6Glass DataRowToGlass(DataRow row)
  309. {
  310. return new V6Glass()
  311. {
  312. Code = GetString(row, nameof(V6Glass.Code)),
  313. Description = GetString(row, nameof(V6Glass.Description)),
  314. Treatment = GetString(row, nameof(V6Glass.Treatment)),
  315. Height = GetDouble(row, nameof(V6Glass.Height)),
  316. Width = GetDouble(row, nameof(V6Glass.Width)),
  317. Location = GetString(row, nameof(V6Glass.Location)),
  318. Quantity = GetDouble(row, nameof(V6Glass.Quantity)),
  319. Cost = GetDouble(row, nameof(V6Glass.Cost)),
  320. };
  321. }
  322. }