|
@@ -5,153 +5,192 @@ using Comal.Classes;
|
|
|
using InABox.Core;
|
|
|
using System;
|
|
|
|
|
|
-namespace Comal.Stores
|
|
|
+namespace Comal.Stores;
|
|
|
+
|
|
|
+internal class DeliveryStore : BaseStore<Delivery>
|
|
|
{
|
|
|
- internal class DeliveryStore : BaseStore<Delivery>
|
|
|
+ private void SendNotifications(Delivery delivery)
|
|
|
{
|
|
|
- private void SendNotifications(Delivery delivery)
|
|
|
- {
|
|
|
- if (delivery == null)
|
|
|
- return;
|
|
|
- var delnumber = delivery.Number;
|
|
|
- var jobid = delivery.Job.ID;
|
|
|
- var delemp = delivery.Employee.ID;
|
|
|
-
|
|
|
- // Get Job Details
|
|
|
- var job = jobid.Equals(Guid.Empty)
|
|
|
- ? null
|
|
|
- : Provider.Query(
|
|
|
- new Filter<Job>(x => x.ID).IsEqualTo(jobid),
|
|
|
- Columns.None<Job>().Add(
|
|
|
- x => x.JobNumber,
|
|
|
- x => x.Name,
|
|
|
- x => x.SiteLead.ID,
|
|
|
- x => x.ProjectLead.ID
|
|
|
- )
|
|
|
- ).Rows.FirstOrDefault();
|
|
|
- var jobnumber = job != null ? job.Get<Job, string>(x => x.JobNumber) : "";
|
|
|
- var jobname = job != null ? job.Get<Job, string>(x => x.Name) : "";
|
|
|
- var sitelead = job != null ? job.Get<Job, Guid>(x => x.SiteLead.ID) : Guid.Empty;
|
|
|
- var projlead = job != null ? job.Get<Job, Guid>(x => x.ProjectLead.ID) : Guid.Empty;
|
|
|
-
|
|
|
- // Scan for Delivery Items and Build the List to Send
|
|
|
- var items = Provider.Query(
|
|
|
- new Filter<DeliveryItem>(x => x.Delivery.ID).IsEqualTo(delivery.ID),
|
|
|
- Columns.None<DeliveryItem>().Add(
|
|
|
- x => x.ID,
|
|
|
- x => x.ShipmentLink.ID,
|
|
|
- x => x.ShipmentLink.Code,
|
|
|
- x => x.RequisitionLink.ID,
|
|
|
- x => x.RequisitionLink.Number,
|
|
|
- x => x.Piece,
|
|
|
- x => x.Title,
|
|
|
- x => x.Barcode,
|
|
|
- x => x.ManufacturingPacketLink.Serial,
|
|
|
- x => x.Description
|
|
|
+ if (delivery == null)
|
|
|
+ return;
|
|
|
+ var delnumber = delivery.Number;
|
|
|
+ var jobid = delivery.Job.ID;
|
|
|
+ var delemp = delivery.Employee.ID;
|
|
|
+
|
|
|
+ // Get Job Details
|
|
|
+ var job = jobid.Equals(Guid.Empty)
|
|
|
+ ? null
|
|
|
+ : Provider.Query(
|
|
|
+ new Filter<Job>(x => x.ID).IsEqualTo(jobid),
|
|
|
+ Columns.None<Job>().Add(
|
|
|
+ x => x.JobNumber,
|
|
|
+ x => x.Name,
|
|
|
+ x => x.SiteLead.ID,
|
|
|
+ x => x.ProjectLead.ID
|
|
|
)
|
|
|
- );
|
|
|
+ ).Rows.FirstOrDefault();
|
|
|
+ var jobnumber = job != null ? job.Get<Job, string>(x => x.JobNumber) : "";
|
|
|
+ var jobname = job != null ? job.Get<Job, string>(x => x.Name) : "";
|
|
|
+ var sitelead = job != null ? job.Get<Job, Guid>(x => x.SiteLead.ID) : Guid.Empty;
|
|
|
+ var projlead = job != null ? job.Get<Job, Guid>(x => x.ProjectLead.ID) : Guid.Empty;
|
|
|
|
|
|
- var lines = new Dictionary<string, List<string>>();
|
|
|
- foreach (var row in items.Rows)
|
|
|
+ // Scan for Delivery Items and Build the List to Send
|
|
|
+ var items = Provider.Query(
|
|
|
+ new Filter<DeliveryItem>(x => x.Delivery.ID).IsEqualTo(delivery.ID),
|
|
|
+ Columns.None<DeliveryItem>().Add(
|
|
|
+ x => x.ID,
|
|
|
+ x => x.ShipmentLink.ID,
|
|
|
+ x => x.ShipmentLink.Code,
|
|
|
+ x => x.RequisitionLink.ID,
|
|
|
+ x => x.RequisitionLink.Number,
|
|
|
+ x => x.Piece,
|
|
|
+ x => x.Title,
|
|
|
+ x => x.Barcode,
|
|
|
+ x => x.ManufacturingPacketLink.Serial,
|
|
|
+ x => x.Description
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ var lines = new Dictionary<string, List<string>>();
|
|
|
+ foreach (var row in items.Rows)
|
|
|
+ {
|
|
|
+ var tag = "";
|
|
|
+ if (row.IsEntityLinkValid<DeliveryItem, ShipmentLink>(x => x.ShipmentLink))
|
|
|
+ tag = string.Format("Rack #{0}", row.Get<DeliveryItem, string>(c => c.ShipmentLink.Code));
|
|
|
+ else if (row.IsEntityLinkValid<DeliveryItem, RequisitionLink>(x => x.RequisitionLink))
|
|
|
+ tag = string.Format("Requisition #{0}", row.Get<DeliveryItem, int>(c => c.RequisitionLink.Number));
|
|
|
+ if (!string.IsNullOrWhiteSpace(tag))
|
|
|
{
|
|
|
- var tag = "";
|
|
|
- if (row.IsEntityLinkValid<DeliveryItem, ShipmentLink>(x => x.ShipmentLink))
|
|
|
- tag = string.Format("Rack #{0}", row.Get<DeliveryItem, string>(c => c.ShipmentLink.Code));
|
|
|
- else if (row.IsEntityLinkValid<DeliveryItem, RequisitionLink>(x => x.RequisitionLink))
|
|
|
- tag = string.Format("Requisition #{0}", row.Get<DeliveryItem, int>(c => c.RequisitionLink.Number));
|
|
|
- if (!string.IsNullOrWhiteSpace(tag))
|
|
|
- {
|
|
|
- if (!lines.ContainsKey(tag))
|
|
|
- lines[tag] = new List<string>();
|
|
|
- if (row.IsEntityLinkValid<DeliveryItem, RequisitionLink>(x => x.RequisitionLink))
|
|
|
- lines[tag].Add(string.Format("{0}: {1}", row.Get<DeliveryItem, string>(c => c.Piece),
|
|
|
- row.Get<DeliveryItem, string>(c => c.Description)));
|
|
|
-
|
|
|
- else
|
|
|
- lines[tag].Add(string.Format("{0}: {1} - {2}", row.Get<DeliveryItem, string>(c => c.Barcode),
|
|
|
- row.Get<DeliveryItem, string>(c => c.ManufacturingPacketLink.Serial), row.Get<DeliveryItem, string>(c => c.Description)));
|
|
|
- }
|
|
|
+ if (!lines.ContainsKey(tag))
|
|
|
+ lines[tag] = new List<string>();
|
|
|
+ if (row.IsEntityLinkValid<DeliveryItem, RequisitionLink>(x => x.RequisitionLink))
|
|
|
+ lines[tag].Add(string.Format("{0}: {1}", row.Get<DeliveryItem, string>(c => c.Piece),
|
|
|
+ row.Get<DeliveryItem, string>(c => c.Description)));
|
|
|
+
|
|
|
+ else
|
|
|
+ lines[tag].Add(string.Format("{0}: {1} - {2}", row.Get<DeliveryItem, string>(c => c.Barcode),
|
|
|
+ row.Get<DeliveryItem, string>(c => c.ManufacturingPacketLink.Serial), row.Get<DeliveryItem, string>(c => c.Description)));
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- var sb = new StringBuilder();
|
|
|
- sb.AppendLine("The following items have been delivered:");
|
|
|
- foreach (var key in lines.Keys)
|
|
|
+ var sb = new StringBuilder();
|
|
|
+ sb.AppendLine("The following items have been delivered:");
|
|
|
+ foreach (var key in lines.Keys)
|
|
|
+ {
|
|
|
+ sb.AppendLine(" ");
|
|
|
+ sb.AppendLine(key);
|
|
|
+ sb.AppendLine(new string('=', key.Length));
|
|
|
+ foreach (var line in lines[key])
|
|
|
+ sb.AppendLine(line);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Send the notifications
|
|
|
+ var emps = Provider.Query(
|
|
|
+ new Filter<Employee>(x => x.UserLink.UserID).IsEqualTo(UserID)
|
|
|
+ .Or(x => x.ID).IsEqualTo(delemp)
|
|
|
+ .Or(x => x.ID).IsEqualTo(sitelead)
|
|
|
+ .Or(x => x.ID).IsEqualTo(projlead)
|
|
|
+ ,
|
|
|
+ Columns.None<Employee>().Add(
|
|
|
+ x => x.ID,
|
|
|
+ x => x.UserLink.ID
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ var me = emps.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.UserLink.ID) == UserGuid);
|
|
|
+
|
|
|
+ var notifications = new List<Notification>();
|
|
|
+ foreach (var emp in emps.Rows)
|
|
|
+ if (emp.Get<Employee, Guid>(x => x.UserLink.ID) != UserGuid)
|
|
|
{
|
|
|
- sb.AppendLine(" ");
|
|
|
- sb.AppendLine(key);
|
|
|
- sb.AppendLine(new string('=', key.Length));
|
|
|
- foreach (var line in lines[key])
|
|
|
- sb.AppendLine(line);
|
|
|
+ var notification = new Notification();
|
|
|
+ notification.Employee.ID = emp.Get<Employee, Guid>(x => x.ID);
|
|
|
+ notification.Title = string.Format("Delivery #{0} ({1}: {2}) has been Delivered", delnumber, jobnumber, jobname);
|
|
|
+ notification.Description = sb.ToString();
|
|
|
+ notification.Job.ID = jobid;
|
|
|
+ notification.EntityType = typeof(Delivery).EntityName();
|
|
|
+ notification.EntityID = delivery.ID;
|
|
|
+ notification.Sender.ID = me != null ? me.Get<Employee, Guid>(x => x.ID) : Guid.Empty;
|
|
|
+ notifications.Add(notification);
|
|
|
}
|
|
|
|
|
|
- // Send the notifications
|
|
|
- var emps = Provider.Query(
|
|
|
- new Filter<Employee>(x => x.UserLink.UserID).IsEqualTo(UserID)
|
|
|
- .Or(x => x.ID).IsEqualTo(delemp)
|
|
|
- .Or(x => x.ID).IsEqualTo(sitelead)
|
|
|
- .Or(x => x.ID).IsEqualTo(projlead)
|
|
|
- ,
|
|
|
- Columns.None<Employee>().Add(
|
|
|
- x => x.ID,
|
|
|
- x => x.UserLink.ID
|
|
|
- )
|
|
|
- );
|
|
|
+ if (notifications.Any())
|
|
|
+ FindSubStore<Notification>().Save(notifications, "");
|
|
|
+ }
|
|
|
|
|
|
- var me = emps.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.UserLink.ID) == UserGuid);
|
|
|
+ private void UpdateEquipment(Delivery delivery)
|
|
|
+ {
|
|
|
+ var deliveryEquipments = Provider.Query(
|
|
|
+ new Filter<DeliveryEquipment>(x => x.Delivery.ID).IsEqualTo(delivery.ID),
|
|
|
+ Columns.None<DeliveryEquipment>()
|
|
|
+ .Add(x => x.Equipment.ID).Add(x => x.Type))
|
|
|
+ .ToObjects<DeliveryEquipment>();
|
|
|
+ var jobEquipments = Provider.Query(
|
|
|
+ new Filter<JobEquipment>(x => x.JobLink.ID).IsEqualTo(delivery.Job.ID),
|
|
|
+ Columns.Required<JobEquipment>()
|
|
|
+ .Add(x => x.ID)
|
|
|
+ .Add(x => x.EquipmentLink.ID))
|
|
|
+ .ToObjects<JobEquipment>()
|
|
|
+ .ToDictionary(x => x.EquipmentLink.ID);
|
|
|
|
|
|
- var notifications = new List<Notification>();
|
|
|
- foreach (var emp in emps.Rows)
|
|
|
- if (emp.Get<Employee, Guid>(x => x.UserLink.ID) != UserGuid)
|
|
|
- {
|
|
|
- var notification = new Notification();
|
|
|
- notification.Employee.ID = emp.Get<Employee, Guid>(x => x.ID);
|
|
|
- notification.Title = string.Format("Delivery #{0} ({1}: {2}) has been Delivered", delnumber, jobnumber, jobname);
|
|
|
- notification.Description = sb.ToString();
|
|
|
- notification.Job.ID = jobid;
|
|
|
- notification.EntityType = typeof(Delivery).EntityName();
|
|
|
- notification.EntityID = delivery.ID;
|
|
|
- notification.Sender.ID = me != null ? me.Get<Employee, Guid>(x => x.ID) : Guid.Empty;
|
|
|
- notifications.Add(notification);
|
|
|
- }
|
|
|
+ var jobEquipmentStore = FindSubStore<JobEquipment>();
|
|
|
|
|
|
- if (notifications.Any())
|
|
|
- FindSubStore<Notification>().Save(notifications, "");
|
|
|
+ var toSave = new List<JobEquipment>();
|
|
|
+ foreach(var deliveryEquipment in deliveryEquipments)
|
|
|
+ {
|
|
|
+ if(jobEquipments.TryGetValue(deliveryEquipment.Equipment.ID, out var jobEquipment))
|
|
|
+ {
|
|
|
+ jobEquipment.OnSite = deliveryEquipment.Type == DeliveryEquipmentType.DropOff;
|
|
|
+ toSave.Add(jobEquipment);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var newJobEquipment = new JobEquipment();
|
|
|
+ newJobEquipment.EquipmentLink.CopyFrom(deliveryEquipment.Equipment);
|
|
|
+ newJobEquipment.JobLink.CopyFrom(delivery.Job);
|
|
|
+ newJobEquipment.OnSite = deliveryEquipment.Type == DeliveryEquipmentType.DropOff;
|
|
|
+ toSave.Add(newJobEquipment);
|
|
|
+ }
|
|
|
}
|
|
|
+ jobEquipmentStore.Save(toSave, "Updated by delivery.");
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void AfterSave(Delivery entity)
|
|
|
+ {
|
|
|
+ base.AfterSave(entity);
|
|
|
|
|
|
- protected override void AfterSave(Delivery entity)
|
|
|
+ if (!entity.Delivered.IsEmpty() && entity.HasOriginalValue(x => x.Delivered) && entity.GetOriginalValue(x => x.Delivered).IsEmpty())
|
|
|
{
|
|
|
- base.AfterSave(entity);
|
|
|
+ SendNotifications(entity);
|
|
|
+ UpdateEquipment(entity);
|
|
|
+ }
|
|
|
|
|
|
- if (!entity.Delivered.IsEmpty() && entity.HasOriginalValue(x => x.Delivered) && entity.GetOriginalValue(x => x.Delivered).IsEmpty())
|
|
|
- SendNotifications(entity);
|
|
|
+ UpdateTrackingKanban<DeliveryKanban, Delivery, DeliveryLink>(entity, d =>
|
|
|
+ {
|
|
|
+ return !entity.Delivered.IsEmpty()
|
|
|
+ ? KanbanStatus.Complete
|
|
|
+ : entity.Assignment.IsValid()
|
|
|
+ ? KanbanStatus.InProgress
|
|
|
+ : KanbanStatus.Open;
|
|
|
+ });
|
|
|
|
|
|
- UpdateTrackingKanban<DeliveryKanban, Delivery, DeliveryLink>(entity, d =>
|
|
|
- {
|
|
|
- return !entity.Delivered.IsEmpty()
|
|
|
- ? KanbanStatus.Complete
|
|
|
- : entity.Assignment.IsValid()
|
|
|
- ? KanbanStatus.InProgress
|
|
|
- : KanbanStatus.Open;
|
|
|
- });
|
|
|
-
|
|
|
- //requi tracking kanban is usually updated in RequisitionStore, the requi is not necessarily saved when a delivery is completed
|
|
|
- if (entity.Delivered != DateTime.MinValue)
|
|
|
+ //requi tracking kanban is usually updated in RequisitionStore, the requi is not necessarily saved when a delivery is completed
|
|
|
+ if (entity.Delivered != DateTime.MinValue)
|
|
|
+ {
|
|
|
+ CoreTable table = Provider.Query<Requisition>(new Filter<Requisition>(x => x.Delivery.ID).IsEqualTo(entity.ID));
|
|
|
+ if (table.Rows.Any())
|
|
|
{
|
|
|
- CoreTable table = Provider.Query<Requisition>(new Filter<Requisition>(x => x.Delivery.ID).IsEqualTo(entity.ID));
|
|
|
- if (table.Rows.Any())
|
|
|
+ foreach (CoreRow row in table.Rows)
|
|
|
{
|
|
|
- foreach (CoreRow row in table.Rows)
|
|
|
- {
|
|
|
- Requisition requisition = row.ToObject<Requisition>();
|
|
|
- UpdateTrackingKanban<RequisitionKanban, Requisition, RequisitionLink>(requisition, e => { return KanbanStatus.Complete; });
|
|
|
- }
|
|
|
+ Requisition requisition = row.ToObject<Requisition>();
|
|
|
+ UpdateTrackingKanban<RequisitionKanban, Requisition, RequisitionLink>(requisition, e => { return KanbanStatus.Complete; });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- protected override void BeforeDelete(Delivery entity)
|
|
|
- {
|
|
|
- UnlinkTrackingKanban<DeliveryKanban, Delivery, DeliveryLink>(entity);
|
|
|
- }
|
|
|
+ protected override void BeforeDelete(Delivery entity)
|
|
|
+ {
|
|
|
+ UnlinkTrackingKanban<DeliveryKanban, Delivery, DeliveryLink>(entity);
|
|
|
}
|
|
|
}
|