up
This commit is contained in:
+255
-246
@@ -241,321 +241,330 @@ class ShowServicios extends Component
|
|||||||
/* ---------------- Btn Comprar ---------------- */
|
/* ---------------- Btn Comprar ---------------- */
|
||||||
public function comprar()
|
public function comprar()
|
||||||
{
|
{
|
||||||
DB::beginTransaction();
|
$maxIntentos = 3; // cantidad de veces que se intentará
|
||||||
try {
|
$intento = 0;
|
||||||
|
|
||||||
$this->validate([
|
while ($intento < $maxIntentos) {
|
||||||
'cantidad_tarjetaPlan' => 'required|numeric|min:1',
|
|
||||||
]);
|
|
||||||
|
|
||||||
if ($this->cantidad_tarjetaPlan == 0) {
|
DB::beginTransaction();
|
||||||
$this->alert('error', 'La cantidad debe ser mayor a 0', [
|
try {
|
||||||
'position' => 'top'
|
|
||||||
|
$intento++;
|
||||||
|
$this->validate([
|
||||||
|
'cantidad_tarjetaPlan' => 'required|numeric|min:1',
|
||||||
]);
|
]);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auth()->user()->saldo->valor < $this->valor_tarjetaPlan * $this->cantidad_tarjetaPlan) {
|
if ($this->cantidad_tarjetaPlan == 0) {
|
||||||
$this->alert('error', 'Saldo insuficiente', [
|
$this->alert('error', 'La cantidad debe ser mayor a 0', [
|
||||||
'position' => 'top'
|
'position' => 'top'
|
||||||
]);
|
]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan;
|
if (auth()->user()->saldo->valor < $this->valor_tarjetaPlan * $this->cantidad_tarjetaPlan) {
|
||||||
$total_valor = (int) $this->valor_tarjetaPlan * (int) $this->cantidad_tarjetaPlan;
|
$this->alert('error', 'Saldo insuficiente', [
|
||||||
$utilidad_total = (int) $this->utilidad * (int) $this->cantidad_tarjetaPlan;
|
'position' => 'top'
|
||||||
|
]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null;
|
$cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan;
|
||||||
$usuario_id = $usuario_existe;
|
$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_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null;
|
||||||
$usuario = new User;
|
$usuario_id = $usuario_existe;
|
||||||
$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;
|
if (is_null($usuario_existe)) {
|
||||||
}
|
$usuario = new User;
|
||||||
//dd($this->fechaFinal);
|
$usuario->name = $this->nombre_tarjetaPlan;
|
||||||
$historial = new Historiale;
|
$usuario->email = $this->email_tarjetaPlan;
|
||||||
$historial->fecha_inicio = Carbon::now();
|
$usuario->password = '123';
|
||||||
$historial->fecha_final = $this->fechaFinal;
|
$usuario->celular = '+57' . $this->celular_tarjetaPlan;
|
||||||
$historial->valor = $total_valor;
|
$usuario->save();
|
||||||
$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();
|
$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;
|
$servicioNombre = $pantallas->servicio->nombre;
|
||||||
$servicio_id = $pantallas->servicio_id;
|
$servicio_id = $pantallas->servicio_id;
|
||||||
$pantalla_servicio_completa = $pantallas->servicio->completa;
|
$pantalla_servicio_completa = $pantallas->servicio->completa;
|
||||||
$pantalla_servicio = $pantallas->servicio->pantallas;
|
$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) {
|
if ((int) $pantalla_servicio >= (int) $cantidad_solicitada && (int) $pantalla_servicio_completa != (int) $this->pantallas_tarjetaPlan) {
|
||||||
//dd('soy este');
|
//dd('soy este');
|
||||||
$suma = $pantalla_servicio - $cantidad_solicitada;
|
$suma = $pantalla_servicio - $cantidad_solicitada;
|
||||||
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
||||||
|
|
||||||
$baseQuery = Cuentas::select('*')
|
$baseQuery = Cuentas::select('*')
|
||||||
->where('servicio_id', $this->servicio_id)
|
->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')
|
||||||
->withCount('historiales')
|
->withCount('historiales')
|
||||||
->orderBy('inicio', 'ASC');
|
->orderBy('inicio', 'ASC');
|
||||||
|
|
||||||
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
|
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
|
||||||
->where('historiales_count', '<=', $suma)
|
->where('historiales_count', '<=', $suma)
|
||||||
->orderBy('inicio', 'ASC')
|
->orderBy('inicio', 'ASC')
|
||||||
->lockForUpdate()
|
->lockForUpdate()
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ($cuenta->isEmpty()) {
|
if ($cuenta->isEmpty()) {
|
||||||
$suma3 = $pantalla_servicio - $pantallas;
|
$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;
|
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
||||||
$baseQuery = Cuentas::select('*')->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)
|
||||||
|
->Orderby('inicio', 'ASC')
|
||||||
->withCount('historiales')
|
->withCount('historiales')
|
||||||
->where('estado', 'activo')
|
->where('estado', 'activo');
|
||||||
->Orderby('inicio', 'ASC');
|
|
||||||
|
|
||||||
|
|
||||||
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
|
$cuenta = Cuentas::fromSub($baseQuery, 'alias')
|
||||||
->where('historiales_count', '<=', $suma3)
|
->where('historiales_count', '<=', $suma2)
|
||||||
->orderBy('inicio', 'ASC')
|
->orderBy('inicio', 'ASC')
|
||||||
->lockForUpdate()
|
->lockForUpdate()
|
||||||
->get();
|
->get();
|
||||||
|
} else {
|
||||||
|
$cuenta = null;
|
||||||
|
$cantidad_necesaria = null;
|
||||||
}
|
}
|
||||||
} elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) {
|
|
||||||
|
|
||||||
//Cuentas completas
|
$cuentasMostar = '';
|
||||||
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
|
for ($h = 0; $h < (int) $cantidad_necesaria; $h++) {
|
||||||
$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++) {
|
|
||||||
|
|
||||||
|
|
||||||
if (!empty($cuenta) && isset($cuenta[$h])) {
|
if (!empty($cuenta) && isset($cuenta[$h])) {
|
||||||
$esta_error = false;
|
$esta_error = false;
|
||||||
$cuentasMostar = '';
|
$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 = new Historial_cuenta;
|
||||||
$historial_cuenta->cuenta_id = $cuenta[$h]->id;
|
$historial_cuenta->cuenta_id = $cuenta[$h]->id;
|
||||||
$historial_cuenta->historial_id = $historial->id;
|
$historial_cuenta->historial_id = $historial->id;
|
||||||
$historial_cuenta->perfil = (int) $perfil + (int) $y + 1;
|
$historial_cuenta->perfil = (int) $perfil + (int) $y + 1;
|
||||||
$historial_cuenta->save();
|
$historial_cuenta->save();
|
||||||
|
|
||||||
if ($cuenta[$h]->correo != $cuentasMostar) {
|
if ($cuenta[$h]->correo != $cuentasMostar) {
|
||||||
$cuentasMostar .= $cuenta[$h]->correo . ' ';
|
$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 {
|
} else {
|
||||||
$esta_error = true;
|
|
||||||
$historial->update(['estado' => 'cancelado']);
|
|
||||||
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
|
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||||
$logsGeneral = new Log_general();
|
$logsGeneral = new Log_general();
|
||||||
$logsGeneral->user_id = Auth::user()->id;
|
$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->tipo = 'compra_error';
|
||||||
$logsGeneral->historial_id = $historial->id;
|
$logsGeneral->historial_id = $historial->id;
|
||||||
$logsGeneral->save();
|
$logsGeneral->save();
|
||||||
$this->alert('warning', 'No hay cuentas disponibles!', [
|
$this->alert('warning', 'No hay cuentas disponibles!', [
|
||||||
'position' => 'top'
|
'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 ($perfilesDuplicados->isNotEmpty()) {
|
||||||
if (Auth::user()->subvendedor_id) {
|
// Eliminar historial_cuenta
|
||||||
if ($utilidad_total > 0) {
|
Historial_cuenta::where('historial_id', $historial->id)->delete();
|
||||||
$subvendedorPadre = User::with('saldo')->find(Auth::user()->subvendedor_id);
|
|
||||||
|
|
||||||
if ($subvendedorPadre && $subvendedorPadre->saldo) {
|
// Cancelar historial
|
||||||
// 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']);
|
$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 = new Log_general();
|
||||||
$logsGeneral->user_id = Auth::user()->id;
|
$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->tipo = 'compra_error';
|
||||||
$logsGeneral->historial_id = $historial->id;
|
$logsGeneral->historial_id = $historial->id;
|
||||||
$logsGeneral->save();
|
$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'
|
'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
|
DB::commit();
|
||||||
$perfilesDuplicados = Historial_cuenta::select('cuenta_id', 'perfil')
|
|
||||||
->where('historial_id', $historial->id)
|
|
||||||
->groupBy('cuenta_id', 'perfil')
|
|
||||||
->havingRaw('COUNT(*) > 1')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
if ($perfilesDuplicados->isNotEmpty()) {
|
$this->tarjetaPlan = false;
|
||||||
// Eliminar historial_cuenta
|
$this->cantidad_tarjetaPlan = 1;
|
||||||
Historial_cuenta::where('historial_id', $historial->id)->delete();
|
} catch (Exception $e) {
|
||||||
|
// Hacer rollback de cualquier cambio realizado en la base de datos
|
||||||
// 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();
|
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