ConsignmentsGrid.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Windows.Media.Imaging;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.WPF;
  13. namespace PRSDesktop
  14. {
  15. internal class ConsignmentGrid : DynamicDataGrid<Consignment>
  16. {
  17. //Button receive = null;
  18. public ConsignmentGrid()
  19. {
  20. ActionColumns.Add(new DynamicImageColumn(ConsignmentImage, ShowBillOfLading) { Position = DynamicActionColumnPosition.Start });
  21. HiddenColumns.Add(x => x.BillOfLading.ID);
  22. HiddenColumns.Add(x => x.Closed);
  23. HiddenColumns.Add(x => x.Supplier.Code);
  24. }
  25. protected override void DoReconfigure(DynamicGridOptions options)
  26. {
  27. base.DoReconfigure(options);
  28. options.RecordCount = true;
  29. options.SelectColumns = true;
  30. options.FilterRows = true;
  31. }
  32. public bool ShowAll { get; set; }
  33. public Guid SelectedCategory { get; set; }
  34. public Guid SelectedType { get; set; }
  35. //private bool ReceiveConsignment(Button sender, CoreRow[] rows)
  36. //{
  37. // if (!rows.Any())
  38. // {
  39. // MessageBox.Show("Please select a row first");
  40. // return false;
  41. // }
  42. // if (!rows.First().Get<Consignment, DateTime>(x => x.Closed).IsEmpty())
  43. // {
  44. // MessageBox.Show("Consignment is already closed!");
  45. // return false;
  46. // }
  47. // Guid id = rows.First().Get<Consignment, Guid>(x => x.ID);
  48. // DateTime now = DateTime.Now;
  49. // using (new WaitCursor())
  50. // {
  51. // PurchaseOrderItem[] items = new Client<PurchaseOrderItem>().Load(new Filter<PurchaseOrderItem>(x => x.Consignment.ID).IsEqualTo(id).And(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue));
  52. // foreach (var item in items)
  53. // item.ReceivedDate = now;
  54. // new Client<PurchaseOrderItem>().Save(items, "Consignment Received");
  55. // //Consignment consignment = LoadItem(rows.First());
  56. // //consignment.Closed = now;
  57. // //new Client<Consignment>().Save(consignment, "Consignment Received");
  58. // }
  59. // return true;
  60. //}
  61. //protected override void SelectItems(CoreRow[] rows)
  62. //{
  63. // receive.IsEnabled = (rows != null) && rows.Any(r=>r.Get<Consignment, DateTime>(c => c.Closed).IsEmpty());
  64. // base.SelectItems(rows);
  65. //}
  66. protected override void Reload(
  67. Filters<Consignment> criteria, Columns<Consignment> columns, ref SortOrder<Consignment>? sort,
  68. CancellationToken token, Action<CoreTable?, Exception?> action)
  69. {
  70. if (!ShowAll)
  71. criteria.Add(new Filter<Consignment>(x => x.ActualWarehouseArrival).IsEqualTo(DateTime.MinValue));
  72. if (SelectedCategory != CoreUtils.FullGuid)
  73. criteria.Add(new Filter<Consignment>(x => x.Category.ID).IsEqualTo(SelectedCategory));
  74. if (SelectedType != CoreUtils.FullGuid)
  75. criteria.Add(new Filter<Consignment>(x => x.Type.ID).IsEqualTo(SelectedType));
  76. base.Reload(criteria, columns, ref sort, token, action);
  77. }
  78. private bool ShowBillOfLading(CoreRow? arg)
  79. {
  80. if(arg is null)
  81. {
  82. return false;
  83. }
  84. var id = arg.Get<Consignment, Guid>(x => x.BillOfLading.ID);
  85. if (!Entity.IsEntityLinkValid<Consignment, PDFDocumentLink>(x => x.BillOfLading, arg))
  86. return false;
  87. var doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  88. if (doc == null)
  89. {
  90. MessageBox.Show("Unable to Load Document");
  91. return false;
  92. }
  93. var ext = Path.GetExtension(doc.FileName);
  94. var tmpfile = Path.ChangeExtension(Path.GetTempFileName(), ext);
  95. File.WriteAllBytes(tmpfile, doc.Data);
  96. var gsProcessInfo = new ProcessStartInfo();
  97. gsProcessInfo.Verb = "open";
  98. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  99. gsProcessInfo.FileName = tmpfile;
  100. gsProcessInfo.UseShellExecute = true;
  101. Process.Start(gsProcessInfo);
  102. return false;
  103. }
  104. private BitmapImage? ConsignmentImage(CoreRow? arg)
  105. {
  106. if (arg == null)
  107. return PRSDesktop.Resources.doc_pdf.AsBitmapImage();
  108. var id = arg.Get<Consignment, Guid>(x => x.BillOfLading.ID);
  109. return !Entity.IsEntityLinkValid<Consignment, PDFDocumentLink>(x => x.BillOfLading, arg) ? null : PRSDesktop.Resources.doc_pdf.AsBitmapImage();
  110. }
  111. protected override void DoReconfigureEditors(DynamicEditorGrid grid, Consignment[] items)
  112. {
  113. base.DoReconfigureEditors(grid, items);
  114. var fc = grid.FindEditor(nameof(Consignment.ForeignCurrencyCost)) as CurrencyEditorControl;
  115. if (fc != null)
  116. {
  117. fc.SetEnabled(items.All(x => x.Supplier.Currency.ID != Guid.Empty));
  118. fc.CurrencySymbol = items.First().Supplier.Currency.Symbol;
  119. }
  120. var et = grid.FindEditor(nameof(Consignment.ExTax));
  121. if (et != null)
  122. {
  123. et.SetEnabled(items.All(x=>x.Supplier.Currency.ID == Guid.Empty));
  124. }
  125. }
  126. }
  127. }