ManufacturingPacketPopup.xaml.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using comal.timesheets.Manufacturing;
  2. using Comal.Classes;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. namespace comal.timesheets
  12. {
  13. [XamlCompilation(XamlCompilationOptions.Compile)]
  14. public partial class ManufacturingPacketPopup
  15. {
  16. Guid _manufacturingPacketID = Guid.Empty;
  17. List<ManufacturingPacketStageShell> manufacturingPacketShells = new List<ManufacturingPacketStageShell>();
  18. List<AuditTrailShell> auditTrailShells = new List<AuditTrailShell>();
  19. public ManufacturingPacketPopup(Guid manufacturingpacketlinkID, Guid orderID)
  20. {
  21. InitializeComponent();
  22. _manufacturingPacketID = manufacturingpacketlinkID;
  23. LoadStages();
  24. LoadAuditTrail();
  25. if (orderID != Guid.Empty)
  26. LoadOrder(orderID);
  27. }
  28. private void LoadOrder(Guid orderID)
  29. {
  30. Task.Run(() =>
  31. {
  32. CoreTable table = QueryPurchaseOrderItem(orderID);
  33. while (table == null)
  34. table = QueryPurchaseOrderItem(orderID);
  35. if (table.Rows.Any())
  36. {
  37. var poItem = table.Rows.FirstOrDefault().ToObject<PurchaseOrderItem>();
  38. string dueDate = poItem.Consignment.EstimatedWarehouseArrival.ToString("dd MMM yy");
  39. string supplierName = poItem.PurchaseOrderLink.SupplierLink.Name;
  40. string recDate = poItem.ReceivedDate.ToString("dd MMM yy");
  41. if (poItem.ReceivedDate != DateTime.MinValue) //item has been received back to comal
  42. {
  43. Device.BeginInvokeOnMainThread(() =>
  44. {
  45. ETAFrame.IsVisible = true;
  46. ETALbl.Text = "Item received from " + supplierName + " on " + recDate;
  47. });
  48. }
  49. else
  50. {
  51. Device.BeginInvokeOnMainThread(() =>
  52. {
  53. ETAFrame.IsVisible = true;
  54. ETALbl.Text = "Item at " + supplierName + ", ETA: " + dueDate
  55. + System.Environment.NewLine + "(ETAs are auto-generated one week from date of purchase order creation)";
  56. });
  57. }
  58. }
  59. });
  60. }
  61. private CoreTable QueryPurchaseOrderItem(Guid orderID)
  62. {
  63. try
  64. {
  65. return new Client<PurchaseOrderItem>().Query
  66. (
  67. new Filter<PurchaseOrderItem>(x => x.ID).IsEqualTo(orderID),
  68. new Columns<PurchaseOrderItem>(x => x.Consignment.EstimatedWarehouseArrival,
  69. x => x.PurchaseOrderLink.SupplierLink.Name,
  70. x => x.ReceivedDate)
  71. );
  72. }
  73. catch (Exception ex)
  74. {
  75. InABox.Mobile.MobileLogging.Log(ex);
  76. return null;
  77. }
  78. }
  79. private void LoadStages()
  80. {
  81. Task.Run(() =>
  82. {
  83. CoreTable table = QueryStages();
  84. while (table == null)
  85. table = QueryStages();
  86. if (table.Rows.Any())
  87. {
  88. foreach (CoreRow row in table.Rows)
  89. {
  90. List<object> list = row.Values;
  91. if (list[2] == null) { list[2] = 0.0; } //2
  92. if (list[3] == null) { list[3] = ""; } //3
  93. ManufacturingPacketStageShell shell = new ManufacturingPacketStageShell()
  94. {
  95. PercentageComplete = list[2].ToString() + "%",
  96. ManufacturingSectionLinkName = list[3].ToString(),
  97. };
  98. Device.BeginInvokeOnMainThread(() =>
  99. {
  100. FactoryStageContentView factoryStageContentView = new FactoryStageContentView(shell);
  101. stageStackLayout.Children.Add(factoryStageContentView);
  102. });
  103. }
  104. }
  105. else
  106. {
  107. AddDefaults();
  108. }
  109. });
  110. }
  111. private CoreTable QueryStages()
  112. {
  113. try
  114. {
  115. return new Client<ManufacturingPacketStage>().Query(
  116. new Filter<ManufacturingPacketStage>(x => x.Parent.ID).IsEqualTo(_manufacturingPacketID),
  117. new Columns<ManufacturingPacketStage>(
  118. x => x.Station, //0
  119. x => x.Sequence, //1
  120. x => x.PercentageComplete, //2
  121. x => x.ManufacturingSectionLink.Name, //3
  122. x => x.QualityNotes //4
  123. ),
  124. new SortOrder<ManufacturingPacketStage>(x => x.Sequence, SortDirection.Ascending)
  125. );
  126. }
  127. catch (Exception ex)
  128. {
  129. InABox.Mobile.MobileLogging.Log(ex);
  130. return null;
  131. }
  132. }
  133. private void AddDefaults()
  134. {
  135. ManufacturingPacketStageShell shell = new ManufacturingPacketStageShell()
  136. {
  137. PercentageComplete = "0%",
  138. ManufacturingSectionLinkName = "Progress",
  139. };
  140. Device.BeginInvokeOnMainThread(() =>
  141. {
  142. FactoryStageContentView factoryStageContentView = new FactoryStageContentView(shell);
  143. stageStackLayout.Children.Add(factoryStageContentView);
  144. });
  145. }
  146. private void LoadAuditTrail()
  147. {
  148. Task.Run(() =>
  149. {
  150. CoreTable table = QueryAuditTrail();
  151. while (table == null)
  152. table = QueryAuditTrail();
  153. if (table.Rows.Any())
  154. {
  155. foreach (CoreRow row in table.Rows)
  156. {
  157. List<object> list = row.Values;
  158. if (list[0] == null) { list[0] = DateTime.MinValue; } //0
  159. if (list[1] == null) { list[1] = ""; } //1
  160. if (list[2] == null) { list[2] = ""; } //2
  161. AuditTrailShell shell = new AuditTrailShell()
  162. {
  163. Timestamp = DateTime.Parse(list[0].ToString()).ToString("dd MMM yy"),
  164. User = list[1].ToString(),
  165. Note = list[2].ToString()
  166. };
  167. auditTrailShells.Add(shell);
  168. }
  169. Device.BeginInvokeOnMainThread(() =>
  170. {
  171. auditListView.ItemsSource = auditTrailShells;
  172. });
  173. }
  174. });
  175. }
  176. private CoreTable QueryAuditTrail()
  177. {
  178. try
  179. {
  180. return new Client<AuditTrail>().Query(
  181. new Filter<AuditTrail>(x => x.EntityID).IsEqualTo(_manufacturingPacketID),
  182. new Columns<AuditTrail>(
  183. x => x.Timestamp, //0
  184. x => x.User, //1
  185. x => x.Note //2
  186. ),
  187. new SortOrder<AuditTrail>(x => x.Timestamp, SortDirection.Ascending)
  188. );
  189. }
  190. catch (Exception ex)
  191. {
  192. InABox.Mobile.MobileLogging.Log(ex);
  193. return null;
  194. }
  195. }
  196. }
  197. public class ManufacturingPacketStageShell
  198. {
  199. public int Station { get; set; }
  200. public long Sequence { get; set; }
  201. public string PercentageComplete { get; set; }
  202. public string ManufacturingSectionLinkName { get; set; }
  203. public ManufacturingPacketStageShell()
  204. {
  205. Station = 0;
  206. Sequence = 0;
  207. PercentageComplete = "";
  208. ManufacturingSectionLinkName = "";
  209. }
  210. }
  211. public class AuditTrailShell
  212. {
  213. public string Timestamp { get; set; }
  214. public string User { get; set; }
  215. public string Note { get; set; }
  216. public AuditTrailShell()
  217. {
  218. Timestamp = "";
  219. User = "";
  220. Note = "";
  221. }
  222. }
  223. }