EmployeeProductStore.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Linq;
  2. using Comal.Classes;
  3. using InABox.Core;
  4. using System;
  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.CopyFrom(entity.Product.DefaultInstance.Dimensions);
  32. movement.Qty = 0.0F - movement.Issued * movement.Dimensions.Value;
  33. movement.Notes = string.Format("Issued to " + entity.Employee.Name);
  34. FindSubStore<StockMovement>().Save(movement, "Employee/Product Issue Created");
  35. entity.IssuedMovement.ID = movement.ID;
  36. }
  37. else
  38. {
  39. var movement = Provider.Load(new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.IssuedMovement.ID)).FirstOrDefault();
  40. movement ??= new StockMovement();
  41. movement.Product.ID = entity.Product.ID;
  42. movement.Dimensions.CopyFrom(entity.Product.DefaultInstance.Dimensions);
  43. movement.Location.ID = entity.Product.DefaultLocation.ID;
  44. movement.Date = entity.Issued;
  45. movement.Issued = entity.Quantity;
  46. movement.Received = 0.0F;
  47. movement.Type = StockMovementType.Issue;
  48. movement.Qty = 0.0F - movement.Issued * movement.Dimensions.Value;
  49. movement.Notes = string.Format("Issued to " + entity.Employee.Name);
  50. FindSubStore<StockMovement>().Save(movement, "Employee/Product Issue Updated");
  51. entity.IssuedMovement.ID = movement.ID;
  52. }
  53. }
  54. }
  55. private void CheckReturn(EmployeeProduct entity)
  56. {
  57. if (entity.Returned.IsEmpty())
  58. {
  59. if (entity.ReturnedMovement.IsValid())
  60. {
  61. var movement = new StockMovement { ID = entity.ReturnedMovement.ID };
  62. FindSubStore<StockMovement>().Delete(movement, "Employee/Product Return Deleted");
  63. entity.ReturnedMovement.ID = Guid.Empty;
  64. }
  65. }
  66. else
  67. {
  68. if (!entity.ReturnedMovement.IsValid())
  69. {
  70. var movement = new StockMovement();
  71. movement.Product.ID = entity.Product.ID;
  72. movement.Dimensions.CopyFrom(entity.Product.DefaultInstance.Dimensions);
  73. movement.Location.ID = entity.Product.DefaultLocation.ID;
  74. movement.Date = entity.Returned;
  75. movement.Issued = 0.0F;
  76. movement.Received = entity.Quantity;
  77. movement.Type = StockMovementType.Receive;
  78. movement.Qty = movement.Received * movement.Dimensions.Value;
  79. movement.Notes = string.Format("Returned by " + entity.Employee.Name);
  80. FindSubStore<StockMovement>().Save(movement, "Employee/Product Return Created");
  81. entity.ReturnedMovement.ID = movement.ID;
  82. }
  83. else
  84. {
  85. var movement = Provider.Load(new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.ReturnedMovement.ID)).FirstOrDefault();
  86. movement ??= new StockMovement();
  87. movement.Product.ID = entity.Product.ID;
  88. movement.Dimensions.CopyFrom(entity.Product.DefaultInstance.Dimensions);
  89. movement.Location.ID = entity.Product.DefaultLocation.ID;
  90. movement.Date = entity.Returned;
  91. movement.Issued = 0.0F;
  92. movement.Received = entity.Quantity;
  93. movement.Type = StockMovementType.Receive;
  94. movement.Qty = movement.Received * movement.Dimensions.Value;
  95. movement.Notes = string.Format("Returned by " + entity.Employee.Name);
  96. FindSubStore<StockMovement>().Save(movement, "Employee/Product Return Updated");
  97. entity.ReturnedMovement.ID = movement.ID;
  98. }
  99. }
  100. }
  101. protected override void BeforeSave(EmployeeProduct entity)
  102. {
  103. base.BeforeSave(entity);
  104. CheckIssue(entity);
  105. CheckReturn(entity);
  106. }
  107. protected override void BeforeDelete(EmployeeProduct entity)
  108. {
  109. base.BeforeDelete(entity);
  110. var movements = Provider.Load(new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.IssuedMovement.ID).Or(x => x.ID)
  111. .IsEqualTo(entity.ReturnedMovement.ID));
  112. FindSubStore<StockMovement>().Delete(movements, "Entity/Product Deleted");
  113. }
  114. }
  115. }