JobRequisitionItemStore.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using Comal.Classes;
  3. using Comal.Stores;
  4. using InABox.Core;
  5. using InABox.Database;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace PRSStores;
  9. public enum JobRequisitionItemAction
  10. {
  11. Created,
  12. Updated,
  13. Deleted,
  14. None
  15. }
  16. public class JobRequisitionItemStore : BaseStore<JobRequisitionItem>
  17. {
  18. protected override void AfterSave(JobRequisitionItem entity)
  19. {
  20. base.AfterSave(entity);
  21. if(entity.HasOriginalValue(x => x.Cancelled))
  22. {
  23. CancelMovements(entity);
  24. }
  25. }
  26. private static IEnumerable<StockMovement> GetMovements(IStore store, Guid jriID, Filter<StockMovement>? filter, Columns<StockMovement> columns)
  27. {
  28. return store.Provider
  29. .Query(
  30. Filter<StockMovement>.And(
  31. new Filter<StockMovement>(x => x.JobRequisitionItem.ID).IsEqualTo(jriID),
  32. filter),
  33. columns)
  34. .ToObjects<StockMovement>();
  35. }
  36. private void CancelMovements(JobRequisitionItem entity)
  37. {
  38. // Here, we care about *all* movements into or out of this requi. If stuff has been issued, it must be included,
  39. // since we cannot return issued stock back to general stock for the job.
  40. var movements = GetMovements(this, entity.ID, null,
  41. Columns.None<StockMovement>().Add(
  42. x => x.Product.ID,
  43. x => x.Style.ID,
  44. x => x.Job.ID,
  45. x => x.Location.ID,
  46. x => x.Units,
  47. x => x.Cost,
  48. x => x.OrderItem.ID)
  49. .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local));
  50. var newMovements = new List<StockMovement>();
  51. foreach(var movement in movements)
  52. {
  53. var from = movement.CreateMovement();
  54. from.Date = entity.Cancelled;
  55. from.Cost = movement.Cost;
  56. from.System = true;
  57. from.JobRequisitionItem.ID = entity.ID;
  58. from.OrderItem.ID = movement.OrderItem.ID;
  59. from.Notes = "Requisition item cancelled";
  60. var to = movement.CreateMovement();
  61. to.Date = entity.Cancelled;
  62. to.Cost = movement.Cost;
  63. to.System = true;
  64. to.Notes = "Requisition item cancelled";
  65. to.OrderItem.ID = movement.OrderItem.ID;
  66. to.Transaction = from.Transaction;
  67. if(movement.Units > 0)
  68. {
  69. // If this movement was an increase to reservation allocation, we create a transfer out of the reservation.
  70. from.Issued = movement.Units;
  71. to.Received = movement.Units;
  72. from.Type = StockMovementType.TransferOut;
  73. to.Type = StockMovementType.TransferIn;
  74. }
  75. else if(movement.Units < 0)
  76. {
  77. // If this movement was a decrease to reservation allocation, we create a transfer into the reservation.
  78. from.Received = -movement.Units;
  79. to.Issued = -movement.Units;
  80. from.Type = StockMovementType.TransferIn;
  81. to.Type = StockMovementType.TransferOut;
  82. }
  83. newMovements.Add(from);
  84. newMovements.Add(to);
  85. }
  86. if(newMovements.Count > 0)
  87. {
  88. var batch = new StockMovementBatch
  89. {
  90. Notes = "Requisition item cancelled."
  91. };
  92. FindSubStore<StockMovementBatch>().Save(batch, "");
  93. foreach(var mvt in newMovements)
  94. {
  95. mvt.Batch.ID = batch.ID;
  96. }
  97. FindSubStore<StockMovement>().Save(newMovements, "Requisition item cancelled.");
  98. }
  99. }
  100. }