V6Client.cs 12 KB

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