| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | 
							- 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<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),
 
-                     new Columns<Job>(
 
-                         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),
 
-                 new Columns<DeliveryItem>(
 
-                     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))
 
-                 {
 
-                     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)
 
-             {
 
-                 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)
 
-                 ,
 
-                 new Columns<Employee>(
 
-                     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)
 
-                 {
 
-                     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);
 
-                 }
 
-             if (notifications.Any())
 
-                 FindSubStore<Notification>().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<DeliveryKanban, Delivery, DeliveryLink>(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<Requisition>(new Filter<Requisition>(x => x.Delivery.ID).IsEqualTo(entity.ID));
 
-                 if (table.Rows.Any())
 
-                 {
 
-                     foreach (CoreRow row in table.Rows)
 
-                     {
 
-                         Requisition requisition = row.ToObject<Requisition>();
 
-                         UpdateTrackingKanban<RequisitionKanban, Requisition, RequisitionLink>(requisition, e => { return KanbanCategory.Complete; });
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         protected override void BeforeDelete(Delivery entity)
 
-         {
 
-             UnlinkTrackingKanban<DeliveryKanban, Delivery, DeliveryLink>(entity);
 
-         }
 
-     }
 
- }
 
 
  |