Calendar.xaml.cs 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Threading;
  11. using Comal.Classes;
  12. using InABox.Clients;
  13. using InABox.Configuration;
  14. using InABox.Core;
  15. using InABox.DynamicGrid;
  16. using InABox.WPF;
  17. using Org.BouncyCastle.Asn1.X509.Qualified;
  18. using PRS.Shared;
  19. using Syncfusion.SfSkinManager;
  20. using Syncfusion.UI.Xaml.Scheduler;
  21. using Selection = InABox.Core.Selection;
  22. using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
  23. namespace PRSDesktop
  24. {
  25. public class SfScheduler2 : SfScheduler
  26. {
  27. public static readonly DependencyProperty HeaderVisibilityProperty =
  28. DependencyProperty.Register(
  29. nameof(HeaderVisibility),
  30. typeof(Visibility),
  31. typeof(SfScheduler2),
  32. new UIPropertyMetadata(Visibility.Collapsed)
  33. );
  34. public Visibility HeaderVisibility
  35. {
  36. get => (Visibility)GetValue(HeaderVisibilityProperty);
  37. set
  38. {
  39. SetValue(HeaderVisibilityProperty,value);
  40. SetHeaderVisibility();
  41. }
  42. }
  43. private void SetHeaderVisibility()
  44. {
  45. if (GetTemplateChild("PART_ScheduleHeaderControl") is SchedulerHeaderControl cell)
  46. cell.Visibility = HeaderVisibility;
  47. }
  48. public override void OnApplyTemplate()
  49. {
  50. base.OnApplyTemplate();
  51. SetHeaderVisibility();
  52. }
  53. }
  54. public partial class Calendar
  55. {
  56. private enum Suppress
  57. {
  58. Selector, // Prevent the Selector from Being Changed
  59. Calendar, // Prevent the Calendar from Being Reconfigured
  60. Events, // Prevent the Selectors from Responding to Events
  61. Refresh, // Stop the Data from Being refreshed
  62. Settings // Dont allow settings to be updated
  63. }
  64. private EventSuppressor suppressor = null;
  65. public void DisableUpdate()
  66. {
  67. if (suppressor == null)
  68. suppressor = new EventSuppressor(Suppress.Refresh, Suppress.Settings);
  69. }
  70. public void EnableUpdate()
  71. {
  72. if (suppressor != null)
  73. {
  74. suppressor.Dispose();
  75. suppressor = null;
  76. }
  77. }
  78. private void DoSetValue<T>(DependencyProperty property, T value, Action? updateselector, Action? updateinterface)
  79. {
  80. SetValue(property, value);
  81. if (!EventSuppressor.IsSet(Suppress.Selector) && (updateselector != null))
  82. using (new EventSuppressor(Suppress.Events))
  83. updateselector();
  84. if (!EventSuppressor.IsSet(Suppress.Calendar) && (updateinterface != null))
  85. updateinterface();
  86. if (!EventSuppressor.IsSet(Suppress.Refresh))
  87. Refresh();
  88. if (!EventSuppressor.IsSet(Suppress.Settings))
  89. {
  90. Properties.SettingsVisible = SettingsVisible;
  91. Properties.Date = SelectedDate;
  92. Properties.StartHour = (int)Bookings.DaysViewSettings.StartHour;
  93. Properties.EndHour = (int)Bookings.DaysViewSettings.EndHour;
  94. Properties.CalendarView = CalendarView;
  95. Properties.EmployeeSelector = EmployeeSettings;
  96. Properties.EmployeeSelection = EmployeeSelection;
  97. Properties.TimeInterval = TimeInterval;
  98. Properties.AssignmentType = AssignmentType;
  99. Properties.BackgroundType = BackgroundType;
  100. Properties.Zoom = Zoom;
  101. SaveSettings?.Invoke(this, Properties);
  102. }
  103. }
  104. #region HeaderVisibility Dependency Property
  105. public static readonly DependencyProperty HeaderVisibilityProperty =
  106. DependencyProperty.Register(
  107. nameof(HeaderVisibility),
  108. typeof(Visibility),
  109. typeof(Calendar),
  110. new UIPropertyMetadata(Visibility.Collapsed)
  111. );
  112. public Visibility HeaderVisibility
  113. {
  114. get => (Visibility)GetValue(HeaderVisibilityProperty);
  115. set
  116. {
  117. SetValue(HeaderVisibilityProperty,value);
  118. Bookings.HeaderVisibility = value;
  119. }
  120. }
  121. #endregion
  122. #region SettingsVisible Dependency Property
  123. public static readonly DependencyProperty SettingsVisibleProperty =
  124. DependencyProperty.Register(
  125. nameof(SettingsVisible),
  126. typeof(CalendarSettingsVisibility),
  127. typeof(Calendar),
  128. new UIPropertyMetadata(CalendarSettingsVisibility.Disabled)
  129. );
  130. public CalendarSettingsVisibility SettingsVisible
  131. {
  132. get => (CalendarSettingsVisibility)GetValue(SettingsVisibleProperty);
  133. set => SetSettingsVisibility(value);
  134. }
  135. private void SetSettingsVisibility(CalendarSettingsVisibility value)
  136. {
  137. DoSetValue(
  138. SettingsVisibleProperty,
  139. value,
  140. () => { },
  141. () =>
  142. {
  143. _splitPanel.View = value == CalendarSettingsVisibility.Visible
  144. ? DynamicSplitPanelView.Combined
  145. : DynamicSplitPanelView.Master;
  146. _splitPanel.AllowableViews = value == CalendarSettingsVisibility.Disabled
  147. ? DynamicSplitPanelView.Master
  148. : DynamicSplitPanelView.Master | DynamicSplitPanelView.Combined;
  149. ResizeColumns(this.ActualWidth);
  150. }
  151. );
  152. }
  153. #endregion
  154. #region CalendarView Dependency Property
  155. public static readonly DependencyProperty CalendarViewProperty =
  156. DependencyProperty.Register(
  157. nameof(CalendarView),
  158. typeof(CalendarViewType),
  159. typeof(Calendar),
  160. new UIPropertyMetadata(CalendarViewType.Day)
  161. );
  162. public CalendarViewType CalendarView
  163. {
  164. get => (CalendarViewType)GetValue(CalendarViewProperty);
  165. set => SetCalendarView(value);
  166. }
  167. private void SetCalendarView(CalendarViewType value)
  168. {
  169. DoSetValue(
  170. CalendarViewProperty,
  171. value,
  172. () => CalendarViewSelector.SelectedIndex = (int)value,
  173. () =>
  174. {
  175. Bookings.ViewType = value switch
  176. {
  177. CalendarViewType.Day => SchedulerViewType.Day,
  178. CalendarViewType.WorkWeek => SchedulerViewType.WorkWeek,
  179. CalendarViewType.Week => SchedulerViewType.Week,
  180. _ => SchedulerViewType.Day
  181. };
  182. ResizeColumns(this.ActualWidth);
  183. }
  184. );
  185. }
  186. private void CalendarViewSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
  187. {
  188. if (EventSuppressor.IsSet(Suppress.Events))
  189. return;
  190. using (new EventSuppressor(Suppress.Selector))
  191. SetCalendarView((CalendarViewType)CalendarViewSelector.SelectedIndex);
  192. }
  193. #endregion
  194. #region EmployeeSelector Dependency Property
  195. public static readonly DependencyProperty EmployeeSelectionProperty =
  196. DependencyProperty.Register(
  197. nameof(EmployeeSelection),
  198. typeof(EmployeeSelectorData),
  199. typeof(Calendar),
  200. new UIPropertyMetadata(new EmployeeSelectorData())
  201. );
  202. public EmployeeSelectorData EmployeeSelection
  203. {
  204. get => (EmployeeSelectorData)GetValue(EmployeeSelectionProperty);
  205. set => SetEmployeeSelection(value);
  206. }
  207. private void SetEmployeeSelection(EmployeeSelectorData value)
  208. {
  209. DoSetValue(
  210. EmployeeSelectionProperty,
  211. value,
  212. () => EmployeeSelector.Selection = value,
  213. () =>
  214. {
  215. _employees = EmployeeSelector.GetEmployeeData((row, rosters) => new EmployeeResourceModel(row, rosters));
  216. ReloadColumns();
  217. }
  218. );
  219. }
  220. private void EmployeeSelector_OnSelectionChanged(object sender, EmployeeSelectorSelectionChangedArgs args)
  221. {
  222. if (EventSuppressor.IsSet(Suppress.Events))
  223. return;
  224. using (new EventSuppressor(Suppress.Selector))
  225. SetEmployeeSelection(args.Selection);
  226. }
  227. #endregion
  228. #region EmployeeSettings Dependency Property
  229. public static readonly DependencyProperty EmployeeSettingsProperty =
  230. DependencyProperty.Register(
  231. nameof(EmployeeSettings),
  232. typeof(EmployeeSelectorSettings),
  233. typeof(Calendar),
  234. new UIPropertyMetadata(new EmployeeSelectorSettings())
  235. );
  236. public EmployeeSelectorSettings EmployeeSettings
  237. {
  238. get => (EmployeeSelectorSettings)GetValue(EmployeeSettingsProperty);
  239. set => SetEmployeeSettings(value);
  240. }
  241. private void SetEmployeeSettings(EmployeeSelectorSettings value)
  242. {
  243. DoSetValue(
  244. EmployeeSettingsProperty,
  245. value,
  246. () => EmployeeSelector.Settings = value,
  247. () =>
  248. {
  249. // Nothing to do here
  250. }
  251. );
  252. }
  253. private void EmployeeSelector_OnSettingsChanged(object sender, EmployeeSelectorSettingsChangedArgs args)
  254. {
  255. if (EventSuppressor.IsSet(Suppress.Events))
  256. return;
  257. using (new EventSuppressor(Suppress.Selector))
  258. SetEmployeeSettings(args.Settings);
  259. }
  260. #endregion
  261. #region TimeInterval DependencyProperty
  262. public static readonly DependencyProperty TimeIntervalProperty =
  263. DependencyProperty.Register(
  264. "TimeInterval",
  265. typeof(CalendarTimeInterval),
  266. typeof(Calendar),
  267. new PropertyMetadata(CalendarTimeInterval.FifteenMinutes)
  268. );
  269. public CalendarTimeInterval TimeInterval
  270. {
  271. get => (CalendarTimeInterval)GetValue(TimeIntervalProperty);
  272. set => SetTimeInterval(value);
  273. }
  274. private void SetTimeInterval(CalendarTimeInterval value)
  275. {
  276. DoSetValue(
  277. TimeIntervalProperty,
  278. value,
  279. () => IntervalSelector.SelectedIndex = (int)value,
  280. () =>
  281. {
  282. Bookings.DaysViewSettings.TimeInterval = TimeIntervalToTimeSpan(value);
  283. UpdateZoom();
  284. });
  285. }
  286. private void IntervalSelector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  287. {
  288. if (EventSuppressor.IsSet(Suppress.Events))
  289. return;
  290. using (new EventSuppressor(Suppress.Selector))
  291. SetTimeInterval((CalendarTimeInterval)IntervalSelector.SelectedIndex);
  292. }
  293. public TimeSpan TimeIntervalToTimeSpan(CalendarTimeInterval interval)
  294. {
  295. return interval switch
  296. {
  297. CalendarTimeInterval.FiveMinutes => new TimeSpan(0, 5, 0),
  298. CalendarTimeInterval.SixMinutes => new TimeSpan(0, 6, 0),
  299. CalendarTimeInterval.TenMinutes => new TimeSpan(0, 10, 0),
  300. CalendarTimeInterval.FifteenMinutes => new TimeSpan(0, 15, 0),
  301. CalendarTimeInterval.TwentyMinutes => new TimeSpan(0, 20, 0),
  302. CalendarTimeInterval.ThirtyMinutes => new TimeSpan(0, 30, 0),
  303. _ => new TimeSpan(1, 0, 0)
  304. };
  305. }
  306. public int BlocksPerHour(CalendarTimeInterval interval)
  307. {
  308. return interval switch
  309. {
  310. CalendarTimeInterval.FiveMinutes => 12,
  311. CalendarTimeInterval.SixMinutes => 10,
  312. CalendarTimeInterval.TenMinutes => 6,
  313. CalendarTimeInterval.FifteenMinutes => 4,
  314. CalendarTimeInterval.TwentyMinutes => 3,
  315. CalendarTimeInterval.ThirtyMinutes => 2,
  316. _ => 1
  317. };
  318. }
  319. private TimeSpan AdjustStartTime(TimeSpan time)
  320. {
  321. long blocksize = TimeIntervalToTimeSpan(TimeInterval).Ticks;
  322. //long blocksperday = TimeSpan.FromDays(1).Ticks / ;
  323. long blocknumber = time.Ticks / blocksize;
  324. long blockstart = blocknumber * blocksize;
  325. return TimeSpan.FromTicks(blockstart);
  326. }
  327. #endregion
  328. #region SelectedDate Dependency Property
  329. public static readonly DependencyProperty SelectedDateProperty =
  330. DependencyProperty.Register(
  331. nameof(SelectedDate),
  332. typeof(DateTime),
  333. typeof(Calendar),
  334. new UIPropertyMetadata(DateTime.Today)
  335. );
  336. public DateTime SelectedDate
  337. {
  338. get => (DateTime)GetValue(SelectedDateProperty);
  339. set => SetSelectedDate(value);
  340. }
  341. private void SetSelectedDate(DateTime value)
  342. {
  343. DoSetValue(
  344. SelectedDateProperty,
  345. value,
  346. () => { /* DateSelector.Date = value; */ },
  347. () => {
  348. Bookings.DisplayDate = value;
  349. Bookings.SelectedDate = value;
  350. });
  351. }
  352. // private void DateSelector_DateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  353. // {
  354. // if (EventSuppressor.IsSet(Suppress.Events))
  355. // return;
  356. // using (new EventSuppressor(Suppress.Selector))
  357. // SetSelectedDate(DateSelector.Date);
  358. // }
  359. public DateTime StartDate => (Properties?.CalendarView ?? CalendarViewType.Day) == CalendarViewType.Day
  360. ? SelectedDate
  361. : SelectedDate.StartOfWeek(DayOfWeek.Monday);
  362. public DateTime EndDate => Properties.CalendarView == CalendarViewType.Day
  363. ? StartDate.AddDays(1)
  364. : Properties.CalendarView == CalendarViewType.WorkWeek
  365. ? StartDate.AddDays(5)
  366. : StartDate.AddDays(7);
  367. #endregion
  368. #region StartHour Dependency Properties
  369. public static readonly DependencyProperty StartHourProperty =
  370. DependencyProperty.Register(
  371. nameof(StartHour),
  372. typeof(int),
  373. typeof(Calendar),
  374. new UIPropertyMetadata(6)
  375. );
  376. public int StartHour
  377. {
  378. get => (int)GetValue(StartHourProperty);
  379. set => SetStartHour(value);
  380. }
  381. private void SetStartHour(int value)
  382. {
  383. value = Math.Min(EndHour-1,Math.Max(0, value));
  384. DoSetValue(
  385. StartHourProperty,
  386. value,
  387. () => StartTimeSelector.Text = FormatHour(value),
  388. () =>
  389. {
  390. Bookings.DaysViewSettings.StartHour = value;
  391. UpdateZoom();
  392. });
  393. }
  394. private void StartTimeSelector_Down_Click(object sender, RoutedEventArgs e)
  395. {
  396. if (EventSuppressor.IsSet(Suppress.Events))
  397. return;
  398. SetStartHour(StartHour - 1);
  399. }
  400. private void StartTimeSelector_Up_Click(object sender, RoutedEventArgs e)
  401. {
  402. if (EventSuppressor.IsSet(Suppress.Events))
  403. return;
  404. SetStartHour(StartHour + 1);
  405. }
  406. #endregion
  407. #region End Hour Property
  408. public static readonly DependencyProperty EndHourProperty =
  409. DependencyProperty.Register(
  410. nameof(EndHour),
  411. typeof(int),
  412. typeof(Calendar),
  413. new UIPropertyMetadata(18)
  414. );
  415. public int EndHour
  416. {
  417. get => (int)GetValue(EndHourProperty);
  418. set => SetEndHour(value);
  419. }
  420. private void SetEndHour(int value)
  421. {
  422. value = Math.Max(StartHour + 1, Math.Min(24, value));
  423. DoSetValue(
  424. EndHourProperty,
  425. value,
  426. () => FinishTimeSelector.Text = FormatHour(value),
  427. () =>
  428. {
  429. Bookings.DaysViewSettings.EndHour = value;
  430. UpdateZoom();
  431. });
  432. }
  433. private void FinishTimeSelector_Down_Click(object sender, RoutedEventArgs e)
  434. {
  435. if (EventSuppressor.IsSet(Suppress.Events))
  436. return;
  437. SetEndHour(EndHour - 1);
  438. }
  439. private void FinishTimeSelector_Up_Click(object sender, RoutedEventArgs e)
  440. {
  441. if (EventSuppressor.IsSet(Suppress.Events))
  442. return;
  443. SetEndHour(EndHour + 1);
  444. }
  445. private string FormatHour(int hour)
  446. {
  447. return hour <= 0 || hour >= 24
  448. ? "Midnight"
  449. : hour < 12
  450. ? string.Format("{0}:00 AM", hour)
  451. : hour > 12
  452. ? string.Format("{0}:00 PM", hour)
  453. : "12:00 NN";
  454. }
  455. #endregion
  456. #region AssignmentType Dependency Property
  457. public static readonly DependencyProperty AssignmentTypeProperty =
  458. DependencyProperty.Register(
  459. nameof(AssignmentType),
  460. typeof(CalendarAssignmentType),
  461. typeof(Calendar),
  462. new UIPropertyMetadata(CalendarAssignmentType.Booked)
  463. );
  464. public CalendarAssignmentType AssignmentType
  465. {
  466. get => (CalendarAssignmentType)GetValue(AssignmentTypeProperty);
  467. set => SetAssignmentType(value);
  468. }
  469. private void SetAssignmentType(CalendarAssignmentType value)
  470. {
  471. DoSetValue(
  472. AssignmentTypeProperty,
  473. value,
  474. () => AssignmentTypeSelector.SelectedIndex = (int)value,
  475. null
  476. );
  477. }
  478. private void AssignmentTypeSelector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  479. {
  480. if (EventSuppressor.IsSet(Suppress.Events))
  481. return;
  482. using (new EventSuppressor(Suppress.Selector))
  483. SetAssignmentType((CalendarAssignmentType)AssignmentTypeSelector.SelectedIndex);
  484. }
  485. #endregion
  486. #region BackgroundType Dependency Property
  487. public static readonly DependencyProperty BackgroundTypeProperty =
  488. DependencyProperty.Register(
  489. nameof(BackgroundType),
  490. typeof(CalendarBackgroundType),
  491. typeof(Calendar),
  492. new UIPropertyMetadata(CalendarBackgroundType.Roster)
  493. );
  494. public CalendarBackgroundType BackgroundType
  495. {
  496. get => (CalendarBackgroundType)GetValue(BackgroundTypeProperty);
  497. set => SetBackgroundType(value);
  498. }
  499. private void SetBackgroundType(CalendarBackgroundType type)
  500. {
  501. DoSetValue(
  502. BackgroundTypeProperty,
  503. type,
  504. () => BackgroundTypeSelector.SelectedIndex = (int)type,
  505. null
  506. );
  507. }
  508. private void BackgroundTypeSelector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  509. {
  510. if (EventSuppressor.IsSet(Suppress.Events))
  511. return;
  512. using (new EventSuppressor(Suppress.Selector))
  513. SetBackgroundType((CalendarBackgroundType)BackgroundTypeSelector.SelectedIndex);
  514. }
  515. #endregion
  516. #region Zoom Dependency Properties
  517. public static readonly DependencyProperty ZoomProperty =
  518. DependencyProperty.Register(
  519. nameof(Zoom),
  520. typeof(double),
  521. typeof(Calendar),
  522. new UIPropertyMetadata((double)100F)
  523. );
  524. public double Zoom
  525. {
  526. get => (double)GetValue(ZoomProperty);
  527. set => SetZoom(value);
  528. }
  529. private void SetZoom(double value)
  530. {
  531. DoSetValue(
  532. ZoomProperty,
  533. value,
  534. () => ZoomSelector.Text = $"{value:F0}%",
  535. () => UpdateZoom()
  536. );
  537. }
  538. private void UpdateZoom()
  539. {
  540. if (double.IsNaN(this.ActualHeight) || (this.ActualHeight == 0.0F))
  541. return;
  542. var blocksize = (this.ActualHeight - (Bookings.DaysViewSettings.ViewHeaderHeight + Bookings.DaysViewSettings.ResourceHeaderSize + 2.0F)) / ((EndHour - StartHour) * this.BlocksPerHour(TimeInterval));
  543. Bookings.DaysViewSettings.TimeIntervalSize = (double)Zoom * blocksize / 100.0F;
  544. }
  545. private void ZoomSelector_Down_Click(object sender, RoutedEventArgs e)
  546. {
  547. if (EventSuppressor.IsSet(Suppress.Events))
  548. return;
  549. ZoomOut();
  550. }
  551. private void ZoomSelector_Up_Click(object sender, RoutedEventArgs e)
  552. {
  553. if (EventSuppressor.IsSet(Suppress.Events))
  554. return;
  555. ZoomIn();
  556. }
  557. public void ZoomIn() => SetZoom(Zoom * 1.125F);
  558. public void ZoomOut() => SetZoom(Zoom / 1.125F);
  559. public void ResetZoom() => SetZoom(100.0F);
  560. #endregion
  561. #region Event Handlers
  562. public event LoadSettings<CalendarSettings> LoadSettings;
  563. public event SaveSettings<CalendarSettings> SaveSettings;
  564. public CalendarConfigurationEvent ConfigurationChanged;
  565. public event CalendarDataEvent CustomiseContextMenu;
  566. public event CalendarDataEvent SelectionChanged;
  567. public event CalendarDataEvent ItemCreated;
  568. public event CalendarDataEvent ItemChanged;
  569. public event CalendarHandledEvent ItemEditing;
  570. #endregion
  571. public void SelectEmployee(Guid employeeid) => EmployeeSelector.SelectEmployee(employeeid);
  572. // Populated as requiew when EmployeeSelector.SelectionChanged is triggered
  573. private EmployeeResourceModel[] _employees = new EmployeeResourceModel[] { };
  574. // Populated once at startup
  575. private StandardLeaveModel[] _standardleaves = new StandardLeaveModel[] { };
  576. private LeaveRequestModel[] _leaverequests = new LeaveRequestModel[] { };
  577. // Populated on each Refresh
  578. private TimeSheetModel[] _timesheets = new TimeSheetModel[] { };
  579. // Populated on each Refresh
  580. private List<AssignmentModel> _assignments = new List<AssignmentModel>();
  581. private bool bColumnsLoaded;
  582. private AssignmentGrid ag;
  583. private DynamicDataGrid<Meeting> mg;
  584. public bool IsReady { get; set; }
  585. public CalendarSettings Properties { get; set; }
  586. //public SfScheduler2? Bookings => this.FindName("_bookings") as SfScheduler2;
  587. public Calendar()
  588. {
  589. using (EventSuppressor.All<Suppress>())
  590. {
  591. InitializeComponent();
  592. SetValue(StartHourProperty, 0);
  593. SetValue(EndHourProperty, 24);
  594. SfSkinManager.SetTheme(Bookings, new Theme() { ThemeName = "Office2019White", ScrollBarMode = ScrollBarMode.Compact });
  595. }
  596. }
  597. public virtual void Setup()
  598. {
  599. using (new EventSuppressor(Suppress.Settings, Suppress.Refresh, Suppress.Events))
  600. {
  601. Properties = LoadSettings?.Invoke(this) ?? new CalendarSettings();
  602. SetCalendarView(Properties.CalendarView);
  603. SetSettingsVisibility(Properties.SettingsVisible);
  604. SetSelectedDate(Properties.Date);
  605. SetStartHour(Properties.StartHour);
  606. SetEndHour(Properties.EndHour);
  607. SetTimeInterval(Properties.TimeInterval);
  608. SetAssignmentType(Properties.AssignmentType);
  609. SetBackgroundType(Properties.BackgroundType);
  610. SetCalendarView(Properties.CalendarView);
  611. SetZoom(Properties.Zoom);
  612. EmployeeSelector.Setup();
  613. SetEmployeeSettings(Properties.EmployeeSelector);
  614. SetEmployeeSelection(Properties.EmployeeSelection);
  615. _employees = EmployeeSelector.GetEmployeeData((row, rosters) => new EmployeeResourceModel(row, rosters));
  616. MultiQuery query = new MultiQuery();
  617. query.Add(
  618. new Filter<LeaveRequest>(x=>x.Status).IsNotEqualTo(LeaveRequestStatus.Rejected),
  619. LeaveRequestModel.Columns
  620. );
  621. query.Add(
  622. null,
  623. StandardLeaveModel.Columns
  624. );
  625. query.Query();
  626. _standardleaves = query.Get<StandardLeave>().Rows.Select(row => new StandardLeaveModel(row)).ToArray();
  627. _leaverequests = query.Get<LeaveRequest>().Rows.Select(row => new LeaveRequestModel(row)).ToArray();
  628. var widthtimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
  629. widthtimer.Tick += (o, e) =>
  630. {
  631. if (Bookings.ActualWidth > 0.0F)
  632. {
  633. widthtimer.IsEnabled = false;
  634. ReloadColumns();
  635. }
  636. };
  637. widthtimer.IsEnabled = true;
  638. }
  639. }
  640. public virtual void Shutdown(CancelEventArgs? cancel)
  641. {
  642. }
  643. private bool bFirst = true;
  644. public virtual void Refresh()
  645. {
  646. RefreshData(StartDate, EndDate);
  647. }
  648. private void RefreshData(DateTime startDate, DateTime endDate)
  649. {
  650. if (EventSuppressor.IsSet(Suppress.Refresh))
  651. return;
  652. using (new WaitCursor())
  653. {
  654. if (!bColumnsLoaded)
  655. ReloadColumns();
  656. MultiQuery query = new MultiQuery();
  657. var empids = _employees.Select(x => x.ID).ToArray();
  658. if (BackgroundType != CalendarBackgroundType.Roster)
  659. {
  660. query.Add<TimeSheet>(
  661. new Filter<TimeSheet>(x => x.EmployeeLink.ID).InList(empids)
  662. .And(x => x.Date).IsGreaterThanOrEqualTo(startDate)
  663. .And(x => x.Date).IsLessThanOrEqualTo(endDate)
  664. .And(x=>x.LeaveRequestLink.ID).IsEqualTo(Guid.Empty)
  665. .And(x=>x.StandardLeaveLink.ID).IsEqualTo(Guid.Empty),
  666. TimeSheetModel.Columns
  667. );
  668. }
  669. query.Add<Assignment>(
  670. new Filter<Assignment>(x => x.EmployeeLink.ID).InList(empids)
  671. .And(x => x.Date).IsGreaterThanOrEqualTo(startDate)
  672. .And(x => x.Date).IsLessThanOrEqualTo(endDate),
  673. AssignmentModel.Columns,
  674. new SortOrder<Assignment>(x => x.EmployeeLink.ID).ThenBy(x => x.Date).ThenBy(x => x.Booked.Duration, SortDirection.Descending)
  675. );
  676. query.Query();
  677. _timesheets = (BackgroundType == CalendarBackgroundType.Roster)
  678. ? new TimeSheetModel[] { }
  679. : query.Get<TimeSheet>().Rows.Select(r => new TimeSheetModel(r)).ToArray();
  680. _assignments = query.Get<Assignment>().Rows.Select(r => new AssignmentModel(r)).ToList();
  681. LoadBackground();
  682. var appointments = new CalendarAppointments();
  683. LoadStandardLeaves(appointments);
  684. LoadLeaveRequests(appointments);
  685. LoadAssignments(appointments);
  686. try
  687. {
  688. Bookings.DisplayDate = Bookings.SelectedDate.HasValue ? Bookings.SelectedDate.Value : StartDate;
  689. Bookings.ItemsSource = appointments;
  690. }
  691. catch (Exception e)
  692. {
  693. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  694. }
  695. bFirst = false;
  696. }
  697. }
  698. public EmployeeRosterItem GetRoster(Guid employeeid, DateTime date)
  699. {
  700. var employee = _employees.FirstOrDefault(x => x.ID == employeeid);
  701. var roster = RosterUtils.GetRoster(employee?.Roster, employee?.RosterStart, date);
  702. return roster;
  703. }
  704. public bool GetActiveWindow(Guid employeeid, DateTime date, ref TimeSpan start, ref TimeSpan finish)
  705. {
  706. bool result = false;
  707. foreach (var assignment in _assignments.Where(a => (a.EmployeeID == employeeid) && (a.Date) == date))
  708. {
  709. result = true;
  710. var curstart = AssignmentType switch
  711. {
  712. CalendarAssignmentType.Booked => assignment.BookedStart,
  713. CalendarAssignmentType.Actual => assignment.ActualStart,
  714. _ => Assignment.EffectiveTime(assignment.ActualStart, assignment.BookedStart)
  715. };
  716. var curfinish = AssignmentType switch
  717. {
  718. CalendarAssignmentType.Booked => assignment.BookedFinish,
  719. CalendarAssignmentType.Actual => assignment.ActualFinish,
  720. _ => Assignment.EffectiveTime(
  721. assignment.ActualFinish,
  722. Assignment.EffectiveTime(assignment.ActualStart, assignment.BookedStart)
  723. .Add(assignment.BookedDuration)
  724. )
  725. };
  726. start = start > curstart ? curstart : start;
  727. finish = finish < curfinish ? curfinish : finish;
  728. }
  729. if ((BackgroundType == CalendarBackgroundType.Roster) ||
  730. ((BackgroundType == CalendarBackgroundType.Automatic) && (date >= DateTime.Today)))
  731. {
  732. var employee = _employees.FirstOrDefault(x => x.ID == employeeid);
  733. if (employee != null)
  734. {
  735. var roster = RosterUtils.GetRoster(employee.Roster, employee.RosterStart, date);
  736. if (roster != null)
  737. {
  738. var blocks = roster.GetBlocks(date, TimeSpan.MinValue, TimeSpan.MaxValue);
  739. foreach (var block in blocks)
  740. {
  741. start = start > block.Start ? block.Start : start;
  742. finish = finish < block.Finish ? block.Finish : finish;
  743. }
  744. }
  745. }
  746. }
  747. else
  748. {
  749. foreach (var timesheet in _timesheets.Where(t => (t.EmployeeID == employeeid) && (t.Date == date)))
  750. {
  751. result = true;
  752. var curstart = !timesheet.Approved.IsEmpty()
  753. ? timesheet.ApprovedStart
  754. : timesheet.Start;
  755. var curfinish = !timesheet.Approved.IsEmpty()
  756. ? timesheet.ApprovedFinish
  757. : timesheet.Finish;
  758. start = start > curstart ? curstart : start;
  759. finish = finish < curfinish ? curfinish : finish;
  760. }
  761. }
  762. return result;
  763. }
  764. public CoreRow[] GetAssignments(Guid employeeid, DateTime date)
  765. {
  766. return _assignments.Where(a => (a.Row != null) && (a.EmployeeID == employeeid) && (a.Date == date)).Select(x => x.Row!).ToArray();
  767. }
  768. public void UpdateAssignment(Assignment assignment)
  769. {
  770. void UpdateCalendar<TEntity, TModel>(
  771. TEntity entity,
  772. List<TModel> collection,
  773. Action<TModel> refresh
  774. ) where TEntity : Entity where TModel : class, IModel
  775. {
  776. if (Bookings.ItemsSource is CalendarAppointments appointments)
  777. {
  778. var appointment = appointments.FirstOrDefault(x => (Guid)x.Id == entity.ID) as ScheduleAppointment;
  779. if (appointment != null)
  780. {
  781. appointments.Remove(appointment);
  782. var calappt = appointment as ICalendarAppointment<TModel>;
  783. if (calappt != null)
  784. collection.Remove(calappt.Model);
  785. }
  786. var table = new CoreTable();
  787. table.LoadColumns(typeof(TEntity));
  788. var row = table.NewRow();
  789. table.LoadRow(row, entity);
  790. var model = (Activator.CreateInstance(typeof(TModel),row) as TModel)!;
  791. collection.Add(model);
  792. refresh(model);
  793. }
  794. }
  795. if (Bookings.ItemsSource is CalendarAppointments appointments)
  796. UpdateCalendar(assignment, _assignments, (model) => LoadAssignment(model,appointments));
  797. }
  798. private void LoadBackground()
  799. {
  800. var regions = new ObservableCollection<SpecialTimeRegion>();
  801. foreach (var resource in Bookings.ResourceCollection)
  802. {
  803. var sEmpID = (((SchedulerResource)resource).Id as string) ?? "";
  804. var empid = Guid.Parse(sEmpID);
  805. var employee = _employees.FirstOrDefault(x => x.ID == empid);
  806. for (var date = StartDate; date < EndDate; date = date.AddDays(1))
  807. {
  808. if ((BackgroundType == CalendarBackgroundType.Roster) ||
  809. ((BackgroundType == CalendarBackgroundType.Automatic) && (date >= DateTime.Today)))
  810. {
  811. var roster = RosterUtils.GetRoster(employee?.Roster, employee?.RosterStart, date);
  812. if (roster != null)
  813. {
  814. var blocks = roster.GetBlocks(date, TimeSpan.FromSeconds(0), TimeSpan.FromDays(1));
  815. foreach (var block in blocks)
  816. {
  817. regions.Add(
  818. new SpecialTimeRegion
  819. {
  820. StartTime = date.Add(block.Start),
  821. EndTime = date.Add(block.Finish.Subtract(TimeSpan.FromSeconds(1))),
  822. ResourceIdCollection = new ObservableCollection<object> { ((SchedulerResource)resource).Id },
  823. Background = new SolidColorBrush(Colors.Yellow) {Opacity = 0.3},
  824. Foreground = new SolidColorBrush(Colors.Black),
  825. Text = ""
  826. }
  827. );
  828. }
  829. }
  830. }
  831. else
  832. {
  833. foreach (var timesheet in _timesheets.Where(t => (t.EmployeeID == empid) && (t.Date == date)))
  834. {
  835. var start = !timesheet.Approved.IsEmpty()
  836. ? timesheet.ApprovedStart
  837. : timesheet.Start;
  838. var finish = !timesheet.Approved.IsEmpty()
  839. ? timesheet.ApprovedFinish
  840. : timesheet.Finish;
  841. regions.Add(
  842. new SpecialTimeRegion
  843. {
  844. StartTime = date.Add(start),
  845. EndTime = date.Add(finish),
  846. ResourceIdCollection = new ObservableCollection<object> { ((SchedulerResource)resource).Id },
  847. Background = new SolidColorBrush(!timesheet.Approved.IsEmpty() ? Colors.LightGreen : Colors.LightSalmon) { Opacity = 0.4 },
  848. Foreground = new SolidColorBrush(Colors.Transparent),
  849. Text = "",
  850. CanMergeAdjacentRegions = false
  851. }
  852. );
  853. }
  854. }
  855. }
  856. }
  857. Bookings.DaysViewSettings.SpecialTimeRegions = regions;
  858. }
  859. private void LoadStandardLeaves(CalendarAppointments appointments)
  860. {
  861. for (var date = StartDate; date < EndDate; date = date.AddDays(1))
  862. {
  863. var leaves = _standardleaves.Where(x =>
  864. (x.From <= date)
  865. && (x.To.Add(x.ToTime) > date)
  866. ).ToArray();
  867. foreach (var leave in leaves)
  868. {
  869. foreach (var resource in Bookings.ResourceCollection)
  870. {
  871. var sEmpID = ((SchedulerResource)resource).Id as string;
  872. var empid = Guid.Parse(sEmpID);
  873. var employee = _employees.FirstOrDefault(x => x.ID == empid);
  874. var start = (date.Date == leave.From.Date) ? leave.FromTime : TimeSpan.FromSeconds(0);
  875. var finish = (date.Date == leave.To.Date) ? leave.ToTime : TimeSpan.FromDays(1).Subtract(TimeSpan.FromSeconds(1));
  876. var roster = RosterUtils.GetRoster(employee?.Roster, employee?.RosterStart, date);
  877. if (roster != null)
  878. {
  879. var blocks = roster.GetBlocks(date, start, finish);
  880. foreach (var block in blocks)
  881. {
  882. var appt = new StandardLeaveAppointment(leave, _ => empid, x => x.Color, block);
  883. appointments.Add(appt);
  884. }
  885. }
  886. }
  887. }
  888. }
  889. }
  890. private void LoadLeaveRequests(CalendarAppointments appointments)
  891. {
  892. for (var date = StartDate; date < EndDate; date = date.AddDays(1))
  893. {
  894. var ids = Bookings.ResourceCollection
  895. .Cast<SchedulerResource>()
  896. .Where(x=>!String.IsNullOrWhiteSpace(x.Id as String))
  897. .Select(x => Guid.Parse(x.Id.ToString() ?? ""));
  898. var leaves = _leaverequests.Where(x =>
  899. (x.From <= date)
  900. && (x.To.Add(x.ToTime) > date)
  901. && ids.Contains(x.EmployeeID)
  902. ).ToArray();
  903. foreach (var leave in leaves)
  904. {
  905. var employee = _employees.FirstOrDefault(x => x.ID == leave.EmployeeID);
  906. var roster = RosterUtils.GetRoster(employee?.Roster, employee?.RosterStart, date);
  907. if (roster != null)
  908. {
  909. var start = (date.Date == leave.From.Date) ? leave.FromTime : TimeSpan.FromSeconds(0);
  910. var finish = (date.Date == leave.To.Date) ? leave.ToTime : TimeSpan.FromDays(1).Subtract(TimeSpan.FromSeconds(1));
  911. var blocks = roster.GetBlocks(date, start, finish);
  912. foreach (var block in blocks)
  913. {
  914. var appt = new LeaveRequestAppointment(leave, x=>x.EmployeeID, x => x.Color, block);
  915. appointments.Add(appt);
  916. }
  917. }
  918. }
  919. }
  920. }
  921. private void LoadAssignment(AssignmentModel assignment, CalendarAppointments appointments)
  922. {
  923. var model = new AssignmentAppointment(assignment, x => x.EmployeeID, x => x.Color, AssignmentType);
  924. appointments.Add(model);
  925. }
  926. private void LoadAssignments(CalendarAppointments appointments)
  927. {
  928. foreach (var assignment in _assignments)
  929. LoadAssignment(assignment, appointments);
  930. }
  931. public DataModel DataModel(Selection selection)
  932. {
  933. var ids = _assignments.Select(x => x.ID).ToArray();
  934. return new AutoDataModel<Assignment>(new Filter<Assignment>(x => x.ID).InList(ids));
  935. }
  936. // private void ResizeIntervals(double height)
  937. // {
  938. // if (Bookings.FindVisualChildren<ScrollPanel>().Any())
  939. // {
  940. //
  941. // if (height > 95 && Bookings.DaysViewSettings.EndHour - Bookings.DaysViewSettings.StartHour > 0)
  942. // {
  943. // double scrollheight = _employees.Length * 75 > Bookings.ActualWidth ? 15.0F : 0.0F;
  944. // var header = _employees.Length > 1 ? 93.0F + scrollheight : 50.0F;
  945. // Bookings.DaysViewSettings.TimeIntervalSize =
  946. // (height - header) / ((Bookings.DaysViewSettings.EndHour - Bookings.DaysViewSettings.StartHour) * 2.0F);
  947. // }
  948. // }
  949. //
  950. // // var scrollers = Bookings.FindVisualChildren<ScrollViewer>().Where(x => string.Equals(x.Name, "PART_TimeSlotScrollViewer")).ToArray();
  951. // // foreach (var scroll in scrollers)
  952. // // scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
  953. //
  954. // }
  955. private void Schedule_SizeChanged(object sender, SizeChangedEventArgs e)
  956. {
  957. //ResizeIntervals(e.NewSize.Height);
  958. ResizeColumns(e.NewSize.Width);
  959. }
  960. private void ResizeColumns(double width)
  961. {
  962. if (double.IsNaN(width) || !Bookings.FindVisualChildren<ScrollPanel>().Any())
  963. return;
  964. var maxcount = (int)width / 75;
  965. Bookings.DaysViewSettings.VisibleResourceCount = Math.Min(maxcount,
  966. _employees.Length * (Bookings.ViewType == SchedulerViewType.Day ? 1 : Bookings.ViewType == SchedulerViewType.WorkWeek ? 5 : 7));
  967. if (Bookings.ResourceCollection is ObservableCollection<SchedulerResource> resources)
  968. {
  969. var colwidth = GetResourceColumnWidth();
  970. foreach (var emp in _employees)
  971. {
  972. var resource = resources.FirstOrDefault(x => String.Equals(x.Id?.ToString(), emp.ID.ToString()));
  973. if (resource != null)
  974. {
  975. var comps = emp.Name.Split(' ');
  976. var display = emp.Name;
  977. if (colwidth < 75)
  978. display = string.Format("{0}{1}", comps[0].Length > 0 ? comps[0][..1] : "",
  979. comps.Length > 1 ? comps.Skip(1).First()[..1].ToUpper() : "");
  980. else if (colwidth < 150)
  981. display = string.Format("{0} {1}", comps.First(),
  982. comps.Length > 1 ? comps.Skip(1).First()[..1].ToUpper() : "");
  983. resource.Name = display;
  984. }
  985. }
  986. }
  987. }
  988. private T CheckGrid<T>(ref T grid) where T : new()
  989. {
  990. if (grid == null)
  991. grid = new T();
  992. return grid;
  993. }
  994. private void ReloadColumns()
  995. {
  996. ResizeColumns(Bookings.ActualWidth);
  997. var colwidth = GetResourceColumnWidth();
  998. var resources = new List<SchedulerResource>();
  999. foreach (var emp in _employees)
  1000. {
  1001. var comps = emp.Name.Split(' ');
  1002. var display = emp.Name;
  1003. if (colwidth < 75)
  1004. display = CoreUtils.Codify(emp.Name);
  1005. else if (colwidth < 150)
  1006. display = string.Format("{0} {1}", comps.Length > 0 ? comps.First() : "", comps.Length > 1 ? comps.Skip(1).First().Substring(0, 1).ToUpper() : "");
  1007. resources.Add(new SchedulerResource { Name = display, Id = emp.ID.ToString() });
  1008. }
  1009. var sorted = new ObservableCollection<SchedulerResource>();
  1010. foreach (var resource in resources.OrderBy(x => x.Name))
  1011. sorted.Add(resource);
  1012. try
  1013. {
  1014. Bookings.DaysViewSettings.ResourceHeaderSize = sorted.Count <= 1 ? 0 : 45;
  1015. }
  1016. catch (Exception e)
  1017. {
  1018. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  1019. }
  1020. //ResizeIntervals(Bookings.ActualHeight);
  1021. try
  1022. {
  1023. Bookings.ResourceCollection = sorted;
  1024. }
  1025. catch (Exception e)
  1026. {
  1027. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  1028. }
  1029. bColumnsLoaded = true;
  1030. }
  1031. // private SchedulerResource GetCurrentResource()
  1032. // {
  1033. // var p = Mouse.GetPosition(Bookings);
  1034. // var panels = Bookings.FindVisualChildren<ScrollViewer>().FirstOrDefault(x => string.Equals(x.Name, "PART_ViewHeaderScrollViewer"));
  1035. // var resource = (int)((p.X + panels.HorizontalOffset - (Bookings.DaysViewSettings.TimeRulerSize + 20F)) / GetResourceColumnWidth());
  1036. // return (Bookings.ResourceCollection as Collection<SchedulerResource>)[resource];
  1037. // }
  1038. //
  1039. // private DateTime GetCurrentTime()
  1040. // {
  1041. // var p = Mouse.GetPosition(Bookings);
  1042. // var hours = (p.Y - (_employees.Length > 1 ? 95.0F : 50.0F)) / (Bookings.DaysViewSettings.TimeIntervalSize * 2F) +
  1043. // Bookings.DaysViewSettings.StartHour;
  1044. // var result = Bookings.SelectedDate.Value + TimeSpan.FromHours(hours);
  1045. // return result;
  1046. // }
  1047. private double GetResourceColumnWidth()
  1048. {
  1049. var colcount = Math.Max(1, _employees.Length);
  1050. colcount = colcount * (Bookings.ViewType == SchedulerViewType.Day ? 1 : Bookings.ViewType == SchedulerViewType.Day ? 5 : 7);
  1051. var colwidth = Bookings.ActualWidth / colcount;
  1052. var minwidth = (Bookings.ActualWidth - (Bookings.DaysViewSettings.TimeRulerSize + 20F)) / Bookings.DaysViewSettings.VisibleResourceCount;
  1053. return Math.Max(minwidth, colwidth);
  1054. }
  1055. private void Schedule_AppointmentEditorOpening(object sender, AppointmentEditorOpeningEventArgs e)
  1056. {
  1057. e.Cancel = true;
  1058. }
  1059. private Dictionary<CalendarMenuName, MenuItem> _menuitems = new Dictionary<CalendarMenuName, MenuItem>();
  1060. public MenuItem? FindMenu(CalendarMenuName name)
  1061. {
  1062. _menuitems.TryGetValue(name, out MenuItem result);
  1063. return result;
  1064. }
  1065. MenuItem CreateMenu<T>(ItemsControl menu, CalendarMenuName name, string header, Action<T?>? action = null, T? data = null) where T : class
  1066. {
  1067. var item = new MenuItem();
  1068. item.Name = name.ToString();
  1069. item.Header = header;
  1070. if (action != null)
  1071. item.Click += (o,args) => action(data);
  1072. item.IsEnabled = data != null;
  1073. menu.Items.Add(item);
  1074. _menuitems[name] = item;
  1075. return item;
  1076. }
  1077. private void Bookings_OnSchedulerContextMenuOpening(object? sender, SchedulerContextMenuOpeningEventArgs e)
  1078. {
  1079. e.ContextMenu.Items.Clear();
  1080. if ((e.MenuType == SchedulerContextMenuType.Appointment) && (e.MenuInfo.Appointment is AssignmentAppointment appointment))
  1081. {
  1082. if (appointment.Model.MeetingID != Guid.Empty)
  1083. CreateMenu(e.ContextMenu, CalendarMenuName.Edit, "Edit Meeting", EditMeeting, appointment.Model);
  1084. else
  1085. {
  1086. CreateMenu(e.ContextMenu, CalendarMenuName.Edit, "Edit Assignment", EditAssignment, appointment.Model);
  1087. e.ContextMenu.Items.Add(new Separator());
  1088. CreateMenu(e.ContextMenu, CalendarMenuName.Copy, "Copy Assignment", CopyAssignment, appointment.Model);
  1089. e.ContextMenu.Items.Add(new Separator());
  1090. CreateMenu(e.ContextMenu, CalendarMenuName.Fill, "Fill Available Time", FillAssignment, appointment.Model);
  1091. }
  1092. e.ContextMenu.Items.Add(new Separator());
  1093. CreateDigitalFormsMenu(e.ContextMenu, appointment);
  1094. if (appointment.Model.MeetingID != Guid.Empty)
  1095. CreateMenu(e.ContextMenu, CalendarMenuName.Edit, "Delete Meeting", DeleteMeeting, appointment.Model);
  1096. else
  1097. CreateMenu(e.ContextMenu, CalendarMenuName.Delete, "Delete Assignment", DeleteAssignment, appointment.Model);
  1098. e.ContextMenu.Items.Add(new Separator());
  1099. CreateMenu<object>(e.ContextMenu, CalendarMenuName.ZoomIn, "Zoom In", (data) => ZoomIn(), null);
  1100. CreateMenu<object>(e.ContextMenu, CalendarMenuName.ZoomOut, "Zoom Out", (data) => ZoomOut(), null);
  1101. CreateMenu<object>(e.ContextMenu, CalendarMenuName.ResetZoom, "Reset Zoom", (data) => ResetZoom(), null);
  1102. CustomiseContextMenu?.Invoke(e.ContextMenu, new CalendarDataEventArgs<object>((e.MenuInfo.Appointment as ICalendarAppointment)?.Model));
  1103. }
  1104. else if (e.MenuType == SchedulerContextMenuType.TimeSlotCell)
  1105. {
  1106. if (Guid.TryParse(e.MenuInfo.Resource.Id?.ToString(), out Guid employeeid))
  1107. {
  1108. var slot = new CalendarTimeSlot(employeeid, e.MenuInfo.DateTime.Value);
  1109. var createmenu = new MenuItem() { Header = "Create.." };
  1110. CreateMenu(createmenu, CalendarMenuName.CreateNew, "New Assignment", (data) => CreateAssignment(slot), slot);
  1111. CreateMenu(createmenu, CalendarMenuName.CreateNew, "New Meeting", (data) => CreateMeeting(slot), slot);
  1112. e.ContextMenu.Items.Add(createmenu);
  1113. if (_copiedmodel != null)
  1114. {
  1115. e.ContextMenu.Items.Add(new Separator());
  1116. CreateMenu(e.ContextMenu, CalendarMenuName.Paste, "Paste Assignment", (data) => PasteAssignment(slot), slot);
  1117. }
  1118. e.ContextMenu.Items.Add(new Separator());
  1119. CreateMenu<object>(e.ContextMenu, CalendarMenuName.ZoomIn, "Zoom In", (data) => ZoomIn(), slot);
  1120. CreateMenu<object>(e.ContextMenu, CalendarMenuName.ZoomOut, "Zoom Out", (data) => ZoomOut(), slot);
  1121. CreateMenu<object>(e.ContextMenu, CalendarMenuName.ResetZoom, "Reset Zoom", (data) => ResetZoom(), slot);
  1122. CustomiseContextMenu?.Invoke(e.ContextMenu, new CalendarDataEventArgs<CalendarTimeSlot>(slot));
  1123. }
  1124. }
  1125. }
  1126. private static void CreateDigitalFormsMenu(ContextMenu menu, AssignmentAppointment appointment)
  1127. {
  1128. var digitalForms = new MenuItem { Header = "Digital Forms" };
  1129. DynamicGridUtils.PopulateFormMenu<AssignmentForm, Assignment, AssignmentLink>(
  1130. digitalForms,
  1131. appointment.Model.ID,
  1132. () => new Client<Assignment>().Load(new Filter<Assignment>(x => x.ID).IsEqualTo(appointment.Model.ID)).First(),
  1133. false);
  1134. menu.Items.Add(digitalForms);
  1135. menu.Items.Add(new Separator());
  1136. }
  1137. public void CreateMeeting(CalendarTimeSlot slot)
  1138. {
  1139. if (slot == null)
  1140. {
  1141. MessageBox.Show("Please select an employee first!");
  1142. return;
  1143. }
  1144. var meeting = new Meeting();
  1145. meeting.Date = slot.Time.Date;
  1146. meeting.Time.Start = AdjustStartTime(slot.Time.TimeOfDay);
  1147. meeting.Time.Duration = TimeIntervalToTimeSpan(TimeInterval);
  1148. meeting.Time.Finish = meeting.Time.Start + meeting.Time.Duration;
  1149. ItemCreated?.Invoke(this, new CalendarDataEventArgs<Meeting>(meeting));
  1150. var args = new CalendarHandledEventArgs<Meeting>(meeting);
  1151. ItemEditing?.Invoke(this, args);
  1152. if (args.Status == CalendarHandledStatus.Cancel)
  1153. return;
  1154. if (args.Status == CalendarHandledStatus.Handled)
  1155. {
  1156. Refresh();
  1157. return;
  1158. }
  1159. CheckGrid(ref mg);
  1160. var items = new[] { meeting };
  1161. bool bOK = mg.EditItems(
  1162. items,
  1163. (type) =>
  1164. {
  1165. if (type == typeof(Assignment))
  1166. return LoadMeetingEmployees(meeting, slot.EmployeeID);
  1167. else if (type == typeof(MeetingItem))
  1168. return LoadMeetingItems();
  1169. return null;
  1170. },
  1171. true
  1172. );
  1173. if (bOK)
  1174. Refresh();
  1175. }
  1176. private CoreTable LoadMeetingEmployees(Meeting meeting, Guid employeeid)
  1177. {
  1178. var result = new CoreTable();
  1179. result.LoadColumns(typeof(Assignment));
  1180. var assignment = new Assignment();
  1181. assignment.EmployeeLink.ID = employeeid;
  1182. var emp = new Client<Employee>().Load(new Filter<Employee>(x => x.ID).IsEqualTo(employeeid)).FirstOrDefault();
  1183. if (emp != null)
  1184. assignment.EmployeeLink.Synchronise(emp);
  1185. result.LoadRows(new[] { assignment });
  1186. return result;
  1187. }
  1188. private CoreTable LoadMeetingItems()
  1189. {
  1190. var result = new CoreTable();
  1191. result.LoadColumns(typeof(MeetingItem));
  1192. return result;
  1193. }
  1194. private void EditMeeting(AssignmentModel model)
  1195. {
  1196. if (model == null)
  1197. {
  1198. MessageBox.Show("Please select an entry first!");
  1199. return;
  1200. }
  1201. CheckGrid(ref mg);
  1202. var meeting = new Client<Meeting>().Query(
  1203. new Filter<Meeting>(x => x.ID).IsEqualTo(model.MeetingID),
  1204. mg.LoadEditorColumns()
  1205. ).Rows.FirstOrDefault()?.ToObject<Meeting>();
  1206. if ((meeting != null) && (mg.EditItems(new[] { meeting })))
  1207. {
  1208. ItemChanged?.Invoke(this, new CalendarDataEventArgs<Meeting>(meeting));
  1209. Refresh();
  1210. }
  1211. }
  1212. private void DeleteMeeting(AssignmentModel model)
  1213. {
  1214. if (model == null)
  1215. {
  1216. MessageBox.Show("Please select an entry first!");
  1217. return;
  1218. }
  1219. if (MessageBox.Show("Are you sure you wish to delete this meeting?", "Confirm Delete", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1220. return;
  1221. var meeting = new Meeting { ID = model.MeetingID };
  1222. new Client<Meeting>().Delete(meeting, "Meeting Deleted from Scheduler");
  1223. ItemChanged?.Invoke(this, new CalendarDataEventArgs<Meeting>(meeting));
  1224. Refresh();
  1225. SelectionChanged?.Invoke(this, new CalendarDataEventArgs<Meeting>(null));
  1226. }
  1227. public Assignment CreateAssignment(CalendarTimeSlot slot)
  1228. {
  1229. if (slot == null)
  1230. {
  1231. MessageBox.Show("Please select an employee first!");
  1232. return null;
  1233. }
  1234. var ass = new Assignment();
  1235. ass.Date = slot.Time.Date;
  1236. ass.Booked.Start = AdjustStartTime(slot.Time.TimeOfDay);
  1237. ass.Booked.Duration = TimeIntervalToTimeSpan(TimeInterval);
  1238. ass.Booked.Finish = ass.Booked.Start + ass.Booked.Duration;
  1239. if ((AssignmentType == CalendarAssignmentType.Actual) || ((AssignmentType == CalendarAssignmentType.Automatic) && (ass.Date <= DateTime.Today)))
  1240. {
  1241. ass.Actual.Start = ass.Booked.Start;
  1242. ass.Actual.Duration = ass.Booked.Duration;
  1243. ass.Actual.Finish = ass.Actual.Start + ass.Actual.Duration;
  1244. }
  1245. ass.EmployeeLink.ID = slot.EmployeeID;
  1246. ItemCreated?.Invoke(this, new CalendarDataEventArgs<Assignment>(ass));
  1247. var args = new CalendarHandledEventArgs<Assignment>(ass);
  1248. ItemEditing?.Invoke(this, args);
  1249. if (args.Status == CalendarHandledStatus.Cancel)
  1250. return null;
  1251. if ((args.Status == CalendarHandledStatus.Handled) || CheckGrid(ref ag).EditItems(new[] { ass }))
  1252. {
  1253. UpdateAssignment(ass);
  1254. _copiedmodel = null;
  1255. }
  1256. return ass;
  1257. }
  1258. private void FillAssignment(AssignmentModel model)
  1259. {
  1260. MessageBox.Show("Not Yet Implemented");
  1261. }
  1262. private void EditAssignment(AssignmentModel model)
  1263. {
  1264. if (model == null)
  1265. {
  1266. MessageBox.Show("Please select an entry first!");
  1267. return;
  1268. }
  1269. var grid = CheckGrid(ref ag);
  1270. var ass = new Client<Assignment>().Query(new Filter<Assignment>(x => x.ID).IsEqualTo(model.ID), grid.LoadEditorColumns())
  1271. .ToObjects<Assignment>().FirstOrDefault();
  1272. if (grid.EditItems(new[] { ass }))
  1273. {
  1274. ItemChanged?.Invoke(this, new CalendarDataEventArgs<Assignment>(ass));
  1275. Refresh();
  1276. }
  1277. }
  1278. private void DeleteAssignment(AssignmentModel model)
  1279. {
  1280. if (model == null)
  1281. {
  1282. MessageBox.Show("Please select an entry first!");
  1283. return;
  1284. }
  1285. if (MessageBox.Show("Are you sure you wish to delete this assignment?", "Confirm Delete", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  1286. return;
  1287. var ass = new Assignment { ID = model.ID };
  1288. new Client<Assignment>().Delete(ass, "Assignment Deleted from Scheduler");
  1289. ItemChanged?.Invoke(this, new CalendarDataEventArgs<Assignment>(ass));
  1290. Refresh();
  1291. SelectionChanged?.Invoke(this, new CalendarDataEventArgs<Assignment>(null));
  1292. }
  1293. private AssignmentModel _copiedmodel;
  1294. private void CopyAssignment(AssignmentModel model)
  1295. {
  1296. if (model == null)
  1297. {
  1298. MessageBox.Show("Please select an entry first!");
  1299. return;
  1300. }
  1301. _copiedmodel = model;
  1302. }
  1303. private Assignment PasteAssignment(CalendarTimeSlot slot)
  1304. {
  1305. if (slot == null)
  1306. {
  1307. MessageBox.Show("Please select an employee first!");
  1308. return null;
  1309. }
  1310. var ass = _assignments.FirstOrDefault(a => a.ID == _copiedmodel.ID)?.Row?.ToObject<Assignment>();
  1311. if (ass == null)
  1312. {
  1313. MessageBox.Show("Cannot find copied entry!");
  1314. return null;
  1315. }
  1316. ass.Date = SelectedDate.Date;
  1317. ass.ID = Guid.Empty;
  1318. ass.Number = 0;
  1319. ass.CommitChanges();
  1320. ass.Booked.Start = AdjustStartTime(slot.Time.TimeOfDay);
  1321. ass.Booked.Duration = TimeIntervalToTimeSpan(TimeInterval);
  1322. ass.Booked.Finish = ass.Booked.Start + ass.Booked.Duration;
  1323. if ((AssignmentType == CalendarAssignmentType.Actual) || ((AssignmentType == CalendarAssignmentType.Automatic) && (ass.Date <= DateTime.Today)))
  1324. {
  1325. ass.Actual.Start = ass.Booked.Start;
  1326. ass.Actual.Duration = ass.Booked.Duration;
  1327. ass.Actual.Finish = ass.Actual.Start + ass.Actual.Duration;
  1328. }
  1329. ass.EmployeeLink.ID = slot.EmployeeID;
  1330. new Client<Assignment>().Save(ass, "");
  1331. UpdateAssignment(ass);
  1332. _copiedmodel = null;
  1333. return ass;
  1334. }
  1335. private void Bookings_OnAppointmentTapped(object? sender, AppointmentTappedArgs e)
  1336. {
  1337. //if (e.Appointment is AssignmentModel model)
  1338. if (e.Appointment is AssignmentAppointment appointment)
  1339. {
  1340. ICalendarDataEventArgs args = new CalendarDataEventArgs<Assignment>(
  1341. appointment.Model.Row?.ToObject<Assignment>()
  1342. );
  1343. SelectionChanged?.Invoke(this, args);
  1344. }
  1345. else
  1346. SelectionChanged?.Invoke(this, new CalendarDataEventArgs<Assignment>(null));
  1347. }
  1348. private void Calendar_OnSizeChanged(object sender, SizeChangedEventArgs e)
  1349. {
  1350. if (double.IsNaN(this.ActualHeight) || (this.ActualHeight == 0))
  1351. return;
  1352. UpdateZoom();
  1353. }
  1354. private void _settingsButton_OnClick(object sender, RoutedEventArgs e)
  1355. {
  1356. _splitPanel.View = DynamicSplitPanelView.Master;
  1357. }
  1358. private void _splitPanel_OnOnChanged(object sender, DynamicSplitPanelSettings e)
  1359. {
  1360. if (EventSuppressor.IsSet(Suppress.Events))
  1361. return;
  1362. if (e.View == DynamicSplitPanelView.Combined && SettingsVisible != CalendarSettingsVisibility.Visible)
  1363. SetSettingsVisibility(CalendarSettingsVisibility.Visible);
  1364. else if (e.View == DynamicSplitPanelView.Master && SettingsVisible == CalendarSettingsVisibility.Visible)
  1365. SetSettingsVisibility(CalendarSettingsVisibility.Hidden);
  1366. }
  1367. private void Bookings_OnQueryAppointments(object? sender, QueryAppointmentsEventArgs e)
  1368. {
  1369. if (bFirst)
  1370. return;
  1371. SelectedDate = e.VisibleDateRange.StartDate.Date;
  1372. }
  1373. }
  1374. }