PurchaseOrderItemStore.cs 11 KB

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