using comal.timesheets.Manufacturing; using Comal.Classes; using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class ManufacturingPacketPopup { Guid _manufacturingPacketID = Guid.Empty; List manufacturingPacketShells = new List(); List auditTrailShells = new List(); public ManufacturingPacketPopup(Guid manufacturingpacketlinkID, Guid orderID) { InitializeComponent(); _manufacturingPacketID = manufacturingpacketlinkID; LoadStages(); LoadAuditTrail(); if (orderID != Guid.Empty) LoadOrder(orderID); } private void LoadOrder(Guid orderID) { Task.Run(() => { CoreTable table = QueryPurchaseOrderItem(orderID); while (table == null) table = QueryPurchaseOrderItem(orderID); if (table.Rows.Any()) { var poItem = table.Rows.FirstOrDefault().ToObject(); string dueDate = poItem.Consignment.EstimatedWarehouseArrival.ToString("dd MMM yy"); string supplierName = poItem.PurchaseOrderLink.SupplierLink.Name; string recDate = poItem.ReceivedDate.ToString("dd MMM yy"); if (poItem.ReceivedDate != DateTime.MinValue) //item has been received back to comal { Device.BeginInvokeOnMainThread(() => { ETAFrame.IsVisible = true; ETALbl.Text = "Item received from " + supplierName + " on " + recDate; }); } else { Device.BeginInvokeOnMainThread(() => { ETAFrame.IsVisible = true; ETALbl.Text = "Item at " + supplierName + ", ETA: " + dueDate + System.Environment.NewLine + "(ETAs are auto-generated one week from date of purchase order creation)"; }); } } }); } private CoreTable QueryPurchaseOrderItem(Guid orderID) { try { return new Client().Query ( new Filter(x => x.ID).IsEqualTo(orderID), new Columns(x => x.Consignment.EstimatedWarehouseArrival, x => x.PurchaseOrderLink.SupplierLink.Name, x => x.ReceivedDate) ); } catch (Exception ex) { InABox.Mobile.MobileLogging.Log(ex); return null; } } private void LoadStages() { Task.Run(() => { CoreTable table = QueryStages(); while (table == null) table = QueryStages(); if (table.Rows.Any()) { foreach (CoreRow row in table.Rows) { List list = row.Values; if (list[2] == null) { list[2] = 0.0; } //2 if (list[3] == null) { list[3] = ""; } //3 ManufacturingPacketStageShell shell = new ManufacturingPacketStageShell() { PercentageComplete = list[2].ToString() + "%", ManufacturingSectionLinkName = list[3].ToString(), }; Device.BeginInvokeOnMainThread(() => { FactoryStageContentView factoryStageContentView = new FactoryStageContentView(shell); stageStackLayout.Children.Add(factoryStageContentView); }); } } else { AddDefaults(); } }); } private CoreTable QueryStages() { try { return new Client().Query( new Filter(x => x.Parent.ID).IsEqualTo(_manufacturingPacketID), new Columns( x => x.Station, //0 x => x.Sequence, //1 x => x.PercentageComplete, //2 x => x.ManufacturingSectionLink.Name, //3 x => x.QualityNotes //4 ), new SortOrder(x => x.Sequence, SortDirection.Ascending) ); } catch (Exception ex) { InABox.Mobile.MobileLogging.Log(ex); return null; } } private void AddDefaults() { ManufacturingPacketStageShell shell = new ManufacturingPacketStageShell() { PercentageComplete = "0%", ManufacturingSectionLinkName = "Progress", }; Device.BeginInvokeOnMainThread(() => { FactoryStageContentView factoryStageContentView = new FactoryStageContentView(shell); stageStackLayout.Children.Add(factoryStageContentView); }); } private void LoadAuditTrail() { Task.Run(() => { CoreTable table = QueryAuditTrail(); while (table == null) table = QueryAuditTrail(); if (table.Rows.Any()) { foreach (CoreRow row in table.Rows) { List list = row.Values; if (list[0] == null) { list[0] = DateTime.MinValue; } //0 if (list[1] == null) { list[1] = ""; } //1 if (list[2] == null) { list[2] = ""; } //2 AuditTrailShell shell = new AuditTrailShell() { Timestamp = DateTime.Parse(list[0].ToString()).ToString("dd MMM yy"), User = list[1].ToString(), Note = list[2].ToString() }; auditTrailShells.Add(shell); } Device.BeginInvokeOnMainThread(() => { auditListView.ItemsSource = auditTrailShells; }); } }); } private CoreTable QueryAuditTrail() { try { return new Client().Query( new Filter(x => x.EntityID).IsEqualTo(_manufacturingPacketID), new Columns( x => x.Timestamp, //0 x => x.User, //1 x => x.Note //2 ), new SortOrder(x => x.Timestamp, SortDirection.Ascending) ); } catch (Exception ex) { InABox.Mobile.MobileLogging.Log(ex); return null; } } } public class ManufacturingPacketStageShell { public int Station { get; set; } public long Sequence { get; set; } public string PercentageComplete { get; set; } public string ManufacturingSectionLinkName { get; set; } public ManufacturingPacketStageShell() { Station = 0; Sequence = 0; PercentageComplete = ""; ManufacturingSectionLinkName = ""; } } public class AuditTrailShell { public string Timestamp { get; set; } public string User { get; set; } public string Note { get; set; } public AuditTrailShell() { Timestamp = ""; User = ""; Note = ""; } } }