PurchaseOrderPage.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Configuration;
  9. using InABox.Core;
  10. using InABox.Mobile;
  11. using Plugin.Media;
  12. using Xamarin.Forms;
  13. using Xamarin.Forms.Xaml;
  14. using Xamarin.Forms;
  15. using Xamarin.Forms.Xaml;
  16. namespace comal.timesheets
  17. {
  18. [XamlCompilation(XamlCompilationOptions.Compile)]
  19. public partial class PurchaseOrderPage : BasePage
  20. {
  21. List<PurchaseOrderShell> purchaseOrderShells = new List<PurchaseOrderShell>();
  22. bool bLoading = true;
  23. List<string> filterOptions = new List<string>();
  24. bool firstLoad = true;
  25. public PurchaseOrderPage()
  26. {
  27. InitializeComponent();
  28. LoadList();
  29. }
  30. protected override void OnAppearing()
  31. {
  32. base.OnAppearing();
  33. if (!firstLoad)
  34. UpdateListWithNumberOfItems();
  35. }
  36. private async void LoadList()
  37. {
  38. await Task.Run(() =>
  39. {
  40. bLoading = true;
  41. purchaseOrderShells.Clear();
  42. filterOptions.Clear();
  43. filterOptions.Add("All");
  44. CoreTable table = QueryTable();
  45. while (table == null)
  46. table = QueryTable();
  47. if (table.Rows.Any())
  48. {
  49. foreach (CoreRow row in table.Rows)
  50. {
  51. List<object> list = row.Values;
  52. if (list[0] == null) { list[0] = Guid.Empty; } //0
  53. if (list[1] == null) { list[1] = ""; } //1
  54. if (list[2] == null) { list[2] = DateTime.MinValue; } //2
  55. if (list[3] == null) { list[3] = ""; } //3
  56. if (list[4] == null) { list[4] = Guid.Empty; } //4
  57. if (list[5] == null) { list[5] = ""; } //5
  58. if (list[6] == null) { list[6] = ""; } //6
  59. PurchaseOrderShell purchaseOrderShell = new PurchaseOrderShell()
  60. {
  61. ID = Guid.Parse(list[0].ToString()),
  62. PONumber = list[1].ToString(),
  63. DueDate = "Due: " + DateTime.Parse(list[2].ToString()).ToString("dd-MMMM-yy"),
  64. Status = list[3].ToString(),
  65. SupplierID = Guid.Parse(list[4].ToString()),
  66. SupplierName = list[5].ToString(),
  67. Notes = list[6].ToString()
  68. };
  69. if (!string.IsNullOrWhiteSpace(purchaseOrderShell.SupplierName))
  70. {
  71. if (!filterOptions.Contains(purchaseOrderShell.SupplierName))
  72. {
  73. filterOptions.Add(purchaseOrderShell.SupplierName);
  74. }
  75. }
  76. purchaseOrderShells.Add(purchaseOrderShell);
  77. }
  78. firstLoad = false;
  79. Device.BeginInvokeOnMainThread(() =>
  80. {
  81. searchEnt.Text = "";
  82. filterOptionsControl.Options = filterOptions;
  83. filterOptionsControl.CreateRadioButtonsAndSetDefault(filterOptions.First());
  84. purchaseOrderListView.ItemsSource = purchaseOrderShells;
  85. filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;
  86. bLoading = false;
  87. });
  88. UpdateListWithNumberOfItems();
  89. }
  90. });
  91. }
  92. private CoreTable QueryTable()
  93. {
  94. try
  95. {
  96. return new Client<PurchaseOrder>().Query
  97. (
  98. new Filter<PurchaseOrder>(x => x.ClosedDate).IsEqualTo(DateTime.MinValue),
  99. new Columns<PurchaseOrder>(
  100. x => x.ID, //0
  101. x => x.PONumber, //1
  102. x => x.DueDate, //2
  103. x => x.Status, //3
  104. x => x.SupplierLink.ID, //4
  105. x => x.SupplierName, //5
  106. x => x.Notes //6
  107. ),
  108. new SortOrder<PurchaseOrder>(x => x.DueDate, SortDirection.Descending)
  109. );
  110. }
  111. catch (Exception ex)
  112. {
  113. InABox.Mobile.MobileLogging.Log(ex);
  114. return null;
  115. }
  116. }
  117. private void UpdateListWithNumberOfItems()
  118. {
  119. Task.Run(() =>
  120. {
  121. List<PurchaseOrderShell> toAdd = new List<PurchaseOrderShell>();
  122. foreach (PurchaseOrderShell shell1 in purchaseOrderShells)
  123. {
  124. shell1.Items = 0;
  125. }
  126. CoreTable table = new Client<PurchaseOrderItem>().Query(new Filter<PurchaseOrderItem>(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue),
  127. new Columns<PurchaseOrderItem>(x => x.PurchaseOrderLink.ID));
  128. foreach (CoreRow row in table.Rows)
  129. {
  130. if (purchaseOrderShells.Contains(purchaseOrderShells.Find(x => x.ID == Guid.Parse(row.Values[0].ToString()))))
  131. {
  132. PurchaseOrderShell shell = purchaseOrderShells.Find(x => x.ID == Guid.Parse(row.Values[0].ToString()));
  133. shell.Items++;
  134. shell.NumberOfItems = "Number of Items Unreceived: " + shell.Items;
  135. if (!toAdd.Contains(shell))
  136. toAdd.Add(shell);
  137. }
  138. }
  139. purchaseOrderShells.Clear();
  140. foreach (PurchaseOrderShell shell2 in toAdd)
  141. {
  142. purchaseOrderShells.Add(shell2);
  143. }
  144. Device.BeginInvokeOnMainThread(() =>
  145. {
  146. purchaseOrderListView.ItemsSource = null;
  147. if (!string.IsNullOrWhiteSpace(searchEnt.Text))
  148. {
  149. RunSearch();
  150. }
  151. else
  152. {
  153. if (filterOptionsControl.CurrentOption == "All")
  154. {
  155. purchaseOrderListView.ItemsSource = purchaseOrderShells;
  156. listCountLbl.Text = "Number of items in list: " + purchaseOrderShells.Count;
  157. }
  158. else
  159. {
  160. var list = purchaseOrderShells.Where(x => x.SupplierName.Equals(filterOptionsControl.CurrentOption));
  161. purchaseOrderListView.ItemsSource = list;
  162. listCountLbl.Text = "Number of items in list: " + list.Count();
  163. }
  164. }
  165. });
  166. });
  167. }
  168. private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)
  169. {
  170. if (filterOption == filterOptionsControl.CurrentOption)
  171. return;
  172. bLoading = true;
  173. searchEnt.Text = "";
  174. bLoading = false;
  175. filterOptionsControl.CurrentOption = filterOption;
  176. if (filterOption == "All")
  177. {
  178. purchaseOrderListView.ItemsSource = purchaseOrderShells;
  179. listCountLbl.Text = "Number of items in list: " + purchaseOrderShells.Count;
  180. }
  181. else
  182. {
  183. var list = purchaseOrderShells.Where(x => x.SupplierName.Equals(filterOption));
  184. purchaseOrderListView.ItemsSource = list;
  185. listCountLbl.Text = "Number of items in list: " + list.Count();
  186. }
  187. }
  188. private async void PurchaseOrder_Clicked(object sender, EventArgs e)
  189. {
  190. PurchaseOrderShell purchaseOrderShell = purchaseOrderListView.SelectedItem as PurchaseOrderShell;
  191. Receivals receivalsPage = new Receivals(purchaseOrderShell.ID, purchaseOrderShell.PONumber, purchaseOrderShell.SupplierID);
  192. Navigation.PushAsync(receivalsPage);
  193. }
  194. private void SearchEnt_Changed(object sender, EventArgs e)
  195. {
  196. if (bLoading)
  197. return;
  198. if (!string.IsNullOrWhiteSpace(searchEnt.Text))
  199. {
  200. RunSearch();
  201. }
  202. else
  203. {
  204. listCountLbl.Text = "Number of items in list: " + purchaseOrderShells.Count;
  205. purchaseOrderListView.ItemsSource = purchaseOrderShells;
  206. }
  207. }
  208. private async void RunSearch()
  209. {
  210. await Task.Run(() =>
  211. {
  212. List<PurchaseOrderShell> searchList = new List<PurchaseOrderShell>();
  213. if (filterOptionsControl.CurrentOption == "All")
  214. {
  215. searchList = purchaseOrderShells;
  216. }
  217. else
  218. {
  219. var newList = purchaseOrderShells.Where(x => x.SupplierName.Equals(filterOptionsControl.CurrentOption));
  220. foreach (var shell in newList)
  221. {
  222. searchList.Add(shell);
  223. }
  224. }
  225. var list = searchList.Where(x =>
  226. x.PONumber.Contains(searchEnt.Text) || x.PONumber.Contains(UpperCaseFirst(searchEnt.Text)) || x.PONumber.Contains(searchEnt.Text.ToLower()) || x.PONumber.Contains(searchEnt.Text.ToUpper()) ||
  227. x.Notes.Contains(searchEnt.Text) || x.Notes.Contains(UpperCaseFirst(searchEnt.Text)) || x.Notes.Contains(searchEnt.Text.ToLower()) || x.Notes.Contains(searchEnt.Text.ToUpper()) ||
  228. x.SupplierName.Contains(searchEnt.Text) || x.SupplierName.Contains(UpperCaseFirst(searchEnt.Text)) || x.SupplierName.Contains(searchEnt.Text.ToLower()) || x.SupplierName.Contains(searchEnt.Text.ToUpper()) ||
  229. x.Status.Contains(searchEnt.Text) || x.Status.Contains(UpperCaseFirst(searchEnt.Text)) || x.Status.Contains(searchEnt.Text.ToLower()) || x.Status.Contains(searchEnt.Text.ToUpper()) ||
  230. x.DueDate.Contains(searchEnt.Text) || x.DueDate.Contains(UpperCaseFirst(searchEnt.Text)) || x.DueDate.Contains(searchEnt.Text.ToLower()) || x.DueDate.Contains(searchEnt.Text.ToUpper())
  231. );
  232. Device.BeginInvokeOnMainThread(() =>
  233. {
  234. listCountLbl.Text = "Number of items in list: " + list.Count();
  235. purchaseOrderListView.ItemsSource = list;
  236. });
  237. });
  238. }
  239. static String UpperCaseFirst(string s)
  240. {
  241. char[] a = s.ToCharArray();
  242. a[0] = char.ToUpper(a[0]);
  243. return new string(a);
  244. }
  245. }
  246. public class PurchaseOrderShell
  247. {
  248. public Guid ID { get; set; }
  249. public string PONumber { get; set; }
  250. public string DueDate { get; set; }
  251. public string Status { get; set; }
  252. public Guid SupplierID { get; set; }
  253. public string SupplierName { get; set; }
  254. public string Notes { get; set; }
  255. public int Items { get; set; }
  256. public string NumberOfItems { get; set; }
  257. public PurchaseOrderShell()
  258. {
  259. ID = Guid.Empty;
  260. PONumber = "";
  261. DueDate = "";
  262. Status = "";
  263. SupplierID = Guid.Empty;
  264. SupplierName = "";
  265. Notes = "";
  266. Items = 0;
  267. NumberOfItems = "";
  268. }
  269. }
  270. }