EmployeeProductStore.cs 5.9 KB

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