Skip to content

A free open source IT asset/license management system

34.8 KB
Raw Blame
<?php

namespace App\Presenters;

use App\Enums\FileStorage;
use App\Models\CustomField;
use Carbon\CarbonImmutable;
use DateTime;
use Illuminate\Support\Facades\Storage;

/**
 * Class AssetPresenter
 *
 * @property \App\Models\Asset $model Concrete-typed override of the base
 *                                    Presenter's $model so PHPStan can resolve Asset-specific
 *                                    properties (supplier, model relation, tag_color, etc)
 *                                    without falling back to SnipeModel and flagging every
 *                                    access as property.notFound.
 */
class AssetPresenter extends Presenter
{
    /**
     * Json Column Layout for bootstrap table
     *
     * @return string
     */
    public static function dataTableLayout($hide_fields = [])
    {
        $layout = [
            [
                'field' => 'checkbox',
                'scope' => 'col',
                'checkbox' => true,
                'titleTooltip' => trans('general.select_all_none'),
                'printIgnore' => true,
                'class' => 'hidden-print',
            ], [
                'field' => 'id',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => true,
                'switchable' => true,
                'title' => trans('general.id'),
                'visible' => false,
            ],  [
                'field' => 'asset_tag',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'switchable' => false,
                'title' => trans('admin/hardware/table.asset_tag'),
                'visible' => true,
                'formatter' => 'hardwareLinkFormatter',
            ],  [
                'field' => 'name',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                // Match the import wizard's Name target label so the
                // asset datatable export round-trips through the
                // importer's auto-mapper on every locale.
                'title' => trans('general.item_name_var', ['item' => trans('general.asset')]),
                'visible' => true,
                'formatter' => 'hardwareLinkFormatter',
            ], [
                'field' => 'company',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'switchable' => true,
                'title' => trans('general.company'),
                'visible' => false,
                'formatter' => 'companiesLinkObjFormatter',
            ], [
                'field' => 'image',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => true,
                'switchable' => true,
                'title' => trans('general.image'),
                'visible' => true,
                'formatter' => 'imageFormatter',
            ], [
                'field' => 'serial',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/form.serial'),
                'visible' => true,
                'formatter' => 'hardwareLinkFormatter',
            ],  [
                'field' => 'model',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/form.model'),
                'visible' => true,
                'formatter' => 'modelsLinkObjFormatter',
            ], [
                'field' => 'model_number',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/models/table.modelnumber'),
                'visible' => false,
            ], [
                'field' => 'category',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('general.category'),
                'visible' => true,
                'formatter' => 'categoriesLinkObjFormatter',
            ], [
                'field' => 'status',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/table.status'),
                'visible' => true,
                'formatter' => 'statuslabelsLinkObjFormatter',
            ],
            [
                'field' => 'assigned_to',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/form.checkedout_to'),
                'visible' => true,
                'formatter' => 'polymorphicItemFormatter',
            ], [
                'field' => 'employee_number',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'title' => trans('general.employee_number'),
                'visible' => false,
                'formatter' => 'employeeNumFormatter',
            ], [
                'field' => 'jobtitle',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/users/table.title'),
                'visible' => false,
                'formatter' => 'jobtitleFormatter',
            ], [
                'field' => 'location',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/table.location'),
                'visible' => true,
                'formatter' => 'deployedLocationFormatter',
            ], [
                'field' => 'rtd_location',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/form.default_location'),
                'visible' => false,
                'formatter' => 'deployedLocationFormatter',
            ], [
                'field' => 'manufacturer',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('general.manufacturer'),
                'visible' => false,
                'formatter' => 'manufacturersLinkObjFormatter',
            ], [
                'field' => 'supplier',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('general.supplier'),
                'visible' => false,
                'formatter' => 'suppliersLinkObjFormatter',
            ], [
                'field' => 'purchase_date',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.purchase_date'),
                'formatter' => 'dateDisplayFormatter',
            ],
            //            [
            //                'field' => 'first_checkout',
            //                'searchable' => true,
            //                'sortable' => true,
            //                'visible' => false,
            //                'title' => trans('general.first_checkout'),
            //                'formatter' => 'dateDisplayFormatter',
            //            ],
            [
                'field' => 'last_checkout',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.last_checkout'),
                'formatter' => 'dateDisplayFormatter',
            ],
            [
                'field' => 'age',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => false,
                'visible' => false,
                'title' => trans('general.age'),
            ], [
                'field' => 'purchase_cost',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('general.purchase_cost'),
                'footerFormatter' => 'sumFormatter',
                'class' => 'text-right',
            ], [
                'field' => 'book_value',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'title' => trans('admin/hardware/table.book_value'),
                'footerFormatter' => 'sumFormatter',
                'class' => 'text-right',
            ], [
                'field' => 'order_number',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.order_number'),
                'formatter' => 'orderNumberObjFilterFormatter',
            ], [
                'field' => 'eol',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => true,
                'visible' => false,
                'title' => trans('admin/hardware/form.eol_rate'),
            ],
            [
                'field' => 'asset_eol_date',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('admin/hardware/form.eol_date'),
                'formatter' => 'dateDisplayFormatter',
            ], [
                'field' => 'warranty_months',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('admin/hardware/form.warranty'),
            ], [
                'field' => 'warranty_expires',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => false,
                'visible' => false,
                'title' => trans('admin/hardware/form.warranty_expires'),
                'formatter' => 'dateDisplayFormatter',
            ], [
                'field' => 'requestable',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('admin/hardware/general.requestable'),
                'formatter' => 'trueFalseFormatter',

            ], [
                'field' => 'notes',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.notes'),

            ], [
                'field' => 'checkout_counter',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.checkouts_count'),

            ], [
                'field' => 'checkin_counter',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.checkins_count'),

            ], [
                'field' => 'requests_counter',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.user_requests_count'),

            ], [
                'field' => 'created_by',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('general.created_by'),
                'visible' => false,
                'formatter' => 'usersLinkObjFormatter',
            ],
            [
                'field' => 'created_at',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'switchable' => true,
                'title' => trans('general.created_at'),
                'visible' => false,
                'formatter' => 'dateDisplayFormatter',
            ], [
                'field' => 'updated_at',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'switchable' => true,
                'title' => trans('general.updated_at'),
                'visible' => false,
                'formatter' => 'dateDisplayFormatter',
            ],
        ];

        if (! in_array('deleted_at', $hide_fields)) {
            $layout[] = [
                'field' => 'deleted_at',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => true,
                'switchable' => true,
                'title' => trans('general.deleted_at'),
                'visible' => true,
                'formatter' => 'dateDisplayFormatter',
            ];
        }

        $layout = array_merge($layout, [
            [
                'field' => 'last_checkout',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('admin/hardware/table.checkout_date'),
                'formatter' => 'dateDisplayFormatter',
            ], [
                'field' => 'last_checkin',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('admin/hardware/table.last_checkin_date'),
                'formatter' => 'dateDisplayFormatter',
            ], [
                'field' => 'expected_checkin',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('admin/hardware/form.expected_checkin'),
                'formatter' => 'dateDisplayFormatter',
            ], [
                'field' => 'last_audit_date',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.last_audit'),
                'formatter' => 'dateDisplayFormatter',
            ], [
                'field' => 'next_audit_date',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.next_audit_date'),
                'formatter' => 'dateDisplayFormatter',
            ], [
                'field' => 'byod',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'visible' => false,
                'title' => trans('general.byod'),
                'class' => 'byod',
                'formatter' => 'trueFalseFormatter',

            ],
        ]);

        // This looks complicated, but we have to confirm that the custom fields exist in custom fieldsets
        // *and* those fieldsets are associated with models, otherwise we'll trigger
        // javascript errors on the bootstrap tables side of things, since we're asking for properties
        // on fields that will never be passed through the REST API since they're not associated with
        // models. We only pass the fieldsets that pertain to each asset (via their model) so that we
        // don't junk up the REST API with tons of custom fields that don't apply

        $fields = CustomField::whereHas('fieldset', function ($query) {
            $query->whereHas('models');
        })->get();

        foreach ($fields as $field) {
            $layout[] = [
                'field' => $field->db_column,
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'switchable' => true,
                'title' => e($field->name),
                'formatter' => 'customFieldsFormatter',
                // Use tableExport attributes on the TD so dot-containing
                // text values (e.g. "79.99" in a text field) stop being
                // misread as EU-locale thousands numbers on export. See
                // issue #19595 and the customFieldsCellStyle definition
                // in bootstrap-table.
                'cellStyle' => 'customFieldsCellStyle',
                'escape' => true,
                'class' => ($field->field_encrypted == '1') ? 'css-padlock' : '',
                'visible' => ($field->show_in_listview == '1') ? true : false,
            ];
        }

        // Sync-adapter side-table columns. Hidden by default, exposable
        // via the column picker for admins syncing from an MDM / RMM.
        // Sort routed through Asset::scopeOrderExternalSource (leftJoin
        // on asset_external_sources) and search routed through the
        // externalSource relation in $searchableRelations. last_seen
        // sortable but not searchable (datetime substring match is
        // nonsensical for the top search bar).
        $layout[] = [
            'field' => 'primary_mac',
            'scope' => 'col',
            'searchable' => true,
            'sortable' => true,
            'switchable' => true,
            'title' => trans('admin/settings/sync_adapters.field_mac'),
            'visible' => false,
        ];
        $layout[] = [
            'field' => 'primary_ip',
            'scope' => 'col',
            'searchable' => true,
            'sortable' => true,
            'switchable' => true,
            'title' => trans('admin/settings/sync_adapters.field_ip'),
            'visible' => false,
        ];
        $layout[] = [
            'field' => 'external_os',
            'scope' => 'col',
            'searchable' => true,
            'sortable' => true,
            'switchable' => true,
            'title' => trans('admin/settings/sync_adapters.field_os'),
            'visible' => false,
        ];
        $layout[] = [
            'field' => 'external_os_version',
            'scope' => 'col',
            'searchable' => true,
            'sortable' => true,
            'switchable' => true,
            'title' => trans('admin/settings/sync_adapters.field_os_version'),
            'visible' => false,
        ];
        $layout[] = [
            'field' => 'last_seen',
            'scope' => 'col',
            'searchable' => false,
            'sortable' => true,
            'switchable' => true,
            'title' => trans('admin/settings/sync_adapters.field_last_seen'),
            'visible' => false,
            'formatter' => 'dateDisplayFormatter',
        ];

        $layout[] = [
            'field' => 'checkincheckout',
            'scope' => 'col',
            'searchable' => false,
            'sortable' => false,
            'switchable' => false,
            'title' => trans('general.checkin').'/'.trans('general.checkout'),
            'visible' => true,
            'formatter' => 'hardwareInOutFormatter',
            'printIgnore' => true,
            'class' => 'hidden-print',
        ];

        $layout[] = [
            'field' => 'actions',
            'scope' => 'col',
            'searchable' => false,
            'sortable' => false,
            'switchable' => false,
            'title' => trans('table.actions'),
            'formatter' => 'hardwareActionsFormatter',
            'printIgnore' => true,
            'class' => 'hidden-print',
        ];

        return json_encode($layout);
    }

    public static function assignedAccessoriesDataTableLayout()
    {
        $layout = [
            [
                'field' => 'id',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'switchable' => true,
                'title' => trans('general.id'),
                'visible' => false,
            ],
            [
                'field' => 'accessory',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'switchable' => true,
                'title' => trans('general.accessory'),
                'visible' => true,
                'formatter' => 'accessoriesLinkObjFormatter',
            ],
            [
                'field' => 'image',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'switchable' => true,
                'title' => trans('general.image'),
                'visible' => true,
                'formatter' => 'imageFormatter',
            ],
            [
                'field' => 'note',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'switchable' => true,
                'title' => trans('general.notes'),
                'visible' => true,
            ],
            [
                'field' => 'created_at',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'switchable' => true,
                'title' => trans('admin/hardware/table.checkout_date'),
                'visible' => true,
                'formatter' => 'dateDisplayFormatter',
            ],
            [
                'field' => 'created_by',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'title' => trans('general.created_by'),
                'visible' => false,
                'formatter' => 'usersLinkObjFormatter',
            ],
            [
                'field' => 'available_actions',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'switchable' => false,
                'title' => trans('table.actions'),
                'formatter' => 'accessoriesInOutFormatter',
                'printIgnore' => true,
            ],
        ];

        return json_encode($layout);
    }

    /**
     * Generate html link to this items name.
     *
     * @return string
     */
    public function nameUrl()
    {
        if (auth()->user()->can('view', ['\App\Models\Asset', $this])) {
            return '<a href="'.route('hardware.show', $this->id).'">'.e($this->display_name).'</a>';
        } else {
            return e($this->display_name);
        }
    }

    public function formattedNameLink()
    {
        if (auth()->user()->can('view', ['\App\Models\Asset', $this])) {
            return '<a href="'.route('hardware.show', e($this->id)).'" class="'.(($this->deleted_at != '') ? 'deleted' : '').'">'.e($this->display_name).'</a>';
        }

        return '<span class="'.(($this->deleted_at != '') ? 'deleted' : '').'">'.e($this->display_name).'</span>';
    }

    public function formattedTagLink()
    {
        if (auth()->user()->can('view', ['\App\Models\Asset', $this])) {
            return '<a href="'.route('hardware.show', e($this->id)).'" class="'.(($this->deleted_at != '') ? 'deleted' : '').'">'.e($this->asset_tag).'</a>';
        }

        return '<span class="'.(($this->deleted_at != '') ? 'deleted' : '').'">'.e($this->asset_tag).'</span>';
    }

    public function modelUrl()
    {
        if ($this->model->model) {
            return $this->model->model->present()->nameUrl();
        }

        return '';
    }

    /**
     * Generate img tag to this items image.
     *
     * @return mixed|string
     */
    public function imageUrl()
    {
        $imagePath = '';
        $imageAlt = '';
        if ($this->image && ! empty($this->image)) {
            $imagePath = $this->image;
            $imageAlt = $this->name;
        } elseif ($this->model && ! empty($this->model->image)) {
            $imagePath = $this->model->image;
            $imageAlt = $this->model->name;
        }
        if (! empty($imagePath)) {
            $url = Storage::disk('public')->url(FileStorage::Assets->publicPath().e($imagePath));
            $imagePath = '<img src="'.$url.'" height="50" width="50" alt="'.e($imageAlt).'">';
        }

        return $imagePath;
    }

    /**
     * Generate img tag to this items image.
     *
     * @return mixed|string
     */
    public function imageSrc()
    {
        $imagePath = '';
        if ($this->image && ! empty($this->image)) {
            $imagePath = $this->image;
        } elseif ($this->model && ! empty($this->model->image)) {
            $imagePath = $this->model->image;
        }
        if (! empty($imagePath)) {
            return Storage::disk('public')->url(FileStorage::Assets->publicPath().e($imagePath));
        }

        return $imagePath;
    }

    /**
     * Get Displayable Name
     *
     * @return string
     *
     * @todo this should be factored out - it should be subsumed by fullName (below)
     *
     **/
    public function name()
    {
        return $this->fullName;
    }

    /**
     * Helper for notification polymorphism.
     *
     * @return mixed
     */
    public function fullName()
    {
        $str = '';

        // Asset name
        if ($this->model->name) {
            $str .= $this->model->name;
        }

        // Asset tag
        if ($this->asset_tag) {
            $str .= ' #'.$this->model->asset_tag;
        }

        // Asset Model name
        if ($this->model->model) {
            $str .= ' - '.$this->model->model->name;
        }

        return $str;
    }

    /**
     * Returns the date this item hits EOL.
     *
     * @return false|string
     */
    public function eol_date()
    {
        if (($this->purchase_date) && ($this->model->model) && ($this->model->model->eol)) {
            return CarbonImmutable::parse($this->purchase_date)->addMonths($this->model->model->eol)->format('Y-m-d');
        }
    }

    /**
     * How many months until this asset hits EOL.
     *
     * @return null
     */
    public function months_until_eol()
    {
        $today = date('Y-m-d');
        $d1 = new DateTime($today);
        $d2 = new DateTime($this->eol_date());

        if ($this->eol_date() > $today) {
            $interval = $d2->diff($d1);
        } else {
            $interval = null;
        }

        return $interval;
    }

    /**
     * @return string
     *                This handles the status label "meta" status of "deployed" if
     *                it's assigned. Should maybe deprecate.
     */
    public function statusMeta()
    {
        if ($this->model->assigned_to) {
            return 'deployed';
        }

        return $this->model->status->getStatuslabelType();
    }

    /**
     * @return string
     *                This handles the status label "meta" status of "deployed" if
     *                it's assigned. Should maybe deprecate.
     */
    public function statusText()
    {
        if ($this->model->assigned) {
            return trans('general.deployed');
        }

        return $this->model->status->name;
    }

    /**
     * @return string
     *                This handles the status label "meta" status of "deployed" if
     *                it's assigned. Results look like:
     *
     * (if assigned and the status label is "Ready to Deploy"):
     * (Deployed)
     *
     * (f assigned and status label is not "Ready to Deploy":)
     * Deployed (Another Status Label)
     *
     * (if not deployed:)
     * Another Status Label
     */
    public function fullStatusText()
    {
        // Make sure the status is valid
        if ($this->status) {

            // If the status is assigned to someone or something...
            if ($this->model->assigned) {

                // If it's assigned and not set to the default "ready to deploy" status
                if ($this->status->name != trans('general.ready_to_deploy')) {
                    return trans('general.deployed').' ('.$this->model->status->name.')';
                }

                // If it's assigned to the default "ready to deploy" status, just
                // say it's deployed - otherwise it's confusing to have a status that is
                // both "ready to deploy" and deployed at the same time.
                return trans('general.deployed');
            }

            // Return just the status name
            return $this->model->status->name;
        }

        // This status doesn't seem valid - either data has been manually edited or
        // the status label was deleted.
        return 'Invalid status';
    }

    /**
     * Date the warranty expires.
     *
     * @return false|string
     */
    public function warranty_expires()
    {
        if (($this->purchase_date) && ($this->warranty_months)) {
            $date = date_create($this->purchase_date);
            date_add($date, date_interval_create_from_date_string($this->warranty_months.' months'));

            return date_format($date, 'Y-m-d');
        }

        return false;
    }

    /**
     * Url to view this item.
     *
     * @return string
     */
    public function viewUrl()
    {
        return route('hardware.show', $this->id);
    }

    public function glyph()
    {
        return '<x-icon type="assets" />';
    }

    public function calendarUrl(): ?string
    {
        return route('hardware.show', $this->model->id);
    }

    public function calendarColor(): ?string
    {
        // Trailing `?->tag_color` (right before the ??) is stylistically
        // redundant per PHPStan since if the left-hand relation is
        // null the ?-> short-circuits already and the ?? catches the
        // null. Drop the trailing nullsafe on the last hop only;
        // keep it on intermediate relation hops that really can be
        // null at runtime (model->category, supplier).
        return $this->model->tag_color
            ?? $this->model->model?->category?->tag_color
            ?? $this->model->supplier?->tag_color;
    }

    /**
     * Column layout for the assets tab on /account/requestable. Feeds
     * <x-table> via api.assets.requestable. Row shape comes from
     * AssetsTransformer with available_actions.request/cancel
     * populated so the assetRequestActionsFormatter JS helper can
     * render the request/cancel button-swap. Custom fields flagged
     * show_in_requestable_list=1 append as extra columns so admin-
     * defined per-asset attributes surface here without a code
     * change.
     */
    public static function dataTableLayoutRequestable(): string
    {
        $layout = [
            [
                'field' => 'image',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => true,
                'title' => trans('general.image'),
                'formatter' => 'imageFormatter',
            ], [
                'field' => 'asset_tag',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('general.asset_tag'),
            ], [
                'field' => 'model',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/table.asset_model'),
            ], [
                'field' => 'model_number',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/models/table.modelnumber'),
            ], [
                'field' => 'name',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                // Use the same trans key the import wizard uses for the
                // Name target label so the assets datatable download
                // round-trips through the importer's auto-mapper on
                // every locale. See ReportsController::postCustom for
                // the sibling change and the rationale.
                'title' => trans('general.item_name_var', ['item' => trans('general.asset')]),
            ], [
                'field' => 'serial',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/table.serial'),
            ], [
                // Full object (not .name) so the formatter can read
                // tag_color for the color-square icon prefix.
                // Not searchable because Asset::$searchableRelations
                // doesn't include category (Asset's category lives
                // through the model relation, not a direct join),
                // so a `search=` query wouldn't hit it.
                'field' => 'category',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => false,
                'title' => trans('general.category'),
                'formatter' => 'categoriesLinkObjFormatter',
            ], [
                'field' => 'company.name',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => false,
                'title' => trans('general.company'),
            ], [
                'field' => 'location',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/table.location'),
            ], [
                'field' => 'status',
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => trans('admin/hardware/table.status'),
            ], [
                'field' => 'expected_checkin',
                'scope' => 'col',
                'searchable' => false,
                'sortable' => true,
                'title' => trans('admin/hardware/form.expected_checkin'),
                'formatter' => 'dateDisplayFormatter',
            ],
        ];

        // Custom fields flagged for the requestable list. Same
        // fieldset-must-be-attached-to-a-model guard as the standard
        // asset layout uses (see dataTableLayout above) so the JS
        // side doesn't ask for row properties that never travel over
        // the REST API.
        $fields = CustomField::whereHas('fieldset', function ($query) {
            $query->whereHas('models');
        })->where('field_encrypted', 0)
            ->where('show_in_requestable_list', 1)
            ->get();

        foreach ($fields as $field) {
            $layout[] = [
                'field' => 'custom_fields.'.$field->db_column,
                'scope' => 'col',
                'searchable' => true,
                'sortable' => true,
                'title' => e($field->name),
            ];
        }

        $layout[] = [
            'field' => 'actions',
            'scope' => 'col',
            'searchable' => false,
            'sortable' => false,
            'switchable' => false,
            'title' => trans('table.actions'),
            'formatter' => 'assetRequestActionsFormatter',
            'printIgnore' => true,
            'class' => 'hidden-print',
        ];

        return json_encode($layout);
    }
}