V6Client.cs 14 KB

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