V6Client.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Configuration;
  12. using InABox.Core;
  13. using InABox.Dxf;
  14. using Syncfusion.Windows.Tools.Controls;
  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. Settings.CheckSQL();
  24. }
  25. protected override string GetConnectionString() => Settings.AsConnectionString();
  26. public IEnumerable<V6Quote> GetQuotes()
  27. {
  28. try
  29. {
  30. List<V6Quote> _projects = new();
  31. if (!IsConnected)
  32. return _projects;
  33. var _quotes = Query(Settings.QuoteSQL,"quotes");
  34. foreach (DataRow _row in _quotes.Rows)
  35. _projects.Add(DataRowToQuote(_row));
  36. return _projects;
  37. }
  38. catch (Exception e)
  39. {
  40. Console.WriteLine(e);
  41. throw;
  42. }
  43. }
  44. private V6Quote DataRowToQuote(DataRow row)
  45. {
  46. var _quote = new V6Quote()
  47. {
  48. ID = GetInteger(row,nameof(V6Quote.ID)),
  49. Revision = GetInteger(row, nameof(V6Quote.Revision)),
  50. Number = GetInteger(row, nameof(V6Quote.Number)),
  51. Variation = GetString(row,nameof(V6Quote.Variation)),
  52. ClientID = GetString(row,nameof(V6Quote.ClientID)),
  53. ClientName = GetString(row,nameof(V6Quote.ClientName)),
  54. Title = GetString(row,nameof(V6Quote.Title)),
  55. SellPrice = GetDouble(row,nameof(V6Quote.SellPrice)),
  56. Street = GetString(row,nameof(V6Quote.Street)),
  57. City = GetString(row,nameof(V6Quote.City)),
  58. State = GetString(row,nameof(V6Quote.State)),
  59. PostCode = GetString(row,nameof(V6Quote.PostCode)),
  60. };
  61. return _quote;
  62. }
  63. private static string CheckQuery(string query, int number, string variation, int? quoteitem = null)
  64. {
  65. string _basefilter = quoteitem == null
  66. ? $"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) "
  67. : $"qi.quote_item_id = {quoteitem} ";
  68. return query.Replace("where ", $"where {_basefilter} and ", StringComparison.CurrentCultureIgnoreCase);
  69. }
  70. public V6Quote? GetQuote(int number, string variation)
  71. {
  72. try
  73. {
  74. var _query = CheckQuery(Settings.QuoteSQL, number, variation);
  75. var _table = Query(_query,"quote");
  76. return _table.Rows.Count > 0
  77. ? DataRowToQuote(_table.Rows[0])
  78. : null;
  79. }
  80. catch (Exception e)
  81. {
  82. Console.WriteLine(e);
  83. throw;
  84. }
  85. }
  86. public List<V6Elevation> GetItems(int number, string variation)
  87. {
  88. try
  89. {
  90. List<V6Elevation> _result = new();
  91. var _quote = GetQuote(number, variation);
  92. if (_quote == null)
  93. return _result;
  94. var _query = CheckQuery(Settings.ElevationSQL, number, variation);
  95. var _table = Query(_query, "items");
  96. _result.AddRange(_table.Rows.OfType<DataRow>().Select(DataRowToItem));
  97. return _result;
  98. }
  99. catch (Exception e)
  100. {
  101. Console.WriteLine(e);
  102. throw;
  103. }
  104. }
  105. private V6Elevation DataRowToItem(DataRow row)
  106. {
  107. var _result = new V6Elevation()
  108. {
  109. ID = GetInteger(row, nameof(V6Elevation.ID)),
  110. Description = GetString(row,nameof(V6Elevation.Description)),
  111. Quantity = (int)GetDouble(row,nameof(V6Elevation.Quantity)),
  112. };
  113. return _result;
  114. }
  115. public V6Drawings GetDrawings(int itemnumber)
  116. {
  117. try
  118. {
  119. var _query = CheckQuery(Settings.DrawingsSQL, 0, "", itemnumber);
  120. var _table = Query(_query,"drawings");
  121. return _table.Rows.Count > 0
  122. ? DataRowToDrawings(_table.Rows[0])
  123. : null;
  124. }
  125. catch (Exception e)
  126. {
  127. Console.WriteLine(e);
  128. throw;
  129. }
  130. }
  131. private V6Drawings DataRowToDrawings(DataRow row)
  132. {
  133. return new V6Drawings()
  134. {
  135. Drawings = GetBinary(row, nameof(V6Drawings.Drawings))
  136. };
  137. }
  138. public List<V6Drawing> DecodeDrawings(byte[] _binary, string[] filetypes)
  139. {
  140. string StreamToString(Stream stream)
  141. {
  142. stream.Position = 0;
  143. using (StreamReader _reader = new StreamReader(stream, Encoding.UTF8))
  144. return _reader.ReadToEnd();
  145. }
  146. byte[] StreamToByteArray(Stream stream)
  147. {
  148. using(var _memoryStream = new MemoryStream())
  149. {
  150. stream.Position = 0;
  151. stream.CopyTo(_memoryStream);
  152. return _memoryStream.ToArray();
  153. }
  154. }
  155. List<V6Drawing> _result = new();
  156. if (_binary.Length != 0)
  157. {
  158. Syncfusion.Compression.Zip.ZipArchive _zip = new Syncfusion.Compression.Zip.ZipArchive();
  159. using (var _ms = new MemoryStream(_binary))
  160. {
  161. _zip.Open(_ms,false);
  162. var _descriptor = _zip.Items.FirstOrDefault(x =>
  163. string.Equals(x.ItemName, "descriptor.json", StringComparison.CurrentCultureIgnoreCase));
  164. if (_descriptor != null)
  165. {
  166. var _json = StreamToString(_descriptor.DataStream);
  167. var _drawings = Serialization.Deserialize<V6DrawingIndex>(_json) ?? new V6DrawingIndex();
  168. foreach (var _file in _drawings.Files)
  169. {
  170. if (filetypes == null || filetypes.Any(x =>
  171. String.Equals(x, _file.FileType, StringComparison.CurrentCultureIgnoreCase)))
  172. {
  173. var _frame = _zip.Items.FirstOrDefault(x =>
  174. string.Equals(x.ItemName, _file.FileName, StringComparison.CurrentCultureIgnoreCase));
  175. if (_frame != null)
  176. {
  177. var _bmp = DxfUtils.ProcessImage(_frame.DataStream);
  178. using (var _bmpStream = new MemoryStream())
  179. {
  180. _bmp.Save(_bmpStream, ImageFormat.Png);
  181. var _drawing = new V6Drawing();
  182. _drawing.Data = StreamToByteArray(_bmpStream);
  183. _drawing.FileName = Path.ChangeExtension(_file.FileName, ".png");
  184. _result.Add(_drawing);
  185. //File.WriteAllBytes(System.IO.Path.Combine(@"c:\development\ecoview",_file.FileName),_file.Data);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. return _result;
  194. }
  195. public List<V6Labour> GetLabour(int number, string variation, int? quoteitem = null)
  196. {
  197. try
  198. {
  199. var _result = new List<V6Labour>();
  200. var _quote = GetQuote(number, variation);
  201. if (_quote == null)
  202. return _result;
  203. string _query = CheckQuery(Settings.LabourSQL, number, variation, quoteitem);
  204. var _table = Query(_query,"labour");
  205. foreach (DataRow _row in _table.Rows)
  206. {
  207. var _labour = DataRowToLabour(_row);
  208. _result.Add(_labour);
  209. }
  210. return _result;
  211. }
  212. catch (Exception e)
  213. {
  214. Console.WriteLine(e);
  215. throw;
  216. }
  217. }
  218. private V6Labour DataRowToLabour(DataRow row)
  219. {
  220. var _labour = new V6Labour();
  221. _labour.Code = GetString(row, nameof(V6Labour.Code));
  222. _labour.Description = GetString(row, nameof(V6Labour.Description));
  223. _labour.Minutes = GetDouble(row, nameof(V6Labour.Minutes));
  224. _labour.Cost = GetDouble(row, nameof(V6Labour.Cost));
  225. return _labour;
  226. }
  227. public List<V6Profile> GetProfiles(int number, string variation, int? quoteitem = null)
  228. {
  229. try
  230. {
  231. var _result = new List<V6Profile>();
  232. var _quote = GetQuote(number, variation);
  233. if (_quote == null)
  234. return _result;
  235. string _query = CheckQuery(Settings.ProfileSQL, number, variation, quoteitem);
  236. var _table = Query(_query,"profile");
  237. foreach (DataRow _row in _table.Rows)
  238. {
  239. var _profile = DataRowToProfile(_row);
  240. _result.Add(_profile);
  241. }
  242. return _result;
  243. }
  244. catch (Exception e)
  245. {
  246. Console.WriteLine(e);
  247. throw;
  248. }
  249. }
  250. private V6Profile DataRowToProfile(DataRow row)
  251. {
  252. var _result = new V6Profile();
  253. _result.Code = GetString(row, nameof(V6Profile.Code));
  254. _result.Description = GetString(row, nameof(V6Profile.Description));
  255. _result.Length = GetDouble(row, nameof(V6Profile.Length));
  256. _result.Quantity = Math.Ceiling(GetDouble(row,nameof(V6Profile.Quantity)) / (_result.Length.IsEffectivelyEqual(0.0) ? 1.0 : _result.Length));
  257. _result.Cost = GetDouble(row, nameof(V6Profile.Cost));
  258. _result.Finish = GetString(row, nameof(V6Profile.Finish));
  259. return _result;
  260. }
  261. public List<V6Component> GetComponents(int number, string variation, int? quoteitem = null)
  262. {
  263. try
  264. {
  265. var _result = new List<V6Component>();
  266. var _quote = GetQuote(number, variation);
  267. if (_quote == null)
  268. return _result;
  269. string _query = CheckQuery(Settings.ComponentSQL, number, variation, quoteitem);
  270. var _table = Query(_query,"sundries");
  271. foreach (DataRow _row in _table.Rows)
  272. {
  273. var _sundry = DataRowToComponent(_row);
  274. _result.Add(_sundry);
  275. }
  276. return _result;
  277. }
  278. catch (Exception e)
  279. {
  280. Console.WriteLine(e);
  281. throw;
  282. }
  283. }
  284. private V6Component DataRowToComponent(DataRow row)
  285. {
  286. var _result = new V6Component();
  287. _result.Code = GetString(row, nameof(V6Component.Code));
  288. _result.Description = GetString(row, nameof(V6Component.Description));
  289. _result.PackSize = GetDouble(row, nameof(V6Component.PackSize));
  290. _result.Quantity = GetDouble(row, nameof(V6Component.Quantity));
  291. _result.Cost = GetDouble(row, nameof(V6Component.Cost));
  292. return _result;
  293. }
  294. public List<V6Glass> GetGlass(int number, string variation, int? quoteitem = null)
  295. {
  296. try
  297. {
  298. var _result = new List<V6Glass>();
  299. var _quote = GetQuote(number, variation);
  300. if (_quote == null)
  301. return _result;
  302. string _query = CheckQuery(Settings.GlassSQL, number, variation, quoteitem);
  303. var _table = Query(_query,"glass");
  304. foreach (DataRow _row in _table.Rows)
  305. {
  306. _result.Add(DataRowToGlass(_row));
  307. }
  308. return _result;
  309. }
  310. catch (Exception e)
  311. {
  312. Console.WriteLine(e);
  313. throw;
  314. }
  315. }
  316. private V6Glass DataRowToGlass(DataRow row)
  317. {
  318. return new V6Glass()
  319. {
  320. Code = GetString(row, nameof(V6Glass.Code)),
  321. Description = GetString(row, nameof(V6Glass.Description)),
  322. Treatment = GetString(row, nameof(V6Glass.Treatment)),
  323. Height = GetDouble(row, nameof(V6Glass.Height)),
  324. Width = GetDouble(row, nameof(V6Glass.Width)),
  325. Location = GetString(row, nameof(V6Glass.Location)),
  326. Quantity = GetDouble(row, nameof(V6Glass.Quantity)),
  327. Cost = GetDouble(row, nameof(V6Glass.Cost)),
  328. };
  329. }
  330. }