ManufacturingScreen.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using comal.timesheets.CustomControls;
  2. using Comal.Classes;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. using Plugin.Media;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Linq.Expressions;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using InABox.Mobile;
  15. using Xamarin.CommunityToolkit.Extensions;
  16. using Xamarin.CommunityToolkit.UI.Views;
  17. using Xamarin.Essentials;
  18. using Xamarin.Forms;
  19. using Xamarin.Forms.Xaml;
  20. using XF.Material.Forms.UI.Dialogs;
  21. using LogType = InABox.Core.LogType;
  22. namespace comal.timesheets
  23. {
  24. [XamlCompilation(XamlCompilationOptions.Compile)]
  25. public partial class ManufacturingScreen
  26. {
  27. List<ManufacturingPacketShell> shells = new List<ManufacturingPacketShell>();
  28. List<ManufacturingPacketShell> factorySearchList = new List<ManufacturingPacketShell>();
  29. List<String> factories = new List<string>();
  30. Job job = new Job();
  31. bool firstLoad = true;
  32. public ManufacturingScreen()
  33. {
  34. InitializeComponent();
  35. LoadPackets(Guid.Empty);
  36. }
  37. private void LoadPackets(Guid jobid)
  38. {
  39. Task.Run(() =>
  40. {
  41. shells.Clear();
  42. factorySearchList.Clear();
  43. Filter<ManufacturingPacket> filter = new Filter<ManufacturingPacket>(x => x.Completed).IsEqualTo(DateTime.MinValue).
  44. And(x => x.Archived).IsEqualTo(DateTime.MinValue);
  45. if (jobid != Guid.Empty)
  46. filter = filter.And(x => x.SetoutLink.JobLink.ID).IsEqualTo(jobid);
  47. CoreTable table = QueryPackets(filter);
  48. while (table == null)
  49. table = QueryPackets(filter);
  50. foreach (CoreRow row in table.Rows)
  51. {
  52. ManufacturingPacketShell shell = new ManufacturingPacketShell()
  53. {
  54. Title = row.Get<ManufacturingPacket, string>(x => x.Title),
  55. Quantity = row.Get<ManufacturingPacket, int>(x => x.Quantity).ToString(),
  56. DrawingID = row.Get<ManufacturingPacket, Guid>(x => x.Drawing.ID),
  57. StageLinkSection = row.Get<ManufacturingPacket, string>(x => x.StageLink.Section),
  58. JobNumber = row.Get<ManufacturingPacket, string>(x => x.JobLink.JobNumber),
  59. ITPCode = row.Get<ManufacturingPacket, string>(x => x.JobLink.JobNumber)
  60. + " " + row.Get<ManufacturingPacket, string>(x => x.ITP.Code),
  61. Created = "C: " + row.Get<ManufacturingPacket, DateTime>(x => x.Created).ToString("dd MMM yy"),
  62. DueDate = "D: " + row.Get<ManufacturingPacket, DateTime>(x => x.DueDate).ToString("dd MMM yy"),
  63. StageLinkStation = row.Get<ManufacturingPacket, int>(x => x.StageLink.Station),
  64. StageLinkPercentage = row.Get<ManufacturingPacket, double>(x => x.StageLink.PercentageComplete) + "%",
  65. FactoryName = row.Get<ManufacturingPacket, string>(x => x.ManufacturingTemplateLink.Factory.Name),
  66. Location = row.Get<ManufacturingPacket, string>(x => x.Location),
  67. SetoutID = row.Get<ManufacturingPacket, Guid>(x => x.SetoutLink.ID),
  68. SetoutNumber = row.Get<ManufacturingPacket, string>(x => x.SetoutLink.Number),
  69. ID = row.Get<ManufacturingPacket, Guid>(x => x.ID),
  70. TemplateLinkCode = row.Get<ManufacturingPacket, string>(x => x.ManufacturingTemplateLink.Code),
  71. JobName = row.Get<ManufacturingPacket, string>(x => x.SetoutLink.JobLink.Name),
  72. OrderID = row.Get<ManufacturingPacket, Guid>(x => x.OrderItem.ID),
  73. OrderRecDate = row.Get<ManufacturingPacket, DateTime>(x => x.OrderItem.ReceivedDate),
  74. JobID = row.Get<ManufacturingPacket, Guid>(x => x.JobLink.ID),
  75. };
  76. if (!string.IsNullOrWhiteSpace(row.Get<ManufacturingPacket, string>(x => x.WaterMark)))
  77. shell.Serial = "[" + row.Get<ManufacturingPacket, string>(x => x.WaterMark) + "] " + row.Get<ManufacturingPacket, string>(x => x.Serial) + ":";
  78. else
  79. shell.Serial = row.Get<ManufacturingPacket, string>(x => x.Serial);
  80. if (!string.IsNullOrWhiteSpace(shell.StageLinkSection))
  81. shell.StageLinkPercentage = shell.StageLinkPercentage + " of " + shell.StageLinkSection;
  82. else
  83. shell.StageLinkPercentage = "TBI";
  84. if (!string.IsNullOrWhiteSpace(row.Get<ManufacturingPacket, string>(x => x.Issues)))
  85. {
  86. shell.ImagePath = "notifications.png";
  87. shell.ImageHeight = 25.0;
  88. shell.ImageWidth = 25.0;
  89. if (Device.RuntimePlatform.Equals(Device.iOS))
  90. {
  91. shell.ImageHeight = 35.0;
  92. shell.ImageWidth = 35.0;
  93. }
  94. }
  95. if (shell.OrderID != Guid.Empty && shell.OrderRecDate == DateTime.MinValue)
  96. {
  97. shell.OnOrderVisible = true;
  98. shell.LastRowHeight = 30;
  99. if (row.Get<ManufacturingPacket, DateTime>(x => x.OrderItem.Consignment.EstimatedWarehouseArrival) != DateTime.MinValue)
  100. shell.OrderETA = "ON ORDER ETA: " + row.Get<ManufacturingPacket, DateTime>(x => x.OrderItem.Consignment.EstimatedWarehouseArrival).ToString("dd MMM yy");
  101. else
  102. shell.OrderETA = "ON ORDER";
  103. }
  104. if (!factories.Contains(row.Get<ManufacturingPacket, string>(x => x.ManufacturingTemplateLink.Factory.Name)))
  105. factories.Add(row.Get<ManufacturingPacket, string>(x => x.ManufacturingTemplateLink.Factory.Name));
  106. shells.Add(shell);
  107. }
  108. Device.BeginInvokeOnMainThread(() =>
  109. {
  110. if (firstLoad)
  111. {
  112. filterOptionsControl.Options = factories;
  113. filterOptionsControl.CreateRadioButtonsAndSetDefault("Fabrication");
  114. filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;
  115. firstLoad = false;
  116. }
  117. listView.ItemsSource = null;
  118. listView.ItemsSource = shells.Where(x => x.FactoryName.Equals("Fabrication"));
  119. var list = shells.Where(x => x.FactoryName.Equals("Fabrication"));
  120. foreach (ManufacturingPacketShell shell in list)
  121. {
  122. factorySearchList.Add(shell);
  123. }
  124. titleLbl.Text = "Packets: " + factorySearchList.Count();
  125. });
  126. });
  127. }
  128. private CoreTable QueryPackets(Filter<ManufacturingPacket> filter)
  129. {
  130. try
  131. {
  132. return new Client<ManufacturingPacket>().Query
  133. (
  134. filter,
  135. new Columns<ManufacturingPacket>(
  136. x => x.Title, //0
  137. x => x.Quantity, //1
  138. x => x.Drawing.ID, //2
  139. x => x.StageLink.Section, //3
  140. x => x.SetoutLink.JobLink.JobNumber, //4
  141. x => x.ITP.Code, //5
  142. x => x.Created, //6
  143. x => x.DueDate, //7
  144. x => x.StageLink.Station, //8
  145. x => x.StageLink.PercentageComplete, //9
  146. x => x.ManufacturingTemplateLink.Factory.Name, //10
  147. x => x.Location, //11
  148. x => x.Serial, //12
  149. x => x.SetoutLink.ID, //13
  150. x => x.SetoutLink.Number, //14
  151. x => x.ID, //15
  152. x => x.ManufacturingTemplateLink.Code, //16
  153. x => x.SetoutLink.JobLink.Name, //17
  154. x => x.WaterMark, //18
  155. x => x.Issues, //19
  156. x => x.OrderItem.ID, //20
  157. x => x.OrderItem.ReceivedDate, //21
  158. x => x.OrderItem.Consignment.EstimatedWarehouseArrival, //22
  159. x => x.SetoutLink.JobLink.ID //23
  160. )
  161. );
  162. }
  163. catch (Exception ex)
  164. {
  165. InABox.Mobile.MobileLogging.Log(ex);
  166. return null;
  167. }
  168. }
  169. private void JobFilterBtn_Clicked(object sender, EventArgs e)
  170. {
  171. JobSelectionPage page = new JobSelectionPage(
  172. (job) =>
  173. {
  174. jobBtn.Text = job.Name + " (" + job.JobNumber + ")";
  175. LoadPackets(job.ID);
  176. }
  177. );
  178. Navigation.PushAsync(page);
  179. }
  180. private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)
  181. {
  182. if (filterOption == filterOptionsControl.CurrentOption)
  183. return;
  184. filterOptionsControl.CurrentOption = filterOption;
  185. var list = shells.Where(x => x.FactoryName.Equals(filterOption));
  186. factorySearchList.Clear();
  187. foreach (ManufacturingPacketShell shell in list)
  188. {
  189. factorySearchList.Add(shell);
  190. }
  191. if (string.IsNullOrWhiteSpace(searchEnt.Text))
  192. {
  193. listView.ItemsSource = shells.Where(x => x.FactoryName.Equals(filterOption));
  194. titleLbl.Text = "Packets: " + factorySearchList.Count();
  195. }
  196. else
  197. {
  198. RunSearch(factorySearchList);
  199. }
  200. }
  201. private async void LoadPDF(Guid setoutid)
  202. {
  203. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  204. {
  205. CoreTable table = QuerySetOutDocuments(setoutid);
  206. while (table == null)
  207. table = QuerySetOutDocuments(setoutid);
  208. if (table.Rows.Any())
  209. {
  210. if (table.Rows.Count == 1)
  211. {
  212. List<object> list = table.Rows[0].Values;
  213. if (list[1] == null) list[1] = Guid.Empty;
  214. PDFViewer pDFViewer = new PDFViewer(Guid.Parse(list[1].ToString()));
  215. Device.BeginInvokeOnMainThread(() =>
  216. {
  217. Navigation.PushAsync(pDFViewer);
  218. });
  219. }
  220. else if (table.Rows.Count > 1)
  221. {
  222. Dictionary<string, Guid> fileNameIDs = new Dictionary<string, Guid>();
  223. foreach (CoreRow row in table.Rows)
  224. {
  225. List<object> list = row.Values;
  226. if (list[0] == null) list[0] = "";
  227. if (list[1] == null) list[1] = Guid.Empty;
  228. fileNameIDs.Add(list[0].ToString(), Guid.Parse(list[1].ToString()));
  229. }
  230. PDFList pdfList = new PDFList(); //fileNameIDs);
  231. Navigation.PushAsync(pdfList);
  232. }
  233. }
  234. else
  235. {
  236. Device.BeginInvokeOnMainThread(() =>
  237. {
  238. DisplayAlert("Alert", "No Drawings found", "OK");
  239. });
  240. }
  241. }
  242. }
  243. private CoreTable QuerySetOutDocuments(Guid setoutid)
  244. {
  245. try
  246. {
  247. return new Client<SetoutDocument>().Query
  248. (
  249. new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo(setoutid),
  250. new Columns<SetoutDocument>(x => x.DocumentLink.FileName, x => x.DocumentLink.ID)
  251. );
  252. }
  253. catch (Exception ex)
  254. {
  255. InABox.Mobile.MobileLogging.Log(ex);
  256. return null;
  257. }
  258. }
  259. private async void ListView_Tapped(object sender, EventArgs e)
  260. {
  261. ManufacturingPacketShell shell = listView.SelectedItem as ManufacturingPacketShell;
  262. string chosenOption = await DisplayActionSheet("Choose an Option", "Cancel", null, "View Drawing", "View Status");
  263. switch (chosenOption)
  264. {
  265. case "Cancel":
  266. break;
  267. default:
  268. break;
  269. case "View Drawing":
  270. LoadPDF(shell.SetoutID);
  271. break;
  272. case "View Status":
  273. ManufacturingPacketPopup popup = new ManufacturingPacketPopup(shell.ID, shell.OrderID);
  274. Navigation.PushAsync(popup);
  275. break;
  276. }
  277. }
  278. private void SearchEnt_Changed(object sender, EventArgs e)
  279. {
  280. if (string.IsNullOrWhiteSpace(searchEnt.Text))
  281. {
  282. listView.ItemsSource = shells.Where(x => x.FactoryName.Equals(filterOptionsControl.CurrentOption));
  283. }
  284. else
  285. {
  286. RunSearch(factorySearchList);
  287. }
  288. }
  289. private void RunSearch(List<ManufacturingPacketShell> packetShells)
  290. {
  291. var list = packetShells.Where(x =>
  292. x.Title.Contains(searchEnt.Text) || x.Title.Contains(searchEnt.Text.ToUpper()) || x.Title.Contains(UpperCaseFirst(searchEnt.Text)) || x.Title.Contains(searchEnt.Text.ToLower()) ||
  293. x.Quantity.Contains(searchEnt.Text) ||
  294. x.JobNumber.Contains(searchEnt.Text) ||
  295. x.DueDate.Contains(searchEnt.Text) || x.DueDate.Contains(searchEnt.Text.ToUpper()) || x.DueDate.Contains(searchEnt.Text.ToLower()) || x.DueDate.Contains(UpperCaseFirst(searchEnt.Text)) ||
  296. x.Created.Contains(searchEnt.Text) || x.Created.Contains(searchEnt.Text.ToUpper()) || x.Created.Contains(searchEnt.Text.ToLower()) || x.Created.Contains(UpperCaseFirst(searchEnt.Text)) ||
  297. x.ITPCode.Contains(searchEnt.Text) || x.ITPCode.Contains(searchEnt.Text.ToUpper()) || x.ITPCode.Contains(searchEnt.Text.ToLower()) || x.ITPCode.Contains(UpperCaseFirst(searchEnt.Text)) ||
  298. x.Location.Contains(searchEnt.Text) || x.Location.Contains(searchEnt.Text.ToUpper()) || x.Location.Contains(searchEnt.Text.ToLower()) || x.Location.Contains(UpperCaseFirst(searchEnt.Text)) ||
  299. x.SetoutNumber.Contains(searchEnt.Text) || x.SetoutNumber.Contains(searchEnt.Text.ToUpper()) || x.SetoutNumber.Contains(searchEnt.Text.ToLower()) || x.SetoutNumber.Contains(UpperCaseFirst(searchEnt.Text)) ||
  300. x.Serial.Contains(searchEnt.Text) || x.Serial.Contains(searchEnt.Text.ToUpper()) || x.Serial.Contains(searchEnt.Text.ToLower()) || x.Serial.Contains(UpperCaseFirst(searchEnt.Text))
  301. );
  302. listView.ItemsSource = list;
  303. titleLbl.Text = "Packets: " + list.Count();
  304. }
  305. static String UpperCaseFirst(string s)
  306. {
  307. char[] a = s.ToCharArray();
  308. a[0] = char.ToUpper(a[0]);
  309. return new string(a);
  310. }
  311. }
  312. public class ManufacturingPacketShell
  313. {
  314. public Guid ID { get; set; }
  315. public string Title { get; set; }
  316. public string Quantity { get; set; }
  317. public Guid DrawingID { get; set; }
  318. public string StageLinkSection { get; set; }
  319. public string JobNumber { get; set; }
  320. public Guid JobID { get; set; }
  321. public string ITPCode { get; set; }
  322. public string Created { get; set; }
  323. public string DueDate { get; set; }
  324. public int StageLinkStation { get; set; }
  325. public string StageLinkPercentage { get; set; }
  326. public string FactoryName { get; set; }
  327. public string Location { get; set; }
  328. public string Serial { get; set; }
  329. public Guid SetoutID { get; set; }
  330. public string SetoutNumber { get; set; }
  331. public string TemplateLinkCode { get; set; }
  332. public string JobName { get; set; }
  333. public string WaterMark { get; set; }
  334. public string ImagePath { get; set; }
  335. public double ImageHeight { get; set; }
  336. public double ImageWidth { get; set; }
  337. public Color Color { get; set; }
  338. public Guid OrderID { get; set; }
  339. public DateTime OrderRecDate { get; set; }
  340. public double LastRowHeight { get; set; }
  341. public bool OnOrderVisible { get; set; }
  342. public String OrderETA { get; set; }
  343. public ManufacturingPacketShell()
  344. {
  345. ID = Guid.Empty;
  346. Title = "";
  347. Quantity = "";
  348. DrawingID = Guid.Empty;
  349. StageLinkSection = "";
  350. JobNumber = "";
  351. ITPCode = "";
  352. Created = "";
  353. DueDate = "";
  354. StageLinkStation = 0;
  355. StageLinkPercentage = "";
  356. FactoryName = "";
  357. Location = "";
  358. Serial = "";
  359. SetoutID = Guid.Empty;
  360. SetoutNumber = "";
  361. TemplateLinkCode = "";
  362. JobName = "";
  363. WaterMark = "";
  364. ImagePath = "";
  365. ImageHeight = 0.0;
  366. ImageWidth = 0.0;
  367. OrderID = Guid.Empty;
  368. Color = Color.Default;
  369. OrderRecDate = DateTime.MinValue;
  370. LastRowHeight = 0;
  371. OnOrderVisible = false;
  372. OrderETA = "";
  373. JobID = Guid.Empty;
  374. }
  375. }
  376. }