123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- namespace PRSDesktop
- {
- public static class NotificationUtils
- {
- private static Entity LoadEntity(Type type, Guid id, params string[] columnnames)
- {
- var filter = Activator.CreateInstance(typeof(Filter<>).MakeGenericType(type), "ID", Operator.IsEqualTo, id);
- IColumns? columns = null;
- if (columnnames.Any())
- {
- columns = Columns.Create(type);
- foreach (var columnname in columnnames)
- columns.Add(columnname);
- }
- var table = ClientFactory.CreateClient(type).Query(filter, columns);
- var result = table.Rows.FirstOrDefault()?.ToObject(type) as Entity;
- return result;
- }
- private static IDynamicGrid FindDataGrid(Type type)
- {
- var gridtypes = CoreUtils.TypeList(
- AppDomain.CurrentDomain.GetAssemblies(),
- myType =>
- myType.IsClass
- && !myType.IsAbstract
- && !myType.IsGenericType
- && myType.BaseType != null
- && myType.BaseType.Name == typeof(DynamicDataGrid<>).Name
- && myType.ContainsInheritedGenericType(type)
- ).ToArray();
- var gridtype = gridtypes.Any() ? gridtypes.First() : typeof(DynamicDataGrid<>).MakeGenericType(type);
- var grid = Activator.CreateInstance(gridtype) as IDynamicGrid;
- return grid;
- }
- private static bool PerformAction(Guid[] ids, Func<Notification, bool> action)
- {
- if (!ids.Any())
- //if (Notifications.SelectedRows?.FirstOrDefault() == null)
- {
- MessageBox.Show("Please select a message first!");
- return false;
- }
- var updates = new List<Notification>();
- Notification[] notifications = { };
- var columns = new Columns<Notification>().Default(ColumnType.IncludeForeignKeys);
- columns.Add(x => x.EntityID);
- columns.Add(x => x.EntityType);
- columns.Add(x => x.Closed);
- columns.Add(x => x.Sender.Name);
- columns.Add(x => x.Description);
- using (new WaitCursor())
- {
- notifications = new Client<Notification>().Query(
- new Filter<Notification>(x => x.ID).InList(ids),
- columns
- ).Rows.Select(x => x.ToObject<Notification>()).ToArray();
- }
- foreach (var notification in notifications)
- if (action.Invoke(notification))
- updates.Add(notification);
- if (updates.Any())
- {
- using (new WaitCursor())
- {
- new Client<Notification>().Save(updates, "");
- }
- return true;
- }
- return false;
- }
- public static bool IsDigitalForm(string name)
- {
- if (string.IsNullOrWhiteSpace(name))
- return false;
- var type = CoreUtils.GetEntity(name);
- var result = type.GetInterfaces().Contains(typeof(IDigitalFormInstance));
- return result;
- }
- public static Type? GetEntityType(string name)
- {
- if (string.IsNullOrWhiteSpace(name))
- return null;
- if (!IsDigitalForm(name))
- return CoreUtils.GetEntityOrNull(name);
- var form = CoreUtils.GetEntityOrNull(name);
- if (form is null)
- return null;
- var parent = CoreUtils.GetProperty(form, "Parent");
- var type = parent.PropertyType.GetInheritedGenericTypeArguments().FirstOrDefault();
- return type;
- }
- public static bool MakeVisible(FrameworkElement element, float left, float right, params bool[] visibleconditions)
- {
- var visible = true;
- foreach (var condition in visibleconditions)
- visible = visible && condition;
- element.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
- element.Margin = new Thickness(left, element.Margin.Top, right, element.Margin.Bottom);
- return visible;
- }
- public static bool ViewEntity(Guid[] ids)
- {
- return PerformAction(ids, notification =>
- {
- var type = GetEntityType(notification.EntityType);
- var id = notification.EntityID;
- if (IsDigitalForm(notification.EntityType))
- {
- var form = (LoadEntity(
- CoreUtils.GetEntity(notification.EntityType),
- notification.EntityID,
- "ID", "Parent.ID") as IDigitalFormInstance)!;
- id = form.ParentID();
- }
- var entity = LoadEntity(type, id);
- var grid = FindDataGrid(type);
- return grid.EditItems(new object[] { entity });
- });
- }
- public static bool ViewForm(Guid[] ids)
- {
- return PerformAction(ids, notification =>
- {
- var type = CoreUtils.GetEntity(notification.EntityType);
- var form = LoadEntity(type, notification.EntityID);
- if(form is IDigitalFormInstance formInstance)
- {
- if(DynamicFormEditWindow.EditDigitalForm(formInstance, out var dataModel)){
- dataModel.Update(null);
- return true;
- }
- }
- return false;
- });
- }
- public static bool ReplyOrForward(Guid[] ids, string prefix, bool selectrecipient, bool sentfolder)
- {
- return PerformAction(ids, notification =>
- {
- var sb = new StringBuilder(string.Format("\n\nAt {0:HH:mmtt} on {0:dd MMM yy}, {1} wrote:\n", notification.Created,
- notification.Sender.Name));
- var desc = string.IsNullOrWhiteSpace(notification.Description) ? "" : CoreUtils.StripHTML(notification.Description);
- foreach (var line in desc.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Where(x => !string.IsNullOrWhiteSpace(x)))
- sb.AppendFormat("> {0}\n", line.Trim());
- var form = new NotificationForm
- {
- Subject = string.Format("{0} {1}", prefix, notification.Title),
- Recipient = selectrecipient ? sentfolder ? notification.Employee.ID : notification.Sender.ID : Guid.Empty,
- JobID = notification.Job.ID,
- Description = sb.ToString()
- };
- if (form.ShowDialog() == true)
- {
- notification.Description = string.Format("{0}\nReplied to Message", notification.Description);
- if (MessageBox.Show("Do you want to dismiss this message?", "Task Created", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
- notification.Closed = DateTime.Now;
- return true;
- }
- return false;
- });
- }
- public static bool CreateTask(Guid[] ids, Guid employeeid)
- {
- return PerformAction(ids, notification =>
- {
- var task = new Kanban
- {
- Title = notification.Title,
- Notes = new[] { notification.Description },
- Category = "Open"
- };
- task.JobLink.ID = notification.Job.ID;
- task.EmployeeLink.ID = employeeid;
- task.ManagerLink.ID = notification.Sender.ID;
- var kg = new KanbanGrid();
- if (kg.EditItems(new[] { task }))
- {
- using (new WaitCursor())
- {
- task = new Client<Kanban>().Load(new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID)).FirstOrDefault();
- }
- notification.Description = string.Format("{0}\nCreated Task for {1}", notification.Description, task.EmployeeLink.Name);
- //notification.Kanban.ID = task.ID;
- if (MessageBox.Show("Do you want to dismiss this message?", "Task Created", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
- notification.Closed = DateTime.Now;
- return true;
- }
- return false;
- });
- }
- public static bool CreateRequi(Guid[] ids, Guid employeeid)
- {
- return PerformAction(ids, notification =>
- {
- var requi = new Requisition
- {
- Title = notification.Title,
- Notes = new[] { notification.Description }
- };
- requi.JobLink.ID = notification.Job.ID;
- requi.Employee.ID = employeeid;
- requi.RequestedBy.ID = notification.Sender.ID;
- var grid = new DynamicDataGrid<Requisition>();
- if (grid.EditItems(new[] { requi }))
- {
- notification.Description = string.Format("{0}\nCreated Requi# {1}", notification.Description, requi.Number);
- //notification.Requisition.ID = requi.ID;
- if (MessageBox.Show("Do you want to dismiss this message?", "Requisition Created", MessageBoxButton.YesNo) ==
- MessageBoxResult.Yes)
- notification.Closed = DateTime.Now;
- return true;
- }
- return false;
- });
- }
- public static bool CreateDelivery(Guid[] ids)
- {
- return PerformAction(ids, notification =>
- {
- var delivery = new Delivery
- {
- Notes = notification.Description,
- Due = DateTime.Today.AddDays(1)
- };
- delivery.Employee.ID = notification.Sender.ID;
- delivery.Job.ID = notification.Job.ID;
- var grid = new DynamicDataGrid<Delivery>();
- if (grid.EditItems(new[] { delivery }))
- {
- notification.Description = string.Format("{0}\nCreated Delivery# {1}", notification.Description, delivery.Number);
- //notification.Delivery.ID = delivery.ID;
- if (MessageBox.Show("Do you want to dismiss this message?", "Delivery Created", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
- notification.Closed = DateTime.Now;
- return true;
- }
- return false;
- });
- }
- public static bool AttachToJob(Guid[] ids)
- {
- return PerformAction(ids, notification =>
- {
- var jobs = new MultiSelectDialog<Job>(
- null,
- new Columns<Job>(x => x.ID), //new System.Linq.Expressions.Expression<Func<Job, object>>[] { x=>x.ID },
- false
- );
- if (jobs.ShowDialog() != true)
- return false;
- notification.Job.ID = jobs.IDs().First();
- return true;
- });
- }
- public static bool ViewJob(Guid[] ids)
- {
- return PerformAction(ids, notification =>
- {
- var job = new Client<Job>().Load(new Filter<Job>(x => x.ID).IsEqualTo(notification.Job.ID)).FirstOrDefault();
- if (job != null)
- FindDataGrid(typeof(Job)).EditItems(new[] { job });
- return false;
- });
- }
- public static bool CreateSetout(Guid[] ids)
- {
- return PerformAction(ids, notification =>
- {
- var templates = new MultiSelectDialog<ManufacturingTemplate>(
- null,
- new Columns<ManufacturingTemplate>(x =>
- x.ID), //new System.Linq.Expressions.Expression<Func<ManufacturingTemplate, object>>[] { x=>x.ID },
- false
- );
- if (templates.ShowDialog() != true)
- return false;
- var template = templates.Items().FirstOrDefault();
- Setout setout = null;
- ManufacturingPacket packet = null;
- var pstages = new List<ManufacturingPacketStage>();
- using (new WaitCursor())
- {
- var setoutnumber = string.Format("{0}-{1:yyMMdd}-", notification.Job.JobNumber, DateTime.Today);
- var setouts = new Client<Setout>().Query(
- new Filter<Setout>(x => x.JobLink.ID).IsEqualTo(notification.Job.ID).And(x => x.Number).BeginsWith(setoutnumber),
- new Columns<Setout>(x => x.Number),
- new SortOrder<Setout>(x => x.Number)
- ).Rows;
- var i = 1;
- while (setouts.Any(r => r.Get<Setout, string>(c => c.Number).Equals(string.Format("{0}{1:D3}", setoutnumber, i))))
- i++;
- setout = new Setout
- {
- Description = notification.Description,
- Reference = notification.Title,
- Number = string.Format("{0}{1:D2}", setoutnumber, i)
- };
- setout.JobLink.ID = notification.Job.ID;
- new Client<Setout>().Save(setout, "Created from Notification");
- packet = new ManufacturingPacket();
- packet.SetoutLink.ID = setout.ID;
- //packet.JobLink.ID = notification.Job.ID;
- packet.Serial = string.Format("{0}-001", setout.Number);
- packet.Group = template.Factory.Name;
- packet.ManufacturingTemplateLink.ID = template.ID;
- packet.ManufacturingTemplateLink.Code = template.Code;
- packet.Title = notification.Title;
- packet.Quantity = 1;
- new Client<ManufacturingPacket>().Save(packet, "Created from Notification");
- var tstages = new Client<ManufacturingTemplateStage>().Load(
- new Filter<ManufacturingTemplateStage>(x => x.Template.ID).IsEqualTo(packet.ManufacturingTemplateLink.ID),
- new SortOrder<ManufacturingTemplateStage>(x => x.Sequence)
- );
- foreach (var tstage in tstages)
- {
- var pstage = new ManufacturingPacketStage
- {
- Time = tstage.Time,
- Sequence = tstage.Sequence,
- SequenceType = tstage.SequenceType,
- Started = DateTime.MinValue,
- PercentageComplete = 0.0F,
- Completed = DateTime.MinValue,
- QualityChecks = tstage.QualityChecks,
- QualityStatus = QualityStatus.NotChecked,
- QualityNotes = ""
- };
- var section = new Client<ManufacturingSection>()
- .Load(new Filter<ManufacturingSection>(x => x.ID).IsEqualTo(tstage.Section.ID))
- .FirstOrDefault();
- if (section != null)
- {
- pstage.ManufacturingSectionLink.ID = tstage.Section.ID;
- pstage.ManufacturingSectionLink.Name = tstage.Section.Name;
- }
- pstages.Add(pstage);
- }
- new Client<ManufacturingPacketStage>().Save(pstages, "Created from Notification");
- }
- var grid = new DynamicDataGrid<Setout>();
- if (grid.EditItems(new[] { setout }))
- {
- notification.Description = string.Format("{0}\nCreated Setout# {1}", notification.Description, setout.Number);
- //notification.Setout.ID = setout.ID;
- if (MessageBox.Show("Do you want to dismiss this message?", "Setout Created", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
- notification.Closed = DateTime.Now;
- return true;
- }
- using (new WaitCursor())
- {
- // Cascade on PAcket / PacketStage should** take care of the child records
- // Be good to keep an eye on that!
- new Client<Setout>().Delete(setout, "");
- }
- return false;
- });
- }
- public static bool Archive(Guid[] ids)
- {
- return PerformAction(ids, notification =>
- {
- notification.Closed = DateTime.Now;
- return true;
- });
- }
- public static bool Reopen(Guid[] ids)
- {
- return PerformAction(ids, notification =>
- {
- notification.Closed = DateTime.MinValue;
- return true;
- });
- }
- }
- }
|