NewHoldingPage.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Threading.Tasks;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using Xamarin.Forms;
  11. using XF.Material.Forms.UI;
  12. using XF.Material.Forms.UI.Dialogs;
  13. namespace PRS.Mobile
  14. {
  15. public delegate void SaveHoldingEvent(StockHolding holding, Document image);
  16. public partial class NewHoldingPage
  17. {
  18. public event SaveHoldingEvent OnSaveHolding;
  19. bool firstLoad = true;
  20. private Product _product = null;
  21. private Document _image = null;
  22. private ProductStyle _style = null;
  23. private Job _job = null;
  24. private ProductUOM _uom = null;
  25. private double _unitsize = 1.0F;
  26. private async Task<bool> CheckData()
  27. {
  28. if (_product == null)
  29. {
  30. await MaterialDialog.Instance.AlertAsync("Please select a Product before continuing!");
  31. return false;
  32. }
  33. if (_style == null)
  34. {
  35. await MaterialDialog.Instance.AlertAsync("Please select a Style before continuing!");
  36. return false;
  37. }
  38. if (_uom == null)
  39. {
  40. await MaterialDialog.Instance.AlertAsync("Please select a Unit Size before continuing!");
  41. return false;
  42. }
  43. if (_unitsize <= 0.0F)
  44. {
  45. await MaterialDialog.Instance.AlertAsync("Unit Size must be greater than zero!");
  46. return false;
  47. }
  48. return true;
  49. }
  50. public NewHoldingPage(Job job)
  51. {
  52. firstLoad = true;
  53. try
  54. {
  55. _job = new Job();
  56. _job.ID = job.ID;
  57. _job.JobNumber = job.JobNumber;
  58. _job.Name = job.Name;
  59. }
  60. catch { }
  61. InitializeComponent();
  62. ToolbarItems.Clear();
  63. ToolbarItems.Add(new ToolbarItem("Save", "", () =>
  64. {
  65. if (!CheckData().Result)
  66. return;
  67. StockHolding holding = new StockHolding();
  68. holding.Product.ID = _product.ID;
  69. holding.Product.Code = _product.Code;
  70. holding.Product.Name = _product.Name;
  71. holding.Product.Image.ID = _product.Image.ID;
  72. holding.Style.ID = _style.ID;
  73. holding.Style.Code = _style.Code;
  74. holding.Style.Description = _style.Description;
  75. holding.Job.ID = _job != null ? _job.ID : Guid.Empty;
  76. holding.Job.JobNumber = _job != null ? _job.JobNumber : "";
  77. holding.Job.Name = _job != null ? _job.Name : "";
  78. OnSaveHolding?.Invoke(holding, _image);
  79. Navigation.PopAsync();
  80. }));
  81. if(_job.ID != Guid.Empty) RefreshPage();
  82. firstLoad = false;
  83. }
  84. protected override void OnAppearing()
  85. {
  86. base.OnAppearing();
  87. if(!firstLoad) RefreshPage();
  88. }
  89. private void UpdateLabel<T>(MaterialLabel label, T item, params Expression<Func<T,String>>[] values)
  90. {
  91. List<String> data = new List<string>();
  92. if (item != null)
  93. {
  94. foreach (var value in values)
  95. data.Add(value.Compile().Invoke(item));
  96. }
  97. label.Text = String.Join(": ",data);
  98. label.IsVisible = !String.IsNullOrWhiteSpace(label.Text);
  99. }
  100. void RefreshPage()
  101. {
  102. ImageSource source = _image != null ? ImageSource.FromStream(() => new MemoryStream(_image.Data)) : null;
  103. Image.Source = source;
  104. NoImage.IsVisible = (_product != null) && (_image == null);
  105. Image.IsVisible = (_product != null) && (_image != null);
  106. UpdateLabel<Product>(ProductCode, _product, x => x.Code);
  107. UpdateLabel<Product>(ProductDescription, _product, x => x.Name);
  108. UpdateLabel<ProductStyle>(StyleDescription, _style, x => x.Description);
  109. UpdateLabel<Job>(Job, _job, x => x.JobNumber, x => x.Name);
  110. UpdateLabel<ProductUOM>(UnitCode, _uom, x => x.Code);
  111. Size.Text = _unitsize.ToString();
  112. }
  113. void SelectProduct_Clicked(System.Object sender, System.EventArgs e)
  114. {
  115. GenericSelectionPage page = new GenericSelectionPage(
  116. "Select Product",
  117. new SelectionViewModel<Product>(
  118. new Filter<Product>(X=>X.Expired).IsEqualTo(DateTime.MinValue),
  119. new Expression<Func<Product, object>>[] { X => X.Code, X => X.Name },
  120. 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 },
  121. new SortOrder<Product>(x => x.Code)
  122. )
  123. );
  124. page.OnItemSelected += (o,e) => {
  125. _product = e.Row.ToObject<Product>();
  126. _style = new ProductStyle()
  127. {
  128. ID = _product.DefaultStyle.ID,
  129. Code = _product.DefaultStyle.Code,
  130. Description = _product.DefaultStyle.Description
  131. };
  132. _uom = new ProductUOM()
  133. {
  134. ID = _product.Units.ID,
  135. Code = _product.Units.Code,
  136. Description = _product.Units.Description
  137. };
  138. _image = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(_product.Image.ID)).FirstOrDefault();
  139. };
  140. Navigation.PushAsync(page);
  141. }
  142. void SelectStyle_Clicked(System.Object sender, System.EventArgs e)
  143. {
  144. GenericSelectionPage page = new GenericSelectionPage(
  145. "Select Product Style",
  146. new SelectionViewModel<ProductStyle>(
  147. null,
  148. new Expression<Func<ProductStyle, object>>[] { x => x.Code, x => x.Description },
  149. null,
  150. new SortOrder<ProductStyle>(x => x.Code)
  151. )
  152. );
  153. page.OnItemSelected += (o,e) => {
  154. _style = e.Row.ToObject<ProductStyle>();
  155. };
  156. Navigation.PushAsync(page);
  157. }
  158. void SelectJob_Clicked(System.Object sender, System.EventArgs e)
  159. {
  160. JobSelectionPage jobSelectionPage = new JobSelectionPage(
  161. (job) =>
  162. {
  163. _job.ID = job.ID;
  164. _job.Name = job.Name;
  165. _job.JobNumber = job.JobNumber;
  166. }
  167. );
  168. Navigation.PushAsync(jobSelectionPage);
  169. }
  170. void SelectUnits_Clicked(System.Object sender, System.EventArgs e)
  171. {
  172. GenericSelectionPage page = new GenericSelectionPage(
  173. "Select UOM",
  174. new SelectionViewModel<ProductUOM>(
  175. null,
  176. new Expression<Func<ProductUOM, object>>[] { x=>x.Code, x => x.Description },
  177. null,
  178. new SortOrder<ProductUOM>(x => x.Description)
  179. )
  180. );
  181. page.OnItemSelected += (o,e) => {
  182. _uom = e.Row.ToObject<ProductUOM>();
  183. };
  184. Navigation.PushAsync(page);
  185. }
  186. void SmallerSize_Clicked(System.Object sender, System.EventArgs e)
  187. {
  188. _unitsize = Math.Max(_unitsize - 1.0F, 0.0F);
  189. Size.Text = _unitsize.ToString();
  190. }
  191. void LargerSize_Clicked(System.Object sender, System.EventArgs e)
  192. {
  193. _unitsize += 1.0F;
  194. Size.Text = _unitsize.ToString();
  195. }
  196. void Size_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
  197. {
  198. if (double.TryParse(Size.Text, out double size))
  199. _unitsize = size;
  200. }
  201. }
  202. }