SiteManufacturing.xaml.cs 13 KB

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