PurchaseOrderPage.xaml.cs 12 KB

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