TimesheetTimberlinePoster.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. using Comal.Classes;
  2. using CsvHelper;
  3. using CsvHelper.Configuration.Attributes;
  4. using InABox.Core;
  5. using InABox.Core.Postable;
  6. using InABox.Poster.Timberline;
  7. using InABox.Scripting;
  8. using Microsoft.Win32;
  9. using PRS.Shared.TimeSheetTimberline;
  10. using System.ComponentModel;
  11. using System.Globalization;
  12. using System.IO;
  13. namespace PRS.Shared
  14. {
  15. namespace TimeSheetTimberline
  16. {
  17. /// <summary>
  18. /// Represents a block of time having an <see cref="Comal.Classes.Activity"/>. It will always be linked to a timesheet.
  19. /// </summary>
  20. /// <remarks>
  21. /// 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
  22. /// are posted correctly.
  23. /// </remarks>
  24. public class ActivityBlock
  25. {
  26. public Guid Activity { get; set; }
  27. public TimeSpan Start { get; set; }
  28. public TimeSpan Finish { get; set; }
  29. public TimeSheet TimeSheet { get; set; }
  30. public TimeSpan Duration => Finish - Start;
  31. /// <summary>
  32. /// Create an <see cref="ActivityBlock"/> from an <see cref="Assignment"/>, taking the <see cref="Start"/> and <see cref="Finish"/> from
  33. /// <see cref="Assignment.EffectiveStartTime"/> and <see cref="Assignment.EffectiveFinishTime"/>.
  34. /// </summary>
  35. /// <remarks>
  36. /// The activity is sourced from the assignment, unless the assignment has no activity, in which case the activity on the timesheet is used.
  37. /// </remarks>
  38. public ActivityBlock(Assignment assignment, TimeSheet sheet)
  39. {
  40. Activity = assignment.ActivityLink.ID != Guid.Empty
  41. ? assignment.ActivityLink.ID
  42. : sheet.ActivityLink.ID;
  43. Start = assignment.EffectiveStartTime();
  44. Finish = assignment.EffectiveFinishTime();
  45. TimeSheet = sheet;
  46. }
  47. /// <summary>
  48. /// Creates an <see cref="ActivityBlock"/> from the timesheet, using the <see
  49. /// cref="TimeSheet.ApprovedStart"/> and <see cref="TimeSheet.ApprovedFinish"/>.
  50. /// </summary>
  51. public ActivityBlock(TimeSheet sheet)
  52. {
  53. Activity = sheet.ActivityLink.ID;
  54. Start = sheet.ApprovedStart;
  55. Finish = sheet.ApprovedFinish;
  56. TimeSheet = sheet;
  57. }
  58. /// <summary>
  59. /// Creates an <see cref="ActivityBlock"/> from the timesheet, but with a custom start and finish,
  60. /// for use with masking activities.
  61. /// </summary>
  62. public ActivityBlock(TimeSheet sheet, TimeSpan start, TimeSpan finish)
  63. {
  64. Activity = sheet.ActivityLink.ID;
  65. Start = start;
  66. Finish = finish;
  67. TimeSheet = sheet;
  68. }
  69. /// <summary>
  70. /// Ensure that this <see cref="ActivityBlock"/> fits within the bounds of the <see cref="TimeSheet"/>.
  71. /// </summary>
  72. /// <returns>Itself.</returns>
  73. public ActivityBlock Chop(TimeSheet sheet)
  74. {
  75. if (Start < sheet.ApprovedStart)
  76. {
  77. Start = sheet.ApprovedStart;
  78. }
  79. if (Finish > sheet.ApprovedFinish)
  80. {
  81. Finish = sheet.ApprovedFinish;
  82. }
  83. return this;
  84. }
  85. /// <summary>
  86. /// Check if this block is partially or fully within the given timesheet.
  87. /// </summary>
  88. public bool ContainedInTimeSheet(TimeSheet sheet) =>
  89. Start < sheet.ApprovedFinish && Finish > sheet.ApprovedStart;
  90. public bool IntersectsWith(ActivityBlock other)
  91. {
  92. return Start < other.Finish && Finish > other.Start;
  93. }
  94. }
  95. public interface IBlock
  96. {
  97. IJob Job { get; set; }
  98. string Extra { get; set; }
  99. string TaskID { get; set; }
  100. TimeSpan Duration { get; set; }
  101. string PayrollID { get; set; }
  102. TimeSheet TimeSheet { get; set; }
  103. }
  104. public class PaidWorkBlock(string taskID, TimeSpan duration, string payID, IJob job, TimeSheet timeSheet) : IBlock
  105. {
  106. public IJob Job { get; set; } = job;
  107. public string Extra { get; set; } = "";
  108. public string TaskID { get; set; } = taskID;
  109. public TimeSpan Duration { get; set; } = duration;
  110. public string PayrollID { get; set; } = payID;
  111. public TimeSheet TimeSheet { get; set; } = timeSheet;
  112. }
  113. public class LeaveBlock(string payrollID, TimeSpan duration, TimeSheet timeSheet) : IBlock
  114. {
  115. public IJob Job { get; set; } = new Job();
  116. public string Extra { get; set; } = "";
  117. public string TaskID { get; set; } = "";
  118. public TimeSpan Duration { get; set; } = duration;
  119. public string PayrollID { get; set; } = payrollID;
  120. public TimeSheet TimeSheet { get; set; } = timeSheet;
  121. }
  122. public class BaseArgs(IDataModel<TimeSheet> model, Guid employee, DateTime date) : CancelEventArgs
  123. {
  124. public IDataModel<TimeSheet> Model { get; set; } = model;
  125. public Guid Employee { get; set; } = employee;
  126. public DateTime Date { get; set; } = date;
  127. }
  128. public class ProcessRawDataArgs(
  129. IDataModel<TimeSheet> model, Guid employee, DateTime date,
  130. List<TimeSheet> timeSheets, List<Assignment> assignments) : BaseArgs(model, employee, date)
  131. {
  132. public List<TimeSheet> TimeSheets { get; set; } = timeSheets;
  133. public List<Assignment> Assignments { get; set; } = assignments;
  134. }
  135. public class ProcessActivityBlocksArgs(
  136. IDataModel<TimeSheet> model, Guid employee, DateTime date,
  137. List<ActivityBlock> activityBlocks) : BaseArgs(model, employee, date)
  138. {
  139. public List<ActivityBlock> ActivityBlocks { get; set; } = activityBlocks;
  140. }
  141. public class ProcessTimeBlocksArgs(
  142. IDataModel<TimeSheet> model, Guid employee, DateTime date,
  143. List<IBlock> blocks) : BaseArgs(model, employee, date)
  144. {
  145. public List<IBlock> Blocks { get; set; } = blocks;
  146. }
  147. public class ProcessItemArgs(
  148. IDataModel<TimeSheet> model, Guid employee, DateTime date,
  149. IJob job,
  150. TimesheetTimberlineItem item) : BaseArgs(model, employee, date)
  151. {
  152. public TimesheetTimberlineItem Item { get; set; } = item;
  153. public IJob Job { get; set; } = job;
  154. }
  155. }
  156. public class TimeSheetTimberlineResult : PostResult<TimeSheet>
  157. {
  158. private List<TimesheetTimberlineItem> items = new List<TimesheetTimberlineItem>();
  159. public IEnumerable<TimesheetTimberlineItem> Items => items;
  160. public void AddItem(TimesheetTimberlineItem item)
  161. {
  162. items.Add(item);
  163. }
  164. public void Sort()
  165. {
  166. items.Sort((a, b) =>
  167. {
  168. var sort = a.Employee.CompareTo(b.Employee);
  169. if (sort != 0) return sort;
  170. return a.InDate.CompareTo(b.InDate);
  171. });
  172. }
  173. }
  174. public class TimesheetTimberlineItem
  175. {
  176. [Index(0)]
  177. public string Employee { get; set; } = "";
  178. [Index(1)]
  179. [CsvHelper.Configuration.Attributes.TypeConverter(typeof(TimberlinePosterDateConverter))]
  180. public DateOnly InDate { get; set; }
  181. [Index(2)]
  182. public string Job { get; set; } = "";
  183. [Index(3)]
  184. public string Extra { get; set; } = "";
  185. [Index(4)]
  186. public string Task { get; set; } = "";
  187. [Index(5)]
  188. public double Hours { get; set; }
  189. [Index(6)]
  190. public string PayID { get; set; } = "";
  191. }
  192. public enum TimesheetTimberlineActivityCalculation
  193. {
  194. /// <summary>
  195. /// Assignments are completely ignored by the export, so that the activities and time is solely provided by the time sheet.
  196. /// </summary>
  197. TimesheetOnly,
  198. /// <summary>
  199. /// Timesheets with activities are processed like in <see cref="TimesheetOnly"/>, but for timesheets without activities, the
  200. /// assignments are used to generate time blocks, resorting to the timesheet where there is no assignment.
  201. /// </summary>
  202. TimesheetPriority,
  203. /// <summary>
  204. /// Leave timesheets are processed like <see cref="TimesheetOnly"/>, but all other timesheets use assignments to generate time blocks,
  205. /// resorting to the timesheet when there is no assignment for a block of time.
  206. /// </summary>
  207. AssignmentPriority
  208. }
  209. public class TimesheetTimberlineSettings : TimberlinePosterSettings<TimeSheet>
  210. {
  211. [EnumLookupEditor(typeof(TimesheetTimberlineActivityCalculation), LookupWidth = 200)]
  212. public TimesheetTimberlineActivityCalculation ActivityCalculation { get; set; }
  213. protected override string DefaultScript()
  214. {
  215. return
  216. @"using PRS.Shared;
  217. using PRS.Shared.TimeSheetTimberline;
  218. using InABox.Core;
  219. using System.Collections.Generic;
  220. public class Module
  221. {
  222. public void BeforePost(IDataModel<TimeSheet> model)
  223. {
  224. // Perform pre-processing
  225. }
  226. public void ProcessRawData(ProcessRawDataArgs args)
  227. {
  228. // Before PRS calculates anything, you can edit the list of timesheets and assignments it is working with here.
  229. }
  230. public void ProcessActivityBlocks(ProcessActivityBlocksArgs args)
  231. {
  232. // 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.
  233. }
  234. public void ProcessTimeBlocks(ProcessTimeBlocksArgs args)
  235. {
  236. // This function is called after PRS has determined the length, duration and overtime rules for all the blocks of time. Here, you can edit
  237. // this data before it is collated into the export.
  238. }
  239. public void ProcessItem(ProcessItemArgs args)
  240. {
  241. // This is the final function before PRS exports each item. You can edit the data as you wish.
  242. }
  243. public void AfterPost(IDataModel<TimeSheet> model)
  244. {
  245. // Perform post-processing
  246. }
  247. }";
  248. }
  249. }
  250. public class TimesheetTimberlinePoster : ITimberlinePoster<TimeSheet, TimesheetTimberlineSettings>
  251. {
  252. public ScriptDocument? Script { get; set; }
  253. public TimesheetTimberlineSettings Settings { get; set; }
  254. private Dictionary<Guid, Activity> _activities = null!; // Initialised on DoProcess()
  255. private Dictionary<Guid, OvertimeInterval[]> _overtimeIntervals = null!; // Initialised on DoProcess()
  256. public bool BeforePost(IDataModel<TimeSheet> model)
  257. {
  258. model.RemoveTable<Document>("CompanyLogo");
  259. model.RemoveTable<CoreTable>("CompanyInformation");
  260. model.RemoveTable<Employee>();
  261. model.RemoveTable<User>();
  262. model.SetColumns<TimeSheet>(Columns.None<TimeSheet>().Add(x => x.ID)
  263. .Add(x => x.Approved)
  264. .Add(x => x.EmployeeLink.ID)
  265. .Add(x => x.EmployeeLink.Code)
  266. .Add(x => x.Date)
  267. .Add(x => x.ApprovedDuration)
  268. .Add(x => x.ApprovedStart)
  269. .Add(x => x.ApprovedFinish)
  270. .Add(x => x.ActivityLink.ID)
  271. .Add(x => x.JobLink.ID)
  272. .Add(x => x.JobLink.JobNumber));
  273. // Since the activities could come from the assignment or the time sheets, we'll just
  274. // load all the activities, rather than use subquery stuff or multiple tables.
  275. model.AddTable<Activity>(
  276. null,
  277. Columns.None<Activity>().Add(x => x.ID).Add(x => x.Code).Add(x => x.PayrollID).Add(x => x.IsLeave),
  278. null,
  279. isdefault: true);
  280. // Grab every employee on the listed timesheets, that have a PayrollID.
  281. model.AddLookupTable<TimeSheet, Employee>(x => x.EmployeeLink.ID, x => x.ID,
  282. new Filter<Employee>(x => x.PayrollID).IsNotEqualTo(""),
  283. Columns.None<Employee>()
  284. .Add(x => x.ID)
  285. .Add(x => x.Code)
  286. .Add(x => x.PayrollID)
  287. .Add(x => x.OvertimeRuleLink.ID)
  288. .Add(x => x.RosterStart),
  289. lookupalias: "Employees", isdefault: true);
  290. // We also need to load the rosters and all the overtime intervals on those rosters for
  291. // each employee.
  292. model.AddChildTable<Employee, EmployeeRosterItem>(x => x.ID, x => x.Employee.ID,
  293. columns: Columns.None<EmployeeRosterItem>()
  294. .Add(x => x.ID)
  295. .Add(x => x.Overtime.ID)
  296. .Add(x => x.Employee.ID),
  297. parentalias: "Employees", childalias: "Rosters", isdefault: true);
  298. // Note how we skip the actual Overtime class and just link on the shared link.
  299. model.AddChildTable<EmployeeRosterItem, OvertimeInterval>(x => x.Overtime.ID, x => x.Overtime.ID,
  300. null,
  301. Columns.None<OvertimeInterval>().Add(x => x.ID)
  302. .Add(x => x.Overtime.ID)
  303. .Add(x => x.Sequence)
  304. .Add(x => x.IntervalType)
  305. .Add(x => x.Interval)
  306. .Add(x => x.PayrollID)
  307. .Add(x => x.IsPaid),
  308. isdefault: true,
  309. parentalias: "Rosters");
  310. // Also need every assignment on the same date as the listed timesheets. This will load
  311. // more than necessary, since we only care about those for the right employees, but this
  312. // is simpler than having a complex sub-query. I guess our data model system doesn't
  313. // allow for multiple parent tables.
  314. model.AddLookupTable<TimeSheet, Assignment>(x => x.Date, x => x.Date, null,
  315. Columns.None<Assignment>().Add(x => x.ID)
  316. .Add(x => x.Date)
  317. .Add(x => x.EmployeeLink.ID)
  318. .Add(x => x.Actual.Start)
  319. .Add(x => x.Actual.Duration)
  320. .Add(x => x.Actual.Finish)
  321. .Add(x => x.Booked.Start)
  322. .Add(x => x.Booked.Duration)
  323. .Add(x => x.Booked.Finish)
  324. .Add(x => x.ActivityLink.ID),
  325. isdefault: true);
  326. Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
  327. return true;
  328. }
  329. private void ProcessRawData(ProcessRawDataArgs args)
  330. {
  331. Script?.Execute(methodname: "ProcessRawData", parameters: new object[] { args });
  332. }
  333. private void ProcessActivityBlocks(ProcessActivityBlocksArgs args)
  334. {
  335. Script?.Execute(methodname: "ProcessActivityBlocks", parameters: new object[] { args });
  336. }
  337. private void ProcessTimeBlocks(ProcessTimeBlocksArgs args)
  338. {
  339. Script?.Execute(methodname: "ProcessTimeBlocks", parameters: new object[] { args });
  340. }
  341. private void ProcessItem(ProcessItemArgs args)
  342. {
  343. Script?.Execute(methodname: "ProcessItem", parameters: new object[] { args });
  344. }
  345. /// <summary>
  346. /// Return a list of <see cref="ActivityBlock"/>s, where every assignment in the given list
  347. /// that occurs within the timesheet is given a block of time, and any remaining time is
  348. /// filled by the time sheet.
  349. /// </summary>
  350. /// <remarks>
  351. /// <list type="bullet">
  352. /// <item>
  353. /// If the time sheet is of type leave, it completely overrides the assignments.
  354. /// </item>
  355. /// <item>
  356. /// If there are any overlapping assignments, their total time is merged and then
  357. /// distributed proportionally to each of the overlapping assignments, outputted in
  358. /// order by start time.
  359. /// </item>
  360. /// </list>
  361. /// </remarks>
  362. /// <returns>
  363. /// A list of activity blocks, starting at the beginning of the time sheet and filling
  364. /// continuous time to the end of the time sheet.
  365. /// </returns>
  366. private IEnumerable<ActivityBlock> GetMaskedActivityBlocks(IEnumerable<Assignment> assignments, TimeSheet sheet)
  367. {
  368. // If the time sheet has an activity which is leave, it overrides any assignments.
  369. if (sheet.ActivityLink.ID != Guid.Empty
  370. && _activities.TryGetValue(sheet.ActivityLink.ID, out var activity)
  371. && activity.IsLeave)
  372. {
  373. yield return new ActivityBlock(sheet);
  374. yield break;
  375. }
  376. // Otherwise, we find every assignment that exists inside this time sheet, and truncate (or "chop") it to fit
  377. // inside the time sheet. We also want to order by time.
  378. var blocks = assignments.Select(x => new ActivityBlock(x, sheet))
  379. .Where(x => x.ContainedInTimeSheet(sheet)).Select(x => x.Chop(sheet))
  380. .OrderBy(x => x.Start).ToList();
  381. // Redistribute time of overlapping blocks.
  382. for(int i = 0; i < blocks.Count; ++i)
  383. {
  384. var block = blocks[i];
  385. // Total duration of all overlapping blocks.
  386. var totalTime = block.Duration;
  387. // End time of the block created by merging all overlapping blocks.
  388. var maxFinish = block.Finish;
  389. // Find all overlapping blocks; j represents the next non-overlapping block.
  390. int j = i + 1;
  391. for (; j < blocks.Count && block.IntersectsWith(blocks[j]); ++j)
  392. {
  393. totalTime += blocks[j].Duration;
  394. if (blocks[j].Finish > maxFinish)
  395. {
  396. maxFinish = blocks[j].Finish;
  397. }
  398. }
  399. // Total time of the block created by merging all overlapping blocks.
  400. var netTime = maxFinish - block.Start;
  401. var start = block.Start;
  402. for(int k = i; k < j; ++k)
  403. {
  404. var newBlock = blocks[k];
  405. var frac = newBlock.Duration.TotalHours / totalTime.TotalHours;
  406. var duration = netTime.Multiply(frac);
  407. newBlock.Start = start;
  408. newBlock.Finish = start + duration;
  409. start = newBlock.Finish;
  410. }
  411. // Note that we don't skip over the blocks that we have re-distributed time to, since any of the blocks after 'i'
  412. // may overlap with later blocks that don't overlap with blocks[i]. However, after redistributing, blocks[i] will only
  413. // get smaller, not bigger, so blocks[i] definitely won't overlap with later blocks, and so can now be safely skipped.
  414. }
  415. // Keep track of the current time, which is the end of the last block processed.
  416. var curTime = sheet.ApprovedStart;
  417. foreach(var block in blocks)
  418. {
  419. // If there is a gap between the last block and the current block, then we use the
  420. // time sheet to create a small activity block filling the gap.
  421. if (block.Start > curTime)
  422. {
  423. yield return new ActivityBlock(sheet, curTime, block.Start);
  424. }
  425. yield return block;
  426. curTime = block.Finish;
  427. }
  428. // If there is time at the end, also fill that extra time using the time sheet.
  429. if(curTime < sheet.ApprovedFinish)
  430. {
  431. yield return new ActivityBlock(sheet, curTime, sheet.ApprovedFinish);
  432. }
  433. }
  434. /// <summary>
  435. /// Based on <see cref="TimesheetTimberlineSettings.ActivityCalculation"/>, split each
  436. /// timesheet up into a number of activity blocks. Note that time is <b>only</b> allocated
  437. /// where a time sheet is, so that there will be no ActivityBlocks with time outside of the
  438. /// time represented by the list of time sheets.
  439. /// </summary>
  440. /// <remarks>
  441. /// The output will have continuous activity blocks filling all the time of each time sheet.
  442. /// <br/>
  443. /// Note that if the timesheets themselves overlap, no special functionality exists, and
  444. /// there will in this case be overlapping time blocks.
  445. /// </remarks>
  446. /// <param name="assignments">A list of assignments for this employee and date.</param>
  447. /// <param name="sheets">A list of timesheets for this employee and date.</param>
  448. /// <returns>A list of blocks of time that represent an activity the employee was doing.</returns>
  449. private List<ActivityBlock> GetActivityBlocks(IEnumerable<Assignment> assignments, IList<TimeSheet> sheets)
  450. {
  451. switch (Settings.ActivityCalculation)
  452. {
  453. case TimesheetTimberlineActivityCalculation.TimesheetOnly:
  454. // In this case, we ignore 'assignments' entirely and just the time sheet constructor for the blocks.
  455. return sheets.Select(x => new ActivityBlock(x)).OrderBy(x => x.Start).ToList();
  456. case TimesheetTimberlineActivityCalculation.TimesheetPriority:
  457. var sheetLookup = sheets.ToLookup(x => x.ActivityLink.ID == Guid.Empty);
  458. // Every timesheet that has an activity is a valid activity block. Then, for
  459. // each timesheet without an activity, we merge the assignments into the time sheet.
  460. return sheetLookup[false].Select(x => new ActivityBlock(x))
  461. .Concat(sheetLookup[true].SelectMany(x => GetMaskedActivityBlocks(assignments, x)))
  462. .OrderBy(x => x.Start)
  463. .ToList();
  464. case TimesheetTimberlineActivityCalculation.AssignmentPriority:
  465. // Every timesheet is masked, unless it is a leave timesheet.
  466. return sheets.SelectMany(x => GetMaskedActivityBlocks(assignments, x)).OrderBy(x => x.Start).ToList();
  467. default:
  468. throw new Exception($"Invalid Activity calculation {Settings.ActivityCalculation}");
  469. }
  470. }
  471. /// <summary>
  472. /// Take a list of paid work blocks, and create a new list of paid work blocks by assigning
  473. /// PayrollIDs based on the overtime intervals. If a given interval is unpaid, then no paid
  474. /// work blocks are created for that time.
  475. /// </summary>
  476. private List<IBlock> EvaluateOvertime(IEnumerable<IBlock> time, Guid overtimeID)
  477. {
  478. var overtimeIntervals = _overtimeIntervals.GetValueOrDefault(overtimeID)?.ToArray() ?? [];
  479. var curOvertimeIdx = 0;
  480. OvertimeInterval? GetOvertimeInterval() => curOvertimeIdx < overtimeIntervals.Length ? overtimeIntervals[curOvertimeIdx] : null;
  481. var curInterval = GetOvertimeInterval()?.Interval ?? TimeSpan.Zero;
  482. var newItems = new List<IBlock>();
  483. foreach (var block in time)
  484. {
  485. var duration = block.Duration;
  486. while (duration > TimeSpan.Zero)
  487. {
  488. var interval = GetOvertimeInterval();
  489. if (interval != null)
  490. {
  491. switch (interval.IntervalType)
  492. {
  493. case OvertimeIntervalType.Interval:
  494. if (duration >= curInterval)
  495. {
  496. // In this case, the paid work block is more than the rest of
  497. // the current interval, so we use up all the remaining interval
  498. // time, and then move to the next interval.
  499. if (interval.IsPaid)
  500. {
  501. if(block is PaidWorkBlock paid)
  502. {
  503. newItems.Add(new PaidWorkBlock(block.TaskID, curInterval, interval.PayrollID, block.Job, block.TimeSheet));
  504. }
  505. else if(block is LeaveBlock leave)
  506. {
  507. newItems.Add(new LeaveBlock(leave.PayrollID, curInterval, leave.TimeSheet));
  508. }
  509. }
  510. duration -= curInterval;
  511. ++curOvertimeIdx;
  512. curInterval = GetOvertimeInterval()?.Interval ?? TimeSpan.Zero;
  513. }
  514. else
  515. {
  516. // Otherwise, we use up the entire paid work block, and decrease the interval by the duration remaining.
  517. if (interval.IsPaid)
  518. {
  519. if(block is PaidWorkBlock paid)
  520. {
  521. newItems.Add(new PaidWorkBlock(block.TaskID, duration, interval.PayrollID, block.Job, block.TimeSheet));
  522. }
  523. else if(block is LeaveBlock leave)
  524. {
  525. newItems.Add(new LeaveBlock(leave.PayrollID, duration, leave.TimeSheet));
  526. }
  527. }
  528. curInterval -= duration;
  529. duration = TimeSpan.Zero;
  530. }
  531. break;
  532. case OvertimeIntervalType.RemainingTime:
  533. // In this case, the interval is unchanged.
  534. if (interval.IsPaid)
  535. {
  536. if(block is PaidWorkBlock paid)
  537. {
  538. newItems.Add(new PaidWorkBlock(block.TaskID, duration, interval.PayrollID, block.Job, block.TimeSheet));
  539. }
  540. else if(block is LeaveBlock leave)
  541. {
  542. newItems.Add(new LeaveBlock(leave.PayrollID, duration, leave.TimeSheet));
  543. }
  544. }
  545. duration = TimeSpan.Zero;
  546. break;
  547. default:
  548. throw new NotImplementedException($"Not implemented Overtime interval type {interval.IntervalType}");
  549. }
  550. }
  551. else
  552. {
  553. // If there is no overtime interval, then we use up the rest of the time on
  554. // the block with a blank PayrollID. Theoretically, this shouldn't happen,
  555. // since the "RemainingTime" interval is required.
  556. if(block is PaidWorkBlock paid)
  557. {
  558. newItems.Add(new PaidWorkBlock(block.TaskID, duration, "", block.Job, block.TimeSheet));
  559. }
  560. else if(block is LeaveBlock leave)
  561. {
  562. newItems.Add(new LeaveBlock(leave.PayrollID, duration, leave.TimeSheet));
  563. }
  564. duration = TimeSpan.Zero;
  565. }
  566. }
  567. }
  568. return newItems;
  569. }
  570. private TimeSheetTimberlineResult DoProcess(IDataModel<TimeSheet> model)
  571. {
  572. var items = new TimeSheetTimberlineResult();
  573. var timesheets = model.GetTable<TimeSheet>().ToObjects<TimeSheet>().ToList();
  574. if(timesheets.Any(x => x.Approved.IsEmpty()))
  575. {
  576. throw new Exception("Unapproved Timesheets detected");
  577. }
  578. else if (!timesheets.Any())
  579. {
  580. throw new Exception("No approved timesheets found");
  581. }
  582. _activities = model.GetTable<Activity>().ToObjects<Activity>().ToDictionary(x => x.ID, x => x);
  583. _overtimeIntervals = model.GetTable<OvertimeInterval>().ToObjects<OvertimeInterval>()
  584. .GroupBy(x => x.Overtime.ID)
  585. .ToDictionary(x => x.Key, x => x.OrderBy(x => x.Sequence).ToArray());
  586. var rosters = model.GetTable<EmployeeRosterItem>("Rosters").ToObjects<EmployeeRosterItem>()
  587. .GroupBy(x => x.Employee.ID).ToDictionary(x => x.Key, x => x.ToArray());
  588. var employees = model.GetTable<Employee>("Employees").ToObjects<Employee>()
  589. .ToDictionary(x => x.ID, x => x);
  590. // We run through by date and employee, grouping both assignments and time sheets.
  591. var assignments = model.GetTable<Assignment>().ToObjects<Assignment>()
  592. .GroupBy(x => new { x.Date, Employee = x.EmployeeLink.ID }).ToDictionary(x => x.Key, x => x.ToList());
  593. var daily = timesheets.GroupBy(x => new { x.Date, Employee = x.EmployeeLink.ID }).ToDictionary(x => x.Key, x => x.ToList());
  594. foreach(var (key, sheets) in daily)
  595. {
  596. var dateAssignments = assignments.GetValueOrDefault(new { key.Date, key.Employee }, new List<Assignment>());
  597. // Delegate to script to process raw data.
  598. var rawArgs = new ProcessRawDataArgs(model, key.Employee, key.Date, sheets, dateAssignments);
  599. ProcessRawData(rawArgs);
  600. if (rawArgs.Cancel)
  601. {
  602. foreach(var sheet in sheets)
  603. {
  604. items.AddFailed(sheet, "Post cancelled by script.");
  605. }
  606. continue;
  607. }
  608. // Split each timesheet into its activity blocks.
  609. var activityBlocks = GetActivityBlocks(rawArgs.Assignments, rawArgs.TimeSheets);
  610. // Process activity blocks with script.
  611. var activityArgs = new ProcessActivityBlocksArgs(model, key.Employee, key.Date, activityBlocks);
  612. ProcessActivityBlocks(activityArgs);
  613. if (activityArgs.Cancel)
  614. {
  615. foreach (var sheet in sheets)
  616. {
  617. items.AddFailed(sheet, "Post cancelled by script.");
  618. }
  619. continue;
  620. }
  621. // Add up all the time for the time sheets.
  622. var approvedDuration = rawArgs.TimeSheets.Aggregate(TimeSpan.Zero, (x, y) => x + y.ApprovedDuration);
  623. if(approvedDuration == TimeSpan.Zero)
  624. {
  625. foreach (var sheet in sheets)
  626. {
  627. items.AddFailed(sheet, "Zero Approved Duration");
  628. }
  629. continue;
  630. }
  631. // Convert the activity blocks into LeaveBlocks and PaidWorkBlocks, based on the activity on the block.
  632. var blocks = new List<IBlock>();
  633. foreach (var block in activityArgs.ActivityBlocks)
  634. {
  635. string taskID;
  636. bool isLeave;
  637. if (block.Activity == Guid.Empty
  638. || !_activities.TryGetValue(block.Activity, out var activity))
  639. {
  640. if(block.Activity != Guid.Empty)
  641. {
  642. Logger.Send(LogType.Error, "", $"Error in Timesheet Timberline export: Activity {block.Activity} does not exist!");
  643. }
  644. taskID = "";
  645. isLeave = false;
  646. }
  647. else
  648. {
  649. isLeave = activity.IsLeave;
  650. taskID = activity.PayrollID;
  651. }
  652. if (isLeave)
  653. {
  654. blocks.Add(new LeaveBlock(taskID, block.Finish - block.Start, block.TimeSheet));
  655. }
  656. else
  657. {
  658. // Leave PayID blank until we've worked out the rosters
  659. blocks.Add(new PaidWorkBlock(taskID, block.Finish - block.Start, "", block.TimeSheet.JobLink, block.TimeSheet));
  660. }
  661. }
  662. // Find the roster data.
  663. var employee = employees.GetValueOrDefault(key.Employee);
  664. var employeeRosters = rosters.GetValueOrDefault(employee != null ? employee.ID : Guid.Empty);
  665. var overtimeID = RosterUtils.GetRoster(employeeRosters, employee?.RosterStart, key.Date)?.Overtime.ID ?? Guid.Empty;
  666. // Split up the paid work blocks by the overtime and assign PayrollIDs.
  667. var blockItems = EvaluateOvertime(blocks, overtimeID);
  668. var blockArgs = new ProcessTimeBlocksArgs(model, key.Employee, key.Date, blockItems);
  669. ProcessTimeBlocks(blockArgs);
  670. if (blockArgs.Cancel)
  671. {
  672. foreach (var sheet in sheets)
  673. {
  674. items.AddFailed(sheet, "Post cancelled by script.");
  675. }
  676. continue;
  677. }
  678. // First presumptively succeed all sheets, and then fail them if any of their blocks are failed.
  679. foreach (var sheet in sheets)
  680. {
  681. items.AddSuccess(sheet);
  682. }
  683. var newItems = new List<Tuple<TimesheetTimberlineItem, List<TimeSheet>>>();
  684. // Group the blocks by job, TaskID (activity PayrollID) and PayrollID (from the overtime interval).
  685. foreach(var group in blockItems.GroupBy(x => new { x.Job.ID, x.TaskID, x.PayrollID }))
  686. {
  687. var block = group.ToArray();
  688. var first = block[0];
  689. var item = new TimesheetTimberlineItem
  690. {
  691. Employee = employee?.PayrollID ?? "",
  692. InDate = DateOnly.FromDateTime(key.Date),
  693. Job = first.Job.JobNumber,
  694. Extra = "",
  695. Task = group.Key.TaskID,
  696. Hours = Math.Round(group.Sum(x => x.Duration.TotalHours), 2),
  697. PayID = group.Key.PayrollID
  698. };
  699. var itemArgs = new ProcessItemArgs(model, key.Employee, key.Date, first.Job, item);
  700. ProcessItem(itemArgs);
  701. var blockTimeSheets = block.Select(x => x.TimeSheet).ToList();
  702. if (!itemArgs.Cancel)
  703. {
  704. newItems.Add(new(itemArgs.Item, blockTimeSheets));
  705. }
  706. else
  707. {
  708. foreach(var sheet in blockTimeSheets)
  709. {
  710. (sheet as IPostable).FailPost("Post cancelled by script.");
  711. }
  712. }
  713. }
  714. foreach(var item in newItems)
  715. {
  716. if(item.Item2.All(x => x.PostedStatus == PostedStatus.Posted))
  717. {
  718. items.AddItem(item.Item1);
  719. }
  720. }
  721. }
  722. items.Sort();
  723. return items;
  724. }
  725. public IPostResult<TimeSheet> Process(IDataModel<TimeSheet> model)
  726. {
  727. var items = DoProcess(model);
  728. var dlg = new SaveFileDialog()
  729. {
  730. Filter = "CSV Files (*.csv)|*.csv"
  731. };
  732. if (dlg.ShowDialog() == true)
  733. {
  734. using var writer = new StreamWriter(dlg.FileName);
  735. using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
  736. foreach (var item in items.Items)
  737. {
  738. csv.WriteRecord(item);
  739. csv.NextRecord();
  740. }
  741. return items;
  742. }
  743. else
  744. {
  745. throw new PostCancelledException();
  746. }
  747. }
  748. public void AfterPost(IDataModel<TimeSheet> model, IPostResult<TimeSheet> result)
  749. {
  750. Script?.Execute(methodname: "AfterPost", parameters: new object[] { model });
  751. }
  752. }
  753. public class TimesheetTimberlinePosterEngine<T> : TimberlinePosterEngine<TimeSheet, TimesheetTimberlineSettings>
  754. {
  755. }
  756. }