| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- 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<bool> 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<T>(MaterialLabel label, T item, params Expression<Func<T,String>>[] values)
- {
- List<String> data = new List<string>();
- 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<Product>(ProductCode, _product, x => x.Code);
- UpdateLabel<Product>(ProductDescription, _product, x => x.Name);
- UpdateLabel<ProductStyle>(StyleDescription, _style, x => x.Description);
- UpdateLabel<Job>(Job, _job, x => x.JobNumber, x => x.Name);
- UpdateLabel<ProductUOM>(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<Product>(
- new Filter<Product>(X=>X.Expired).IsEqualTo(DateTime.MinValue),
- new Expression<Func<Product, object>>[] { X => X.Code, X => X.Name },
- new Expression<Func<Product, object>>[] { 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<Product>(x => x.Code)
- )
- );
- page.OnItemSelected += (o,e) => {
- _product = e.Row.ToObject<Product>();
- _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<Document>().Load(new Filter<Document>(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<ProductStyle>(
- null,
- new Expression<Func<ProductStyle, object>>[] { x => x.Code, x => x.Description },
- null,
- new SortOrder<ProductStyle>(x => x.Code)
- )
- );
- page.OnItemSelected += (o,e) => {
- _style = e.Row.ToObject<ProductStyle>();
- };
- 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<ProductUOM>(
- null,
- new Expression<Func<ProductUOM, object>>[] { x=>x.Code, x => x.Description },
- null,
- new SortOrder<ProductUOM>(x => x.Description)
- )
- );
- page.OnItemSelected += (o,e) => {
- _uom = e.Row.ToObject<ProductUOM>();
- };
- 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;
-
- }
- }
- }
|