Skip to content

Commit 6a998d2

Browse files
committed
Adding changes to support the new CalculateHoursSummary.
1 parent b006600 commit 6a998d2

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn/Controllers/TimePlanningWorkingHoursController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
using System.Text;
2323
using Microsoft.AspNetCore.Authorization;
2424
using Microsoft.AspNetCore.Http;
25+
using Microsoft.AspNetCore.Routing;
2526
using TimePlanning.Pn.Infrastructure.Models.WorkingHours;
2627

2728
namespace TimePlanning.Pn.Controllers
@@ -68,6 +69,13 @@ public async Task<OperationDataResult<TimePlanningWorkingHourSimpleModel>> Read(
6869
return await _workingHoursService.ReadSimple(dateTime);
6970
}
7071

72+
[HttpGet]
73+
[Route("calculate-hours-summary")]
74+
public async Task<OperationDataResult<TimePlanningHoursSummaryModel>> CalculateHoursSummary(DateTime startDate, DateTime endDate)
75+
{
76+
return await _workingHoursService.CalculateHoursSummary(startDate, endDate);
77+
}
78+
7179
[HttpPut]
7280
public async Task<OperationResult> Update([FromBody] TimePlanningWorkingHoursUpdateCreateModel model)
7381
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace TimePlanning.Pn.Infrastructure.Models.WorkingHours.Index;
2+
3+
public class TimePlanningHoursSummaryModel
4+
{
5+
public double TotalPlanHours { get; set; }
6+
public double TotalNettoHours { get; set; }
7+
public double Difference { get; set; }
8+
}

eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn/Services/TimePlanningWorkingHoursService/ITimePlanningWorkingHoursService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ public interface ITimePlanningWorkingHoursService
4848
Task<OperationResult> UpdateWorkingHour(int sdkSiteId, TimePlanningWorkingHoursUpdateModel model, string token);
4949

5050
Task<OperationDataResult<TimePlanningWorkingHourSimpleModel>> ReadSimple(DateTime dateTime);
51+
52+
Task<OperationDataResult<TimePlanningHoursSummaryModel>> CalculateHoursSummary(DateTime startDate, DateTime endDate);
5153
}
5254
}

eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn/Services/TimePlanningWorkingHoursService/TimePlanningWorkingHoursService.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,58 @@ public async Task<OperationDataResult<TimePlanningWorkingHourSimpleModel>> ReadS
560560
timePlanningWorkingHoursModel);
561561
}
562562

563+
public async Task<OperationDataResult<TimePlanningHoursSummaryModel>> CalculateHoursSummary(DateTime startDate,
564+
DateTime endDate)
565+
{
566+
try
567+
{
568+
// Adjust startDate to 00:00:00 and endDate to 23:59:59
569+
startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, 0, 0, 0);
570+
endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, 23, 59, 59);
571+
572+
var core = await coreHelper.GetCore();
573+
var sdkContext = core.DbContextHelper.GetDbContext();
574+
var currentUser = await userService.GetCurrentUserAsync();
575+
var fullName = currentUser.FirstName.Trim() + " " + currentUser.LastName.Trim();
576+
var sdkSite = await sdkContext.Sites.SingleOrDefaultAsync(x =>
577+
x.Name.Replace(" ", "") == fullName.Replace(" ", "") &&
578+
x.WorkflowState != Constants.WorkflowStates.Removed);
579+
580+
if (sdkSite == null)
581+
{
582+
return new OperationDataResult<TimePlanningHoursSummaryModel>(false, "Site not found", null);
583+
}
584+
585+
var planRegistrations = await dbContext.PlanRegistrations
586+
.Where(x => x.Date >= startDate && x.Date <= endDate)
587+
.Where(x => x.SdkSitId == sdkSite.MicrotingUid)
588+
.Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
589+
.ToListAsync();
590+
591+
var totalPlanHours = planRegistrations.Sum(x => x.PlanHours);
592+
var totalNettoHours = planRegistrations.Sum(x => x.NettoHours);
593+
var difference = totalNettoHours - totalPlanHours;
594+
595+
var summary = new TimePlanningHoursSummaryModel
596+
{
597+
TotalPlanHours = totalPlanHours,
598+
TotalNettoHours = totalNettoHours,
599+
Difference = difference
600+
};
601+
602+
return new OperationDataResult<TimePlanningHoursSummaryModel>(true, summary);
603+
}
604+
catch (Exception e)
605+
{
606+
SentrySdk.CaptureException(e);
607+
logger.LogError(e.Message);
608+
logger.LogTrace(e.StackTrace);
609+
return new OperationDataResult<TimePlanningHoursSummaryModel>(false,
610+
"Error while calculating hours summary");
611+
}
612+
}
613+
614+
563615
private static double RoundToTwoDecimalPlaces(double value)
564616
{
565617
double roundedValue = Math.Round(value, 2);

0 commit comments

Comments
 (0)