{{-- resources/views/approvals/instances/show.blade.php --}} @extends('layouts.app') @section('title', 'Approval Details') @section('page-header')

Approval Details

Back
@endsection @section('content') @php // Helper function to safely format dates $formatDate = function($date) { if (!$date) return 'N/A'; if ($date instanceof \DateTime) return $date->format('M d, Y H:i'); if (is_string($date)) return date('M d, Y H:i', strtotime($date)); return 'N/A'; }; // ============================================================ // GET DOCUMENT STATUS FROM DOCUMENTS TABLE (ENUM only) // ============================================================ $documentStatus = 'Draft'; $documentStatusColor = 'secondary'; $document = null; $documentId = null; if (isset($instance)) { if ($instance->record_type === 'DOCUMENT') { $document = \App\Models\Document::where('document_id', $instance->record_id)->first(); if ($document) { $documentId = $document->document_id; } } elseif ($instance->record_type === 'sop') { $document = \App\Models\Document::where('source_id', $instance->record_id) ->where('source_type', 'sop') ->first(); if ($document) { $documentId = $document->document_id; } } if ($document) { // Only use ENUM values from documents table $documentStatus = $document->document_status ?? 'Draft'; // Map document status to badge color (ENUM values only) $documentStatusColor = match($documentStatus) { 'Approved' => 'success', 'Under Review' => 'warning', 'Draft' => 'secondary', 'Rejected' => 'danger', 'Archived' => 'dark', 'Request_Changes' => 'info', default => 'secondary' }; } } // ============================================================ // GET APPROVAL STATUS (from universal_approval_instances) // ============================================================ $approvalStatus = $instance->status ?? 'PENDING'; $approvalStatusColor = match($approvalStatus) { 'APPROVED' => 'success', 'REJECTED' => 'danger', 'IN_PROGRESS' => 'info', 'PENDING' => 'warning', 'CANCELLED' => 'secondary', default => 'secondary' }; // ============================================================ // CALCULATE WORKFLOW PROGRESS // ============================================================ $totalSteps = $steps->count(); $completedSteps = $instance->actions->where('action', '!=', 'PENDING')->count(); $progress = $totalSteps > 0 ? ($completedSteps / $totalSteps) * 100 : 0; // ============================================================ // CHECK IF USER CAN APPROVE // ============================================================ $canApprove = false; $currentStep = $steps->firstWhere('step_order', $instance->current_step); $userId = auth()->id(); $commentsAllowed = (bool) ($currentStep?->allow_comments ?? false); $attachmentsAllowed = (bool) ($currentStep?->allow_attachments ?? false); $signatureRequired = (bool) ($currentStep?->require_esignature ?? false); $signatureUser = $currentStep?->approver_type === 'USER' && $currentStep->approver_user_id ? \App\Models\User::with('employee')->find($currentStep->approver_user_id) : auth()->user(); $employeeSignature = $signatureUser?->employee?->employee_signature; $employeeSignaturePath = $employeeSignature ? str_replace('\\', '/', $employeeSignature) : null; $employeeSignaturePath = $employeeSignaturePath ? preg_replace('#^(?:.*?/)?storage/app/public/|^public/storage/#i', '', ltrim($employeeSignaturePath, '/')) : null; $employeeSignatureUrl = $employeeSignature ? (str_starts_with($employeeSignature, 'http') ? $employeeSignature : asset('storage/' . ltrim($employeeSignaturePath, '/'))) : null; // Debug information $debugInfo = [ 'user_id' => $userId, 'approval_status' => $approvalStatus, 'document_status' => $documentStatus, 'current_step' => $instance->current_step, 'step_approver_user_id' => $currentStep ? $currentStep->approver_user_id : null, 'step_approver_type' => $currentStep ? $currentStep->approver_type : null, 'step_required_role' => $currentStep ? $currentStep->required_role : null, ]; // Check if user can approve based on: // 1. Approval status is PENDING or IN_PROGRESS (from universal_approval_instances) // 2. Document status is NOT Approved, Rejected, or Archived (from documents table) // 3. User is the approver for the current step if ($currentStep && in_array($approvalStatus, ['PENDING', 'IN_PROGRESS']) && !in_array($documentStatus, ['Approved', 'Rejected', 'Archived'])) { // Check if the user is the direct approver for this step (USER type) if ($currentStep->approver_type == 'USER' && $currentStep->approver_user_id == $userId) { $canApprove = true; } // Check if the user has the required role (ROLE type) elseif ($currentStep->approver_type == 'ROLE' && !empty($currentStep->required_role)) { if (auth()->user()->hasRole($currentStep->required_role)) { $canApprove = true; } } } // Check if user has already acted on THIS INSTANCE for this step $hasActed = false; if ($currentStep) { $hasActed = $instance->actions ->where('step_id', $currentStep->step_id) ->where('approver_user_id', $userId) ->where('action', '!=', 'PENDING') ->isNotEmpty(); } // Get the action for the current step (if any) $currentStepAction = null; if ($currentStep) { $currentStepAction = $instance->actions ->where('step_id', $currentStep->step_id) ->where('action', '!=', 'PENDING') ->first(); } // ============================================================ // DETERMINE IF DOCUMENT IS COMPLETED (based on document status) // ============================================================ $isFullyApproved = $documentStatus == 'Approved'; $isRejected = $documentStatus == 'Rejected'; $isArchived = $documentStatus == 'Archived'; $isUnderReview = $documentStatus == 'Under Review'; $isDraft = $documentStatus == 'Draft'; $isRequestChanges = $documentStatus == 'Request_Changes'; // Check if user is the approver for the current step $isUserApprover = false; if ($currentStep) { if ($currentStep->approver_type == 'USER' && $currentStep->approver_user_id == $userId) { $isUserApprover = true; } elseif ($currentStep->approver_type == 'ROLE' && !empty($currentStep->required_role)) { $isUserApprover = auth()->user()->hasRole($currentStep->required_role); } } @endphp @if(session('success')) @endif @if(session('error')) @endif
Document Preview
{{ $instance->record_type }} - {{ $instance->record_number }}
{{ $documentStatus }}
Title: {{ $instance->record_title }}
Initiator: {{ $instance->initiator->name ?? 'N/A' }} on {{ $formatDate($instance->initiated_at) }}
Document Status: {{ $documentStatus }}
Approval Status: {{ $approvalStatus }} @if($approvalStatus == 'IN_PROGRESS') Step {{ $instance->current_step }} of {{ $totalSteps }} @endif
Instance ID: #{{ $instance->instance_id }} @if($documentId) | Document ID: #{{ $documentId }} @endif
Workflow Progress
{{ number_format($progress, 0) }}% Complete
Step {{ $instance->current_step }} of {{ $totalSteps }}
Approval Timeline
@foreach($steps as $step) @php $stepActions = $instance->actions->where('step_id', $step->step_id); $action = $stepActions->first(function ($stepAction) { return $stepAction->action !== 'PENDING'; }) ?? $stepActions->first(); $isCompleted = $action !== null && $action->action != 'PENDING'; $isCurrent = $step->step_order == $instance->current_step && !$isCompleted; // Check if current user is approver for this step $isUserApproverForStep = false; if ($step->approver_type == 'USER' && $step->approver_user_id == $userId) { $isUserApproverForStep = true; } elseif ($step->approver_type == 'ROLE' && !empty($step->required_role)) { $isUserApproverForStep = auth()->user()->hasRole($step->required_role); } @endphp
Step {{ $step->step_order }}: {{ $step->step_name }}
{{ $isCompleted ? 'Completed' : ($isCurrent ? 'In Progress' : 'Pending') }}
@if($action && $action->action != 'PENDING')
{{ $action->approver->name ?? 'N/A' }} {{ $action->action_type == 'APPROVE' && $step->step_order < $totalSteps ? 'REVIEWED' : $action->action_type }}
@if($action->comments)

{{ $action->comments }}

@endif {{ $formatDate($action->action_date) }}
@elseif($isCurrent)
@if($isUserApproverForStep) Your Action Required @else Waiting for approver @endif
@endif
@endforeach
@if($instance->actions->count() > 0)
Actions History
@foreach($instance->actions->where('action', '!=', 'PENDING') as $action) @endforeach
Date/Time Step Approver Action Comments
{{ $formatDate($action->action_date) }} {{ $action->step->step_name ?? 'N/A' }} {{ $action->approver->name ?? 'N/A' }} {{ $action->action_type }} {{ $action->comments ?: '-' }}
@endif
Debug Information
Current User ID: {{ $userId }}
Approval Status: {{ $approvalStatus }}
Document Status: {{ $documentStatus }}
Current Step: {{ $instance->current_step }}
Step Approver User ID: {{ $currentStep ? $currentStep->approver_user_id : 'N/A' }}
Step Approver Type: {{ $currentStep ? $currentStep->approver_type : 'N/A' }}
Required Role: {{ $currentStep ? $currentStep->required_role : 'N/A' }}
Is User Approver: {{ $isUserApprover ? '✅ YES' : '❌ NO' }}
Has Acted: {{ $hasActed ? '✅ YES' : '❌ NO' }}
Can Approve: {{ $canApprove ? '✅ YES' : '❌ NO' }}
Document Status (ENUM): {{ $documentStatus }}
Is Fully Approved: {{ $isFullyApproved ? 'YES' : 'NO' }}
Is Rejected: {{ $isRejected ? 'YES' : 'NO' }}
Is Archived: {{ $isArchived ? 'YES' : 'NO' }}
Details
#{{ $instance->instance_id }}
{{ $instance->workflow->workflow_name }}
{{ $instance->workflow->module_type }}
{{ $instance->initiator->name ?? 'N/A' }}
{{ $formatDate($instance->initiated_at) }}
{{ $documentStatus }}
{{ $approvalStatus }} @if($approvalStatus == 'IN_PROGRESS') Step {{ $instance->current_step }} @endif
{{ $currentStep?->step_name ?? 'N/A' }}
@if($instance->completed_at)
{{ $formatDate($instance->completed_at) }}
@if($instance->final_decision_by)
{{ $instance->finalDecisionBy->name ?? 'N/A' }}
@endif @endif
@if($signatureRequired || $employeeSignatureUrl)
Employee Signature
@if($employeeSignatureUrl)
Registered electronic signature
Electronic signature of {{ $signatureUser?->name ?? 'assigned approver' }}
{{ $signatureUser?->name ?? 'Assigned approver' }}
Available for this approval step
@else
No employee signature is registered.
@endif @if($signatureRequired)
E-signature confirmation is required before this step can be submitted.
@endif
@endif @if($canApprove && !$hasActed && !$isFullyApproved && !$isRejected && !$isArchived)
Take Action
Instance #{{ $instance->instance_id }}
You are the approver for Step {{ $currentStep->step_order }}: {{ $currentStep->step_name }} @if($currentStep->required_role)
Role: {{ $currentStep->required_role }} @endif
Document Status: {{ $documentStatus }}
Approval Status: {{ $approvalStatus }}
@csrf @if($commentsAllowed)
@endif @if($attachmentsAllowed)
Maximum 10 MB per file.
@endif @if($signatureRequired)
An electronic signature is required for this step. @if($employeeSignatureUrl)
Signature to be applied:
Employee signature
@else
No employee signature is registered for your account.
@endif
@error('signature_confirmation')
{{ $message }}
@enderror
@endif
@if($currentStep->step_order < $totalSteps) @endif
Your action will be recorded with electronic signature
@elseif($hasActed && !$isFullyApproved && !$isRejected && !$isArchived)
Action Taken
You have already acted on this step for Instance #{{ $instance->instance_id }}.

Please wait for the next approver to take action.

Document Status: {{ $documentStatus }}

@if($currentStepAction)
Your action: {{ $currentStepAction->action_type }} @if($currentStepAction->comments)
Comment: {{ $currentStepAction->comments }} @endif
@endif
@elseif($isFullyApproved)
Approved
This document has been fully approved.

Document Status: Approved
@if($instance->completed_at) Approved on: {{ $formatDate($instance->completed_at) }} @endif

@elseif($isRejected)
Rejected
This document has been rejected.

Document Status: Rejected
@if($instance->completed_at) Rejected on: {{ $formatDate($instance->completed_at) }} @endif

@elseif($isArchived)
Archived
This document has been archived.

Document Status: Archived

@elseif(!$canApprove && !$isFullyApproved && !$isRejected && !$isArchived)
Waiting for Approver
You are not the approver for this step.

Current Step: {{ $currentStep?->step_name ?? 'Unknown' }}
Document Status: {{ $documentStatus }}
Approval Status: {{ $approvalStatus }}
This instance (#{{ $instance->instance_id }}) is awaiting action from the designated approver. @if($currentStep && $currentStep->approver_user_id)
Approver: User ID {{ $currentStep->approver_user_id }} @endif @if($currentStep && $currentStep->required_role)
Required Role: {{ $currentStep->required_role }} @endif

@endif
Quick Actions
View All Approvals @if($instance->record_type == 'DOCUMENT' && isset($instance->record_id)) View Document @endif
@endsection @push('styles') @endpush @push('scripts') @endpush