Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for user journey when calculating days booked per week #633

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ functionality to PhpReport
*/
define ('YEARLY_HOLIDAY_HOURS', 184);

/**
* @name STANDARD_WORKING_DAY
* @global int standard working day length (i.e., journey)
*/
define ('STANDARD_WORKING_DAY', 8);

/**
* @name ALL_USERS_GROUP
* @global string users group used for retrieving all users
Expand Down
7 changes: 7 additions & 0 deletions config/config.template
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ define('DAO_BACKEND', 'PostgreSQL');
*/
define ('YEARLY_HOLIDAY_HOURS', 184);

/**
* @name STANDARD_WORKING_DAY
* @global int standard working day length (i.e., journey)
*/
define ('STANDARD_WORKING_DAY', 8);


/**
* @name ALL_USERS_GROUP
* @global string users group used for retrieving all users
Expand Down
7 changes: 7 additions & 0 deletions config/config.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ functionality to PhpReport
*/
define ('YEARLY_HOLIDAY_HOURS', 184);

/**
* @name STANDARD_WORKING_DAY
* @global int standard working day length (i.e., journey)
*/
define ('STANDARD_WORKING_DAY', 8);


/**
* @name ALL_USERS_GROUP
* @global string users group used for retrieving all users
Expand Down
2 changes: 1 addition & 1 deletion model/facade/action/GetHolidaySummaryReportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function doExecute()
);
$validJourney = array_pop($validJourney);
$validJourney = $validJourney ? $validJourney->getJourney() : 0;
$leaves = HolidayService::groupByWeeks($leaves, $this->weeks);
$leaves = HolidayService::groupByWeeks($leaves, $this->weeks, $journeyHistories);
if (count($leaves) == 0) {
$leaves = $this->weeks;
}
Expand Down
56 changes: 53 additions & 3 deletions tests/unit/web/services/HolidayServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,19 @@ public function testGroupByWeeksSameYear(): void
"end" => 420,
]
];
$journey = new JourneyHistoryVO();
$journey->setInitDate(DateTime::createFromFormat('Y-m-d', '2021-01-01'));
$journey->setEndDate(DateTime::createFromFormat('Y-m-d', '2021-12-31'));
$journey->setJourney(7.0);
$journeyHistories[] = array();
$journeyHistories[0] = $journey;
$result = [
'2021W41' => 2,
'2021W44' => 1
];
$this->assertEquals(
$result,
$this->instance::groupByWeeks($dates)
$this->instance::groupByWeeks($dates, $journeyHistories)
);
}

Expand All @@ -198,13 +204,51 @@ public function testGroupByWeeksWithPartialLeaves(): void
"end" => 420,
]
];
$journey = new JourneyHistoryVO();
$journey->setInitDate(DateTime::createFromFormat('Y-m-d', '2021-01-01'));
$journey->setEndDate(DateTime::createFromFormat('Y-m-d', '2021-12-31'));
$journey->setJourney(7.0);
$journeyHistories[] = array();
$journeyHistories[0] = $journey;
$result = [
'2021W41' => 1.5,
'2021W44' => 1
];
$this->assertEquals(
$result,
$this->instance::groupByWeeks($dates)
$this->instance::groupByWeeks($dates, $journeyHistories)
);
}

public function testGroupByWeeksWithExtraLeaves(): void
{
$dates = [
"2021-10-14" => [
"init" => 0,
"end" => 336,
],
"2021-10-15" => [
"init" => 0,
"end" => 336,
],
"2021-11-01" => [
"init" => 0,
"end" => 420,
]
];
$journey = new JourneyHistoryVO();
$journey->setInitDate(DateTime::createFromFormat('Y-m-d', '2021-01-01'));
$journey->setEndDate(DateTime::createFromFormat('Y-m-d', '2021-12-31'));
$journey->setJourney(5.6);
$journeyHistories[] = array();
$journeyHistories[0] = $journey;
$result = [
'2021W41' => 2,
'2021W44' => 1.25
];
$this->assertEquals(
$result,
$this->instance::groupByWeeks($dates, $journeyHistories)
);
}

Expand All @@ -230,9 +274,15 @@ public function testGroupByWeeksWithDateInPastYearISOWeek(): void
'2020W53' => 2,
'2021W44' => 1
];
$journey = new JourneyHistoryVO();
$journey->setInitDate(DateTime::createFromFormat('Y-m-d', '2021-01-01'));
$journey->setEndDate(DateTime::createFromFormat('Y-m-d', '2021-12-31'));
$journey->setJourney(7.0);
$journeyHistories[] = array();
$journeyHistories[0] = $journey;
$this->assertEquals(
$result,
$this->instance::groupByWeeks($dates)
$this->instance::groupByWeeks($dates, $journeyHistories)
);
}

Expand Down
25 changes: 25 additions & 0 deletions web/js/holidayManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ function formatMinutesToHours(minutes) {
return minutesLeft > 0 ? hours + minutesLeft + "m" : hours;
}

function formatMinutesToDecimal(minutes){
let hours = (minutes / 60).toFixed(1);
return hours;
}

function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
Expand Down Expand Up @@ -259,6 +264,26 @@ var app = new Vue({
coveredDates: [d],
})
}
else {
const durationInDecimal = formatMinutesToDecimal(datesAndRanges.dates[d].end - datesAndRanges.dates[d].init);
const duration = formatMinutesToHours(datesAndRanges.dates[d].end - datesAndRanges.dates[d].init);
const journeyDuration = formatMinutesToHours(datesAndRanges.journey * 60);
if (durationInDecimal > datesAndRanges.journey)
{
datesAndRanges.dates[d].hoursOver = (durationInDecimal - datesAndRanges.journey).toFixed(1);
attributes.push({
highlight: {
color: 'red',
fillMode: 'light',
},
dates: new Date(d + 'T00:00:00'),
popover: {
label: `Vacation hours (${duration}) longer than workday hours (${journeyDuration})`
},
coveredDates: [d],
})
}
}
});
return {
ranges: attributes,
Expand Down
21 changes: 17 additions & 4 deletions web/services/HolidayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
include_once(PHPREPORT_ROOT . '/model/facade/UsersFacade.php');
include_once(PHPREPORT_ROOT . '/model/vo/UserVO.php');
require_once(PHPREPORT_ROOT . '/util/LoginManager.php');
include_once(PHPREPORT_ROOT . '/util/ConfigurationParametersManager.php');

class HolidayService
{
Expand Down Expand Up @@ -177,18 +178,24 @@ static function getWeeksFromYear($year = NULL): array
return $weeks;
}

static function groupByWeeks(array $leavesDetails, $weeks = []): array
static function groupByWeeks(array $leavesDetails, array $journeyHistories, $weeks = []) : array
{
if (count($leavesDetails) == 0) return [];
$dates = array_keys($leavesDetails);
$previous_week = date("o\WW", strtotime($dates[0]));
$weeks[$previous_week] = $leavesDetails[$dates[0]]['amount'] ?? 1;
for ($i = 1; $i < count($dates); $i++) {
$currentJourney= array_filter($journeyHistories, fn($history) => $history->dateBelongsToHistory(date_create($dates[$i])))[0];
$journey = $currentJourney->getJourney() ?? \ConfigurationParametersManager::getParameter('STANDARD_WORKING_DAY');
$current_week = date("o\WW", strtotime($dates[$i]));
if ($current_week == $previous_week) {
$weeks[$current_week] += $leavesDetails[$dates[$i]]['amount'] ?? 1;
$leaveInDays = ($leavesDetails[$dates[$i]]['end'] - $leavesDetails[$dates[$i]]['init']) / 60;
$leavePerDayBasedOnJourney = $leaveInDays / $journey;
$weeks[$current_week] += $leavePerDayBasedOnJourney ?? 1;
} else {
$weeks[$current_week] = $leavesDetails[$dates[$i]]['amount'] ?? 1;
$leaveInDays = ($leavesDetails[$dates[$i]]['end'] - $leavesDetails[$dates[$i]]['init']) / 60;
$leavePerDayBasedOnJourney = $leaveInDays / $journey;
$weeks[$current_week] = $leavePerDayBasedOnJourney ?? 1;
$previous_week = $current_week;
}
$weeks[$current_week] = round($weeks[$current_week], 2);
Expand Down Expand Up @@ -250,10 +257,16 @@ public function getUserVacationsRanges(string $init = NULL, string $end = NULL,
$vacations = \UsersFacade::GetScheduledHolidays($init, $end, $userVO);
$vacations = $this::mapHalfLeaves($vacations, $journeyHistories);

for ($i = 1; $i < count($vacations); $i++){
$currentJourney = array_filter($journeyHistories, fn($history) => $history->dateBelongsToHistory(date_create($vacations[$i])))[0];
}

$journey = $currentJourney->getJourney() ?? \ConfigurationParametersManager::getParameter('STANDARD_WORKING_DAY');
return [
'dates' => $vacations,
'ranges' => $this->datesToRanges(array_keys($vacations)),
'weeks' => $this->groupByWeeks($vacations)
'weeks' => $this->groupByWeeks($vacations, $journeyHistories),
'journey' => $journey
];
}

Expand Down