V6Client.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 CheckItemQuery(string query, string fallback, IEnumerable<int> quoteitems)
  93. {
  94. string _basefilter = $"qi.quote_item_id in ({string.Join(",",quoteitems)}) ";
  95. var result = string.IsNullOrWhiteSpace(query)
  96. ? fallback
  97. : query;
  98. result = Regex.Replace(result, @"1\s*=\s*1", _basefilter);
  99. return result;
  100. //result = result.Replace("\n"," ").Replace("\r"," ").Replace("1=1", $"{_basefilter}", StringComparison.CurrentCultureIgnoreCase);
  101. //while (result.Contains(" "))
  102. // result = result.Replace(" ", " ");
  103. //return result;
  104. }
  105. public List<V6Variation> GetVariations(V6Project project)
  106. {
  107. List<V6Variation> _result = new()
  108. { new V6Variation() { ID = "", Description = "Main Project", SellPrice = project.SellPrice } };
  109. var _query = CheckQuoteQuery(V6Variation.SQL, V6Variation.SQL, project.Number, "");
  110. try
  111. {
  112. var _table = Query(_query, "items");
  113. _result.AddRange(_table.Rows.OfType<DataRow>().Select(DataRowToVariation));
  114. }
  115. catch (Exception e)
  116. {
  117. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  118. }
  119. return _result;
  120. }
  121. private V6Variation DataRowToVariation(DataRow row)
  122. {
  123. var _result = new V6Variation()
  124. {
  125. ID = GetString(row, nameof(V6Variation.ID)),
  126. Description = GetString(row,nameof(V6Variation.Description)),
  127. };
  128. return _result;
  129. }
  130. public List<V6Elevation> GetElevations(V6Project project, string variationid)
  131. {
  132. List<V6Elevation> _result = new();
  133. var _query = CheckQuoteQuery(V6Elevation.SQL, V6Elevation.SQL, project.Number, variationid);
  134. try
  135. {
  136. var _table = Query(_query, "items");
  137. _result.AddRange(_table.Rows.OfType<DataRow>().Select(DataRowToElevation));
  138. }
  139. catch (Exception e)
  140. {
  141. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  142. }
  143. return _result;
  144. }
  145. private V6Elevation DataRowToElevation(DataRow row)
  146. {
  147. var _result = new V6Elevation()
  148. {
  149. ID = GetInteger(row, nameof(V6Elevation.ID)),
  150. Description = GetString(row,nameof(V6Elevation.Description)),
  151. Quantity = (int)GetDouble(row,nameof(V6Elevation.Quantity)),
  152. Drawings = GetInteger(row,nameof(V6Elevation.Drawings))
  153. };
  154. return _result;
  155. }
  156. public V6Drawings? GetDrawingSet(V6Project project, int itemnumber)
  157. {
  158. var _query = CheckItemQuery(V6Drawings.SQL, V6Drawings.SQL, [itemnumber]);
  159. try
  160. {
  161. var _table = Query(_query, "drawings");
  162. return _table.Rows.Count > 0
  163. ? DataRowToDrawings(_table.Rows[0])
  164. : null;
  165. }
  166. catch (Exception e)
  167. {
  168. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  169. }
  170. return new V6Drawings();
  171. }
  172. private V6Drawings DataRowToDrawings(DataRow row)
  173. {
  174. return new V6Drawings()
  175. {
  176. BinaryData = GetBinary(row, nameof(V6Drawings.BinaryData))
  177. };
  178. }
  179. public V6BOM GetBOM(V6Project project, int[] quoteitems)
  180. {
  181. var result = new V6BOM();
  182. result.Finishes = GetFinishes(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  183. result.Profiles = GetProfiles(project, quoteitems);
  184. result.Gaskets = GetGaskets(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  185. result.Components = GetComponents(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  186. result.Glass = GetGlass(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  187. result.Labour = GetLabour(project, (q,f) => CheckItemQuery(q, f, quoteitems));
  188. return result;
  189. }
  190. public V6BOM GetBOM(V6Project project, string variation)
  191. {
  192. var result = new V6BOM();
  193. result.Finishes = GetFinishes(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  194. result.Profiles = GetProfiles(project, variation);
  195. result.Gaskets = GetGaskets(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  196. result.Components = GetComponents(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  197. result.Glass = GetGlass(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  198. result.Labour = GetLabour(project, (q,f) => CheckQuoteQuery(q,f,project.Number, variation));
  199. return result;
  200. }
  201. public List<V6Labour> GetLabour(V6Project project, Func<string,string,string> getSql)
  202. {
  203. var _result = new List<V6Labour>();
  204. string _query = getSql(Settings.LabourSQL, V6Labour.SQL);
  205. try
  206. {
  207. var _table = Query(_query,"labour");
  208. foreach (DataRow _row in _table.Rows)
  209. {
  210. var _labour = DataRowToLabour(_row);
  211. _result.Add(_labour);
  212. }
  213. return _result;
  214. }
  215. catch (Exception e)
  216. {
  217. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  218. }
  219. return _result;
  220. }
  221. private V6Labour DataRowToLabour(DataRow row)
  222. {
  223. var _labour = new V6Labour();
  224. _labour.Code = GetString(row, nameof(V6Labour.Code));
  225. _labour.Description = GetString(row, nameof(V6Labour.Description));
  226. _labour.Quantity = GetDouble(row, nameof(V6Labour.Quantity));
  227. _labour.Cost = GetDouble(row, nameof(V6Labour.Cost));
  228. return _labour;
  229. }
  230. public List<V6Finish> GetFinishes(V6Project project, Func<string,string,string> getSql)
  231. {
  232. var _result = new List<V6Finish>();
  233. string _query = getSql(V6Finish.SQL, V6Finish.SQL);
  234. try
  235. {
  236. var _table = Query(_query,"finish");
  237. foreach (DataRow _row in _table.Rows)
  238. {
  239. var _finish = DataRowToFinish(_row);
  240. _result.Add(_finish);
  241. }
  242. }
  243. catch (Exception e)
  244. {
  245. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  246. }
  247. return _result;
  248. }
  249. private V6Finish DataRowToFinish(DataRow row)
  250. {
  251. var _result = new V6Finish();
  252. _result.Code = GetString(row, nameof(V6Profile.Code));
  253. _result.Description = GetString(row, nameof(V6Profile.Description));
  254. return _result;
  255. }
  256. public List<V6Profile> GetProfiles(V6Project project, string variation)
  257. {
  258. var _result = new List<V6Profile>();
  259. string _query = CheckQuoteQuery(Settings.BOMProfilesSQL, V6Profile.BillOfMaterialsSQL, project.Number, variation);
  260. try
  261. {
  262. var _table = Query(_query,"profile");
  263. foreach (DataRow _row in _table.Rows)
  264. {
  265. var _profile = DataRowToProfile(_row);
  266. _result.Add(_profile);
  267. }
  268. return _result;
  269. }
  270. catch (Exception e)
  271. {
  272. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  273. }
  274. return _result;
  275. }
  276. public List<V6Profile> GetProfiles(V6Project project, int[] quoteitems)
  277. {
  278. var _result = new List<V6Profile>();
  279. string _query = CheckItemQuery(Settings.DesignProfilesSQL, V6Profile.DesignSQL, quoteitems);
  280. try
  281. {
  282. var _table = Query(_query,"profile");
  283. foreach (DataRow _row in _table.Rows)
  284. {
  285. var _profile = DataRowToProfile(_row);
  286. _result.Add(_profile);
  287. }
  288. return _result;
  289. }
  290. catch (Exception e)
  291. {
  292. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  293. }
  294. return _result;
  295. }
  296. private V6Profile DataRowToProfile(DataRow row)
  297. {
  298. var _result = new V6Profile();
  299. _result.Code = GetString(row, nameof(V6Profile.Code));
  300. _result.Description = GetString(row, nameof(V6Profile.Description));
  301. _result.Length = GetDouble(row, nameof(V6Profile.Length)) * GetScale(Settings.ProfileMeasurement);
  302. _result.Quantity = Math.Ceiling(GetDouble(row,nameof(V6Profile.Quantity)));
  303. _result.Cost = GetDouble(row, nameof(V6Profile.Cost));
  304. _result.Finish = GetString(row, nameof(V6Profile.Finish));
  305. return _result;
  306. }
  307. public List<V6Gasket> GetGaskets(V6Project project, Func<string,string,string> getSql)
  308. {
  309. var _result = new List<V6Gasket>();
  310. string _query = getSql(Settings.GasketSQL, V6Gasket.SQL);
  311. try
  312. {
  313. var _table = Query(_query,"gasket");
  314. foreach (DataRow _row in _table.Rows)
  315. {
  316. var _gasket = DataRowToGasket(_row);
  317. _result.Add(_gasket);
  318. }
  319. return _result;
  320. }
  321. catch (Exception e)
  322. {
  323. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  324. }
  325. return _result;
  326. }
  327. private V6Gasket DataRowToGasket(DataRow row)
  328. {
  329. var _result = new V6Gasket();
  330. _result.Code = GetString(row, nameof(V6Gasket.Code));
  331. _result.Description = GetString(row, nameof(V6Gasket.Description));
  332. _result.Quantity = GetDouble(row, nameof(V6Gasket.Quantity)) * GetScale(Settings.GasketMeasurement);
  333. _result.Cost = GetDouble(row, nameof(V6Profile.Cost)) / GetScale(Settings.GasketMeasurement);
  334. return _result;
  335. }
  336. public List<V6Component> GetComponents(V6Project project, Func<string,string,string> getSql)
  337. {
  338. var _result = new List<V6Component>();
  339. string _query = getSql(Settings.ComponentSQL, V6Component.SQL);
  340. try
  341. {
  342. var _table = Query(_query, "sundries");
  343. foreach (DataRow _row in _table.Rows)
  344. {
  345. var _sundry = DataRowToComponent(_row);
  346. _result.Add(_sundry);
  347. }
  348. }
  349. catch (Exception e)
  350. {
  351. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  352. }
  353. return _result;
  354. }
  355. private V6Component DataRowToComponent(DataRow row)
  356. {
  357. var _result = new V6Component();
  358. _result.Code = GetString(row, nameof(V6Component.Code));
  359. _result.Description = GetString(row, nameof(V6Component.Description));
  360. _result.PackSize = GetDouble(row, nameof(V6Component.PackSize));
  361. _result.Quantity = GetDouble(row, nameof(V6Component.Quantity));
  362. _result.Cost = GetDouble(row, nameof(V6Component.Cost));
  363. return _result;
  364. }
  365. public List<V6Glass> GetGlass(V6Project project, Func<string,string,string> getSql)
  366. {
  367. var _result = new List<V6Glass>();
  368. string _query = getSql(Settings.GlassSQL, V6Glass.SQL);
  369. try
  370. {
  371. var _table = Query(_query,"glass");
  372. foreach (DataRow _row in _table.Rows)
  373. _result.Add(DataRowToGlass(_row));
  374. }
  375. catch (Exception e)
  376. {
  377. Logger.Send(LogType.Error,"",$"{e.Message}\n{_query}");
  378. }
  379. return _result;
  380. }
  381. private V6Glass DataRowToGlass(DataRow row)
  382. {
  383. var result = new V6Glass();
  384. result.Code = GetString(row, nameof(V6Glass.Code));
  385. result.Description = GetString(row, nameof(V6Glass.Description));
  386. result.Treatment = GetString(row, nameof(V6Glass.Treatment));
  387. result.Height = GetDouble(row, nameof(V6Glass.Height)) * GetScale(Settings.ProfileMeasurement);
  388. result.Width = GetDouble(row, nameof(V6Glass.Width)) * GetScale(Settings.GlassMeasurement);
  389. result.Location = GetString(row, nameof(V6Glass.Location));
  390. result.Quantity = GetDouble(row, nameof(V6Glass.Quantity));
  391. result.Cost = GetDouble(row, nameof(V6Glass.Cost));
  392. return result;
  393. }
  394. private double GetScale(V6Measurement scale)
  395. {
  396. return scale == V6Measurement.Metres
  397. ? 0.0254
  398. : Settings.ProfileMeasurement == V6Measurement.Millimetres
  399. ? 25.4
  400. : 1.0;
  401. }
  402. }