DeliveryStore.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Comal.Classes;
  6. using InABox.Core;
  7. namespace Comal.Stores
  8. {
  9. internal class DeliveryStore : BaseStore<Delivery>
  10. {
  11. private void SendNotifications(Delivery delivery)
  12. {
  13. if (delivery == null)
  14. return;
  15. var delnumber = delivery.Number;
  16. var jobid = delivery.Job.ID;
  17. var delemp = delivery.Employee.ID;
  18. // Get Job Details
  19. var job = jobid.Equals(Guid.Empty)
  20. ? null
  21. : Provider.Query(
  22. new Filter<Job>(x => x.ID).IsEqualTo(jobid),
  23. new Columns<Job>(
  24. x => x.JobNumber,
  25. x => x.Name,
  26. x => x.EmployeeLink.ID,
  27. x => x.ManagerLink.ID
  28. )
  29. ).Rows.FirstOrDefault();
  30. var jobnumber = job != null ? job.Get<Job, string>(x => x.JobNumber) : "";
  31. var jobname = job != null ? job.Get<Job, string>(x => x.Name) : "";
  32. var jobemp = job != null ? job.Get<Job, Guid>(x => x.EmployeeLink.ID) : Guid.Empty;
  33. var jobmgr = job != null ? job.Get<Job, Guid>(x => x.ManagerLink.ID) : Guid.Empty;
  34. // Scan for Delivery Items and Build the List to Send
  35. var items = Provider.Query(
  36. new Filter<DeliveryItem>(x => x.Delivery.ID).IsEqualTo(delivery.ID),
  37. new Columns<DeliveryItem>(
  38. x => x.ID,
  39. x => x.ShipmentLink.ID,
  40. x => x.ShipmentLink.Code,
  41. x => x.RequisitionLink.ID,
  42. x => x.RequisitionLink.Number,
  43. x => x.Piece,
  44. x => x.Title,
  45. x => x.Barcode,
  46. x => x.ManufacturingPacketLink.Serial,
  47. x => x.Description
  48. )
  49. );
  50. var lines = new Dictionary<string, List<string>>();
  51. foreach (var row in items.Rows)
  52. {
  53. var tag = "";
  54. if (row.IsEntityLinkValid<DeliveryItem, ShipmentLink>(x => x.ShipmentLink))
  55. tag = string.Format("Rack #{0}", row.Get<DeliveryItem, string>(c => c.ShipmentLink.Code));
  56. else if (row.IsEntityLinkValid<DeliveryItem, RequisitionLink>(x => x.RequisitionLink))
  57. tag = string.Format("Requisition #{0}", row.Get<DeliveryItem, int>(c => c.RequisitionLink.Number));
  58. if (!string.IsNullOrWhiteSpace(tag))
  59. {
  60. if (!lines.ContainsKey(tag))
  61. lines[tag] = new List<string>();
  62. if (row.IsEntityLinkValid<DeliveryItem, RequisitionLink>(x => x.RequisitionLink))
  63. lines[tag].Add(string.Format("{0}: {1}", row.Get<DeliveryItem, string>(c => c.Piece),
  64. row.Get<DeliveryItem, string>(c => c.Description)));
  65. else
  66. lines[tag].Add(string.Format("{0}: {1} - {2}", row.Get<DeliveryItem, string>(c => c.Barcode),
  67. row.Get<DeliveryItem, string>(c => c.ManufacturingPacketLink.Serial), row.Get<DeliveryItem, string>(c => c.Description)));
  68. }
  69. }
  70. var sb = new StringBuilder();
  71. sb.AppendLine("The following items have been delivered:");
  72. foreach (var key in lines.Keys)
  73. {
  74. sb.AppendLine(" ");
  75. sb.AppendLine(key);
  76. sb.AppendLine(new string('=', key.Length));
  77. foreach (var line in lines[key])
  78. sb.AppendLine(line);
  79. }
  80. // Send the notifications
  81. var emps = Provider.Query(
  82. new Filter<Employee>(x => x.UserLink.UserID).IsEqualTo(UserID)
  83. .Or(x => x.ID).IsEqualTo(delemp)
  84. .Or(x => x.ID).IsEqualTo(jobemp)
  85. .Or(x => x.ID).IsEqualTo(jobmgr)
  86. ,
  87. new Columns<Employee>(
  88. x => x.ID,
  89. x => x.UserLink.ID
  90. )
  91. );
  92. var me = emps.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.UserLink.ID) == UserGuid);
  93. var notifications = new List<Notification>();
  94. foreach (var emp in emps.Rows)
  95. if (emp.Get<Employee, Guid>(x => x.UserLink.ID) != UserGuid)
  96. {
  97. var notification = new Notification();
  98. notification.Employee.ID = emp.Get<Employee, Guid>(x => x.ID);
  99. notification.Title = string.Format("Delivery #{0} ({1}: {2}) has been Delivered", delnumber, jobnumber, jobname);
  100. notification.Description = sb.ToString();
  101. notification.Job.ID = jobid;
  102. notification.EntityType = typeof(Delivery).EntityName();
  103. notification.EntityID = delivery.ID;
  104. notification.Sender.ID = me != null ? me.Get<Employee, Guid>(x => x.ID) : Guid.Empty;
  105. notifications.Add(notification);
  106. }
  107. if (notifications.Any())
  108. FindSubStore<Notification>().Save(notifications, "");
  109. }
  110. protected override void AfterSave(Delivery entity)
  111. {
  112. base.AfterSave(entity);
  113. if (!entity.Delivered.IsEmpty() && entity.HasOriginalValue(x => x.Delivered) && entity.GetOriginalValue(x => x.Delivered).IsEmpty())
  114. SendNotifications(entity);
  115. UpdateTrackingKanban<DeliveryKanban, Delivery, DeliveryLink>(entity, d =>
  116. {
  117. return !entity.Delivered.IsEmpty()
  118. ? KanbanCategory.Complete
  119. : entity.Assignment.IsValid()
  120. ? KanbanCategory.InProgress
  121. : KanbanCategory.Open;
  122. });
  123. //requi tracking kanban is usually updated in RequisitionStore, the requi is not necessarily saved when a delivery is completed
  124. if (entity.Delivered != DateTime.MinValue)
  125. {
  126. CoreTable table = Provider.Query<Requisition>(new Filter<Requisition>(x => x.Delivery.ID).IsEqualTo(entity.ID));
  127. if (table.Rows.Any())
  128. {
  129. foreach (CoreRow row in table.Rows)
  130. {
  131. Requisition requisition = row.ToObject<Requisition>();
  132. UpdateTrackingKanban<RequisitionKanban, Requisition, RequisitionLink>(requisition, e => { return KanbanCategory.Complete; });
  133. }
  134. }
  135. }
  136. }
  137. protected override void BeforeDelete(Delivery entity)
  138. {
  139. UnlinkTrackingKanban<DeliveryKanban, Delivery, DeliveryLink>(entity);
  140. }
  141. }
  142. }