PurchaseOrderItemStore.cs 13 KB

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