improved reports query, could be further optimized
This commit is contained in:
parent
e12c7473f8
commit
97b765b5cc
4 changed files with 30 additions and 55 deletions
|
@ -11,6 +11,7 @@ use App\Models\AssetModel;
|
||||||
use App\Models\Category;
|
use App\Models\Category;
|
||||||
use App\Models\AssetMaintenance;
|
use App\Models\AssetMaintenance;
|
||||||
use App\Models\CheckoutAcceptance;
|
use App\Models\CheckoutAcceptance;
|
||||||
|
use App\Models\Company;
|
||||||
use App\Models\CustomField;
|
use App\Models\CustomField;
|
||||||
use App\Models\Depreciation;
|
use App\Models\Depreciation;
|
||||||
use App\Models\License;
|
use App\Models\License;
|
||||||
|
@ -18,6 +19,7 @@ use App\Models\ReportTemplate;
|
||||||
use App\Models\Setting;
|
use App\Models\Setting;
|
||||||
use App\Notifications\CheckoutAssetNotification;
|
use App\Notifications\CheckoutAssetNotification;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
use Illuminate\Support\Facades\Mail;
|
use Illuminate\Support\Facades\Mail;
|
||||||
|
@ -1109,43 +1111,44 @@ class ReportsController extends Controller
|
||||||
$this->authorize('reports.view');
|
$this->authorize('reports.view');
|
||||||
$showDeleted = $deleted == 'deleted';
|
$showDeleted = $deleted == 'deleted';
|
||||||
|
|
||||||
|
// $acceptances = CheckoutAcceptance::pending()
|
||||||
|
// ->where('checkoutable_type', 'App\Models\Asset')
|
||||||
|
// ->withTrashed()
|
||||||
|
// ->with([
|
||||||
|
// 'assignedTo',
|
||||||
|
// 'checkoutable.assignedTo',
|
||||||
|
// 'checkoutable.model'
|
||||||
|
// ])->get();
|
||||||
$assetsForReport = collect();
|
$assetsForReport = collect();
|
||||||
|
|
||||||
$query = CheckoutAcceptance::pending()
|
$query = CheckoutAcceptance::pending()
|
||||||
->where('checkoutable_type', 'App\Models\Asset')
|
->where('checkoutable_type', 'App\Models\Asset')
|
||||||
->with(['checkoutable.assignedTo', 'checkoutable.model']); // Eager load common relationships
|
->with([
|
||||||
|
'checkoutable' => function (MorphTo $query) {
|
||||||
|
$query->morphWith([
|
||||||
|
AssetModel::class => ['model'],
|
||||||
|
Company::class => ['company'],
|
||||||
|
Asset::class => ['assignedTo'],
|
||||||
|
])->with('model.category');
|
||||||
|
},
|
||||||
|
'assignedTo' => function($query){
|
||||||
|
$query->withTrashed();
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
if ($showDeleted) {
|
if ($showDeleted) {
|
||||||
$query->withTrashed()->with(['assignedTo']);
|
$query->withTrashed();
|
||||||
}
|
|
||||||
else {
|
|
||||||
$query->with(['assignedTo' => function ($query) {
|
|
||||||
$query->withTrashed();
|
|
||||||
}]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process records in chunks
|
$assetsForReport = $query->get()
|
||||||
$query->chunk(100, function ($chunk) use (&$assetsForReport) {
|
->map(function ($acceptance) {
|
||||||
|
return [
|
||||||
$filtered = $chunk->filter(function ($acceptance) {
|
'assetItem' => $acceptance->checkoutable,
|
||||||
$acceptance_checkoutable_flag = false;
|
'acceptance' => $acceptance,
|
||||||
|
];
|
||||||
if ($acceptance->checkoutable){
|
|
||||||
$acceptance_checkoutable_flag = $acceptance->checkoutable->assignedTo();
|
|
||||||
}
|
|
||||||
// Return true if user
|
|
||||||
return $acceptance->checkoutable_type === 'App\Models\Asset' && $acceptance_checkoutable_flag;
|
|
||||||
})->map(function ($acceptance) {
|
|
||||||
|
|
||||||
return [
|
|
||||||
'assetItem' => $acceptance->checkoutable,
|
|
||||||
'acceptance' => $acceptance,
|
|
||||||
];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Merge results into the main collection
|
// Merge results into the main collection
|
||||||
$assetsForReport = $assetsForReport->merge($filtered);
|
|
||||||
});
|
|
||||||
|
|
||||||
return view('reports/unaccepted_assets', compact('assetsForReport','showDeleted' ));
|
return view('reports/unaccepted_assets', compact('assetsForReport','showDeleted' ));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Traits;
|
|
||||||
|
|
||||||
trait CheckoutableTrait
|
|
||||||
{
|
|
||||||
public const LOCATION = 'location';
|
|
||||||
public const ASSET = 'asset';
|
|
||||||
public const USER = 'user';
|
|
||||||
public function checkedOutToUser(): bool
|
|
||||||
{
|
|
||||||
return $this->assignedType() === self::USER;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function checkedOutToLocation(): bool
|
|
||||||
{
|
|
||||||
return $this->assignedType() === self::LOCATION;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function checkedOutToAsset(): bool
|
|
||||||
{
|
|
||||||
return $this->assignedType() === self::ASSET;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,7 +5,6 @@ namespace App\Models;
|
||||||
use App\Events\CheckoutableCheckedOut;
|
use App\Events\CheckoutableCheckedOut;
|
||||||
use App\Exceptions\CheckoutNotAllowed;
|
use App\Exceptions\CheckoutNotAllowed;
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Http\Traits\CheckoutableTrait;
|
|
||||||
use App\Http\Traits\UniqueUndeletedTrait;
|
use App\Http\Traits\UniqueUndeletedTrait;
|
||||||
use App\Models\Traits\Acceptable;
|
use App\Models\Traits\Acceptable;
|
||||||
use App\Models\Traits\Searchable;
|
use App\Models\Traits\Searchable;
|
||||||
|
@ -34,7 +33,6 @@ class Asset extends Depreciable
|
||||||
protected $with = ['model', 'adminuser'];
|
protected $with = ['model', 'adminuser'];
|
||||||
|
|
||||||
use CompanyableTrait;
|
use CompanyableTrait;
|
||||||
use CheckoutableTrait;
|
|
||||||
use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait;
|
use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait;
|
||||||
|
|
||||||
public const LOCATION = 'location';
|
public const LOCATION = 'location';
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Http\Traits\CheckoutableTrait;
|
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
@ -12,7 +11,6 @@ use Illuminate\Notifications\Notifiable;
|
||||||
class CheckoutAcceptance extends Model
|
class CheckoutAcceptance extends Model
|
||||||
{
|
{
|
||||||
use HasFactory, SoftDeletes, Notifiable;
|
use HasFactory, SoftDeletes, Notifiable;
|
||||||
use CheckoutableTrait;
|
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'accepted_at' => 'datetime',
|
'accepted_at' => 'datetime',
|
||||||
|
|
Loading…
Add table
Reference in a new issue