{{-- Repeatable guarantor capture for individual loan applications. @param \App\Models\LoanBookApplication|null $application @param \Illuminate\Support\Collection|null $existingGuarantors @param bool $unifiedAppraisal One card per person: KYC + security + signature + single save --}} @php use App\Support\Validation\KenyanFieldRules; $unifiedAppraisal = (bool) ($unifiedAppraisal ?? false); $tabWorkspace = (bool) ($tabWorkspace ?? false); $appraisal = $appraisal ?? ($application?->appraisal ?? null); $rawGuarantors = $existingGuarantors ?? ($application?->guarantors ?? collect()); $existing = $rawGuarantors ->values() ->map(fn ($g, $idx) => [ 'loan_book_guarantor_id' => $g->id, 'sequence' => $g->sequence, 'capture_mode' => old("guarantors.{$idx}.capture_mode", filled(data_get($g->meta_json, 'client_source_id')) ? 'client' : (filled(data_get($g->meta_json, 'register_source_id')) ? 'existing' : 'new')), 'register_guarantor_id' => old("guarantors.{$idx}.register_guarantor_id", data_get($g->meta_json, 'register_source_id')), 'loan_client_id' => old("guarantors.{$idx}.loan_client_id", data_get($g->meta_json, 'client_source_id') ?? $g->guarantor_client_id), 'full_name' => old("guarantors.{$idx}.full_name", $g->full_name), 'phone' => old("guarantors.{$idx}.phone", $g->phone), 'id_number' => old("guarantors.{$idx}.id_number", $g->id_number), 'relationship' => old("guarantors.{$idx}.relationship", $g->relationship), 'address' => old("guarantors.{$idx}.address", $g->address()), 'latitude' => old("guarantors.{$idx}.latitude", $g->latitude()), 'longitude' => old("guarantors.{$idx}.longitude", $g->longitude()), 'gps_accuracy_meters' => old("guarantors.{$idx}.gps_accuracy_meters", data_get($g->meta_json, 'gps_accuracy_meters')), 'gps_source' => old("guarantors.{$idx}.gps_source", data_get($g->meta_json, 'gps_source')), 'landmark' => old("guarantors.{$idx}.landmark", data_get($g->meta_json, 'landmark')), 'residential_status' => old("guarantors.{$idx}.residential_status", data_get($g->meta_json, 'residential_status')), 'gender' => old("guarantors.{$idx}.gender", data_get($g->meta_json, 'gender')), 'marital_status' => old("guarantors.{$idx}.marital_status", data_get($g->meta_json, 'marital_status')), 'date_of_birth' => old("guarantors.{$idx}.date_of_birth", data_get($g->meta_json, 'date_of_birth')), 'title' => old("guarantors.{$idx}.title", data_get($g->meta_json, 'title')), 'is_primary' => (bool) old("guarantors.{$idx}.is_primary", $g->isPrimary()), 'guarantee_amount' => old("guarantors.{$idx}.guarantee_amount", $g->guaranteeAmount()), 'is_client' => $g->isClientLinked(), 'photo_path' => old("guarantors.{$idx}.photo_path", $g->photo_path), 'id_front_photo_path' => old("guarantors.{$idx}.id_front_photo_path", $g->id_front_photo_path), 'id_back_photo_path' => old("guarantors.{$idx}.id_back_photo_path", $g->id_back_photo_path), 'photo_url' => $g->kycImageUrl($g->photo_path), 'id_front_url' => $g->kycImageUrl($g->id_front_photo_path), 'id_back_url' => $g->kycImageUrl($g->id_back_photo_path), 'security_items' => collect($g->appraisalSecurityItems())->whenEmpty(fn ($c) => $c->push([ 'item' => '', 'description' => '', 'identification' => '', 'value' => '', ]))->values()->all(), 'show_security_editor' => collect($g->appraisalSecurityItems())->contains( fn ($row) => filled($row['item'] ?? null) || filled($row['description'] ?? null) || filled($row['identification'] ?? null) || filled($row['value'] ?? null), ), 'existing_signature_url' => $g->appraisalSignatureDataUri(), 'guarantor_signed_at' => \App\Support\LoanBook\SignatureTimestamp::forInput( data_get($g->meta_json, 'appraisal.signed_at') ), 'signature_data' => '', ]) ->all(); if ($existing === [] && is_array(old('guarantors'))) { $existing = collect(old('guarantors'))->values()->map(fn ($row) => array_merge([ 'capture_mode' => 'new', 'register_guarantor_id' => '', 'loan_client_id' => '', 'full_name' => '', 'phone' => '', 'id_number' => '', 'relationship' => '', 'address' => '', 'latitude' => null, 'longitude' => null, 'gps_accuracy_meters' => null, 'gps_source' => '', 'landmark' => '', 'residential_status' => '', 'gender' => '', 'marital_status' => '', 'date_of_birth' => '', 'title' => '', 'is_primary' => false, 'guarantee_amount' => null, 'is_client' => false, 'photo_path' => '', 'id_front_photo_path' => '', 'id_back_photo_path' => '', 'photo_url' => null, 'id_front_url' => null, 'id_back_url' => null, ], is_array($row) ? $row : []))->all(); } if ($existing === []) { $existing = [[ 'loan_book_guarantor_id' => null, 'sequence' => null, 'capture_mode' => 'new', 'register_guarantor_id' => '', 'loan_client_id' => '', 'full_name' => '', 'phone' => '', 'id_number' => '', 'relationship' => '', 'address' => '', 'latitude' => null, 'longitude' => null, 'gps_accuracy_meters' => null, 'gps_source' => '', 'landmark' => '', 'residential_status' => '', 'gender' => '', 'marital_status' => '', 'date_of_birth' => '', 'title' => '', 'is_primary' => true, 'guarantee_amount' => null, 'is_client' => false, 'photo_path' => '', 'id_front_photo_path' => '', 'id_back_photo_path' => '', 'photo_url' => null, 'id_front_url' => null, 'id_back_url' => null, 'security_items' => [['item' => '', 'description' => '', 'identification' => '', 'value' => '']], 'existing_signature_url' => null, 'guarantor_signed_at' => \App\Support\LoanBook\SignatureTimestamp::nowForInput(), 'signature_data' => '', ]]; } $guaranteeAmount = (float) old('amount_requested', $application?->amount_requested ?? 0); $borrowerClientId = (int) ($application?->loan_client_id ?? 0); $eligibilityRules = app(\App\Services\LoanBook\LendingPolicySettings::class)->eligibilityRules(); $guarantorRequired = (bool) ($eligibilityRules['guarantor_required'] ?? false); $guarantorPhotosRequired = (bool) ($eligibilityRules['guarantor_photos_required'] ?? false); $guarantorLocationRequired = (bool) ($eligibilityRules['guarantor_location_required'] ?? false); $guarantorIdRequired = (bool) ($eligibilityRules['guarantor_id_required'] ?? false); $repeatExistingGuarantorWaive = $application ? \App\Support\LoanBook\RepeatExistingGuarantorKycWaiver::appliesForApplication($application) : \App\Support\LoanBook\RepeatExistingGuarantorKycWaiver::appliesForClient( $loanClient ?? $prefilledClient ?? $selectedClient ?? null ); $registerShowUrlTemplate = str_replace('999999', '__ID__', route('loan.lookups.guarantors.show', ['loan_book_guarantor' => 999999])); $clientSearchUrl = route('loan.lookups.guarantors.clients.search'); $clientShowUrlTemplate = str_replace('999999', '__ID__', route('loan.lookups.guarantors.clients.show', ['loan_client' => 999999])); $pastGuarantorCollateral = $pastGuarantorCollateral ?? []; @endphp
@if ($tabWorkspace)
@elseif ($unifiedAppraisal)
@else

Guarantors @if ($guarantorRequired)*@elseOptional@endif

@endif @error('guarantors')

{{ $message }}

@enderror