Skip to content

A free open source IT asset/license management system

1.3 KB
Raw Blame
<?php

namespace App\Models\Traits;

use App\Enums\ActionType;
use App\Models\Actionlog;
use App\Models\CompanyableScope;

trait HasUploads
{
    public function uploads()
    {
        // Bypass FMCS company scoping: access is already gated by the policy on the
        // parent object. Objects like AssetModel and Company have no company_id, so
        // their upload logs always have company_id = null, which the scope would hide.
        //
        // Also surface QuantityAdjust rows that carry a receipt/invoice attachment
        // (AdjustsQuantity trait writes the filename onto the same log row rather
        // than emitting a separate 'uploaded' entry) so the Files tab shows those
        // documents alongside regular uploads.
        return $this->hasMany(Actionlog::class, 'item_id')
            ->withoutGlobalScope(CompanyableScope::class)
            ->where('item_type', self::class)
            ->whereIn('action_type', ['uploaded', ActionType::QuantityAdjust->value])
            ->whereNotNull('filename')
            ->whereNotIn('filename', function ($query) {
                $query->select('filename')
                    ->from('action_logs')
                    ->where('item_type', '=', self::class)
                    ->where('action_type', '=', 'upload deleted')
                    ->where('item_id', $this->id);
            });
    }
}