TasksByStatusControl.xaml.cs 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Reflection;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using Comal.Classes;
  17. using InABox.Clients;
  18. using InABox.Core;
  19. using InABox.DynamicGrid;
  20. using InABox.Wpf;
  21. using InABox.WPF;
  22. using org.omg.CORBA;
  23. using Syncfusion.UI.Xaml.Kanban;
  24. using Color = System.Drawing.Color;
  25. namespace PRSDesktop
  26. {
  27. public class EmployeeModel
  28. {
  29. public EmployeeModel(Guid id, string name, Guid thumbnail, BitmapImage image)
  30. {
  31. ID = id;
  32. Name = name;
  33. Image = image;
  34. ThumbnailID = thumbnail;
  35. }
  36. public Guid ID { get; set; }
  37. public string Name { get; set; }
  38. public BitmapImage Image { get; set; }
  39. public Guid ThumbnailID { get; set; }
  40. }
  41. public class StatusTasksHeaderTimeConverter : IValueConverter
  42. {
  43. public static IEnumerable<TaskModel> Tasks { get; set; }
  44. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  45. {
  46. if (Tasks == null)
  47. return "0:00";
  48. var dataContext = value as ColumnTag;
  49. if (dataContext == null)
  50. return "0:00";
  51. var getter = dataContext.GetType().GetProperty("Column", BindingFlags.NonPublic | BindingFlags.Instance);
  52. if (getter == null)
  53. return "0:00";
  54. var column = (KanbanColumn)getter.GetValue(dataContext);
  55. if (column == null)
  56. return "0:00";
  57. double result = 0.0F;
  58. foreach (var kanban in Tasks.Where(x => Equals(x.Category, column.Categories)))
  59. result += kanban.EstimatedTime.TotalHours;
  60. return string.Format("{0:F2}", result);
  61. }
  62. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  63. {
  64. throw new NotImplementedException();
  65. }
  66. }
  67. public class BoolToVisibilityConverter : IValueConverter
  68. {
  69. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  70. {
  71. if (Equals(value, true))
  72. return Visibility.Visible;
  73. return Visibility.Collapsed;
  74. }
  75. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  76. {
  77. throw new NotImplementedException();
  78. }
  79. }
  80. /// <summary>
  81. /// Interaction logic for KanbanPanel.xaml
  82. /// </summary>
  83. public partial class TasksByStatusControl : UserControl, ITaskControl
  84. {
  85. private enum Suppress
  86. {
  87. This
  88. }
  89. private BitmapImage _attachimg = PRSDesktop.Resources.attachment.AsBitmapImage();
  90. private readonly ObservableCollection<EmployeeModel> _employeelist = new();
  91. private CoreTable _employees;
  92. public CoreTable _kanbans;
  93. private BitmapImage _lockimg = PRSDesktop.Resources.lock_sml.AsBitmapImage();
  94. private ObservableCollection<TaskModel> _models = new();
  95. private CoreTable _types;
  96. public List<string> CheckedKanbans = new();
  97. // CoreUtils.FullGuid => All Staff
  98. // Guid.Empty => Unallocated
  99. // Anything Else => Actual Staff Member
  100. private Guid EmployeeID = Guid.Empty;
  101. private Guid? MyID;
  102. private string MyName = "";
  103. private string searchtext = "";
  104. private Guid selectedtype = CoreUtils.FullGuid;
  105. public TasksByStatusControl()
  106. {
  107. using (new EventSuppressor(Suppress.This))
  108. InitializeComponent();
  109. }
  110. public string SectionName => "Tasks By Status";
  111. public DataModel DataModel(Selection selection)
  112. {
  113. var ids = SelectedModels().Select(x => Guid.Parse(x.ID)).ToArray();
  114. return new AutoDataModel<Kanban>(new Filter<Kanban>(x => x.ID).InList(ids));
  115. }
  116. private void ResizeColumns()
  117. {
  118. //if (!bResizeRequired)
  119. // return;
  120. using (var d = Dispatcher.DisableProcessing())
  121. {
  122. var CollapsedWidth = 50;
  123. var CollapsedColumns = 0;
  124. Array.ForEach(Kanban.Columns.ToArray(), x => { CollapsedColumns += x.IsExpanded ? 0 : 1; });
  125. if (Kanban.Columns.Count > 0 && CollapsedColumns != Kanban.Columns.Count)
  126. {
  127. var ColumnWidth = (Kanban.ActualWidth - CollapsedColumns * CollapsedWidth) / (Kanban.Columns.Count - CollapsedColumns) - 2;
  128. if (ColumnWidth != Kanban.ColumnWidth) Kanban.ColumnWidth = ColumnWidth;
  129. //bResizeRequired = false;
  130. }
  131. }
  132. }
  133. private void TaskMenu_Opened(object sender, RoutedEventArgs e)
  134. {
  135. Host.PopulateMenu(this, sender as ContextMenu);
  136. }
  137. private void Kanban_SizeChanged(object sender, SizeChangedEventArgs e)
  138. {
  139. Kanban.ColumnWidth = Kanban.ActualWidth / Kanban.Columns.Count - 1.0F;
  140. }
  141. private void Kanban_CardDragStart(object sender, KanbanDragStartEventArgs e)
  142. {
  143. var models = SelectedModels(e.SelectedCard.Content as TaskModel);
  144. if (models.Any(x => x.Locked))
  145. e.IsCancel = true;
  146. }
  147. private void Kanban_CardDragEnd(object sender, KanbanDragEndEventArgs e)
  148. {
  149. using (new WaitCursor())
  150. {
  151. var target = e.TargetColumn.Categories;
  152. var models = SelectedModels(e.SelectedCard.Content as TaskModel).Where(x => !Equals(x.Category, target)).ToArray();
  153. if (!models.Any())
  154. return;
  155. var kanbans = Host.LoadKanbans(models, new Columns<Kanban>(x => x.ID, x => x.Category));
  156. foreach (var kanban in kanbans)
  157. kanban.Category = target;
  158. new Client<Kanban>().Save(kanbans, string.Format("Task Status Updated to {0}", target), (o, err) => { });
  159. foreach (var model in models)
  160. {
  161. model.Checked = false;
  162. model.Category = target;
  163. }
  164. FilterKanbans();
  165. }
  166. }
  167. private void Search_KeyUp(object sender, KeyEventArgs e)
  168. {
  169. if (string.IsNullOrWhiteSpace(Search.Text) || e.Key == Key.Return)
  170. {
  171. searchtext = Search.Text;
  172. Refresh(true);
  173. }
  174. }
  175. private void TaskTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
  176. {
  177. if (!IsReady || EventSuppressor.IsSet(Suppress.This))
  178. return;
  179. if (e.AddedItems.Count > 0)
  180. {
  181. var item = (KeyValuePair<Guid, string>)e.AddedItems[0];
  182. selectedtype = item.Key;
  183. }
  184. else
  185. {
  186. selectedtype = CoreUtils.FullGuid;
  187. }
  188. Host.KanbanSettings.StatusSettings.SelectedType = selectedtype;
  189. Host.SaveSettings();
  190. ReloadKanbans();
  191. }
  192. private void IncludeCompleted_Checked(object sender, RoutedEventArgs e)
  193. {
  194. if (!IsReady)
  195. return;
  196. Host.KanbanSettings.StatusSettings.IncludeCompleted = IncludeCompleted.IsChecked.Value;
  197. Host.SaveSettings();
  198. SetupColumns();
  199. ReloadKanbans();
  200. }
  201. private void IncludeObserved_Checked(object sender, RoutedEventArgs e)
  202. {
  203. if (!IsReady)
  204. return;
  205. Host.KanbanSettings.StatusSettings.IncludeObserved = IncludeObserved.IsChecked.Value;
  206. Host.SaveSettings();
  207. ReloadKanbans();
  208. }
  209. private void IncludeLocked_Checked(object sender, RoutedEventArgs e)
  210. {
  211. if (!IsReady)
  212. return;
  213. Host.KanbanSettings.StatusSettings.IncludeLocked = IncludeLocked.IsChecked.Value;
  214. Host.SaveSettings();
  215. ReloadKanbans();
  216. }
  217. private void ViewType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  218. {
  219. if (Kanban != null)
  220. Kanban.CardTemplate = ViewType.SelectedIndex > 0
  221. ? Resources["CompactKanban"] as DataTemplate
  222. : Resources["FullKanban"] as DataTemplate;
  223. if (!IsReady || EventSuppressor.IsSet(Suppress.This))
  224. return;
  225. Host.KanbanSettings.StatusSettings.CompactView = ViewType.SelectedIndex > 0;
  226. Host.SaveSettings();
  227. }
  228. private static bool UpdatingEmployees = false;
  229. private void Employees_SelectionChanged(object sender, SelectionChangedEventArgs e)
  230. {
  231. if (UpdatingEmployees || EventSuppressor.IsSet(Suppress.This))
  232. return;
  233. if (e.AddedItems.Count == 0)
  234. {
  235. EmployeeID = Guid.Empty;
  236. }
  237. else
  238. {
  239. var model = _employeelist[Employees.SelectedIndex];
  240. EmployeeID = model.ID;
  241. }
  242. CheckedKanbans.Clear();
  243. if (IsReady)
  244. Refresh(true);
  245. }
  246. private void Export_Click(object sender, RoutedEventArgs e)
  247. {
  248. var form = new DynamicExportForm(typeof(Kanban), _kanbans.Columns.Select(x => x.ColumnName));
  249. if (form.ShowDialog() != true)
  250. return;
  251. var export = new Client<Kanban>().Query(
  252. GetKanbanFilter(),
  253. new Columns<Kanban>(form.Fields),
  254. LookupFactory.DefineSort<Kanban>()
  255. );
  256. var employee = "Tasks for All Staff";
  257. if (EmployeeID != CoreUtils.FullGuid)
  258. {
  259. if (EmployeeID == Guid.Empty)
  260. {
  261. employee = "Unallocated Tasks";
  262. }
  263. else
  264. {
  265. var model = _employeelist.FirstOrDefault(x => x.ID.Equals(EmployeeID));
  266. employee = model == null ? "Tasks for (Unknown)" : "Tasks for " + (model.ID == MyID ? MyName : model.Name);
  267. }
  268. }
  269. ExcelExporter.DoExport<Kanban>(
  270. export,
  271. string.Format(
  272. "{0} ({1:dd-MMM-yy})",
  273. employee,
  274. DateTime.Today
  275. )
  276. );
  277. }
  278. #region ITaskControl Support
  279. public bool IsReady { get; set; }
  280. public ITaskHost Host { get; set; }
  281. public KanbanViewType KanbanViewType => KanbanViewType.Status;
  282. public IEnumerable<TaskModel> SelectedModels(TaskModel sender = null)
  283. {
  284. var result = _models.Where(x => x.Checked).ToList();
  285. if (sender != null && !result.Contains(sender))
  286. result.Add(sender);
  287. return result;
  288. }
  289. #endregion
  290. #region Setup
  291. public void Setup()
  292. {
  293. using (new EventSuppressor(Suppress.This))
  294. {
  295. SetupToolbar();
  296. SetupColumns();
  297. SetupData();
  298. SetupKanbanTypesLookup(false);
  299. SetupMyEmployee();
  300. SetupEmployeeList();
  301. }
  302. }
  303. private void SetupMyEmployee()
  304. {
  305. var row = _employees.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.UserLink.ID) == ClientFactory.UserGuid);
  306. if (row != null)
  307. {
  308. MyID = row.Get<Employee, Guid>(c => c.ID);
  309. MyName = row.Get<Employee, string>(x => x.Name);
  310. }
  311. }
  312. private void SetupEmployeeList()
  313. {
  314. IEnumerable<CoreRow> active = null;
  315. var anonymous = PRSDesktop.Resources.anonymous.AsBitmapImage();
  316. if (Security.IsAllowed<CanViewOthersTasks>())
  317. {
  318. active = _employees.Rows.Where(r =>
  319. r.Get<Employee, bool>(x => x.CanAllocateTasks) && (r.Get<Employee, DateTime>(x => x.FinishDate).IsEmpty() ||
  320. r.Get<Employee, DateTime>(x => x.FinishDate) > DateTime.Today));
  321. _employeelist.Add(new EmployeeModel(CoreUtils.FullGuid, "All Staff", Guid.Empty, PRSDesktop.Resources.everyone.AsBitmapImage()));
  322. _employeelist.Add(new EmployeeModel(Guid.Empty, "Unallocated", Guid.Empty, null));
  323. }
  324. else
  325. {
  326. active = _employees.Rows.Where(r => r.Get<Employee, Guid>(c => c.UserLink.ID).Equals(ClientFactory.UserGuid));
  327. }
  328. EmployeeModel selected = null;
  329. foreach (var row in active)
  330. {
  331. var id = row.Get<Employee, Guid>(x => x.ID);
  332. var userid = row.Get<Employee, Guid>(x => x.UserLink.ID);
  333. var thumbnailid = row.Get<Employee, Guid>(x => x.Thumbnail.ID);
  334. var name = userid.Equals(ClientFactory.UserGuid) ? "My Tasks" : row.Get<Employee, string>(x => x.Name);
  335. var model = new EmployeeModel(id, name, thumbnailid, anonymous);
  336. if (userid.Equals(ClientFactory.UserGuid))
  337. {
  338. _employeelist.Insert(0, model);
  339. selected = model;
  340. }
  341. else
  342. {
  343. _employeelist.Add(model);
  344. }
  345. }
  346. if (Security.IsAllowed<CanViewOthersTasks>())
  347. {
  348. EmployeeListColumn.Width = new GridLength(1.0F, GridUnitType.Auto);
  349. var thumbnails = active
  350. .Select(r => r.EntityLinkID<Employee, ImageDocumentLink>(x => x.Thumbnail) ?? Guid.Empty)
  351. .Where(x => x != Guid.Empty).ToArray();
  352. Employees.ItemsSource = _employeelist;
  353. Employees.SelectedItem = _employeelist.First();
  354. EmployeeID = _employeelist.First().ID;
  355. if (thumbnails.Any())
  356. new Client<Document>().Query(
  357. new Filter<Document>(x => x.ID).InList(thumbnails),
  358. new Columns<Document>(x => x.ID, x => x.Data),
  359. null,
  360. (data, error) =>
  361. {
  362. if (data != null)
  363. ProcessThumbnails(data);
  364. }
  365. );
  366. }
  367. else
  368. {
  369. EmployeeListColumn.Width = new GridLength(0.0F, GridUnitType.Pixel);
  370. Employees.ItemsSource = _employeelist;
  371. Employees.SelectedItem = _employeelist.First();
  372. EmployeeID = _employeelist.First().ID;
  373. }
  374. }
  375. private void ProcessThumbnails(CoreTable data)
  376. {
  377. Dispatcher.Invoke(() =>
  378. {
  379. foreach (var row in data.Rows)
  380. {
  381. var id = row.Get<Document, Guid>(x => x.ID);
  382. var model = _employeelist.FirstOrDefault(x => x.ThumbnailID.Equals(id));
  383. if (model != null)
  384. {
  385. model.Image = new BitmapImage();
  386. model.Image.LoadImage(row.Get<Document, byte[]>(x => x.Data));
  387. }
  388. }
  389. UpdatingEmployees = true;
  390. Employees.ItemsSource = null;
  391. Employees.ItemsSource = _employeelist;
  392. Employees.SelectedItem = _employeelist.First();
  393. EmployeeID = _employeelist.First().ID;
  394. UpdatingEmployees = false;
  395. });
  396. }
  397. private void SetupKanbanTypesLookup(bool reload)
  398. {
  399. if (ClientFactory.IsSupported<KanbanType>())
  400. {
  401. if (reload)
  402. {
  403. _types = new Client<KanbanType>().Query(
  404. new Filter<KanbanType>(x => x.Hidden).IsEqualTo(false),
  405. new Columns<KanbanType>(x => x.ID, x => x.Description),
  406. new SortOrder<KanbanType>(x => x.Description)
  407. );
  408. }
  409. var tasktypes = new Dictionary<Guid, string>
  410. {
  411. { CoreUtils.FullGuid, "All Types" },
  412. { Guid.Empty, "Unallocated Types" }
  413. };
  414. _types.IntoDictionary<KanbanType, Guid, string>(tasktypes, x => x.ID, row => row.Get<KanbanType, string>(x => x.Description));
  415. TaskTypes.ItemsSource = tasktypes;
  416. if (tasktypes.ContainsKey(Host.KanbanSettings.StatusSettings.SelectedType))
  417. TaskTypes.SelectedValue = Host.KanbanSettings.StatusSettings.SelectedType;
  418. else
  419. TaskTypes.SelectedValue = CoreUtils.FullGuid;
  420. TaskTypesLabel.Visibility = Visibility.Visible;
  421. TaskTypes.Visibility = Visibility.Visible;
  422. }
  423. else
  424. {
  425. TaskTypesLabel.Visibility = Visibility.Collapsed;
  426. TaskTypes.Visibility = Visibility.Collapsed;
  427. }
  428. }
  429. private void SetupToolbar()
  430. {
  431. IncludeCompleted.Visibility = Security.IsAllowed<CanHideTaskCompletedColumn>() ? Visibility.Visible : Visibility.Collapsed;
  432. IncludeCompleted.IsChecked = IncludeCompleted.Visibility == Visibility.Visible ? Host.KanbanSettings.StatusSettings.IncludeCompleted : true;
  433. IncludeObserved.IsChecked = Host.KanbanSettings.StatusSettings.IncludeObserved;
  434. ViewType.SelectedIndex = Host.KanbanSettings.StatusSettings.CompactView ? 1 : 0;
  435. }
  436. private void SetupColumns()
  437. {
  438. Kanban.Columns.Clear();
  439. var indicatorColorPalette = new IndicatorColorPalette();
  440. indicatorColorPalette.Add(new ColorMapping { Key = "Red", Color = Colors.LightSalmon });
  441. indicatorColorPalette.Add(new ColorMapping { Key = "Orange", Color = Colors.Orange });
  442. indicatorColorPalette.Add(new ColorMapping { Key = "Yellow", Color = Colors.LightYellow });
  443. indicatorColorPalette.Add(new ColorMapping { Key = "Green", Color = Colors.LightGreen });
  444. Kanban.IndicatorColorPalette = indicatorColorPalette;
  445. Kanban.Columns.Add(new KanbanColumn
  446. {
  447. Categories = "Open",
  448. Title = "To Do"
  449. });
  450. Kanban.Columns.Add(new KanbanColumn
  451. {
  452. Categories = "In Progress",
  453. Title = "In Progress"
  454. });
  455. Kanban.Columns.Add(new KanbanColumn
  456. {
  457. Categories = "Waiting",
  458. Title = "Waiting for Others"
  459. });
  460. if (Host.KanbanSettings.StatusSettings.IncludeCompleted)
  461. Kanban.Columns.Add(new KanbanColumn
  462. {
  463. Categories = "Complete",
  464. Title = "Completed"
  465. });
  466. Kanban.InvalidateVisual();
  467. foreach (var column in Kanban.Columns)
  468. {
  469. var menu = new ContextMenu();
  470. menu.Tag = column;
  471. var item = new MenuItem { Header = "New Task" };
  472. item.Click += CreateTask;
  473. menu.Items.Add(item);
  474. menu.Items.Add(new Separator());
  475. item = new MenuItem { Header = "Select All " + column.Title + " Tasks", Tag = column };
  476. item.Click += SelectAll_Click;
  477. menu.Items.Add(item);
  478. item = new MenuItem { Header = "Unselect All " + column.Title + " Tasks", Tag = column };
  479. item.Click += UnSelectAll_Click;
  480. menu.Items.Add(item);
  481. column.ContextMenu = menu;
  482. }
  483. Kanban.ColumnWidth = Kanban.ActualWidth / Kanban.Columns.Count - 1.0F;
  484. }
  485. private void SetupData()
  486. {
  487. var query = new MultiQuery();
  488. query.Add(
  489. null,
  490. new Columns<Employee>(x => x.ID, x => x.Name, x => x.Thumbnail.ID, x => x.CanAllocateTasks, x => x.Email, x => x.Mobile,
  491. x => x.FinishDate, x => x.UserLink.ID),
  492. new SortOrder<Employee>(x => x.Name)
  493. );
  494. if (ClientFactory.IsSupported<KanbanType>())
  495. query.Add(
  496. new Filter<KanbanType>(x => x.Hidden).IsEqualTo(false),
  497. new Columns<KanbanType>(x => x.ID, x => x.Description),
  498. new SortOrder<KanbanType>(x => x.Description)
  499. );
  500. query.Query();
  501. _employees = query.Get<Employee>();
  502. _types = ClientFactory.IsSupported<KanbanType>()
  503. ? query.Get<KanbanType>()
  504. : null;
  505. }
  506. #endregion
  507. #region Refresh / Reload
  508. private Filter<T> GetSearchFilter<T>(Expression<Func<T, object>> expression) where T : Entity, new()
  509. {
  510. Filter<T> result = null;
  511. var comps = searchtext.Trim().Split(' ');
  512. foreach (var comp in comps)
  513. result = result == null ? new Filter<T>(expression).Contains(comp) : result.And(expression).Contains(comp);
  514. return result;
  515. }
  516. private Filter<KanbanSubscriber> GetKanbanSubscriberFilter()
  517. {
  518. var filter = new Filter<KanbanSubscriber>(x => x.Kanban.Closed).IsEqualTo(DateTime.MinValue);
  519. if (Host.Job != null)
  520. {
  521. if (Host.Job.ID != Guid.Empty)
  522. filter = filter.And(x => x.Kanban.JobLink.ID).IsEqualTo(Host.Job.ID);
  523. else
  524. filter = filter.And(x => x.Kanban.JobLink.ID).None();
  525. }
  526. // All Tasks (EmployeeID.HasValue == false) or Unallocated (EmployeeID = Guid.Empty) are retrieved directly from the Kanban Table
  527. // so if we are here, we can assume that we are pulling subscriber data
  528. var empfilter = new Filter<KanbanSubscriber>(x => x.Employee.ID).IsEqualTo(EmployeeID);
  529. filter.Ands.Add(empfilter);
  530. if (EmployeeID != MyID)
  531. filter = filter.And(x => x.Kanban.Private).IsEqualTo(false);
  532. //if (!includeobserved)
  533. // filter = filter.And(new Filter<KanbanSubscriber>(x => x.Assignee).IsEqualTo(true).Or(x => x.Manager).IsEqualTo(true));
  534. //if (!includecompleted)
  535. // filter = filter.And(x => x.Kanban.Completed).IsEqualTo(DateTime.MinValue);
  536. //if (selectedtype != CoreUtils.FullGuid)
  537. // filter = filter.And(x => x.Kanban.Type.ID).IsEqualTo(selectedtype);
  538. if (!string.IsNullOrWhiteSpace(searchtext))
  539. {
  540. var search = GetSearchFilter<KanbanSubscriber>(x => x.Kanban.JobLink.Name);
  541. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.JobLink.JobNumber));
  542. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.Summary));
  543. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.Title));
  544. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.ManagerLink.Name));
  545. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.EmployeeLink.Name));
  546. if (int.TryParse(searchtext.Trim(), out var tasknumber))
  547. search.Ors.Add(new Filter<KanbanSubscriber>(x => x.Kanban.Number).IsEqualTo(tasknumber));
  548. filter.Ands.Add(search);
  549. }
  550. return filter;
  551. }
  552. private Filter<Kanban> GetKanbanFilter()
  553. {
  554. var filter = new Filter<Kanban>(x => x.Closed).IsEqualTo(DateTime.MinValue);
  555. if (Host.Job != null)
  556. {
  557. if (Host.Job.ID != Guid.Empty)
  558. filter = filter.And(x => x.JobLink.ID).IsEqualTo(Host.Job.ID);
  559. else
  560. filter = filter.And(x => x.JobLink.ID).None();
  561. }
  562. if (EmployeeID != CoreUtils.FullGuid)
  563. {
  564. if (EmployeeID != Guid.Empty)
  565. {
  566. var empfilter = new Filter<Kanban>(x => x.EmployeeLink.ID).IsEqualTo(EmployeeID).Or(x => x.ManagerLink.ID).IsEqualTo(EmployeeID);
  567. filter.Ands.Add(empfilter);
  568. }
  569. else
  570. {
  571. filter = filter.And(x => x.EmployeeLink.ID).IsEqualTo(EmployeeID);
  572. }
  573. }
  574. if (EmployeeID != MyID)
  575. filter = filter.And(x => x.Private).IsEqualTo(false);
  576. //if (!includecompleted)
  577. // filter = filter.And(x => x.Completed).IsEqualTo(DateTime.MinValue);
  578. //if (selectedtype != CoreUtils.FullGuid)
  579. // filter = filter.And(x => x.Type.ID).IsEqualTo(selectedtype);
  580. return filter;
  581. }
  582. public void Refresh(bool resetselection)
  583. {
  584. Application.Current.Dispatcher.Invoke(() => { Mouse.OverrideCursor = Cursors.Wait; });
  585. if (EmployeeID != CoreUtils.FullGuid && EmployeeID != Guid.Empty)
  586. {
  587. _kanbans = new Client<KanbanSubscriber>().Query(
  588. GetKanbanSubscriberFilter(),
  589. new Columns<KanbanSubscriber>
  590. (
  591. x => x.Kanban.ID,
  592. x => x.Kanban.DueDate,
  593. x => x.Kanban.Completed,
  594. //x => x.Kanban.Description,
  595. x => x.Kanban.Summary,
  596. x => x.Kanban.Category,
  597. x => x.Kanban.EmployeeLink.ID,
  598. x => x.Kanban.ManagerLink.ID,
  599. x => x.Kanban.Notes,
  600. x => x.Kanban.Title,
  601. x => x.Kanban.JobLink.ID,
  602. x => x.Kanban.JobLink.JobNumber,
  603. x => x.Kanban.JobLink.Name,
  604. x => x.Kanban.Type.ID,
  605. x => x.Kanban.Type.Code,
  606. x => x.Kanban.Number,
  607. x => x.Kanban.Attachments,
  608. x => x.Kanban.Locked
  609. ),
  610. new SortOrder<KanbanSubscriber>(x => x.Kanban.DueDate) { Direction = SortDirection.Ascending }
  611. );
  612. foreach (var column in _kanbans.Columns)
  613. column.ColumnName = column.ColumnName.Replace("Kanban.", "");
  614. }
  615. else
  616. {
  617. _kanbans = new Client<Kanban>().Query(
  618. GetKanbanFilter(),
  619. new Columns<Kanban>
  620. (
  621. x => x.ID,
  622. x => x.DueDate,
  623. x => x.Completed,
  624. //x => x.Description,
  625. x => x.Summary,
  626. x => x.Category,
  627. x => x.EmployeeLink.ID,
  628. x => x.ManagerLink.ID,
  629. x => x.Notes,
  630. x => x.Title,
  631. x => x.JobLink.ID,
  632. x => x.JobLink.JobNumber,
  633. x => x.JobLink.Name,
  634. x => x.Type.ID,
  635. x => x.Type.Code,
  636. x => x.Number,
  637. x => x.Attachments,
  638. x => x.Locked
  639. ),
  640. new SortOrder<Kanban>(x => x.DueDate) { Direction = SortDirection.Ascending }
  641. );
  642. }
  643. ReloadKanbans();
  644. Application.Current.Dispatcher.Invoke(() => { Mouse.OverrideCursor = null; });
  645. }
  646. private void ReloadKanbans()
  647. {
  648. //SetupColumns();
  649. //ResizeColumns();
  650. _models = new ObservableCollection<TaskModel>();
  651. foreach (var row in _kanbans.Rows)
  652. try
  653. {
  654. var empid = row.Get<IKanban, Guid>(e => e.EmployeeLink.ID);
  655. var mgrid = row.EntityLinkID<IKanban, EmployeeLink>(x => x.ManagerLink) ?? Guid.Empty;
  656. var completed = row.Get<IKanban, DateTime>(e => e.Completed);
  657. var locked = row.Get<IKanban, bool>(e => e.Locked);
  658. var type = row.Get<IKanban, Guid>(e => e.Type.ID);
  659. var category = row.Get<IKanban, string>(x => x.Category);
  660. if (string.IsNullOrWhiteSpace(category))
  661. category = "Open";
  662. var bLockedOK = Host.KanbanSettings.StatusSettings.IncludeLocked || locked == false;
  663. var bObserveOK = EmployeeID == CoreUtils.FullGuid || Host.KanbanSettings.StatusSettings.IncludeObserved || empid == EmployeeID || mgrid == EmployeeID;
  664. var bCompleteOK = Host.KanbanSettings.StatusSettings.IncludeCompleted || completed.IsEmpty();
  665. var bTypeOK = selectedtype == CoreUtils.FullGuid || type == selectedtype;
  666. if (bLockedOK && bCompleteOK && bObserveOK && bTypeOK)
  667. {
  668. var model = new TaskModel();
  669. var EmployeeEntry = _employeelist.Where(x => x.ID.Equals(empid)).FirstOrDefault();
  670. var empimg = EmployeeEntry?.Image;
  671. var ManagerEntry = _employeelist.Where(x => x.ID.Equals(mgrid)).FirstOrDefault();
  672. //var description = row.Get<IKanban, String>(x => x.Summary);
  673. //if (String.IsNullOrWhiteSpace(description))
  674. // description = String.Join("\r\n", row.Get<IKanban, String[]>(x => x.Notes)).Split(new String[] { "===============" }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
  675. //if (String.IsNullOrWhiteSpace(description))
  676. // description = CoreUtils.StripHTML(row.Get<IKanban, String>(x => x.Description));
  677. //if (String.IsNullOrWhiteSpace(description))
  678. // description = "";
  679. var job = row.Get<IKanban, string>(x => x.JobLink.JobNumber);
  680. model.Title = row.Get<IKanban, string>(x => x.Title);
  681. model.ID = row.Get<IKanban, Guid>(x => x.ID).ToString();
  682. model.Description = row.Get<IKanban, string>(x => x.Summary) ?? "";
  683. model.Category = category;
  684. var color = EmployeeID == Guid.Empty || empid == EmployeeID || EmployeeID == CoreUtils.FullGuid
  685. ? TaskModel.KanbanColor(
  686. row.Get<IKanban, DateTime>(x => x.DueDate),
  687. row.Get<IKanban, DateTime>(x => x.Completed))
  688. : mgrid == EmployeeID
  689. ? Color.Silver
  690. : Color.Plum;
  691. if (row.Get<IKanban, bool>(x => x.Locked))
  692. color = color.MixColors(0.5F, Color.White);
  693. model.ColorKey = ImageUtils.ColorToString(color);
  694. model.Image = empimg;
  695. model.ImageURL = null;
  696. model.Attachments = row.Get<IKanban, int>(x => x.Attachments) > 0; // ? _attachimg : null;
  697. model.DueDate = row.Get<IKanban, DateTime>(x => x.DueDate);
  698. model.CompletedDate = row.Get<IKanban, DateTime>(x => x.Completed);
  699. model.Locked = row.Get<IKanban, bool>(x => x.Locked); // ? _lockimg : null;
  700. var notes = new List<List<string>> { new() };
  701. if ((row.Get<IKanban, string[]>(x => x.Notes) != null))
  702. {
  703. foreach (var line in row.Get<IKanban, string[]>(x => x.Notes))
  704. {
  705. if (line == "===================================")
  706. {
  707. notes.Add(new());
  708. }
  709. else
  710. {
  711. notes.Last().Add(line);
  712. }
  713. }
  714. model.Notes = string.Join("\n===================================\n", notes.Reverse<List<string>>().Select(x => string.Join('\n', x)));
  715. }
  716. model.EmployeeID = empid;
  717. model.ManagerID = mgrid;
  718. var sEmp = "";
  719. if (empid != EmployeeID)
  720. {
  721. if (!Entity.IsEntityLinkValid<IKanban, EmployeeLink>(x => x.EmployeeLink, row))
  722. {
  723. sEmp = "Unallocated";
  724. }
  725. else
  726. {
  727. var tuple = _employeelist.FirstOrDefault(x => x.ID.Equals(empid));
  728. sEmp = tuple != null ? tuple.ID == MyID ? MyName : tuple.Name : "";
  729. }
  730. }
  731. var sMgr = "";
  732. if (mgrid != EmployeeID)
  733. if (mgrid != Guid.Empty)
  734. {
  735. var tuple = _employeelist.FirstOrDefault(x => x.ID.Equals(mgrid));
  736. sMgr = tuple != null ? tuple.ID == MyID ? MyName : tuple.Name : "";
  737. }
  738. if (!string.IsNullOrEmpty(sEmp))
  739. {
  740. if (!string.IsNullOrWhiteSpace(sMgr) && !string.Equals(sMgr, sEmp))
  741. model.AssignedTo = string.Format("Assigned to {0} by {1}", sEmp, sMgr);
  742. else
  743. model.AssignedTo = string.Format("Assigned to {0} ", sEmp);
  744. }
  745. else
  746. {
  747. if (!string.IsNullOrWhiteSpace(sMgr))
  748. model.AssignedTo = string.Format("Allocated by {0} ", sMgr);
  749. }
  750. //model.AssignedTo = String.Format("M: {0} / E: {1}", sMgr, sEmp);
  751. model.JobID = row.Get<IKanban, Guid>(x => x.JobLink.ID);
  752. model.JobNumber = row.Get<IKanban, string>(x => x.JobLink.JobNumber);
  753. if (string.IsNullOrWhiteSpace(model.JobNumber))
  754. model.JobNumber = "";
  755. model.JobName = row.Get<IKanban, string>(x => x.JobLink.Name);
  756. model.Checked = CheckedKanbans.Contains(row.Get<IKanban, Guid>(x => x.ID).ToString());
  757. model.Type = new KanbanType
  758. {
  759. ID = row.Get<IKanban, Guid>(x => x.Type.ID),
  760. Code = row.Get<IKanban, string>(x => x.Type.Code)
  761. };
  762. model.Number = row.Get<IKanban, int>(x => x.Number);
  763. _models.Add(model);
  764. }
  765. }
  766. catch (Exception e)
  767. {
  768. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  769. }
  770. StatusTasksHeaderTimeConverter.Tasks = _models;
  771. FilterKanbans();
  772. }
  773. private void FilterKanbans()
  774. {
  775. Progress.ShowModal("Loading", (progress) =>
  776. {
  777. Dispatcher.BeginInvoke(() => { Progress.Show("Loading"); });
  778. if (JobFilterID == Guid.Empty)
  779. {
  780. var list = _models
  781. .Where(x => x.Search(Search.Text.Split()))
  782. .OrderBy(x => x.EmployeeID == EmployeeID ? 0 : 1).ThenBy(x => x.DueDate);
  783. Dispatcher.BeginInvoke(() => { Kanban.ItemsSource = list; });
  784. }
  785. else
  786. {
  787. var list = _models
  788. .Where(x => x.Search(Search.Text.Split()) && x.JobSearch(JobFilterID))
  789. .OrderBy(x => x.EmployeeID == EmployeeID ? 0 : 1).ThenBy(x => x.DueDate);
  790. Dispatcher.BeginInvoke(() => { Kanban.ItemsSource = list; });
  791. }
  792. Task.Run(() =>
  793. {
  794. Thread.Sleep(500);
  795. Dispatcher.BeginInvoke(() =>
  796. {
  797. Progress.Close();
  798. });
  799. });
  800. });
  801. }
  802. #endregion
  803. #region Kanban Selection Stuff
  804. private void SelectColumn(KanbanColumn column, bool selected)
  805. {
  806. CheckedKanbans.Clear();
  807. if (selected)
  808. {
  809. CheckedKanbans.AddRange(_models.Where(x => Equals(x.Category, column.Categories)).Select(x => x.ID));
  810. foreach (var model in _models)
  811. model.Checked = Equals(model.Category, column.Categories);
  812. }
  813. FilterKanbans();
  814. }
  815. private void UnSelectAll_Click(object sender, RoutedEventArgs e)
  816. {
  817. var column = ((MenuItem)sender).Tag as KanbanColumn;
  818. SelectColumn(column, false);
  819. }
  820. private void SelectAll_Click(object sender, RoutedEventArgs e)
  821. {
  822. var column = ((MenuItem)sender).Tag as KanbanColumn;
  823. SelectColumn(column, true);
  824. }
  825. //private IEnumerable<Guid> GetSelectedKanbanIDs(String currentid)
  826. //{
  827. // List<Guid> result = new List<Guid>();
  828. // if (!CheckedKanbans.Contains(currentid))
  829. // result.Add(Guid.Parse(currentid));
  830. // CheckedKanbans.ForEach((id) => result.Add(Guid.Parse(id)));
  831. // return result;
  832. //}
  833. //private Kanban[] GetSelectedKanbans(String currentid)
  834. //{
  835. // var ids = GetSelectedKanbanIDs(currentid);
  836. // return Host.LoadKanbans(ids).ToArray();
  837. //}
  838. private void CheckBox_Checked(object sender, RoutedEventArgs e)
  839. {
  840. var task = ((CheckBox)sender).Tag as TaskModel;
  841. if (task == null)
  842. return;
  843. if (CheckedKanbans.Contains(task.ID))
  844. CheckedKanbans.Remove(task.ID);
  845. else
  846. CheckedKanbans.Add(task.ID);
  847. }
  848. #endregion
  849. #region Context Menu Actions
  850. private void CreateTask(object sender, RoutedEventArgs e)
  851. {
  852. CreateKanban();
  853. }
  854. private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  855. {
  856. if (e.ClickCount > 1)
  857. {
  858. var task = ((Border)sender).Tag as TaskModel;
  859. DoEdit(task);
  860. e.Handled = true;
  861. }
  862. }
  863. private void EditTask_Click(object sender, RoutedEventArgs e)
  864. {
  865. var task = ((MenuItem)e.Source).Tag as TaskModel;
  866. DoEdit(task);
  867. e.Handled = true;
  868. }
  869. //private void CreateSetout_Click(object sender, RoutedEventArgs e)
  870. //{
  871. // MenuItem menu = sender as MenuItem;
  872. // TaskModel task = menu.Tag as TaskModel;
  873. // if (task.JobID.Equals(Guid.Empty))
  874. // {
  875. // MessageBox.Show("Please link this task to a job before creating a setout!");
  876. // return;
  877. // }
  878. // if (MessageBox.Show("This will convert this task into a Setout.\n\nDo you wish to continue?", "Confirmation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  879. // return;
  880. // ManufacturingTemplate template = new Client<ManufacturingTemplate>().Load(new Filter<ManufacturingTemplate>(x => x.Code).IsEqualTo("PRS")).FirstOrDefault();
  881. // if (template == null)
  882. // {
  883. // MessageBox.Show("[Pressing] Template does not exist!");
  884. // return;
  885. // }
  886. // String setoutnumber = "";
  887. // Progress.ShowModal("Creating Setout", (progress) =>
  888. // {
  889. // MultiQuery query = new MultiQuery();
  890. // query.Add<ManufacturingTemplateStage>(
  891. // new Filter<ManufacturingTemplateStage>(x => x.Template.ID).IsEqualTo(template.ID),
  892. // null,
  893. // new SortOrder<ManufacturingTemplateStage>(x => x.Sequence)
  894. // );
  895. // query.Add<Kanban>(
  896. // new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID),
  897. // null,
  898. // null
  899. // );
  900. // query.Query();
  901. // ManufacturingTemplateStage[] tstages = query.Get<ManufacturingTemplateStage>().Rows.Select(x => x.ToObject<ManufacturingTemplateStage>()).ToArray();
  902. // Kanban kanban = query.Get<Kanban>().Rows.FirstOrDefault()?.ToObject<Kanban>();
  903. // progress.Report("Creating Setouts");
  904. // CoreTable setouts = new Client<Setout>().Query(
  905. // new Filter<Setout>(x => x.JobLink.ID).IsEqualTo(kanban.JobLink.ID),
  906. // new Columns<Setout>(x => x.JobLink.JobNumber, x => x.Number),
  907. // null
  908. // );
  909. // int ireq = 0;
  910. // String sreq = "";
  911. // while (true)
  912. // {
  913. // ireq++;
  914. // sreq = String.Format("{0}-{1:yyMMdd}-{2}", kanban.JobLink.JobNumber, DateTime.Now, ireq);
  915. // if (!setouts.Rows.Any(r => sreq.Equals(r.Get<Setout, String>(c => c.Number))))
  916. // break;
  917. // }
  918. // Setout setout = new Setout();
  919. // setout.Number = sreq;
  920. // setout.JobLink.ID = kanban.JobLink.ID; // = new Client<Job>().Load(new Filter<Job>(x => x.ID).IsEqualTo(kanban.JobLink.ID)).FirstOrDefault();
  921. // setout.Reference = kanban.Title;
  922. // var notes = kanban.Notes.ToList();
  923. // var description = kanban.Summary;
  924. // if (String.IsNullOrWhiteSpace(description))
  925. // description = CoreUtils.StripHTML(kanban.Description);
  926. // if (!String.IsNullOrWhiteSpace(description))
  927. // notes.Insert(0, description);
  928. // setout.Description = String.Join("\n==========================================\n", notes);
  929. // new Client<Setout>().Save(setout, "Created from Task");
  930. // setoutnumber = setout.Number;
  931. // progress.Report("Creating Manufacturing Packet");
  932. // ManufacturingPacket packet = new ManufacturingPacket()
  933. // {
  934. // Group = template.Factory.Name,
  935. // Serial = template.Code,
  936. // Title = kanban.Title,
  937. // Quantity = 1,
  938. // BarcodeQty = 1,
  939. // DueDate = kanban.DueDate
  940. // };
  941. // packet.SetoutLink.ID = setout.ID;
  942. // //packet.JobLink.ID = setout.JobLink.ID;
  943. // packet.ManufacturingTemplateLink.ID = template.ID;
  944. // packet.ManufacturingTemplateLink.Code = template.Code;
  945. // new Client<ManufacturingPacket>().Save(packet, "Created from Task");
  946. // List<ManufacturingPacketStage> pstages = new List<ManufacturingPacketStage>();
  947. // foreach (var tstage in tstages)
  948. // {
  949. // var pstage = new ManufacturingPacketStage()
  950. // {
  951. // Time = tstage.Time,
  952. // Sequence = tstage.Sequence,
  953. // SequenceType = tstage.SequenceType,
  954. // Started = DateTime.MinValue,
  955. // PercentageComplete = 0.0F,
  956. // Completed = DateTime.MinValue,
  957. // QualityChecks = tstage.QualityChecks,
  958. // QualityStatus = QualityStatus.NotChecked,
  959. // QualityNotes = "",
  960. // };
  961. // pstage.Parent.ID = packet.ID;
  962. // pstage.ManufacturingSectionLink.ID = tstage.Section.ID;
  963. // pstage.ManufacturingSectionLink.Name = tstage.Section.Name;
  964. // pstages.Add(pstage);
  965. // }
  966. // new Client<ManufacturingPacketStage>().Save(pstages, "Created from Task", (_, __) => { });
  967. // progress.Report("Processing Documents");
  968. // List<SetoutDocument> _setoutdocuments = new List<SetoutDocument>();
  969. // List<KanbanDocument> _kanbandocuments = new List<KanbanDocument>();
  970. // KanbanDocument[] docrefs = new Client<KanbanDocument>().Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(kanban.ID));
  971. // foreach (var docref in docrefs)
  972. // {
  973. // Guid docid = ProcessSetoutDocument(docref);
  974. // var newdoc = new SetoutDocument();
  975. // newdoc.EntityLink.ID = setout.ID;
  976. // newdoc.DocumentLink.ID = docid;
  977. // _setoutdocuments.Add(newdoc);
  978. // if (docid != docref.DocumentLink.ID)
  979. // {
  980. // docref.DocumentLink.ID = docid;
  981. // _kanbandocuments.Add(docref);
  982. // }
  983. // }
  984. // new Client<SetoutDocument>().Save(_setoutdocuments, "Converted from Task", (_, __) => { });
  985. // new Client<KanbanDocument>().Save(_kanbandocuments, "Converted to PDF", (_, __) => { });
  986. // SetoutKanban link = new SetoutKanban();
  987. // link.Entity.ID = setout.ID;
  988. // link.Kanban.ID = kanban.ID;
  989. // new Client<SetoutKanban>().Save(link, "Converting Task -> Setout", (_, __) => { });
  990. // progress.Report("Updating Task");
  991. // kanban.Title = kanban.Title + " (" + setoutnumber + ")";
  992. // kanban.Locked = true;
  993. // new Client<Kanban>().Save(kanban, "Converting Kanban to Setout");
  994. // });
  995. // MessageBox.Show(String.Format("Created Setout {0}", setoutnumber));
  996. // Refresh();
  997. //}
  998. //private Guid ProcessSetoutDocument(KanbanDocument docref)
  999. //{
  1000. // Guid result = docref.DocumentLink.ID;
  1001. // String ext = System.IO.Path.GetExtension(docref.DocumentLink.FileName).ToLower();
  1002. // if (ext.EndsWith("txt"))
  1003. // {
  1004. // var doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(docref.DocumentLink.ID)).FirstOrDefault();
  1005. // PdfDocument pdf = new PdfDocument();
  1006. // PdfPage page = pdf.Pages.Add();
  1007. // PdfGraphics graphics = page.Graphics;
  1008. // PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 12);
  1009. // String text = System.Text.Encoding.UTF8.GetString(doc.Data);
  1010. // graphics.DrawString(text, font, PdfBrushes.Black, new PointF(0, 0));
  1011. // MemoryStream ms = new MemoryStream();
  1012. // pdf.Save(ms);
  1013. // pdf.Close(true);
  1014. // byte[] data = ms.ToArray();
  1015. // var newdoc = new Document()
  1016. // {
  1017. // Data = data,
  1018. // FileName = System.IO.Path.ChangeExtension(docref.DocumentLink.FileName, "pdf"),
  1019. // CRC = CoreUtils.CalculateCRC(data),
  1020. // TimeStamp = DateTime.Now,
  1021. // };
  1022. // new Client<Document>().Save(newdoc, "Converted from Text");
  1023. // return newdoc.ID;
  1024. // }
  1025. // else if (ext.EndsWith("png") || ext.EndsWith("bmp") || ext.EndsWith("jpg") || ext.EndsWith("jpeg"))
  1026. // {
  1027. // var doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(docref.DocumentLink.ID)).FirstOrDefault();
  1028. // PdfBitmap image = new PdfBitmap(new MemoryStream(doc.Data));
  1029. // PdfDocument pdf = new PdfDocument();
  1030. // pdf.PageSettings.Orientation = image.Height > image.Width ? PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
  1031. // pdf.PageSettings.Size = new SizeF(image.Width, image.Height);
  1032. // PdfPage page = pdf.Pages.Add();
  1033. // PdfGraphics graphics = page.Graphics;
  1034. // graphics.DrawImage(image, 0.0F, 0.0F);
  1035. // MemoryStream ms = new MemoryStream();
  1036. // pdf.Save(ms);
  1037. // pdf.Close(true);
  1038. // byte[] data = ms.ToArray();
  1039. // var newdoc = new Document()
  1040. // {
  1041. // Data = data,
  1042. // FileName = System.IO.Path.ChangeExtension(docref.DocumentLink.FileName, "pdf"),
  1043. // CRC = CoreUtils.CalculateCRC(data),
  1044. // TimeStamp = DateTime.Now,
  1045. // };
  1046. // new Client<Document>().Save(newdoc, "Converted from Image");
  1047. // return newdoc.ID;
  1048. // }
  1049. // return result;
  1050. //}
  1051. //private void CreateRequisition_Click(object sender, RoutedEventArgs e)
  1052. //{
  1053. // MenuItem menu = sender as MenuItem;
  1054. // TaskModel task = menu.Tag as TaskModel;
  1055. // if (task.JobID.Equals(Guid.Empty))
  1056. // {
  1057. // MessageBox.Show("Please link this task to a job before creating a requisition!");
  1058. // return;
  1059. // }
  1060. // if (MessageBox.Show("This will convert this task into a Requisition.\n\nDo you wish to continue?", "Confirmation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1061. // return;
  1062. // int requinumber = 0;
  1063. // Progress.ShowModal("Creating Requisition", (progress) =>
  1064. // {
  1065. // Kanban kanban = new Client<Kanban>().Load(new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID)).FirstOrDefault();
  1066. // Requisition requi = new Requisition();
  1067. // requi.JobLink.ID = kanban.JobLink.ID;
  1068. // requi.RequestedBy.ID = kanban.ManagerLink.ID;
  1069. // requi.Employee.ID = Guid.Empty;
  1070. // requi.Title = kanban.Title;
  1071. // requi.Request = String.IsNullOrWhiteSpace(kanban.Summary)
  1072. // ? String.IsNullOrWhiteSpace(kanban.Summary)
  1073. // ? String.Join("\n", kanban.Notes)
  1074. // : CoreUtils.StripHTML(kanban.Description)
  1075. // : kanban.Summary;
  1076. // requi.Notes = kanban.Notes;
  1077. // requi.Due = kanban.DueDate;
  1078. // new Client<Requisition>().Save(requi, "Created from Task");
  1079. // requinumber = requi.Number;
  1080. // progress.Report("Updating Documents");
  1081. // List<RequisitionDocument> _documents = new List<RequisitionDocument>();
  1082. // KanbanDocument[] documents = new Client<KanbanDocument>().Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(kanban.ID));
  1083. // foreach (var document in documents)
  1084. // {
  1085. // var newdoc = new RequisitionDocument();
  1086. // newdoc.EntityLink.ID = requi.ID;
  1087. // newdoc.DocumentLink.ID = document.DocumentLink.ID;
  1088. // _documents.Add(newdoc);
  1089. // }
  1090. // new Client<RequisitionDocument>().Save(_documents, "Converted from Task", (_, __) => { });
  1091. // RequisitionKanban link = new RequisitionKanban();
  1092. // link.Entity.ID = requi.ID;
  1093. // link.Kanban.ID = kanban.ID;
  1094. // new Client<RequisitionKanban>().Save(link, "Converting Task -> Requisition", (_, __) => { });
  1095. // progress.Report("Updating Task");
  1096. // kanban.Category = "Open";
  1097. // kanban.Completed = DateTime.MinValue;
  1098. // kanban.Locked = true;
  1099. // kanban.Title = kanban.Title + " (Requi #" + requi.Number.ToString() + ")";
  1100. // new Client<Kanban>().Save(kanban, "Converted to Requisition", (_, __) => { });
  1101. // });
  1102. // MessageBox.Show(String.Format("Created Requisition {0}", requinumber));
  1103. // Refresh();
  1104. //}
  1105. //private void CreatePurchaseOrder_Click(object sender, RoutedEventArgs e)
  1106. //{
  1107. // MenuItem menu = sender as MenuItem;
  1108. // TaskModel task = menu.Tag as TaskModel;
  1109. // if (MessageBox.Show("This will convert this task into a Purchase Order.\n\nDo you wish to continue?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  1110. // {
  1111. // String ponumber = "";
  1112. // Progress.ShowModal("Creating Purchase Order", (progress) =>
  1113. // {
  1114. // Kanban kanban = new Client<Kanban>().Load(new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID)).FirstOrDefault();
  1115. // PurchaseOrder order = new PurchaseOrder();
  1116. // order.Notes = kanban.Summary;
  1117. // order.RaisedBy.ID = kanban.EmployeeLink.ID;
  1118. // new Client<PurchaseOrder>().Save(order, "Created from Task Screen");
  1119. // ponumber = order.PONumber;
  1120. // progress.Report("Updating Documents");
  1121. // List<PurchaseOrderDocument> docs = new List<PurchaseOrderDocument>();
  1122. // var taskdocs = new Client<KanbanDocument>().Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(Guid.Parse(task.ID)));
  1123. // foreach (var taskdoc in taskdocs)
  1124. // {
  1125. // PurchaseOrderDocument doc = new PurchaseOrderDocument();
  1126. // doc.DocumentLink.ID = taskdoc.DocumentLink.ID;
  1127. // doc.EntityLink.ID = order.ID;
  1128. // docs.Add(doc);
  1129. // }
  1130. // new Client<PurchaseOrderDocument>().Save(docs, "", (_, __) => { });
  1131. // progress.Report("Creating Links");
  1132. // PurchaseOrderKanban link = new PurchaseOrderKanban();
  1133. // link.Entity.ID = order.ID;
  1134. // link.Kanban.ID = kanban.ID;
  1135. // new Client<PurchaseOrderKanban>().Save(link, "Converting Task -> Purchase Order", (_, __) => { });
  1136. // progress.Report("Updating Task");
  1137. // kanban.Category = "Open";
  1138. // kanban.Completed = DateTime.MinValue;
  1139. // kanban.Locked = true;
  1140. // kanban.Title = "(PO#" + order.PONumber.ToString() + ") " + kanban.Title;
  1141. // new Client<Kanban>().Save(kanban, "Converted to Purchase Order", (_, __) => { });
  1142. // });
  1143. // MessageBox.Show(String.Format("Created Purchase Order {0}", ponumber));
  1144. // Refresh();
  1145. // }
  1146. //}
  1147. //private void CreateDelivery_Click(object sender, RoutedEventArgs e)
  1148. //{
  1149. // MenuItem menu = sender as MenuItem;
  1150. // TaskModel task = menu.Tag as TaskModel;
  1151. // if (MessageBox.Show("This will convert this task into a Delivery.\n\nDo you wish to continue?", "Confirmation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1152. // return;
  1153. // int deliverynumber = 0;
  1154. // Progress.ShowModal("Creating Delivery", (progress) =>
  1155. // {
  1156. // Kanban kanban = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID)).Rows.FirstOrDefault()?.ToObject<Kanban>();
  1157. // Delivery delivery = new Delivery();
  1158. // delivery.Due = kanban.DueDate;
  1159. // delivery.Job.ID = kanban.JobLink.ID;
  1160. // delivery.Job.Synchronise(kanban.JobLink);
  1161. // delivery.Notes = kanban.Summary;
  1162. // delivery.Employee.ID = kanban.ManagerLink.ID;
  1163. // delivery.Employee.Synchronise(kanban.ManagerLink);
  1164. // new Client<Delivery>().Save(delivery, "Created From Task");
  1165. // deliverynumber = delivery.Number;
  1166. // progress.Report("Updating Documents");
  1167. // List<DeliveryDocument> docs = new List<DeliveryDocument>();
  1168. // var taskdocs = new Client<KanbanDocument>().Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(Guid.Parse(task.ID)));
  1169. // foreach (var taskdoc in taskdocs)
  1170. // {
  1171. // DeliveryDocument doc = new DeliveryDocument();
  1172. // doc.DocumentLink.ID = taskdoc.DocumentLink.ID;
  1173. // doc.EntityLink.ID = delivery.ID;
  1174. // docs.Add(doc);
  1175. // }
  1176. // new Client<DeliveryDocument>().Save(docs, "", (_, __) => { });
  1177. // progress.Report("Creating Links");
  1178. // DeliveryKanban link = new DeliveryKanban();
  1179. // link.Entity.ID = delivery.ID;
  1180. // link.Kanban.ID = kanban.ID;
  1181. // new Client<DeliveryKanban>().Save(link, "Converting Task -> Delivery", (_, __) => { });
  1182. // progress.Report("Updating Task");
  1183. // kanban.Category = "Open";
  1184. // kanban.Completed = DateTime.MinValue;
  1185. // kanban.Locked = true;
  1186. // kanban.Title = "(Del#" + delivery.Number.ToString() + ") " + kanban.Title;
  1187. // new Client<Kanban>().Save(kanban, "Converted to Delivery", (_, __) => { });
  1188. // });
  1189. // MessageBox.Show(String.Format("Created Delivery {0}", deliverynumber));
  1190. // Refresh();
  1191. //}
  1192. //private void CloseTask_Click(object sender, RoutedEventArgs e)
  1193. //{
  1194. // if (MessageBox.Show("Are you sure you want to remove the selected tasks from the list?", "Confirm removal", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1195. // return;
  1196. // Progress.ShowModal("Closing Kanbans", (progress) =>
  1197. // {
  1198. // TaskModel task = ((MenuItem)e.Source).Tag as TaskModel;
  1199. // Kanban[] kanbans = GetSelectedKanbans(task.ID);
  1200. // for (int i = 0; i < kanbans.Length; i++)
  1201. // {
  1202. // Kanban kanban = kanbans[i];
  1203. // kanban.Closed = DateTime.Now;
  1204. // }
  1205. // new Client<Kanban>().Save(kanbans, "Kanban Marked as Closed");
  1206. // CheckedKanbans.Clear();
  1207. // });
  1208. // Refresh();
  1209. //}
  1210. //private void EmailTask_Click(object sender, RoutedEventArgs e)
  1211. //{
  1212. // MenuItem menu = sender as MenuItem;
  1213. // TaskModel task = menu.Tag as TaskModel;
  1214. // Kanban kanban = new Client<Kanban>().Load(new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID)).FirstOrDefault();
  1215. // List<String> to = new List<string>();
  1216. // String from = "";
  1217. // String salutation = "";
  1218. // if (MyID.HasValue)
  1219. // {
  1220. // CoreRow me = _employees.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.ID).Equals(MyID));
  1221. // if (me != null)
  1222. // from = me.Get<Employee, String>(c => c.Name).Split(' ').FirstOrDefault();
  1223. // if (kanban.EmployeeLink.ID != MyID.Value)
  1224. // {
  1225. // CoreRow emp = _employees.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.ID).Equals(kanban.EmployeeLink.ID));
  1226. // if (emp != null)
  1227. // {
  1228. // String email = emp.Get<Employee, String>(c => c.Email);
  1229. // if (!string.IsNullOrEmpty(email))
  1230. // {
  1231. // to.Add(email);
  1232. // String name = emp.Get<Employee, String>(c => c.Name).Split(' ').FirstOrDefault();
  1233. // salutation = salutation + (String.IsNullOrEmpty(salutation) ? "Hi " : " and ") + name;
  1234. // }
  1235. // }
  1236. // }
  1237. // if (kanban.ManagerLink.ID != MyID.Value)
  1238. // {
  1239. // CoreRow emp = _employees.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.ID).Equals(kanban.ManagerLink.ID));
  1240. // if (emp != null)
  1241. // {
  1242. // String email = emp.Get<Employee, String>(c => c.Email);
  1243. // if (!string.IsNullOrEmpty(email))
  1244. // {
  1245. // to.Add(email);
  1246. // String name = emp.Get<Employee, String>(c => c.Name).Split(' ').FirstOrDefault();
  1247. // salutation = salutation + (String.IsNullOrEmpty(salutation) ? "Hi " : " and ") + name;
  1248. // }
  1249. // }
  1250. // }
  1251. // }
  1252. // Outlook.Application outlookApp = new Outlook.Application();
  1253. // Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
  1254. // mailItem.Subject = "PRS Task: " + kanban.Title;
  1255. // mailItem.To = String.Join("; ", to);
  1256. // mailItem.HTMLBody = String.Format("{0},<br><br>Please see the above task in PRS.<br><br>Regards,<br><br>{1}<br><br><b><u>Task Description:</u></b><br><i>{2}</i><br><br><b><u>Additional Notes:</u></b><br>{3}", salutation, from, CoreUtils.StripHTML(kanban.Description), String.Join("\r\n", kanban.Notes));
  1257. // //mailItem.Attachments.Add(filename, Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
  1258. // mailItem.Display(false);
  1259. //}
  1260. //private void CompleteTask_Click(object sender, RoutedEventArgs e)
  1261. //{
  1262. // if (MessageBox.Show("Are you sure you want to mark the selected tasks as complete?", "Confirm Completion", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1263. // return;
  1264. // TaskModel task = ((MenuItem)e.Source).Tag as TaskModel;
  1265. // Progress.ShowModal("Completing Task", (progress) =>
  1266. // {
  1267. // Kanban[] kanbans = GetSelectedKanbans(task.ID);
  1268. // for (int i = 0; i < kanbans.Length; i++)
  1269. // {
  1270. // Kanban kanban = kanbans[i];
  1271. // kanban.Completed = DateTime.Now;
  1272. // kanban.Category = "Complete";
  1273. // }
  1274. // new Client<Kanban>().Save(kanbans, "Kanban Marked as Completed");
  1275. // CheckedKanbans.Clear();
  1276. // });
  1277. // Refresh();
  1278. //}
  1279. #endregion
  1280. #region Kanban Creation / Editing
  1281. public void CreateKanban()
  1282. {
  1283. var result = Host.CreateKanban(
  1284. kanban =>
  1285. {
  1286. kanban.EmployeeLink.ID = EmployeeID != CoreUtils.FullGuid ? EmployeeID : MyID.Value;
  1287. kanban.ManagerLink.ID = MyID.Value;
  1288. kanban.ManagerLink.UserLink.ID = ClientFactory.UserGuid;
  1289. });
  1290. if (result != null)
  1291. Refresh(true);
  1292. }
  1293. private void DoEdit(TaskModel task)
  1294. {
  1295. if (task == null)
  1296. return;
  1297. var result = Host.EditReferences(new[] { task });
  1298. if (result)
  1299. Refresh(true);
  1300. }
  1301. #endregion
  1302. private void TaskTypesLabel_OnClick(object sender, RoutedEventArgs e)
  1303. {
  1304. var list = new MasterList(typeof(KanbanType));
  1305. list.ShowDialog();
  1306. SetupKanbanTypesLookup(true);
  1307. }
  1308. private void JobFilterBtn_OnClick(object sender, RoutedEventArgs e)
  1309. {
  1310. if (JobFilterID != Guid.Empty)
  1311. {
  1312. JobFilterBtn.Content = "Filter Job";
  1313. JobFilterID = Guid.Empty;
  1314. FilterKanbans();
  1315. return;
  1316. }
  1317. var window = new ThemableWindow();
  1318. var grid = new JobGrid();
  1319. grid.Reconfigure(options =>
  1320. {
  1321. options.Remove(DynamicGridOption.EditRows);
  1322. options.Remove(DynamicGridOption.DeleteRows);
  1323. options.Remove(DynamicGridOption.AddRows);
  1324. options.Remove(DynamicGridOption.MultiSelect);
  1325. options.Remove(DynamicGridOption.ExportData);
  1326. options.Remove(DynamicGridOption.ImportData);
  1327. });
  1328. grid.OnSelectItem += (object sender, DynamicGridSelectionEventArgs e) =>
  1329. {
  1330. if ((sender as JobGrid).SelectedRows.Count() == 0)
  1331. return;
  1332. else
  1333. {
  1334. var item = (sender as JobGrid).SelectedRows[0];
  1335. AddJobFilter(item);
  1336. window.Close();
  1337. }
  1338. };
  1339. grid.Refresh(true, true);
  1340. window.Content = grid;
  1341. window.ShowDialog();
  1342. }
  1343. Guid JobFilterID = Guid.Empty;
  1344. private void AddJobFilter(CoreRow item)
  1345. {
  1346. JobFilterID = item.Get<Job, Guid>(x => x.ID);
  1347. JobFilterBtn.Content = item.Get<Job, string>(x => x.JobNumber) + " (click to cancel)";
  1348. FilterKanbans();
  1349. }
  1350. }
  1351. }