V6Client.cs 16 KB

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