A free open source IT asset/license management system
Updated seeders and factories for more realistic calendar data
snipe110c7dd90f41
diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.phpindex 32a56f0fcf..f6ff654280 100644--- a/database/factories/AssetFactory.php+++ b/database/factories/AssetFactory.php@@ -52,9 +52,14 @@ class AssetFactory extends Factory'requestable' => $this->faker->boolean(),'assigned_to' => null,'assigned_type' => null,+ // Populated in configure() below so the seeded demo shows+ // audit-due / warranty-expiration events on the calendar+ // instead of every asset having null dates. Tests that pin+ // specific values on a state override these.'next_audit_date' => null,'last_checkout' => null,'asset_eol_date' => null,+ 'warranty_months' => null,];}@@ -66,6 +71,29 @@ class AssetFactory extends Factory$asset->asset_eol_date = $this->faker->boolean(5)? CarbonImmutable::parse($asset->purchase_date)->addMonths(rand(0, 20))->format('Y-m-d'): CarbonImmutable::parse($asset->purchase_date)->addMonths($asset->model?->eol ?? rand(12, 60))->format('Y-m-d');++ // Only sprinkle demo-friendly dates on factory instances that+ // didn't get pinned by a state / attribute override. Callers+ // that pass an explicit value (tests, targeted seeds) keep+ // control; the plain factory-default path picks up a mix so+ // the calendar page and audit / warranty widgets aren't empty+ // in a fresh demo.++ // ~20% of assets get an audit_due date in a range spanning the+ // recent past through six months out. Skews toward "soon" so+ // the calendar's default visible window has enough content.+ if ($asset->next_audit_date === null && $this->faker->boolean(20)) {+ $asset->next_audit_date = CarbonImmutable::now()+ ->addDays($this->faker->numberBetween(-30, 180))+ ->format('Y-m-d');+ }++ // ~60% of assets get a warranty. Range covers laptops (12mo)+ // through longer-cycle desktops (up to 60mo). warranty_expires+ // is a computed accessor: purchase_date + warranty_months.+ if ($asset->warranty_months === null && $this->faker->boolean(60)) {+ $asset->warranty_months = $this->faker->randomElement([12, 24, 36, 48, 60]);+ }});}diff --git a/database/factories/LicenseFactory.php b/database/factories/LicenseFactory.phpindex 0ae43cdc87..2e672fcae8 100644--- a/database/factories/LicenseFactory.php+++ b/database/factories/LicenseFactory.php@@ -25,10 +25,19 @@ class LicenseFactory extends Factory*/public function definition(){+ // ~50% of seeded licenses get an expiration date spanning a+ // range around today, so the calendar's default visible window+ // has enough license.expiration events to be interesting on+ // demos. Tests pinning specific dates via state overrides+ // still win.+ $expirationDate = $this->faker->boolean(50)+ ? $this->faker->dateTimeBetween('-60 days', '+18 months', date_default_timezone_get())->format('Y-m-d')+ : null;+return ['category_id' => Category::factory(),'created_by' => User::factory()->superuser(),- 'expiration_date' => null,+ 'expiration_date' => $expirationDate,'license_email' => $this->faker->safeEmail(),'name' => $this->faker->name(),'notes' => 'Created by DB seeder',diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.phpindex 92c87e8e72..aa91bfaf75 100644--- a/database/factories/UserFactory.php+++ b/database/factories/UserFactory.php@@ -27,6 +27,14 @@ class UserFactory extends Factory*/public function definition(){+ // ~10% of seeded users get an employment end_date roughly+ // centered on today, so the calendar's user.end_date lane has+ // enough content to look populated on a demo install. Tests+ // pinning specific dates via state overrides still win.+ $endDate = $this->faker->boolean(10)+ ? $this->faker->dateTimeBetween('-30 days', '+9 months', date_default_timezone_get())->format('Y-m-d')+ : null;+return ['activated' => 1,'address' => $this->faker->address(),@@ -36,6 +44,7 @@ class UserFactory extends Factory'display_name' => null,'email' => $this->faker->safeEmail(),'employee_num' => $this->faker->numberBetween(3500, 35050),+ 'end_date' => $endDate,'first_name' => $this->faker->firstName(),'jobtitle' => $this->faker->jobTitle(),'last_name' => $this->faker->lastName(),diff --git a/database/seeders/AccessorySeeder.php b/database/seeders/AccessorySeeder.phpindex 410d9aac09..700967cd89 100644--- a/database/seeders/AccessorySeeder.php+++ b/database/seeders/AccessorySeeder.php@@ -67,6 +67,27 @@ class AccessorySeeder extends Seeder]);}+ // Extra low-stock accessories so the dashboard's low-stock+ // widget and the top-nav alert bell have realistic rows to+ // display in a fresh demo. Qty stays >= the max seeded+ // checkouts_count below (4) so the alert bell's `remaining =+ // qty - checkouts_count` calc doesn't go negative on any of+ // these rows. min_amt sits above qty so they still register+ // as low-stock (remaining < min_amt) even after checkouts.+ foreach ([+ ['name' => 'USB-C Hubs', 'qty' => 5, 'min_amt' => 10],+ ['name' => 'Wireless Headsets', 'qty' => 4, 'min_amt' => 12],+ ] as $lowStock) {+ $acq = $randomAcquisition();+ Accessory::factory()+ ->withInitialAcquisition($acq['supplier'], $acq['cost'], $acq['date'])+ ->create(array_merge($lowStock, [+ 'location_id' => $locationIds->random(),+ 'default_supplier_id' => $acq['supplier']->id,+ 'created_by' => $admin->id,+ ]));+ }+// Check out a handful of each accessory to random users so the// view page doesn't render empty. Uses the AccessoryCheckout// model directly (which is what the checkout controller writesdiff --git a/database/seeders/AssetSeeder.php b/database/seeders/AssetSeeder.phpindex 350aeed795..5a60445aff 100644--- a/database/seeders/AssetSeeder.php+++ b/database/seeders/AssetSeeder.php@@ -4,9 +4,11 @@ namespace Database\Seeders;use App\Models\Actionlog;use App\Models\Asset;+use App\Models\CheckoutRequest;use App\Models\Location;use App\Models\Supplier;use App\Models\User;+use Carbon\Carbon;use Database\Seeders\Concerns\ReportsMemory;use Illuminate\Database\Eloquent\Factories\Sequence;use Illuminate\Database\Seeder;@@ -82,6 +84,47 @@ class AssetSeeder extends SeederDB::table('checkout_requests')->truncate();+ // Seed a handful of demo-friendly "needs attention" rows so the+ // dashboard widget has realistic content on a fresh install:+ //+ // - Overdue checkins: pick a few assigned assets and backdate+ // their expected_checkin so overdueCheckins is populated on+ // the dashboard.+ // - Pending checkout requests: seed against requestable+ // assets so pendingRequestsCount is populated too.+ //+ // Kept small (5 of each) so the widget looks lived-in but not+ // overwhelming on the demo.+ $assignedAssetIds = Asset::whereNotNull('assigned_to')+ ->inRandomOrder()+ ->limit(5)+ ->pluck('id');+ foreach ($assignedAssetIds as $id) {+ Asset::whereKey($id)->update([+ 'expected_checkin' => Carbon::now()->subDays(rand(3, 30)),+ ]);+ }++ $requestableAssets = Asset::where('requestable', 1)+ ->inRandomOrder()+ ->limit(5)+ ->get();+ $requesterIds = User::where('activated', 1)+ ->where('show_in_list', '!=', '0')+ ->inRandomOrder()+ ->limit(5)+ ->pluck('id');+ if ($requesterIds->isNotEmpty()) {+ foreach ($requestableAssets as $asset) {+ CheckoutRequest::create([+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ 'user_id' => $requesterIds->random(),+ 'quantity' => 1,+ ]);+ }+ }+$this->reportMemory('AssetSeeder end (all factory batches complete)');}diff --git a/database/seeders/ConsumableSeeder.php b/database/seeders/ConsumableSeeder.phpindex 9fb847a0c4..dd28dd1054 100644--- a/database/seeders/ConsumableSeeder.php+++ b/database/seeders/ConsumableSeeder.php@@ -49,6 +49,27 @@ class ConsumableSeeder extends Seeder]);}+ // Extra low-stock consumables so the dashboard's low-stock+ // widget and the top-nav alert bell have realistic rows to+ // display in a fresh demo. Qty stays >= the max seeded+ // checkouts_count below (4) so the alert bell's `remaining =+ // qty - checkouts_count` calc doesn't go negative on any of+ // these rows. min_amt sits above qty so they still register+ // as low-stock (remaining < min_amt) even after checkouts.+ foreach ([+ ['name' => 'Envelopes (#10)', 'qty' => 5, 'min_amt' => 10],+ ['name' => 'AA Batteries', 'qty' => 4, 'min_amt' => 15],+ ['name' => 'Ethernet Patch Cables (Cat 6, 3ft)', 'qty' => 5, 'min_amt' => 12],+ ] as $lowStock) {+ $acq = $randomAcquisition();+ Consumable::factory()+ ->withInitialAcquisition($acq['supplier'], $acq['cost'], $acq['date'])+ ->create(array_merge($lowStock, [+ 'default_supplier_id' => $acq['supplier']->id,+ 'created_by' => $admin->id,+ ]));+ }+// Check out a couple of each consumable to random users so the// view page doesn't render as an empty checkout list.$checkoutTargets = User::where('activated', 1)