PurchaseOrderItemStore.cs 13 KB

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