Skip to content

A free open source IT asset/license management system

Allow filtering by checkout range

Marcus Moored7fa47cb24e9
diff --git a/app/Http/Controllers/Reports/CustomComponentReportController.php b/app/Http/Controllers/Reports/CustomComponentReportController.php
index 27c3e68e72..a7e6a727e1 100644
--- a/app/Http/Controllers/Reports/CustomComponentReportController.php
+++ b/app/Http/Controllers/Reports/CustomComponentReportController.php
@@ -3,6 +3,7 @@
namespace App\Http\Controllers\Reports;
use App\Http\Controllers\Controller;
+use App\Models\Actionlog;
use App\Models\Component;
use App\Models\ReportTemplate;
use Carbon\Carbon;
@@ -106,6 +107,18 @@ class CustomComponentReportController extends Controller
'last_updated_before' => 'updated_at',
]);
+ if ($request->filled('checkout_date_start') && $request->filled('checkout_date_end')) {
+ $checkout_start = Carbon::parse($request->input('checkout_date_start'))->startOfDay();
+ $checkout_end = Carbon::parse($request->input('checkout_date_end', now()))->endOfDay();
+
+ $componentIdsWithinCheckoutRange = Actionlog::where('action_type', '=', 'checkout')
+ ->where('item_type', Component::class)
+ ->whereBetween('action_date', [$checkout_start, $checkout_end])
+ ->pluck('item_id');
+
+ $query->whereIn('components.id', $componentIdsWithinCheckoutRange);
+ }
+
if ($request->input('deleted_components') === 'include_deleted') {
$query->withTrashed();
}
diff --git a/tests/Feature/Reporting/Custom/CustomComponentReportTest.php b/tests/Feature/Reporting/Custom/CustomComponentReportTest.php
index 286bb452c5..5b97ea114d 100644
--- a/tests/Feature/Reporting/Custom/CustomComponentReportTest.php
+++ b/tests/Feature/Reporting/Custom/CustomComponentReportTest.php
@@ -2,6 +2,8 @@
namespace Tests\Feature\Reporting\Custom;
+use App\Events\CheckoutableCheckedOut;
+use App\Models\Asset;
use App\Models\Category;
use App\Models\Company;
use App\Models\Component;
@@ -411,7 +413,44 @@ class CustomComponentReportTest extends TestCase
public function test_limiting_by_checkout_date()
{
- $this->markTestIncomplete();
+ $user = User::factory()->create();
+ [$assetA, $assetB] = Asset::factory()->count(2)->create()->all();
+
+ $componentA = Component::factory()->checkedOutToAsset($assetA)->create(['name' => 'Component A']);
+ $componentB = Component::factory()->checkedOutToAsset($assetB)->create(['name' => 'Component B']);
+
+ // we'll time travel a bit when firing these events to set the action log entries for testing.
+ $this->travel(-30)->days(
+ fn () => event(new CheckoutableCheckedOut(
+ $componentA,
+ $assetA,
+ $user,
+ '',
+ [],
+ 1,
+ ))
+ );
+
+ $this->travel(-15)->days(
+ fn () => event(new CheckoutableCheckedOut(
+ $componentB,
+ $assetB,
+ $user,
+ '',
+ [],
+ 1,
+ ))
+ );
+
+ $this->sendRequest([
+ 'component_name' => '1',
+ 'checkout_date_start' => now()->subDays(45)->toDateString(),
+ 'checkout_date_end' => now()->subDays(20)->toDateString(),
+ ])
+ ->assertOk()
+ ->assertCsvHeader()
+ ->assertSeeTextInStreamedResponse('Component A')
+ ->assertDontSeeTextInStreamedResponse('Component B');
}
public function test_limiting_by_created_at()