up
This commit is contained in:
+255
-246
@@ -241,321 +241,330 @@ class ShowServicios extends Component
|
||||
/* ---------------- Btn Comprar ---------------- */
|
||||
public function comprar()
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$maxIntentos = 3; // cantidad de veces que se intentará
|
||||
$intento = 0;
|
||||
|
||||
$this->validate([
|
||||
'cantidad_tarjetaPlan' => 'required|numeric|min:1',
|
||||
]);
|
||||
while ($intento < $maxIntentos) {
|
||||
|
||||
if ($this->cantidad_tarjetaPlan == 0) {
|
||||
$this->alert('error', 'La cantidad debe ser mayor a 0', [
|
||||
'position' => 'top'
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
|
||||
$intento++;
|
||||
$this->validate([
|
||||
'cantidad_tarjetaPlan' => 'required|numeric|min:1',
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (auth()->user()->saldo->valor < $this->valor_tarjetaPlan * $this->cantidad_tarjetaPlan) {
|
||||
$this->alert('error', 'Saldo insuficiente', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
if ($this->cantidad_tarjetaPlan == 0) {
|
||||
$this->alert('error', 'La cantidad debe ser mayor a 0', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan;
|
||||
$total_valor = (int) $this->valor_tarjetaPlan * (int) $this->cantidad_tarjetaPlan;
|
||||
$utilidad_total = (int) $this->utilidad * (int) $this->cantidad_tarjetaPlan;
|
||||
if (auth()->user()->saldo->valor < $this->valor_tarjetaPlan * $this->cantidad_tarjetaPlan) {
|
||||
$this->alert('error', 'Saldo insuficiente', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null;
|
||||
$usuario_id = $usuario_existe;
|
||||
$cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan;
|
||||
$total_valor = (int) $this->valor_tarjetaPlan * (int) $this->cantidad_tarjetaPlan;
|
||||
$utilidad_total = (int) $this->utilidad * (int) $this->cantidad_tarjetaPlan;
|
||||
|
||||
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_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null;
|
||||
$usuario_id = $usuario_existe;
|
||||
|
||||
$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();
|
||||
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();
|
||||
|
||||
$pantallas = Tarifas::where('id', $this->tarifa_id)->with('servicio')->first();
|
||||
$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();
|
||||
|
||||
|
||||
$servicioNombre = $pantallas->servicio->nombre;
|
||||
$servicio_id = $pantallas->servicio_id;
|
||||
$pantalla_servicio_completa = $pantallas->servicio->completa;
|
||||
$pantalla_servicio = $pantallas->servicio->pantallas;
|
||||
$servicioNombre = $pantallas->servicio->nombre;
|
||||
$servicio_id = $pantallas->servicio_id;
|
||||
$pantalla_servicio_completa = $pantallas->servicio->completa;
|
||||
$pantalla_servicio = $pantallas->servicio->pantallas;
|
||||
|
||||
$pantallas = $pantallas->pantallas ?? null;
|
||||
$pantallas = $pantallas->pantallas ?? null;
|
||||
|
||||
|
||||
if ((int) $pantalla_servicio >= (int) $cantidad_solicitada && (int) $pantalla_servicio_completa != (int) $this->pantallas_tarjetaPlan) {
|
||||
//dd('soy este');
|
||||
$suma = $pantalla_servicio - $cantidad_solicitada;
|
||||
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
||||
if ((int) $pantalla_servicio >= (int) $cantidad_solicitada && (int) $pantalla_servicio_completa != (int) $this->pantallas_tarjetaPlan) {
|
||||
//dd('soy este');
|
||||
$suma = $pantalla_servicio - $cantidad_solicitada;
|
||||
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
||||
|
||||
$baseQuery = Cuentas::select('*')
|
||||
->where('servicio_id', $this->servicio_id)
|
||||
->whereDate('inicio', '>=', $this->fecha)
|
||||
->whereDate('vencimiento', '>=', $this->fecha_final_1)
|
||||
->whereDate('vencimiento', '<=', $this->fecha_final_2)
|
||||
->where('estado', 'activo')
|
||||
->withCount('historiales')
|
||||
->orderBy('inicio', 'ASC');
|
||||
$baseQuery = Cuentas::select('*')
|
||||
->where('servicio_id', $this->servicio_id)
|
||||
->whereDate('inicio', '>=', $this->fecha)
|
||||
->whereDate('vencimiento', '>=', $this->fecha_final_1)
|
||||
->whereDate('vencimiento', '<=', $this->fecha_final_2)
|
||||
->where('estado', 'activo')
|
||||
->withCount('historiales')
|
||||
->orderBy('inicio', 'ASC');
|
||||
|
||||
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
|
||||
->where('historiales_count', '<=', $suma)
|
||||
->orderBy('inicio', 'ASC')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
|
||||
->where('historiales_count', '<=', $suma)
|
||||
->orderBy('inicio', 'ASC')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
|
||||
|
||||
|
||||
if ($cuenta->isEmpty()) {
|
||||
$suma3 = $pantalla_servicio - $pantallas;
|
||||
if ($cuenta->isEmpty()) {
|
||||
$suma3 = $pantalla_servicio - $pantallas;
|
||||
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
||||
$baseQuery = Cuentas::select('*')->where('servicio_id', $this->servicio_id)
|
||||
->whereDate('inicio', '>=', $this->fecha)
|
||||
->whereDate('vencimiento', '>=', $this->fecha_final_1)
|
||||
->whereDate('vencimiento', '<=', $this->fecha_final_2)
|
||||
->withCount('historiales')
|
||||
->where('estado', 'activo')
|
||||
->Orderby('inicio', 'ASC');
|
||||
|
||||
|
||||
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
|
||||
->where('historiales_count', '<=', $suma3)
|
||||
->orderBy('inicio', 'ASC')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
}
|
||||
} elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) {
|
||||
|
||||
//Cuentas completas
|
||||
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
||||
$cuenta = Cuentas::where('servicio_id', $this->servicio_id)
|
||||
->whereDate('vencimiento', '>=', $this->fecha_final_1)
|
||||
->whereDate('vencimiento', '<=', $this->fecha_final_2)
|
||||
->Orderby('inicio', 'ASC')
|
||||
->withCount('historiales')
|
||||
->where('estado', 'pendiente')
|
||||
->doesntHave('perfil')
|
||||
->get();
|
||||
} elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) {
|
||||
|
||||
$suma2 = $pantalla_servicio - $pantallas;
|
||||
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
||||
$baseQuery = Cuentas::select('*')->where('servicio_id', $this->servicio_id)
|
||||
->whereDate('inicio', '>=', $this->fecha)
|
||||
->whereDate('vencimiento', '>=', $this->fecha_final_1)
|
||||
->whereDate('vencimiento', '<=', $this->fecha_final_2)
|
||||
->Orderby('inicio', 'ASC')
|
||||
->withCount('historiales')
|
||||
->where('estado', 'activo')
|
||||
->Orderby('inicio', 'ASC');
|
||||
|
||||
->where('estado', 'activo');
|
||||
|
||||
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
|
||||
->where('historiales_count', '<=', $suma3)
|
||||
->where('historiales_count', '<=', $suma2)
|
||||
->orderBy('inicio', 'ASC')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
} else {
|
||||
$cuenta = null;
|
||||
$cantidad_necesaria = null;
|
||||
}
|
||||
} elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) {
|
||||
|
||||
//Cuentas completas
|
||||
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
||||
$cuenta = Cuentas::where('servicio_id', $this->servicio_id)
|
||||
->whereDate('vencimiento', '>=', $this->fecha_final_1)
|
||||
->whereDate('vencimiento', '<=', $this->fecha_final_2)
|
||||
->Orderby('inicio', 'ASC')
|
||||
->withCount('historiales')
|
||||
->where('estado', 'pendiente')
|
||||
->doesntHave('perfil')
|
||||
->get();
|
||||
} elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) {
|
||||
|
||||
$suma2 = $pantalla_servicio - $pantallas;
|
||||
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
||||
$baseQuery = Cuentas::select('*')->where('servicio_id', $this->servicio_id)
|
||||
->whereDate('inicio', '>=', $this->fecha)
|
||||
->whereDate('vencimiento', '>=', $this->fecha_final_1)
|
||||
->whereDate('vencimiento', '<=', $this->fecha_final_2)
|
||||
->Orderby('inicio', 'ASC')
|
||||
->withCount('historiales')
|
||||
->where('estado', 'activo');
|
||||
|
||||
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
|
||||
->where('historiales_count', '<=', $suma2)
|
||||
->orderBy('inicio', 'ASC')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
} else {
|
||||
$cuenta = null;
|
||||
$cantidad_necesaria = null;
|
||||
}
|
||||
|
||||
$cuentasMostar = '';
|
||||
for ($h = 0; $h < (int) $cantidad_necesaria; $h++) {
|
||||
$cuentasMostar = '';
|
||||
for ($h = 0; $h < (int) $cantidad_necesaria; $h++) {
|
||||
|
||||
|
||||
if (!empty($cuenta) && isset($cuenta[$h])) {
|
||||
$esta_error = false;
|
||||
$cuentasMostar = '';
|
||||
if (!empty($cuenta) && isset($cuenta[$h])) {
|
||||
$esta_error = false;
|
||||
$cuentasMostar = '';
|
||||
|
||||
$perfil = $cuenta[$h]->historiales_count;
|
||||
$perfil = $cuenta[$h]->historiales_count;
|
||||
|
||||
for ($y = 0; $y < (int) $pantallas; $y++) {
|
||||
for ($y = 0; $y < (int) $pantallas; $y++) {
|
||||
|
||||
$historial_cuenta = new Historial_cuenta;
|
||||
$historial_cuenta->cuenta_id = $cuenta[$h]->id;
|
||||
$historial_cuenta->historial_id = $historial->id;
|
||||
$historial_cuenta->perfil = (int) $perfil + (int) $y + 1;
|
||||
$historial_cuenta->save();
|
||||
$historial_cuenta = new Historial_cuenta;
|
||||
$historial_cuenta->cuenta_id = $cuenta[$h]->id;
|
||||
$historial_cuenta->historial_id = $historial->id;
|
||||
$historial_cuenta->perfil = (int) $perfil + (int) $y + 1;
|
||||
$historial_cuenta->save();
|
||||
|
||||
if ($cuenta[$h]->correo != $cuentasMostar) {
|
||||
$cuentasMostar .= $cuenta[$h]->correo . ' ';
|
||||
if ($cuenta[$h]->correo != $cuentasMostar) {
|
||||
$cuentasMostar .= $cuenta[$h]->correo . ' ';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$esta_error = true;
|
||||
$historial->update(['estado' => 'cancelado']);
|
||||
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||
$logsGeneral = new Log_general();
|
||||
$logsGeneral->user_id = Auth::user()->id;
|
||||
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo error de "No hay cuentas disponibles" Comprando el servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor . ' Se desligaron todas las cuentas.';
|
||||
$logsGeneral->tipo = 'compra_error';
|
||||
$logsGeneral->historial_id = $historial->id;
|
||||
$logsGeneral->save();
|
||||
$this->alert('warning', 'No hay cuentas disponibles!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$esta_error && !empty($historial->cuentas)) {
|
||||
$descuento = Saldo::where('usuario_id', Auth::user()->id)->first();
|
||||
|
||||
$saldo_anterior = $descuento->valor ?? 0;
|
||||
$nuevo_saldo = $saldo_anterior - $total_valor;
|
||||
$cobro = $descuento->update(['valor' => $nuevo_saldo]);
|
||||
if ($cobro) {
|
||||
|
||||
$historial->update(['estado' => 'activo']);
|
||||
|
||||
// Si el campo subvendedor tiene id entonces la utilidad va a su subvendedor padre
|
||||
if (Auth::user()->subvendedor_id) {
|
||||
if ($utilidad_total > 0) {
|
||||
$subvendedorPadre = User::with('saldo')->find(Auth::user()->subvendedor_id);
|
||||
|
||||
if ($subvendedorPadre && $subvendedorPadre->saldo) {
|
||||
// Sumar la utilidad al saldo actual
|
||||
$nuevoSaldo = $subvendedorPadre->saldo->valor + $utilidad_total;
|
||||
|
||||
// Actualizar el saldo
|
||||
$subvendedorPadre->saldo->update([
|
||||
'valor' => $nuevoSaldo
|
||||
]);
|
||||
|
||||
// Registrar en el log la utilidad generada
|
||||
$logsGeneral = new Log_general();
|
||||
$logsGeneral->user_id = Auth::user()->id;
|
||||
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email .
|
||||
' ha comprado el servicio ' . $servicioNombre . ' - ' . $cuentasMostar .
|
||||
' cantidad de ' . $this->cantidad_tarjetaPlan .
|
||||
' por un valor de $' . $total_valor .
|
||||
', Saldo anterior: $' . $saldo_anterior .
|
||||
', Nuevo saldo disponible: $' . $nuevo_saldo .
|
||||
'. Esta compra generó una utilidad de $' . $utilidad_total .
|
||||
' al usuario padre ' . $subvendedorPadre->name . ' - ' . $subvendedorPadre->email . '.';
|
||||
$logsGeneral->historial_id = $historial->id;
|
||||
$logsGeneral->tipo = 'compra';
|
||||
$logsGeneral->save();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Log sin utilidad al padre
|
||||
$logsGeneral = new Log_general();
|
||||
$logsGeneral->user_id = Auth::user()->id;
|
||||
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email .
|
||||
' ha comprado el servicio ' . $servicioNombre . ' - ' . $cuentasMostar .
|
||||
' cantidad de ' . $this->cantidad_tarjetaPlan .
|
||||
' por un valor de $' . $total_valor .
|
||||
', Saldo anterior: $' . $saldo_anterior .
|
||||
', Nuevo saldo disponible: $' . $nuevo_saldo . '.';
|
||||
$logsGeneral->historial_id = $historial->id;
|
||||
$logsGeneral->tipo = 'compra';
|
||||
$logsGeneral->save();
|
||||
}
|
||||
|
||||
$this->credenciales($historial);
|
||||
} else {
|
||||
$historial->update(['estado' => 'cancelado']);
|
||||
$logsGeneral = new Log_general();
|
||||
$logsGeneral->user_id = Auth::user()->id;
|
||||
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo un error con el cobro del servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor;
|
||||
$logsGeneral->tipo = 'compra_error';
|
||||
$logsGeneral->historial_id = $historial->id;
|
||||
$logsGeneral->save();
|
||||
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||
$this->alert('warning', 'Error en el cobro, comuniquese con soporte!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
$esta_error = true;
|
||||
$historial->update(['estado' => 'cancelado']);
|
||||
|
||||
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||
$logsGeneral = new Log_general();
|
||||
$logsGeneral->user_id = Auth::user()->id;
|
||||
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo error de "No hay cuentas disponibles" Comprando el servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor . ' Se desligaron todas las cuentas.';
|
||||
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo error de "No hay cuentas disponibles" Comprando el servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor . ' No se desconto el saldo, ni se asignaron cuentas.';
|
||||
$logsGeneral->tipo = 'compra_error';
|
||||
$logsGeneral->historial_id = $historial->id;
|
||||
$logsGeneral->save();
|
||||
$this->alert('warning', 'No hay cuentas disponibles!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$esta_error && !empty($historial->cuentas)) {
|
||||
$descuento = Saldo::where('usuario_id', Auth::user()->id)->first();
|
||||
|
||||
$saldo_anterior = $descuento->valor ?? 0;
|
||||
$nuevo_saldo = $saldo_anterior - $total_valor;
|
||||
$cobro = $descuento->update(['valor' => $nuevo_saldo]);
|
||||
if ($cobro) {
|
||||
|
||||
$historial->update(['estado' => 'activo']);
|
||||
// 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();
|
||||
|
||||
// Si el campo subvendedor tiene id entonces la utilidad va a su subvendedor padre
|
||||
if (Auth::user()->subvendedor_id) {
|
||||
if ($utilidad_total > 0) {
|
||||
$subvendedorPadre = User::with('saldo')->find(Auth::user()->subvendedor_id);
|
||||
if ($perfilesDuplicados->isNotEmpty()) {
|
||||
// Eliminar historial_cuenta
|
||||
Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||
|
||||
if ($subvendedorPadre && $subvendedorPadre->saldo) {
|
||||
// Sumar la utilidad al saldo actual
|
||||
$nuevoSaldo = $subvendedorPadre->saldo->valor + $utilidad_total;
|
||||
|
||||
// Actualizar el saldo
|
||||
$subvendedorPadre->saldo->update([
|
||||
'valor' => $nuevoSaldo
|
||||
]);
|
||||
|
||||
// Registrar en el log la utilidad generada
|
||||
$logsGeneral = new Log_general();
|
||||
$logsGeneral->user_id = Auth::user()->id;
|
||||
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email .
|
||||
' ha comprado el servicio ' . $servicioNombre . ' - ' . $cuentasMostar .
|
||||
' cantidad de ' . $this->cantidad_tarjetaPlan .
|
||||
' por un valor de $' . $total_valor .
|
||||
', Saldo anterior: $' . $saldo_anterior .
|
||||
', Nuevo saldo disponible: $' . $nuevo_saldo .
|
||||
'. Esta compra generó una utilidad de $' . $utilidad_total .
|
||||
' al usuario padre ' . $subvendedorPadre->name . ' - ' . $subvendedorPadre->email . '.';
|
||||
$logsGeneral->historial_id = $historial->id;
|
||||
$logsGeneral->tipo = 'compra';
|
||||
$logsGeneral->save();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Log sin utilidad al padre
|
||||
$logsGeneral = new Log_general();
|
||||
$logsGeneral->user_id = Auth::user()->id;
|
||||
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email .
|
||||
' ha comprado el servicio ' . $servicioNombre . ' - ' . $cuentasMostar .
|
||||
' cantidad de ' . $this->cantidad_tarjetaPlan .
|
||||
' por un valor de $' . $total_valor .
|
||||
', Saldo anterior: $' . $saldo_anterior .
|
||||
', Nuevo saldo disponible: $' . $nuevo_saldo . '.';
|
||||
$logsGeneral->historial_id = $historial->id;
|
||||
$logsGeneral->tipo = 'compra';
|
||||
$logsGeneral->save();
|
||||
}
|
||||
|
||||
$this->credenciales($historial);
|
||||
} else {
|
||||
// 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 = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo un error con el cobro del servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor;
|
||||
$logsGeneral->detalle = $detalleError;
|
||||
$logsGeneral->tipo = 'compra_error';
|
||||
$logsGeneral->historial_id = $historial->id;
|
||||
$logsGeneral->save();
|
||||
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||
$this->alert('warning', 'Error en el cobro, comuniquese con soporte!', [
|
||||
|
||||
$this->alert('warning', 'Error: Se detectaron perfiles duplicados. Se canceló la compra.', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
|
||||
// Salir antes de confirmar la transacción
|
||||
DB::rollBack();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
|
||||
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||
$logsGeneral = new Log_general();
|
||||
$logsGeneral->user_id = Auth::user()->id;
|
||||
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo error de "No hay cuentas disponibles" Comprando el servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor . ' No se desconto el saldo, ni se asignaron cuentas.';
|
||||
$logsGeneral->tipo = 'compra_error';
|
||||
$logsGeneral->historial_id = $historial->id;
|
||||
$logsGeneral->save();
|
||||
$this->alert('warning', 'No hay cuentas disponibles!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// 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();
|
||||
DB::commit();
|
||||
|
||||
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
|
||||
$this->tarjetaPlan = false;
|
||||
$this->cantidad_tarjetaPlan = 1;
|
||||
} catch (Exception $e) {
|
||||
// Hacer rollback de cualquier cambio realizado en la base de datos
|
||||
DB::rollBack();
|
||||
return;
|
||||
|
||||
if ($intento >= $maxIntentos) {
|
||||
$this->credenciales = false;
|
||||
$this->cuentas = [];
|
||||
|
||||
$this->alert('warning', 'Error al comprar: Esta cuenta ha sido tomada por otro usuario al mismo tiempo, intenta nuevamente. Error: ' . $e->getMessage(), [
|
||||
'position' => 'top'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
usleep(200000); // 200ms
|
||||
}
|
||||
|
||||
|
||||
DB::commit();
|
||||
|
||||
$this->tarjetaPlan = false;
|
||||
$this->cantidad_tarjetaPlan = 1;
|
||||
} catch (Exception $e) {
|
||||
// Hacer rollback de cualquier cambio realizado en la base de datos
|
||||
DB::rollBack();
|
||||
|
||||
// Establecer variables a su estado inicial o a un valor que indique error
|
||||
$this->credenciales = false;
|
||||
$this->cuentas = [];
|
||||
|
||||
// Mostrar un mensaje de alerta al usuario
|
||||
$this->alert('warning', 'Error al comprar: Esta cuenta ha sido tomada por otro usuario al mismo tiempo, intenta nuevamente. Error: ' . $e->getMessage(), [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user