using System; using System.Collections.Generic; using System.Linq; using System.Text; using Comal.Classes; using InABox.Core; namespace Comal.Stores { internal class DeliveryStore : BaseStore { 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(x => x.ID).IsEqualTo(jobid), new Columns( x => x.JobNumber, x => x.Name, x => x.EmployeeLink.ID, x => x.ManagerLink.ID ) ).Rows.FirstOrDefault(); var jobnumber = job != null ? job.Get(x => x.JobNumber) : ""; var jobname = job != null ? job.Get(x => x.Name) : ""; var jobemp = job != null ? job.Get(x => x.EmployeeLink.ID) : Guid.Empty; var jobmgr = job != null ? job.Get(x => x.ManagerLink.ID) : Guid.Empty; // Scan for Delivery Items and Build the List to Send var items = Provider.Query( new Filter(x => x.Delivery.ID).IsEqualTo(delivery.ID), new Columns( 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>(); foreach (var row in items.Rows) { var tag = ""; if (row.IsEntityLinkValid(x => x.ShipmentLink)) tag = string.Format("Rack #{0}", row.Get(c => c.ShipmentLink.Code)); else if (row.IsEntityLinkValid(x => x.RequisitionLink)) tag = string.Format("Requisition #{0}", row.Get(c => c.RequisitionLink.Number)); if (!string.IsNullOrWhiteSpace(tag)) { if (!lines.ContainsKey(tag)) lines[tag] = new List(); if (row.IsEntityLinkValid(x => x.RequisitionLink)) lines[tag].Add(string.Format("{0}: {1}", row.Get(c => c.Piece), row.Get(c => c.Description))); else lines[tag].Add(string.Format("{0}: {1} - {2}", row.Get(c => c.Barcode), row.Get(c => c.ManufacturingPacketLink.Serial), row.Get(c => c.Description))); } } 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(x => x.UserLink.UserID).IsEqualTo(UserID) .Or(x => x.ID).IsEqualTo(delemp) .Or(x => x.ID).IsEqualTo(jobemp) .Or(x => x.ID).IsEqualTo(jobmgr) , new Columns( x => x.ID, x => x.UserLink.ID ) ); var me = emps.Rows.FirstOrDefault(r => r.Get(c => c.UserLink.ID) == UserGuid); var notifications = new List(); foreach (var emp in emps.Rows) if (emp.Get(x => x.UserLink.ID) != UserGuid) { var notification = new Notification(); notification.Employee.ID = emp.Get(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(x => x.ID) : Guid.Empty; notifications.Add(notification); } if (notifications.Any()) FindSubStore().Save(notifications, ""); } protected override void AfterSave(Delivery entity) { base.AfterSave(entity); if (!entity.Delivered.IsEmpty() && entity.HasOriginalValue(x => x.Delivered) && entity.GetOriginalValue(x => x.Delivered).IsEmpty()) SendNotifications(entity); UpdateTrackingKanban(entity, d => { return !entity.Delivered.IsEmpty() ? KanbanCategory.Complete : entity.Assignment.IsValid() ? KanbanCategory.InProgress : KanbanCategory.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) { CoreTable table = Provider.Query(new Filter(x => x.Delivery.ID).IsEqualTo(entity.ID)); if (table.Rows.Any()) { foreach (CoreRow row in table.Rows) { Requisition requisition = row.ToObject(); UpdateTrackingKanban(requisition, e => { return KanbanCategory.Complete; }); } } } } protected override void BeforeDelete(Delivery entity) { UnlinkTrackingKanban(entity); } } }