@extends('layouts.app') @section('title', 'Sucursales | Veltrix v2') @section('page_title', 'Sucursales') @section('page_subtitle', 'Gestion operativa en tiempo real') @section('page_icon', 'bx bxs-store') @section('content') @php $sucursalesRows = $sucursales instanceof \Illuminate\Support\Collection ? $sucursales : collect($sucursales ?? []); $usuariosRows = $usuarios instanceof \Illuminate\Support\Collection ? $usuarios : collect($usuarios ?? []); $cajasRows = $cajas instanceof \Illuminate\Support\Collection ? $cajas : collect($cajas ?? []); $paisesRows = $paises instanceof \Illuminate\Support\Collection ? $paises : collect($paises ?? []); $ciudadesRows = $ciudades instanceof \Illuminate\Support\Collection ? $ciudades : collect($ciudades ?? []); $branchLimitData = is_array($branchLimit ?? null) ? $branchLimit : null; $branchCountValue = (int) ($branchCount ?? $sucursalesRows->count()); $activeBranchesValue = (int) ($activeBranches ?? $sucursalesRows->where('activa', true)->count()); $totalUsersAssigned = (int) ($totalAssignedUsers ?? $usuariosRows->filter(static fn ($user): bool => (int) ($user->sucursal_id ?? 0) > 0)->count()); $totalCashBoxesValue = (int) ($totalCashBoxes ?? $cajasRows->count()); $branchCanCreateValue = (bool) ($branchCanCreate ?? true); $branchPlanName = trim((string) (($branchLimitData['plan_nombre'] ?? '') ?: 'Plan actual')); $branchMax = is_array($branchLimitData) ? max(0, (int) ($branchLimitData['max_sucursales'] ?? 0)) : null; $branchLimitReached = $branchMax !== null && ! $branchCanCreateValue; $planUsage = null; if ($branchMax !== null && $branchMax > 0) { $planUsage = min(100, (int) round(($branchCountValue / $branchMax) * 100)); } $usersByBranch = $usuariosRows ->filter(static fn ($user): bool => (int) ($user->sucursal_id ?? 0) > 0) ->groupBy(static fn ($user): int => (int) ($user->sucursal_id ?? 0)); $cashBoxesByBranch = $cajasRows ->filter(static fn ($caja): bool => (int) ($caja->sucursal_id ?? 0) > 0) ->groupBy(static fn ($caja): int => (int) ($caja->sucursal_id ?? 0)); $cityOptions = $ciudadesRows->map(static function ($city): array { $cityId = (int) ($city->id ?? 0); $countryId = (int) ($city->pais_id ?? 0); $cityName = trim((string) ($city->nombre ?? '')); $countryName = trim((string) ($city->pais->nombre ?? '')); $departmentName = trim((string) ($city->departamento ?? '')); $label = $cityName; if ($departmentName !== '') { $label .= ' - '.$departmentName; } return [ 'id' => $cityId, 'country_id' => $countryId, 'name' => $cityName, 'country' => $countryName, 'department' => $departmentName, 'label' => $label, ]; })->filter(static fn (array $city): bool => $city['id'] > 0)->values()->all(); $citiesById = []; foreach ($cityOptions as $city) { $citiesById[(int) $city['id']] = $city; } $oldFormContext = trim((string) old('_form_context', '')); $createSelectedCityId = $oldFormContext === 'create' ? (int) old('ciudad_id', 0) : 0; $createSelectedCountryId = $oldFormContext === 'create' ? (int) old('pais_id', $citiesById[$createSelectedCityId]['country_id'] ?? 0) : 0; $createSelectedDepartment = $oldFormContext === 'create' ? trim((string) old('departamento', $citiesById[$createSelectedCityId]['department'] ?? '')) : ''; if ($createSelectedCountryId <= 0 && $createSelectedCityId > 0) { $createSelectedCountryId = (int) ($citiesById[$createSelectedCityId]['country_id'] ?? 0); } $editingBranchFormId = $oldFormContext === 'update' ? (int) old('_branch_form_id', 0) : 0; @endphp @if ($errors->any())
{{ $errors->first() }}
@endif
Sucursales

{{ $branchCountValue }}

Activas

{{ $activeBranchesValue }}

Usuarios asignados

{{ $totalUsersAssigned }}

Cajas

{{ $totalCashBoxesValue }}

Límite por plan

@if ($branchMax !== null) {{ $branchPlanName }} · {{ $branchCountValue }}/{{ $branchMax }} sucursales usadas @else Sin plan activo · {{ $branchCountValue }} sucursales usadas @endif
{{ $branchLimitReached ? 'Límite alcanzado' : 'Disponible' }}
@if ($branchMax !== null && $branchMax > 0)
{{ $planUsage ?? 0 }}%
@elseif ($branchMax === 0)
Este plan no permite crear sucursales.
@else
No se encontró una suscripción activa para calcular límite de sucursales.
@endif

Nueva sucursal

@csrf
@if ($cityOptions === []) No hay ciudades cargadas. Importa el catálogo DANE. @endif

Gestión de sucursales

@if ($sucursalesRows->isEmpty())
No hay sucursales creadas.
@else
@foreach ($sucursalesRows as $index => $sucursal) @php $branchId = (int) ($sucursal->id ?? 0); $branchCity = trim((string) ($sucursal->ciudad->nombre ?? 'Sin ciudad')); $branchCode = trim((string) ($sucursal->codigo ?? '')); $branchActiveState = (bool) ($sucursal->activa ?? false); $assignedUsers = $usersByBranch->get($branchId, collect()); $boxes = $cashBoxesByBranch->get($branchId, collect()); $collapseId = 'vxBranchManage'.$branchId; $headerId = 'vxBranchManageHeading'.$branchId; $isFirst = $index === 0; $searchIndex = strtolower(trim((string) ($sucursal->nombre ?? '').' '.$branchCode.' '.$branchCity)); $branchCurrentCityId = (int) ($sucursal->ciudad_id ?? 0); $branchSelectedCityId = $editingBranchFormId === $branchId ? (int) old('ciudad_id', $branchCurrentCityId) : $branchCurrentCityId; $branchSelectedCountryId = $editingBranchFormId === $branchId ? (int) old('pais_id', $citiesById[$branchSelectedCityId]['country_id'] ?? ($sucursal->ciudad->pais_id ?? 0)) : (int) ($sucursal->ciudad->pais_id ?? 0); if ($branchSelectedCountryId <= 0 && $branchSelectedCityId > 0) { $branchSelectedCountryId = (int) ($citiesById[$branchSelectedCityId]['country_id'] ?? 0); } $branchSelectedDepartment = $editingBranchFormId === $branchId ? trim((string) old('departamento', $citiesById[$branchSelectedCityId]['department'] ?? ($sucursal->ciudad->departamento ?? ''))) : trim((string) ($sucursal->ciudad->departamento ?? '')); $branchNameInput = $editingBranchFormId === $branchId ? trim((string) old('nombre', (string) ($sucursal->nombre ?? ''))) : trim((string) ($sucursal->nombre ?? '')); $branchCodeInput = $editingBranchFormId === $branchId ? trim((string) old('codigo', $branchCode)) : $branchCode; $branchPhoneInput = $editingBranchFormId === $branchId ? trim((string) old('telefono', (string) ($sucursal->telefono ?? ''))) : trim((string) ($sucursal->telefono ?? '')); $branchAddressInput = $editingBranchFormId === $branchId ? trim((string) old('direccion', (string) ($sucursal->direccion ?? ''))) : trim((string) ($sucursal->direccion ?? '')); @endphp

@csrf
Creada: {{ optional($sucursal->created_at)->format('Y-m-d H:i:s') ?? '-' }}
@csrf
@endforeach
@endif

Usuarios por sucursal

@if ($sucursalesRows->isEmpty())

Primero crea una sucursal para asignar usuarios.

@else
@foreach ($sucursalesRows as $index => $sucursal) @php $branchId = (int) ($sucursal->id ?? 0); $assignedUsers = $usersByBranch->get($branchId, collect()); $collapseId = 'vxUsersBranch'.$branchId; $headerId = 'vxUsersBranchHeading'.$branchId; $isFirst = $index === 0; @endphp

@csrf
@if ($assignedUsers->isEmpty())
Sin usuarios asignados.
@else
@foreach ($assignedUsers as $assigned)
{{ (string) ($assigned->nombre ?? '') }} {{ (string) ($assigned->email ?? '') }}
@csrf
@endforeach
@endif
@endforeach
@endif

Cajas por sucursal

@if ($sucursalesRows->isEmpty())

Primero crea una sucursal para crear cajas.

@else
@foreach ($sucursalesRows as $index => $sucursal) @php $branchId = (int) ($sucursal->id ?? 0); $branchBoxes = $cashBoxesByBranch->get($branchId, collect()); $collapseId = 'vxCashBranch'.$branchId; $headerId = 'vxCashBranchHeading'.$branchId; $isFirst = $index === 0; @endphp

@csrf
@if ($branchBoxes->isEmpty())
Sin cajas registradas.
@else
@foreach ($branchBoxes as $cashBox) @php $activeBox = (string) ($cashBox->estado ?? '') === 'activa'; @endphp
{{ (string) ($cashBox->nombre ?? '') }} {{ $activeBox ? 'Activa' : 'Inactiva' }}
@csrf
@endforeach
@endif
@endforeach
@endif
@endsection