123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Windows.Controls;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using PRSDesktop.WidgetGroups;
- namespace PRSDesktop
- {
- public class TimesheetWidgetProperties : IDashboardProperties { }
- public class TimesheetWidgetElement : DashboardElement<TimesheetWidget, HumanResources, TimesheetWidgetProperties> { }
- /// <summary>
- /// Interaction logic for TimesheetWidget.xaml
- /// </summary>
- public partial class TimesheetWidget : UserControl, IDashboardWidget<HumanResources, TimesheetWidgetProperties>
- {
- private DateTime _date = DateTime.Today;
- private Guid _groupid = CoreUtils.FullGuid;
- private CoreTable activities;
- private CoreTable employees;
- private readonly DataTable report = new();
- private CoreTable timesheets;
- public TimesheetWidget()
- {
- InitializeComponent();
- report.Columns.Add("ID", typeof(Guid));
- report.Columns.Add("Name", typeof(string));
- report.Columns.Add("Leave", typeof(string));
- report.Columns.Add("LateStart", typeof(TimeSpan));
- report.Columns.Add("EarlyFinish", typeof(TimeSpan));
- report.Columns.Add("ClockIn", typeof(string));
- report.Columns.Add("ClockOut", typeof(string));
- report.PrimaryKey = new[] { report.Columns[0] };
- }
- public DateTime Date
- {
- get => _date;
- set
- {
- _date = value;
- LoadTimeSheets();
- }
- }
- public Guid GroupID
- {
- get => _groupid;
- set
- {
- _groupid = value;
- LoadEmployees(value);
- }
- }
- public TimesheetWidgetProperties Properties { get; set; }
- public void Setup()
- {
- LoadEmployees(GroupID);
- LoadActivities();
- }
- public void Refresh()
- {
- LoadTimeSheets();
- }
- public void Shutdown()
- {
- }
- private void ClearReport()
- {
- Report.ItemsSource = null;
- }
- private void LoadEmployees(Guid id)
- {
- ClearReport();
- employees = null;
- new Client<Employee>().Query(
- id != CoreUtils.FullGuid ? new Filter<Employee>(x => x.Group.ID).IsEqualTo(id) : null,
- null,
- null,
- (o, e) =>
- {
- employees = o;
- CheckData();
- }
- );
- }
- private void LoadActivities()
- {
- ClearReport();
- activities = null;
- new Client<Activity>().Query(
- new Filter<Activity>(x => x.IsLeave).IsEqualTo(true),
- new Columns<Activity>(
- x => x.ID,
- x => x.Description
- ),
- null,
- (o, e) =>
- {
- activities = o;
- CheckData();
- }
- );
- }
- private void LoadTimeSheets()
- {
- ClearReport();
- timesheets = null;
- new Client<TimeSheet>().Query(
- new Filter<TimeSheet>(x => x.Date).IsEqualTo(_date),
- null,
- new SortOrder<TimeSheet>(x => x.EmployeeLink.Name),
- (o, e) =>
- {
- timesheets = o;
- CheckData();
- }
- );
- }
- private void CheckData()
- {
- if (employees != null && activities != null && timesheets != null)
- ProcessData();
- }
- private string Codify(string name)
- {
- var result = "";
- var comps = name.ToUpper().Split(' ');
- foreach (var comp in comps)
- if (comp.Any())
- result += comp.First();
- return string.IsNullOrWhiteSpace(result) ? "??" : result;
- }
- private void ProcessData()
- {
- try
- {
- report.Rows.Clear();
- foreach (var time in timesheets.Rows)
- {
- var empid = time.Get<TimeSheet, Guid>(x => x.EmployeeLink.ID);
- var empname = time.Get<TimeSheet, string>(x => x.EmployeeLink.Name);
- if (report.Rows.Find(empid) == null)
- {
- var emprow = employees.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.ID).Equals(empid));
- if (emprow != null)
- {
- var userid = emprow.Get<Employee, string>(x => x.UserLink.UserID);
- var usualstart = emprow.Get<Employee, TimeSpan>(x => x.UsualStart);
- var usualfinish = emprow.Get<Employee, TimeSpan>(x => x.UsualFinish);
- var rows = timesheets.Rows.Where(r => r.Get<TimeSheet, Guid>(c => c.EmployeeLink.ID).Equals(empid));
- var start = new TimeSpan(long.MaxValue);
- TimeSpan? late = null;
- var finish = new TimeSpan(long.MinValue);
- TimeSpan? early = null;
- var leave = "";
- var clockin = "";
- var clockout = "";
- var leaves = new List<string>();
- foreach (var row in rows)
- {
- var activity = row.Get<TimeSheet, Guid>(x => x.ActivityLink.ID);
- var activityrow = activities.Rows.FirstOrDefault(r => r.Get<Activity, Guid>(c => c.ID).Equals(activity));
- if (activityrow != null)
- {
- var thisleave = Codify(activityrow.Get<Activity, string>(x => x.Description));
- if (!leaves.Contains(thisleave))
- leaves.Add(thisleave);
- }
- leave = string.Join(",", leaves);
- var createdby = row.Get<TimeSheet, string>(x => x.CreatedBy);
- if (createdby == null)
- createdby = "";
- if (usualstart.Ticks != 0)
- {
- var thisstart = row.Get<TimeSheet, TimeSpan>(x => x.Start);
- if (thisstart < start)
- {
- start = thisstart;
- if (start > usualstart)
- late = !late.HasValue || start < late.Value ? start : late;
- else
- late = null;
- clockin = createdby.Equals(userid) ? "" : createdby;
- }
- }
- if (usualfinish.Ticks != 0)
- {
- var thisfinish = row.Get<TimeSheet, TimeSpan>(x => x.Finish);
- if (thisfinish.Ticks == 0)
- {
- usualfinish = new TimeSpan(0);
- early = null;
- }
- else
- {
- if (thisfinish > finish)
- {
- finish = thisfinish;
- if (finish < usualfinish)
- early = !early.HasValue || finish > early.Value ? finish : early;
- else
- early = null;
- clockout = createdby.Equals(userid) ? "" : createdby;
- }
- }
- }
- }
- leave = string.Join(",", leaves);
- if (!string.IsNullOrEmpty(leave) || late.HasValue || early.HasValue || !string.IsNullOrEmpty(clockin) ||
- !string.IsNullOrEmpty(clockout))
- report.Rows.Add(empid, empname, leave, late, early, clockin, clockout);
- }
- }
- }
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
- }
- Dispatcher.Invoke(() => { Report.ItemsSource = report; });
- }
- }
- }
|