Revert "Update ShowServicios.php"

This reverts commit 2c68201f3b.
This commit is contained in:
lizandrogd
2025-07-07 17:29:23 -05:00
parent 2c68201f3b
commit a2239647ce
+74 -96
View File
@@ -110,7 +110,7 @@ class ShowServicios extends Component
->orderBy('ubicacion') ->orderBy('ubicacion')
->get(); ->get();
//dd($consulta_servicios); //dd($consulta_servicios);
return view('livewire.show-servicios', [ return view('livewire.show-servicios', [
@@ -174,25 +174,24 @@ class ShowServicios extends Component
}); });
}) })
->count(); ->count();
} else { } else {
$cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan; $cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan;
$cantidad_restante = $servicio_tarifa->pantallas - $cantidad_solicitada; $cantidad_restante = $servicio_tarifa->pantallas - $cantidad_solicitada;
//Traer todas las cuentas por pantalla 'activo' de los ultimos 15 dias. solo disponibles //Traer todas las cuentas por pantalla 'activo' de los ultimos 15 dias. solo disponibles
$cuentas_pantallas = Cuentas::whereDate('inicio', '>=', $this->fecha) $baseQuery = Cuentas::select('*')->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2) ->whereDate('vencimiento', '<=', $this->fecha_final_2)
->where('servicio_id', $servicio_tarifa->id) ->where('servicio_id', $servicio_tarifa->id)
->where('estado', 'activo') ->where('estado', 'activo')
->whereRaw('( ->withCount('historiales')
SELECT COUNT(*) ->where('estado', 'activo');
FROM historiales_cuenta
WHERE historiales_cuenta.cuenta_id = cuentas.id
) <= ?', [$cantidad_restante])
->orderBy('inicio', 'ASC')
->lockForUpdate()
->get();
$cuentas_pantallas = Cuentas::fromSub($baseQuery, 'alias')
->where('historiales_count', '<=', $cantidad_restante)
->orderBy('inicio', 'ASC')
->get();
@@ -211,6 +210,7 @@ class ShowServicios extends Component
} else { } else {
$this->disponibles = $cuentas_pantallas; $this->disponibles = $cuentas_pantallas;
} }
} }
//dd($this->tarjetaPlan); //dd($this->tarjetaPlan);
@@ -222,6 +222,7 @@ class ShowServicios extends Component
$this->cantidad_tarjetaPlan = 1; $this->cantidad_tarjetaPlan = 1;
$this->tarjetaPlan = false; $this->tarjetaPlan = false;
$this->tarifa_id = ''; $this->tarifa_id = '';
} }
public function notificacionPocas($servicio, $restantes) public function notificacionPocas($servicio, $restantes)
@@ -269,30 +270,30 @@ class ShowServicios extends Component
$usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null; $usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null;
$usuario_id = $usuario_existe; $usuario_id = $usuario_existe;
if (is_null($usuario_existe)) { if (is_null($usuario_existe)) {
$usuario = new User; $usuario = new User;
$usuario->name = $this->nombre_tarjetaPlan; $usuario->name = $this->nombre_tarjetaPlan;
$usuario->email = $this->email_tarjetaPlan; $usuario->email = $this->email_tarjetaPlan;
$usuario->password = '123'; $usuario->password = '123';
$usuario->celular = '+57' . $this->celular_tarjetaPlan; $usuario->celular = '+57' . $this->celular_tarjetaPlan;
$usuario->save(); $usuario->save();
$usuario_id = $usuario->id; $usuario_id = $usuario->id;
} }
//dd($this->fechaFinal); //dd($this->fechaFinal);
$historial = new Historiale; $historial = new Historiale;
$historial->fecha_inicio = Carbon::now(); $historial->fecha_inicio = Carbon::now();
$historial->fecha_final = $this->fechaFinal; $historial->fecha_final = $this->fechaFinal;
$historial->valor = $total_valor; $historial->valor = $total_valor;
$historial->tipo_pago = 'manual'; $historial->tipo_pago = 'manual';
$historial->estado = 'pendiente'; $historial->estado = 'pendiente';
$historial->vendedor_id = Auth()->user()->id; $historial->vendedor_id = Auth()->user()->id;
$historial->tarifa_id = $this->tarifa_id; $historial->tarifa_id = $this->tarifa_id;
$historial->cliente_id = $usuario_id; $historial->cliente_id = $usuario_id;
$historial->cantidad = $this->cantidad_tarjetaPlan; $historial->cantidad = $this->cantidad_tarjetaPlan;
$historial->nombre_cliente = $this->nombre_tarjetaPlan; $historial->nombre_cliente = $this->nombre_tarjetaPlan;
$historial->utilidad = $utilidad_total; $historial->utilidad = $utilidad_total;
$historial->save(); $historial->save();
$pantallas = Tarifas::where('id', $this->tarifa_id)->with('servicio')->first(); $pantallas = Tarifas::where('id', $this->tarifa_id)->with('servicio')->first();
@@ -310,41 +311,40 @@ class ShowServicios extends Component
$suma = $pantalla_servicio - $cantidad_solicitada; $suma = $pantalla_servicio - $cantidad_solicitada;
$cantidad_necesaria = $this->cantidad_tarjetaPlan; $cantidad_necesaria = $this->cantidad_tarjetaPlan;
$cuenta = Cuentas::where('servicio_id', $this->servicio_id) $baseQuery = Cuentas::select('*')
->where('servicio_id', $this->servicio_id)
->whereDate('inicio', '>=', $this->fecha) ->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2) ->whereDate('vencimiento', '<=', $this->fecha_final_2)
->where('estado', 'activo') ->where('estado', 'activo')
->whereRaw('( ->withCount('historiales')
SELECT COUNT(*) ->orderBy('inicio', 'ASC');
FROM historiales_cuenta
WHERE historiales_cuenta.cuenta_id = cuentas.id
) <= ?', [$suma])
->orderBy('inicio', 'ASC')
->lockForUpdate()
->get();
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
->where('historiales_count', '<=', $suma)
->orderBy('inicio', 'ASC')
->get();
if ($cuenta->isEmpty()) { if ($cuenta->isEmpty()) {
$suma3 = $pantalla_servicio - $pantallas; $suma3 = $pantalla_servicio - $pantallas;
$cantidad_necesaria = $this->cantidad_tarjetaPlan; $cantidad_necesaria = $this->cantidad_tarjetaPlan;
$baseQuery = Cuentas::select('*')->where('servicio_id', $this->servicio_id)
$cuenta = Cuentas::where('servicio_id', $this->servicio_id)
->whereDate('inicio', '>=', $this->fecha) ->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2) ->whereDate('vencimiento', '<=', $this->fecha_final_2)
->withCount('historiales')
->where('estado', 'activo') ->where('estado', 'activo')
->whereRaw('( ->Orderby('inicio', 'ASC');
SELECT COUNT(*)
FROM historiales_cuenta
WHERE historiales_cuenta.cuenta_id = cuentas.id $cuenta = Cuentas::fromSub($baseQuery, 'alias')
) <= ?', [$suma3]) ->where('historiales_count', '<=', $suma3)
->orderBy('inicio', 'ASC') ->orderBy('inicio', 'ASC')
->lockForUpdate()
->get(); ->get();
} }
} elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) { } elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) {
//Cuentas completas //Cuentas completas
@@ -357,25 +357,26 @@ class ShowServicios extends Component
->where('estado', 'pendiente') ->where('estado', 'pendiente')
->doesntHave('perfil') ->doesntHave('perfil')
->get(); ->get();
} elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) { } elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) {
$suma2 = $pantalla_servicio - $pantallas; $suma2 = $pantalla_servicio - $pantallas;
$cantidad_necesaria = $this->cantidad_tarjetaPlan; $cantidad_necesaria = $this->cantidad_tarjetaPlan;
$baseQuery = Cuentas::select('*')->where('servicio_id', $this->servicio_id)
$cuenta = Cuentas::where('servicio_id', $this->servicio_id)
->whereDate('inicio', '>=', $this->fecha) ->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2) ->whereDate('vencimiento', '<=', $this->fecha_final_2)
->where('estado', 'activo') ->Orderby('inicio', 'ASC')
->whereRaw('( ->withCount('historiales')
SELECT COUNT(*) ->where('estado', 'activo');
FROM historiales_cuenta
WHERE historiales_cuenta.cuenta_id = cuentas.id $cuenta = Cuentas::fromSub($baseQuery, 'alias')
) <= ?', [$suma2]) ->where('historiales_count', '<=', $suma2)
->orderBy('inicio', 'ASC') ->orderBy('inicio', 'ASC')
->lockForUpdate()
->get(); ->get();
} else { } else {
$cuenta = null; $cuenta = null;
$cantidad_necesaria = null; $cantidad_necesaria = null;
@@ -388,44 +389,20 @@ class ShowServicios extends Component
if (!empty($cuenta) && isset($cuenta[$h])) { if (!empty($cuenta) && isset($cuenta[$h])) {
$esta_error = false; $esta_error = false;
$cuentasMostar = ''; $cuentasMostar = '';
// 🔐 Bloqueo de cuenta para evitar perfiles duplicados
$cuentaBloqueada = Cuentas::where('id', $cuenta[$h]->id)
->lockForUpdate()
->first();
// 🧠 Obtener perfiles ya asignados $perfil = $cuenta[$h]->historiales_count;
$perfilesUsados = Historial_cuenta::where('cuenta_id', $cuentaBloqueada->id)
->pluck('perfil')
->toArray();
for ($y = 0; $y < (int) $pantallas; $y++) {
$perfilAsignado = 1; $historial_cuenta = new Historial_cuenta;
$perfilesAsignados = 0; $historial_cuenta->cuenta_id = $cuenta[$h]->id;
$historial_cuenta->historial_id = $historial->id;
$historial_cuenta->perfil = (int) $perfil + (int) $y + 1;
$historial_cuenta->save();
while ($perfilesAsignados < (int) $pantallas) { if ($cuenta[$h]->correo != $cuentasMostar) {
if (!in_array($perfilAsignado, $perfilesUsados)) { $cuentasMostar .= $cuenta[$h]->correo . ' ';
$historial_cuenta = new Historial_cuenta;
$historial_cuenta->cuenta_id = $cuentaBloqueada->id;
$historial_cuenta->historial_id = $historial->id;
$historial_cuenta->perfil = $perfilAsignado;
$historial_cuenta->save();
$perfilesUsados[] = $perfilAsignado;
$perfilesAsignados++;
if ($cuentaBloqueada->correo != $cuentasMostar) {
$cuentasMostar .= $cuentaBloqueada->correo . ' ';
}
} }
$perfilAsignado++;
// Evita bucles infinitos por error
if ($perfilAsignado > $pantallas * 2) { // margen de seguridad mayor
$esta_error = true;
break;
}
} }
} else { } else {
$esta_error = true; $esta_error = true;
@@ -476,6 +453,7 @@ class ShowServicios extends Component
'position' => 'top' 'position' => 'top'
]); ]);
} }
} else { } else {
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete(); $eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();