using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using Comal.Classes; using InABox.Clients; using InABox.Core; using Xamarin.Forms; using XF.Material.Forms.UI; using XF.Material.Forms.UI.Dialogs; namespace PRS.Mobile { public delegate void SaveHoldingEvent(StockHolding holding, Document image); public partial class NewHoldingPage { public event SaveHoldingEvent OnSaveHolding; bool firstLoad = true; private Product _product = null; private Document _image = null; private ProductStyle _style = null; private Job _job = null; private ProductUOM _uom = null; private double _unitsize = 1.0F; private async Task CheckData() { if (_product == null) { await MaterialDialog.Instance.AlertAsync("Please select a Product before continuing!"); return false; } if (_style == null) { await MaterialDialog.Instance.AlertAsync("Please select a Style before continuing!"); return false; } if (_uom == null) { await MaterialDialog.Instance.AlertAsync("Please select a Unit Size before continuing!"); return false; } if (_unitsize <= 0.0F) { await MaterialDialog.Instance.AlertAsync("Unit Size must be greater than zero!"); return false; } return true; } public NewHoldingPage(Job job) { firstLoad = true; try { _job = new Job(); _job.ID = job.ID; _job.JobNumber = job.JobNumber; _job.Name = job.Name; } catch { } InitializeComponent(); ToolbarItems.Clear(); ToolbarItems.Add(new ToolbarItem("Save", "", () => { if (!CheckData().Result) return; StockHolding holding = new StockHolding(); holding.Product.ID = _product.ID; holding.Product.Code = _product.Code; holding.Product.Name = _product.Name; holding.Product.Image.ID = _product.Image.ID; holding.Style.ID = _style.ID; holding.Style.Code = _style.Code; holding.Style.Description = _style.Description; holding.Job.ID = _job != null ? _job.ID : Guid.Empty; holding.Job.JobNumber = _job != null ? _job.JobNumber : ""; holding.Job.Name = _job != null ? _job.Name : ""; OnSaveHolding?.Invoke(holding, _image); Navigation.PopAsync(); })); if(_job.ID != Guid.Empty) RefreshPage(); firstLoad = false; } protected override void OnAppearing() { base.OnAppearing(); if(!firstLoad) RefreshPage(); } private void UpdateLabel(MaterialLabel label, T item, params Expression>[] values) { List data = new List(); if (item != null) { foreach (var value in values) data.Add(value.Compile().Invoke(item)); } label.Text = String.Join(": ",data); label.IsVisible = !String.IsNullOrWhiteSpace(label.Text); } void RefreshPage() { ImageSource source = _image != null ? ImageSource.FromStream(() => new MemoryStream(_image.Data)) : null; Image.Source = source; NoImage.IsVisible = (_product != null) && (_image == null); Image.IsVisible = (_product != null) && (_image != null); UpdateLabel(ProductCode, _product, x => x.Code); UpdateLabel(ProductDescription, _product, x => x.Name); UpdateLabel(StyleDescription, _style, x => x.Description); UpdateLabel(Job, _job, x => x.JobNumber, x => x.Name); UpdateLabel(UnitCode, _uom, x => x.Code); Size.Text = _unitsize.ToString(); } void SelectProduct_Clicked(System.Object sender, System.EventArgs e) { GenericSelectionPage page = new GenericSelectionPage( "Select Product", new SelectionViewModel( new Filter(X=>X.Expired).IsEqualTo(DateTime.MinValue), new Expression>[] { X => X.Code, X => X.Name }, new Expression>[] { x=>x.Image.ID, x=>x.Units.ID, x=>x.Units.Code, x=>x.Units.Description, x=>x.DefaultStyle.ID, x=>x.DefaultStyle.Code, x=>x.DefaultStyle.Description }, new SortOrder(x => x.Code) ) ); page.OnItemSelected += (o,e) => { _product = e.Row.ToObject(); _style = new ProductStyle() { ID = _product.DefaultStyle.ID, Code = _product.DefaultStyle.Code, Description = _product.DefaultStyle.Description }; _uom = new ProductUOM() { ID = _product.Units.ID, Code = _product.Units.Code, Description = _product.Units.Description }; _image = new Client().Load(new Filter(x => x.ID).IsEqualTo(_product.Image.ID)).FirstOrDefault(); }; Navigation.PushAsync(page); } void SelectStyle_Clicked(System.Object sender, System.EventArgs e) { GenericSelectionPage page = new GenericSelectionPage( "Select Product Style", new SelectionViewModel( null, new Expression>[] { x => x.Code, x => x.Description }, null, new SortOrder(x => x.Code) ) ); page.OnItemSelected += (o,e) => { _style = e.Row.ToObject(); }; Navigation.PushAsync(page); } void SelectJob_Clicked(System.Object sender, System.EventArgs e) { JobSelectionPage jobSelectionPage = new JobSelectionPage( (job) => { _job.ID = job.ID; _job.Name = job.Name; _job.JobNumber = job.JobNumber; } ); Navigation.PushAsync(jobSelectionPage); } void SelectUnits_Clicked(System.Object sender, System.EventArgs e) { GenericSelectionPage page = new GenericSelectionPage( "Select UOM", new SelectionViewModel( null, new Expression>[] { x=>x.Code, x => x.Description }, null, new SortOrder(x => x.Description) ) ); page.OnItemSelected += (o,e) => { _uom = e.Row.ToObject(); }; Navigation.PushAsync(page); } void SmallerSize_Clicked(System.Object sender, System.EventArgs e) { _unitsize = Math.Max(_unitsize - 1.0F, 0.0F); Size.Text = _unitsize.ToString(); } void LargerSize_Clicked(System.Object sender, System.EventArgs e) { _unitsize += 1.0F; Size.Text = _unitsize.ToString(); } void Size_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e) { if (double.TryParse(Size.Text, out double size)) _unitsize = size; } } }