AssignmentList.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. using Comal.Classes;
  7. using Xamarin.Forms;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using InABox.Configuration;
  11. using Syncfusion.SfSchedule.XForms;
  12. using XF.Material.Forms;
  13. using XF.Material.Forms.UI.Dialogs;
  14. using XF.Material.Forms.UI.Dialogs.Configurations;
  15. using PRSSecurity = InABox.Core.Security;
  16. namespace comal.timesheets
  17. {
  18. public enum AssignmentView
  19. {
  20. Day,
  21. TimeLine
  22. }
  23. public enum AssignmentLookupType
  24. {
  25. ActiveJobs,
  26. UnbookedJobs,
  27. Tasks
  28. }
  29. public partial class AssignmentList
  30. {
  31. public AssignmentModel DataModel { get; private set; }
  32. private AssignmentEdit _editor = null;
  33. private Guid[] _employeeids = new Guid[] { };
  34. private AssignmentModuleSettings _settings = null;
  35. private AssignmentView _view = AssignmentView.Day;
  36. private AssignmentLookupType _lookuptype = AssignmentLookupType.UnbookedJobs;
  37. private String _teamname = "";
  38. public AssignmentList()
  39. {
  40. InitializeComponent();
  41. DataModel = new AssignmentModel(App.Data, GetFilter, true);
  42. BindingContext = DataModel;
  43. _settings = new LocalConfiguration<AssignmentModuleSettings>().Load();
  44. DatePicker.Date = _settings.Date.IsEmpty() ? DateTime.Today : _settings.Date;
  45. _view = _settings.View;
  46. _employeeids = (_settings.Employees != null)
  47. ? _settings.Employees
  48. : new Guid[] { App.Data.Me.ID };
  49. _lookuptype = _settings.LookupType;
  50. LookupType.Text = CoreUtils.Neatify(_lookuptype.ToString());
  51. _teamname = String.IsNullOrWhiteSpace(_settings.TeamName) ? App.Data.Me.Name : _settings.TeamName;
  52. ShowHideSources.IsVisible = PRSSecurity.IsAllowed<CanViewMobileAssignmentsSidebar>();
  53. }
  54. protected override void OnAppearing()
  55. {
  56. base.OnAppearing();
  57. RefreshLookups();
  58. Reload();
  59. }
  60. private void RefreshLookups()
  61. {
  62. if (_lookuptype == AssignmentLookupType.Tasks)
  63. {
  64. App.Data.KanbanLookups.Refresh(false, () =>
  65. {
  66. Device.BeginInvokeOnMainThread(() =>
  67. {
  68. Lookups.ItemsSource = new ObservableCollection<KanbanLookupShell>(App.Data.KanbanLookups);
  69. });
  70. });
  71. }
  72. else if (_lookuptype == AssignmentLookupType.ActiveJobs)
  73. {
  74. App.Data.JobLookups.Refresh(false, () =>
  75. {
  76. Device.BeginInvokeOnMainThread(() =>
  77. {
  78. Lookups.ItemsSource = new ObservableCollection<JobLookupShell>(App.Data.JobLookups);
  79. });
  80. });
  81. }
  82. else if (_lookuptype == AssignmentLookupType.UnbookedJobs)
  83. {
  84. App.Data.JobLookups.Refresh(false, () =>
  85. {
  86. Device.BeginInvokeOnMainThread(() =>
  87. {
  88. Lookups.ItemsSource = new ObservableCollection<JobLookupShell>(App.Data.JobLookups.Where(x=>x.OpenAssignments == 0));
  89. });
  90. });
  91. }
  92. }
  93. private void Reload()
  94. {
  95. DayView.DataSource = null;
  96. TimeLineView.DataSource = null;
  97. ScheduleType.Text = _teamname;
  98. if (_view == AssignmentView.Day)
  99. {
  100. TimelineFrame.IsVisible = false;
  101. DayFrame.IsVisible = true;
  102. DayView.DayViewSettings.DayLabelSettings.TimeFormat = "HH:mm";
  103. }
  104. else
  105. {
  106. DayFrame.IsVisible = false;
  107. TimelineFrame.IsVisible = true;
  108. TimeLineView.ResourceViewSettings.VisibleResourceCount = Math.Max(1, Math.Min(16, _employeeids.Length));
  109. TimeLineView.TimelineViewSettings.AppointmentHeight =
  110. (this.Height / TimeLineView.ResourceViewSettings.VisibleResourceCount) + 100;
  111. var resources = new ObservableCollection<object>();
  112. foreach (var empid in _employeeids)
  113. {
  114. var empname = App.Data.Employees.Items.FirstOrDefault(x => x.ID == empid)?.Name ?? empid.ToString();
  115. if (_employeeids.Length > 10)
  116. empname = String.Join("", empname.Split(' ').Select(x => x.Substring(0, 1)));
  117. else if (_employeeids.Length > 6)
  118. {
  119. var comps = empname.Split(' ').ToArray();
  120. empname = $"{comps.First()} {String.Join("", comps.Skip(1).Select(x => x.Substring(0, 1)))}";
  121. }
  122. resources.Add(
  123. new ScheduleResource()
  124. {
  125. Name = empname,
  126. Id = empid,
  127. Color = Color.Transparent,
  128. Image = ""
  129. }
  130. );
  131. }
  132. TimeLineView.ScheduleResources = resources;
  133. TimeLineView.ShowResourceView = true;
  134. }
  135. RefreshData();
  136. }
  137. private Filter<Assignment> GetFilter() =>
  138. new Filter<Assignment>(x => x.Date).IsEqualTo(DatePicker.Date)
  139. .And(x => x.EmployeeLink.ID).InList(_employeeids);
  140. private void RefreshData()
  141. {
  142. DataModel.Refresh(true, () => Device.BeginInvokeOnMainThread(UpdateScreen));
  143. }
  144. private void UpdateScreen()
  145. {
  146. Title = $"{DatePicker.Date:dd MMMM yyyy}";
  147. if (_view == AssignmentView.Day)
  148. {
  149. DayView.DataSource = new ObservableCollection<AssignmentShell>(DataModel.Items);
  150. DayView.MoveToDate = ShowRelevantTime();
  151. }
  152. else
  153. {
  154. TimeLineView.DataSource = new ObservableCollection<AssignmentShell>(DataModel.Items);
  155. TimeLineView.MoveToDate = ShowRelevantTime();
  156. }
  157. }
  158. private DateTime ShowRelevantTime()
  159. {
  160. return (DataModel.Items.Count > 0 ? (DataModel.Items.First() as AssignmentShell).StartTime : DateTime.Now).AddMinutes(-30);
  161. }
  162. private void Lookups_OnItemTapped(object sender, ItemTappedEventArgs e)
  163. {
  164. if (e.Item is ILookupShell lookup)
  165. {
  166. if (lookup.Selected)
  167. lookup.Selected = false;
  168. else
  169. {
  170. IList<ILookupShell> lookups = _lookuptype == AssignmentLookupType.Tasks
  171. ? App.Data.KanbanLookups.Items.ToArray<ILookupShell>()
  172. : App.Data.JobLookups.Items.ToArray<ILookupShell>();
  173. {
  174. foreach (var other in lookups.Where(x => x.Selected).ToArray())
  175. other.Selected = false;
  176. }
  177. lookup.Selected = true;
  178. }
  179. }
  180. }
  181. private void LookupsType_Tapped(object sender, EventArgs e)
  182. {
  183. _lookuptype = _lookuptype == AssignmentLookupType.Tasks
  184. ? AssignmentLookupType.ActiveJobs
  185. : _lookuptype == AssignmentLookupType.ActiveJobs
  186. ? AssignmentLookupType.UnbookedJobs
  187. : AssignmentLookupType.Tasks;
  188. _settings.LookupType = _lookuptype;
  189. LookupType.Text = CoreUtils.Neatify(_lookuptype.ToString());
  190. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  191. RefreshLookups();
  192. }
  193. private async void Schedule_OnCellTapped(object sender, CellTappedEventArgs e)
  194. {
  195. if (e.Appointment is AssignmentShell item)
  196. {
  197. if (item.DeliveryID != Guid.Empty)
  198. {
  199. var delivery = new DeliveryDetails() { DeliveryID = item.DeliveryID};
  200. Navigation.PushAsync(delivery);
  201. }
  202. else
  203. {
  204. var editor = new AssignmentEdit(item);
  205. Navigation.PushAsync(editor);
  206. }
  207. }
  208. }
  209. private async void Schedule_OnCellLongPressed(object sender, CellTappedEventArgs e)
  210. {
  211. if (e.Appointment == null)
  212. {
  213. if (InABox.Core.Security.CanEdit<Assignment>())
  214. {
  215. CreateAssignment(
  216. e.Datetime,
  217. e.Resource as ScheduleResource
  218. );
  219. }
  220. }
  221. else if (InABox.Core.Security.CanDelete<Assignment>()
  222. && e.Appointment is AssignmentShell assignment)
  223. {
  224. await DeleteAssignment(assignment.Id);
  225. }
  226. }
  227. private void CreateAssignment(DateTime date, ScheduleResource resource)
  228. {
  229. var assignment = new Assignment()
  230. {
  231. Date = date.Date,
  232. Title = "New Assignment",
  233. };
  234. assignment.Booked.Start = new TimeSpan(date.TimeOfDay.Hours, 0, 0);
  235. assignment.Booked.Finish = date.TimeOfDay.Add(new TimeSpan(1, 0, 0));
  236. assignment.Booked.Duration = new TimeSpan(1, 0, 0);
  237. assignment.EmployeeLink.ID = (resource is ScheduleResource sr)
  238. ? (Guid)sr.Id
  239. : App.Data.Me.ID;
  240. var job = (_lookuptype == AssignmentLookupType.ActiveJobs) || (_lookuptype == AssignmentLookupType.UnbookedJobs)
  241. ? App.Data.JobLookups.Items.FirstOrDefault(x => x.Selected)
  242. : null;
  243. if (job != null)
  244. {
  245. assignment.JobLink.ID = job.ID;
  246. assignment.JobLink.JobNumber = job.Code;
  247. assignment.JobLink.Name = job.Description;
  248. assignment.Description = job.Notes;
  249. assignment.Title = job.Description;
  250. job.Selected = false;
  251. }
  252. var task = _lookuptype == AssignmentLookupType.Tasks
  253. ? App.Data.KanbanLookups.Items.FirstOrDefault(x => x.Selected)
  254. : null;
  255. if (task != null)
  256. {
  257. assignment.Task.ID = task.ID;
  258. assignment.Task.Number = int.Parse(task.Code);
  259. assignment.Task.Title = task.Description;
  260. assignment.Title = task.Notes;
  261. assignment.Description = task.Description;
  262. task.Selected = false;
  263. }
  264. var editor = new AssignmentEdit(assignment);
  265. Navigation.PushAsync(editor);
  266. }
  267. private async Task DeleteAssignment(Guid id)
  268. {
  269. var confirm = await MaterialDialog.Instance.ConfirmAsync(
  270. "Are you sure you wish to delete this assignment?",
  271. "Confirm Deletion",
  272. "Yes, Delete",
  273. "Cancel",
  274. new MaterialAlertDialogConfiguration()
  275. {
  276. ButtonFontFamily = Material.FontFamily.Body2
  277. }
  278. );
  279. if (confirm == true)
  280. {
  281. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Deleting Assignment"))
  282. {
  283. var assignment = new Assignment() { ID = id };
  284. new Client<Assignment>().Delete(assignment, "Deleted on Mobile Device");
  285. }
  286. RefreshData();
  287. }
  288. }
  289. private void SelectDate_OnClicked(object sender, EventArgs e)
  290. {
  291. DatePicker.Focus();
  292. }
  293. private void DatePicker_OnDateSelected(object sender, DateChangedEventArgs e)
  294. {
  295. if (_employeeids.Any())
  296. {
  297. _settings.Date = DatePicker.Date;
  298. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  299. }
  300. DayView.MoveToDate = DatePicker.Date;
  301. TimeLineView.MoveToDate = DatePicker.Date;
  302. RefreshData();
  303. }
  304. private async void SelectEmployees_OnClicked(object sender, EventArgs e)
  305. {
  306. var actions = new List<string>() { "Only Me" };
  307. //actions.AddRange(App.Data.TeamEmployeeShells.Where(x=>x.ID == App.Data.Employee.ID).Select(x=>x.TeamName).Distinct());
  308. actions.AddRange(App.Data.EmployeeTeams.Select(x => x.TeamName).Distinct());
  309. var result = await MaterialDialog.Instance.SelectActionAsync(title: "Select a Team",
  310. actions: actions);
  311. if (result == 0)
  312. {
  313. _view = AssignmentView.Day;
  314. _employeeids = new Guid[] { App.Data.Me.ID };
  315. _teamname = App.Data.Me.Name;
  316. }
  317. else if (result > 0)
  318. {
  319. _view = AssignmentView.TimeLine;
  320. _employeeids = App.Data.EmployeeTeams.Where(x => String.Equals(x.TeamName, actions[result]))
  321. .Select(x => x.ID).Distinct().ToArray();
  322. _teamname = actions[result];
  323. }
  324. _settings.Employees = _employeeids;
  325. _settings.View = _view;
  326. _settings.TeamName = _teamname;
  327. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  328. Dispatcher.BeginInvokeOnMainThread(() =>
  329. {
  330. Reload();
  331. });
  332. }
  333. private bool _sidebarvisible = false;
  334. private void ShowHideSources_OnClicked(object sender, EventArgs e)
  335. {
  336. _sidebarvisible = !_sidebarvisible;
  337. ShowHideSources.Source = _sidebarvisible
  338. ? ImageSource.FromFile("rightarrowwhite.png")
  339. : ImageSource.FromFile("downarrowwhite.png");
  340. JobColumn.Width = _sidebarvisible
  341. ? new GridLength(150, GridUnitType.Absolute)
  342. : new GridLength(0, GridUnitType.Absolute);
  343. }
  344. }
  345. }