Ports #10494 to master

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2022-04-12 21:04:57 +01:00
parent 270143bb46
commit 1441cf9f4f
4 changed files with 28 additions and 10 deletions

View file

@ -26,7 +26,7 @@ class LicensesController extends Controller
public function index(Request $request) public function index(Request $request)
{ {
$this->authorize('view', License::class); $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')) { if ($request->filled('company_id')) {

View file

@ -235,7 +235,7 @@ class LicensesController extends Controller
public function show($licenseId = null) public function show($licenseId = null)
{ {
$license = License::with('assignedusers', 'licenseSeats.user', 'licenseSeats.asset')->find($licenseId); $license = License::with('assignedusers')->find($licenseId);
if ($license) { if ($license) {
$this->authorize('view', $license); $this->authorize('view', $license);

View file

@ -45,7 +45,7 @@ class License extends Depreciable
protected $rules = array( protected $rules = array(
'name' => 'required|string|min:3|max:255', '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_email' => 'email|nullable|max:120',
'license_name' => 'string|nullable|max:100', 'license_name' => 'string|nullable|max:100',
'notes' => 'string|nullable', 'notes' => 'string|nullable',
@ -173,13 +173,25 @@ class License extends Depreciable
$logAction->logaction('delete seats'); $logAction->logaction('delete seats');
return true; return true;
} }
// Else we're adding seats. //Create enough seats for the change.
DB::transaction(function () use ($license, $oldSeats, $newSeats) { $licenseInsert = [];
for ($i = $oldSeats; $i < $newSeats; $i++) { for ($i = $oldSeats; $i < $newSeats; $i++) {
$license->licenseSeatsRelation()->save(new LicenseSeat, ['user_id' => Auth::id()]); $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) { if ($license->id) {
//Log the addition of license to the log. //Log the addition of license to the log.
$logAction = new Actionlog(); $logAction = new Actionlog();

View file

@ -1,4 +1,8 @@
<?php <?php
namespace Database\Factories;
use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -13,16 +17,18 @@ $factory->define(App\Models\License::class, function (Faker\Generator $faker) {
return [ return [
'user_id' => 1, 'user_id' => 1,
'license_name' => $faker->name, 'name' => $this->faker->name,
'license_email' => $faker->safeEmail, 'license_email' => $faker->safeEmail,
'serial' => $faker->uuid, 'serial' => $faker->uuid,
'notes' => 'Created by DB seeder', 'notes' => 'Created by DB seeder',
'seats' => $this->faker->numberBetween(1, 10),
'purchase_date' => $faker->dateTimeBetween('-1 years','now', date_default_timezone_get()), 'purchase_date' => $faker->dateTimeBetween('-1 years','now', date_default_timezone_get()),
'order_number' => $faker->numberBetween(1000000, 50000000), 'order_number' => $faker->numberBetween(1000000, 50000000),
'expiration_date' => $faker->dateTimeBetween('now', '+3 years', date_default_timezone_get())->format('Y-m-d H:i:s'), 'expiration_date' => $faker->dateTimeBetween('now', '+3 years', date_default_timezone_get())->format('Y-m-d H:i:s'),
'reassignable' => $faker->boolean(), 'reassignable' => $faker->boolean(),
'termination_date' => $faker->dateTimeBetween('-1 years','now', date_default_timezone_get())->format('Y-m-d H:i:s'), 'termination_date' => $faker->dateTimeBetween('-1 years','now', date_default_timezone_get())->format('Y-m-d H:i:s'),
'supplier_id' => $faker->numberBetween(1,5), 'supplier_id' => $faker->numberBetween(1,5),
'category_id' => Category::where('category_type', '=', 'license')->inRandomOrder()->first()->id,
]; ];
}); });