Skip to content
This repository has been archived by the owner on Feb 8, 2025. It is now read-only.

fix: fix Carbon::now() calls in loops #555

Merged
merged 4 commits into from
Feb 14, 2021
Merged
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
2 changes: 1 addition & 1 deletion app/Helpers/BirthdayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class BirthdayHelper
*/
public static function isBirthdayInXDays(Carbon $startDate, Carbon $birthdate, int $numberOfDays): bool
{
$future = $startDate->addDays($numberOfDays);
$future = $startDate->copy()->addDays($numberOfDays);
$birthdate->year = $startDate->year;

if ($birthdate->isPast()) {
Expand Down
5 changes: 3 additions & 2 deletions app/Helpers/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ public static function determineDateStatus(Carbon $date): string
*/
public static function hoursOrDaysLeft(Carbon $date): string
{
$hoursLeft = Carbon::now()->diffInHours($date);
$now = Carbon::now();
$hoursLeft = $now->diffInHours($date);

if ($hoursLeft < 24) {
$timeLeft = trans_choice('app.hours_left', $hoursLeft);
} else {
$timeLeft = trans_choice('app.days_left', Carbon::now()->diffInDays($date));
$timeLeft = trans_choice('app.days_left', $now->diffInDays($date));
}

return $timeLeft;
Expand Down
16 changes: 9 additions & 7 deletions app/Http/ViewHelpers/Company/CompanyViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,21 @@ public static function birthdaysThisWeek(Company $company): array
->select('id', 'first_name', 'last_name', 'avatar', 'birthdate')
->get();

$now = Carbon::now();
$minDate = $now->copy()->startOfWeek(Carbon::MONDAY);
$maxDate = $now->copy()->endOfWeek(Carbon::SUNDAY);

$birthdaysCollection = collect([]);
foreach ($employees as $employee) {
$birthdateWithCurrentYear = $employee->birthdate->copy()->year(Carbon::now()->year);

$minDate = Carbon::now()->startOfWeek(Carbon::MONDAY);
$maxDate = Carbon::now()->endOfWeek(Carbon::SUNDAY);
$birthdateWithCurrentYear = $employee->birthdate->copy()->year($now->year);

if (BirthdayHelper::isBirthdayInRange($birthdateWithCurrentYear, $minDate, $maxDate)) {
$birthdaysCollection->push([
'id' => $employee->id,
'name' => $employee->name,
'avatar' => $employee->avatar,
'birthdate' => DateHelper::formatMonthAndDay($birthdateWithCurrentYear),
'sort_key' => Carbon::createFromDate(Carbon::now()->year, $birthdateWithCurrentYear->month, $birthdateWithCurrentYear->day)->format('Y-m-d'),
'sort_key' => Carbon::createFromDate($now->year, $birthdateWithCurrentYear->month, $birthdateWithCurrentYear->day)->format('Y-m-d'),
'url' => route('employees.show', [
'company' => $company,
'employee' => $employee->id,
Expand All @@ -133,12 +134,13 @@ public static function birthdaysThisWeek(Company $company): array
*/
public static function newHiresThisWeek(Company $company): Collection
{
$now = Carbon::now();
$employees = $company->employees()
->select('id', 'first_name', 'last_name', 'avatar', 'hired_at', 'position_id')
->where('locked', false)
->whereNotNull('hired_at')
->whereDate('hired_at', '>=', Carbon::now()->startOfWeek(Carbon::MONDAY))
->whereDate('hired_at', '<=', Carbon::now()->endOfWeek(Carbon::SUNDAY))
->whereDate('hired_at', '>=', $now->copy()->startOfWeek(Carbon::MONDAY))
->whereDate('hired_at', '<=', $now->copy()->endOfWeek(Carbon::SUNDAY))
->with('position')
->orderBy('hired_at', 'asc')
->get();
Expand Down
5 changes: 3 additions & 2 deletions app/Http/ViewHelpers/Dashboard/DashboardHRViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ public static function employeesWithoutManagersWithPendingTimesheets(Company $co

public static function statisticsAboutTimesheets(Company $company)
{
$now = Carbon::now();
$totals = DB::table('timesheets')
->whereDate('started_at', '>=', Carbon::now()->startOfWeek(Carbon::MONDAY)->subDays(30))
->whereDate('started_at', '<', Carbon::now()->startOfWeek(Carbon::MONDAY))
->whereDate('started_at', '>=', $now->copy()->startOfWeek(Carbon::MONDAY)->subDays(30))
->whereDate('started_at', '<', $now->copy()->startOfWeek(Carbon::MONDAY))
->selectRaw('count(*) as total')
->selectRaw("count(case when status = '".Timesheet::REJECTED."' then 1 end) as rejected")
->first();
Expand Down
10 changes: 6 additions & 4 deletions app/Http/ViewHelpers/Dashboard/DashboardManagerViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public static function oneOnOnes(Employee $manager, Collection $directReports):
{
$oneOnOnesCollection = collect([]);
$company = $manager->company;
$now = Carbon::now();

foreach ($directReports as $directReport) {
$employee = $directReport->directReport;
Expand All @@ -132,7 +133,7 @@ public static function oneOnOnes(Employee $manager, Collection $directReports):
'author_id' => $employee->id,
'manager_id' => $manager->id,
'employee_id' => $employee->id,
'date' => Carbon::now()->format('Y-m-d'),
'date' => $now->format('Y-m-d'),
]);
}

Expand Down Expand Up @@ -171,6 +172,7 @@ public static function contractRenewals(Employee $manager, Collection $directRep
{
$collection = collect([]);
$company = $manager->company;
$now = Carbon::now();

foreach ($directReports as $directReport) {
$employee = $directReport->directReport;
Expand All @@ -187,7 +189,7 @@ public static function contractRenewals(Employee $manager, Collection $directRep
continue;
}

$dateInOneMonth = Carbon::now()->addMonths(1);
$dateInOneMonth = $now->copy()->addMonths(1);

if ($employee->contract_renewed_at->isAfter($dateInOneMonth)) {
continue;
Expand All @@ -204,8 +206,8 @@ public static function contractRenewals(Employee $manager, Collection $directRep
]),
'contract_information' => [
'contract_renewed_at' => DateHelper::formatDate($employee->contract_renewed_at),
'number_of_days' => $employee->contract_renewed_at->diffInDays(Carbon::now()),
'late' => $employee->contract_renewed_at->isBefore(Carbon::now()),
'number_of_days' => $employee->contract_renewed_at->diffInDays($now),
'late' => $employee->contract_renewed_at->isBefore($now),
],
]);
}
Expand Down
12 changes: 7 additions & 5 deletions app/Http/ViewHelpers/Dashboard/DashboardMeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public static function oneOnOnes(Employee $employee): Collection
$managers = $employee->getListOfManagers();
$company = $employee->company;
$managersCollection = collect([]);
$now = Carbon::now();

foreach ($managers as $manager) {
// for each manager, we need to check if there is an active one on
Expand All @@ -227,7 +228,7 @@ public static function oneOnOnes(Employee $employee): Collection
'author_id' => $employee->id,
'manager_id' => $manager->id,
'employee_id' => $employee->id,
'date' => Carbon::now()->format('Y-m-d'),
'date' => $now->format('Y-m-d'),
]);
}

Expand Down Expand Up @@ -274,23 +275,24 @@ public static function contractRenewal(Employee $employee): ?array
return null;
}

$dateInOneMonth = Carbon::now()->addMonths(1);
$now = Carbon::now();
$dateInOneMonth = $now->copy()->addMonths(1);

if ($employee->contract_renewed_at->isAfter($dateInOneMonth)) {
return null;
}

if ($employee->contract_renewed_at->isBefore(Carbon::now())) {
if ($employee->contract_renewed_at->isBefore($now)) {
return [
'contract_renewed_at' => DateHelper::formatDate($employee->contract_renewed_at),
'number_of_days' => $employee->contract_renewed_at->diffInDays(Carbon::now()),
'number_of_days' => $employee->contract_renewed_at->diffInDays($now),
'late' => true,
];
}

return [
'contract_renewed_at' => DateHelper::formatDate($employee->contract_renewed_at),
'number_of_days' => $employee->contract_renewed_at->diffInDays(Carbon::now()),
'number_of_days' => $employee->contract_renewed_at->diffInDays($now),
'late' => false,
];
}
Expand Down
28 changes: 17 additions & 11 deletions app/Http/ViewHelpers/Dashboard/DashboardTeamViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public static function birthdays(Team $team): array

// build the collection of data
$birthdaysCollection = collect([]);
$now = Carbon::now();

foreach ($employees as $employee) {
if (! $employee->birthdate) {
continue;
}

if (BirthdayHelper::isBirthdayInXDays(Carbon::now(), $employee->birthdate, 30)) {
if (BirthdayHelper::isBirthdayInXDays($now, $employee->birthdate, 30)) {
$birthdaysCollection->push([
'id' => $employee->id,
'url' => route('employees.show', [
Expand All @@ -51,7 +53,7 @@ public static function birthdays(Team $team): array
'name' => $employee->name,
'avatar' => $employee->avatar,
'birthdate' => DateHelper::formatMonthAndDay($employee->birthdate),
'sort_key' => Carbon::createFromDate(Carbon::now()->year, $employee->birthdate->month, $employee->birthdate->day)->format('Y-m-d'),
'sort_key' => Carbon::createFromDate($now->year, $employee->birthdate->month, $employee->birthdate->day)->format('Y-m-d'),
]);
}
}
Expand Down Expand Up @@ -80,8 +82,10 @@ public static function workFromHome(Team $team): Collection
});

$workFromHomeCollection = collect([]);
$now = Carbon::now();

foreach ($employees as $employee) {
if (! WorkFromHomeHelper::hasWorkedFromHomeOnDate($employee, Carbon::now())) {
if (! WorkFromHomeHelper::hasWorkedFromHomeOnDate($employee, $now)) {
continue;
}

Expand Down Expand Up @@ -227,8 +231,9 @@ public static function worklogs(Team $team, Carbon $date): array
*/
public static function upcomingNewHires(Team $team): Collection
{
$currentDay = Carbon::now()->format('Y-m-d');
$nextSunday = Carbon::now()->addWeek()->endOfWeek(Carbon::SUNDAY)->format('Y-m-d');
$now = Carbon::now();
$currentDay = $now->format('Y-m-d');
$nextSunday = $now->copy()->addWeek()->endOfWeek(Carbon::SUNDAY)->format('Y-m-d');

$employees = $team->employees()
->where('locked', false)
Expand Down Expand Up @@ -282,12 +287,13 @@ public static function upcomingHiredDateAnniversaries(Team $team): Collection
return ! $employee->hired_at->isCurrentYear();
});

$nextMonday = Carbon::now()->format('Y-m-d');
$nextSunday = Carbon::now()->addWeek()->endOfWeek(Carbon::SUNDAY)->format('Y-m-d');
$now = Carbon::now();
$nextMonday = $now->format('Y-m-d');
$nextSunday = $now->copy()->addWeek()->endOfWeek(Carbon::SUNDAY)->format('Y-m-d');
$upcomingWeek = CarbonPeriod::create($nextMonday, $nextSunday);

$employees = $employees->filter(function ($employee) use ($upcomingWeek) {
return $upcomingWeek->contains($employee->hired_at->setYear(Carbon::now()->year)->format('Y-m-d'));
$employees = $employees->filter(function ($employee) use ($upcomingWeek, $now) {
return $upcomingWeek->contains($employee->hired_at->setYear($now->year)->format('Y-m-d'));
});

$employeesCollection = collect([]);
Expand All @@ -296,8 +302,8 @@ public static function upcomingHiredDateAnniversaries(Team $team): Collection
'id' => $employee->id,
'name' => $employee->name,
'avatar' => $employee->avatar,
'anniversary_date' => DateHelper::formatDayAndMonthInParenthesis($employee->hired_at->setYear(Carbon::now()->year)),
'anniversary_age' => Carbon::now()->year - $employee->hired_at->year,
'anniversary_date' => DateHelper::formatDayAndMonthInParenthesis($employee->hired_at->setYear($now->year)),
'anniversary_age' => $now->year - $employee->hired_at->year,
'url' => route('employees.show', [
'company' => $employee->company,
'employee' => $employee,
Expand Down
7 changes: 4 additions & 3 deletions app/Http/ViewHelpers/Employee/EmployeeOneOnOneViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ class EmployeeOneOnOneViewHelper
*/
public static function stats(Collection $entries): array
{
$entriesLast365Days = $entries->filter(function ($entry) {
return $entry->happened_at > Carbon::now()->subYear();
$now = Carbon::now();
$entriesLast365Days = $entries->filter(function ($entry) use ($now) {
return $entry->happened_at > $now->copy()->subYear();
});

// now calculating the average number of days between one on ones
$previousEntry = Carbon::now();
$previousEntry = $now;
$average = collect([]);
foreach ($entriesLast365Days as $entry) {
$numberOfDays = $previousEntry->diffInDays($entry->happened_at);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public static function latestRateYourManagerSurveys(Employee $employee): ?array
// the manager the date of the next survey
$survey = $surveysToDisplay->first();
if (! $survey->active) {
$now = Carbon::now();
$surveysCollection->push([
'id' => null,
'month' => Carbon::now()->format('M Y'),
'deadline' => DateHelper::hoursOrDaysLeft(Carbon::now()->endOfMonth()),
'month' => $now->format('M Y'),
'deadline' => DateHelper::hoursOrDaysLeft($now->copy()->endOfMonth()),
]);
}

Expand Down
5 changes: 3 additions & 2 deletions app/Http/ViewHelpers/Employee/EmployeeShowViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,12 @@ public static function worklogs(Employee $employee): array
*/
public static function workFromHomeStats(Employee $employee): array
{
$currentYear = Carbon::now()->year;
$now = Carbon::now();
$currentYear = $now->year;
$workFromHomes = $employee->workFromHomes()->whereYear('date', (string) $currentYear)->get();

return [
'work_from_home_today' => WorkFromHomeHelper::hasWorkedFromHomeOnDate($employee, Carbon::now()),
'work_from_home_today' => WorkFromHomeHelper::hasWorkedFromHomeOnDate($employee, $now),
'number_times_this_year' => $workFromHomes->count(),
'url' => route('employee.work.workfromhome', [
'company' => $employee->company,
Expand Down
5 changes: 3 additions & 2 deletions app/Http/ViewHelpers/Employee/EmployeeSurveysViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public static function rateYourManagerSurveys(Employee $employee): ?array
// the manager the date of the next survey
$survey = $allSurveys->first();
if (! $survey->active) {
$now = Carbon::now();
$surveysCollection->push([
'id' => null,
'month' => Carbon::now()->format('M Y'),
'deadline' => DateHelper::hoursOrDaysLeft(Carbon::now()->endOfMonth()),
'month' => $now->format('M Y'),
'deadline' => DateHelper::hoursOrDaysLeft($now->copy()->endOfMonth()),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Company/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ public function getHolidaysInformation(): array
// get the yearly completion rate
$currentDate = Carbon::now();
$daysInYear = DateHelper::getNumberOfDaysInYear($currentDate);
$yearCompletionRate = Carbon::now()->dayOfYear * 100 / $daysInYear;
$yearCompletionRate = $currentDate->dayOfYear * 100 / $daysInYear;

return [
'current_balance_round' => round($this->holiday_balance, 0, PHP_ROUND_HALF_DOWN),
Expand Down