Fix "sin cuentas disponibles" and wrong pricing in bot/chat
Three bugs fixed: 1. TelegramBotService queried Cuentas by tarifa_id + estado=activo which never matched. Now uses CuentasHelper::contar/buscar — same date-range + historial-count logic as the web (pendiente for full accounts, activo + slot check for per-screen accounts). 2. efectivePrice in TelegramBotService and PublicChat used the Preferencial table (legacy) instead of Usuario_tarifa (used by the web admin panel). CuentasHelper::precio checks Usuario_tarifa first, Preferencial as fallback. 3. payWithSaldo compared saldo against tarifa->valor (base price) instead of the effective price stored in state. Also registered the purchase with the wrong amount. Extract shared logic to CuentasHelper (contar, buscar, precio) to avoid duplication between TelegramBotService and PublicChat. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
ba4ec8b6cf
commit
1f25c31603
@@ -13,6 +13,7 @@ use App\Models\Historiale;
|
||||
use App\Models\Historial_cuenta;
|
||||
use App\Models\Preferencial;
|
||||
use App\Models\Promociones;
|
||||
use App\Services\CuentasHelper;
|
||||
use App\Models\TarifaPromo;
|
||||
use App\Models\Usuario_promo;
|
||||
use App\Models\recarga;
|
||||
@@ -1411,107 +1412,17 @@ class PublicChat extends Component
|
||||
|
||||
private function contarDisponibles(Tarifas $tarifa): int
|
||||
{
|
||||
$servicio = $tarifa->servicio;
|
||||
$fechaBase = now()->subDays(8)->format('Y-m-d');
|
||||
$fechaMin = now()->addDays($tarifa->dias - 15)->format('Y-m-d');
|
||||
$fechaMax = now()->addDays($tarifa->dias + 15)->format('Y-m-d');
|
||||
|
||||
if ((int) $tarifa->pantallas === (int) $servicio->completa) {
|
||||
return Cuentas::where('servicio_id', $servicio->id)
|
||||
->whereDate('vencimiento', '>=', $fechaMin)
|
||||
->whereDate('vencimiento', '<=', $fechaMax)
|
||||
->where('estado', 'pendiente')
|
||||
->doesntHave('perfil')
|
||||
->count();
|
||||
}
|
||||
|
||||
$suma = $servicio->pantallas - $tarifa->pantallas;
|
||||
$baseQ = Cuentas::select('*')
|
||||
->where('servicio_id', $servicio->id)
|
||||
->whereDate('inicio', '>=', $fechaBase)
|
||||
->whereDate('vencimiento', '>=', $fechaMin)
|
||||
->whereDate('vencimiento', '<=', $fechaMax)
|
||||
->where('estado', 'activo')
|
||||
->withCount('historiales');
|
||||
|
||||
$cuentas = Cuentas::fromSub($baseQ, 'alias')
|
||||
->where('historiales_count', '<=', $suma)
|
||||
->get();
|
||||
|
||||
if ($cuentas->isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$tomadas = Historial_cuenta::whereIn('cuenta_id', $cuentas->pluck('id'))
|
||||
->whereNotNull('historial_id')
|
||||
->count();
|
||||
|
||||
return (int) floor((($cuentas->count() * $servicio->pantallas) - $tomadas) / $tarifa->pantallas);
|
||||
return CuentasHelper::contar($tarifa);
|
||||
}
|
||||
|
||||
private function buscarCuentaDisponible(Tarifas $tarifa): ?Cuentas
|
||||
{
|
||||
$servicio = $tarifa->servicio;
|
||||
$fechaBase = now()->subDays(8)->format('Y-m-d');
|
||||
$fechaMin = now()->addDays($tarifa->dias - 15)->format('Y-m-d');
|
||||
$fechaMax = now()->addDays($tarifa->dias + 15)->format('Y-m-d');
|
||||
|
||||
if ((int) $tarifa->pantallas === (int) $servicio->completa) {
|
||||
return Cuentas::where('servicio_id', $servicio->id)
|
||||
->whereDate('vencimiento', '>=', $fechaMin)
|
||||
->whereDate('vencimiento', '<=', $fechaMax)
|
||||
->where('estado', 'pendiente')
|
||||
->doesntHave('perfil')
|
||||
->orderBy('inicio', 'ASC')
|
||||
->first();
|
||||
}
|
||||
|
||||
$suma = $servicio->pantallas - $tarifa->pantallas;
|
||||
$baseQ = Cuentas::select('*')
|
||||
->where('servicio_id', $servicio->id)
|
||||
->whereDate('inicio', '>=', $fechaBase)
|
||||
->whereDate('vencimiento', '>=', $fechaMin)
|
||||
->whereDate('vencimiento', '<=', $fechaMax)
|
||||
->where('estado', 'activo')
|
||||
->withCount('historiales')
|
||||
->orderBy('inicio', 'ASC');
|
||||
|
||||
$cuenta = Cuentas::fromSub($baseQ, 'alias')
|
||||
->where('historiales_count', '<=', $suma)
|
||||
->orderBy('inicio', 'ASC')
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if (! $cuenta) {
|
||||
$suma2 = $servicio->pantallas - $tarifa->pantallas;
|
||||
$baseQ2 = Cuentas::select('*')
|
||||
->where('servicio_id', $servicio->id)
|
||||
->whereDate('inicio', '>=', $fechaBase)
|
||||
->whereDate('vencimiento', '>=', $fechaMin)
|
||||
->whereDate('vencimiento', '<=', $fechaMax)
|
||||
->withCount('historiales')
|
||||
->where('estado', 'activo')
|
||||
->orderBy('inicio', 'ASC');
|
||||
|
||||
$cuenta = Cuentas::fromSub($baseQ2, 'alias')
|
||||
->where('historiales_count', '<=', $suma2)
|
||||
->orderBy('inicio', 'ASC')
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
}
|
||||
|
||||
return $cuenta;
|
||||
return CuentasHelper::buscar($tarifa);
|
||||
}
|
||||
|
||||
private function efectivePrice(int $tarifaId, ?int $userId, float $base): float
|
||||
{
|
||||
if ($userId) {
|
||||
$pref = Preferencial::where('usuario_id', $userId)->where('tarifa_id', $tarifaId)->first();
|
||||
if ($pref) {
|
||||
return (float) $pref->valor;
|
||||
}
|
||||
}
|
||||
return (float) $base;
|
||||
return CuentasHelper::precio($tarifaId, $userId, $base);
|
||||
}
|
||||
|
||||
private function volver(string $action): void
|
||||
|
||||
Reference in New Issue
Block a user