PurchaseOrderItemStore.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Comal.Classes;
  6. using InABox.Core;
  7. namespace Comal.Stores
  8. {
  9. internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
  10. {
  11. private void UpdateStockMovements(PurchaseOrderItem entity)
  12. {
  13. if (!entity.Product.IsValid())
  14. {
  15. Logger.Send(LogType.Information, UserID, "PurchaseOrderItem.Product.ID is blank!");
  16. return;
  17. }
  18. var locationid = entity.StockLocation.ID;
  19. var locationValid = entity.StockLocation.IsValid();
  20. var movementtask = new Task<List<StockMovement>>(() =>
  21. {
  22. var result = Provider.Query(
  23. new Filter<StockMovement>(x => x.OrderItem.ID).IsEqualTo(entity.ID),
  24. new Columns<StockMovement>(
  25. x => x.Date,
  26. x => x.Product.ID,
  27. x => x.Received,
  28. x => x.Employee.ID,
  29. x => x.OrderItem.ID,
  30. x => x.Job.ID,
  31. x => x.Location.ID,
  32. x => x.Dimensions.Unit.ID,
  33. x => x.Dimensions.Unit.Formula,
  34. x => x.Dimensions.Unit.Format,
  35. x => x.Dimensions.Quantity,
  36. x => x.Dimensions.Length,
  37. x => x.Dimensions.Width,
  38. x => x.Dimensions.Height,
  39. x => x.Dimensions.Weight,
  40. x => x.Notes
  41. )
  42. ).Rows.Select(x => x.ToObject<StockMovement>()).ToList();
  43. if (!result.Any())
  44. result.Add(new StockMovement());
  45. return result;
  46. });
  47. movementtask.Start();
  48. var producttask = new Task<CoreRow>(() =>
  49. {
  50. return Provider.Query(
  51. new Filter<Product>(x => x.ID).IsEqualTo(entity.Product.ID),
  52. new Columns<Product>(
  53. x => x.DefaultLocation.ID,
  54. x => x.Warehouse.ID,
  55. x => x.Dimensions.Unit.ID,
  56. x => x.Dimensions.Unit.Formula,
  57. x => x.Dimensions.Unit.Format,
  58. x => x.Dimensions.Quantity,
  59. x => x.Dimensions.Length,
  60. x => x.Dimensions.Width,
  61. x => x.Dimensions.Height,
  62. x => x.Dimensions.Weight,
  63. x => x.NonStock
  64. )
  65. ).Rows.FirstOrDefault();
  66. });
  67. producttask.Start();
  68. var locationtask = new Task<CoreTable>(() =>
  69. {
  70. return Provider.Query(
  71. new Filter<StockLocation>(x => x.Default).IsEqualTo(true),
  72. new Columns<StockLocation>(x => x.ID, x => x.Warehouse.ID, x => x.Warehouse.Default)
  73. );
  74. });
  75. locationtask.Start();
  76. Task.WaitAll(movementtask, producttask, locationtask);
  77. var movements = movementtask.Result;
  78. var product = producttask.Result;
  79. var defaultlocations = locationtask.Result;
  80. if (product == null)
  81. {
  82. Logger.Send(LogType.Information, UserID, "Cannot Find PurchaseOrderItem.Product.ID!");
  83. return;
  84. }
  85. if (product.Get<Product, bool>(x => x.NonStock))
  86. {
  87. Logger.Send(LogType.Information, UserID, "PurchaseOrderItem.Product is marked as Non Stock!");
  88. return;
  89. }
  90. if (!locationValid)
  91. {
  92. Logger.Send(LogType.Information, UserID, "PurchaseOrderItem.Location.ID is blank!");
  93. if (product != null)
  94. {
  95. var productlocationid = product.EntityLinkID<Product, StockLocationLink>(x => x.DefaultLocation) ?? Guid.Empty;
  96. if (productlocationid != Guid.Empty)
  97. {
  98. Logger.Send(LogType.Information, UserID, "- Using Product.DefaultLocation.ID as location");
  99. locationid = productlocationid;
  100. }
  101. else
  102. {
  103. var productwarehouseid = product.Get<Product, Guid>(c => c.Warehouse.ID);
  104. var row = defaultlocations.Rows.FirstOrDefault(r => r.Get<StockLocation, Guid>(c => c.Warehouse.ID) == productwarehouseid);
  105. if (row != null)
  106. {
  107. Logger.Send(LogType.Information, UserID, "- Using Product.Warehouse -> Default as location");
  108. locationid = row.Get<StockLocation, Guid>(x => x.ID);
  109. }
  110. }
  111. }
  112. if (locationid == Guid.Empty)
  113. {
  114. var row = defaultlocations.Rows.FirstOrDefault(r => r.Get<StockLocation, bool>(c => c.Warehouse.Default));
  115. if (row != null)
  116. {
  117. Logger.Send(LogType.Information, UserID, "- Using Default Warehouse -> Default Location as location");
  118. locationid = row.Get<StockLocation, Guid>(x => x.ID);
  119. }
  120. }
  121. if (locationid == Guid.Empty)
  122. {
  123. Logger.Send(LogType.Information, UserID, "- Cannot find Location : Skipping Movement Creation");
  124. return;
  125. }
  126. }
  127. if (
  128. (entity.Dimensions.Unit.ID == Guid.Empty)
  129. && (entity.Dimensions.Height == 0)
  130. && (entity.Dimensions.Width == 0)
  131. && (entity.Dimensions.Length == 0)
  132. && (entity.Dimensions.Weight == 0)
  133. )
  134. {
  135. Logger.Send(LogType.Information, UserID, "PurchaseOrderItem.Unit Size is zero!");
  136. entity.Dimensions.CopyFrom(product.ToObject<Product>().Dimensions);
  137. }
  138. foreach (var movement in movements)
  139. {
  140. movement.Batch.Type = StockMovementBatchType.Receipt;
  141. movement.Date = entity.ReceivedDate;
  142. movement.Product.ID = entity.Product.ID;
  143. movement.Received = entity.Qty;
  144. movement.Employee.ID = Guid.Empty;
  145. movement.OrderItem.ID = entity.ID;
  146. movement.Job.ID = entity.Job.ID;
  147. movement.Location.ID = locationid;
  148. movement.Dimensions.CopyFrom(entity.Dimensions);
  149. movement.Style.ID = entity.Style.ID;
  150. movement.Style.Code = entity.Style.Code;
  151. movement.Style.Description = entity.Style.Description;
  152. movement.Notes = string.Format("Received on PO {0}", entity.PurchaseOrderLink.PONumber);
  153. }
  154. var updates = movements.Where(x => x.IsChanged());
  155. if (updates.Any())
  156. FindSubStore<StockMovement>().Save(updates, "Updated by Purchase Order Modification");
  157. }
  158. private void DeleteStockMovements(PurchaseOrderItem entity)
  159. {
  160. var movements = Provider.Query(
  161. new Filter<StockMovement>(x => x.OrderItem.ID).IsEqualTo(entity.ID),
  162. new Columns<StockMovement>(x => x.ID)
  163. ).Rows.Select(x => x.ToObject<StockMovement>());
  164. if (movements.Any())
  165. FindSubStore<StockMovement>().Delete(movements, "Purchase Order Item marked as Unreceived");
  166. }
  167. protected override void BeforeSave(PurchaseOrderItem entity)
  168. {
  169. base.BeforeSave(entity);
  170. EnsureJobMaterials(entity.Product, entity.Job, entity.Style, entity.Dimensions);
  171. }
  172. protected override void AfterSave(PurchaseOrderItem entity)
  173. {
  174. base.AfterSave(entity);
  175. if (entity.ReceivedDate.IsEmpty())
  176. DeleteStockMovements(entity);
  177. else
  178. {
  179. UpdateStockMovements(entity);
  180. UpdateJobRequiItems(entity);
  181. }
  182. //}
  183. CleanupJobMaterials(entity.ID, entity.Product, entity.Job, entity.Style, entity.Dimensions, false);
  184. }
  185. private void UpdateJobRequiItems(PurchaseOrderItem entity)
  186. {
  187. var table = Provider.Query(
  188. new Filter<JobRequisitionItem>(x => x.PurchaseOrderItem.ID).IsEqualTo(entity.ID),
  189. new Columns<JobRequisitionItem>(x => x.ID, x => x.Status)
  190. );
  191. if (table.Rows.Any())
  192. {
  193. JobRequisitionItem item = table.Rows.FirstOrDefault().ToObject<JobRequisitionItem>();
  194. if (item.Status == JobRequisitionItemStatus.TreatmentOnOrder)
  195. item.Status = JobRequisitionItemStatus.TreatmentReceived;
  196. else
  197. item.Status = JobRequisitionItemStatus.Received;
  198. }
  199. }
  200. protected override void BeforeDelete(PurchaseOrderItem entity)
  201. {
  202. base.BeforeDelete(entity);
  203. DeleteStockMovements(entity);
  204. var delete = Provider.Query(
  205. new Filter<PurchaseOrderItem>(x => x.ID).IsEqualTo(entity.ID),
  206. new Columns<PurchaseOrderItem>(x => x.ID)
  207. .Add(x => x.Product.ID)
  208. .Add(x => x.Job.ID)
  209. .Add(x => x.Style.ID)
  210. .Add(x => x.Dimensions.Unit.ID)
  211. .Add(x => x.Dimensions.Length)
  212. .Add(x => x.Dimensions.Width)
  213. .Add(x => x.Dimensions.Height)
  214. .Add(x => x.Dimensions.Weight)
  215. ).Rows.FirstOrDefault()?.ToObject<PurchaseOrderItem>();
  216. CleanupJobMaterials(delete.ID, delete.Product, delete.Job, delete.Style, delete.Dimensions, true);
  217. }
  218. protected override void AfterDelete(PurchaseOrderItem entity)
  219. {
  220. base.AfterDelete(entity);
  221. RemoveJobRequisitionItemLink(entity);
  222. }
  223. private void RemoveJobRequisitionItemLink(PurchaseOrderItem entity)
  224. {
  225. CoreTable table = Provider.Query<JobRequisitionItem>
  226. (
  227. new Filter<JobRequisitionItem>(x => x.PurchaseOrderItem.ID).IsEqualTo(entity.ID),
  228. new Columns<JobRequisitionItem>(x => x.ID, x => x.PurchaseOrderItem.PurchaseOrderLink.PONumber, x => x.Status)
  229. );
  230. if (table.Rows.Any())
  231. {
  232. JobRequisitionItem item = table.Rows.FirstOrDefault().ToObject<JobRequisitionItem>();
  233. item.PurchaseOrderItem.PurchaseOrderLink.PONumber = "";
  234. item.Status = JobRequisitionItemStatus.NotChecked;
  235. Provider.Save<JobRequisitionItem>(item);
  236. }
  237. }
  238. }
  239. }