@extends('layouts.portal') @section('title', 'Will Management') @section('page_title', 'Will Management') @section('page_subtitle', 'Manage client wills') @section('sidebar') @include('agent.partials.sidebar', ['active' => 'wills']) @endsection {{-- Helper function to safely count values --}} @php if (!function_exists('safeCount')) { function safeCount($value) { if (is_array($value) || ($value instanceof Countable)) { return count($value); } return 0; } } @endphp @section('content')

{{ $stats['total'] }}

Total Wills

{{ $stats['under_review'] }}

Pending Review

{{ $stats['drafting'] }}

Drafting

{{ $stats['stored'] }}

Completed

All Wills

@forelse ($wills as $will) @php // Check if this is a trust or will based on model class, table, or _source_table attribute // IMPORTANT: Only mark as trust if it's actually from the Trust model/table $isItATrust = (get_class($will) === \App\Models\Trust::class || $will->getTable() === 'trusts' || ($will->_source_table ?? '') === 'trusts'); // DEBUG: Log model info for debugging \Log::info("Will #{$will->id}: Model=" . get_class($will) . ", Table=" . $will->getTable() . ", SourceTable=" . ($will->_source_table ?? 'N/A') . ", WillType=" . ($will->will_type ?? 'N/A') . ", IsItATrust=" . ($isItATrust ? 'YES' : 'NO') . ", Status=" . $will->status); // For trusts, calculate progress based on completed sections if ($isItATrust) { $willType = 'Trust'; // Check trust sections completion $hasTrustCreator = !empty($will->trust_creator) || !empty($will->trust_creator_details); $hasTrustDetails = !empty($will->trust_name) && !empty($will->trust_type) && !empty($will->trust_purpose); $hasAssets = \DB::table('trust_assets')->where('trust_id', $will->id)->exists(); $hasBeneficiaries = \DB::table('trust_beneficiaries')->where('trust_id', $will->id)->exists(); $hasCaretakers = \DB::table('trust_caretakers')->where('trust_id', $will->id)->exists(); $hasAllowances = \DB::table('trust_allowances')->where('trust_id', $will->id)->exists(); $hasGifts = \DB::table('trust_gifts')->where('trust_id', $will->id)->exists(); // Final Setup is complete if any of the final setup related tables have data // (admin allowances, guardian allowances, or donations) $hasAdminAllowances = \DB::table('trust_administrator_allowances')->where('trust_id', $will->id)->exists(); $hasGuardianAllowances = \DB::table('trust_guardian_allowances')->where('trust_id', $will->id)->exists(); $hasDonations = \DB::table('trust_donations')->where('trust_id', $will->id)->exists(); // Final setup is considered complete if ANY of these exist, OR if trust is already approved/stored $hasFinalSetup = $hasAdminAllowances || $hasGuardianAllowances || $hasDonations || in_array($will->status, ['stored', 'approved', 'signed', 'completed']); // Count completed sections $completedSections = 0; if ($hasTrustCreator) $completedSections++; if ($hasTrustDetails) $completedSections++; if ($hasAssets) $completedSections++; if ($hasBeneficiaries) $completedSections++; if ($hasCaretakers) $completedSections++; if ($hasAllowances) $completedSections++; if ($hasGifts) $completedSections++; if ($hasFinalSetup) $completedSections++; $totalSections = 8; // Trust has 8 main sections $progressPercentage = ($totalSections > 0) ? ($completedSections / $totalSections) * 100 : 0; // Determine resume route - redirect to FIRST incomplete step if (!$hasTrustCreator || !$hasTrustDetails) { $resumeRoute = route('agent.trusts.create', ['client' => $will->client_id]); } elseif (!$hasAssets) { $resumeRoute = route('agent.trusts.assets.index', $will->id); } elseif (!$hasBeneficiaries) { $resumeRoute = route('agent.trusts.beneficiaries.index', $will->id); } elseif (!$hasCaretakers) { $resumeRoute = route('agent.trusts.caretakers.index', $will->id); } elseif (!$hasAllowances) { $resumeRoute = route('agent.trusts.allowances.index', $will->id); } elseif (!$hasGifts) { $resumeRoute = route('agent.trusts.gifts.index', $will->id); } elseif (!$hasFinalSetup) { $resumeRoute = route('agent.trusts.final-setup.index', $will->id); } else { // All sections complete, go to show/review page $resumeRoute = route('agent.trusts.show', $will); } } else { // Parse instructions - handle both string (JSON) and array formats $instructions = []; if ($will->instructions) { if (is_string($will->instructions)) { $instructions = json_decode($will->instructions, true) ?? []; } else if (is_array($will->instructions)) { $instructions = $will->instructions; } } // Testator sections $testatorDetails = is_array($will->testator_details) ? $will->testator_details : (json_decode($will->testator_details, true) ?? []); $hasPrimary = !empty($testatorDetails['primary']['full_name']); $hasSecondary = !empty($testatorDetails['secondary']['full_name']); $hasExecutor = !empty($testatorDetails['executor']['full_name']); $hasGuardian = !empty($testatorDetails['guardian']['full_name']); // Other sections - check database tables directly for accurate count $hasBeneficiaries = \DB::table('will_beneficiaries')->where('will_id', $will->id)->exists() || (!empty($will->beneficiaries) && safeCount($will->beneficiaries) > 0); // Check assets in multiple places: will_assets table, asset_allocations field, or instructions JSON $assetAllocations = is_array($will->asset_allocations) ? $will->asset_allocations : (json_decode($will->asset_allocations, true) ?? []); $instructionsAssets = is_array($instructions['assets'] ?? null) ? $instructions['assets'] : (json_decode($instructions['assets'] ?? '[]', true) ?? []); $hasAssets = \DB::table('will_assets')->where('will_id', $will->id)->exists() || (!empty($assetAllocations) && safeCount($assetAllocations) > 0) || (!empty($instructionsAssets) && safeCount($instructionsAssets) > 0); $hasLiabilities = \DB::table('will_liabilities')->where('will_id', $will->id)->exists(); // Check existing will - marked complete if either: // 1. User explicitly said "no" to existing will // 2. User said "yes" AND provided the details $hasExistingWill = (!empty($instructions['has_existing_will']) && $instructions['has_existing_will'] === 'no') || (!empty($instructions['has_existing_will']) && $instructions['has_existing_will'] === 'yes' && !empty($instructions['existing_will'])); // Check database relationships for gifts, donations, witnesses $instructionsGifts = is_array($instructions['gifts'] ?? null) ? $instructions['gifts'] : (json_decode($instructions['gifts'] ?? '[]', true) ?? []); $instructionsDonations = is_array($instructions['donations'] ?? null) ? $instructions['donations'] : (json_decode($instructions['donations'] ?? '[]', true) ?? []); $instructionsWitnesses = is_array($instructions['witnesses'] ?? null) ? $instructions['witnesses'] : (json_decode($instructions['witnesses'] ?? '[]', true) ?? []); $instructionsAllowances = is_array($instructions['allowances'] ?? null) ? $instructions['allowances'] : (json_decode($instructions['allowances'] ?? '[]', true) ?? []); $instructionsTrustees = is_array($instructions['trustees'] ?? null) ? $instructions['trustees'] : (json_decode($instructions['trustees'] ?? '[]', true) ?? []); $hasGifts = \DB::table('will_gifts')->where('will_id', $will->id)->exists() || (!empty($instructionsGifts) && safeCount($instructionsGifts) > 0) || (!empty($instructions['gifts_section_completed'])); $hasDonations = \DB::table('will_donations')->where('will_id', $will->id)->exists() || (!empty($instructionsDonations) && safeCount($instructionsDonations) > 0) || (!empty($instructions['donations_section_completed'])); $hasWitnesses = \DB::table('will_witnesses')->where('will_id', $will->id)->exists() || (!empty($instructionsWitnesses) && safeCount($instructionsWitnesses) > 0); $hasAllowances = (!empty($instructionsAllowances) && safeCount($instructionsAllowances) > 0) || (!empty($instructions['allowances_section_completed'])); $hasTrustees = (!empty($instructionsTrustees) && safeCount($instructionsTrustees) > 0) || (!empty($instructions['trustees_section_completed'])); // DEBUG: Log what sections are complete for this will if ($will->id == 207 || $will->id == 208) { \Log::info("=== DEBUG Will #{$will->id} Sections ==="); \Log::info("hasPrimary: " . ($hasPrimary ? 'YES' : 'NO')); \Log::info("hasBeneficiaries: " . ($hasBeneficiaries ? 'YES' : 'NO')); \Log::info("hasAssets: " . ($hasAssets ? 'YES' : 'NO')); \Log::info("hasLiabilities: " . ($hasLiabilities ? 'YES' : 'NO')); \Log::info("hasExistingWill: " . ($hasExistingWill ? 'YES' : 'NO')); \Log::info("hasAllowances: " . ($hasAllowances ? 'YES' : 'NO')); \Log::info("hasGifts: " . ($hasGifts ? 'YES' : 'NO')); \Log::info("hasDonations: " . ($hasDonations ? 'YES' : 'NO')); \Log::info("hasTrustees: " . ($hasTrustees ? 'YES' : 'NO')); \Log::info("hasWitnesses: " . ($hasWitnesses ? 'YES' : 'NO')); \Log::info("Instructions type: " . gettype($will->instructions)); \Log::info("Instructions value: " . json_encode($instructions)); \Log::info("Testator Details: ", $testatorDetails); // Check database tables \Log::info("DB Check - Beneficiaries count: " . \DB::table('will_beneficiaries')->where('will_id', $will->id)->count()); \Log::info("DB Check - Assets count: " . \DB::table('will_assets')->where('will_id', $will->id)->count()); \Log::info("DB Check - Liabilities count: " . \DB::table('will_liabilities')->where('will_id', $will->id)->count()); \Log::info("DB Check - Gifts count: " . \DB::table('will_gifts')->where('will_id', $will->id)->count()); \Log::info("DB Check - Donations count: " . \DB::table('will_donations')->where('will_id', $will->id)->count()); \Log::info("DB Check - Witnesses count: " . \DB::table('will_witnesses')->where('will_id', $will->id)->count()); } // Calculate completed sections count $completedSections = 0; $totalSections = 9; // Default for individual/joint wills // Check if this is an Islamic will if ($will->will_type === 'islamic' || $will->will_type === 'islamic_will') { // Islamic Will Sections (9 total): // 1. Testator, 2. Islamic Info, 3. Wasi, 4. Assets, // 5. Liabilities, 6. Funeral, 7. Wasiyyah, 8. Hibah, 9. Witnesses if ($hasPrimary) $completedSections++; // Check Islamic-specific sections $hasIslamicInfo = \DB::table('islamic_will_details')->where('will_id', $will->id)->exists(); if ($hasIslamicInfo) $completedSections++; $hasWasi = \DB::table('islamic_wasis')->where('will_id', $will->id)->exists(); if ($hasWasi) $completedSections++; if ($hasAssets) $completedSections++; if ($hasLiabilities) $completedSections++; $hasFuneral = \DB::table('islamic_funeral_instructions')->where('will_id', $will->id)->exists(); if ($hasFuneral) $completedSections++; $hasWasiyyah = \DB::table('islamic_bequests')->where('will_id', $will->id)->exists(); if ($hasWasiyyah) $completedSections++; $hasHibah = \DB::table('islamic_arrangements')->where('will_id', $will->id)->exists(); if ($hasHibah) $completedSections++; // Re-check witnesses for Islamic will (only from database) $hasIslamicWitnesses = \DB::table('will_witnesses')->where('will_id', $will->id)->exists(); if ($hasIslamicWitnesses) $completedSections++; $totalSections = 9; // Islamic will has 9 sections // Determine resume route for Islamic will if (!$hasPrimary) { $resumeRoute = route('agent.wills.testator', $will); } elseif (!$hasIslamicInfo) { $resumeRoute = route('agent.islamic.info', $will); } elseif (!$hasWasi) { $resumeRoute = route('agent.islamic.wasi', $will); } elseif (!$hasAssets) { $resumeRoute = route('agent.wills.assets', $will); } elseif (!$hasLiabilities) { $resumeRoute = route('agent.wills.liabilities', $will); } elseif (!$hasFuneral) { $resumeRoute = route('agent.islamic.funeral', $will); } elseif (!$hasWasiyyah) { $resumeRoute = route('agent.islamic.wasiyyah', $will); } elseif (!$hasHibah) { $resumeRoute = route('agent.islamic.hibah', $will); } elseif (!$hasIslamicWitnesses) { $resumeRoute = route('agent.wills.witnesses', $will); } else { $resumeRoute = route('agent.wills', $will); } } else { // Regular Individual/Joint Will sections if ($hasPrimary) $completedSections++; if ($hasBeneficiaries) $completedSections++; if ($hasAssets) $completedSections++; if ($hasLiabilities) $completedSections++; if ($hasExistingWill) $completedSections++; if ($hasAllowances) $completedSections++; if ($hasGifts) $completedSections++; if ($hasDonations) $completedSections++; // Only count trustees for trust wills if ($will->will_type === 'trust' && $hasTrustees) $completedSections++; if ($hasWitnesses) $completedSections++; // Total sections - 9 for individual/joint, 10 for trust wills $totalSections = ($will->will_type === 'trust') ? 10 : 9; // Determine resume route - redirect to FIRST incomplete step $resumeRoute = route('agent.wills.testator', $will); if (!$hasPrimary) { $resumeRoute = route('agent.wills.testator', $will); } elseif (!$hasBeneficiaries) { $resumeRoute = route('agent.wills.beneficiaries', $will); } elseif (!$hasAssets) { $resumeRoute = route('agent.wills.assets', $will); } elseif (!$hasLiabilities) { $resumeRoute = route('agent.wills.liabilities', $will); } elseif (!$hasExistingWill) { $resumeRoute = route('agent.wills.existing-will', $will); } elseif (!$hasAllowances) { $resumeRoute = route('agent.wills.allowances', $will); } elseif (!$hasGifts) { $resumeRoute = route('agent.wills.gifts', $will); } elseif (!$hasDonations) { $resumeRoute = route('agent.wills.donations', $will); } elseif ($will->will_type === 'trust' && !$hasTrustees) { $resumeRoute = route('agent.wills.trustees', $will); } elseif (!$hasWitnesses) { $resumeRoute = route('agent.wills.witnesses', $will); } } // Override if will is already submitted/completed if (in_array($will->status, ['stored', 'under_review', 'approved', 'signed', 'completed'])) { $completedSections = $totalSections; } $progressPercentage = ($totalSections > 0) ? ($completedSections / $totalSections) * 100 : 0; } @endphp @empty @endforelse
Will ID Client Will Type Status Created Actions
#{{ $will->id }}
{{ $will->client ? strtoupper(substr($will->client->full_name, 0, 1)) : '?' }}

{{ $will->client ? $will->client->full_name : 'Unknown Client' }}

{{ $will->client ? $will->client->email : 'No email' }}

@if($isItATrust) {{ ucfirst($will->trust_type ?? 'Trust') }} @else {{ ucfirst($will->will_type) }} @endif @if($will->status == 'corrections_requested') Corrections Requested @else {{ ucfirst(str_replace('_', ' ', $will->status)) }} @endif @if($will->status == 'corrections_requested' && $will->corrections_remarks)
{{ Str::limit($will->corrections_remarks, 50) }}
@endif
{{ $will->created_at->format('M d, Y') }}
{{ round($progressPercentage) }}%
{{ $completedSections }}/{{ $totalSections }} @if($completedSections == $totalSections) Complete @endif
@if($isItATrust) {{-- Trust-specific actions --}} @if(in_array($will->status, ['stored', 'under_review', 'approved', 'signed', 'completed'])) {{-- Trust already approved/stored - show view and edit buttons --}}
Current Version: v{{ $will->version ?? 1 }} | Last Updated: {{ $will->updated_at->format('M d, Y') }}
@else {{-- Trust in draft/drafting status --}}
@if($completedSections == 0) {{-- Trust not started yet --}} Start with Trust Creator Details → @elseif($completedSections < $totalSections) {{-- Trust in progress --}} Continue Trust Creation @else {{-- Trust completed but not submitted yet --}} @endif
@endif @elseif(in_array($will->status, ['drafting', 'draft'])) @if($completedSections == 0)
@if($will->will_type === 'islamic' || $will->will_type === 'islamic_will') Start with Islamic Information → @else Start with Testator Information → @endif
@elseif($completedSections < $totalSections) @else
@endif @elseif($will->status == 'corrections_requested')
Corrections Required
@if($will->corrections_remarks)

{{ $will->corrections_remarks }}

@endif
@if($will->will_type === 'islamic' || $will->will_type === 'islamic_will') Edit Corrections @else Edit Corrections @endif
@elseif(in_array($will->status, ['stored', 'under_review', 'approved', 'signed', 'completed']))
View Will (v{{ $will->version ?? 1 }}) @if($will->will_type === 'islamic' || $will->will_type === 'islamic_will') Edit Islamic Will @else Edit Will @endif
Current Version: v{{ $will->version ?? 1 }} | Last Updated: {{ $will->updated_at->format('M d, Y') }}
@elseif($will->status == 'pending_approval') {{-- Pending approval status - check if incomplete --}} @if($completedSections < $totalSections) {{-- Still incomplete, allow continuing --}} @else {{-- Complete and pending approval - Show Submit for Review button --}}
@if($isItATrust) @else @endif
@endif @else
{{ ucfirst(str_replace('_', ' ', $will->status)) }}
@endif
No wills found
@endsection