From 38c5b430184498ad64a9291cad3ce8d2112b6c98 Mon Sep 17 00:00:00 2001 From: Petri Saarinen Date: Mon, 30 Nov 2026 10:47:01 +0200 Subject: [PATCH] Include the last day of a date range in expense and time entry queries SQLite stores date casts as "Y-m-d 00:00:00", so a between ending at "Y-m-d" left out the month's last day and the week's Sunday (the own expenses test failed on the 30th). Ranges now end before the next day. Co-Authored-By: Claude Opus 5.5 --- .../Repositories/EloquentExpenseQueries.php | 5 ++++- .../Repositories/EloquentTimeEntryQueries.php | 9 ++++++--- .../Repositories/EloquentTimeEntryRepository.php | 4 +++- tests/Feature/Modules/Expenses/ExpensesTest.php | 12 ++++++++++++ .../Feature/Modules/TimeTracking/TimeEntriesTest.php | 10 ++++++++++ 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/Modules/Expenses/Infrastructure/Persistence/Repositories/EloquentExpenseQueries.php b/app/Modules/Expenses/Infrastructure/Persistence/Repositories/EloquentExpenseQueries.php index 1c37186..3d36308 100644 --- a/app/Modules/Expenses/Infrastructure/Persistence/Repositories/EloquentExpenseQueries.php +++ b/app/Modules/Expenses/Infrastructure/Persistence/Repositories/EloquentExpenseQueries.php @@ -34,7 +34,10 @@ class EloquentExpenseQueries implements ExpenseQueries return $this->summaries(ExpenseModel::query() ->where('user_id', $userId) - ->whereBetween('spent_on', [$start->toDateString(), $start->copy()->endOfMonth()->toDateString()])); + // An upper bound before the next month: SQLite keeps date casts as "Y-m-d 00:00:00", + // which a between ending at the month's last "Y-m-d" would leave out. + ->where('spent_on', '>=', $start->toDateString()) + ->where('spent_on', '<', $start->copy()->addMonth()->toDateString())); } public function find(int $id): ?ExpenseSummary diff --git a/app/Modules/TimeTracking/Infrastructure/Persistence/Repositories/EloquentTimeEntryQueries.php b/app/Modules/TimeTracking/Infrastructure/Persistence/Repositories/EloquentTimeEntryQueries.php index 3d3077d..b2a34c7 100644 --- a/app/Modules/TimeTracking/Infrastructure/Persistence/Repositories/EloquentTimeEntryQueries.php +++ b/app/Modules/TimeTracking/Infrastructure/Persistence/Repositories/EloquentTimeEntryQueries.php @@ -42,7 +42,8 @@ class EloquentTimeEntryQueries implements TimeEntryQueries $entries = TimeEntryModel::query() ->where('user_id', $userId) - ->whereBetween('worked_on', [$days[0], $days[6]]) + ->where('worked_on', '>=', $days[0]) + ->where('worked_on', '<', CarbonImmutable::parse($days[6])->addDay()->toDateString()) ->get(); $pinnedIds = $this->pins->pinnedTaskIds($userId); $scheduledIds = $this->tasks->scheduledTaskIdsFor($userId, $days[0], $days[6]); @@ -100,7 +101,8 @@ class EloquentTimeEntryQueries implements TimeEntryQueries $start = CarbonImmutable::createFromFormat('!Y-m', $month) ?: CarbonImmutable::now()->startOfMonth(); $entries = TimeEntryModel::query() ->where('user_id', $userId) - ->whereBetween('worked_on', [$start->toDateString(), $start->endOfMonth()->toDateString()]) + ->where('worked_on', '>=', $start->toDateString()) + ->where('worked_on', '<', $start->addMonth()->toDateString()) ->get(); $paymentBases = []; @@ -135,7 +137,8 @@ class EloquentTimeEntryQueries implements TimeEntryQueries $entries = TimeEntryModel::query() ->where('user_id', $userId) - ->whereBetween('worked_on', [$days[0], $days[6]]) + ->where('worked_on', '>=', $days[0]) + ->where('worked_on', '<', CarbonImmutable::parse($days[6])->addDay()->toDateString()) ->get(); $taskIds = array_values(array_unique(array_merge( diff --git a/app/Modules/TimeTracking/Infrastructure/Persistence/Repositories/EloquentTimeEntryRepository.php b/app/Modules/TimeTracking/Infrastructure/Persistence/Repositories/EloquentTimeEntryRepository.php index a402aac..c725125 100644 --- a/app/Modules/TimeTracking/Infrastructure/Persistence/Repositories/EloquentTimeEntryRepository.php +++ b/app/Modules/TimeTracking/Infrastructure/Persistence/Repositories/EloquentTimeEntryRepository.php @@ -55,7 +55,9 @@ class EloquentTimeEntryRepository implements TimeEntryRepository { return TimeEntryModel::query() ->where('user_id', $userId) - ->whereBetween('worked_on', [$from->format('Y-m-d'), $to->format('Y-m-d')]) + // Exclusive upper bound: SQLite keeps date casts as "Y-m-d 00:00:00". + ->where('worked_on', '>=', $from->format('Y-m-d')) + ->where('worked_on', '<', $to->modify('+1 day')->format('Y-m-d')) ->orderBy('worked_on') ->get() ->map(fn (TimeEntryModel $model): TimeEntry => $this->toEntity($model)) diff --git a/tests/Feature/Modules/Expenses/ExpensesTest.php b/tests/Feature/Modules/Expenses/ExpensesTest.php index 63056e8..f3cd91b 100644 --- a/tests/Feature/Modules/Expenses/ExpensesTest.php +++ b/tests/Feature/Modules/Expenses/ExpensesTest.php @@ -178,3 +178,15 @@ it('links the job page to its expenses and shows the own expenses by month', fun ->assertSee('Lokakuu 2026') ->assertSee('Lokakuun hankinta'); }); + +it('lists an expense of the month\'s last day on the own expenses page', function () { + $user = User::factory()->create(); + ExpenseModel::factory()->create(['customer_job_id' => $this->job->id, 'user_id' => $user->id, 'description' => 'Helmikuun viimeinen hankinta', 'spent_on' => '2026-02-28']); + ExpenseModel::factory()->create(['customer_job_id' => $this->job->id, 'user_id' => $user->id, 'description' => 'Maaliskuun hankinta', 'spent_on' => '2026-03-01']); + + $this->actingAs($user) + ->get(route('expenses.mine', ['kuukausi' => '2026-02'])) + ->assertOk() + ->assertSee('Helmikuun viimeinen hankinta') + ->assertDontSee('Maaliskuun hankinta'); +}); diff --git a/tests/Feature/Modules/TimeTracking/TimeEntriesTest.php b/tests/Feature/Modules/TimeTracking/TimeEntriesTest.php index bfb174e..4434ce9 100644 --- a/tests/Feature/Modules/TimeTracking/TimeEntriesTest.php +++ b/tests/Feature/Modules/TimeTracking/TimeEntriesTest.php @@ -166,3 +166,13 @@ it('keeps subcontractors to their own jobs and blocks removing tasks with hours' $this->actingAs(User::factory()->subcontractor()->create()); $this->get(route('time.task', $this->task->id))->assertForbidden(); }); + +it('counts the hours of Sunday in the week', function () { + $sunday = now()->startOfWeek()->addDays(6)->toDateString(); + TimeEntryModel::factory()->create(['task_id' => $this->task->id, 'user_id' => $this->employee->id, 'worked_on' => $sunday, 'hours' => 2]); + + $sheet = Livewire::test('time::week')->instance()->sheet; + + expect($sheet->totalQuarters)->toBe(8) + ->and($sheet->dayTotals[$sunday])->toBe(8); +});