ultimatepos/Modules/Portal/Http/Controllers/Customer/EquipmentController.php

69 lines
2.3 KiB
PHP

<?php
namespace Modules\Portal\Http\Controllers\Customer;
use Illuminate\Support\Facades\Schema;
use Modules\Portal\Http\Controllers\BasePortalController;
use Modules\Portal\Services\CustomerMaintenanceScopeService;
class EquipmentController extends BasePortalController
{
public function index()
{
$this->ensureCrmSubscription();
$scope = CustomerMaintenanceScopeService::forCurrentUser();
$equipment = $scope->equipmentQuery()
->with(['category:id,name', 'project:id,name', 'productionLine:id,name'])
->orderBy('name')
->paginate(20);
return view('portal::customer.equipment.index', compact('equipment'));
}
public function show($id)
{
$this->ensureCrmSubscription();
$scope = CustomerMaintenanceScopeService::forCurrentUser();
if (! $scope->ownsEquipment((int) $id)) {
abort(404);
}
$item = $scope->equipmentQuery()
->with([
'category',
'project',
'productionLine',
])
->findOrFail($id);
if ($scope->preventiveSchedulesAvailable()) {
$item->load(['preventiveSchedules' => fn ($q) => $q->orderBy('next_due_at')]);
}
if ($scope->workOrdersAvailable()) {
$item->load(['workOrders' => fn ($q) => $q->with('assignedTechnician:id,first_name,last_name')->latest()->limit(10)]);
}
$inspections = collect();
if (Schema::hasTable('maintenance_inspection_visits')) {
$inspections = \Modules\Maintenance\Models\InspectionVisit::where('business_id', $this->businessId())
->where('equipment_id', $item->id)
->orderByDesc('visit_date')
->limit(10)
->get();
}
$serviceRecords = collect();
if (Schema::hasTable('maintenance_service_records')) {
$serviceRecords = \Modules\Maintenance\Models\ServiceRecord::where('business_id', $this->businessId())
->where('equipment_id', $item->id)
->orderByDesc('completed_at')
->limit(10)
->get();
}
return view('portal::customer.equipment.show', compact('item', 'inspections', 'serviceRecords'));
}
}