using Comal.Classes; using InABox.Clients; using InABox.Core; using iText.StyledXmlParser.Node; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class PurchaseOrderDetails : ContentPage { List shells = new List(); public PurchaseOrderDetails(PurchaseOrderShell purchaseOrderShell) { InitializeComponent(); NavigationPage.SetHasBackButton(this, false); LoadList(purchaseOrderShell); } private void ExitBtn_Clicked(object sender, EventArgs e) { Navigation.PopAsync(); } private void LoadList(PurchaseOrderShell purchaseOrderShell) { try { CoreTable table = DoPOItemQuery(purchaseOrderShell.ID); if (table.Rows.Any()) GenerateShells(table); purchaseOrderItemListView.ItemsSource = shells; } catch { } } private void GenerateShells(CoreTable table) { foreach (CoreRow row in table.Rows) { shells.Add(CreateShell(row)); } } private CoreTable DoPOItemQuery(Guid ID) { return new Client().Query(new Filter(x => x.PurchaseOrderLink.ID).IsEqualTo(ID), new Columns( x => x.ID, x => x.Job.JobNumber, x => x.Description, x => x.Qty, x => x.ReceivedDate ) ); } private PurchaseOrderRequiItemShell CreateShell(CoreRow row) { PurchaseOrderRequiItemShell shell = new PurchaseOrderRequiItemShell { ID = row.Get(x => x.ID), JobNumber = row.Get(x => x.Job.JobNumber), Description = row.Get(x => x.Description), Qty = row.Get(x => x.Qty).ToString() }; if (row.Get(x => x.ReceivedDate) != DateTime.MinValue) shell.ReceivedDate = (row.Get(x => x.ReceivedDate)).ToString("dd MMM yy HH:mm"); return shell; } private void PoItem_Tapped(object sender, EventArgs e) { PurchaseOrderRequiItemShell shell = purchaseOrderItemListView.SelectedItem as PurchaseOrderRequiItemShell; if (shell.RequiVisible) { var foundShell = shells.FirstOrDefault(x => x.ID == shell.ID); int index = shells.IndexOf(foundShell); foundShell.RequiVisible = false; foundShell.RequiRowHeight = 0; shells.RemoveAt(index); shells.Insert(index, foundShell); purchaseOrderItemListView.ItemsSource = null; purchaseOrderItemListView.ItemsSource = shells; } else { CoreTable table = new Client().Query(new Filter(x => x.PurchaseOrderItem.ID).IsEqualTo(shell.ID), new Columns(x => x.ID, x => x.Qty, x => x.Requisition.Number, x => x.Product.Code, x => x.Product.Name) ); if (table.Rows.Any()) { CoreRow row = table.Rows.FirstOrDefault(); var foundShell = shells.FirstOrDefault(x => x.ID == shell.ID); int index = shells.IndexOf(foundShell); foundShell.RequiNumber = "NO." + (row.Get(x => x.Requisition.Number)).ToString(); foundShell.RequiQty = (row.Get(x => x.Qty)).ToString(); foundShell.ProductCode = row.Get(x => x.Product.Name); foundShell.RequiVisible = true; foundShell.RequiRowHeight = CalculateHeight(foundShell.ProductCode.Length); shells.RemoveAt(index); shells.Insert(index, foundShell); purchaseOrderItemListView.ItemsSource = null; purchaseOrderItemListView.ItemsSource = shells; } } } private int CalculateHeight(int calculatedHeight) { if (calculatedHeight > 25) calculatedHeight = 25; else if (calculatedHeight <= 25 && calculatedHeight < 50) calculatedHeight = 50; else if (calculatedHeight <= 50 && calculatedHeight < 75) calculatedHeight = 75; calculatedHeight = calculatedHeight + 60; return calculatedHeight; } } public class PurchaseOrderRequiItemShell { public Guid ID { get; set; } public string JobNumber { get; set; } public string Description { get; set; } public string Qty { get; set; } public string ReceivedDate { get; set; } public int RequiRowHeight { get; set; } public bool RequiVisible { get; set; } public string RequiNumber { get; set; } public string ProductCode { get; set; } public string RequiQty { get; set; } public PurchaseOrderRequiItemShell() { ID = Guid.Empty; JobNumber = ""; Description = ""; Qty = ""; ReceivedDate = ""; RequiRowHeight = 0; RequiVisible = false; RequiNumber = ""; ProductCode = ""; RequiQty = ""; } } }