Skip to content

Commit

Permalink
[fix] disabling adding time registrations in previous timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed May 2, 2024
1 parent deb51f6 commit ab40097
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
17 changes: 10 additions & 7 deletions app/Domain/Timesheets/Controllers/ShowMy.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,16 @@ public function saveTimeSheet(array $postData): void
"kind" => $kind,
);

try {
$this->timesheetService->upsertTime($ticketId, $values);
$this->tpl->setNotification("Timesheet saved successfully", "success", "save_timesheet");
} catch (\Exception $e) {
$this->tpl->setNotification("Error logging time: " . $e->getMessage(), "error", "save_timesheet");
error_log($e);
continue;
//This should not be the case since we set the input to disabled, but check anyways
if($timestamp !== "false" && $timestamp != false) {
try {
$this->timesheetService->upsertTime($ticketId, $values);
$this->tpl->setNotification("Timesheet saved successfully", "success", "save_timesheet");
} catch (\Exception $e) {
$this->tpl->setNotification("Error logging time: " . $e->getMessage(), "error", "save_timesheet");
error_log($e);
continue;
}
}
}
}
Expand Down
55 changes: 38 additions & 17 deletions app/Domain/Timesheets/Services/Timesheets.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,13 @@ public function getWeeklyTimesheets(int $projectId, CarbonInterface $fromDate, i

// Various Entries can be in different timezones and thus would not be caught by upsert or grouping by
// default Creating new rows for each timezone adjustment
//to avoid timezone collisions we disable adding new times to rows that were created in an different timezone
$timezonedTime = $currentWorkDate->format("H:i:s");

$groupKey = $timesheet["ticketId"] . "-" . $timesheet["kind"] . "-" . $timezonedTime;

if (!isset($timesheetGroups[$groupKey])) {
// Build an array of 7 days for the weekly timesheet. Include the start date of the current users
// timezone in UTC. That way we can compare the dates coming from the db and add the actual time entry
// to the correct date in our prepared line

// timezone in UTC. That way we can compare the dates coming from the db
$timesheetGroups[$groupKey] = array(
"kind" => $timesheet["kind"],
"clientName" => $timesheet["clientName"],
Expand All @@ -340,75 +338,98 @@ public function getWeeklyTimesheets(int $projectId, CarbonInterface $fromDate, i
"day1" => array(
"start" => $fromDate,
"end" => $fromDate->addHours(23)->addMinutes(59),
"actualWorkDate" => $fromDate->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second),
"actualWorkDate" => $workdateOffsetStart === 0 ? $fromDate->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second) : "",

"hours" => 0,
"description" => "",
),
"day2" => array(
"start" => $fromDate->addDays(1),
"end" => $fromDate->addDays(1)->addHours(23)->addMinutes(59),
"actualWorkDate" => $fromDate->addDays(1)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second),
"actualWorkDate" => $workdateOffsetStart === 0 ? $fromDate->addDays(1)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second) : "",
"hours" => 0,
"description" => "",
),
"day3" => array(
"start" => $fromDate->addDays(2),
"end" => $fromDate->addDays(2)->addHours(23)->addMinutes(59),
"actualWorkDate" => $fromDate->addDays(2)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second),
"actualWorkDate" => $workdateOffsetStart === 0 ? $fromDate->addDays(2)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second) : "",
"hours" => 0,
"description" => "",
),
"day4" => array(
"start" => $fromDate->addDays(3),
"end" => $fromDate->addDays(3)->addHours(23)->addMinutes(59),
"actualWorkDate" => $fromDate->addDays(3)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second),
"actualWorkDate" => $workdateOffsetStart === 0 ? $fromDate->addDays(3)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second) : "",
"hours" => 0,
"description" => "",
),
"day5" => array(
"start" => $fromDate->addDays(4),
"end" => $fromDate->addDays(4)->addHours(23)->addMinutes(59),
"actualWorkDate" => $fromDate->addDays(4)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second),
"actualWorkDate" => $workdateOffsetStart === 0 ? $fromDate->addDays(4)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second) : "",
"hours" => 0,
"description" => "",
),
"day6" => array(
"start" => $fromDate->addDays(5),
"end" => $fromDate->addDays(5)->addHours(23)->addMinutes(59),
"actualWorkDate" => $fromDate->addDays(5)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second),
"actualWorkDate" => $workdateOffsetStart === 0 ? $fromDate->addDays(5)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second) : "",
"hours" => 0,
"description" => "",
),
"day7" => array(
"start" => $fromDate->addDays(6),
"end" => $fromDate->addDays(6)->addHours(23)->addMinutes(59),
"actualWorkDate" => $fromDate->addDays(6)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second),
"actualWorkDate" => $workdateOffsetStart === 0 ? $fromDate->addDays(6)->setTime($currentWorkDate->hour, $currentWorkDate->minute, $currentWorkDate->second) : "",
"hours" => 0,
"description" => "",
),
"rowSum" => 0,
);
}

//Take the date from the db and add the hours to the appropriate day in our week array
// Check if timesheet entry falls within the day range of the weekly grouped timesheets that we are trying
// to pull up.
//
// Why would that be different you might ask?
//
// If a user adds time entries and then changes timezones (even just 1 hour) the values in the db
// will be different since it is based on start of the day 00:00:00 in the current users timezone and then
// stored as UTC timezone shoifted value in the db.
// If the value is not exact but falls within the time period we're adding a new row
for ($i = 1; $i < 8; $i++) {
if ($timesheetGroups[$groupKey]["day" . $i]['actualWorkDate'] == $currentWorkDate) {

//It should be impossible for 2 entries in the db to have the same group key given the unique
// index on the date + ticketid + type.
// but just in case add up the hours. A save will merge the entries later.
$start = $timesheetGroups[$groupKey]["day" . $i]["start"];
$end = $timesheetGroups[$groupKey]["day" . $i]["end"];

if ($currentWorkDate->gte($start) && $currentWorkDate->lt($end)) {
$timesheetGroups[$groupKey]["day" . $i]['hours'] += $timesheet['hours'];
$timesheetGroups[$groupKey]["day" . $i]['actualWorkDate'] = $currentWorkDate;
$timesheetGroups[$groupKey]["day" . $i]['description'] = $timesheet['description'];

// No need to check further, we found what we came for
break;
}
}

/*for ($i = 1; $i < 8; $i++) {
if ($timesheetGroups[$groupKey]["day" . $i]['actualWorkDate'] == $currentWorkDate) {
$timesheetGroups[$groupKey]["day" . $i]['hours'] += $timesheet['hours'];
$timesheetGroups[$groupKey]["day" . $i]['description'] = $timesheet['description'];
// No need to check further, we found what we came for
break;
}
}*/




// Add to rowsum
$timesheetGroups[$groupKey]["rowSum"] += $timesheet['hours'];
}



return $timesheetGroups;
}

Expand Down
17 changes: 11 additions & 6 deletions app/Domain/Timesheets/Templates/showMy.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,10 @@
<?php
$i = 0;
foreach ($days as $day) { ?>
<th class="<?php if ($dateFrom->isToday()) {
<th class="<?php if ($dateFrom->addDays($i)->setToUserTimezone()->isToday()) {
echo "active";
} ?>"><?php echo $day ?><br />
} ?>
"><?php echo $day ?><br />
<?php

echo $dateFrom->addDays($i)->formatDateForUser();
Expand Down Expand Up @@ -293,7 +294,7 @@
<?php echo $tpl->__($tpl->get('kind')[$timeRow['kind']]); ?>
<?php if ($timeRow['hasTimesheetOffset']) { ?>
<i class="fa-solid fa-clock-rotate-left pull-right label-blue"
data-tippy-content="This entry was likely created using a different timezone">
data-tippy-content="This entry was likely created using a different timezone. Only existing entries can be updated in this timezone">
</i>
<?php } ?>
</td>
Expand All @@ -304,19 +305,23 @@

<td width="7%" class="row<?php
echo $dayKey;
if ($timeRow[$dayKey]["actualWorkDate"]->setToUserTimezone()->isToday()) {
if ($timeRow[$dayKey]["start"]->setToUserTimezone()->isToday()) {
echo " active";
}
?>">


<?php
$inputNameKey = $timeRow["ticketId"] . "|" . $timeRow["kind"] . "|" . $timeRow[$dayKey]["actualWorkDate"]->formatDateForUser() . "|" . $timeRow[$dayKey]["actualWorkDate"]->getTimestamp();
$inputNameKey = $timeRow["ticketId"] . "|" . $timeRow["kind"] . "|" . ($timeRow[$dayKey]["actualWorkDate"] ? $timeRow[$dayKey]["actualWorkDate"]->formatDateForUser() : "false" ). "|" . ($timeRow[$dayKey]["actualWorkDate"] ? $timeRow[$dayKey]["actualWorkDate"]->getTimestamp() : "false");
?>
<input type="text"
class="hourCell"
<?php if(empty($timeRow[$dayKey]["actualWorkDate"])) echo "disabled='disabled'"; ?>
name="<?php echo $inputNameKey ?>"
value="<?php echo $timeRow[$dayKey]["hours"]; ?>"
<?php if(empty($timeRow[$dayKey]["actualWorkDate"])) { ?>
data-tippy-content="Cannot add time entry in previous timezone"
<?php } ?>
/>

<?php if (!empty($timeRow[$dayKey]["description"])) {?>
Expand Down Expand Up @@ -418,7 +423,7 @@ class="hourCell"
</tfoot>
</table>
<div class="right">
<input type="submit" name="saveTimeSheet" value="Save"/>
<input type="submit" name="saveTimeSheet" value="Save" />
</div>
<div class="clearall"></div>
</form>
Expand Down
4 changes: 2 additions & 2 deletions public/assets/css/components/forms.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ select, textarea, input[type="text"], input[type="password"], input[type="dateti
color:var(--primary-font-color);
}

input:read-only,
input[type="text"]:read-only,
input:disabled,
.btn-primary[disabled]{
border:none;
box-shadow:none;
padding-left: 0px;
opacity:0.4;
}

body input:focus {
Expand Down

0 comments on commit ab40097

Please sign in to comment.