diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php index 2371f4bdacbe..fcdfea5a13b4 100644 --- a/app/Http/Controllers/Api/LicensesController.php +++ b/app/Http/Controllers/Api/LicensesController.php @@ -26,7 +26,7 @@ class LicensesController extends Controller public function index(Request $request) { $this->authorize('view', License::class); - $licenses = Company::scopeCompanyables(License::with('company', 'manufacturer', 'freeSeats', 'supplier','category')->withCount('freeSeats as free_seats_count')); + $licenses = Company::scopeCompanyables(License::with('company', 'manufacturer', 'supplier','category')->withCount('freeSeats as free_seats_count')); if ($request->filled('company_id')) { diff --git a/app/Http/Controllers/Licenses/LicensesController.php b/app/Http/Controllers/Licenses/LicensesController.php index c13eb8d9e0bb..dc7a34c371cf 100755 --- a/app/Http/Controllers/Licenses/LicensesController.php +++ b/app/Http/Controllers/Licenses/LicensesController.php @@ -235,7 +235,7 @@ public function destroy($licenseId) public function show($licenseId = null) { - $license = License::with('assignedusers', 'licenseSeats.user', 'licenseSeats.asset')->find($licenseId); + $license = License::with('assignedusers')->find($licenseId); if ($license) { $this->authorize('view', $license); diff --git a/app/Models/License.php b/app/Models/License.php index 37a0e8d4112d..fa2b3b6eacf8 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -45,7 +45,7 @@ class License extends Depreciable protected $rules = array( 'name' => 'required|string|min:3|max:255', - 'seats' => 'required|min:1|max:999|integer', + 'seats' => 'required|min:1|max:9999|integer', 'license_email' => 'email|nullable|max:120', 'license_name' => 'string|nullable|max:100', 'notes' => 'string|nullable', @@ -173,13 +173,25 @@ public static function adjustSeatCount($license, $oldSeats, $newSeats) $logAction->logaction('delete seats'); return true; } - // Else we're adding seats. - DB::transaction(function () use ($license, $oldSeats, $newSeats) { - for ($i = $oldSeats; $i < $newSeats; $i++) { - $license->licenseSeatsRelation()->save(new LicenseSeat, ['user_id' => Auth::id()]); - } + //Create enough seats for the change. + $licenseInsert = []; + for ($i = $oldSeats; $i < $newSeats; $i++) { + $licenseInsert[] = [ + 'user_id' => Auth::id(), + 'license_id' => $license->id, + 'created_at' => now(), + 'updated_at' => now() + ]; + } + //Chunk and use DB transactions to prevent timeouts. + + collect($licenseInsert)->chunk(1000)->each(function ($chunk) { + DB::transaction(function () use ($chunk) { + LicenseSeat::insert($chunk->toArray()); + }); }); - // On initail create, we shouldn't log the addition of seats. + + // On initial create, we shouldn't log the addition of seats. if ($license->id) { //Log the addition of license to the log. $logAction = new Actionlog(); diff --git a/database/factories/LicenseFactory.php b/database/factories/LicenseFactory.php index b4b440f45c39..96b77bc14b1b 100644 --- a/database/factories/LicenseFactory.php +++ b/database/factories/LicenseFactory.php @@ -1,4 +1,8 @@ 1, - 'license_name' => $faker->name, + 'name' => $this->faker->name, 'license_email' => $faker->safeEmail, 'serial' => $faker->uuid, 'notes' => 'Created by DB seeder', + 'seats' => $this->faker->numberBetween(1, 10), 'purchase_date' => $faker->dateTimeBetween('-1 years','now', date_default_timezone_get()), 'order_number' => $faker->numberBetween(1000000, 50000000), 'expiration_date' => $faker->dateTimeBetween('now', '+3 years', date_default_timezone_get())->format('Y-m-d H:i:s'), 'reassignable' => $faker->boolean(), 'termination_date' => $faker->dateTimeBetween('-1 years','now', date_default_timezone_get())->format('Y-m-d H:i:s'), 'supplier_id' => $faker->numberBetween(1,5), + 'category_id' => Category::where('category_type', '=', 'license')->inRandomOrder()->first()->id, ]; });