Selaa lähdekoodia

Improved interface for timerblin timesheet export

Kenric Nugteren 1 vuosi sitten
vanhempi
commit
93bd748a0a
1 muutettua tiedostoa jossa 95 lisäystä ja 62 poistoa
  1. 95 62
      prs.shared/Posters/Timberline/TimesheetTimberlinePoster.cs

+ 95 - 62
prs.shared/Posters/Timberline/TimesheetTimberlinePoster.cs

@@ -10,6 +10,7 @@ using PRS.Shared.TimeSheetTimberline;
 using Syncfusion.Windows.Shared;
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Globalization;
 using System.IO;
 using System.Linq;
@@ -139,6 +140,71 @@ namespace PRS.Shared
             }
         }
 
+        public class BaseArgs : CancelEventArgs
+        {
+            public IDataModel<TimeSheet> Model { get; set; }
+
+            public Guid Employee { get; set; }
+
+            public DateTime Date { get; set; }
+
+            public BaseArgs(IDataModel<TimeSheet> model, Guid employee, DateTime date)
+            {
+                Model = model;
+                Employee = employee;
+                Date = date;
+            }
+        }
+
+        public class ProcessRawDataArgs : BaseArgs
+        {
+            public List<TimeSheet> TimeSheets { get; set; }
+
+            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 class ProcessActivityBlocksArgs : BaseArgs
+        {
+            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 class ProcessTimeBlocksArgs : BaseArgs
+        {
+            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 class ProcessItemArgs : BaseArgs
+        {
+            public TimesheetTimberlineItem Item { get; set; }
+
+            public ProcessItemArgs(
+                IDataModel<TimeSheet> model, Guid employee, DateTime date,
+                TimesheetTimberlineItem item) : base(model, employee, date)
+            {
+                Item = item;
+            }
+        }
     }
 
     public class TimesheetTimberlineItem
@@ -193,37 +259,25 @@ public class Module
         // Perform pre-processing
     }
     
-    public bool ProcessDailyTimeSheets(IDataModel<TimeSheet> model, Guid employee, DateTime date, List<TimeSheet> timesheets, List<Assignment> assignments)
+    public void ProcessRawData(ProcessRawDataArgs args)
     {
         // Before PRS calculates anything, you can edit the list of timesheets and assignments it is working with here.
-
-        // Return false unless you wish PRS to skip these entries.
-        return false;
     }
 
-    public bool ProcessActivityBlocks(IDataModel<TimeSheet> model, Guid employee, DateTime date, List<ActivityBlock> blocks)
+    public void ProcessActivityBlocks(ProcessActivityBlocksArgs args)
     {
         // Once PRS has aggregated the list of timesheets and assignments into a list of time blocks with given activities, you can edit these time blocks here.
-
-        // Always return false, unless you wish PRS to skip all these entries.
-        return false;
     }
 
-    public bool ProcessBlocks(IDataModel<TimeSheet> model, Guid employee, DateTime date, List<PaidWorkBlock> work, List<LeaveBlock> leave)
+    public void ProcessTimeBlocks(ProcessTimeBlocksArgs args)
     {
         // This function is called after PRS has determined the length, duration and overtime rules for all the blocks of time. Here, you can edit
         // this data before it is collated into the export.
-        
-        // Always return false, unless you wish PRS to skip these entries.
-        return false;
     }
 
-    public bool ProcessItem(IDataModel<TimeSheet> model, Guid employee, DateTime date, TimesheetTimberlineItem line)
+    public void ProcessItem(ProcessItemArgs args)
     {
         // This is the final function before PRS exports each item. You can edit the data as you wish.
-
-        // Returning true here means that PRS adds this item to the export. If you wish this item to be skipped, return false.
-        return true;
     }
 
     public void AfterPost(IDataModel<TimeSheet> model)
@@ -302,52 +356,24 @@ public class Module
             return true;
         }
 
-        /// <summary>
-        /// Preprocess the timesheets and assignments for a given day and employee, and optionally skip them.
-        /// </summary>
-        /// <param name="employee"></param>
-        /// <param name="date"></param>
-        /// <param name="timesheets"></param>
-        /// <param name="assignments"></param>
-        /// <returns><see langword="true"/> if this set of timesheets has now been processed, and so they should be skipped.</returns>
-        private bool ProcessDailyTimeSheets(IDataModel<TimeSheet> model, Guid employee, DateTime date, List<TimeSheet> timesheets, List<Assignment> assignments)
+        private void ProcessRawData(ProcessRawDataArgs args)
         {
-            return Script?.Execute(methodname: "ProcessDailyTimeSheets", parameters: new object[] { model, employee, date, timesheets, assignments })
-                ?? false;
+            Script?.Execute(methodname: "ProcessRawData", parameters: new object[] { args });
         }
 
-        /// <summary>
-        /// Process the activity blocks for a given day and employee, and optionally skip them.
-        /// </summary>
-        /// <param name="employee"></param>
-        /// <param name="date"></param>
-        /// <param name="blocks"></param>
-        /// <returns><see langword="true"/> if this set of timesheets should be skipped.</returns>
-        private bool ProcessActivityBlocks(IDataModel<TimeSheet> model, Guid employee, DateTime date, List<ActivityBlock> blocks)
+        private void ProcessActivityBlocks(ProcessActivityBlocksArgs args)
         {
-            return Script?.Execute(methodname: "ProcessActivityBlocks", parameters: new object[] { model, employee, date, blocks })
-                ?? false;
+            Script?.Execute(methodname: "ProcessActivityBlocks", parameters: new object[] { args });
         }
 
-        /// <summary>
-        /// Process the blocks for a given day and employee after the roster has been calculated, and optionally skip them.
-        /// </summary>
-        /// <returns><see langword="true"/> if this set of timesheets should be skipped.</returns>
-        private bool ProcessBlocks(IDataModel<TimeSheet> model, Guid employee, DateTime date, List<PaidWorkBlock> work, List<LeaveBlock> leave)
+        private void ProcessTimeBlocks(ProcessTimeBlocksArgs args)
         {
-            return Script?.Execute(methodname: "ProcessBlocks", parameters: new object[] { model, employee, date, work, leave })
-                ?? false;
+            Script?.Execute(methodname: "ProcessTimeBlocks", parameters: new object[] { args });
         }
 
-        /// <summary>
-        /// Process the item before it gets added to the export.
-        /// </summary>
-        /// <param name="line"></param>
-        /// <returns><see langword="false"/> if this item shouldn't be exported.</returns>
-        private bool ProcessItem(IDataModel<TimeSheet> model, Guid employee, DateTime date, TimesheetTimberlineItem line)
+        private void ProcessItem(ProcessItemArgs args)
         {
-            return Script?.Execute(methodname: "ProcessItem", parameters: new object[] { model, employee, date, line }, defaultResult: true)
-                ?? true;
+            Script?.Execute(methodname: "ProcessItem", parameters: new object[] { args });
         }
 
         private IEnumerable<ActivityBlock> GetMaskedActivityBlocks(IEnumerable<Assignment> assignments, TimeSheet sheet)
@@ -519,23 +545,26 @@ public class Module
             {
                 var dateAssignments = assignments.GetValueOrDefault(new { key.Date, key.Employee }, new List<Assignment>());
 
-                if(ProcessDailyTimeSheets(model, key.Employee, key.Date, sheets, dateAssignments))
+                var rawArgs = new ProcessRawDataArgs(model, key.Employee, key.Date, sheets, dateAssignments);
+                ProcessRawData(rawArgs);
+                if (rawArgs.Cancel)
                 {
                     continue;
                 }
 
-                var activityBlocks = GetActivityBlocks(dateAssignments, sheets);
-
-                if (ProcessActivityBlocks(model, key.Employee, key.Date, activityBlocks))
+                var activityBlocks = GetActivityBlocks(rawArgs.Assignments, rawArgs.TimeSheets);
+                var activityArgs = new ProcessActivityBlocksArgs(model, key.Employee, key.Date, activityBlocks);
+                ProcessActivityBlocks(activityArgs);
+                if (activityArgs.Cancel)
                 {
                     continue;
                 }
 
-                var approvedDuration = sheets.Aggregate(TimeSpan.Zero, (x, y) => x + y.ApprovedDuration);
+                var approvedDuration = rawArgs.TimeSheets.Aggregate(TimeSpan.Zero, (x, y) => x + y.ApprovedDuration);
 
                 var leave = new List<LeaveBlock>();
                 var workTime = new List<PaidWorkBlock>();
-                foreach (var block in activityBlocks)
+                foreach (var block in activityArgs.ActivityBlocks)
                 {
                     string payID;
                     bool isLeave;
@@ -576,12 +605,14 @@ public class Module
 
                     var workItems = EvaluateOvertime(workTime, overtimeID);
 
-                    if (ProcessBlocks(model, key.Employee, key.Date, workItems, leave))
+                    var blockArgs = new ProcessTimeBlocksArgs(model, key.Employee, key.Date, workItems, leave);
+                    ProcessTimeBlocks(blockArgs);
+                    if (blockArgs.Cancel)
                     {
                         continue;
                     }
 
-                    var blocks = (workItems as IEnumerable<IBlock>).Concat(leave);
+                    var blocks = (blockArgs.WorkBlocks as IEnumerable<IBlock>).Concat(blockArgs.LeaveBlocks);
                     foreach(var block in blocks.GroupBy(x => new { x.Job, x.TaskID, x.PayrollID }, x => x))
                     {
                         var item = new TimesheetTimberlineItem
@@ -594,9 +625,11 @@ public class Module
                             Hours = block.Sum(x => x.Duration.TotalHours),
                             PayID = block.Key.PayrollID
                         };
-                        if (ProcessItem(model, key.Employee, key.Date, item))
+                        var itemArgs = new ProcessItemArgs(model, key.Employee, key.Date, item);
+                        ProcessItem(itemArgs);
+                        if (!itemArgs.Cancel)
                         {
-                            items.Add(item);
+                            items.Add(itemArgs.Item);
                         }
                     }
                 }