PurchaseOrderDetails.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Xaml;
  9. namespace comal.timesheets
  10. {
  11. [XamlCompilation(XamlCompilationOptions.Compile)]
  12. public partial class PurchaseOrderDetails
  13. {
  14. List<PurchaseOrderRequiItemShell> shells = new List<PurchaseOrderRequiItemShell>();
  15. public PurchaseOrderDetails(PurchaseOrderShell purchaseOrderShell)
  16. {
  17. InitializeComponent();
  18. NavigationPage.SetHasBackButton(this, false);
  19. LoadList(purchaseOrderShell);
  20. }
  21. private void ExitBtn_Clicked(object sender, EventArgs e)
  22. {
  23. Navigation.PopAsync();
  24. }
  25. private void LoadList(PurchaseOrderShell purchaseOrderShell)
  26. {
  27. try
  28. {
  29. CoreTable table = DoPOItemQuery(purchaseOrderShell.ID);
  30. if (table.Rows.Any())
  31. GenerateShells(table);
  32. purchaseOrderItemListView.ItemsSource = shells;
  33. }
  34. catch { }
  35. }
  36. private void GenerateShells(CoreTable table)
  37. {
  38. foreach (CoreRow row in table.Rows)
  39. {
  40. shells.Add(CreateShell(row));
  41. }
  42. }
  43. private CoreTable DoPOItemQuery(Guid ID)
  44. {
  45. return new Client<PurchaseOrderItem>().Query(new Filter<PurchaseOrderItem>(x => x.PurchaseOrderLink.ID).IsEqualTo(ID),
  46. new Columns<PurchaseOrderItem>(
  47. x => x.ID,
  48. x => x.Job.JobNumber,
  49. x => x.Description,
  50. x => x.Qty,
  51. x => x.ReceivedDate
  52. )
  53. );
  54. }
  55. private PurchaseOrderRequiItemShell CreateShell(CoreRow row)
  56. {
  57. PurchaseOrderRequiItemShell shell = new PurchaseOrderRequiItemShell
  58. {
  59. ID = row.Get<PurchaseOrderItem, Guid>(x => x.ID),
  60. JobNumber = row.Get<PurchaseOrderItem, string>(x => x.Job.JobNumber),
  61. Description = row.Get<PurchaseOrderItem, string>(x => x.Description),
  62. Qty = row.Get<PurchaseOrderItem, double>(x => x.Qty).ToString()
  63. };
  64. if (row.Get<PurchaseOrderItem, DateTime>(x => x.ReceivedDate) != DateTime.MinValue)
  65. shell.ReceivedDate = (row.Get<PurchaseOrderItem, DateTime>(x => x.ReceivedDate)).ToString("dd MMM yy HH:mm");
  66. return shell;
  67. }
  68. private void PoItem_Tapped(object sender, EventArgs e)
  69. {
  70. PurchaseOrderRequiItemShell shell = purchaseOrderItemListView.SelectedItem as PurchaseOrderRequiItemShell;
  71. if (shell.RequiVisible)
  72. {
  73. var foundShell = shells.FirstOrDefault(x => x.ID == shell.ID);
  74. int index = shells.IndexOf(foundShell);
  75. foundShell.RequiVisible = false;
  76. foundShell.RequiRowHeight = 0;
  77. shells.RemoveAt(index);
  78. shells.Insert(index, foundShell);
  79. purchaseOrderItemListView.ItemsSource = null;
  80. purchaseOrderItemListView.ItemsSource = shells;
  81. }
  82. else
  83. {
  84. CoreTable table = new Client<JobRequisitionItem>().Query(new Filter<JobRequisitionItem>(x => x.PurchaseOrderItem.ID).IsEqualTo(shell.ID),
  85. new Columns<JobRequisitionItem>(x => x.ID, x => x.Qty, x => x.Requisition.Number, x => x.Product.Code, x => x.Product.Name)
  86. );
  87. if (table.Rows.Any())
  88. {
  89. CoreRow row = table.Rows.FirstOrDefault();
  90. var foundShell = shells.FirstOrDefault(x => x.ID == shell.ID);
  91. int index = shells.IndexOf(foundShell);
  92. foundShell.RequiNumber = "NO." + (row.Get<JobRequisitionItem, int>(x => x.Requisition.Number)).ToString();
  93. foundShell.RequiQty = (row.Get<JobRequisitionItem, double>(x => x.Qty)).ToString();
  94. foundShell.ProductCode = row.Get<JobRequisitionItem, string>(x => x.Product.Name);
  95. foundShell.RequiVisible = true;
  96. foundShell.RequiRowHeight = CalculateHeight(foundShell.ProductCode.Length);
  97. shells.RemoveAt(index);
  98. shells.Insert(index, foundShell);
  99. purchaseOrderItemListView.ItemsSource = null;
  100. purchaseOrderItemListView.ItemsSource = shells;
  101. }
  102. }
  103. }
  104. private int CalculateHeight(int calculatedHeight)
  105. {
  106. if (calculatedHeight > 25)
  107. calculatedHeight = 25;
  108. else if (calculatedHeight <= 25 && calculatedHeight < 50)
  109. calculatedHeight = 50;
  110. else if (calculatedHeight <= 50 && calculatedHeight < 75)
  111. calculatedHeight = 75;
  112. calculatedHeight = calculatedHeight + 60;
  113. return calculatedHeight;
  114. }
  115. }
  116. public class PurchaseOrderRequiItemShell
  117. {
  118. public Guid ID { get; set; }
  119. public string JobNumber { get; set; }
  120. public string Description { get; set; }
  121. public string Qty { get; set; }
  122. public string ReceivedDate { get; set; }
  123. public int RequiRowHeight { get; set; }
  124. public bool RequiVisible { get; set; }
  125. public string RequiNumber { get; set; }
  126. public string ProductCode { get; set; }
  127. public string RequiQty { get; set; }
  128. public PurchaseOrderRequiItemShell()
  129. {
  130. ID = Guid.Empty;
  131. JobNumber = "";
  132. Description = "";
  133. Qty = "";
  134. ReceivedDate = "";
  135. RequiRowHeight = 0;
  136. RequiVisible = false;
  137. RequiNumber = "";
  138. ProductCode = "";
  139. RequiQty = "";
  140. }
  141. }
  142. }