TasksByStatusControl.xaml.cs 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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.Settings.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.Settings.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.Settings.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.Settings.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.Settings.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 KanbanView KanbanView => KanbanView.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.Settings.StatusSettings.SelectedType))
  417. TaskTypes.SelectedValue = Host.Settings.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.Settings.StatusSettings.IncludeCompleted : true;
  433. IncludeObserved.IsChecked = Host.Settings.StatusSettings.IncludeObserved;
  434. ViewType.SelectedIndex = Host.Settings.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.Settings.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.ParentID != Guid.Empty)
  520. filter = filter.And(x => x.Kanban.JobLink.ID).IsEqualTo(Host.ParentID);
  521. // All Tasks (EmployeeID.HasValue == false) or Unallocated (EmployeeID = Guid.Empty) are retrieved directly from the Kanban Table
  522. // so if we are here, we can assume that we are pulling subscriber data
  523. var empfilter = new Filter<KanbanSubscriber>(x => x.Employee.ID).IsEqualTo(EmployeeID);
  524. filter.Ands.Add(empfilter);
  525. if (EmployeeID != MyID)
  526. filter = filter.And(x => x.Kanban.Private).IsEqualTo(false);
  527. //if (!includeobserved)
  528. // filter = filter.And(new Filter<KanbanSubscriber>(x => x.Assignee).IsEqualTo(true).Or(x => x.Manager).IsEqualTo(true));
  529. //if (!includecompleted)
  530. // filter = filter.And(x => x.Kanban.Completed).IsEqualTo(DateTime.MinValue);
  531. //if (selectedtype != CoreUtils.FullGuid)
  532. // filter = filter.And(x => x.Kanban.Type.ID).IsEqualTo(selectedtype);
  533. if (!string.IsNullOrWhiteSpace(searchtext))
  534. {
  535. var search = GetSearchFilter<KanbanSubscriber>(x => x.Kanban.JobLink.Name);
  536. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.JobLink.JobNumber));
  537. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.Summary));
  538. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.Title));
  539. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.ManagerLink.Name));
  540. search.Ors.Add(GetSearchFilter<KanbanSubscriber>(x => x.Kanban.EmployeeLink.Name));
  541. if (int.TryParse(searchtext.Trim(), out var tasknumber))
  542. search.Ors.Add(new Filter<KanbanSubscriber>(x => x.Kanban.Number).IsEqualTo(tasknumber));
  543. filter.Ands.Add(search);
  544. }
  545. return filter;
  546. }
  547. private Filter<Kanban> GetKanbanFilter()
  548. {
  549. var filter = new Filter<Kanban>(x => x.Closed).IsEqualTo(DateTime.MinValue);
  550. if (Host.ParentID != Guid.Empty)
  551. filter = filter.And(x => x.JobLink.ID).IsEqualTo(Host.ParentID);
  552. if (EmployeeID != CoreUtils.FullGuid)
  553. {
  554. if (EmployeeID != Guid.Empty)
  555. {
  556. var empfilter = new Filter<Kanban>(x => x.EmployeeLink.ID).IsEqualTo(EmployeeID).Or(x => x.ManagerLink.ID).IsEqualTo(EmployeeID);
  557. filter.Ands.Add(empfilter);
  558. }
  559. else
  560. {
  561. filter = filter.And(x => x.EmployeeLink.ID).IsEqualTo(EmployeeID);
  562. }
  563. }
  564. if (EmployeeID != MyID)
  565. filter = filter.And(x => x.Private).IsEqualTo(false);
  566. //if (!includecompleted)
  567. // filter = filter.And(x => x.Completed).IsEqualTo(DateTime.MinValue);
  568. //if (selectedtype != CoreUtils.FullGuid)
  569. // filter = filter.And(x => x.Type.ID).IsEqualTo(selectedtype);
  570. return filter;
  571. }
  572. public void Refresh(bool resetselection)
  573. {
  574. Application.Current.Dispatcher.Invoke(() => { Mouse.OverrideCursor = Cursors.Wait; });
  575. if (EmployeeID != CoreUtils.FullGuid && EmployeeID != Guid.Empty)
  576. {
  577. _kanbans = new Client<KanbanSubscriber>().Query(
  578. GetKanbanSubscriberFilter(),
  579. new Columns<KanbanSubscriber>
  580. (
  581. x => x.Kanban.ID,
  582. x => x.Kanban.DueDate,
  583. x => x.Kanban.Completed,
  584. //x => x.Kanban.Description,
  585. x => x.Kanban.Summary,
  586. x => x.Kanban.Category,
  587. x => x.Kanban.EmployeeLink.ID,
  588. x => x.Kanban.ManagerLink.ID,
  589. x => x.Kanban.Notes,
  590. x => x.Kanban.Title,
  591. x => x.Kanban.JobLink.ID,
  592. x => x.Kanban.JobLink.JobNumber,
  593. x => x.Kanban.JobLink.Name,
  594. x => x.Kanban.Type.ID,
  595. x => x.Kanban.Type.Code,
  596. x => x.Kanban.Number,
  597. x => x.Kanban.Attachments,
  598. x => x.Kanban.Locked
  599. ),
  600. new SortOrder<KanbanSubscriber>(x => x.Kanban.DueDate) { Direction = SortDirection.Ascending }
  601. );
  602. foreach (var column in _kanbans.Columns)
  603. column.ColumnName = column.ColumnName.Replace("Kanban.", "");
  604. }
  605. else
  606. {
  607. _kanbans = new Client<Kanban>().Query(
  608. GetKanbanFilter(),
  609. new Columns<Kanban>
  610. (
  611. x => x.ID,
  612. x => x.DueDate,
  613. x => x.Completed,
  614. //x => x.Description,
  615. x => x.Summary,
  616. x => x.Category,
  617. x => x.EmployeeLink.ID,
  618. x => x.ManagerLink.ID,
  619. x => x.Notes,
  620. x => x.Title,
  621. x => x.JobLink.ID,
  622. x => x.JobLink.JobNumber,
  623. x => x.JobLink.Name,
  624. x => x.Type.ID,
  625. x => x.Type.Code,
  626. x => x.Number,
  627. x => x.Attachments,
  628. x => x.Locked
  629. ),
  630. new SortOrder<Kanban>(x => x.DueDate) { Direction = SortDirection.Ascending }
  631. );
  632. }
  633. ReloadKanbans();
  634. Application.Current.Dispatcher.Invoke(() => { Mouse.OverrideCursor = null; });
  635. }
  636. private void ReloadKanbans()
  637. {
  638. //SetupColumns();
  639. //ResizeColumns();
  640. _models = new ObservableCollection<TaskModel>();
  641. foreach (var row in _kanbans.Rows)
  642. try
  643. {
  644. var empid = row.Get<IKanban, Guid>(e => e.EmployeeLink.ID);
  645. var mgrid = row.EntityLinkID<IKanban, EmployeeLink>(x => x.ManagerLink) ?? Guid.Empty;
  646. var completed = row.Get<IKanban, DateTime>(e => e.Completed);
  647. var locked = row.Get<IKanban, bool>(e => e.Locked);
  648. var type = row.Get<IKanban, Guid>(e => e.Type.ID);
  649. var category = row.Get<IKanban, string>(x => x.Category);
  650. if (string.IsNullOrWhiteSpace(category))
  651. category = "Open";
  652. var bLockedOK = Host.Settings.StatusSettings.IncludeLocked || locked == false;
  653. var bObserveOK = EmployeeID == CoreUtils.FullGuid || Host.Settings.StatusSettings.IncludeObserved || empid == EmployeeID || mgrid == EmployeeID;
  654. var bCompleteOK = Host.Settings.StatusSettings.IncludeCompleted || completed.IsEmpty();
  655. var bTypeOK = selectedtype == CoreUtils.FullGuid || type == selectedtype;
  656. if (bLockedOK && bCompleteOK && bObserveOK && bTypeOK)
  657. {
  658. var model = new TaskModel();
  659. var EmployeeEntry = _employeelist.Where(x => x.ID.Equals(empid)).FirstOrDefault();
  660. var empimg = EmployeeEntry?.Image;
  661. var ManagerEntry = _employeelist.Where(x => x.ID.Equals(mgrid)).FirstOrDefault();
  662. //var description = row.Get<IKanban, String>(x => x.Summary);
  663. //if (String.IsNullOrWhiteSpace(description))
  664. // description = String.Join("\r\n", row.Get<IKanban, String[]>(x => x.Notes)).Split(new String[] { "===============" }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
  665. //if (String.IsNullOrWhiteSpace(description))
  666. // description = CoreUtils.StripHTML(row.Get<IKanban, String>(x => x.Description));
  667. //if (String.IsNullOrWhiteSpace(description))
  668. // description = "";
  669. var job = row.Get<IKanban, string>(x => x.JobLink.JobNumber);
  670. model.Title = row.Get<IKanban, string>(x => x.Title);
  671. model.ID = row.Get<IKanban, Guid>(x => x.ID).ToString();
  672. model.Description = row.Get<IKanban, string>(x => x.Summary) ?? "";
  673. model.Category = category;
  674. var color = EmployeeID == Guid.Empty || empid == EmployeeID || EmployeeID == CoreUtils.FullGuid
  675. ? TaskModel.KanbanColor(
  676. row.Get<IKanban, DateTime>(x => x.DueDate),
  677. row.Get<IKanban, DateTime>(x => x.Completed))
  678. : mgrid == EmployeeID
  679. ? Color.Silver
  680. : Color.Plum;
  681. if (row.Get<IKanban, bool>(x => x.Locked))
  682. color = color.MixColors(0.5F, Color.White);
  683. model.ColorKey = ImageUtils.ColorToString(color);
  684. model.Image = empimg;
  685. model.ImageURL = null;
  686. model.Attachments = row.Get<IKanban, int>(x => x.Attachments) > 0; // ? _attachimg : null;
  687. model.DueDate = row.Get<IKanban, DateTime>(x => x.DueDate);
  688. model.CompletedDate = row.Get<IKanban, DateTime>(x => x.Completed);
  689. model.Locked = row.Get<IKanban, bool>(x => x.Locked); // ? _lockimg : null;
  690. var notes = new List<List<string>> { new() };
  691. if ((row.Get<IKanban, string[]>(x => x.Notes) != null))
  692. {
  693. foreach (var line in row.Get<IKanban, string[]>(x => x.Notes))
  694. {
  695. if (line == "===================================")
  696. {
  697. notes.Add(new());
  698. }
  699. else
  700. {
  701. notes.Last().Add(line);
  702. }
  703. }
  704. model.Notes = string.Join("\n===================================\n", notes.Reverse<List<string>>().Select(x => string.Join('\n', x)));
  705. }
  706. model.EmployeeID = empid;
  707. model.ManagerID = mgrid;
  708. var sEmp = "";
  709. if (empid != EmployeeID)
  710. {
  711. if (!Entity.IsEntityLinkValid<IKanban, EmployeeLink>(x => x.EmployeeLink, row))
  712. {
  713. sEmp = "Unallocated";
  714. }
  715. else
  716. {
  717. var tuple = _employeelist.FirstOrDefault(x => x.ID.Equals(empid));
  718. sEmp = tuple != null ? tuple.ID == MyID ? MyName : tuple.Name : "";
  719. }
  720. }
  721. var sMgr = "";
  722. if (mgrid != EmployeeID)
  723. if (mgrid != Guid.Empty)
  724. {
  725. var tuple = _employeelist.FirstOrDefault(x => x.ID.Equals(mgrid));
  726. sMgr = tuple != null ? tuple.ID == MyID ? MyName : tuple.Name : "";
  727. }
  728. if (!string.IsNullOrEmpty(sEmp))
  729. {
  730. if (!string.IsNullOrWhiteSpace(sMgr) && !string.Equals(sMgr, sEmp))
  731. model.AssignedTo = string.Format("Assigned to {0} by {1}", sEmp, sMgr);
  732. else
  733. model.AssignedTo = string.Format("Assigned to {0} ", sEmp);
  734. }
  735. else
  736. {
  737. if (!string.IsNullOrWhiteSpace(sMgr))
  738. model.AssignedTo = string.Format("Allocated by {0} ", sMgr);
  739. }
  740. //model.AssignedTo = String.Format("M: {0} / E: {1}", sMgr, sEmp);
  741. model.JobID = row.Get<IKanban, Guid>(x => x.JobLink.ID);
  742. model.JobNumber = row.Get<IKanban, string>(x => x.JobLink.JobNumber);
  743. if (string.IsNullOrWhiteSpace(model.JobNumber))
  744. model.JobNumber = "";
  745. model.JobName = row.Get<IKanban, string>(x => x.JobLink.Name);
  746. model.Checked = CheckedKanbans.Contains(row.Get<IKanban, Guid>(x => x.ID).ToString());
  747. model.Type = new KanbanType
  748. {
  749. ID = row.Get<IKanban, Guid>(x => x.Type.ID),
  750. Code = row.Get<IKanban, string>(x => x.Type.Code)
  751. };
  752. model.Number = row.Get<IKanban, int>(x => x.Number);
  753. _models.Add(model);
  754. }
  755. }
  756. catch (Exception e)
  757. {
  758. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  759. }
  760. StatusTasksHeaderTimeConverter.Tasks = _models;
  761. FilterKanbans();
  762. }
  763. private void FilterKanbans()
  764. {
  765. Progress.ShowModal("Loading", (progress) =>
  766. {
  767. Dispatcher.BeginInvoke(() => { Progress.Show("Loading"); });
  768. if (JobFilterID == Guid.Empty)
  769. {
  770. var list = _models
  771. .Where(x => x.Search(Search.Text.Split()))
  772. .OrderBy(x => x.EmployeeID == EmployeeID ? 0 : 1).ThenBy(x => x.DueDate);
  773. Dispatcher.BeginInvoke(() => { Kanban.ItemsSource = list; });
  774. }
  775. else
  776. {
  777. var list = _models
  778. .Where(x => x.Search(Search.Text.Split()) && x.JobSearch(JobFilterID))
  779. .OrderBy(x => x.EmployeeID == EmployeeID ? 0 : 1).ThenBy(x => x.DueDate);
  780. Dispatcher.BeginInvoke(() => { Kanban.ItemsSource = list; });
  781. }
  782. Task.Run(() =>
  783. {
  784. Thread.Sleep(500);
  785. Dispatcher.BeginInvoke(() =>
  786. {
  787. Progress.Close();
  788. });
  789. });
  790. });
  791. }
  792. #endregion
  793. #region Kanban Selection Stuff
  794. private void SelectColumn(KanbanColumn column, bool selected)
  795. {
  796. CheckedKanbans.Clear();
  797. if (selected)
  798. {
  799. CheckedKanbans.AddRange(_models.Where(x => Equals(x.Category, column.Categories)).Select(x => x.ID));
  800. foreach (var model in _models)
  801. model.Checked = Equals(model.Category, column.Categories);
  802. }
  803. FilterKanbans();
  804. }
  805. private void UnSelectAll_Click(object sender, RoutedEventArgs e)
  806. {
  807. var column = ((MenuItem)sender).Tag as KanbanColumn;
  808. SelectColumn(column, false);
  809. }
  810. private void SelectAll_Click(object sender, RoutedEventArgs e)
  811. {
  812. var column = ((MenuItem)sender).Tag as KanbanColumn;
  813. SelectColumn(column, true);
  814. }
  815. //private IEnumerable<Guid> GetSelectedKanbanIDs(String currentid)
  816. //{
  817. // List<Guid> result = new List<Guid>();
  818. // if (!CheckedKanbans.Contains(currentid))
  819. // result.Add(Guid.Parse(currentid));
  820. // CheckedKanbans.ForEach((id) => result.Add(Guid.Parse(id)));
  821. // return result;
  822. //}
  823. //private Kanban[] GetSelectedKanbans(String currentid)
  824. //{
  825. // var ids = GetSelectedKanbanIDs(currentid);
  826. // return Host.LoadKanbans(ids).ToArray();
  827. //}
  828. private void CheckBox_Checked(object sender, RoutedEventArgs e)
  829. {
  830. var task = ((CheckBox)sender).Tag as TaskModel;
  831. if (task == null)
  832. return;
  833. if (CheckedKanbans.Contains(task.ID))
  834. CheckedKanbans.Remove(task.ID);
  835. else
  836. CheckedKanbans.Add(task.ID);
  837. }
  838. #endregion
  839. #region Context Menu Actions
  840. private void CreateTask(object sender, RoutedEventArgs e)
  841. {
  842. CreateKanban();
  843. }
  844. private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  845. {
  846. if (e.ClickCount > 1)
  847. {
  848. var task = ((Border)sender).Tag as TaskModel;
  849. DoEdit(task);
  850. e.Handled = true;
  851. }
  852. }
  853. private void EditTask_Click(object sender, RoutedEventArgs e)
  854. {
  855. var task = ((MenuItem)e.Source).Tag as TaskModel;
  856. DoEdit(task);
  857. e.Handled = true;
  858. }
  859. //private void CreateSetout_Click(object sender, RoutedEventArgs e)
  860. //{
  861. // MenuItem menu = sender as MenuItem;
  862. // TaskModel task = menu.Tag as TaskModel;
  863. // if (task.JobID.Equals(Guid.Empty))
  864. // {
  865. // MessageBox.Show("Please link this task to a job before creating a setout!");
  866. // return;
  867. // }
  868. // if (MessageBox.Show("This will convert this task into a Setout.\n\nDo you wish to continue?", "Confirmation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  869. // return;
  870. // ManufacturingTemplate template = new Client<ManufacturingTemplate>().Load(new Filter<ManufacturingTemplate>(x => x.Code).IsEqualTo("PRS")).FirstOrDefault();
  871. // if (template == null)
  872. // {
  873. // MessageBox.Show("[Pressing] Template does not exist!");
  874. // return;
  875. // }
  876. // String setoutnumber = "";
  877. // Progress.ShowModal("Creating Setout", (progress) =>
  878. // {
  879. // MultiQuery query = new MultiQuery();
  880. // query.Add<ManufacturingTemplateStage>(
  881. // new Filter<ManufacturingTemplateStage>(x => x.Template.ID).IsEqualTo(template.ID),
  882. // null,
  883. // new SortOrder<ManufacturingTemplateStage>(x => x.Sequence)
  884. // );
  885. // query.Add<Kanban>(
  886. // new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID),
  887. // null,
  888. // null
  889. // );
  890. // query.Query();
  891. // ManufacturingTemplateStage[] tstages = query.Get<ManufacturingTemplateStage>().Rows.Select(x => x.ToObject<ManufacturingTemplateStage>()).ToArray();
  892. // Kanban kanban = query.Get<Kanban>().Rows.FirstOrDefault()?.ToObject<Kanban>();
  893. // progress.Report("Creating Setouts");
  894. // CoreTable setouts = new Client<Setout>().Query(
  895. // new Filter<Setout>(x => x.JobLink.ID).IsEqualTo(kanban.JobLink.ID),
  896. // new Columns<Setout>(x => x.JobLink.JobNumber, x => x.Number),
  897. // null
  898. // );
  899. // int ireq = 0;
  900. // String sreq = "";
  901. // while (true)
  902. // {
  903. // ireq++;
  904. // sreq = String.Format("{0}-{1:yyMMdd}-{2}", kanban.JobLink.JobNumber, DateTime.Now, ireq);
  905. // if (!setouts.Rows.Any(r => sreq.Equals(r.Get<Setout, String>(c => c.Number))))
  906. // break;
  907. // }
  908. // Setout setout = new Setout();
  909. // setout.Number = sreq;
  910. // setout.JobLink.ID = kanban.JobLink.ID; // = new Client<Job>().Load(new Filter<Job>(x => x.ID).IsEqualTo(kanban.JobLink.ID)).FirstOrDefault();
  911. // setout.Reference = kanban.Title;
  912. // var notes = kanban.Notes.ToList();
  913. // var description = kanban.Summary;
  914. // if (String.IsNullOrWhiteSpace(description))
  915. // description = CoreUtils.StripHTML(kanban.Description);
  916. // if (!String.IsNullOrWhiteSpace(description))
  917. // notes.Insert(0, description);
  918. // setout.Description = String.Join("\n==========================================\n", notes);
  919. // new Client<Setout>().Save(setout, "Created from Task");
  920. // setoutnumber = setout.Number;
  921. // progress.Report("Creating Manufacturing Packet");
  922. // ManufacturingPacket packet = new ManufacturingPacket()
  923. // {
  924. // Group = template.Factory.Name,
  925. // Serial = template.Code,
  926. // Title = kanban.Title,
  927. // Quantity = 1,
  928. // BarcodeQty = 1,
  929. // DueDate = kanban.DueDate
  930. // };
  931. // packet.SetoutLink.ID = setout.ID;
  932. // //packet.JobLink.ID = setout.JobLink.ID;
  933. // packet.ManufacturingTemplateLink.ID = template.ID;
  934. // packet.ManufacturingTemplateLink.Code = template.Code;
  935. // new Client<ManufacturingPacket>().Save(packet, "Created from Task");
  936. // List<ManufacturingPacketStage> pstages = new List<ManufacturingPacketStage>();
  937. // foreach (var tstage in tstages)
  938. // {
  939. // var pstage = new ManufacturingPacketStage()
  940. // {
  941. // Time = tstage.Time,
  942. // Sequence = tstage.Sequence,
  943. // SequenceType = tstage.SequenceType,
  944. // Started = DateTime.MinValue,
  945. // PercentageComplete = 0.0F,
  946. // Completed = DateTime.MinValue,
  947. // QualityChecks = tstage.QualityChecks,
  948. // QualityStatus = QualityStatus.NotChecked,
  949. // QualityNotes = "",
  950. // };
  951. // pstage.Parent.ID = packet.ID;
  952. // pstage.ManufacturingSectionLink.ID = tstage.Section.ID;
  953. // pstage.ManufacturingSectionLink.Name = tstage.Section.Name;
  954. // pstages.Add(pstage);
  955. // }
  956. // new Client<ManufacturingPacketStage>().Save(pstages, "Created from Task", (_, __) => { });
  957. // progress.Report("Processing Documents");
  958. // List<SetoutDocument> _setoutdocuments = new List<SetoutDocument>();
  959. // List<KanbanDocument> _kanbandocuments = new List<KanbanDocument>();
  960. // KanbanDocument[] docrefs = new Client<KanbanDocument>().Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(kanban.ID));
  961. // foreach (var docref in docrefs)
  962. // {
  963. // Guid docid = ProcessSetoutDocument(docref);
  964. // var newdoc = new SetoutDocument();
  965. // newdoc.EntityLink.ID = setout.ID;
  966. // newdoc.DocumentLink.ID = docid;
  967. // _setoutdocuments.Add(newdoc);
  968. // if (docid != docref.DocumentLink.ID)
  969. // {
  970. // docref.DocumentLink.ID = docid;
  971. // _kanbandocuments.Add(docref);
  972. // }
  973. // }
  974. // new Client<SetoutDocument>().Save(_setoutdocuments, "Converted from Task", (_, __) => { });
  975. // new Client<KanbanDocument>().Save(_kanbandocuments, "Converted to PDF", (_, __) => { });
  976. // SetoutKanban link = new SetoutKanban();
  977. // link.Entity.ID = setout.ID;
  978. // link.Kanban.ID = kanban.ID;
  979. // new Client<SetoutKanban>().Save(link, "Converting Task -> Setout", (_, __) => { });
  980. // progress.Report("Updating Task");
  981. // kanban.Title = kanban.Title + " (" + setoutnumber + ")";
  982. // kanban.Locked = true;
  983. // new Client<Kanban>().Save(kanban, "Converting Kanban to Setout");
  984. // });
  985. // MessageBox.Show(String.Format("Created Setout {0}", setoutnumber));
  986. // Refresh();
  987. //}
  988. //private Guid ProcessSetoutDocument(KanbanDocument docref)
  989. //{
  990. // Guid result = docref.DocumentLink.ID;
  991. // String ext = System.IO.Path.GetExtension(docref.DocumentLink.FileName).ToLower();
  992. // if (ext.EndsWith("txt"))
  993. // {
  994. // var doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(docref.DocumentLink.ID)).FirstOrDefault();
  995. // PdfDocument pdf = new PdfDocument();
  996. // PdfPage page = pdf.Pages.Add();
  997. // PdfGraphics graphics = page.Graphics;
  998. // PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 12);
  999. // String text = System.Text.Encoding.UTF8.GetString(doc.Data);
  1000. // graphics.DrawString(text, font, PdfBrushes.Black, new PointF(0, 0));
  1001. // MemoryStream ms = new MemoryStream();
  1002. // pdf.Save(ms);
  1003. // pdf.Close(true);
  1004. // byte[] data = ms.ToArray();
  1005. // var newdoc = new Document()
  1006. // {
  1007. // Data = data,
  1008. // FileName = System.IO.Path.ChangeExtension(docref.DocumentLink.FileName, "pdf"),
  1009. // CRC = CoreUtils.CalculateCRC(data),
  1010. // TimeStamp = DateTime.Now,
  1011. // };
  1012. // new Client<Document>().Save(newdoc, "Converted from Text");
  1013. // return newdoc.ID;
  1014. // }
  1015. // else if (ext.EndsWith("png") || ext.EndsWith("bmp") || ext.EndsWith("jpg") || ext.EndsWith("jpeg"))
  1016. // {
  1017. // var doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(docref.DocumentLink.ID)).FirstOrDefault();
  1018. // PdfBitmap image = new PdfBitmap(new MemoryStream(doc.Data));
  1019. // PdfDocument pdf = new PdfDocument();
  1020. // pdf.PageSettings.Orientation = image.Height > image.Width ? PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
  1021. // pdf.PageSettings.Size = new SizeF(image.Width, image.Height);
  1022. // PdfPage page = pdf.Pages.Add();
  1023. // PdfGraphics graphics = page.Graphics;
  1024. // graphics.DrawImage(image, 0.0F, 0.0F);
  1025. // MemoryStream ms = new MemoryStream();
  1026. // pdf.Save(ms);
  1027. // pdf.Close(true);
  1028. // byte[] data = ms.ToArray();
  1029. // var newdoc = new Document()
  1030. // {
  1031. // Data = data,
  1032. // FileName = System.IO.Path.ChangeExtension(docref.DocumentLink.FileName, "pdf"),
  1033. // CRC = CoreUtils.CalculateCRC(data),
  1034. // TimeStamp = DateTime.Now,
  1035. // };
  1036. // new Client<Document>().Save(newdoc, "Converted from Image");
  1037. // return newdoc.ID;
  1038. // }
  1039. // return result;
  1040. //}
  1041. //private void CreateRequisition_Click(object sender, RoutedEventArgs e)
  1042. //{
  1043. // MenuItem menu = sender as MenuItem;
  1044. // TaskModel task = menu.Tag as TaskModel;
  1045. // if (task.JobID.Equals(Guid.Empty))
  1046. // {
  1047. // MessageBox.Show("Please link this task to a job before creating a requisition!");
  1048. // return;
  1049. // }
  1050. // if (MessageBox.Show("This will convert this task into a Requisition.\n\nDo you wish to continue?", "Confirmation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1051. // return;
  1052. // int requinumber = 0;
  1053. // Progress.ShowModal("Creating Requisition", (progress) =>
  1054. // {
  1055. // Kanban kanban = new Client<Kanban>().Load(new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID)).FirstOrDefault();
  1056. // Requisition requi = new Requisition();
  1057. // requi.JobLink.ID = kanban.JobLink.ID;
  1058. // requi.RequestedBy.ID = kanban.ManagerLink.ID;
  1059. // requi.Employee.ID = Guid.Empty;
  1060. // requi.Title = kanban.Title;
  1061. // requi.Request = String.IsNullOrWhiteSpace(kanban.Summary)
  1062. // ? String.IsNullOrWhiteSpace(kanban.Summary)
  1063. // ? String.Join("\n", kanban.Notes)
  1064. // : CoreUtils.StripHTML(kanban.Description)
  1065. // : kanban.Summary;
  1066. // requi.Notes = kanban.Notes;
  1067. // requi.Due = kanban.DueDate;
  1068. // new Client<Requisition>().Save(requi, "Created from Task");
  1069. // requinumber = requi.Number;
  1070. // progress.Report("Updating Documents");
  1071. // List<RequisitionDocument> _documents = new List<RequisitionDocument>();
  1072. // KanbanDocument[] documents = new Client<KanbanDocument>().Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(kanban.ID));
  1073. // foreach (var document in documents)
  1074. // {
  1075. // var newdoc = new RequisitionDocument();
  1076. // newdoc.EntityLink.ID = requi.ID;
  1077. // newdoc.DocumentLink.ID = document.DocumentLink.ID;
  1078. // _documents.Add(newdoc);
  1079. // }
  1080. // new Client<RequisitionDocument>().Save(_documents, "Converted from Task", (_, __) => { });
  1081. // RequisitionKanban link = new RequisitionKanban();
  1082. // link.Entity.ID = requi.ID;
  1083. // link.Kanban.ID = kanban.ID;
  1084. // new Client<RequisitionKanban>().Save(link, "Converting Task -> Requisition", (_, __) => { });
  1085. // progress.Report("Updating Task");
  1086. // kanban.Category = "Open";
  1087. // kanban.Completed = DateTime.MinValue;
  1088. // kanban.Locked = true;
  1089. // kanban.Title = kanban.Title + " (Requi #" + requi.Number.ToString() + ")";
  1090. // new Client<Kanban>().Save(kanban, "Converted to Requisition", (_, __) => { });
  1091. // });
  1092. // MessageBox.Show(String.Format("Created Requisition {0}", requinumber));
  1093. // Refresh();
  1094. //}
  1095. //private void CreatePurchaseOrder_Click(object sender, RoutedEventArgs e)
  1096. //{
  1097. // MenuItem menu = sender as MenuItem;
  1098. // TaskModel task = menu.Tag as TaskModel;
  1099. // if (MessageBox.Show("This will convert this task into a Purchase Order.\n\nDo you wish to continue?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  1100. // {
  1101. // String ponumber = "";
  1102. // Progress.ShowModal("Creating Purchase Order", (progress) =>
  1103. // {
  1104. // Kanban kanban = new Client<Kanban>().Load(new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID)).FirstOrDefault();
  1105. // PurchaseOrder order = new PurchaseOrder();
  1106. // order.Notes = kanban.Summary;
  1107. // order.RaisedBy.ID = kanban.EmployeeLink.ID;
  1108. // new Client<PurchaseOrder>().Save(order, "Created from Task Screen");
  1109. // ponumber = order.PONumber;
  1110. // progress.Report("Updating Documents");
  1111. // List<PurchaseOrderDocument> docs = new List<PurchaseOrderDocument>();
  1112. // var taskdocs = new Client<KanbanDocument>().Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(Guid.Parse(task.ID)));
  1113. // foreach (var taskdoc in taskdocs)
  1114. // {
  1115. // PurchaseOrderDocument doc = new PurchaseOrderDocument();
  1116. // doc.DocumentLink.ID = taskdoc.DocumentLink.ID;
  1117. // doc.EntityLink.ID = order.ID;
  1118. // docs.Add(doc);
  1119. // }
  1120. // new Client<PurchaseOrderDocument>().Save(docs, "", (_, __) => { });
  1121. // progress.Report("Creating Links");
  1122. // PurchaseOrderKanban link = new PurchaseOrderKanban();
  1123. // link.Entity.ID = order.ID;
  1124. // link.Kanban.ID = kanban.ID;
  1125. // new Client<PurchaseOrderKanban>().Save(link, "Converting Task -> Purchase Order", (_, __) => { });
  1126. // progress.Report("Updating Task");
  1127. // kanban.Category = "Open";
  1128. // kanban.Completed = DateTime.MinValue;
  1129. // kanban.Locked = true;
  1130. // kanban.Title = "(PO#" + order.PONumber.ToString() + ") " + kanban.Title;
  1131. // new Client<Kanban>().Save(kanban, "Converted to Purchase Order", (_, __) => { });
  1132. // });
  1133. // MessageBox.Show(String.Format("Created Purchase Order {0}", ponumber));
  1134. // Refresh();
  1135. // }
  1136. //}
  1137. //private void CreateDelivery_Click(object sender, RoutedEventArgs e)
  1138. //{
  1139. // MenuItem menu = sender as MenuItem;
  1140. // TaskModel task = menu.Tag as TaskModel;
  1141. // if (MessageBox.Show("This will convert this task into a Delivery.\n\nDo you wish to continue?", "Confirmation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1142. // return;
  1143. // int deliverynumber = 0;
  1144. // Progress.ShowModal("Creating Delivery", (progress) =>
  1145. // {
  1146. // Kanban kanban = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID)).Rows.FirstOrDefault()?.ToObject<Kanban>();
  1147. // Delivery delivery = new Delivery();
  1148. // delivery.Due = kanban.DueDate;
  1149. // delivery.Job.ID = kanban.JobLink.ID;
  1150. // delivery.Job.Synchronise(kanban.JobLink);
  1151. // delivery.Notes = kanban.Summary;
  1152. // delivery.Employee.ID = kanban.ManagerLink.ID;
  1153. // delivery.Employee.Synchronise(kanban.ManagerLink);
  1154. // new Client<Delivery>().Save(delivery, "Created From Task");
  1155. // deliverynumber = delivery.Number;
  1156. // progress.Report("Updating Documents");
  1157. // List<DeliveryDocument> docs = new List<DeliveryDocument>();
  1158. // var taskdocs = new Client<KanbanDocument>().Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(Guid.Parse(task.ID)));
  1159. // foreach (var taskdoc in taskdocs)
  1160. // {
  1161. // DeliveryDocument doc = new DeliveryDocument();
  1162. // doc.DocumentLink.ID = taskdoc.DocumentLink.ID;
  1163. // doc.EntityLink.ID = delivery.ID;
  1164. // docs.Add(doc);
  1165. // }
  1166. // new Client<DeliveryDocument>().Save(docs, "", (_, __) => { });
  1167. // progress.Report("Creating Links");
  1168. // DeliveryKanban link = new DeliveryKanban();
  1169. // link.Entity.ID = delivery.ID;
  1170. // link.Kanban.ID = kanban.ID;
  1171. // new Client<DeliveryKanban>().Save(link, "Converting Task -> Delivery", (_, __) => { });
  1172. // progress.Report("Updating Task");
  1173. // kanban.Category = "Open";
  1174. // kanban.Completed = DateTime.MinValue;
  1175. // kanban.Locked = true;
  1176. // kanban.Title = "(Del#" + delivery.Number.ToString() + ") " + kanban.Title;
  1177. // new Client<Kanban>().Save(kanban, "Converted to Delivery", (_, __) => { });
  1178. // });
  1179. // MessageBox.Show(String.Format("Created Delivery {0}", deliverynumber));
  1180. // Refresh();
  1181. //}
  1182. //private void CloseTask_Click(object sender, RoutedEventArgs e)
  1183. //{
  1184. // if (MessageBox.Show("Are you sure you want to remove the selected tasks from the list?", "Confirm removal", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1185. // return;
  1186. // Progress.ShowModal("Closing Kanbans", (progress) =>
  1187. // {
  1188. // TaskModel task = ((MenuItem)e.Source).Tag as TaskModel;
  1189. // Kanban[] kanbans = GetSelectedKanbans(task.ID);
  1190. // for (int i = 0; i < kanbans.Length; i++)
  1191. // {
  1192. // Kanban kanban = kanbans[i];
  1193. // kanban.Closed = DateTime.Now;
  1194. // }
  1195. // new Client<Kanban>().Save(kanbans, "Kanban Marked as Closed");
  1196. // CheckedKanbans.Clear();
  1197. // });
  1198. // Refresh();
  1199. //}
  1200. //private void EmailTask_Click(object sender, RoutedEventArgs e)
  1201. //{
  1202. // MenuItem menu = sender as MenuItem;
  1203. // TaskModel task = menu.Tag as TaskModel;
  1204. // Kanban kanban = new Client<Kanban>().Load(new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID)).FirstOrDefault();
  1205. // List<String> to = new List<string>();
  1206. // String from = "";
  1207. // String salutation = "";
  1208. // if (MyID.HasValue)
  1209. // {
  1210. // CoreRow me = _employees.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.ID).Equals(MyID));
  1211. // if (me != null)
  1212. // from = me.Get<Employee, String>(c => c.Name).Split(' ').FirstOrDefault();
  1213. // if (kanban.EmployeeLink.ID != MyID.Value)
  1214. // {
  1215. // CoreRow emp = _employees.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.ID).Equals(kanban.EmployeeLink.ID));
  1216. // if (emp != null)
  1217. // {
  1218. // String email = emp.Get<Employee, String>(c => c.Email);
  1219. // if (!string.IsNullOrEmpty(email))
  1220. // {
  1221. // to.Add(email);
  1222. // String name = emp.Get<Employee, String>(c => c.Name).Split(' ').FirstOrDefault();
  1223. // salutation = salutation + (String.IsNullOrEmpty(salutation) ? "Hi " : " and ") + name;
  1224. // }
  1225. // }
  1226. // }
  1227. // if (kanban.ManagerLink.ID != MyID.Value)
  1228. // {
  1229. // CoreRow emp = _employees.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.ID).Equals(kanban.ManagerLink.ID));
  1230. // if (emp != null)
  1231. // {
  1232. // String email = emp.Get<Employee, String>(c => c.Email);
  1233. // if (!string.IsNullOrEmpty(email))
  1234. // {
  1235. // to.Add(email);
  1236. // String name = emp.Get<Employee, String>(c => c.Name).Split(' ').FirstOrDefault();
  1237. // salutation = salutation + (String.IsNullOrEmpty(salutation) ? "Hi " : " and ") + name;
  1238. // }
  1239. // }
  1240. // }
  1241. // }
  1242. // Outlook.Application outlookApp = new Outlook.Application();
  1243. // Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
  1244. // mailItem.Subject = "PRS Task: " + kanban.Title;
  1245. // mailItem.To = String.Join("; ", to);
  1246. // 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));
  1247. // //mailItem.Attachments.Add(filename, Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
  1248. // mailItem.Display(false);
  1249. //}
  1250. //private void CompleteTask_Click(object sender, RoutedEventArgs e)
  1251. //{
  1252. // if (MessageBox.Show("Are you sure you want to mark the selected tasks as complete?", "Confirm Completion", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1253. // return;
  1254. // TaskModel task = ((MenuItem)e.Source).Tag as TaskModel;
  1255. // Progress.ShowModal("Completing Task", (progress) =>
  1256. // {
  1257. // Kanban[] kanbans = GetSelectedKanbans(task.ID);
  1258. // for (int i = 0; i < kanbans.Length; i++)
  1259. // {
  1260. // Kanban kanban = kanbans[i];
  1261. // kanban.Completed = DateTime.Now;
  1262. // kanban.Category = "Complete";
  1263. // }
  1264. // new Client<Kanban>().Save(kanbans, "Kanban Marked as Completed");
  1265. // CheckedKanbans.Clear();
  1266. // });
  1267. // Refresh();
  1268. //}
  1269. #endregion
  1270. #region Kanban Creation / Editing
  1271. public void CreateKanban()
  1272. {
  1273. var result = Host.CreateKanban(
  1274. kanban =>
  1275. {
  1276. kanban.EmployeeLink.ID = EmployeeID != CoreUtils.FullGuid ? EmployeeID : MyID.Value;
  1277. kanban.ManagerLink.ID = MyID.Value;
  1278. kanban.ManagerLink.UserLink.ID = ClientFactory.UserGuid;
  1279. });
  1280. if (result != null)
  1281. Refresh(true);
  1282. }
  1283. private void DoEdit(TaskModel task)
  1284. {
  1285. if (task == null)
  1286. return;
  1287. var result = Host.EditReferences(new[] { task });
  1288. if (result)
  1289. Refresh(true);
  1290. }
  1291. #endregion
  1292. private void TaskTypesLabel_OnClick(object sender, RoutedEventArgs e)
  1293. {
  1294. var list = new MasterList(typeof(KanbanType));
  1295. list.ShowDialog();
  1296. SetupKanbanTypesLookup(true);
  1297. }
  1298. private void JobFilterBtn_OnClick(object sender, RoutedEventArgs e)
  1299. {
  1300. if (JobFilterID != Guid.Empty)
  1301. {
  1302. JobFilterBtn.Content = "Filter Job";
  1303. JobFilterID = Guid.Empty;
  1304. FilterKanbans();
  1305. return;
  1306. }
  1307. var window = new ThemableWindow();
  1308. var grid = new JobGrid();
  1309. grid.Options.Remove(DynamicGridOption.EditRows);
  1310. grid.Options.Remove(DynamicGridOption.DeleteRows);
  1311. grid.Options.Remove(DynamicGridOption.AddRows);
  1312. grid.Options.Remove(DynamicGridOption.MultiSelect);
  1313. grid.Options.Remove(DynamicGridOption.ExportData);
  1314. grid.Options.Remove(DynamicGridOption.ImportData);
  1315. grid.OnSelectItem += (object sender, DynamicGridSelectionEventArgs e) =>
  1316. {
  1317. if ((sender as JobGrid).SelectedRows.Count() == 0)
  1318. return;
  1319. else
  1320. {
  1321. var item = (sender as JobGrid).SelectedRows[0];
  1322. AddJobFilter(item);
  1323. window.Close();
  1324. }
  1325. };
  1326. grid.Refresh(true, true);
  1327. window.Content = grid;
  1328. window.ShowDialog();
  1329. }
  1330. Guid JobFilterID = Guid.Empty;
  1331. private void AddJobFilter(CoreRow item)
  1332. {
  1333. JobFilterID = item.Get<Job, Guid>(x => x.ID);
  1334. JobFilterBtn.Content = item.Get<Job, string>(x => x.JobNumber) + " (click to cancel)";
  1335. FilterKanbans();
  1336. }
  1337. }
  1338. }