123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- namespace PRSDesktop
- {
- public class TimeSheetStandardLeaveGrid : DynamicDataGrid<StandardLeave>
- {
- public DateTime From { get; set; }
- public DateTime To { get; set; }
-
- public TimeSheetStandardLeaveGrid() : base()
- {
- ColumnsTag = "TimeSheetStandardLeave";
-
- Options
- .BeginUpdate()
- .Clear()
- .Add(DynamicGridOption.SelectColumns)
- .EndUpdate();
- ActionColumns.Add(new DynamicImageColumn(SelectedImage, SelectedAction));
- HiddenColumns.Add(x => x.LeaveType.ID);
- HiddenColumns.Add(x => x.From);
- HiddenColumns.Add(x => x.FromTime);
- HiddenColumns.Add(x => x.To);
- HiddenColumns.Add(x => x.ToTime);
- HiddenColumns.Add(x => x.Name);
- }
- protected override void GenerateColumns(DynamicGridColumns columns)
- {
- columns.Add<StandardLeave, String>(x => x.Name, 0, "Name", "", Alignment.MiddleLeft);
- columns.Add<StandardLeave, String>(x => x.LeaveType.Description, 200, "Type", "", Alignment.MiddleCenter);
- columns.Add<StandardLeave, DateTime>(x => x.From, 80, "", "dd MMM yy", Alignment.MiddleCenter);
- columns.Add<StandardLeave, DateTime>(x => x.To, 80, "", "dd MMM yy", Alignment.MiddleCenter);
- }
-
- private BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
- private List<Guid> selectedrows = new List<Guid>();
- private bool SelectedAction(CoreRow? arg)
- {
- if (arg == null)
- {
- foreach (var row in Data.Rows)
- ToggleRow(row);
- }
- else
- ToggleRow(arg);
- return false;
- }
- private void ToggleRow(CoreRow row)
- {
- Guid id = row.Get<StandardLeave, Guid>(x => x.ID);
- if (selectedrows.Contains(id))
- selectedrows.Remove(id);
- else
- selectedrows.Add(id);
- InvalidateRow(row);
- }
- private BitmapImage? SelectedImage(CoreRow? arg)
- {
- return arg == null
- ? tick
- : selectedrows.Contains(arg.Get<StandardLeave,Guid>(x=>x.ID))
- ? tick
- : null;
- }
- protected override void Reload(Filters<StandardLeave> criteria, Columns<StandardLeave> columns, ref SortOrder<StandardLeave>? sort, Action<CoreTable?, Exception?> action)
- {
- criteria.Add(new InABox.Core.Filter<StandardLeave>(x => x.From).IsLessThanOrEqualTo(To)
- .And(x => x.To).IsGreaterThanOrEqualTo(From)
- .And(x =>x.Processed).IsEqualTo(DateTime.MinValue));
- base.Reload(criteria, columns, ref sort, action);
- }
- public void Process(IProgress<String> progress)
- {
- if (!selectedrows.Any())
- return;
-
- progress.Report("Loading Standard Leaves");
- var empdates = new Filter<Employee>().All();
- empdates.Ands.Add(new Filter<Employee>(x => x.StartDate).IsEqualTo(DateTime.MinValue)
- .Or(x => x.StartDate).IsLessThanOrEqualTo(To));
- empdates.Ands.Add(new Filter<Employee>(x => x.FinishDate).IsEqualTo(DateTime.MinValue)
- .Or(x => x.FinishDate).IsGreaterThanOrEqualTo(From));
-
- var employees = new Client<Employee>().Query(
- empdates,
- new Columns<Employee>(x => x.ID)
- .Add(x=>x.Name)
- .Add(x => x.Roster)
- .Add(x => x.RosterStart)
- .Add(x => x.StartDate)
- .Add(x => x.FinishDate)
- ).Rows.Select(x=>x.ToObject<Employee>()).ToArray();
- //var employeeids = employees.Select(x=>x.ID).ToArray();
- var rosters = employees.ToDictionary<Employee, Guid, String>(x => x.ID, x => x.Roster);
- int i = 0;
- List<StandardLeave> leaves = new List<StandardLeave>();
- foreach (var row in Data.Rows)
- {
- if (selectedrows.Contains(row.Get<StandardLeave,Guid>(x=>x.ID)))
- {
- var leave = row.ToObject<StandardLeave>();
- foreach (var employee in employees)
- {
- var roster = !String.IsNullOrWhiteSpace(rosters[employee.ID])
- ? Serialization.Deserialize<List<EmployeeRosterItem>>(rosters[employee.ID])?.OrderBy(x => x.Day).ToArray()
- : null;
- progress.Report($"Processing Standard Leave {((double)i * 100.0F)/((double)selectedrows.Count * employees.Length):F2}%");
- var timesheets = RosterUtils.CreateLeaveTimesheets(
- employee,
- roster,
- leave.From,
- leave.FromTime,
- leave.To,
- leave.ToTime,
- leave.LeaveType.ID,
- (timesheet) =>
- {
- timesheet.Notes = leave.Name;
- timesheet.StandardLeaveLink.ID = leave.ID;
- }
- );
- new Client<TimeSheet>().Save(timesheets, "Updated from TimeSheet leave Processor");
- i++;
- }
- leave.Processed = DateTime.Now;
- leaves.Add(leave);
- }
- }
- if (leaves.Any())
- {
- progress.Report("Saving Standard Leaves");
- new Client<StandardLeave>().Save(leaves, "Updated from TimeSheet leave Processor");
- }
- }
-
- }
- }
|