V6Client.cs 12 KB

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