V6Client.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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.Groups = GetGroups(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  193. result.Styles = GetStyles(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  194. //result.Finishes = GetFinishes(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  195. result.Profiles = GetProfiles(project, quoteitems);
  196. result.Gaskets = GetGaskets(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  197. result.Components = GetComponents(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  198. result.Glass = GetGlass(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  199. result.Labour = GetLabour(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  200. return result;
  201. }
  202. public V6BOM GetBOM(V6Project project, string variation)
  203. {
  204. var result = new V6BOM();
  205. //result.Groups = GetGroups(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  206. result.Styles = GetStyles(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  207. //result.Finishes = GetFinishes(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  208. result.Profiles = GetProfiles(project, variation);
  209. result.Gaskets = GetGaskets(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  210. result.Components = GetComponents(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  211. result.Glass = GetGlass(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  212. result.Labour = GetLabour(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  213. return result;
  214. }
  215. public List<V6Labour> GetLabour(V6Project project, Func<string,string,string> getSql)
  216. {
  217. var _result = new List<V6Labour>();
  218. string _query = getSql(Settings.LabourSQL, V6Labour.SQL);
  219. try
  220. {
  221. var _table = Query(_query,"labour");
  222. foreach (DataRow _row in _table.Rows)
  223. {
  224. var _labour = DataRowToLabour(_row);
  225. _result.Add(_labour);
  226. }
  227. return _result;
  228. }
  229. catch (Exception e)
  230. {
  231. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  232. }
  233. return _result;
  234. }
  235. private V6Labour DataRowToLabour(DataRow row)
  236. {
  237. var _labour = new V6Labour();
  238. _labour.Code = GetString(row, nameof(V6Labour.Code));
  239. _labour.Description = GetString(row, nameof(V6Labour.Description));
  240. _labour.Quantity = GetDouble(row, nameof(V6Labour.Quantity));
  241. _labour.Cost = GetDouble(row, nameof(V6Labour.Cost));
  242. return _labour;
  243. }
  244. public List<V6Group> GetGroups(V6Project project, Func<string,string,string> getSql)
  245. {
  246. var _result = new List<V6Group>();
  247. string _query = getSql(V6Group.SQL, V6Group.SQL);
  248. try
  249. {
  250. var _table = Query(_query,"group");
  251. foreach (DataRow _row in _table.Rows)
  252. {
  253. var _group = DataRowToGroup(_row);
  254. _result.Add(_group);
  255. }
  256. }
  257. catch (Exception e)
  258. {
  259. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  260. }
  261. return _result;
  262. }
  263. private V6Group DataRowToGroup(DataRow row)
  264. {
  265. var _result = new V6Group();
  266. _result.Code = GetString(row, nameof(V6Group.Code));
  267. _result.Description = GetString(row, nameof(V6Group.Description));
  268. return _result;
  269. }
  270. public List<V6Style> GetStyles(V6Project project, Func<string,string,string> getSql)
  271. {
  272. var _result = new List<V6Style>();
  273. string _query = getSql(V6Style.SQL, V6Style.SQL);
  274. try
  275. {
  276. var _table = Query(_query,"style");
  277. foreach (DataRow _row in _table.Rows)
  278. {
  279. var _finish = DataRowToStyle(_row);
  280. _result.Add(_finish);
  281. }
  282. }
  283. catch (Exception e)
  284. {
  285. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  286. }
  287. return _result;
  288. }
  289. private V6Style DataRowToStyle(DataRow row)
  290. {
  291. var _result = new V6Style();
  292. _result.Code = GetString(row, nameof(V6Style.Code));
  293. _result.Description = GetString(row, nameof(V6Style.Description));
  294. _result.Cost = GetDouble(row, nameof(V6Style.Cost));
  295. return _result;
  296. }
  297. public List<V6Profile> GetProfiles(V6Project project, string variation)
  298. {
  299. var _result = new List<V6Profile>();
  300. string _query = CheckQuoteQuery(Settings.BOMProfilesSQL, V6Profile.BillOfMaterialsSQL, project.Number, variation);
  301. try
  302. {
  303. var _table = Query(_query,"profile");
  304. foreach (DataRow _row in _table.Rows)
  305. {
  306. var _profile = DataRowToProfile(_row);
  307. _result.Add(_profile);
  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. public List<V6Profile> GetProfiles(V6Project project, int[] quoteitems)
  318. {
  319. var _result = new List<V6Profile>();
  320. string _query = CheckItemQuery(Settings.DesignProfilesSQL, V6Profile.DesignSQL, quoteitems);
  321. try
  322. {
  323. var _table = Query(_query,"profile");
  324. foreach (DataRow _row in _table.Rows)
  325. {
  326. var _profile = DataRowToProfile(_row);
  327. _result.Add(_profile);
  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 V6Profile DataRowToProfile(DataRow row)
  338. {
  339. var _result = new V6Profile();
  340. _result.Code = GetString(row, nameof(V6Profile.Code));
  341. _result.Description = GetString(row, nameof(V6Profile.Description));
  342. _result.Length = GetDouble(row, nameof(V6Profile.Length)) * GetScale(Settings.ProfileMeasurement);
  343. _result.Quantity = Math.Ceiling(GetDouble(row,nameof(V6Profile.Quantity)));
  344. _result.Cost = GetDouble(row, nameof(V6Profile.Cost));
  345. _result.Finish = GetString(row, nameof(V6Profile.Finish));
  346. return _result;
  347. }
  348. public List<V6Gasket> GetGaskets(V6Project project, Func<string,string,string> getSql)
  349. {
  350. var _result = new List<V6Gasket>();
  351. string _query = getSql(Settings.GasketSQL, V6Gasket.SQL);
  352. try
  353. {
  354. var _table = Query(_query,"gasket");
  355. foreach (DataRow _row in _table.Rows)
  356. {
  357. var _gasket = DataRowToGasket(_row);
  358. _result.Add(_gasket);
  359. }
  360. return _result;
  361. }
  362. catch (Exception e)
  363. {
  364. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  365. }
  366. return _result;
  367. }
  368. private V6Gasket DataRowToGasket(DataRow row)
  369. {
  370. var _result = new V6Gasket();
  371. _result.Code = GetString(row, nameof(V6Gasket.Code));
  372. _result.Description = GetString(row, nameof(V6Gasket.Description));
  373. _result.Quantity = GetDouble(row, nameof(V6Gasket.Quantity)) * GetScale(Settings.GasketMeasurement);
  374. _result.Cost = GetDouble(row, nameof(V6Profile.Cost)) / GetScale(Settings.GasketMeasurement);
  375. return _result;
  376. }
  377. public List<V6Component> GetComponents(V6Project project, Func<string,string,string> getSql)
  378. {
  379. var _result = new List<V6Component>();
  380. string _query = getSql(Settings.ComponentSQL, V6Component.SQL);
  381. try
  382. {
  383. var _table = Query(_query, "sundries");
  384. foreach (DataRow _row in _table.Rows)
  385. {
  386. var _sundry = DataRowToComponent(_row);
  387. _result.Add(_sundry);
  388. }
  389. }
  390. catch (Exception e)
  391. {
  392. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  393. }
  394. return _result;
  395. }
  396. private V6Component DataRowToComponent(DataRow row)
  397. {
  398. var _result = new V6Component();
  399. _result.Code = GetString(row, nameof(V6Component.Code));
  400. _result.Description = GetString(row, nameof(V6Component.Description));
  401. _result.PackSize = GetDouble(row, nameof(V6Component.PackSize));
  402. _result.Quantity = GetDouble(row, nameof(V6Component.Quantity));
  403. _result.Cost = GetDouble(row, nameof(V6Component.Cost));
  404. return _result;
  405. }
  406. public List<V6Glass> GetGlass(V6Project project, Func<string,string,string> getSql)
  407. {
  408. var _result = new List<V6Glass>();
  409. string _query = getSql(Settings.GlassSQL, V6Glass.SQL);
  410. try
  411. {
  412. var _table = Query(_query,"glass");
  413. foreach (DataRow _row in _table.Rows)
  414. _result.Add(DataRowToGlass(_row));
  415. }
  416. catch (Exception e)
  417. {
  418. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  419. }
  420. return _result;
  421. }
  422. private V6Glass DataRowToGlass(DataRow row)
  423. {
  424. var result = new V6Glass();
  425. result.Code = GetString(row, nameof(V6Glass.Code));
  426. result.Description = GetString(row, nameof(V6Glass.Description));
  427. result.Treatment = GetString(row, nameof(V6Glass.Treatment));
  428. result.Height = GetDouble(row, nameof(V6Glass.Height)) * GetScale(Settings.ProfileMeasurement);
  429. result.Width = GetDouble(row, nameof(V6Glass.Width)) * GetScale(Settings.GlassMeasurement);
  430. result.Location = GetString(row, nameof(V6Glass.Location));
  431. result.Quantity = GetDouble(row, nameof(V6Glass.Quantity));
  432. result.Cost = GetDouble(row, nameof(V6Glass.Cost));
  433. return result;
  434. }
  435. private double GetScale(V6Measurement scale)
  436. {
  437. return scale == V6Measurement.Metres
  438. ? 0.0254
  439. : Settings.ProfileMeasurement == V6Measurement.Millimetres
  440. ? 25.4
  441. : 1.0;
  442. }
  443. }