StockMovementGrid.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  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. public class StockMovementGrid : DynamicDataGrid<StockMovement>, IDataModelSource
  16. {
  17. public static readonly DependencyProperty AllowNullLocationProperty =
  18. DependencyProperty.Register("AllowNullLocation", typeof(bool), typeof(StockMovementGrid), new UIPropertyMetadata(null));
  19. public static readonly DependencyProperty AllowNullBatchProperty =
  20. DependencyProperty.Register("AllowNullBatch", typeof(bool), typeof(StockMovementGrid), new UIPropertyMetadata(null));
  21. private readonly Button AllButton;
  22. private bool bShowAll = true;
  23. private readonly BitmapImage docs = PRSDesktop.Resources.doc_png.AsBitmapImage();
  24. private int syscolumn = -1;
  25. public StockMovementGrid()
  26. {
  27. Options
  28. .BeginUpdate()
  29. .Add(DynamicGridOption.RecordCount)
  30. .Add(DynamicGridOption.SelectColumns)
  31. .Add(DynamicGridOption.FilterRows)
  32. .EndUpdate();
  33. ColumnsTag = "StockMovementGrid";
  34. HiddenColumns.Add(x => x.System);
  35. HiddenColumns.Add(x => x.Transaction);
  36. ActionColumns.Add(new DynamicImageColumn(DocumentsImage, DocumentsClick) { Position = DynamicActionColumnPosition.Start });
  37. HiddenColumns.Add(x => x.Documents);
  38. HiddenColumns.Add(x => x.Batch.ID);
  39. HiddenColumns.Add(x => x.Batch.Type);
  40. AllButton = AddButton("Hide System", null, ToggleAllTransations);
  41. }
  42. public DateTime StartDate { get; set; } = DateTime.MinValue;
  43. public DateTime EndDate { get; set; } = DateTime.MaxValue;
  44. public IStockLocation Location { get; set; }
  45. public bool AllowNullLocation
  46. {
  47. get => (bool)GetValue(AllowNullLocationProperty);
  48. set => SetValue(AllowNullLocationProperty, value);
  49. }
  50. public IStockMovementBatch Batch { get; set; }
  51. public bool AllowNullBatch
  52. {
  53. get => (bool)GetValue(AllowNullBatchProperty);
  54. set => SetValue(AllowNullBatchProperty, value);
  55. }
  56. public event DataModelUpdateEvent OnUpdateDataModel;
  57. public string SectionName => "Stock Movements";
  58. public DataModel DataModel(Selection selection)
  59. {
  60. var ids = ExtractValues(x => x.ID, selection).ToArray();
  61. return new BaseDataModel<StockMovement>(new Filter<StockMovement>(x => x.ID).InList(ids));
  62. }
  63. private bool DocumentsClick(CoreRow arg)
  64. {
  65. if (arg == null || arg.Get<StockMovement, int>(x => x.Documents) == 0)
  66. return false;
  67. var docs = new List<IEntityDocument>();
  68. using (new WaitCursor())
  69. {
  70. var batchid = arg.Get<StockMovement, Guid>(x => x.Batch.ID);
  71. var table = new Client<StockMovementBatchDocument>().Query(
  72. new Filter<StockMovementBatchDocument>(x => x.EntityLink.ID).IsEqualTo(batchid)
  73. );
  74. foreach (var row in table.Rows)
  75. docs.Add(row.ToObject<StockMovementBatchDocument>());
  76. }
  77. if (docs.Any())
  78. {
  79. var editor = new DocumentEditor(docs.ToArray());
  80. //editor.PrintAllowed = Security.IsAllowed<CanPrintFactoryFloorDrawings>();
  81. editor.SaveAllowed = Security.IsAllowed<CanSaveFactoryFloorDrawings>();
  82. editor.ShowDialog();
  83. }
  84. else
  85. {
  86. MessageBox.Show("No Documents Available!");
  87. }
  88. return false;
  89. }
  90. private BitmapImage DocumentsImage(CoreRow arg)
  91. {
  92. if (arg == null)
  93. return docs;
  94. return arg.Get<Delivery, int>(x => x.Documents) > 0 ? docs : null;
  95. }
  96. private bool ToggleAllTransations(Button arg1, CoreRow[] arg2)
  97. {
  98. bShowAll = !bShowAll;
  99. AllButton.Content = bShowAll ? "Hide System" : "Show All";
  100. return true;
  101. }
  102. protected override void Reload(Filters<StockMovement> criteria, Columns<StockMovement> columns, ref SortOrder<StockMovement>? sort,
  103. Action<CoreTable, Exception> action)
  104. {
  105. if (!AllowNullLocation && (Location == null || Location.ID == Guid.Empty))
  106. {
  107. criteria.Add(new Filter<StockMovement>(x => x.ID).IsEqualTo(CoreUtils.FullGuid));
  108. criteria.Add(new Filter<StockMovement>(x => x.ID).IsEqualTo(Guid.Empty));
  109. }
  110. else
  111. {
  112. if (Location != null)
  113. criteria.Add(new Filter<StockMovement>(x => x.Location.ID).IsEqualTo(Location.ID));
  114. }
  115. if (!AllowNullBatch && (Batch == null || Batch.ID == Guid.Empty))
  116. {
  117. criteria.Add(new Filter<StockMovement>(x => x.ID).IsEqualTo(CoreUtils.FullGuid));
  118. criteria.Add(new Filter<StockMovement>(x => x.ID).IsEqualTo(Guid.Empty));
  119. }
  120. else
  121. {
  122. if (Batch != null)
  123. criteria.Add(new Filter<StockMovement>(x => x.Batch.ID).IsEqualTo(Batch.ID));
  124. }
  125. if (!bShowAll)
  126. criteria.Add(new Filter<StockMovement>(x => x.System).IsEqualTo(false));
  127. if (!DateTime.Equals(StartDate, DateTime.MinValue))
  128. criteria.Add(new Filter<StockMovement>(x => x.Date).IsGreaterThanOrEqualTo(StartDate));
  129. if (!DateTime.Equals(EndDate, DateTime.MaxValue))
  130. criteria.Add(new Filter<StockMovement>(x => x.Date).IsLessThan(EndDate.Date.AddDays(1)));
  131. sort = new SortOrder<StockMovement>(x => x.Date, SortDirection.Descending).ThenBy(x => x.System);
  132. base.Reload(criteria, columns, ref sort, action);
  133. }
  134. protected override StockMovement CreateItem()
  135. {
  136. var result = base.CreateItem();
  137. result.Location.ID = Location != null ? Location.ID : Guid.Empty;
  138. return result;
  139. }
  140. protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
  141. {
  142. if (column.ColumnName.Equals("Location.ID"))
  143. return new NullEditor();
  144. return base.GetEditor(item, column);
  145. }
  146. protected override void DeleteItems(params CoreRow[] rows)
  147. {
  148. if (!rows.Any())
  149. {
  150. MessageBox.Show("Please select an item first!");
  151. return;
  152. }
  153. var txnid = rows.First().Get<StockMovement, Guid>(x => x.Transaction);
  154. var allrecords = new Client<StockMovement>().Query(
  155. new Filter<StockMovement>(x => x.Transaction).IsEqualTo(txnid),
  156. new Columns<StockMovement>(x => x.ID)
  157. );
  158. base.DeleteItems(allrecords.Rows.ToArray());
  159. }
  160. //int typecolumn = -1;
  161. protected override DynamicGridStyle GetRowStyle(CoreRow row, DynamicGridStyle style)
  162. {
  163. var result = base.GetRowStyle(row, style);
  164. if (syscolumn == -1)
  165. {
  166. var col = row.Table.Columns.FirstOrDefault(x => x.ColumnName.Equals("System"));
  167. syscolumn = row.Table.Columns.IndexOf(col);
  168. }
  169. if (row.Values[syscolumn].Equals(true)) //row.Get<StockMovement, bool>(x => x.System))
  170. {
  171. result = new DynamicGridRowStyle
  172. {
  173. Foreground = new SolidColorBrush(Colors.Gray),
  174. FontStyle = FontStyles.Italic,
  175. Background = new SolidColorBrush(Colors.Gainsboro)
  176. };
  177. return result;
  178. }
  179. //if (typecolumn == -1)
  180. //{
  181. // CoreColumn col = row.Table.Columns.FirstOrDefault(x => x.ColumnName.Equals("Batch.Type"));
  182. // typecolumn = row.Table.Columns.IndexOf(col);
  183. //}
  184. //var type = (StockMovementBatchType)row.Values[typecolumn];
  185. //result = new NewDynamicGridStyle()
  186. //{
  187. // Background = new SolidColorBrush(
  188. // type == StockMovementBatchType.Stocktake
  189. // ? Colors.LightGreen
  190. // : type == StockMovementBatchType.Receipt
  191. // ? Colors.LightYellow
  192. // : Colors.LightSalmon
  193. // )
  194. //};
  195. return result;
  196. }
  197. }
  198. }