A free open source IT asset/license management system
Small fix for custom report dates
snipe027b38fe58f5
diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.phpindex f48d7a6f95..e0982100d6 100644--- a/app/Http/Controllers/ReportsController.php+++ b/app/Http/Controllers/ReportsController.php@@ -789,7 +789,11 @@ class ReportsController extends Controllerif ($request->filled('purchase_cost_end')) {$assets->whereBetween('assets.purchase_cost', [$request->input('purchase_cost_start'), $request->input('purchase_cost_end')]);} else {- $assets->where('assets.purchase_cost', '>', $request->input('purchase_cost_start'));+ // >= for consistency with the whereBetween branch above,+ // which is inclusive on both sides. Previously '>', so a+ // user filtering "cost >= 0" got nothing at exactly 0, and+ // "cost >= 100" quietly hid assets bought for 100.+ $assets->where('assets.purchase_cost', '>=', $request->input('purchase_cost_start'));}}@@ -840,7 +844,15 @@ class ReportsController extends Controller}if (($request->filled('last_updated_start')) && ($request->filled('last_updated_end'))) {- $assets->whereBetween('assets.updated_at', [$request->input('last_updated_start'), $request->input('last_updated_end')]);+ // updated_at is a timestamp, not a DATE, so a raw string+ // whereBetween is silently exclusive on the end side (the picker+ // returns Y-m-d which MySQL widens to Y-m-d 00:00:00, dropping+ // everything on the end day after midnight). Match the created_at+ // handling above and normalize to start/end of day.+ $last_updated_start = Carbon::parse($request->input('last_updated_start'))->startOfDay();+ $last_updated_end = Carbon::parse($request->input('last_updated_end'))->endOfDay();++ $assets->whereBetween('assets.updated_at', [$last_updated_start, $last_updated_end]);}if (($request->filled('last_updated_before'))) {diff --git a/tests/Feature/Reporting/Custom/CustomAssetReportTest.php b/tests/Feature/Reporting/Custom/CustomAssetReportTest.phpindex 991bd78b1e..ea28987385 100644--- a/tests/Feature/Reporting/Custom/CustomAssetReportTest.php+++ b/tests/Feature/Reporting/Custom/CustomAssetReportTest.php@@ -245,4 +245,168 @@ class CustomAssetReportTest extends TestCase$this->assertTrue($records->contains('super-secret-value'));}++ public function test_checkout_date_range_combined_with_assigned_only_filter()+ {+ // Reproducing the "Allocated Assets (Innovations)" template shape more+ // literally: an assigned-only report combined with a checkout_date_range.+ // Historically the pluck() version of the subquery returned an array+ // that whereIn could handle. The subquery-based version we ship now+ // depends on Laravel correctly compiling an inline SQL subquery. If+ // that ever breaks (or the item_type/action_type filter drifts), the+ // whole join returns empty and the customer's report is just the+ // header row.+ $reporter = User::factory()->canViewReports()->create();++ // Asset currently checked out to a user AND had a checkout log in range.+ $expected = Asset::factory()->assignedToUser()->create(['name' => 'Should Appear']);+ $log = new \App\Models\Actionlog;+ $log->item_type = Asset::class;+ $log->item_id = $expected->id;+ $log->action_type = 'checkout';+ $log->action_date = '2025-03-15 10:00:00';+ $log->created_at = '2025-03-15 10:00:00';+ $log->created_by = $reporter->id;+ $log->save();++ // Currently assigned but checkout log outside range.+ $wrongDate = Asset::factory()->assignedToUser()->create(['name' => 'Wrong Date']);+ $log2 = new \App\Models\Actionlog;+ $log2->item_type = Asset::class;+ $log2->item_id = $wrongDate->id;+ $log2->action_type = 'checkout';+ $log2->action_date = '2024-06-15 10:00:00';+ $log2->created_at = '2024-06-15 10:00:00';+ $log2->created_by = $reporter->id;+ $log2->save();++ // Checkout log in range but currently unassigned (was checked back in).+ $unassigned = Asset::factory()->create(['name' => 'Was Checked Out But Returned']);+ $log3 = new \App\Models\Actionlog;+ $log3->item_type = Asset::class;+ $log3->item_id = $unassigned->id;+ $log3->action_type = 'checkout';+ $log3->action_date = '2025-03-20 10:00:00';+ $log3->created_at = '2025-03-20 10:00:00';+ $log3->created_by = $reporter->id;+ $log3->save();++ $this->actingAs($reporter)+ ->post('reports/custom', [+ 'asset_name' => '1',+ 'asset_tag' => '1',+ 'assignment_status' => 'assigned',+ 'checkout_date_start' => '2025-03-01',+ 'checkout_date_end' => '2025-03-31',+ ])+ ->assertOk()+ ->assertHeader('content-type', 'text/csv; charset=utf-8')+ ->assertSeeTextInStreamedResponse('Should Appear')+ ->assertDontSeeTextInStreamedResponse('Wrong Date')+ ->assertDontSeeTextInStreamedResponse('Was Checked Out But Returned');+ }++ public function test_can_limit_assets_by_checkout_date_range()+ {+ // Reproducing the original issue: user selects "Allocated Assets" template,+ // sets a "Checked Out date range", clicks Generate, gets only the header+ // row. The controller uses an action_logs subquery to find asset_ids that+ // had a checkout action within the range.+ $insideRange = Asset::factory()->create(['name' => 'Asset Checked Out In Range']);+ $outsideRange = Asset::factory()->create(['name' => 'Asset Checked Out Elsewhere']);+ $neverCheckedOut = Asset::factory()->create(['name' => 'Asset Never Checked Out']);++ $reporter = User::factory()->canViewReports()->create();++ $log = new \App\Models\Actionlog;+ $log->item_type = Asset::class;+ $log->item_id = $insideRange->id;+ $log->action_type = 'checkout';+ $log->action_date = '2025-03-15 10:00:00';+ $log->created_at = '2025-03-15 10:00:00';+ $log->created_by = $reporter->id;+ $log->save();++ $log2 = new \App\Models\Actionlog;+ $log2->item_type = Asset::class;+ $log2->item_id = $outsideRange->id;+ $log2->action_type = 'checkout';+ $log2->action_date = '2025-01-15 10:00:00';+ $log2->created_at = '2025-01-15 10:00:00';+ $log2->created_by = $reporter->id;+ $log2->save();++ $this->actingAs($reporter)+ ->post('reports/custom', [+ 'asset_name' => '1',+ 'asset_tag' => '1',+ 'checkout_date_start' => '2025-03-01',+ 'checkout_date_end' => '2025-03-31',+ ])+ ->assertOk()+ ->assertHeader('content-type', 'text/csv; charset=utf-8')+ ->assertSeeTextInStreamedResponse('Asset Checked Out In Range')+ ->assertDontSeeTextInStreamedResponse('Asset Checked Out Elsewhere')+ ->assertDontSeeTextInStreamedResponse('Asset Never Checked Out');+ }++ public function test_purchase_cost_start_only_is_inclusive_of_the_boundary()+ {+ // Regression: with only purchase_cost_start supplied, the filter used+ // strict > which quietly hid assets whose cost equalled the boundary+ // (including 0). The other branch (both endpoints set) uses+ // whereBetween which is inclusive, so start-only should match.+ Asset::factory()->create(['name' => 'Asset At Boundary', 'purchase_cost' => 100]);+ Asset::factory()->create(['name' => 'Asset Below', 'purchase_cost' => 50]);+ Asset::factory()->create(['name' => 'Asset Above', 'purchase_cost' => 200]);++ $this->actingAs(User::factory()->canViewReports()->create())+ ->post('reports/custom', [+ 'asset_name' => '1',+ 'purchase_cost_start' => 100,+ ])+ ->assertOk()+ ->assertHeader('content-type', 'text/csv; charset=utf-8')+ ->assertSeeTextInStreamedResponse('Asset At Boundary')+ ->assertSeeTextInStreamedResponse('Asset Above')+ ->assertDontSeeTextInStreamedResponse('Asset Below');+ }++ public function test_last_updated_range_is_inclusive_of_the_end_day()+ {+ // Regression: whereBetween on assets.updated_at (a timestamp) with a+ // raw Y-m-d string was widened by MySQL to Y-m-d 00:00:00, so anything+ // updated on the end day AFTER midnight was silently excluded. Result:+ // reports that ended "today" (the common case) returned empty or too+ // few rows. The fix normalizes with Carbon startOfDay / endOfDay to+ // match the created_at handling right above it in the controller.+ //+ // Explicit assertions per timestamp instead of a factory `->create()`+ // because Eloquent bumps updated_at to now() on save() unless we set+ // it directly.+ Asset::factory()->create(['name' => 'Asset Before'])+ ->forceFill(['updated_at' => '2025-03-01 00:00:00'])->save();++ Asset::factory()->create(['name' => 'Asset First Day'])+ ->forceFill(['updated_at' => '2025-03-15 08:30:00'])->save();++ Asset::factory()->create(['name' => 'Asset Last Day Afternoon'])+ ->forceFill(['updated_at' => '2025-03-31 15:45:00'])->save();++ Asset::factory()->create(['name' => 'Asset After'])+ ->forceFill(['updated_at' => '2025-04-01 09:00:00'])->save();++ $this->actingAs(User::factory()->canViewReports()->create())+ ->post('reports/custom', [+ 'asset_name' => '1',+ 'asset_tag' => '1',+ 'last_updated_start' => '2025-03-01',+ 'last_updated_end' => '2025-03-31',+ ])+ ->assertOk()+ ->assertHeader('content-type', 'text/csv; charset=utf-8')+ ->assertSeeTextInStreamedResponse('Asset First Day')+ ->assertSeeTextInStreamedResponse('Asset Last Day Afternoon')+ ->assertDontSeeTextInStreamedResponse('Asset After');+ }}