123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using com.healthmarketscience.jackcess.query;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using InABox.WPF;
- namespace PRSDesktop;
- public class ProblemsDockGrid : DynamicDataGrid<Problems>
- {
- protected override void Init()
- {
- base.Init();
- HiddenColumns.Add(x=>x.Problem.Notes);
- HiddenColumns.Add(x=>x.Problem.AssignedTo.Code);
-
-
- ActionColumns.Add(new DynamicImageColumn(TypeImage)
- {
- Position = DynamicActionColumnPosition.Start,
- Filters = IMAGES.Keys.Select(x=>x.Name.Split('.').Last()).ToArray(),
- GetFilterExpression = (col) => TypeExpression(col),
- });
-
- ActionColumns.Add(new DynamicTextColumn(GetLastNote) { Position = DynamicActionColumnPosition.End, Width = 0, HeaderText = "Issues" });
- ActionColumns.Add(new DynamicTextColumn(GetAssignedTo) { Position = DynamicActionColumnPosition.End, Width = 100, HeaderText="Assigned To", Alignment = Alignment.MiddleCenter});
- ActionColumns.Add(new DynamicMenuColumn(ProblemMenu) { Position = DynamicActionColumnPosition.End });
- AddButton("Add Note", PRSDesktop.Resources.notification.AsBitmapImage(), AddNote);
- AddButton("Assign To", PRSDesktop.Resources.employee.AsBitmapImage(), AssignTo);
- AddButton("Mark Resolved", PRSDesktop.Resources.delete.AsBitmapImage(), MarkResolved,
- position: DynamicGridButtonPosition.Right);
- }
-
- private string TypeExpression(DynamicActionColumn column)
- {
- return column.CreateFilterExpression("Type") ?? "";
- }
-
- private bool TypeFilter(CoreRow row, string[] filter)
- {
- string typename = row.Get<Problems, string>(x => x.Type);
- return filter.Contains(typename);
- }
- private object? GetAssignedTo(CoreRow? row) => row?.Get<Problems, string>(x => x.Problem.AssignedTo.Code);
- private object? GetLastNote(CoreRow? row) => row?.Get<Problems, string[]>(x => x.Problem.Notes).LastOrDefault();
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.FilterRows = true;
- options.MultiSelect = true;
- }
- protected override DynamicGridColumns LoadColumns()
- {
- var result = new DynamicGridColumns();
- result.Add<Problems, string>(x => x.Code, 150, "Code", "", Alignment.MiddleLeft);
- result.Add<Problems, string>(x => x.Description, 0, "Description", "", Alignment.MiddleLeft);
- return result;
- }
- private static readonly Dictionary<Type, BitmapImage?> IMAGES = new()
- {
- { typeof(Bill), PRSDesktop.Resources.bill.AsBitmapImage() },
- { typeof(Activity), PRSDesktop.Resources.assignments.AsBitmapImage() },
- { typeof(ProductStyle), PRSDesktop.Resources.palette.AsBitmapImage() },
- { typeof(Product), PRSDesktop.Resources.product.AsBitmapImage() },
- { typeof(CostSheet), PRSDesktop.Resources.costsheet.AsBitmapImage() },
- { typeof(Kit), PRSDesktop.Resources.kit.AsBitmapImage() },
- { typeof(JobBillOfMaterialsItem), PRSDesktop.Resources.box_sml.AsBitmapImage() },
- { typeof(JobRequisitionItem), PRSDesktop.Resources.requisition.AsBitmapImage() },
- { typeof(ManufacturingPacket), PRSDesktop.Resources.factory.AsBitmapImage() },
- };
-
- private BitmapImage? TypeImage(CoreRow? row)
- {
- var _type = row?.Get<Problems, string>(x => x.Type) ?? "";
- var _key = IMAGES.Keys.FirstOrDefault(x => string.Equals(x.Name.Split('.').Last(), _type));
- return _key != null
- ? IMAGES[_key]
- : null;
- }
-
- private string? TypeText(CoreRow? row)
- {
- return row?.Get<Problems, string>(x => x.Type) ?? "";
- }
- private void ProblemMenu(DynamicMenuColumn menu, CoreRow? row)
- {
- menu.AddItem("Add Note", null, AddNote);
- menu.AddItem("Assign To...", null, AssignTo);
- menu.AddSeparator();
- menu.AddItem("Edit Item", null, EditItem);
- menu.AddSeparator();
- menu.AddItem("Mark as Resolved", null, MarkResolved);
- }
-
- private Type? GetType(CoreRow? row)
- {
- var _name = row?.Get<Problems, string>(x => x.Type) ?? "";
- var _type = IMAGES.Keys.FirstOrDefault(x => string.Equals(x.Name.Split('.').Last(), _name));
- return _type;
- }
-
- private IProblems<ManagedProblem>? GetItem(CoreRow? row)
- {
- var _type = GetType(row);
- if (_type == null)
- return null;
- var _id = row.Get<Problems, Guid>(x => x.ID);
- var item = ClientFactory.CreateClient(_type).Query(
- Filter.Create(_type, "ID", Operator.IsEqualTo, _id),
- Columns.Local(_type),
- null
- ).Rows.FirstOrDefault()?.ToObject(_type) as IProblems<ManagedProblem>;
- return item;
- }
- private void AddNote(CoreRow[] rows)
- {
- var _items = rows.Select(GetItem).Where(x => x != null).ToArray();
- if (!_items.Any())
- return;
-
- var _note = "";
- if (TextBoxDialog.Execute("Add Problem Note", ref _note))
- {
-
- Dictionary<Type, List<IProblems<ManagedProblem>>> _updates = new();
- foreach (var _item in _items)
- {
- if (_item == null)
- continue;
- var _notes = _item.Problem.Notes?.ToList() ?? new List<string>();
- _notes.Add(_note);
- _item.Problem.Notes = _notes.ToArray();
- var _type = _item.GetType();
- if (!_updates.ContainsKey(_type))
- _updates[_type] = new List<IProblems<ManagedProblem>>();
- _updates[_type].Add(_item);
- }
-
- Progress.ShowModal("Adding Notes", progress =>
- {
- foreach (var _update in _updates)
- {
- progress.Report($"Adding note to {_update.Value.Count} {new Inflector.Inflector(new CultureInfo("en")).Pluralize(_update.Key.Name.Split('.').Last()).SplitCamelCase()}");
- ClientFactory.CreateClient(_update.Key).Save(_update.Value, "Added Isse note");
- }
- });
-
- Refresh(false, true);
- }
- }
-
- private bool AddNote(Button button, CoreRow[] rows)
- {
- AddNote(rows);
- return false;
- }
-
- private void AddNote(CoreRow? row)
- {
- if (row == null)
- return;
- AddNote([row]);
- }
-
- private void MarkResolved(CoreRow?[] rows)
- {
-
- var _items = rows.Select(GetItem).Where(x => x != null).ToArray();
- if (!_items.Any())
- return;
-
- Dictionary<Type, List<IProblems<ManagedProblem>>> _updates = new();
- foreach (var _item in _items)
- {
- if (_item == null)
- continue;
- _item.Problem.Resolved = DateTime.Now;
- var _type = _item.GetType();
- if (!_updates.ContainsKey(_type))
- _updates[_type] = new List<IProblems<ManagedProblem>>();
- _updates[_type].Add(_item);
- }
-
- Progress.ShowModal("Resolving items", progress =>
- {
- foreach (var _update in _updates)
- {
- progress.Report($"Resolving {_update.Value.Count} {new Inflector.Inflector(new CultureInfo("en")).Pluralize(_update.Key.Name.Split('.').Last()).SplitCamelCase()}");
- ClientFactory.CreateClient(_update.Key).Save(_update.Value, "Issue marked as resolved");
- }
- });
- Refresh(false, true);
- }
-
- private void MarkResolved(CoreRow? row)
- {
- if (row == null)
- return;
- MarkResolved([row]);
- }
-
- private bool MarkResolved(Button button, CoreRow[] rows)
- {
- if (MessageWindow.ShowYesNo("Mark Selected Issues as resolved?","Confirm"))
- MarkResolved(rows);
- return false;
- }
-
- private void AssignTo(CoreRow[] rows)
- {
- var _items = rows.Select(GetItem).ToArray();
- if (!_items.Any())
- return;
- var _dlg = new MultiSelectDialog<Employee>(LookupFactory.DefineFilter<Employee>(), null, false);
- if (_dlg.ShowDialog())
- {
- Dictionary<Type, List<IProblems<ManagedProblem>>> _updates = new();
- foreach (var _item in _items)
- {
- if (_item == null)
- continue;
- _item.Problem.AssignedTo.ID = _dlg.IDs().FirstOrDefault();
- var _type = _item.GetType();
- if (!_updates.ContainsKey(_type))
- _updates[_type] = new List<IProblems<ManagedProblem>>();
- _updates[_type].Add(_item);
- }
- Progress.ShowModal("Reassigning items", progress =>
- {
- foreach (var _update in _updates)
- {
- progress.Report($"Reassigning {_update.Value.Count} {new Inflector.Inflector(new CultureInfo("en")).Pluralize(_update.Key.Name.Split('.').Last()).SplitCamelCase()}");
- ClientFactory.CreateClient(_update.Key).Save(_update.Value, "Reassigning Problem");
- }
- });
-
- Refresh(false, true);
- }
- }
-
- private bool AssignTo(Button button, CoreRow[] rows)
- {
- AssignTo(rows);
- return false;
- }
- private void AssignTo(CoreRow? row)
- {
- if (row != null)
- return;
- AssignTo([ row ]);
- }
- protected override void DoDoubleClick(object sender, DynamicGridCellClickEventArgs args)
- {
- base.DoDoubleClick(sender, args);
- if (args.Row == null)
- return;
- EditItem(args.Row);
- }
- private void EditItem(CoreRow? row)
- {
- var _type = GetType(row);
- if (_type == null)
- return;
- var _item = GetItem(row);
- if (_item == null)
- return;
- var _grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), _type);
- if (_grid.EditItems([ _item ], null, false, this))
- Refresh(false,true);
- }
- }
|