Update ShowServicios.php

This commit is contained in:
lizandrogd
2025-07-07 17:36:45 -05:00
parent a2239647ce
commit 3e7f9542c7
+77 -46
View File
@@ -110,7 +110,7 @@ class ShowServicios extends Component
->orderBy('ubicacion')
->get();
//dd($consulta_servicios);
//dd($consulta_servicios);
return view('livewire.show-servicios', [
@@ -174,27 +174,26 @@ class ShowServicios extends Component
});
})
->count();
} else {
$cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan;
$cantidad_restante = $servicio_tarifa->pantallas - $cantidad_solicitada;
//Traer todas las cuentas por pantalla 'activo' de los ultimos 15 dias. solo disponibles
$baseQuery = Cuentas::select('*')->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2)
->where('servicio_id', $servicio_tarifa->id)
->where('estado', 'activo')
->withCount('historiales')
->where('estado', 'activo');
$baseQuery = Cuentas::select('*')->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2)
->where('servicio_id', $servicio_tarifa->id)
->where('estado', 'activo')
->withCount('historiales')
->where('estado', 'activo');
$cuentas_pantallas = Cuentas::fromSub($baseQuery, 'alias')
->where('historiales_count', '<=', $cantidad_restante)
->orderBy('inicio', 'ASC')
->get();
$cuentas_pantallas = Cuentas::fromSub($baseQuery, 'alias')
->where('historiales_count', '<=', $cantidad_restante)
->orderBy('inicio', 'ASC')
->get();
//dd($cuentas_pantallas); // con count da un numero entero
$cuentas_pantallas_tomadas = Historial_cuenta::whereIn('cuenta_id', $cuentas_pantallas
@@ -210,7 +209,6 @@ class ShowServicios extends Component
} else {
$this->disponibles = $cuentas_pantallas;
}
}
//dd($this->tarjetaPlan);
@@ -222,7 +220,6 @@ class ShowServicios extends Component
$this->cantidad_tarjetaPlan = 1;
$this->tarjetaPlan = false;
$this->tarifa_id = '';
}
public function notificacionPocas($servicio, $restantes)
@@ -270,30 +267,30 @@ class ShowServicios extends Component
$usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null;
$usuario_id = $usuario_existe;
if (is_null($usuario_existe)) {
$usuario = new User;
$usuario->name = $this->nombre_tarjetaPlan;
$usuario->email = $this->email_tarjetaPlan;
$usuario->password = '123';
$usuario->celular = '+57' . $this->celular_tarjetaPlan;
$usuario->save();
if (is_null($usuario_existe)) {
$usuario = new User;
$usuario->name = $this->nombre_tarjetaPlan;
$usuario->email = $this->email_tarjetaPlan;
$usuario->password = '123';
$usuario->celular = '+57' . $this->celular_tarjetaPlan;
$usuario->save();
$usuario_id = $usuario->id;
}
//dd($this->fechaFinal);
$historial = new Historiale;
$historial->fecha_inicio = Carbon::now();
$historial->fecha_final = $this->fechaFinal;
$historial->valor = $total_valor;
$historial->tipo_pago = 'manual';
$historial->estado = 'pendiente';
$historial->vendedor_id = Auth()->user()->id;
$historial->tarifa_id = $this->tarifa_id;
$historial->cliente_id = $usuario_id;
$historial->cantidad = $this->cantidad_tarjetaPlan;
$historial->nombre_cliente = $this->nombre_tarjetaPlan;
$historial->utilidad = $utilidad_total;
$historial->save();
$usuario_id = $usuario->id;
}
//dd($this->fechaFinal);
$historial = new Historiale;
$historial->fecha_inicio = Carbon::now();
$historial->fecha_final = $this->fechaFinal;
$historial->valor = $total_valor;
$historial->tipo_pago = 'manual';
$historial->estado = 'pendiente';
$historial->vendedor_id = Auth()->user()->id;
$historial->tarifa_id = $this->tarifa_id;
$historial->cliente_id = $usuario_id;
$historial->cantidad = $this->cantidad_tarjetaPlan;
$historial->nombre_cliente = $this->nombre_tarjetaPlan;
$historial->utilidad = $utilidad_total;
$historial->save();
$pantallas = Tarifas::where('id', $this->tarifa_id)->with('servicio')->first();
@@ -325,7 +322,7 @@ class ShowServicios extends Component
->orderBy('inicio', 'ASC')
->get();
if ($cuenta->isEmpty()) {
$suma3 = $pantalla_servicio - $pantallas;
@@ -344,7 +341,6 @@ class ShowServicios extends Component
->orderBy('inicio', 'ASC')
->get();
}
} elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) {
//Cuentas completas
@@ -357,8 +353,6 @@ class ShowServicios extends Component
->where('estado', 'pendiente')
->doesntHave('perfil')
->get();
} elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) {
$suma2 = $pantalla_servicio - $pantallas;
@@ -375,8 +369,6 @@ class ShowServicios extends Component
->where('historiales_count', '<=', $suma2)
->orderBy('inicio', 'ASC')
->get();
} else {
$cuenta = null;
$cantidad_necesaria = null;
@@ -453,7 +445,6 @@ class ShowServicios extends Component
'position' => 'top'
]);
}
} else {
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
@@ -468,6 +459,46 @@ class ShowServicios extends Component
]);
}
// Verificar si hay perfiles duplicados en historial_cuenta recién creados
$perfilesDuplicados = Historial_cuenta::select('cuenta_id', 'perfil')
->where('historial_id', $historial->id)
->groupBy('cuenta_id', 'perfil')
->havingRaw('COUNT(*) > 1')
->get();
if ($perfilesDuplicados->isNotEmpty()) {
// Eliminar historial_cuenta
Historial_cuenta::where('historial_id', $historial->id)->delete();
// Cancelar historial
$historial->update(['estado' => 'cancelado']);
// Registrar en log_general
$detalleError = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email
. ' tuvo un error por perfiles duplicados en la compra del servicio '
. $servicioNombre . ' - ' . $cuentasMostar
. ', cantidad de ' . $this->cantidad_tarjetaPlan
. ', valor: $' . $total_valor
. '. Se canceló la operación y se eliminaron los perfiles duplicados.';
$logsGeneral = new Log_general();
$logsGeneral->user_id = Auth::user()->id;
$logsGeneral->detalle = $detalleError;
$logsGeneral->tipo = 'compra_error';
$logsGeneral->historial_id = $historial->id;
$logsGeneral->save();
$this->alert('warning', 'Error: Se detectaron perfiles duplicados. Se canceló la compra.', [
'position' => 'top'
]);
// Salir antes de confirmar la transacción
DB::rollBack();
return;
}
DB::commit();
$this->tarjetaPlan = false;