JobRequiItemAvailableStockGrid.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using jdk.nashorn.@internal.ir.debug;
  7. using NPOI.SS.Formula.Functions;
  8. using org.omg.PortableInterceptor;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. namespace PRSDesktop
  17. {
  18. public delegate void StockReserved();
  19. public class JobRequiItemAvailableStockGrid : DynamicDataGrid<StockHolding>
  20. {
  21. public event StockReserved OnStockReserved;
  22. public Guid ProductID = Guid.Empty;
  23. public JobRequisitionItem Item = new JobRequisitionItem();
  24. public Guid empID = new Guid();
  25. public JobRequiItemAvailableStockGrid()
  26. {
  27. Options.AddRange(
  28. DynamicGridOption.SelectColumns,
  29. DynamicGridOption.FilterRows,
  30. DynamicGridOption.RecordCount,
  31. DynamicGridOption.EditRows
  32. );
  33. HiddenColumns.Add(x => x.Qty);
  34. AddButton("Reserve For Job", null, ReserveStock);
  35. }
  36. private bool ReserveStock(Button btn, CoreRow[] rows)
  37. {
  38. if (!rows.Any())
  39. {
  40. MessageBox.Show("Please select at least one holding to reserve");
  41. return false;
  42. }
  43. var holding = rows[0].ToObject<StockHolding>();
  44. int units = 0;
  45. if (Item.Qty <= holding.Units)
  46. units = Convert.ToInt32(Item.Qty);
  47. else
  48. units = Convert.ToInt32(holding.Units);
  49. if (NumberEdit.Execute("Enter amount to reserve", 1, units, ref units))
  50. {
  51. StockMovementBatch batch = new StockMovementBatch();
  52. batch.Type = StockMovementBatchType.Transfer;
  53. batch.Notes = "Reserving Stock for Requi: " + Item.Requisition.Number;
  54. new Client<StockMovementBatch>().Save(batch, "Created from Job Requi Item Reserve Stock Function");
  55. List<StockMovement> movements = new List<StockMovement>
  56. {
  57. CreateMovement(StockMovementType.Issue, units, holding, batch),
  58. CreateMovement(StockMovementType.Receive, units, holding, batch)
  59. };
  60. new Client<StockMovement>().Save(movements, "Created from Job Requi Item Reserve Stock Function");
  61. Item.Status = JobRequisitionItemStatus.Reserved;
  62. new Client<JobRequisitionItem>().Save(Item, "Updated from Job Requi Item Reserve Stock Function");
  63. MessageBox.Show("Success - " + Item.Product.Name + " stock reserved for Requisition " + Item.Requisition.Number);
  64. OnStockReserved?.Invoke();
  65. }
  66. return true;
  67. }
  68. enum StockMovementType
  69. {
  70. Issue,
  71. Receive
  72. }
  73. private StockMovement CreateMovement(StockMovementType type, double units, StockHolding holding, StockMovementBatch batch)
  74. {
  75. StockMovement mvt = new StockMovement();
  76. mvt.System = true;
  77. mvt.Batch.ID = batch.ID;
  78. mvt.Batch.Type = StockMovementBatchType.Transfer;
  79. mvt.Dimensions.CopyFrom(holding.Dimensions);
  80. mvt.Style.ID = holding.Style.ID;
  81. mvt.Location.ID = holding.Location.ID;
  82. mvt.Product.ID = holding.Product.ID;
  83. mvt.Date = DateTime.Now;
  84. mvt.IsTransfer = true;
  85. mvt.Notes = "Reserving Stock for Requi: " + Item.Requisition.Number;
  86. mvt.JobRequisitionItem.ID = Item.ID;
  87. mvt.Employee.ID = empID;
  88. if (type == StockMovementType.Issue)
  89. {
  90. mvt.Issued = units;
  91. mvt.Job.ID = holding.Job.ID;
  92. }
  93. else
  94. {
  95. mvt.Received = units;
  96. mvt.Job.ID = Item.Job.ID;
  97. }
  98. return mvt;
  99. }
  100. protected override void Reload(Filters<StockHolding> criteria, Columns<StockHolding> columns, ref SortOrder<StockHolding> sort, Action<CoreTable, Exception> action)
  101. {
  102. if (ProductID != Guid.Empty)
  103. criteria.Add(new Filter<StockHolding>(x => x.Product.ID).IsEqualTo(ProductID));
  104. else
  105. criteria.Add(new Filter<StockHolding>().None());
  106. base.Reload(criteria, columns, ref sort, action);
  107. }
  108. protected override bool FilterRecord(CoreRow row)
  109. {
  110. var result = base.FilterRecord(row);
  111. if (result)
  112. {
  113. var qty = row.Get<StockHolding, double>(x => x.Qty);
  114. var iszero = CoreUtils.IsEffectivelyEqual(qty, 0.0F);
  115. result = !iszero;
  116. }
  117. return result;
  118. }
  119. }
  120. }