diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index 3aff2ff572..f0bbfb74a2 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -11,6 +11,7 @@ use App\Models\AssetModel; use App\Models\Category; use App\Models\AssetMaintenance; use App\Models\CheckoutAcceptance; +use App\Models\Company; use App\Models\CustomField; use App\Models\Depreciation; use App\Models\License; @@ -18,6 +19,7 @@ use App\Models\ReportTemplate; use App\Models\Setting; use App\Notifications\CheckoutAssetNotification; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\Mail; @@ -1109,28 +1111,31 @@ class ReportsController extends Controller $this->authorize('reports.view'); $showDeleted = $deleted == 'deleted'; - /** - * Get all assets with pending checkout acceptances - */ - if($showDeleted) { - $acceptances = CheckoutAcceptance::pending()->where('checkoutable_type', 'App\Models\Asset')->withTrashed()->with(['assignedTo' , 'checkoutable.assignedTo', 'checkoutable.model'])->get(); - } else { - $acceptances = CheckoutAcceptance::pending()->where('checkoutable_type', 'App\Models\Asset')->with(['assignedTo' => function ($query) { - $query->withTrashed(); - }, 'checkoutable.assignedTo', 'checkoutable.model'])->get(); + $query = CheckoutAcceptance::pending() + ->where('checkoutable_type', 'App\Models\Asset') + ->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) { + $query->withTrashed(); } - $assetsForReport = $acceptances - ->filter(function ($acceptance) { - $acceptance_checkoutable_flag = false; - if ($acceptance->checkoutable){ - $acceptance_checkoutable_flag = $acceptance->checkoutable->checkedOutToUser(); - } - - return $acceptance->checkoutable_type == 'App\Models\Asset' && $acceptance_checkoutable_flag; - }) - ->map(function($acceptance) { - return ['assetItem' => $acceptance->checkoutable, 'acceptance' => $acceptance]; + $assetsForReport = $query->get() + ->map(function ($acceptance) { + return [ + 'assetItem' => $acceptance->checkoutable, + 'acceptance' => $acceptance, + ]; }); return view('reports/unaccepted_assets', compact('assetsForReport','showDeleted' )); diff --git a/app/Models/CheckoutAcceptance.php b/app/Models/CheckoutAcceptance.php index b1c6c79141..e44a330ebc 100644 --- a/app/Models/CheckoutAcceptance.php +++ b/app/Models/CheckoutAcceptance.php @@ -35,7 +35,7 @@ class CheckoutAcceptance extends Model /** * The resource that was is out * - * @return Illuminate\Database\Eloquent\Relations\MorphTo + * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function checkoutable() { diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index 0d01428ea8..421a93b9d7 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -80,7 +80,7 @@ class AssetObserver { if ($settings = Setting::getSettings()) { $tag = $asset->asset_tag; - $prefix = $settings->auto_increment_prefix; + $prefix = (string)($settings->auto_increment_prefix ?? ''); $number = substr($tag, strlen($prefix)); // IF - auto_increment_assets is on, AND (there is no prefix OR the prefix matches the start of the tag) // AND the rest of the string after the prefix is all digits, THEN...