V6Client.cs 12 KB

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