Skip to content

Commit

Permalink
Merge pull request #10936 from snipe/fixes/backport_licenses_loading
Browse files Browse the repository at this point in the history
Ports #10494 to master
  • Loading branch information
snipe committed Apr 12, 2022
2 parents 161c6b7 + 1441cf9 commit 7479f5f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/LicensesController.php
Expand Up @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Licenses/LicensesController.php
Expand Up @@ -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);
Expand Down
26 changes: 19 additions & 7 deletions app/Models/License.php
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion database/factories/LicenseFactory.php
@@ -1,4 +1,8 @@
<?php
namespace Database\Factories;

use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;

/*
|--------------------------------------------------------------------------
Expand All @@ -13,16 +17,18 @@

return [
'user_id' => 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,
];
});

Expand Down

0 comments on commit 7479f5f

Please sign in to comment.