TaskPanel.xaml.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Controls.Primitives;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Configuration;
  12. using InABox.Core;
  13. using InABox.DynamicGrid;
  14. using InABox.WPF;
  15. using Syncfusion.Pdf.Graphics;
  16. using Syncfusion.Pdf;
  17. using System.Windows.Data;
  18. using System.Windows.Media;
  19. using System.Drawing;
  20. using System.Globalization;
  21. namespace PRSDesktop
  22. {
  23. public class TaskPanelProperties : BaseObject, IGlobalConfigurationSettings
  24. {
  25. [CheckBoxEditor(ToolTip = "Require that all tasks are given a task type.")]
  26. public bool RequireTaskTypes { get; set; } = false;
  27. }
  28. /// <summary>
  29. /// Interaction logic for TaskPanel.xaml
  30. /// </summary>
  31. public partial class TaskPanel : UserControl, IPanel<Kanban>, ITaskHost, IJobControl, IPropertiesPanel<TaskPanelProperties>
  32. {
  33. private bool _bTabChanging;
  34. public Guid MyID { get; set; } = CoreUtils.FullGuid;
  35. private KanbanType[] kanbanTypes = null!; // Initialized in Setup()
  36. public IList<KanbanType> KanbanTypes => kanbanTypes;
  37. public TaskPanel()
  38. {
  39. InitializeComponent();
  40. foreach (TabItem tab in TaskPanels.Items)
  41. {
  42. var panel = (tab.Content as ITaskControl)!;
  43. _viewmap[panel.KanbanView] = tab;
  44. panel.Host = this;
  45. }
  46. if (MyID == CoreUtils.FullGuid)
  47. {
  48. var row = new Client<Employee>()
  49. .Query(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid), new Columns<Employee>(x => x.ID)).Rows
  50. .FirstOrDefault();
  51. if (row != null)
  52. MyID = row.Get<Employee, Guid>(x => x.ID);
  53. }
  54. }
  55. public Guid ParentID { get; set; }
  56. private void ChangeStatus(ITaskControl control, object o, RoutedEventArgs e, string status)
  57. {
  58. if (MessageBox.Show($"Are you sure you want to mark the selected tasks as {status}?", "Confirm Change Status",
  59. MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  60. return;
  61. var tasks = (((MenuItem)e.Source).Tag as IEnumerable<TaskModel>)!;
  62. Progress.ShowModal("Changing Status", progress =>
  63. {
  64. var kanbans = LoadKanbans(tasks, new Columns<Kanban>(x => x.ID, x => x.Completed, x => x.Category));
  65. foreach (var kanban in kanbans)
  66. {
  67. if(status == "Complete")
  68. {
  69. kanban.Completed = DateTime.Now;
  70. }
  71. kanban.Category = status;
  72. }
  73. new Client<Kanban>().Save(kanbans, $"Kanban Marked as {status}");
  74. });
  75. control.Refresh(true);
  76. }
  77. private void CompleteTask(ITaskControl control, RoutedEventArgs e, DateTime completed)
  78. {
  79. if (MessageBox.Show($"Are you sure you want to complete the selected tasks?", "Confirm Completion",
  80. MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  81. return;
  82. var tasks = (((FrameworkElement)e.Source).Tag as IEnumerable<TaskModel>)!;
  83. Progress.ShowModal("Completing Tasks", progress =>
  84. {
  85. var kanbans = LoadKanbans(tasks, new Columns<Kanban>(x => x.ID, x => x.Completed, x => x.Category));
  86. foreach (var kanban in kanbans)
  87. {
  88. kanban.Completed = completed;
  89. kanban.Category = "Complete";
  90. }
  91. new Client<Kanban>().Save(kanbans, $"Kanban Marked as Complete");
  92. });
  93. control.Refresh(true);
  94. }
  95. private void AddChangeStatusButton(ITaskControl control, TaskModel[] models, MenuItem menu, string header, string status)
  96. {
  97. var item = new MenuItem
  98. {
  99. Tag = models,
  100. Header = header
  101. };
  102. item.Click += (o, e) => ChangeStatus(control, o, e, status);
  103. menu.Items.Add(item);
  104. }
  105. public bool CanChangeTasks(IEnumerable<TaskModel> models)
  106. {
  107. foreach (var task in models)
  108. {
  109. if (!MyID.Equals(task.ManagerID) && !MyID.Equals(task.EmployeeID))
  110. {
  111. // If you can change others tasks, IsFullControl is true - but we don't check at the beginning of the function
  112. // to save checking security tokens every time.
  113. return Security.IsAllowed<CanChangeOthersTasks>();
  114. }
  115. }
  116. return true;
  117. }
  118. /// <summary>
  119. /// <paramref name="menu"/> should have <see cref="FrameworkElement.Tag"/> set to a <see cref="TaskModel"/>.
  120. /// </summary>
  121. /// <param name="control"></param>
  122. /// <param name="menu"></param>
  123. public void PopulateMenu(ITaskControl control, ContextMenu menu)
  124. {
  125. menu.Items.Clear();
  126. var models = control.SelectedModels((menu.Tag as TaskModel)!).ToArray();
  127. var references = GetReferences(models);
  128. var bLinks = references.Any(x => x.ReferenceType() != null);
  129. var referencetypes = references.Select(x => x.ReferenceType()).Distinct().ToArray();
  130. var bSingle = models.Length == 1;
  131. var canChange = CanChangeTasks(models);
  132. var edit = new MenuItem
  133. {
  134. Tag = models,
  135. Header = referencetypes.SingleOrDefault() == typeof(Requisition)
  136. ? "Edit Requisition Details"
  137. : referencetypes.SingleOrDefault() == typeof(Setout)
  138. ? "Edit Setout Details"
  139. : referencetypes.SingleOrDefault() == typeof(Delivery)
  140. ? "Edit Delivery Details"
  141. : referencetypes.SingleOrDefault() == typeof(PurchaseOrder)
  142. ? "Edit Order Details"
  143. : "Edit Task" + (bSingle ? "" : "s")
  144. };
  145. edit.Click += (o, e) =>
  146. {
  147. var tasks = (((MenuItem)e.Source).Tag as IEnumerable<TaskModel>)!;
  148. if (EditReferences(tasks))
  149. control.Refresh(true);
  150. e.Handled = true;
  151. };
  152. edit.IsEnabled = referencetypes.Length == 1;
  153. menu.Items.Add(edit);
  154. if (!bLinks && models.Length == 1)
  155. {
  156. var digitalForms = new MenuItem { Header = "Digital Forms" };
  157. var model = models.First();
  158. Guid kanbanID = Guid.Parse(model.ID);
  159. DynamicGridUtils.PopulateFormMenu<KanbanForm, Kanban, KanbanLink>(
  160. digitalForms,
  161. kanbanID,
  162. () => new Client<Kanban>().Load(new Filter<Kanban>(x => x.ID).IsEqualTo(kanbanID)).First(),
  163. model.EmployeeID == MyID);
  164. menu.Items.Add(digitalForms);
  165. }
  166. if (!models.Any(x => !x.CompletedDate.IsEmpty()) && !bLinks)
  167. {
  168. menu.Items.Add(new Separator());
  169. var job = new MenuItem
  170. {
  171. Tag = models,
  172. Header = "Link to Job"
  173. };
  174. job.SubmenuOpened += (o, e) => CreateJobSubMenu(control, job, models);
  175. menu.Items.Add(job);
  176. if (bSingle)
  177. {
  178. menu.AddItem("Create Setout from Task", null, models.First(), task =>
  179. {
  180. var jobID = task.JobID;
  181. if (task.JobID.Equals(Guid.Empty))
  182. {
  183. jobID = ParentID;
  184. }
  185. if (MessageBox.Show("This will convert this task into a Setout.\n\nDo you wish to continue?", "Confirmation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  186. return;
  187. ManufacturingTemplate? template = new Client<ManufacturingTemplate>()
  188. .Load(new Filter<ManufacturingTemplate>(x => x.Code).IsEqualTo("PRS")).FirstOrDefault();
  189. if (template == null)
  190. {
  191. MessageBox.Show("[Pressing] Template does not exist!");
  192. return;
  193. }
  194. string? setoutNumber = null;
  195. Kanban? kanban = null;
  196. ManufacturingTemplateStage[] tstages = Array.Empty<ManufacturingTemplateStage>();
  197. Progress.ShowModal("Creating Setout", (progress) =>
  198. {
  199. var kanbanFilter = new Filter<Kanban>(x => x.ID).IsEqualTo(task.ID);
  200. var tables = Client.QueryMultiple(new Dictionary<string, IQueryDef>
  201. {
  202. { "ManufacturingTemplateStage", new QueryDef<ManufacturingTemplateStage>(
  203. new Filter<ManufacturingTemplateStage>(x => x.Template.ID).IsEqualTo(template.ID),
  204. null,
  205. new SortOrder<ManufacturingTemplateStage>(x => x.Sequence)) },
  206. { "Kanban", new QueryDef<Kanban>(
  207. kanbanFilter,
  208. null,
  209. null) },
  210. { "Setout", new QueryDef<Setout>(
  211. new Filter<Setout>(x => x.JobLink.ID)
  212. .InQuery(new SubQuery<Kanban>(kanbanFilter, new Column<Kanban>(x => x.JobLink.ID))),
  213. new Columns<Setout>(x => x.JobLink.JobNumber, x => x.Number),
  214. null) }
  215. });
  216. tstages = tables["ManufacturingTemplateStage"].Rows
  217. .Select(x => x.ToObject<ManufacturingTemplateStage>()).ToArray();
  218. kanban = tables["Kanban"].Rows.FirstOrDefault()?.ToObject<Kanban>();
  219. if (kanban == null)
  220. {
  221. MessageBox.Show("Task does not exist!");
  222. return;
  223. }
  224. progress.Report("Creating Setouts");
  225. CoreTable setouts = tables["Setout"];
  226. int ireq = 0;
  227. string sreq = "";
  228. while (true)
  229. {
  230. ireq++;
  231. sreq = string.Format("{0}-{1:yyMMdd}-{2}", kanban.JobLink.JobNumber, DateTime.Now, ireq);
  232. if (!setouts.Rows.Any(r => sreq.Equals(r.Get<Setout, String>(c => c.Number))))
  233. break;
  234. }
  235. setoutNumber = sreq;
  236. });
  237. if (setoutNumber == null || kanban == null)
  238. {
  239. return;
  240. }
  241. var result = CreateSetout(
  242. task,
  243. s =>
  244. {
  245. s.Number = setoutNumber;
  246. s.JobLink.ID = jobID;
  247. var notes = kanban.Notes.ToList();
  248. var description = kanban.Summary;
  249. if (string.IsNullOrWhiteSpace(description))
  250. {
  251. description = CoreUtils.StripHTML(kanban.Description);
  252. }
  253. if (!string.IsNullOrWhiteSpace(description))
  254. {
  255. notes.Insert(0, description);
  256. }
  257. s.Description = string.Join("\n==========================================\n", notes);
  258. }
  259. );
  260. if (result != null)
  261. {
  262. Progress.ShowModal("Creating Manufacturing Packet", progress =>
  263. {
  264. ManufacturingPacket packet = new ManufacturingPacket()
  265. {
  266. Serial = template.Code,
  267. Title = kanban.Title,
  268. Quantity = 1,
  269. BarcodeQty = 1,
  270. DueDate = kanban.DueDate
  271. };
  272. packet.ManufacturingTemplateLink.ID = template.ID;
  273. packet.ManufacturingTemplateLink.Code = template.Code;
  274. packet.ManufacturingTemplateLink.Factory.ID = template.Factory.ID;
  275. packet.SetoutLink.ID = result.ID;
  276. new Client<ManufacturingPacket>().Save(packet, "Created from Task");
  277. DoLink<ManufacturingPacketKanban, ManufacturingPacket, ManufacturingPacketLink>(task, packet.ID);
  278. List<ManufacturingPacketStage> pstages = new List<ManufacturingPacketStage>();
  279. foreach (var tstage in tstages)
  280. {
  281. var pstage = new ManufacturingPacketStage()
  282. {
  283. Time = tstage.Time,
  284. Sequence = tstage.Sequence,
  285. SequenceType = tstage.SequenceType,
  286. Started = DateTime.MinValue,
  287. PercentageComplete = 0.0F,
  288. Completed = DateTime.MinValue,
  289. QualityChecks = tstage.QualityChecks,
  290. QualityStatus = QualityStatus.NotChecked,
  291. QualityNotes = "",
  292. };
  293. pstage.Parent.ID = packet.ID;
  294. pstage.ManufacturingSectionLink.ID = tstage.Section.ID;
  295. pstage.ManufacturingSectionLink.Name = tstage.Section.Name;
  296. pstages.Add(pstage);
  297. }
  298. new Client<ManufacturingPacketStage>().Save(pstages, "Created from Task", (_, __) => { });
  299. progress.Report("Processing Documents");
  300. List<SetoutDocument> _setoutdocuments = new List<SetoutDocument>();
  301. List<KanbanDocument> _kanbandocuments = new List<KanbanDocument>();
  302. KanbanDocument[] docrefs = new Client<KanbanDocument>()
  303. .Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(kanban.ID));
  304. foreach (var docref in docrefs)
  305. {
  306. // Convert the document to a PDF
  307. var docid = ProcessKanbanDocument(docref);
  308. var newdoc = new SetoutDocument();
  309. newdoc.EntityLink.ID = result.ID;
  310. newdoc.DocumentLink.ID = docid;
  311. _setoutdocuments.Add(newdoc);
  312. if (docid != docref.DocumentLink.ID)
  313. {
  314. docref.DocumentLink.ID = docid;
  315. _kanbandocuments.Add(docref);
  316. }
  317. }
  318. new Client<SetoutDocument>().Save(_setoutdocuments, "Converted from Task", (_, __) => { });
  319. new Client<KanbanDocument>().Save(_kanbandocuments, "Converted to PDF", (_, __) => { });
  320. progress.Report("Updating Task");
  321. kanban.Title = kanban.Title + " (" + result.Number + ")";
  322. new Client<Kanban>().Save(kanban, "Converting Kanban to Setout");
  323. });
  324. control.Refresh(true);
  325. }
  326. });
  327. menu.AddItem("Create Requisition from Task", null, models, tasks =>
  328. {
  329. var taskModel = tasks.First();
  330. var jobID = taskModel.JobID;
  331. if (taskModel.JobID.Equals(Guid.Empty))
  332. {
  333. jobID = ParentID;
  334. }
  335. var kanbanTable = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(taskModel.ID));
  336. var kanban = kanbanTable.Rows.First().ToObject<Kanban>();
  337. var result = CreateRequisition(
  338. taskModel,
  339. r =>
  340. {
  341. r.RequestedBy.ID = kanban.ManagerLink.ID;
  342. r.Employee.ID = Guid.Empty;
  343. r.Title = kanban.Title;
  344. r.Request = string.IsNullOrWhiteSpace(kanban.Summary)
  345. ? String.IsNullOrWhiteSpace(kanban.Description)
  346. ? String.Join("\n", kanban.Notes)
  347. : CoreUtils.StripHTML(kanban.Description)
  348. : kanban.Summary;
  349. r.Notes = kanban.Notes;
  350. r.Due = kanban.DueDate;
  351. r.JobLink.ID = jobID;
  352. }
  353. );
  354. if (result != null)
  355. {
  356. Progress.ShowModal("Updating Documents", progress =>
  357. {
  358. progress.Report("Updating Documents");
  359. List<RequisitionDocument> requiDocuments = new();
  360. KanbanDocument[] kanbanDocuments = new Client<KanbanDocument>()
  361. .Load(new Filter<KanbanDocument>(x => x.EntityLink.ID).IsEqualTo(kanban.ID));
  362. foreach (var document in kanbanDocuments)
  363. {
  364. var newdoc = new RequisitionDocument();
  365. newdoc.EntityLink.ID = result.ID;
  366. newdoc.DocumentLink.ID = document.DocumentLink.ID;
  367. requiDocuments.Add(newdoc);
  368. }
  369. new Client<RequisitionDocument>().Save(requiDocuments, "Converted from Task", (_, __) => { });
  370. /*RequisitionKanban link = new();
  371. link.Entity.ID = result.ID;
  372. link.Kanban.ID = kanban.ID;
  373. new Client<RequisitionKanban>().Save(link, "Converting Task -> Requisition", (_, __) => { });*/
  374. progress.Report("Updating Task");
  375. kanban.Category = "Open";
  376. kanban.Completed = DateTime.MinValue;
  377. kanban.Title += $" (Requi #{result.Number})";
  378. new Client<Kanban>().Save(kanban, "Converted to Requisition", (_, __) => { });
  379. });
  380. MessageBox.Show(String.Format("Created Requisition {0}", result.Number));
  381. control.Refresh(true);
  382. }
  383. });
  384. menu.AddItem("Create Delivery from Task", null, models, tasks =>
  385. {
  386. var result = CreateDelivery(
  387. tasks.First(),
  388. d =>
  389. {
  390. // Post-Process Requi Here
  391. }
  392. );
  393. if (result != null)
  394. control.Refresh(true);
  395. });
  396. menu.AddItem("Create Purchase Order from Task", null, models, tasks =>
  397. {
  398. var result = CreateOrder(
  399. tasks.First(),
  400. p =>
  401. {
  402. // Post-Process Requi Here
  403. }
  404. );
  405. if (result != null)
  406. control.Refresh(true);
  407. });
  408. }
  409. }
  410. if (!bLinks && canChange)
  411. {
  412. menu.Items.Add(new Separator());
  413. var changeStatus = new MenuItem { Header = "Change Status" };
  414. AddChangeStatusButton(control, models, changeStatus, "Open", "Open");
  415. AddChangeStatusButton(control, models, changeStatus, "In Progress", "In Progress");
  416. AddChangeStatusButton(control, models, changeStatus, "Waiting", "Waiting");
  417. if (models.Any(x => x.CompletedDate.IsEmpty()))
  418. {
  419. var complete = new MenuItem
  420. {
  421. Tag = models,
  422. Header = models.Length > 1 ? "Complete Tasks" : "Complete Task"
  423. };
  424. complete.Click += (o, e) =>
  425. {
  426. CompleteTask(control, e, DateTime.Now);
  427. };
  428. menu.Items.Add(complete);
  429. if (Security.IsAllowed<CanSetKanbanCompletedDate>())
  430. {
  431. var completeDate = new MenuItem
  432. {
  433. Tag = models,
  434. Header = "Set Completed Date"
  435. };
  436. var dateItem = new MenuItem();
  437. var dateCalendar = new System.Windows.Controls.Calendar { SelectedDate = DateTime.MinValue };
  438. dateCalendar.Tag = models;
  439. dateCalendar.SelectedDatesChanged += (o, e) =>
  440. {
  441. if (e.Source is not System.Windows.Controls.Calendar calendar) return;
  442. menu.IsOpen = false;
  443. var selectedDate = calendar.SelectedDate ?? DateTime.Now;
  444. CompleteTask(control, e, selectedDate);
  445. };
  446. dateItem.Header = dateCalendar;
  447. dateItem.Style = Resources["calendarItem"] as Style;
  448. completeDate.Items.Add(dateItem);
  449. menu.Items.Add(completeDate);
  450. }
  451. }
  452. else
  453. {
  454. menu.AddItem(models.Length > 1 ? "Archive Tasks" : "Archive Task", null, models, tasks =>
  455. {
  456. if (MessageBox.Show("Are you sure you want to remove the selected tasks from the list?", "Confirm removal",
  457. MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  458. return;
  459. Progress.ShowModal("Closing Kanbans", progress =>
  460. {
  461. var kanbans = LoadKanbans(tasks, new Columns<Kanban>(x => x.ID, x => x.Closed));
  462. foreach (var kanban in kanbans)
  463. kanban.Closed = DateTime.Now;
  464. new Client<Kanban>().Save(kanbans, "Kanban Marked as Closed");
  465. });
  466. control.Refresh(true);
  467. });
  468. }
  469. menu.Items.Add(changeStatus);
  470. var changeType = new MenuItem { Header = "Change Task Type", Tag = models };
  471. foreach(var type in KanbanTypes)
  472. {
  473. changeType.AddItem($"{type.Code}: {type.Description}", null, type, type =>
  474. {
  475. Progress.ShowModal("Changing Task Type", progress =>
  476. {
  477. var kanbans = LoadKanbans(models, new Columns<Kanban>(x => x.ID, x => x.Type.ID));
  478. foreach (var kanban in kanbans)
  479. {
  480. kanban.Type.ID = type.ID;
  481. }
  482. new Client<Kanban>().Save(kanbans, $"Kanban Task Type changed to {type}");
  483. });
  484. control.Refresh(true);
  485. });
  486. }
  487. menu.Items.Add(changeType);
  488. var changeDueDate = new MenuItem { Header = "Change Due Date" };
  489. var calendarItem = new MenuItem();
  490. var calendar = new System.Windows.Controls.Calendar { SelectedDate = models.Length == 1 ? models[0].DueDate : DateTime.Today };
  491. calendar.Tag = models;
  492. calendar.SelectedDatesChanged += (o, e) =>
  493. {
  494. if (e.Source is not System.Windows.Controls.Calendar calendar) return;
  495. var selectedDate = calendar.SelectedDate ?? DateTime.Now;
  496. var models = (calendar.Tag as IList<TaskModel>)!;
  497. Progress.ShowModal("Changing Due Date", progress =>
  498. {
  499. var kanbans = LoadKanbans(models, new Columns<Kanban>(x => x.ID, x => x.DueDate));
  500. foreach (var kanban in kanbans)
  501. {
  502. kanban.DueDate = selectedDate;
  503. }
  504. new Client<Kanban>().Save(kanbans, $"Kanban Due Date changed to {selectedDate:dd MMM yyyy}");
  505. });
  506. control.Refresh(true);
  507. menu.IsOpen = false;
  508. };
  509. calendarItem.Header = calendar;
  510. calendarItem.Style = Resources["calendarItem"] as Style;
  511. changeDueDate.Items.Add(calendarItem);
  512. menu.Items.Add(changeDueDate);
  513. }
  514. }
  515. /// <summary>
  516. /// Takes a <see cref="KanbanDocument"/>, and if it is a .txt or an image (".png", ".jpg", ".jpeg" or ".bmp"), converts to a PDF
  517. /// with the content of the document, saving a new document with extension changed to ".pdf".
  518. /// </summary>
  519. /// <param name="docref">The original document.</param>
  520. /// <returns>
  521. /// The ID of the new <see cref="Document"/> or,
  522. /// if not one of the given types, the original document ID.
  523. /// </returns>
  524. private static Guid ProcessKanbanDocument(KanbanDocument docref)
  525. {
  526. var result = docref.DocumentLink.ID;
  527. var ext = System.IO.Path.GetExtension(docref.DocumentLink.FileName).ToLower();
  528. if (ext.EndsWith("txt"))
  529. {
  530. var doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(docref.DocumentLink.ID)).FirstOrDefault();
  531. if (doc is null)
  532. {
  533. Logger.Send(LogType.Error, "", $"Document {docref.DocumentLink.ID} does not exist!");
  534. return docref.DocumentLink.ID;
  535. }
  536. PdfDocument pdf = new PdfDocument();
  537. PdfPage page = pdf.Pages.Add();
  538. PdfGraphics graphics = page.Graphics;
  539. PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 12);
  540. String text = System.Text.Encoding.UTF8.GetString(doc.Data);
  541. graphics.DrawString(text, font, PdfBrushes.Black, new PointF(0, 0));
  542. MemoryStream ms = new MemoryStream();
  543. pdf.Save(ms);
  544. pdf.Close(true);
  545. byte[] data = ms.ToArray();
  546. var newdoc = new Document()
  547. {
  548. Data = data,
  549. FileName = System.IO.Path.ChangeExtension(docref.DocumentLink.FileName, "pdf"),
  550. CRC = CoreUtils.CalculateCRC(data),
  551. TimeStamp = DateTime.Now,
  552. };
  553. new Client<Document>().Save(newdoc, "Converted from Text");
  554. return newdoc.ID;
  555. }
  556. else if (ext.EndsWith("png") || ext.EndsWith("bmp") || ext.EndsWith("jpg") || ext.EndsWith("jpeg"))
  557. {
  558. var doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(docref.DocumentLink.ID)).FirstOrDefault();
  559. if (doc is null)
  560. {
  561. Logger.Send(LogType.Error, "", $"Document {docref.DocumentLink.ID} does not exist!");
  562. return docref.DocumentLink.ID;
  563. }
  564. PdfBitmap image = new PdfBitmap(new MemoryStream(doc.Data));
  565. PdfDocument pdf = new PdfDocument();
  566. pdf.PageSettings.Orientation = image.Height > image.Width ? PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
  567. pdf.PageSettings.Size = new SizeF(image.Width, image.Height);
  568. PdfPage page = pdf.Pages.Add();
  569. PdfGraphics graphics = page.Graphics;
  570. graphics.DrawImage(image, 0.0F, 0.0F);
  571. MemoryStream ms = new MemoryStream();
  572. pdf.Save(ms);
  573. pdf.Close(true);
  574. byte[] data = ms.ToArray();
  575. var newdoc = new Document()
  576. {
  577. Data = data,
  578. FileName = System.IO.Path.ChangeExtension(docref.DocumentLink.FileName, "pdf"),
  579. CRC = CoreUtils.CalculateCRC(data),
  580. TimeStamp = DateTime.Now,
  581. };
  582. new Client<Document>().Save(newdoc, "Converted from Image");
  583. return newdoc.ID;
  584. }
  585. return result;
  586. }
  587. private void TaskPanels_SelectionChanged(object sender, SelectionChangedEventArgs e)
  588. {
  589. if (!IsReady)
  590. return;
  591. if (e.Source is not TabControl)
  592. return;
  593. if (_bTabChanging)
  594. return;
  595. try
  596. {
  597. _bTabChanging = true;
  598. var panel = GetCurrentPanel();
  599. if(panel is not null)
  600. {
  601. Settings.ViewType = panel.KanbanView;
  602. new UserConfiguration<KanbanSettings>().Save(Settings);
  603. panel.Refresh(false);
  604. }
  605. }
  606. finally
  607. {
  608. _bTabChanging = false;
  609. }
  610. }
  611. private void CreateJobSubMenu(ITaskControl control, MenuItem job, IEnumerable<TaskModel> tasks)
  612. {
  613. job.Items.Clear();
  614. job.Items.Add(new MenuItem { Header = "Loading...", IsEnabled = false });
  615. using (new WaitCursor())
  616. {
  617. job.Items.Clear();
  618. var jobs = new Client<Job>().Query(
  619. LookupFactory.DefineFilter<Job>(),
  620. LookupFactory.DefineColumns<Job>(),
  621. LookupFactory.DefineSort<Job>()
  622. );
  623. foreach (var row in jobs.Rows)
  624. {
  625. var jobNumber = row.Get<Job, string>(x => x.JobNumber);
  626. var jobName = row.Get<Job, string>(x => x.Name);
  627. job.AddItem($"{jobNumber}: {jobName}", null, tasks, tasks =>
  628. {
  629. using (new WaitCursor())
  630. {
  631. var kanbans = LoadKanbans(tasks, new Columns<Kanban>(x => x.ID, x => x.JobLink.ID));
  632. foreach (var kanban in kanbans)
  633. kanban.JobLink.ID = row.Get<Job, Guid>(x => x.ID);
  634. new Client<Kanban>().Save(kanbans, "Updated Job Number");
  635. control.Refresh(false);
  636. }
  637. });
  638. }
  639. }
  640. }
  641. #region Get/Save Settings
  642. private KanbanSettings? _settings;
  643. public KanbanSettings Settings
  644. {
  645. get
  646. {
  647. _settings ??= new UserConfiguration<KanbanSettings>().Load();
  648. return _settings;
  649. }
  650. }
  651. public void SaveSettings()
  652. {
  653. if(_settings != null)
  654. new UserConfiguration<KanbanSettings>().Save(_settings);
  655. }
  656. #endregion
  657. #region IPanel Stuff
  658. public event DataModelUpdateEvent? OnUpdateDataModel;
  659. public bool IsReady { get; set; }
  660. public void CreateToolbarButtons(IPanelHost host)
  661. {
  662. host.CreatePanelAction(
  663. new PanelAction
  664. {
  665. Caption = "New Task",
  666. OnExecute = a => {
  667. if(CreateKanban(k => { }) != null)
  668. {
  669. Refresh();
  670. }
  671. },
  672. Image = PRSDesktop.Resources.add
  673. }
  674. );
  675. }
  676. public Dictionary<string, object[]> Selected()
  677. {
  678. return new Dictionary<string, object[]>();
  679. }
  680. public void Heartbeat(TimeSpan time)
  681. {
  682. }
  683. private readonly Dictionary<KanbanView, TabItem> _viewmap = new();
  684. private readonly List<ITaskControl> _initialized = new();
  685. private ITaskControl
  686. GetCurrentPanel()
  687. {
  688. var result = (TaskPanels.SelectedContent as ITaskControl)!;
  689. if (result == null)
  690. result = (TaskPanels.Items[0] as DynamicTabItem)?.Content as ITaskControl;
  691. try
  692. {
  693. //if (result != null)
  694. if (!_initialized.Contains(result))
  695. {
  696. result.Setup();
  697. result.IsReady = true;
  698. _initialized.Add(result);
  699. }
  700. }
  701. catch (Exception e)
  702. {
  703. Logger.Send(LogType.Error, "", $"Error in TaskPanel.GetCurrentPanel: {CoreUtils.FormatException(e)}");
  704. }
  705. return result;
  706. }
  707. public void Setup()
  708. {
  709. _settings = new UserConfiguration<KanbanSettings>().Load();
  710. TaskPanels.SelectedItem = _viewmap[_settings.ViewType];
  711. kanbanTypes = new Client<KanbanType>()
  712. .Query(new Filter<KanbanType>(x => x.Hidden).IsEqualTo(false), new Columns<KanbanType>(x => x.ID, x => x.Code, x => x.Description))
  713. .Rows.Select(x => x.ToObject<KanbanType>()).ToArray();
  714. }
  715. public void Shutdown()
  716. {
  717. }
  718. public void Refresh()
  719. {
  720. if (ParentID == Guid.Empty)
  721. {
  722. if (TaskPanels.SelectedItem == TasksPlannerTabItem)
  723. TaskPanels.SelectedItem = _viewmap[KanbanView.Status];
  724. if (TasksPlannerTabItem.Visibility == Visibility.Visible)
  725. TasksPlannerTabItem.Visibility = Visibility.Collapsed;
  726. }
  727. else
  728. {
  729. if (TasksPlannerTabItem.Visibility == Visibility.Collapsed)
  730. TasksPlannerTabItem.Visibility = Visibility.Visible;
  731. }
  732. TasksPlannerTabItem.Visibility = ParentID != Guid.Empty ? Visibility.Visible : Visibility.Collapsed;
  733. GetCurrentPanel()?.Refresh(false);
  734. }
  735. public string SectionName => GetCurrentPanel().SectionName;
  736. public TaskPanelProperties Properties { get; set; }
  737. public DataModel DataModel(Selection selection)
  738. {
  739. return GetCurrentPanel().DataModel(selection);
  740. //return new AutoDataModel<Kanban>(new Filter<Kanban>(x => x.ID).IsEqualTo(Guid.Empty));
  741. }
  742. #endregion
  743. #region CRUD Functionality
  744. private TEntity? DoCreate<TEntity>(Action<TEntity> customise)
  745. where TEntity : Entity, IRemotable, IPersistent, new()
  746. {
  747. var result = new TEntity();
  748. customise?.Invoke(result);
  749. if (DoEdit(new[] { result }, null))
  750. return result;
  751. return null;
  752. }
  753. private readonly Dictionary<Type, IDynamicGrid> _grids = new();
  754. private readonly List<Tuple<Guid, Entity>> _entitycache = new();
  755. private DynamicDataGrid<TEntity> GetGrid<TEntity>() where TEntity : Entity, IRemotable, IPersistent, new()
  756. {
  757. if(!_grids.TryGetValue(typeof(TEntity), out var grid))
  758. {
  759. grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(TEntity)) as DynamicDataGrid<TEntity>)!;
  760. _grids[typeof(TEntity)] = grid;
  761. if (typeof(TEntity) == typeof(Kanban))
  762. {
  763. CustomiseKanbanGrid((grid as DynamicDataGrid<Kanban>)!);
  764. }
  765. }
  766. return (grid as DynamicDataGrid<TEntity>)!;
  767. }
  768. private IEnumerable<TEntity> DoLoad<TEntity>(IEnumerable<TaskModel> models, Columns<TEntity> columns)
  769. where TEntity : Entity, IRemotable, IPersistent, new()
  770. {
  771. var result = new List<TEntity>();
  772. var load = new List<Guid>();
  773. foreach (var model in models)
  774. {
  775. var id = Guid.Parse(model.ID);
  776. var entity = _entitycache.FirstOrDefault(x => Equals(x.Item1, id) && x.Item2 is TEntity) as TEntity;
  777. if (entity is not null)
  778. result.Add(entity);
  779. else
  780. load.Add(id);
  781. }
  782. if (load.Any())
  783. {
  784. var entities = new Client<TEntity>()
  785. .Query(new Filter<TEntity>(x => x.ID).InList(load.ToArray()), columns)
  786. .Rows.Select(x => x.ToObject<TEntity>()).ToList();
  787. foreach (var entity in entities)
  788. _entitycache.Add(new Tuple<Guid, Entity>(entity.ID, entity));
  789. result.AddRange(entities);
  790. }
  791. return result;
  792. }
  793. private IEnumerable<TEntity> DoLoad<TEntityKanban, TEntity, TLink>(IEnumerable<TaskModel> models, Columns<TEntity> columns)
  794. where TEntityKanban : EntityKanban<TEntity, TLink>, new()
  795. where TEntity : Entity, IRemotable, IPersistent, new()
  796. where TLink : IEntityLink<TEntity>, new()
  797. {
  798. var result = DoLoad(models, columns);
  799. if (!result.Any())
  800. foreach (var model in models)
  801. {
  802. var id = Guid.Parse(model.ID);
  803. result = new Client<TEntity>().Load(
  804. new Filter<TEntity>(x => x.ID).InQuery(new Filter<TEntityKanban>(x => x.Kanban.ID).IsEqualTo(id),
  805. x => x.Entity.ID));
  806. foreach (var r in result)
  807. _entitycache.Add(new Tuple<Guid, Entity>(id, r));
  808. }
  809. return result;
  810. }
  811. private void DoCache<TEntity>(Guid kanbanid, TEntity entity) where TEntity : Entity
  812. {
  813. if (!_entitycache.Any(x => Equals(x.Item1, kanbanid) && x.Item2 is TEntity && Equals(x.Item2.ID, entity.ID)))
  814. _entitycache.Add(new Tuple<Guid, Entity>(kanbanid, entity));
  815. }
  816. private bool DoEdit<TEntity>(IEnumerable<TEntity> entities, Action<TEntity>? action = null)
  817. where TEntity : Entity, IRemotable, IPersistent, new()
  818. {
  819. if (entities == null || !entities.Any())
  820. return false;
  821. foreach (var entity in entities)
  822. action?.Invoke(entity);
  823. return GetGrid<TEntity>().EditItems(entities.ToArray());
  824. }
  825. private void DoLink<TEntityKanban, TEntity, TLink>(TaskModel model, Guid entityid)
  826. where TEntityKanban : EntityKanban<TEntity, TLink>, new()
  827. where TEntity : Entity, IRemotable, IPersistent, new()
  828. where TLink : IEntityLink<TEntity>, new()
  829. {
  830. var linktask = Task.Run(() =>
  831. {
  832. var link = new TEntityKanban();
  833. link.Kanban.ID = Guid.Parse(model.ID);
  834. link.Entity.ID = entityid;
  835. new Client<TEntityKanban>().Save(link, "");
  836. });
  837. var kanbantask = Task.Run(() =>
  838. {
  839. var kanban = LoadKanbans(new[] { model }, new Columns<Kanban>(x => x.ID, x => x.Locked)).FirstOrDefault();
  840. if (kanban is not null)
  841. {
  842. kanban.Locked = true;
  843. new Client<Kanban>().Save(kanban, "Locked because of linked " + typeof(TEntity).EntityName().Split('.').Last());
  844. }
  845. });
  846. Task.WaitAll(linktask, kanbantask);
  847. }
  848. private static void DoDelete<TEntity>(IList<TEntity> entities, string auditnote)
  849. where TEntity : Entity, IRemotable, IPersistent, new()
  850. {
  851. new Client<TEntity>().Delete(entities, auditnote);
  852. }
  853. public Kanban? CreateKanban(Action<Kanban> customise)
  854. {
  855. var result = DoCreate<Kanban>(
  856. kanban =>
  857. {
  858. kanban.Title = "New Task";
  859. kanban.Description = "";
  860. kanban.Category = "Open";
  861. kanban.DueDate = DateTime.Today;
  862. kanban.Private = false;
  863. kanban.JobLink.ID = ParentID;
  864. kanban.EmployeeLink.ID = MyID;
  865. kanban.ManagerLink.ID = MyID;
  866. customise?.Invoke(kanban);
  867. });
  868. if (result != null)
  869. DoCache(result.ID, result);
  870. return result;
  871. }
  872. public IEnumerable<Kanban> LoadKanbans(IEnumerable<TaskModel> models, Columns<Kanban> columns)
  873. {
  874. columns.Add(x => x.ID);
  875. columns.Add(x => x.Number);
  876. columns.Add(x => x.Title);
  877. columns.Add(x => x.Notes);
  878. columns.Add(x => x.Summary);
  879. columns.Add(x => x.Completed);
  880. columns.Add(x => x.DueDate);
  881. columns.Add(x => x.ManagerLink.ID);
  882. columns.Add(x => x.EmployeeLink.ID);
  883. return DoLoad(models, columns);
  884. }
  885. public void OnValidateKanban(object sender, Kanban[] items, List<string> errors)
  886. {
  887. if (Properties.RequireTaskTypes && items.Any(x => x.Type.ID == Guid.Empty))
  888. {
  889. errors.Add("[Task Type] may not be blank!");
  890. }
  891. }
  892. public void CustomiseKanbanGrid(DynamicDataGrid<Kanban> grid)
  893. {
  894. grid.OnValidate += OnValidateKanban;
  895. }
  896. public bool EditKanbans(IEnumerable<TaskModel> models, Action<Kanban>? customise = null)
  897. {
  898. var entities = LoadKanbans(models, GetGrid<Kanban>().LoadEditorColumns());
  899. return DoEdit(entities, customise);
  900. }
  901. public void DeleteKanbans(IEnumerable<TaskModel> models, string auditnote)
  902. {
  903. var kanbans = models.Select(x => new Kanban { ID = Guid.Parse(x.ID) }).ToList();
  904. DoDelete(kanbans, auditnote);
  905. }
  906. public Requisition? CreateRequisition(TaskModel model, Action<Requisition>? customise)
  907. {
  908. var result = DoCreate<Requisition>(
  909. requi =>
  910. {
  911. requi.JobLink.ID = ParentID;
  912. customise?.Invoke(requi);
  913. });
  914. if (result != null)
  915. {
  916. var id = Guid.Parse(model.ID);
  917. DoCache(id, result);
  918. DoLink<RequisitionKanban, Requisition, RequisitionLink>(model, result.ID);
  919. }
  920. return result;
  921. }
  922. public bool EditRequisitions(IEnumerable<TaskModel> models, Action<Requisition>? customise = null)
  923. {
  924. var requis = DoLoad<RequisitionKanban, Requisition, RequisitionLink>(models, GetGrid<Requisition>().LoadEditorColumns());
  925. if (requis.Any())
  926. return DoEdit(requis, customise);
  927. return false;
  928. }
  929. public Setout? CreateSetout(TaskModel model, Action<Setout> customise)
  930. {
  931. var result = DoCreate<Setout>(
  932. setout =>
  933. {
  934. setout.JobLink.ID = ParentID;
  935. customise?.Invoke(setout);
  936. });
  937. if (result != null)
  938. {
  939. var id = Guid.Parse(model.ID);
  940. DoCache(id, result);
  941. //DoLink<SetoutKanban, Setout, SetoutLink>(model, result.ID);
  942. }
  943. return result;
  944. }
  945. public bool EditSetouts(IEnumerable<TaskModel> models, Action<Setout>? customise = null)
  946. {
  947. var setouts = DoLoad<SetoutKanban, Setout, SetoutLink>(models, GetGrid<Setout>().LoadEditorColumns());
  948. if (setouts.Any())
  949. return DoEdit(setouts, customise);
  950. return false;
  951. }
  952. public Delivery? CreateDelivery(TaskModel model, Action<Delivery> customise)
  953. {
  954. var result = DoCreate<Delivery>(
  955. delivery =>
  956. {
  957. delivery.Job.ID = ParentID;
  958. customise?.Invoke(delivery);
  959. });
  960. if (result != null)
  961. {
  962. var id = Guid.Parse(model.ID);
  963. DoCache(id, result);
  964. DoLink<DeliveryKanban, Delivery, DeliveryLink>(model, result.ID);
  965. }
  966. return result;
  967. }
  968. public bool EditDeliveries(IEnumerable<TaskModel> models, Action<Delivery>? customise = null)
  969. {
  970. var deliveries = DoLoad<DeliveryKanban, Delivery, DeliveryLink>(models, GetGrid<Delivery>().LoadEditorColumns());
  971. if (deliveries.Any())
  972. return DoEdit(deliveries, customise);
  973. return false;
  974. }
  975. public PurchaseOrder? CreateOrder(TaskModel model, Action<PurchaseOrder> customise)
  976. {
  977. var result = DoCreate<PurchaseOrder>(
  978. order => { customise?.Invoke(order); });
  979. if (result != null)
  980. {
  981. var id = Guid.Parse(model.ID);
  982. DoCache(id, result);
  983. DoLink<PurchaseOrderKanban, PurchaseOrder, PurchaseOrderLink>(model, result.ID);
  984. }
  985. return result;
  986. }
  987. public bool EditPurchaseOrders(IEnumerable<TaskModel> models, Action<PurchaseOrder>? customise = null)
  988. {
  989. var orders = DoLoad<PurchaseOrderKanban, PurchaseOrder, PurchaseOrderLink>(models, GetGrid<PurchaseOrder>().LoadEditorColumns());
  990. if (orders.Any())
  991. return DoEdit(orders, customise);
  992. return false;
  993. }
  994. #endregion
  995. #region EntityReferences
  996. private static void AddQuery<TEntityKanban, TEntity, TLink>(MultiQuery query, Guid[] taskids)
  997. where TEntityKanban : EntityKanban<TEntity, TLink>, new()
  998. where TEntity : Entity
  999. where TLink : IEntityLink<TEntity>, new()
  1000. {
  1001. query.Add(
  1002. new Filter<TEntityKanban>(x => x.Kanban.ID).InList(taskids),
  1003. new Columns<TEntityKanban>(x => x.Entity.ID).Add(x => x.Kanban.ID)
  1004. );
  1005. }
  1006. private static Guid[] ExtractIDs<TEntityKanban, TEntity, TLink>(MultiQuery query)
  1007. where TEntityKanban : EntityKanban<TEntity, TLink>, new()
  1008. where TEntity : Entity
  1009. where TLink : IEntityLink<TEntity>, new()
  1010. {
  1011. var lookup = query.Get<TEntityKanban>().ToLookup<TEntityKanban, Guid, Guid>(x => x.Kanban.ID, x => x.Entity.ID);
  1012. return query.Get<TEntityKanban>().ExtractValues<TEntityKanban, Guid>(x => x.Entity.ID).ToArray();
  1013. }
  1014. public KanbanReferences[] GetReferences(IEnumerable<TaskModel> models)
  1015. {
  1016. var result = new List<KanbanReferences>();
  1017. var ids = models.Select(x => Guid.Parse(x.ID)).ToArray();
  1018. var query = new MultiQuery();
  1019. AddQuery<RequisitionKanban, Requisition, RequisitionLink>(query, ids);
  1020. AddQuery<SetoutKanban, Setout, SetoutLink>(query, ids);
  1021. AddQuery<DeliveryKanban, Delivery, DeliveryLink>(query, ids);
  1022. AddQuery<PurchaseOrderKanban, PurchaseOrder, PurchaseOrderLink>(query, ids);
  1023. query.Query();
  1024. var requis = query.Get<RequisitionKanban>().ToLookup<RequisitionKanban, Guid, Guid>(x => x.Kanban.ID, x => x.Entity.ID);
  1025. var setouts = query.Get<SetoutKanban>().ToLookup<SetoutKanban, Guid, Guid>(x => x.Kanban.ID, x => x.Entity.ID);
  1026. var deliveries = query.Get<DeliveryKanban>().ToLookup<DeliveryKanban, Guid, Guid>(x => x.Kanban.ID, x => x.Entity.ID);
  1027. var orders = query.Get<PurchaseOrderKanban>().ToLookup<PurchaseOrderKanban, Guid, Guid>(x => x.Kanban.ID, x => x.Entity.ID);
  1028. foreach (var id in ids)
  1029. {
  1030. var references = new KanbanReferences
  1031. {
  1032. Kanban = id,
  1033. Requisitions = requis.Contains(id) ? requis[id].ToArray() : Array.Empty<Guid>(),
  1034. Setouts = setouts.Contains(id) ? setouts[id].ToArray() : Array.Empty<Guid>(),
  1035. Deliveries = deliveries.Contains(id) ? deliveries[id].ToArray() : Array.Empty<Guid>(),
  1036. Orders = orders.Contains(id) ? orders[id].ToArray() : Array.Empty<Guid>()
  1037. };
  1038. result.Add(references);
  1039. }
  1040. return result.ToArray();
  1041. }
  1042. public bool EditReferences(IEnumerable<TaskModel> models)
  1043. {
  1044. var result = false;
  1045. var refs = GetReferences(models).First();
  1046. if (refs.ReferenceType() == typeof(Requisition))
  1047. result = EditRequisitions(
  1048. models,
  1049. requi =>
  1050. {
  1051. requi.Notes = Utility.ProcessNotes(requi.Notes, requi.Request);
  1052. requi.Request = "";
  1053. }
  1054. );
  1055. else if (refs.ReferenceType() == typeof(Setout))
  1056. result = EditSetouts(models);
  1057. else if (refs.ReferenceType() == typeof(Delivery))
  1058. result = EditDeliveries(models);
  1059. else if (refs.ReferenceType() == typeof(PurchaseOrder))
  1060. result = EditPurchaseOrders(models);
  1061. else
  1062. result = EditKanbans(models);
  1063. return result;
  1064. }
  1065. #endregion
  1066. }
  1067. }