StockMovementGrid.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. using Syncfusion.UI.Xaml.Diagram.Controls;
  14. namespace PRSDesktop;
  15. public class StockMovementGrid : DynamicDataGrid<StockMovement>, IDataModelSource, ISpecificGrid
  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 Button AllButton;
  22. private bool bShowAll = true;
  23. private readonly BitmapImage docs = PRSDesktop.Resources.doc_png.AsBitmapImage();
  24. private int syscolumn = -1;
  25. private static readonly BitmapImage? post = PRSDesktop.Resources.post.AsBitmapImage();
  26. private static readonly BitmapImage? tick = PRSDesktop.Resources.tick.AsBitmapImage();
  27. private static readonly BitmapImage? warning = PRSDesktop.Resources.warning.AsBitmapImage();
  28. private static readonly BitmapImage? refresh = PRSDesktop.Resources.refresh.AsBitmapImage();
  29. public StockMovementGrid()
  30. {
  31. ColumnsTag = "StockMovementGrid";
  32. }
  33. protected override void DoReconfigure(DynamicGridOptions options)
  34. {
  35. base.DoReconfigure(options);
  36. options.RecordCount = true;
  37. options.SelectColumns = true;
  38. options.FilterRows = true;
  39. }
  40. protected override void Init()
  41. {
  42. base.Init();
  43. HiddenColumns.Add(x => x.System);
  44. HiddenColumns.Add(x => x.Transaction);
  45. HiddenColumns.Add(x=>x.Product.ID);
  46. HiddenColumns.Add(x=>x.Location.ID);
  47. HiddenColumns.Add(x=>x.Style.ID);
  48. HiddenColumns.Add(x=>x.Job.ID);
  49. HiddenColumns.Add(x=>x.Dimensions.Unit.ID);
  50. HiddenColumns.Add(x=>x.Dimensions.UnitSize);
  51. ActionColumns.Add(new DynamicImageColumn(DocumentsImage, DocumentsClick) { Position = DynamicActionColumnPosition.Start });
  52. HiddenColumns.Add(x => x.Documents);
  53. HiddenColumns.Add(x => x.Batch.ID);
  54. HiddenColumns.Add(x => x.Batch.Type);
  55. HiddenColumns.Add(x => x.JobRequisitionItem.Requisition.Number);
  56. AllButton = AddButton("Hide System", null, ToggleAllTransations);
  57. HiddenColumns.Add(x => x.PostedStatus);
  58. HiddenColumns.Add(x => x.PostedNote);
  59. BeforeRefresh += StockMovementGrid_BeforeRefresh;
  60. ActionColumns.Add(new DynamicImageColumn(Posted_Image, null)
  61. {
  62. ToolTip = Posted_ToolTip
  63. });
  64. }
  65. public DateTime StartDate { get; set; } = DateTime.MinValue;
  66. public DateTime EndDate { get; set; } = DateTime.MaxValue;
  67. public IStockLocation Location { get; set; }
  68. public bool AllowNullLocation
  69. {
  70. get => (bool)GetValue(AllowNullLocationProperty);
  71. set => SetValue(AllowNullLocationProperty, value);
  72. }
  73. public IStockMovementBatch Batch { get; set; }
  74. public bool AllowNullBatch
  75. {
  76. get => (bool)GetValue(AllowNullBatchProperty);
  77. set => SetValue(AllowNullBatchProperty, value);
  78. }
  79. public event DataModelUpdateEvent? OnUpdateDataModel;
  80. public string SectionName => "Stock Movements";
  81. public DataModel DataModel(Selection selection)
  82. {
  83. var ids = ExtractValues(x => x.ID, selection).ToArray();
  84. return new BaseDataModel<StockMovement>(new Filter<StockMovement>(x => x.ID).InList(ids));
  85. }
  86. private FrameworkElement? Posted_ToolTip(DynamicActionColumn column, CoreRow? row)
  87. {
  88. if (row is null)
  89. {
  90. return column.TextToolTip("Stock Movement Processed Status");
  91. }
  92. return column.TextToolTip(row.Get<StockMovement, PostedStatus>(x => x.PostedStatus) switch
  93. {
  94. PostedStatus.PostFailed => "Post failed: " + row.Get<StockMovement, string>(x => x.PostedNote),
  95. PostedStatus.RequiresRepost => "Repost required: " + row.Get<StockMovement, string>(x => x.PostedNote),
  96. PostedStatus.Posted => "Processed",
  97. PostedStatus.NeverPosted or _ => "Not posted yet",
  98. });
  99. }
  100. private BitmapImage? Posted_Image(CoreRow? row)
  101. {
  102. if (row is null)
  103. return post;
  104. return row.Get<StockMovement, PostedStatus>(x => x.PostedStatus) switch
  105. {
  106. PostedStatus.PostFailed => warning,
  107. PostedStatus.Posted => tick,
  108. PostedStatus.RequiresRepost => refresh,
  109. PostedStatus.NeverPosted or _ => null,
  110. };
  111. }
  112. private bool DocumentsClick(CoreRow? arg)
  113. {
  114. if (arg == null || arg.Get<StockMovement, int>(x => x.Documents) == 0)
  115. return false;
  116. var docs = new List<IEntityDocument>();
  117. using (new WaitCursor())
  118. {
  119. var batchid = arg.Get<StockMovement, Guid>(x => x.Batch.ID);
  120. var table = new Client<StockMovementBatchDocument>().Query(
  121. new Filter<StockMovementBatchDocument>(x => x.EntityLink.ID).IsEqualTo(batchid)
  122. );
  123. foreach (var row in table.Rows)
  124. docs.Add(row.ToObject<StockMovementBatchDocument>());
  125. }
  126. if (docs.Any())
  127. {
  128. var editor = new DocumentEditor(docs.ToArray());
  129. //editor.PrintAllowed = Security.IsAllowed<CanPrintFactoryFloorDrawings>();
  130. editor.SaveAllowed = Security.IsAllowed<CanSaveFactoryFloorDrawings>();
  131. editor.ShowDialog();
  132. }
  133. else
  134. {
  135. MessageBox.Show("No Documents Available!");
  136. }
  137. return false;
  138. }
  139. private BitmapImage? DocumentsImage(CoreRow? arg)
  140. {
  141. if (arg == null)
  142. return docs;
  143. return arg.Get<Delivery, int>(x => x.Documents) > 0 ? docs : null;
  144. }
  145. private bool ToggleAllTransations(Button arg1, CoreRow[] arg2)
  146. {
  147. bShowAll = !bShowAll;
  148. AllButton.Content = bShowAll ? "Hide System" : "Show All";
  149. return true;
  150. }
  151. protected override void Reload(Filters<StockMovement> criteria, Columns<StockMovement> columns, ref SortOrder<StockMovement>? sort,
  152. Action<CoreTable, Exception> action)
  153. {
  154. if (!AllowNullLocation && (Location == null || Location.ID == Guid.Empty))
  155. {
  156. criteria.Add(new Filter<StockMovement>().None());
  157. }
  158. else
  159. {
  160. if (Location != null)
  161. criteria.Add(new Filter<StockMovement>(x => x.Location.ID).IsEqualTo(Location.ID));
  162. }
  163. if (!AllowNullBatch && (Batch == null || Batch.ID == Guid.Empty))
  164. {
  165. criteria.Add(new Filter<StockMovement>().None());
  166. }
  167. else
  168. {
  169. if (Batch != null)
  170. criteria.Add(new Filter<StockMovement>(x => x.Batch.ID).IsEqualTo(Batch.ID));
  171. }
  172. if (!bShowAll)
  173. criteria.Add(new Filter<StockMovement>(x => x.System).IsEqualTo(false));
  174. if (!DateTime.Equals(StartDate, DateTime.MinValue))
  175. criteria.Add(new Filter<StockMovement>(x => x.Date).IsGreaterThanOrEqualTo(StartDate));
  176. if (!DateTime.Equals(EndDate, DateTime.MaxValue))
  177. criteria.Add(new Filter<StockMovement>(x => x.Date).IsLessThan(EndDate.Date.AddDays(1)));
  178. sort = new SortOrder<StockMovement>(x => x.Date, SortDirection.Descending).ThenBy(x => x.System);
  179. base.Reload(criteria, columns, ref sort, action);
  180. }
  181. public override StockMovement CreateItem()
  182. {
  183. var result = base.CreateItem();
  184. result.Location.ID = Location != null ? Location.ID : Guid.Empty;
  185. return result;
  186. }
  187. protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
  188. {
  189. if (column.ColumnName.Equals("Location.ID"))
  190. return new NullEditor();
  191. return base.GetEditor(item, column);
  192. }
  193. public override void DeleteItems(params CoreRow[] rows)
  194. {
  195. if (!rows.Any())
  196. {
  197. MessageBox.Show("Please select an item first!");
  198. return;
  199. }
  200. var txnid = rows.First().Get<StockMovement, Guid>(x => x.Transaction);
  201. var allrecords = new Client<StockMovement>().Query(
  202. new Filter<StockMovement>(x => x.Transaction).IsEqualTo(txnid),
  203. Columns.None<StockMovement>().Add(x => x.ID)
  204. );
  205. base.DeleteItems(allrecords.Rows.ToArray());
  206. }
  207. private void StockMovementGrid_BeforeRefresh(object sender, BeforeRefreshEventArgs args)
  208. {
  209. syscolumn = -1;
  210. }
  211. //int typecolumn = -1;
  212. protected override DynamicGridStyle GetRowStyle(CoreRow row, DynamicGridStyle style)
  213. {
  214. var result = base.GetRowStyle(row, style);
  215. if (syscolumn == -1)
  216. {
  217. var col = row.Table.Columns.FirstOrDefault(x => x.ColumnName.Equals("System"));
  218. syscolumn = row.Table.Columns.IndexOf(col);
  219. }
  220. if (object.Equals(row.Values[syscolumn], true)) //row.Get<StockMovement, bool>(x => x.System))
  221. {
  222. result = new DynamicGridRowStyle
  223. {
  224. Foreground = new SolidColorBrush(Colors.Gray),
  225. FontStyle = FontStyles.Italic,
  226. Background = new SolidColorBrush(Colors.Gainsboro)
  227. };
  228. return result;
  229. }
  230. //if (typecolumn == -1)
  231. //{
  232. // CoreColumn col = row.Table.Columns.FirstOrDefault(x => x.ColumnName.Equals("Batch.Type"));
  233. // typecolumn = row.Table.Columns.IndexOf(col);
  234. //}
  235. //var type = (StockMovementBatchType)row.Values[typecolumn];
  236. //result = new NewDynamicGridStyle()
  237. //{
  238. // Background = new SolidColorBrush(
  239. // type == StockMovementBatchType.Stocktake
  240. // ? Colors.LightGreen
  241. // : type == StockMovementBatchType.Receipt
  242. // ? Colors.LightYellow
  243. // : Colors.LightSalmon
  244. // )
  245. //};
  246. return result;
  247. }
  248. }