V6Client.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 System.Text.RegularExpressions;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Configuration;
  12. using InABox.Core;
  13. using InABox.Dxf;
  14. using PRSDesktop.Integrations.V6;
  15. namespace PRSDesktop;
  16. public class V6Client : MicrosoftSQLClient
  17. {
  18. public V6Settings Settings { get; private set; }
  19. public V6Client()
  20. {
  21. Client.Save(new V6Usage(),"");
  22. Settings = new GlobalConfiguration<V6Settings>().Load();
  23. }
  24. protected override string GetConnectionString() => Settings.AsConnectionString();
  25. public V6Project? GetProject(string? reference)
  26. {
  27. V6Project? project = null;
  28. if (V6Project.ParseReference(reference, out int number))
  29. {
  30. var _query = CheckQuoteQuery(V6Project.SQL, V6Project.SQL, number, "");
  31. var _table = Query(_query,"quote");
  32. return _table.Rows.Count > 0
  33. ? DataRowToProject(_table.Rows[0])
  34. : null;
  35. }
  36. return project;
  37. }
  38. public IEnumerable<V6Project> GetProjects()
  39. {
  40. List<V6Project> _projects = new();
  41. try
  42. {
  43. var _quotes = Query(V6Project.SQL,"quotes");
  44. if (!IsConnected)
  45. return _projects;
  46. foreach (DataRow _row in _quotes.Rows)
  47. _projects.Add(DataRowToProject(_row));
  48. }
  49. catch (Exception e)
  50. {
  51. Logger.Send(LogType.Error,"",$"{e.Message}n{e.StackTrace}");
  52. }
  53. return _projects;
  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. ClientID = GetString(row,nameof(V6Project.ClientID)),
  63. ClientName = GetString(row,nameof(V6Project.ClientName)),
  64. Title = GetString(row,nameof(V6Project.Title)),
  65. SellPrice = GetDouble(row,nameof(V6Project.SellPrice)),
  66. Street = GetString(row,nameof(V6Project.Street)),
  67. City = GetString(row,nameof(V6Project.City)),
  68. State = GetString(row,nameof(V6Project.State)),
  69. PostCode = GetString(row,nameof(V6Project.PostCode)),
  70. };
  71. return _quote;
  72. }
  73. private static string CheckQuoteQuery(string query, string fallback, int number, string variation)
  74. {
  75. string _basefilter = $"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) ";
  76. var result = string.IsNullOrWhiteSpace(query)
  77. ? fallback
  78. : query;
  79. result = Regex.Replace(result, @"1\s*=\s*1", _basefilter);
  80. return result;
  81. }
  82. private static string CheckItemQuery(string query, string fallback, IEnumerable<int> quoteitems)
  83. {
  84. string _basefilter = $"qi.quote_item_id in ({string.Join(",",quoteitems)}) ";
  85. var result = string.IsNullOrWhiteSpace(query)
  86. ? fallback
  87. : query;
  88. result = Regex.Replace(result, @"1\s*=\s*1", _basefilter);
  89. return result;
  90. //result = result.Replace("\n"," ").Replace("\r"," ").Replace("1=1", $"{_basefilter}", StringComparison.CurrentCultureIgnoreCase);
  91. //while (result.Contains(" "))
  92. // result = result.Replace(" ", " ");
  93. //return result;
  94. }
  95. public List<V6Variation> GetVariations(V6Project project)
  96. {
  97. List<V6Variation> _result = new()
  98. { new V6Variation() { ID = "", Description = "Main Project", SellPrice = project.SellPrice } };
  99. var _query = CheckQuoteQuery(V6Variation.SQL, V6Variation.SQL, project.Number, "");
  100. try
  101. {
  102. var _table = Query(_query, "items");
  103. _result.AddRange(_table.Rows.OfType<DataRow>().Select(DataRowToVariation));
  104. }
  105. catch (Exception e)
  106. {
  107. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  108. }
  109. return _result;
  110. }
  111. private V6Variation DataRowToVariation(DataRow row)
  112. {
  113. var _result = new V6Variation()
  114. {
  115. ID = GetString(row, nameof(V6Variation.ID)),
  116. Description = GetString(row,nameof(V6Variation.Description)),
  117. };
  118. return _result;
  119. }
  120. public List<V6Elevation> GetElevations(V6Project project, string variationid)
  121. {
  122. List<V6Elevation> _result = new();
  123. var _query = CheckQuoteQuery(V6Elevation.SQL, V6Elevation.SQL, project.Number, variationid);
  124. try
  125. {
  126. var _table = Query(_query, "items");
  127. _result.AddRange(_table.Rows.OfType<DataRow>().Select(DataRowToElevation));
  128. }
  129. catch (Exception e)
  130. {
  131. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  132. }
  133. return _result;
  134. }
  135. private V6Elevation DataRowToElevation(DataRow row)
  136. {
  137. var _result = new V6Elevation()
  138. {
  139. ID = GetInteger(row, nameof(V6Elevation.ID)),
  140. Description = GetString(row,nameof(V6Elevation.Description)),
  141. Quantity = (int)GetDouble(row,nameof(V6Elevation.Quantity)),
  142. Drawings = GetInteger(row,nameof(V6Elevation.Drawings))
  143. };
  144. return _result;
  145. }
  146. public V6Drawings? GetDrawingSet(V6Project project, int itemnumber)
  147. {
  148. var _query = CheckItemQuery(V6Drawings.SQL, V6Drawings.SQL, [itemnumber]);
  149. try
  150. {
  151. var _table = Query(_query, "drawings");
  152. return _table.Rows.Count > 0
  153. ? DataRowToDrawings(_table.Rows[0])
  154. : null;
  155. }
  156. catch (Exception e)
  157. {
  158. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  159. }
  160. return new V6Drawings();
  161. }
  162. private V6Drawings DataRowToDrawings(DataRow row)
  163. {
  164. return new V6Drawings()
  165. {
  166. BinaryData = GetBinary(row, nameof(V6Drawings.BinaryData))
  167. };
  168. }
  169. public V6BOM GetBOM(V6Project project, int[] quoteitems)
  170. {
  171. var result = new V6BOM();
  172. result.Finishes = GetFinishes(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  173. result.Profiles = GetProfiles(project, quoteitems);
  174. result.Gaskets = GetGaskets(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  175. result.Components = GetComponents(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  176. result.Glass = GetGlass(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  177. result.Labour = GetLabour(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  178. return result;
  179. }
  180. public V6BOM GetBOM(V6Project project, string variation)
  181. {
  182. var result = new V6BOM();
  183. result.Finishes = GetFinishes(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  184. result.Profiles = GetProfiles(project, variation);
  185. result.Gaskets = GetGaskets(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  186. result.Components = GetComponents(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  187. result.Glass = GetGlass(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  188. result.Labour = GetLabour(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  189. return result;
  190. }
  191. public List<V6Labour> GetLabour(V6Project project, Func<string,string,string> getSql)
  192. {
  193. var _result = new List<V6Labour>();
  194. string _query = getSql(Settings.LabourSQL, V6Labour.SQL);
  195. try
  196. {
  197. var _table = Query(_query,"labour");
  198. foreach (DataRow _row in _table.Rows)
  199. {
  200. var _labour = DataRowToLabour(_row);
  201. _result.Add(_labour);
  202. }
  203. return _result;
  204. }
  205. catch (Exception e)
  206. {
  207. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  208. }
  209. return _result;
  210. }
  211. private V6Labour DataRowToLabour(DataRow row)
  212. {
  213. var _labour = new V6Labour();
  214. _labour.Code = GetString(row, nameof(V6Labour.Code));
  215. _labour.Description = GetString(row, nameof(V6Labour.Description));
  216. _labour.Quantity = GetDouble(row, nameof(V6Labour.Quantity));
  217. _labour.Cost = GetDouble(row, nameof(V6Labour.Cost));
  218. return _labour;
  219. }
  220. public List<V6Finish> GetFinishes(V6Project project, Func<string,string,string> getSql)
  221. {
  222. var _result = new List<V6Finish>();
  223. string _query = getSql(V6Finish.SQL, V6Finish.SQL);
  224. try
  225. {
  226. var _table = Query(_query,"finish");
  227. foreach (DataRow _row in _table.Rows)
  228. {
  229. var _finish = DataRowToFinish(_row);
  230. _result.Add(_finish);
  231. }
  232. }
  233. catch (Exception e)
  234. {
  235. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  236. }
  237. return _result;
  238. }
  239. private V6Finish DataRowToFinish(DataRow row)
  240. {
  241. var _result = new V6Finish();
  242. _result.Code = GetString(row, nameof(V6Profile.Code));
  243. _result.Description = GetString(row, nameof(V6Profile.Description));
  244. return _result;
  245. }
  246. public List<V6Profile> GetProfiles(V6Project project, string variation)
  247. {
  248. var _result = new List<V6Profile>();
  249. string _query = CheckQuoteQuery(Settings.BOMProfilesSQL, V6Profile.BillOfMaterialsSQL, project.Number, variation);
  250. try
  251. {
  252. var _table = Query(_query,"profile");
  253. foreach (DataRow _row in _table.Rows)
  254. {
  255. var _profile = DataRowToProfile(_row);
  256. _result.Add(_profile);
  257. }
  258. return _result;
  259. }
  260. catch (Exception e)
  261. {
  262. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  263. }
  264. return _result;
  265. }
  266. public List<V6Profile> GetProfiles(V6Project project, int[] quoteitems)
  267. {
  268. var _result = new List<V6Profile>();
  269. string _query = CheckItemQuery(Settings.DesignProfilesSQL, V6Profile.DesignSQL, quoteitems);
  270. try
  271. {
  272. var _table = Query(_query,"profile");
  273. foreach (DataRow _row in _table.Rows)
  274. {
  275. var _profile = DataRowToProfile(_row);
  276. _result.Add(_profile);
  277. }
  278. return _result;
  279. }
  280. catch (Exception e)
  281. {
  282. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  283. }
  284. return _result;
  285. }
  286. private V6Profile DataRowToProfile(DataRow row)
  287. {
  288. var _result = new V6Profile();
  289. _result.Code = GetString(row, nameof(V6Profile.Code));
  290. _result.Description = GetString(row, nameof(V6Profile.Description));
  291. _result.Length = GetDouble(row, nameof(V6Profile.Length)) * GetScale(Settings.ProfileMeasurement);
  292. _result.Quantity = Math.Ceiling(GetDouble(row,nameof(V6Profile.Quantity)));
  293. _result.Cost = GetDouble(row, nameof(V6Profile.Cost));
  294. _result.Finish = GetString(row, nameof(V6Profile.Finish));
  295. return _result;
  296. }
  297. public List<V6Gasket> GetGaskets(V6Project project, Func<string,string,string> getSql)
  298. {
  299. var _result = new List<V6Gasket>();
  300. string _query = getSql(Settings.GasketSQL, V6Gasket.SQL);
  301. try
  302. {
  303. var _table = Query(_query,"gasket");
  304. foreach (DataRow _row in _table.Rows)
  305. {
  306. var _gasket = DataRowToGasket(_row);
  307. _result.Add(_gasket);
  308. }
  309. return _result;
  310. }
  311. catch (Exception e)
  312. {
  313. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  314. }
  315. return _result;
  316. }
  317. private V6Gasket DataRowToGasket(DataRow row)
  318. {
  319. var _result = new V6Gasket();
  320. _result.Code = GetString(row, nameof(V6Gasket.Code));
  321. _result.Description = GetString(row, nameof(V6Gasket.Description));
  322. _result.Quantity = GetDouble(row, nameof(V6Gasket.Quantity)) * GetScale(Settings.GasketMeasurement);
  323. _result.Cost = GetDouble(row, nameof(V6Profile.Cost)) / GetScale(Settings.GasketMeasurement);
  324. return _result;
  325. }
  326. public List<V6Component> GetComponents(V6Project project, Func<string,string,string> getSql)
  327. {
  328. var _result = new List<V6Component>();
  329. string _query = getSql(Settings.ComponentSQL, V6Component.SQL);
  330. try
  331. {
  332. var _table = Query(_query, "sundries");
  333. foreach (DataRow _row in _table.Rows)
  334. {
  335. var _sundry = DataRowToComponent(_row);
  336. _result.Add(_sundry);
  337. }
  338. }
  339. catch (Exception e)
  340. {
  341. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  342. }
  343. return _result;
  344. }
  345. private V6Component DataRowToComponent(DataRow row)
  346. {
  347. var _result = new V6Component();
  348. _result.Code = GetString(row, nameof(V6Component.Code));
  349. _result.Description = GetString(row, nameof(V6Component.Description));
  350. _result.PackSize = GetDouble(row, nameof(V6Component.PackSize));
  351. _result.Quantity = GetDouble(row, nameof(V6Component.Quantity));
  352. _result.Cost = GetDouble(row, nameof(V6Component.Cost));
  353. return _result;
  354. }
  355. public List<V6Glass> GetGlass(V6Project project, Func<string,string,string> getSql)
  356. {
  357. var _result = new List<V6Glass>();
  358. string _query = getSql(Settings.GlassSQL, V6Glass.SQL);
  359. try
  360. {
  361. var _table = Query(_query,"glass");
  362. foreach (DataRow _row in _table.Rows)
  363. _result.Add(DataRowToGlass(_row));
  364. }
  365. catch (Exception e)
  366. {
  367. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  368. }
  369. return _result;
  370. }
  371. private V6Glass DataRowToGlass(DataRow row)
  372. {
  373. var result = new V6Glass();
  374. result.Code = GetString(row, nameof(V6Glass.Code));
  375. result.Description = GetString(row, nameof(V6Glass.Description));
  376. result.Treatment = GetString(row, nameof(V6Glass.Treatment));
  377. result.Height = GetDouble(row, nameof(V6Glass.Height)) * GetScale(Settings.ProfileMeasurement);
  378. result.Width = GetDouble(row, nameof(V6Glass.Width)) * GetScale(Settings.GlassMeasurement);
  379. result.Location = GetString(row, nameof(V6Glass.Location));
  380. result.Quantity = GetDouble(row, nameof(V6Glass.Quantity));
  381. result.Cost = GetDouble(row, nameof(V6Glass.Cost));
  382. return result;
  383. }
  384. private double GetScale(V6Measurement scale)
  385. {
  386. return scale == V6Measurement.Metres
  387. ? 0.0254
  388. : Settings.ProfileMeasurement == V6Measurement.Millimetres
  389. ? 25.4
  390. : 1.0;
  391. }
  392. }