|
@@ -10,15 +10,18 @@ using PRS.Shared.TimeSheetTimberline;
|
|
|
using System.ComponentModel;
|
|
|
using System.Globalization;
|
|
|
using System.IO;
|
|
|
-using System.Linq;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
-using System.Windows.Input;
|
|
|
|
|
|
namespace PRS.Shared
|
|
|
{
|
|
|
namespace TimeSheetTimberline
|
|
|
{
|
|
|
+ /// <summary>
|
|
|
+ /// Represents a block of time having an <see cref="Comal.Classes.Activity"/>. It will always be linked to a timesheet.
|
|
|
+ /// </summary>
|
|
|
+ /// <remarks>
|
|
|
+ /// The primary reason we need a link to a time sheet is so that we can mark the given time sheet as posted if all its corresponding activity blocks
|
|
|
+ /// are posted correctly.
|
|
|
+ /// </remarks>
|
|
|
public class ActivityBlock
|
|
|
{
|
|
|
public Guid Activity { get; set; }
|
|
@@ -31,6 +34,13 @@ namespace PRS.Shared
|
|
|
|
|
|
public TimeSpan Duration => Finish - Start;
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Create an <see cref="ActivityBlock"/> from an <see cref="Assignment"/>, taking the <see cref="Start"/> and <see cref="Finish"/> from
|
|
|
+ /// <see cref="Assignment.EffectiveStartTime"/> and <see cref="Assignment.EffectiveFinishTime"/>.
|
|
|
+ /// </summary>
|
|
|
+ /// <remarks>
|
|
|
+ /// The activity is sourced from the assignment, unless the assignment has no activity, in which case the activity on the timesheet is used.
|
|
|
+ /// </remarks>
|
|
|
public ActivityBlock(Assignment assignment, TimeSheet sheet)
|
|
|
{
|
|
|
Activity = assignment.ActivityLink.ID != Guid.Empty
|
|
@@ -42,6 +52,10 @@ namespace PRS.Shared
|
|
|
TimeSheet = sheet;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Creates an <see cref="ActivityBlock"/> from the timesheet, using the <see
|
|
|
+ /// cref="TimeSheet.ApprovedStart"/> and <see cref="TimeSheet.ApprovedFinish"/>.
|
|
|
+ /// </summary>
|
|
|
public ActivityBlock(TimeSheet sheet)
|
|
|
{
|
|
|
Activity = sheet.ActivityLink.ID;
|
|
@@ -50,6 +64,10 @@ namespace PRS.Shared
|
|
|
TimeSheet = sheet;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Creates an <see cref="ActivityBlock"/> from the timesheet, but with a custom start and finish,
|
|
|
+ /// for use with masking activities.
|
|
|
+ /// </summary>
|
|
|
public ActivityBlock(TimeSheet sheet, TimeSpan start, TimeSpan finish)
|
|
|
{
|
|
|
Activity = sheet.ActivityLink.ID;
|
|
@@ -58,6 +76,10 @@ namespace PRS.Shared
|
|
|
TimeSheet = sheet;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Ensure that this <see cref="ActivityBlock"/> fits within the bounds of the <see cref="TimeSheet"/>.
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>Itself.</returns>
|
|
|
public ActivityBlock Chop(TimeSheet sheet)
|
|
|
{
|
|
|
if (Start < sheet.ApprovedStart)
|
|
@@ -71,6 +93,9 @@ namespace PRS.Shared
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Check if this block is partially or fully within the given timesheet.
|
|
|
+ /// </summary>
|
|
|
public bool ContainedInTimeSheet(TimeSheet sheet) =>
|
|
|
Start < sheet.ApprovedFinish && Finish > sheet.ApprovedStart;
|
|
|
|
|
@@ -82,7 +107,7 @@ namespace PRS.Shared
|
|
|
|
|
|
public interface IBlock
|
|
|
{
|
|
|
- string Job { get; set; }
|
|
|
+ IJob Job { get; set; }
|
|
|
|
|
|
string Extra { get; set; }
|
|
|
|
|
@@ -95,120 +120,73 @@ namespace PRS.Shared
|
|
|
TimeSheet TimeSheet { get; set; }
|
|
|
}
|
|
|
|
|
|
- public class PaidWorkBlock : IBlock
|
|
|
+ public class PaidWorkBlock(string taskID, TimeSpan duration, string payID, IJob job, TimeSheet timeSheet) : IBlock
|
|
|
{
|
|
|
- public string Job { get; set; }
|
|
|
+ public IJob Job { get; set; } = job;
|
|
|
|
|
|
- public string Extra { get; set; }
|
|
|
+ public string Extra { get; set; } = "";
|
|
|
|
|
|
- public string TaskID { get; set; }
|
|
|
+ public string TaskID { get; set; } = taskID;
|
|
|
|
|
|
- public TimeSpan Duration { get; set; }
|
|
|
+ public TimeSpan Duration { get; set; } = duration;
|
|
|
|
|
|
- public string PayrollID { get; set; }
|
|
|
+ public string PayrollID { get; set; } = payID;
|
|
|
|
|
|
- public TimeSheet TimeSheet { get; set; }
|
|
|
-
|
|
|
- public PaidWorkBlock(string taskID, TimeSpan duration, string payID, string job, TimeSheet timeSheet)
|
|
|
- {
|
|
|
- TaskID = taskID;
|
|
|
- Duration = duration;
|
|
|
- PayrollID = payID;
|
|
|
- Job = job;
|
|
|
- Extra = "";
|
|
|
- TimeSheet = timeSheet;
|
|
|
- }
|
|
|
+ public TimeSheet TimeSheet { get; set; } = timeSheet;
|
|
|
}
|
|
|
|
|
|
- public class LeaveBlock : IBlock
|
|
|
+ public class LeaveBlock(string payrollID, TimeSpan duration, TimeSheet timeSheet) : IBlock
|
|
|
{
|
|
|
- public string Job { get; set; }
|
|
|
-
|
|
|
- public string Extra { get; set; }
|
|
|
+ public IJob Job { get; set; } = new Job();
|
|
|
|
|
|
- public string TaskID { get; set; }
|
|
|
+ public string Extra { get; set; } = "";
|
|
|
|
|
|
- public TimeSpan Duration { get; set; }
|
|
|
+ public string TaskID { get; set; } = "";
|
|
|
|
|
|
- public string PayrollID { get; set; }
|
|
|
+ public TimeSpan Duration { get; set; } = duration;
|
|
|
|
|
|
- public TimeSheet TimeSheet { get; set; }
|
|
|
+ public string PayrollID { get; set; } = payrollID;
|
|
|
|
|
|
- public LeaveBlock(string payrollID, TimeSpan duration, TimeSheet timeSheet)
|
|
|
- {
|
|
|
- PayrollID = payrollID;
|
|
|
- Duration = duration;
|
|
|
- Job = "";
|
|
|
- Extra = "";
|
|
|
- TaskID = "";
|
|
|
- TimeSheet = timeSheet;
|
|
|
- }
|
|
|
+ public TimeSheet TimeSheet { get; set; } = timeSheet;
|
|
|
}
|
|
|
|
|
|
- public class BaseArgs : CancelEventArgs
|
|
|
+ public class BaseArgs(IDataModel<TimeSheet> model, Guid employee, DateTime date) : CancelEventArgs
|
|
|
{
|
|
|
- public IDataModel<TimeSheet> Model { get; set; }
|
|
|
-
|
|
|
- public Guid Employee { get; set; }
|
|
|
+ public IDataModel<TimeSheet> Model { get; set; } = model;
|
|
|
|
|
|
- public DateTime Date { get; set; }
|
|
|
+ public Guid Employee { get; set; } = employee;
|
|
|
|
|
|
- public BaseArgs(IDataModel<TimeSheet> model, Guid employee, DateTime date)
|
|
|
- {
|
|
|
- Model = model;
|
|
|
- Employee = employee;
|
|
|
- Date = date;
|
|
|
- }
|
|
|
+ public DateTime Date { get; set; } = date;
|
|
|
}
|
|
|
|
|
|
- public class ProcessRawDataArgs : BaseArgs
|
|
|
+ public class ProcessRawDataArgs(
|
|
|
+ IDataModel<TimeSheet> model, Guid employee, DateTime date,
|
|
|
+ List<TimeSheet> timeSheets, List<Assignment> assignments) : BaseArgs(model, employee, date)
|
|
|
{
|
|
|
- public List<TimeSheet> TimeSheets { get; set; }
|
|
|
+ public List<TimeSheet> TimeSheets { get; set; } = timeSheets;
|
|
|
|
|
|
- public List<Assignment> Assignments { get; set; }
|
|
|
-
|
|
|
- public ProcessRawDataArgs(
|
|
|
- IDataModel<TimeSheet> model, Guid employee, DateTime date,
|
|
|
- List<TimeSheet> timeSheets, List<Assignment> assignments): base(model, employee, date)
|
|
|
- {
|
|
|
- TimeSheets = timeSheets;
|
|
|
- Assignments = assignments;
|
|
|
- }
|
|
|
+ public List<Assignment> Assignments { get; set; } = assignments;
|
|
|
}
|
|
|
- public class ProcessActivityBlocksArgs : BaseArgs
|
|
|
+ public class ProcessActivityBlocksArgs(
|
|
|
+ IDataModel<TimeSheet> model, Guid employee, DateTime date,
|
|
|
+ List<ActivityBlock> activityBlocks) : BaseArgs(model, employee, date)
|
|
|
{
|
|
|
- public List<ActivityBlock> ActivityBlocks { get; set; }
|
|
|
-
|
|
|
- public ProcessActivityBlocksArgs(
|
|
|
- IDataModel<TimeSheet> model, Guid employee, DateTime date,
|
|
|
- List<ActivityBlock> activityBlocks) : base(model, employee, date)
|
|
|
- {
|
|
|
- ActivityBlocks = activityBlocks;
|
|
|
- }
|
|
|
+ public List<ActivityBlock> ActivityBlocks { get; set; } = activityBlocks;
|
|
|
}
|
|
|
- public class ProcessTimeBlocksArgs : BaseArgs
|
|
|
+ public class ProcessTimeBlocksArgs(
|
|
|
+ IDataModel<TimeSheet> model, Guid employee, DateTime date,
|
|
|
+ List<IBlock> blocks) : BaseArgs(model, employee, date)
|
|
|
{
|
|
|
- public List<PaidWorkBlock> WorkBlocks { get; set; }
|
|
|
- public List<LeaveBlock> LeaveBlocks { get; set; }
|
|
|
-
|
|
|
- public ProcessTimeBlocksArgs(
|
|
|
- IDataModel<TimeSheet> model, Guid employee, DateTime date,
|
|
|
- List<PaidWorkBlock> workBlocks, List<LeaveBlock> leaveBlocks) : base(model, employee, date)
|
|
|
- {
|
|
|
- WorkBlocks = workBlocks;
|
|
|
- LeaveBlocks = leaveBlocks;
|
|
|
- }
|
|
|
+ public List<IBlock> Blocks { get; set; } = blocks;
|
|
|
}
|
|
|
- public class ProcessItemArgs : BaseArgs
|
|
|
+ public class ProcessItemArgs(
|
|
|
+ IDataModel<TimeSheet> model, Guid employee, DateTime date,
|
|
|
+ IJob job,
|
|
|
+ TimesheetTimberlineItem item) : BaseArgs(model, employee, date)
|
|
|
{
|
|
|
- public TimesheetTimberlineItem Item { get; set; }
|
|
|
+ public TimesheetTimberlineItem Item { get; set; } = item;
|
|
|
|
|
|
- public ProcessItemArgs(
|
|
|
- IDataModel<TimeSheet> model, Guid employee, DateTime date,
|
|
|
- TimesheetTimberlineItem item) : base(model, employee, date)
|
|
|
- {
|
|
|
- Item = item;
|
|
|
- }
|
|
|
+ public IJob Job { get; set; } = job;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -222,6 +200,16 @@ namespace PRS.Shared
|
|
|
{
|
|
|
items.Add(item);
|
|
|
}
|
|
|
+
|
|
|
+ public void Sort()
|
|
|
+ {
|
|
|
+ items.Sort((a, b) =>
|
|
|
+ {
|
|
|
+ var sort = a.Employee.CompareTo(b.Employee);
|
|
|
+ if (sort != 0) return sort;
|
|
|
+ return a.InDate.CompareTo(b.InDate);
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public class TimesheetTimberlineItem
|
|
@@ -251,8 +239,19 @@ namespace PRS.Shared
|
|
|
|
|
|
public enum TimesheetTimberlineActivityCalculation
|
|
|
{
|
|
|
+ /// <summary>
|
|
|
+ /// Assignments are completely ignored by the export, so that the activities and time is solely provided by the time sheet.
|
|
|
+ /// </summary>
|
|
|
TimesheetOnly,
|
|
|
+ /// <summary>
|
|
|
+ /// Timesheets with activities are processed like in <see cref="TimesheetOnly"/>, but for timesheets without activities, the
|
|
|
+ /// assignments are used to generate time blocks, resorting to the timesheet where there is no assignment.
|
|
|
+ /// </summary>
|
|
|
TimesheetPriority,
|
|
|
+ /// <summary>
|
|
|
+ /// Leave timesheets are processed like <see cref="TimesheetOnly"/>, but all other timesheets use assignments to generate time blocks,
|
|
|
+ /// resorting to the timesheet when there is no assignment for a block of time.
|
|
|
+ /// </summary>
|
|
|
AssignmentPriority
|
|
|
}
|
|
|
|
|
@@ -323,37 +322,56 @@ public class Module
|
|
|
model.SetColumns<TimeSheet>(Columns.None<TimeSheet>().Add(x => x.ID)
|
|
|
.Add(x => x.Approved)
|
|
|
.Add(x => x.EmployeeLink.ID)
|
|
|
+ .Add(x => x.EmployeeLink.Code)
|
|
|
.Add(x => x.Date)
|
|
|
.Add(x => x.ApprovedDuration)
|
|
|
.Add(x => x.ApprovedStart)
|
|
|
.Add(x => x.ApprovedFinish)
|
|
|
.Add(x => x.ActivityLink.ID)
|
|
|
+ .Add(x => x.JobLink.ID)
|
|
|
.Add(x => x.JobLink.JobNumber));
|
|
|
+
|
|
|
+ // Since the activities could come from the assignment or the time sheets, we'll just
|
|
|
+ // load all the activities, rather than use subquery stuff or multiple tables.
|
|
|
model.AddTable<Activity>(
|
|
|
null,
|
|
|
Columns.None<Activity>().Add(x => x.ID).Add(x => x.Code).Add(x => x.PayrollID).Add(x => x.IsLeave),
|
|
|
isdefault: true);
|
|
|
- model.AddTable<OvertimeInterval>(
|
|
|
- null,
|
|
|
- Columns.None<OvertimeInterval>().Add(x => x.ID)
|
|
|
- .Add(x => x.Overtime.ID)
|
|
|
- .Add(x => x.Sequence)
|
|
|
- .Add(x => x.IntervalType)
|
|
|
- .Add(x => x.Interval)
|
|
|
- .Add(x => x.PayrollID)
|
|
|
- .Add(x => x.IsPaid),
|
|
|
- isdefault: true);
|
|
|
+ // Grab every employee on the listed timesheets, that have a PayrollID.
|
|
|
model.AddLookupTable<TimeSheet, Employee>(x => x.EmployeeLink.ID, x => x.ID,
|
|
|
new Filter<Employee>(x => x.PayrollID).IsNotEqualTo(""),
|
|
|
- Columns.None<Employee>().Add(x => x.ID).Add(x => x.Code).Add(x => x.PayrollID).Add(x => x.OvertimeRuleLink.ID)
|
|
|
+ Columns.None<Employee>()
|
|
|
+ .Add(x => x.ID)
|
|
|
+ .Add(x => x.Code)
|
|
|
+ .Add(x => x.PayrollID)
|
|
|
+ .Add(x => x.OvertimeRuleLink.ID)
|
|
|
.Add(x => x.RosterStart),
|
|
|
lookupalias: "Employees", isdefault: true);
|
|
|
+ // We also need to load the rosters and all the overtime intervals on those rosters for
|
|
|
+ // each employee.
|
|
|
model.AddChildTable<Employee, EmployeeRosterItem>(x => x.ID, x => x.Employee.ID,
|
|
|
- columns: Columns.None<EmployeeRosterItem>().Add(x => x.ID)
|
|
|
+ columns: Columns.None<EmployeeRosterItem>()
|
|
|
+ .Add(x => x.ID)
|
|
|
.Add(x => x.Overtime.ID)
|
|
|
.Add(x => x.Employee.ID),
|
|
|
parentalias: "Employees", childalias: "Rosters", isdefault: true);
|
|
|
+ // Note how we skip the actual Overtime class and just link on the shared link.
|
|
|
+ model.AddChildTable<EmployeeRosterItem, OvertimeInterval>(x => x.Overtime.ID, x => x.Overtime.ID,
|
|
|
+ null,
|
|
|
+ Columns.None<OvertimeInterval>().Add(x => x.ID)
|
|
|
+ .Add(x => x.Overtime.ID)
|
|
|
+ .Add(x => x.Sequence)
|
|
|
+ .Add(x => x.IntervalType)
|
|
|
+ .Add(x => x.Interval)
|
|
|
+ .Add(x => x.PayrollID)
|
|
|
+ .Add(x => x.IsPaid),
|
|
|
+ isdefault: true,
|
|
|
+ parentalias: "Rosters");
|
|
|
|
|
|
+ // Also need every assignment on the same date as the listed timesheets. This will load
|
|
|
+ // more than necessary, since we only care about those for the right employees, but this
|
|
|
+ // is simpler than having a complex sub-query. I guess our data model system doesn't
|
|
|
+ // allow for multiple parent tables.
|
|
|
model.AddLookupTable<TimeSheet, Assignment>(x => x.Date, x => x.Date, null,
|
|
|
Columns.None<Assignment>().Add(x => x.ID)
|
|
|
.Add(x => x.Date)
|
|
@@ -391,8 +409,30 @@ public class Module
|
|
|
Script?.Execute(methodname: "ProcessItem", parameters: new object[] { args });
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Return a list of <see cref="ActivityBlock"/>s, where every assignment in the given list
|
|
|
+ /// that occurs within the timesheet is given a block of time, and any remaining time is
|
|
|
+ /// filled by the time sheet.
|
|
|
+ /// </summary>
|
|
|
+ /// <remarks>
|
|
|
+ /// <list type="bullet">
|
|
|
+ /// <item>
|
|
|
+ /// If the time sheet is of type leave, it completely overrides the assignments.
|
|
|
+ /// </item>
|
|
|
+ /// <item>
|
|
|
+ /// If there are any overlapping assignments, their total time is merged and then
|
|
|
+ /// distributed proportionally to each of the overlapping assignments, outputted in
|
|
|
+ /// order by start time.
|
|
|
+ /// </item>
|
|
|
+ /// </list>
|
|
|
+ /// </remarks>
|
|
|
+ /// <returns>
|
|
|
+ /// A list of activity blocks, starting at the beginning of the time sheet and filling
|
|
|
+ /// continuous time to the end of the time sheet.
|
|
|
+ /// </returns>
|
|
|
private IEnumerable<ActivityBlock> GetMaskedActivityBlocks(IEnumerable<Assignment> assignments, TimeSheet sheet)
|
|
|
{
|
|
|
+ // If the time sheet has an activity which is leave, it overrides any assignments.
|
|
|
if (sheet.ActivityLink.ID != Guid.Empty
|
|
|
&& _activities.TryGetValue(sheet.ActivityLink.ID, out var activity)
|
|
|
&& activity.IsLeave)
|
|
@@ -401,15 +441,20 @@ public class Module
|
|
|
yield break;
|
|
|
}
|
|
|
|
|
|
+ // Otherwise, we find every assignment that exists inside this time sheet, and truncate (or "chop") it to fit
|
|
|
+ // inside the time sheet. We also want to order by time.
|
|
|
var blocks = assignments.Select(x => new ActivityBlock(x, sheet))
|
|
|
.Where(x => x.ContainedInTimeSheet(sheet)).Select(x => x.Chop(sheet))
|
|
|
.OrderBy(x => x.Start).ToList();
|
|
|
|
|
|
+ // Redistribute time of overlapping blocks.
|
|
|
for(int i = 0; i < blocks.Count; ++i)
|
|
|
{
|
|
|
var block = blocks[i];
|
|
|
|
|
|
+ // Total duration of all overlapping blocks.
|
|
|
var totalTime = block.Duration;
|
|
|
+ // End time of the block created by merging all overlapping blocks.
|
|
|
var maxFinish = block.Finish;
|
|
|
|
|
|
// Find all overlapping blocks; j represents the next non-overlapping block.
|
|
@@ -422,11 +467,13 @@ public class Module
|
|
|
maxFinish = blocks[j].Finish;
|
|
|
}
|
|
|
}
|
|
|
+ // Total time of the block created by merging all overlapping blocks.
|
|
|
var netTime = maxFinish - block.Start;
|
|
|
|
|
|
var start = block.Start;
|
|
|
- foreach(var newBlock in blocks.Skip(i).Take(j - i))
|
|
|
+ for(int k = i; k < j; ++k)
|
|
|
{
|
|
|
+ var newBlock = blocks[k];
|
|
|
var frac = newBlock.Duration.TotalHours / totalTime.TotalHours;
|
|
|
var duration = netTime.Multiply(frac);
|
|
|
|
|
@@ -434,11 +481,18 @@ public class Module
|
|
|
newBlock.Finish = start + duration;
|
|
|
start = newBlock.Finish;
|
|
|
}
|
|
|
+
|
|
|
+ // Note that we don't skip over the blocks that we have re-distributed time to, since any of the blocks after 'i'
|
|
|
+ // may overlap with later blocks that don't overlap with blocks[i]. However, after redistributing, blocks[i] will only
|
|
|
+ // get smaller, not bigger, so blocks[i] definitely won't overlap with later blocks, and so can now be safely skipped.
|
|
|
}
|
|
|
|
|
|
+ // Keep track of the current time, which is the end of the last block processed.
|
|
|
var curTime = sheet.ApprovedStart;
|
|
|
foreach(var block in blocks)
|
|
|
{
|
|
|
+ // If there is a gap between the last block and the current block, then we use the
|
|
|
+ // time sheet to create a small activity block filling the gap.
|
|
|
if (block.Start > curTime)
|
|
|
{
|
|
|
yield return new ActivityBlock(sheet, curTime, block.Start);
|
|
@@ -446,71 +500,125 @@ public class Module
|
|
|
yield return block;
|
|
|
curTime = block.Finish;
|
|
|
}
|
|
|
+ // If there is time at the end, also fill that extra time using the time sheet.
|
|
|
if(curTime < sheet.ApprovedFinish)
|
|
|
{
|
|
|
yield return new ActivityBlock(sheet, curTime, sheet.ApprovedFinish);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Based on <see cref="TimesheetTimberlineSettings.ActivityCalculation"/>, split each
|
|
|
+ /// timesheet up into a number of activity blocks. Note that time is <b>only</b> allocated
|
|
|
+ /// where a time sheet is, so that there will be no ActivityBlocks with time outside of the
|
|
|
+ /// time represented by the list of time sheets.
|
|
|
+ /// </summary>
|
|
|
+ /// <remarks>
|
|
|
+ /// The output will have continuous activity blocks filling all the time of each time sheet.
|
|
|
+ /// <br/>
|
|
|
+ /// Note that if the timesheets themselves overlap, no special functionality exists, and
|
|
|
+ /// there will in this case be overlapping time blocks.
|
|
|
+ /// </remarks>
|
|
|
+ /// <param name="assignments">A list of assignments for this employee and date.</param>
|
|
|
+ /// <param name="sheets">A list of timesheets for this employee and date.</param>
|
|
|
+ /// <returns>A list of blocks of time that represent an activity the employee was doing.</returns>
|
|
|
private List<ActivityBlock> GetActivityBlocks(IEnumerable<Assignment> assignments, IList<TimeSheet> sheets)
|
|
|
{
|
|
|
switch (Settings.ActivityCalculation)
|
|
|
{
|
|
|
case TimesheetTimberlineActivityCalculation.TimesheetOnly:
|
|
|
+ // In this case, we ignore 'assignments' entirely and just the time sheet constructor for the blocks.
|
|
|
return sheets.Select(x => new ActivityBlock(x)).OrderBy(x => x.Start).ToList();
|
|
|
case TimesheetTimberlineActivityCalculation.TimesheetPriority:
|
|
|
var sheetLookup = sheets.ToLookup(x => x.ActivityLink.ID == Guid.Empty);
|
|
|
+ // Every timesheet that has an activity is a valid activity block. Then, for
|
|
|
+ // each timesheet without an activity, we merge the assignments into the time sheet.
|
|
|
return sheetLookup[false].Select(x => new ActivityBlock(x))
|
|
|
.Concat(sheetLookup[true].SelectMany(x => GetMaskedActivityBlocks(assignments, x)))
|
|
|
.OrderBy(x => x.Start)
|
|
|
.ToList();
|
|
|
case TimesheetTimberlineActivityCalculation.AssignmentPriority:
|
|
|
+ // Every timesheet is masked, unless it is a leave timesheet.
|
|
|
return sheets.SelectMany(x => GetMaskedActivityBlocks(assignments, x)).OrderBy(x => x.Start).ToList();
|
|
|
default:
|
|
|
- throw new Exception($"Invalide Activity calculation {Settings.ActivityCalculation}");
|
|
|
+ throw new Exception($"Invalid Activity calculation {Settings.ActivityCalculation}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private List<PaidWorkBlock> EvaluateOvertime(IEnumerable<PaidWorkBlock> workTime, Guid overtimeID)
|
|
|
+ /// <summary>
|
|
|
+ /// Take a list of paid work blocks, and create a new list of paid work blocks by assigning
|
|
|
+ /// PayrollIDs based on the overtime intervals. If a given interval is unpaid, then no paid
|
|
|
+ /// work blocks are created for that time.
|
|
|
+ /// </summary>
|
|
|
+ private List<IBlock> EvaluateOvertime(IEnumerable<IBlock> time, Guid overtimeID)
|
|
|
{
|
|
|
- var overtimeIntervals = _overtimeIntervals.GetValueOrDefault(overtimeID)?.ToList() ?? new List<OvertimeInterval>();
|
|
|
- overtimeIntervals.Reverse();
|
|
|
+ var overtimeIntervals = _overtimeIntervals.GetValueOrDefault(overtimeID)?.ToArray() ?? [];
|
|
|
+ var curOvertimeIdx = 0;
|
|
|
+ OvertimeInterval? GetOvertimeInterval() => curOvertimeIdx < overtimeIntervals.Length ? overtimeIntervals[curOvertimeIdx] : null;
|
|
|
+ var curInterval = GetOvertimeInterval()?.Interval ?? TimeSpan.Zero;
|
|
|
|
|
|
- var workItems = new List<PaidWorkBlock>();
|
|
|
- foreach (var block in workTime)
|
|
|
+ var newItems = new List<IBlock>();
|
|
|
+ foreach (var block in time)
|
|
|
{
|
|
|
var duration = block.Duration;
|
|
|
while (duration > TimeSpan.Zero)
|
|
|
{
|
|
|
- var interval = overtimeIntervals.LastOrDefault();
|
|
|
+ var interval = GetOvertimeInterval();
|
|
|
if (interval != null)
|
|
|
{
|
|
|
switch (interval.IntervalType)
|
|
|
{
|
|
|
case OvertimeIntervalType.Interval:
|
|
|
- if (duration >= interval.Interval)
|
|
|
+ if (duration >= curInterval)
|
|
|
{
|
|
|
+ // In this case, the paid work block is more than the rest of
|
|
|
+ // the current interval, so we use up all the remaining interval
|
|
|
+ // time, and then move to the next interval.
|
|
|
if (interval.IsPaid)
|
|
|
{
|
|
|
- workItems.Add(new(block.TaskID, interval.Interval, interval.PayrollID, block.Job, block.TimeSheet));
|
|
|
+ if(block is PaidWorkBlock paid)
|
|
|
+ {
|
|
|
+ newItems.Add(new PaidWorkBlock(block.TaskID, curInterval, interval.PayrollID, block.Job, block.TimeSheet));
|
|
|
+ }
|
|
|
+ else if(block is LeaveBlock leave)
|
|
|
+ {
|
|
|
+ newItems.Add(new LeaveBlock(leave.PayrollID, curInterval, leave.TimeSheet));
|
|
|
+ }
|
|
|
}
|
|
|
- overtimeIntervals.RemoveAt(overtimeIntervals.Count - 1);
|
|
|
- duration -= interval.Interval;
|
|
|
+ duration -= curInterval;
|
|
|
+ ++curOvertimeIdx;
|
|
|
+ curInterval = GetOvertimeInterval()?.Interval ?? TimeSpan.Zero;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ // Otherwise, we use up the entire paid work block, and decrease the interval by the duration remaining.
|
|
|
if (interval.IsPaid)
|
|
|
{
|
|
|
- workItems.Add(new(block.TaskID, duration, interval.PayrollID, block.Job, block.TimeSheet));
|
|
|
+ if(block is PaidWorkBlock paid)
|
|
|
+ {
|
|
|
+ newItems.Add(new PaidWorkBlock(block.TaskID, duration, interval.PayrollID, block.Job, block.TimeSheet));
|
|
|
+ }
|
|
|
+ else if(block is LeaveBlock leave)
|
|
|
+ {
|
|
|
+ newItems.Add(new LeaveBlock(leave.PayrollID, duration, leave.TimeSheet));
|
|
|
+ }
|
|
|
}
|
|
|
- interval.Interval -= duration;
|
|
|
+ curInterval -= duration;
|
|
|
duration = TimeSpan.Zero;
|
|
|
}
|
|
|
break;
|
|
|
case OvertimeIntervalType.RemainingTime:
|
|
|
+ // In this case, the interval is unchanged.
|
|
|
if (interval.IsPaid)
|
|
|
{
|
|
|
- workItems.Add(new(block.TaskID, duration, interval.PayrollID, block.Job, block.TimeSheet));
|
|
|
+ if(block is PaidWorkBlock paid)
|
|
|
+ {
|
|
|
+ newItems.Add(new PaidWorkBlock(block.TaskID, duration, interval.PayrollID, block.Job, block.TimeSheet));
|
|
|
+ }
|
|
|
+ else if(block is LeaveBlock leave)
|
|
|
+ {
|
|
|
+ newItems.Add(new LeaveBlock(leave.PayrollID, duration, leave.TimeSheet));
|
|
|
+ }
|
|
|
}
|
|
|
duration = TimeSpan.Zero;
|
|
|
break;
|
|
@@ -520,12 +628,22 @@ public class Module
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- workItems.Add(new(block.TaskID, duration, "", block.Job, block.TimeSheet));
|
|
|
+ // If there is no overtime interval, then we use up the rest of the time on
|
|
|
+ // the block with a blank PayrollID. Theoretically, this shouldn't happen,
|
|
|
+ // since the "RemainingTime" interval is required.
|
|
|
+ if(block is PaidWorkBlock paid)
|
|
|
+ {
|
|
|
+ newItems.Add(new PaidWorkBlock(block.TaskID, duration, "", block.Job, block.TimeSheet));
|
|
|
+ }
|
|
|
+ else if(block is LeaveBlock leave)
|
|
|
+ {
|
|
|
+ newItems.Add(new LeaveBlock(leave.PayrollID, duration, leave.TimeSheet));
|
|
|
+ }
|
|
|
duration = TimeSpan.Zero;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return workItems;
|
|
|
+ return newItems;
|
|
|
}
|
|
|
|
|
|
private TimeSheetTimberlineResult DoProcess(IDataModel<TimeSheet> model)
|
|
@@ -551,15 +669,16 @@ public class Module
|
|
|
var employees = model.GetTable<Employee>("Employees").ToObjects<Employee>()
|
|
|
.ToDictionary(x => x.ID, x => x);
|
|
|
|
|
|
+ // We run through by date and employee, grouping both assignments and time sheets.
|
|
|
var assignments = model.GetTable<Assignment>().ToObjects<Assignment>()
|
|
|
.GroupBy(x => new { x.Date, Employee = x.EmployeeLink.ID }).ToDictionary(x => x.Key, x => x.ToList());
|
|
|
-
|
|
|
var daily = timesheets.GroupBy(x => new { x.Date, Employee = x.EmployeeLink.ID }).ToDictionary(x => x.Key, x => x.ToList());
|
|
|
|
|
|
foreach(var (key, sheets) in daily)
|
|
|
{
|
|
|
var dateAssignments = assignments.GetValueOrDefault(new { key.Date, key.Employee }, new List<Assignment>());
|
|
|
|
|
|
+ // Delegate to script to process raw data.
|
|
|
var rawArgs = new ProcessRawDataArgs(model, key.Employee, key.Date, sheets, dateAssignments);
|
|
|
ProcessRawData(rawArgs);
|
|
|
if (rawArgs.Cancel)
|
|
@@ -571,7 +690,9 @@ public class Module
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ // Split each timesheet into its activity blocks.
|
|
|
var activityBlocks = GetActivityBlocks(rawArgs.Assignments, rawArgs.TimeSheets);
|
|
|
+ // Process activity blocks with script.
|
|
|
var activityArgs = new ProcessActivityBlocksArgs(model, key.Employee, key.Date, activityBlocks);
|
|
|
ProcessActivityBlocks(activityArgs);
|
|
|
if (activityArgs.Cancel)
|
|
@@ -583,13 +704,22 @@ public class Module
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ // Add up all the time for the time sheets.
|
|
|
var approvedDuration = rawArgs.TimeSheets.Aggregate(TimeSpan.Zero, (x, y) => x + y.ApprovedDuration);
|
|
|
+ if(approvedDuration == TimeSpan.Zero)
|
|
|
+ {
|
|
|
+ foreach (var sheet in sheets)
|
|
|
+ {
|
|
|
+ items.AddFailed(sheet, "Zero Approved Duration");
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- var leave = new List<LeaveBlock>();
|
|
|
- var workTime = new List<PaidWorkBlock>();
|
|
|
+ // Convert the activity blocks into LeaveBlocks and PaidWorkBlocks, based on the activity on the block.
|
|
|
+ var blocks = new List<IBlock>();
|
|
|
foreach (var block in activityArgs.ActivityBlocks)
|
|
|
{
|
|
|
- string payID;
|
|
|
+ string taskID;
|
|
|
bool isLeave;
|
|
|
|
|
|
if (block.Activity == Guid.Empty
|
|
@@ -600,98 +730,93 @@ public class Module
|
|
|
Logger.Send(LogType.Error, "", $"Error in Timesheet Timberline export: Activity {block.Activity} does not exist!");
|
|
|
}
|
|
|
|
|
|
- payID = "";
|
|
|
+ taskID = "";
|
|
|
isLeave = false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
isLeave = activity.IsLeave;
|
|
|
- payID = activity.PayrollID;
|
|
|
+ taskID = activity.PayrollID;
|
|
|
}
|
|
|
|
|
|
if (isLeave)
|
|
|
{
|
|
|
- leave.Add(new(payID, block.Finish - block.Start, block.TimeSheet));
|
|
|
+ blocks.Add(new LeaveBlock(taskID, block.Finish - block.Start, block.TimeSheet));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// Leave PayID blank until we've worked out the rosters
|
|
|
- workTime.Add(new(payID, block.Finish - block.Start, "", block.TimeSheet.JobLink.JobNumber, block.TimeSheet));
|
|
|
+ blocks.Add(new PaidWorkBlock(taskID, block.Finish - block.Start, "", block.TimeSheet.JobLink, block.TimeSheet));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (approvedDuration > TimeSpan.Zero)
|
|
|
- {
|
|
|
- var employee = employees.GetValueOrDefault(key.Employee);
|
|
|
- var employeeRosters = rosters.GetValueOrDefault(employee != null ? employee.ID : Guid.Empty);
|
|
|
- var overtimeID = RosterUtils.GetRoster(employeeRosters, employee?.RosterStart, key.Date)?.Overtime.ID ?? Guid.Empty;
|
|
|
-
|
|
|
- var workItems = EvaluateOvertime(workTime, overtimeID);
|
|
|
+ // Find the roster data.
|
|
|
+ var employee = employees.GetValueOrDefault(key.Employee);
|
|
|
+ var employeeRosters = rosters.GetValueOrDefault(employee != null ? employee.ID : Guid.Empty);
|
|
|
+ var overtimeID = RosterUtils.GetRoster(employeeRosters, employee?.RosterStart, key.Date)?.Overtime.ID ?? Guid.Empty;
|
|
|
|
|
|
- var blockArgs = new ProcessTimeBlocksArgs(model, key.Employee, key.Date, workItems, leave);
|
|
|
- ProcessTimeBlocks(blockArgs);
|
|
|
- if (blockArgs.Cancel)
|
|
|
- {
|
|
|
- foreach (var sheet in sheets)
|
|
|
- {
|
|
|
- items.AddFailed(sheet, "Post cancelled by script.");
|
|
|
- }
|
|
|
- continue;
|
|
|
- }
|
|
|
+ // Split up the paid work blocks by the overtime and assign PayrollIDs.
|
|
|
+ var blockItems = EvaluateOvertime(blocks, overtimeID);
|
|
|
|
|
|
- // Succeed all sheets, and then fail them if any of their blocks are failed.
|
|
|
+ var blockArgs = new ProcessTimeBlocksArgs(model, key.Employee, key.Date, blockItems);
|
|
|
+ ProcessTimeBlocks(blockArgs);
|
|
|
+ if (blockArgs.Cancel)
|
|
|
+ {
|
|
|
foreach (var sheet in sheets)
|
|
|
{
|
|
|
- items.AddSuccess(sheet);
|
|
|
+ items.AddFailed(sheet, "Post cancelled by script.");
|
|
|
}
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- var blocks = (blockArgs.WorkBlocks as IEnumerable<IBlock>).Concat(blockArgs.LeaveBlocks);
|
|
|
+ // First presumptively succeed all sheets, and then fail them if any of their blocks are failed.
|
|
|
+ foreach (var sheet in sheets)
|
|
|
+ {
|
|
|
+ items.AddSuccess(sheet);
|
|
|
+ }
|
|
|
|
|
|
- var newItems = new List<Tuple<TimesheetTimberlineItem, List<TimeSheet>>>();
|
|
|
- foreach(var block in blocks.GroupBy(x => new { x.Job, x.TaskID, x.PayrollID }, x => x))
|
|
|
+ var newItems = new List<Tuple<TimesheetTimberlineItem, List<TimeSheet>>>();
|
|
|
+ // Group the blocks by job, TaskID (activity PayrollID) and PayrollID (from the overtime interval).
|
|
|
+ foreach(var group in blockItems.GroupBy(x => new { x.Job.ID, x.TaskID, x.PayrollID }))
|
|
|
+ {
|
|
|
+ var block = group.ToArray();
|
|
|
+ var first = block[0];
|
|
|
+
|
|
|
+ var item = new TimesheetTimberlineItem
|
|
|
{
|
|
|
- var item = new TimesheetTimberlineItem
|
|
|
- {
|
|
|
- Employee = employee?.PayrollID ?? "",
|
|
|
- InDate = DateOnly.FromDateTime(key.Date),
|
|
|
- Job = block.Key.Job,
|
|
|
- Extra = "",
|
|
|
- Task = block.Key.TaskID,
|
|
|
- Hours = block.Sum(x => x.Duration.TotalHours),
|
|
|
- PayID = block.Key.PayrollID
|
|
|
- };
|
|
|
- var itemArgs = new ProcessItemArgs(model, key.Employee, key.Date, item);
|
|
|
- ProcessItem(itemArgs);
|
|
|
-
|
|
|
- var blockTimeSheets = block.Select(x => x.TimeSheet).ToList();
|
|
|
- if (!itemArgs.Cancel)
|
|
|
- {
|
|
|
- newItems.Add(new(itemArgs.Item, blockTimeSheets));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- foreach(var sheet in blockTimeSheets)
|
|
|
- {
|
|
|
- (sheet as IPostable).FailPost("Post cancelled by script.");
|
|
|
- }
|
|
|
- }
|
|
|
+ Employee = employee?.PayrollID ?? "",
|
|
|
+ InDate = DateOnly.FromDateTime(key.Date),
|
|
|
+ Job = first.Job.JobNumber,
|
|
|
+ Extra = "",
|
|
|
+ Task = group.Key.TaskID,
|
|
|
+ Hours = Math.Round(group.Sum(x => x.Duration.TotalHours), 2),
|
|
|
+ PayID = group.Key.PayrollID
|
|
|
+ };
|
|
|
+ var itemArgs = new ProcessItemArgs(model, key.Employee, key.Date, first.Job, item);
|
|
|
+ ProcessItem(itemArgs);
|
|
|
+
|
|
|
+ var blockTimeSheets = block.Select(x => x.TimeSheet).ToList();
|
|
|
+ if (!itemArgs.Cancel)
|
|
|
+ {
|
|
|
+ newItems.Add(new(itemArgs.Item, blockTimeSheets));
|
|
|
}
|
|
|
- foreach(var item in newItems)
|
|
|
+ else
|
|
|
{
|
|
|
- if(item.Item2.All(x => x.PostedStatus == PostedStatus.Posted))
|
|
|
+ foreach(var sheet in blockTimeSheets)
|
|
|
{
|
|
|
- items.AddItem(item.Item1);
|
|
|
+ (sheet as IPostable).FailPost("Post cancelled by script.");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
+ foreach(var item in newItems)
|
|
|
{
|
|
|
- foreach (var sheet in sheets)
|
|
|
+ if(item.Item2.All(x => x.PostedStatus == PostedStatus.Posted))
|
|
|
{
|
|
|
- items.AddFailed(sheet, "Zero Approved Duration");
|
|
|
+ items.AddItem(item.Item1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ items.Sort();
|
|
|
|
|
|
return items;
|
|
|
}
|