SiteManufacturing.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using InABox.Mobile;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using LogType = InABox.Core.LogType;
  12. using SelectionChangedEventArgs = Syncfusion.XForms.Buttons.SelectionChangedEventArgs;
  13. namespace comal.timesheets
  14. {
  15. [XamlCompilation(XamlCompilationOptions.Compile)]
  16. public partial class SiteManufacturing : SitePage
  17. {
  18. #region Constructor / Fields
  19. List<SiteManufacturingPacketShell> packets = new List<SiteManufacturingPacketShell>();
  20. public SiteManufacturing()
  21. {
  22. InitializeComponent();
  23. }
  24. protected override void JobLoaded()
  25. {
  26. RefreshOnJobChange(Job);
  27. LoadPackets();
  28. }
  29. #endregion
  30. #region Loading
  31. private void LoadPackets()
  32. {
  33. Task.Run(() =>
  34. {
  35. QueryAndAddPackets();
  36. Device.BeginInvokeOnMainThread(() =>
  37. {
  38. ShowList();
  39. RefreshOnListChange(packets);
  40. });
  41. });
  42. }
  43. private void RefreshOnJobChange(JobDetailModel job)
  44. {
  45. packets.Clear();
  46. RefreshOnListChange(packets);
  47. locationSearchEnt.Text = "";
  48. serialSearchEnt.Text = "";
  49. setoutSearchEnt.Text = "";
  50. ShowLoading();
  51. }
  52. private void ShowLoading()
  53. {
  54. loadingLbl.Text = "Loading...";
  55. loadingCol.Width = new GridLength(1, GridUnitType.Star);
  56. loadingLbl.IsVisible = true;
  57. listCol.Width = 0;
  58. listView.IsVisible = false;
  59. }
  60. private void ShowList()
  61. {
  62. listCol.Width = new GridLength(1, GridUnitType.Star);
  63. listView.IsVisible = true;
  64. loadingCol.Width = 0;
  65. loadingLbl.IsVisible = false;
  66. }
  67. private void QueryAndAddPackets()
  68. {
  69. CoreTable packetstable = QueryPackets();
  70. while (packetstable == null)
  71. packetstable = QueryPackets();
  72. foreach (CoreRow row in packetstable.Rows)
  73. CreateAndAddPacket(row);
  74. }
  75. private CoreTable QueryPackets()
  76. {
  77. try
  78. {
  79. return new Client<ManufacturingPacket>().Query(
  80. new Filter<ManufacturingPacket>(
  81. x => x.SetoutLink.JobLink.ID).IsEqualTo(Job.Item.ID)
  82. .And(x => x.Serial).IsNotEqualTo(null)
  83. .And(x => x.Location).IsNotEqualTo(null)
  84. ,
  85. new Columns<ManufacturingPacket>(
  86. x => x.ID,
  87. x => x.OrderItem.ID,
  88. x => x.Serial,
  89. x => x.Location,
  90. x => x.SetoutLink.ID,
  91. x => x.SetoutLink.Number
  92. ));
  93. }
  94. catch (Exception ex)
  95. {
  96. InABox.Mobile.MobileLogging.Log(ex);
  97. return null;
  98. }
  99. }
  100. private void CreateAndAddPacket(CoreRow row)
  101. {
  102. packets.Add(new SiteManufacturingPacketShell
  103. {
  104. ID = row.Get<ManufacturingPacket, Guid>(x => x.ID),
  105. OrderID = row.Get<ManufacturingPacket, Guid>(x => x.OrderItem.ID),
  106. Serial = "Serial: " + row.Get<ManufacturingPacket, string>(x => x.Serial),
  107. Location = "Location: " + row.Get<ManufacturingPacket, string>(x => x.Location),
  108. SetoutID = row.Get<ManufacturingPacket, Guid>(x => x.SetoutLink.ID),
  109. Setout = "Setout: " + row.Get<ManufacturingPacket, string>(x => x.SetoutLink.Number)
  110. });
  111. }
  112. #endregion
  113. #region Taps
  114. private void ExitBtn_Clicked(object sender, EventArgs e)
  115. {
  116. Navigation.PopAsync();
  117. }
  118. private void Packet_Tapped(object sender, EventArgs e)
  119. {
  120. try
  121. {
  122. SiteManufacturingPacketShell packet = listView.SelectedItem as SiteManufacturingPacketShell;
  123. ManufacturingPacketPopup popup = new ManufacturingPacketPopup(packet.ID, packet.OrderID);
  124. Navigation.PushAsync(popup);
  125. }
  126. catch (Exception ex)
  127. {
  128. DisplayAlert("Error", ex.Message, "OK");
  129. }
  130. }
  131. private void PDFs_Tapped(object sender, EventArgs e)
  132. {
  133. try
  134. {
  135. var item = ((TappedEventArgs)e).Parameter as SiteManufacturingPacketShell;
  136. if (item == null) return;
  137. Dictionary<string, Guid> fileNameIDS = new Dictionary<string, Guid>();
  138. Filter<SetoutDocument> filter = new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo(item.SetoutID)
  139. .And(x => x.Superceded).IsEqualTo(DateTime.MinValue);
  140. CoreTable table = new Client<SetoutDocument>().Query
  141. (
  142. filter,
  143. new Columns<SetoutDocument>(x => x.DocumentLink.ID, x => x.DocumentLink.FileName)
  144. );
  145. if (table.Rows.Any())
  146. {
  147. foreach (CoreRow row in table.Rows)
  148. {
  149. if (!fileNameIDS.ContainsKey(row.Get<string>("DocumentLink.FileName")))
  150. fileNameIDS.Add(row.Get<string>("DocumentLink.FileName"), row.Get<Guid>("DocumentLink.ID"));
  151. }
  152. PDFList list = new PDFList(); //fileNameIDS);
  153. Navigation.PushAsync(list);
  154. }
  155. else
  156. DisplayAlert("Alert", "No Drawings found", "OK");
  157. }
  158. catch (Exception ex)
  159. {
  160. DisplayAlert("Error", ex.Message, "OK");
  161. }
  162. }
  163. #endregion
  164. #region Searching
  165. List<SiteManufacturingPacketShell> serialSearchList = new List<SiteManufacturingPacketShell>();
  166. List<SiteManufacturingPacketShell> locationSearchList = new List<SiteManufacturingPacketShell>();
  167. List<SiteManufacturingPacketShell> setoutSearchList = new List<SiteManufacturingPacketShell>();
  168. bool bSerialSearch = false;
  169. bool bLocationSearch = false;
  170. bool bSetoutSearch = false;
  171. private void SerialSearchEnt_Changed(object sender, EventArgs e)
  172. {
  173. serialSearchList.Clear();
  174. if (!string.IsNullOrWhiteSpace(serialSearchEnt.Text))
  175. {
  176. bSerialSearch = true;
  177. serialSearchList = CreateSerialSearchList(packets);
  178. }
  179. else
  180. bSerialSearch = false;
  181. RunSearch();
  182. }
  183. private void LocationSearchEnt_Changed(object sender, EventArgs e)
  184. {
  185. locationSearchList.Clear();
  186. if (!string.IsNullOrWhiteSpace(locationSearchEnt.Text))
  187. {
  188. bLocationSearch = true;
  189. locationSearchList = CreateLocationSearchList(packets);
  190. }
  191. else
  192. bLocationSearch = false;
  193. RunSearch();
  194. }
  195. private void SetoutSearchEnt_Changed(object sender, EventArgs e)
  196. {
  197. setoutSearchList.Clear();
  198. if (!string.IsNullOrWhiteSpace(setoutSearchEnt.Text))
  199. {
  200. bSetoutSearch = true;
  201. setoutSearchList = CreateSetoutSearchList(packets);
  202. }
  203. else
  204. bSetoutSearch = false;
  205. RunSearch();
  206. }
  207. private void RunSearch()
  208. {
  209. List<SiteManufacturingPacketShell> searchList = new List<SiteManufacturingPacketShell>();
  210. if (bSerialSearch && bLocationSearch && bSetoutSearch)
  211. {
  212. searchList = serialSearchList;
  213. searchList = CreateLocationSearchList(searchList);
  214. searchList = CreateSetoutSearchList(searchList);
  215. }
  216. else if (bSerialSearch && bLocationSearch && !bSetoutSearch)
  217. {
  218. searchList = serialSearchList;
  219. searchList = CreateLocationSearchList(searchList);
  220. }
  221. else if (bSerialSearch && !bLocationSearch && bSetoutSearch)
  222. {
  223. searchList = serialSearchList;
  224. searchList = CreateSetoutSearchList(searchList);
  225. }
  226. else if (!bSerialSearch && bLocationSearch && bSetoutSearch)
  227. {
  228. searchList = locationSearchList;
  229. searchList = CreateSetoutSearchList(searchList);
  230. }
  231. else if (bSerialSearch && !bLocationSearch && !bSetoutSearch)
  232. searchList = serialSearchList;
  233. else if (!bSerialSearch && bLocationSearch && !bSetoutSearch)
  234. searchList = locationSearchList;
  235. else if (!bSerialSearch && !bLocationSearch && bSetoutSearch)
  236. searchList = setoutSearchList;
  237. if (bSerialSearch || bLocationSearch || bSetoutSearch)
  238. RefreshOnListChange(searchList);
  239. else
  240. RefreshOnListChange(packets);
  241. }
  242. private List<SiteManufacturingPacketShell> CreateLocationSearchList(List<SiteManufacturingPacketShell> sublist)
  243. {
  244. return sublist
  245. .Where(x => x.Location.ToUpper().Contains(locationSearchEnt.Text.ToUpper()))
  246. .ToList();
  247. }
  248. private List<SiteManufacturingPacketShell> CreateSerialSearchList(List<SiteManufacturingPacketShell> sublist)
  249. {
  250. return sublist
  251. .Where(x => x.Serial.ToUpper().Contains(serialSearchEnt.Text.ToUpper()))
  252. .ToList();
  253. }
  254. private List<SiteManufacturingPacketShell> CreateSetoutSearchList(List<SiteManufacturingPacketShell> sublist)
  255. {
  256. return sublist
  257. .Where(x=>x.Setout.ToUpper().Contains(setoutSearchEnt.Text.ToUpper()))
  258. .ToList();
  259. }
  260. private void RefreshOnListChange(IEnumerable<SiteManufacturingPacketShell> list)
  261. {
  262. listView.ItemsSource = null;
  263. listView.ItemsSource = list;
  264. Title = "Packets (" + list.Count() + ")";
  265. }
  266. #endregion
  267. private void Display_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  268. {
  269. throw new NotImplementedException();
  270. }
  271. }
  272. #region Classes
  273. /// <summary>
  274. /// ViewModel class for SiteManufacturing Module
  275. /// </summary>
  276. public class SiteManufacturingPacketShell
  277. {
  278. public Guid ID { get; set; }
  279. public Guid OrderID { get; set; }
  280. public Guid SetoutID { get; set; }
  281. public string Serial { get; set; }
  282. public string Location { get; set; }
  283. public string Setout { get; set; }
  284. public SiteManufacturingPacketShell()
  285. {
  286. ID = Guid.Empty;
  287. OrderID = Guid.Empty;
  288. Serial = "";
  289. Location = "";
  290. SetoutID = Guid.Empty;
  291. Setout = "";
  292. }
  293. }
  294. #endregion
  295. }