@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())
{{ $branchCountValue }}
{{ $activeBranchesValue }}
{{ $totalUsersAssigned }}
{{ $totalCashBoxesValue }}
Primero crea una sucursal para asignar usuarios.
@elsePrimero crea una sucursal para crear cajas.
@else