CostSheetKitGrid.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. namespace PRSDesktop
  12. {
  13. internal class CostSheetKitGrid : DynamicDataGrid<CostSheetKit>
  14. {
  15. public CostSheetKitGrid()
  16. {
  17. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows, DynamicGridOption.EditRows,
  18. DynamicGridOption.FilterRows, DynamicGridOption.SelectColumns, DynamicGridOption.MultiSelect, DynamicGridOption.ImportData);
  19. AddButton("Update Sections", PRSDesktop.Resources.kitgroup.AsBitmapImage(), UpdateSections);
  20. AddButton("Show Items", PRSDesktop.Resources.product.AsBitmapImage(), ShowItems);
  21. HiddenColumns.Add(x => x.LastUpdate);
  22. HiddenColumns.Add(x => x.CostSheet.ID);
  23. HiddenColumns.Add(x => x.Kit.ID);
  24. HiddenColumns.Add(x => x.Section.ID);
  25. }
  26. public ICostSheet CostSheet { get; set; }
  27. private bool ShowItems(Button sender, CoreRow[] rows)
  28. {
  29. if (rows == null || rows.Length != 1)
  30. {
  31. MessageBox.Show("Please select a single row!");
  32. return false;
  33. }
  34. var kitid = rows.First().Get<CostSheetKit, Guid>(x => x.Kit.ID);
  35. var msd = new MultiSelectDialog<KitProduct>(
  36. new Filter<KitProduct>(x => x.Kit.ID).IsEqualTo(kitid),
  37. new Columns<KitProduct>()
  38. );
  39. msd.ShowDialog();
  40. return false;
  41. }
  42. private bool UpdateSections(Button arg1, CoreRow[] arg2)
  43. {
  44. using (new WaitCursor())
  45. {
  46. // Save Existing Section/Kit values
  47. var values = new Dictionary<Guid, List<Guid>>();
  48. foreach (var row in Data.Rows)
  49. {
  50. var sectionid = row.Get<CostSheetKit, Guid>(x => x.Section.ID);
  51. if (!values.ContainsKey(sectionid))
  52. values[sectionid] = new List<Guid>();
  53. values[sectionid].Add(row.Get<CostSheetKit, Guid>(x => x.Kit.ID));
  54. var kit = new CostSheetKit { ID = row.Get<CostSheetKit, Guid>(x => x.ID) };
  55. new Client<CostSheetKit>().Delete(kit, "");
  56. }
  57. var sections = new Client<CostSheetSection>().Query(
  58. null,
  59. null,
  60. new SortOrder<CostSheetSection>(x => x.Sequence)
  61. );
  62. var updates = new List<CostSheetKit>();
  63. foreach (var row in sections.Rows)
  64. {
  65. var sectionid = row.Get<CostSheetSection, Guid>(x => x.ID);
  66. var iCount = row.Get<CostSheetSection, int>(x => x.Quantity);
  67. if (values.ContainsKey(sectionid) && iCount < values[sectionid].Count)
  68. iCount = values[sectionid].Count;
  69. for (var i = 0; i < iCount; i++)
  70. {
  71. var kit = new CostSheetKit();
  72. kit.CostSheet.ID = CostSheet != null ? CostSheet.ID : Guid.Empty;
  73. kit.Section.ID = sectionid;
  74. kit.Sequence = row.Get<CostSheetSection, long>(x => x.Sequence);
  75. if (values.ContainsKey(sectionid) && i < values[sectionid].Count)
  76. kit.Kit.ID = values[sectionid][i];
  77. updates.Add(kit);
  78. }
  79. }
  80. new Client<CostSheetKit>().Save(updates, "");
  81. }
  82. return true;
  83. }
  84. protected override void Reload(Filters<CostSheetKit> criteria, Columns<CostSheetKit> columns, ref SortOrder<CostSheetKit> sort,
  85. Action<CoreTable, Exception> action)
  86. {
  87. criteria.Add(new Filter<CostSheetKit>(x => x.CostSheet.ID).IsEqualTo(CostSheet != null ? CostSheet.ID : CoreUtils.FullGuid));
  88. base.Reload(criteria, columns, ref sort, action);
  89. }
  90. protected override CostSheetKit CreateItem()
  91. {
  92. var result = base.CreateItem();
  93. result.CostSheet.ID = CostSheet != null ? CostSheet.ID : Guid.Empty;
  94. return result;
  95. }
  96. protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
  97. {
  98. if (column.ColumnName.Equals("CostSheet.ID"))
  99. return new NullEditor();
  100. return base.GetEditor(item, column);
  101. }
  102. protected override void CustomiseExportColumns(List<string> columnnames)
  103. {
  104. base.CustomiseExportColumns(columnnames);
  105. var costsheet = CoreUtils.GetFullPropertyName<CostSheetKit, string>(x => x.CostSheet.Code, ".");
  106. if (columnnames.Contains(costsheet))
  107. columnnames.Remove(costsheet);
  108. }
  109. protected override string CustomiseExportFileName(string filename)
  110. {
  111. return CostSheet != null ? CostSheet.Code : base.CustomiseExportFileName(filename);
  112. }
  113. protected override string CustomiseImportFileName(string filename)
  114. {
  115. return CostSheet != null ? CostSheet.Code : base.CustomiseExportFileName(filename);
  116. }
  117. protected override bool CustomiseImportItem(CostSheetKit item)
  118. {
  119. if (CostSheet == null)
  120. return false;
  121. var result = base.CustomiseImportItem(item);
  122. if (result)
  123. item.CostSheet.ID = CostSheet.ID;
  124. return result;
  125. }
  126. }
  127. }