using comal.timesheets.CustomControls; using Comal.Classes; using InABox.Clients; using InABox.Core; using Plugin.Media; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading; using System.Threading.Tasks; using InABox.Mobile; using Xamarin.CommunityToolkit.Extensions; using Xamarin.CommunityToolkit.UI.Views; using Xamarin.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI.Dialogs; using LogType = InABox.Core.LogType; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class ManufacturingScreen { List shells = new List(); List factorySearchList = new List(); List factories = new List(); Job job = new Job(); bool firstLoad = true; public ManufacturingScreen() { InitializeComponent(); LoadPackets(Guid.Empty); } private void LoadPackets(Guid jobid) { Task.Run(() => { shells.Clear(); factorySearchList.Clear(); Filter filter = new Filter(x => x.Completed).IsEqualTo(DateTime.MinValue). And(x => x.Archived).IsEqualTo(DateTime.MinValue); if (jobid != Guid.Empty) filter = filter.And(x => x.SetoutLink.JobLink.ID).IsEqualTo(jobid); CoreTable table = QueryPackets(filter); while (table == null) table = QueryPackets(filter); foreach (CoreRow row in table.Rows) { ManufacturingPacketShell shell = new ManufacturingPacketShell() { Title = row.Get(x => x.Title), Quantity = row.Get(x => x.Quantity).ToString(), DrawingID = row.Get(x => x.Drawing.ID), StageLinkSection = row.Get(x => x.StageLink.Section), JobNumber = row.Get(x => x.JobLink.JobNumber), ITPCode = row.Get(x => x.JobLink.JobNumber) + " " + row.Get(x => x.ITP.Code), Created = "C: " + row.Get(x => x.Created).ToString("dd MMM yy"), DueDate = "D: " + row.Get(x => x.DueDate).ToString("dd MMM yy"), StageLinkStation = row.Get(x => x.StageLink.Station), StageLinkPercentage = row.Get(x => x.StageLink.PercentageComplete) + "%", FactoryName = row.Get(x => x.ManufacturingTemplateLink.Factory.Name), Location = row.Get(x => x.Location), SetoutID = row.Get(x => x.SetoutLink.ID), SetoutNumber = row.Get(x => x.SetoutLink.Number), ID = row.Get(x => x.ID), TemplateLinkCode = row.Get(x => x.ManufacturingTemplateLink.Code), JobName = row.Get(x => x.SetoutLink.JobLink.Name), OrderID = row.Get(x => x.OrderItem.ID), OrderRecDate = row.Get(x => x.OrderItem.ReceivedDate), JobID = row.Get(x => x.JobLink.ID), }; if (!string.IsNullOrWhiteSpace(row.Get(x => x.WaterMark))) shell.Serial = "[" + row.Get(x => x.WaterMark) + "] " + row.Get(x => x.Serial) + ":"; else shell.Serial = row.Get(x => x.Serial); if (!string.IsNullOrWhiteSpace(shell.StageLinkSection)) shell.StageLinkPercentage = shell.StageLinkPercentage + " of " + shell.StageLinkSection; else shell.StageLinkPercentage = "TBI"; if (!string.IsNullOrWhiteSpace(row.Get(x => x.Issues))) { shell.ImagePath = "notifications.png"; shell.ImageHeight = 25.0; shell.ImageWidth = 25.0; if (Device.RuntimePlatform.Equals(Device.iOS)) { shell.ImageHeight = 35.0; shell.ImageWidth = 35.0; } } if (shell.OrderID != Guid.Empty && shell.OrderRecDate == DateTime.MinValue) { shell.OnOrderVisible = true; shell.LastRowHeight = 30; if (row.Get(x => x.OrderItem.Consignment.EstimatedWarehouseArrival) != DateTime.MinValue) shell.OrderETA = "ON ORDER ETA: " + row.Get(x => x.OrderItem.Consignment.EstimatedWarehouseArrival).ToString("dd MMM yy"); else shell.OrderETA = "ON ORDER"; } if (!factories.Contains(row.Get(x => x.ManufacturingTemplateLink.Factory.Name))) factories.Add(row.Get(x => x.ManufacturingTemplateLink.Factory.Name)); shells.Add(shell); } Device.BeginInvokeOnMainThread(() => { if (firstLoad) { filterOptionsControl.Options = factories; filterOptionsControl.CreateRadioButtonsAndSetDefault("Fabrication"); filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged; firstLoad = false; } listView.ItemsSource = null; listView.ItemsSource = shells.Where(x => x.FactoryName.Equals("Fabrication")); var list = shells.Where(x => x.FactoryName.Equals("Fabrication")); foreach (ManufacturingPacketShell shell in list) { factorySearchList.Add(shell); } titleLbl.Text = "Packets: " + factorySearchList.Count(); }); }); } private CoreTable QueryPackets(Filter filter) { try { return new Client().Query ( filter, new Columns( x => x.Title, //0 x => x.Quantity, //1 x => x.Drawing.ID, //2 x => x.StageLink.Section, //3 x => x.SetoutLink.JobLink.JobNumber, //4 x => x.ITP.Code, //5 x => x.Created, //6 x => x.DueDate, //7 x => x.StageLink.Station, //8 x => x.StageLink.PercentageComplete, //9 x => x.ManufacturingTemplateLink.Factory.Name, //10 x => x.Location, //11 x => x.Serial, //12 x => x.SetoutLink.ID, //13 x => x.SetoutLink.Number, //14 x => x.ID, //15 x => x.ManufacturingTemplateLink.Code, //16 x => x.SetoutLink.JobLink.Name, //17 x => x.WaterMark, //18 x => x.Issues, //19 x => x.OrderItem.ID, //20 x => x.OrderItem.ReceivedDate, //21 x => x.OrderItem.Consignment.EstimatedWarehouseArrival, //22 x => x.SetoutLink.JobLink.ID //23 ) ); } catch (Exception ex) { InABox.Mobile.MobileLogging.Log(ex); return null; } } private void JobFilterBtn_Clicked(object sender, EventArgs e) { JobSelectionPage page = new JobSelectionPage( (job) => { jobBtn.Text = job.Name + " (" + job.JobNumber + ")"; LoadPackets(job.ID); } ); Navigation.PushAsync(page); } private void FilterOptionsControl_OnFilterOptionChanged(string filterOption) { if (filterOption == filterOptionsControl.CurrentOption) return; filterOptionsControl.CurrentOption = filterOption; var list = shells.Where(x => x.FactoryName.Equals(filterOption)); factorySearchList.Clear(); foreach (ManufacturingPacketShell shell in list) { factorySearchList.Add(shell); } if (string.IsNullOrWhiteSpace(searchEnt.Text)) { listView.ItemsSource = shells.Where(x => x.FactoryName.Equals(filterOption)); titleLbl.Text = "Packets: " + factorySearchList.Count(); } else { RunSearch(factorySearchList); } } private async void LoadPDF(Guid setoutid) { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { CoreTable table = QuerySetOutDocuments(setoutid); while (table == null) table = QuerySetOutDocuments(setoutid); if (table.Rows.Any()) { if (table.Rows.Count == 1) { List list = table.Rows[0].Values; if (list[1] == null) list[1] = Guid.Empty; PDFViewer pDFViewer = new PDFViewer(Guid.Parse(list[1].ToString())); Device.BeginInvokeOnMainThread(() => { Navigation.PushAsync(pDFViewer); }); } else if (table.Rows.Count > 1) { Dictionary fileNameIDs = new Dictionary(); foreach (CoreRow row in table.Rows) { List list = row.Values; if (list[0] == null) list[0] = ""; if (list[1] == null) list[1] = Guid.Empty; fileNameIDs.Add(list[0].ToString(), Guid.Parse(list[1].ToString())); } PDFList pdfList = new PDFList(); //fileNameIDs); Navigation.PushAsync(pdfList); } } else { Device.BeginInvokeOnMainThread(() => { DisplayAlert("Alert", "No Drawings found", "OK"); }); } } } private CoreTable QuerySetOutDocuments(Guid setoutid) { try { return new Client().Query ( new Filter(x => x.EntityLink.ID).IsEqualTo(setoutid), new Columns(x => x.DocumentLink.FileName, x => x.DocumentLink.ID) ); } catch (Exception ex) { InABox.Mobile.MobileLogging.Log(ex); return null; } } private async void ListView_Tapped(object sender, EventArgs e) { ManufacturingPacketShell shell = listView.SelectedItem as ManufacturingPacketShell; string chosenOption = await DisplayActionSheet("Choose an Option", "Cancel", null, "View Drawing", "View Status"); switch (chosenOption) { case "Cancel": break; default: break; case "View Drawing": LoadPDF(shell.SetoutID); break; case "View Status": ManufacturingPacketPopup popup = new ManufacturingPacketPopup(shell.ID, shell.OrderID); Navigation.PushAsync(popup); break; } } private void SearchEnt_Changed(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(searchEnt.Text)) { listView.ItemsSource = shells.Where(x => x.FactoryName.Equals(filterOptionsControl.CurrentOption)); } else { RunSearch(factorySearchList); } } private void RunSearch(List packetShells) { var list = packetShells.Where(x => x.Title.Contains(searchEnt.Text) || x.Title.Contains(searchEnt.Text.ToUpper()) || x.Title.Contains(UpperCaseFirst(searchEnt.Text)) || x.Title.Contains(searchEnt.Text.ToLower()) || x.Quantity.Contains(searchEnt.Text) || x.JobNumber.Contains(searchEnt.Text) || x.DueDate.Contains(searchEnt.Text) || x.DueDate.Contains(searchEnt.Text.ToUpper()) || x.DueDate.Contains(searchEnt.Text.ToLower()) || x.DueDate.Contains(UpperCaseFirst(searchEnt.Text)) || x.Created.Contains(searchEnt.Text) || x.Created.Contains(searchEnt.Text.ToUpper()) || x.Created.Contains(searchEnt.Text.ToLower()) || x.Created.Contains(UpperCaseFirst(searchEnt.Text)) || x.ITPCode.Contains(searchEnt.Text) || x.ITPCode.Contains(searchEnt.Text.ToUpper()) || x.ITPCode.Contains(searchEnt.Text.ToLower()) || x.ITPCode.Contains(UpperCaseFirst(searchEnt.Text)) || x.Location.Contains(searchEnt.Text) || x.Location.Contains(searchEnt.Text.ToUpper()) || x.Location.Contains(searchEnt.Text.ToLower()) || x.Location.Contains(UpperCaseFirst(searchEnt.Text)) || x.SetoutNumber.Contains(searchEnt.Text) || x.SetoutNumber.Contains(searchEnt.Text.ToUpper()) || x.SetoutNumber.Contains(searchEnt.Text.ToLower()) || x.SetoutNumber.Contains(UpperCaseFirst(searchEnt.Text)) || x.Serial.Contains(searchEnt.Text) || x.Serial.Contains(searchEnt.Text.ToUpper()) || x.Serial.Contains(searchEnt.Text.ToLower()) || x.Serial.Contains(UpperCaseFirst(searchEnt.Text)) ); listView.ItemsSource = list; titleLbl.Text = "Packets: " + list.Count(); } static String UpperCaseFirst(string s) { char[] a = s.ToCharArray(); a[0] = char.ToUpper(a[0]); return new string(a); } } public class ManufacturingPacketShell { public Guid ID { get; set; } public string Title { get; set; } public string Quantity { get; set; } public Guid DrawingID { get; set; } public string StageLinkSection { get; set; } public string JobNumber { get; set; } public Guid JobID { get; set; } public string ITPCode { get; set; } public string Created { get; set; } public string DueDate { get; set; } public int StageLinkStation { get; set; } public string StageLinkPercentage { get; set; } public string FactoryName { get; set; } public string Location { get; set; } public string Serial { get; set; } public Guid SetoutID { get; set; } public string SetoutNumber { get; set; } public string TemplateLinkCode { get; set; } public string JobName { get; set; } public string WaterMark { get; set; } public string ImagePath { get; set; } public double ImageHeight { get; set; } public double ImageWidth { get; set; } public Color Color { get; set; } public Guid OrderID { get; set; } public DateTime OrderRecDate { get; set; } public double LastRowHeight { get; set; } public bool OnOrderVisible { get; set; } public String OrderETA { get; set; } public ManufacturingPacketShell() { ID = Guid.Empty; Title = ""; Quantity = ""; DrawingID = Guid.Empty; StageLinkSection = ""; JobNumber = ""; ITPCode = ""; Created = ""; DueDate = ""; StageLinkStation = 0; StageLinkPercentage = ""; FactoryName = ""; Location = ""; Serial = ""; SetoutID = Guid.Empty; SetoutNumber = ""; TemplateLinkCode = ""; JobName = ""; WaterMark = ""; ImagePath = ""; ImageHeight = 0.0; ImageWidth = 0.0; OrderID = Guid.Empty; Color = Color.Default; OrderRecDate = DateTime.MinValue; LastRowHeight = 0; OnOrderVisible = false; OrderETA = ""; JobID = Guid.Empty; } } }