EmployeeProductStore.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Linq;
  3. using Comal.Classes;
  4. using InABox.Core;
  5. namespace Comal.Stores
  6. {
  7. public class EmployeeProductStore : BaseStore<EmployeeProduct>
  8. {
  9. private void CheckIssue(EmployeeProduct entity)
  10. {
  11. if (entity.Issued.IsEmpty())
  12. {
  13. if (entity.IssuedMovement.IsValid())
  14. {
  15. var movement = new StockMovement { ID = entity.IssuedMovement.ID };
  16. FindSubStore<StockMovement>().Delete(movement, "Employee/Product Issue Deleted");
  17. entity.IssuedMovement.ID = Guid.Empty;
  18. }
  19. }
  20. else
  21. {
  22. if (!entity.IssuedMovement.IsValid())
  23. {
  24. var movement = new StockMovement();
  25. movement.Product.ID = entity.Product.ID;
  26. movement.Location.ID = entity.Product.DefaultLocation.ID;
  27. movement.Date = entity.Issued;
  28. movement.Issued = entity.Quantity;
  29. movement.Received = 0.0F;
  30. movement.Type = StockMovementType.Issue;
  31. movement.Dimensions.Unit.ID = entity.Product.Dimensions.Unit.ID;
  32. movement.Dimensions.Unit.Synchronise(entity.Product.Dimensions);
  33. movement.Dimensions.Length = entity.Product.Dimensions.Length;
  34. movement.Dimensions.Width = entity.Product.Dimensions.Width;
  35. movement.Dimensions.Height = entity.Product.Dimensions.Height;
  36. movement.Dimensions.Weight = entity.Product.Dimensions.Weight;
  37. movement.Qty = 0.0F - movement.Issued * movement.Dimensions.Value;
  38. movement.Notes = string.Format("Issued to " + entity.Employee.Name);
  39. FindSubStore<StockMovement>().Save(movement, "Employee/Product Issue Created");
  40. entity.IssuedMovement.ID = movement.ID;
  41. }
  42. else
  43. {
  44. var movement = Provider.Load(new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.IssuedMovement.ID)).FirstOrDefault();
  45. movement ??= new StockMovement();
  46. movement.Product.ID = entity.Product.ID;
  47. movement.Dimensions.CopyFrom(entity.Product.Dimensions);
  48. movement.Location.ID = entity.Product.DefaultLocation.ID;
  49. movement.Date = entity.Issued;
  50. movement.Issued = entity.Quantity;
  51. movement.Received = 0.0F;
  52. movement.Type = StockMovementType.Issue;
  53. movement.Qty = 0.0F - movement.Issued * movement.Dimensions.Value;
  54. movement.Notes = string.Format("Issued to " + entity.Employee.Name);
  55. FindSubStore<StockMovement>().Save(movement, "Employee/Product Issue Updated");
  56. entity.IssuedMovement.ID = movement.ID;
  57. }
  58. }
  59. }
  60. private void CheckReturn(EmployeeProduct entity)
  61. {
  62. if (entity.Returned.IsEmpty())
  63. {
  64. if (entity.ReturnedMovement.IsValid())
  65. {
  66. var movement = new StockMovement { ID = entity.ReturnedMovement.ID };
  67. FindSubStore<StockMovement>().Delete(movement, "Employee/Product Return Deleted");
  68. entity.ReturnedMovement.ID = Guid.Empty;
  69. }
  70. }
  71. else
  72. {
  73. if (!entity.ReturnedMovement.IsValid())
  74. {
  75. var movement = new StockMovement();
  76. movement.Product.ID = entity.Product.ID;
  77. movement.Dimensions.CopyFrom(entity.Product.Dimensions);
  78. movement.Location.ID = entity.Product.DefaultLocation.ID;
  79. movement.Date = entity.Returned;
  80. movement.Issued = 0.0F;
  81. movement.Received = entity.Quantity;
  82. movement.Type = StockMovementType.Receive;
  83. movement.Qty = movement.Received * movement.Dimensions.Value;
  84. movement.Notes = string.Format("Returned by " + entity.Employee.Name);
  85. FindSubStore<StockMovement>().Save(movement, "Employee/Product Return Created");
  86. entity.ReturnedMovement.ID = movement.ID;
  87. }
  88. else
  89. {
  90. var movement = Provider.Load(new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.ReturnedMovement.ID)).FirstOrDefault();
  91. movement ??= new StockMovement();
  92. movement.Product.ID = entity.Product.ID;
  93. movement.Dimensions.CopyFrom(entity.Product.Dimensions);
  94. movement.Location.ID = entity.Product.DefaultLocation.ID;
  95. movement.Date = entity.Returned;
  96. movement.Issued = 0.0F;
  97. movement.Received = entity.Quantity;
  98. movement.Type = StockMovementType.Receive;
  99. movement.Qty = movement.Received * movement.Dimensions.Value;
  100. movement.Notes = string.Format("Returned by " + entity.Employee.Name);
  101. FindSubStore<StockMovement>().Save(movement, "Employee/Product Return Updated");
  102. entity.ReturnedMovement.ID = movement.ID;
  103. }
  104. }
  105. }
  106. protected override void BeforeSave(EmployeeProduct entity)
  107. {
  108. base.BeforeSave(entity);
  109. CheckIssue(entity);
  110. CheckReturn(entity);
  111. }
  112. protected override void BeforeDelete(EmployeeProduct entity)
  113. {
  114. base.BeforeDelete(entity);
  115. var movements = Provider.Load(new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.IssuedMovement.ID).Or(x => x.ID)
  116. .IsEqualTo(entity.ReturnedMovement.ID));
  117. FindSubStore<StockMovement>().Delete(movements, "Entity/Product Deleted");
  118. }
  119. }
  120. }