cambio real

This commit is contained in:
lizandrogd
2025-04-03 22:28:20 -05:00
parent 8d419edbed
commit ac229cdb74
3 changed files with 192 additions and 64 deletions
+49 -13
View File
@@ -278,7 +278,7 @@ class ShowPromociones extends Component
$this->fecha_final_1 = $today = Carbon::now()->addDays($tarifa->dias - 20);
$this->fecha_final_2 = $today = Carbon::now()->addDays($tarifa->dias + 20);
$cuenta = Cuentas::where('servicio_id', $servicio_id)
/* $cuenta = Cuentas::where('servicio_id', $servicio_id)
->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2)
@@ -286,16 +286,52 @@ class ShowPromociones extends Component
->withCount('historiales')
->where('estado', 'pendiente')
->having('historiales_count', '==', 0)
->first();
} else {
->first(); */
$cuenta = Cuentas::where('servicio_id', $servicio_id)
->whereDate('inicio', '>=', $this->fecha)
->Orderby('inicio', 'ASC')
->with('historiales')
->withCount('historiales')
->where('estado', 'activo')
->having('historiales_count', '<=', (int) $faltante)
->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2)
->where('estado', 'pendiente')
->orderBy('inicio', 'ASC')
->with('historiales') // Relación de historiales
->withCount('historiales') // Contar historiales
->first();
if ($cuenta) {
// Filtrar la cuenta si tiene 0 historiales
if ($cuenta->historiales_count != 0) {
$cuenta = null; // Si
}
}
} else {
/* $cuenta = Cuentas::where('servicio_id', $servicio_id)
->whereDate('inicio', '>=', $this->fecha)
->Orderby('inicio', 'ASC')
->with('historiales')
->withCount('historiales')
->where('estado', 'activo')
->having('historiales_count', '<=', (int) $faltante)
->first(); */
$cuenta = Cuentas::where('servicio_id', $servicio_id)
->whereDate('inicio', '>=', $this->fecha)
->where('estado', 'activo')
->orderBy('inicio', 'ASC')
->with('historiales') // Relación de historiales
->withCount('historiales') // Contar historiales
->first();
if ($cuenta && $cuenta->historiales_count <= (int) $faltante) {
// La cuenta cumple con el filtro, puedes usarla.
} else {
// La cuenta no cumple con el filtro, la descartas o manejas según sea necesario.
$cuenta = null;
}
}
if (!is_null($cuenta)) {
@@ -363,7 +399,7 @@ class ShowPromociones extends Component
$logsGeneral = new Log_general();
$logsGeneral->user_id = Auth::user()->id;
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha comprado la promocion ' . $this->consulta->nombre . ' - ' . $this->consulta->descripcion . ' cantidad de ' . $this->historial->cantidad . ' por un valor de $' . $this->historial->valor .', Saldo anterior: $'.$saldo_anterior.', Nuevo saldo disponible: $'.$nuevo_saldo;
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha comprado la promocion ' . $this->consulta->nombre . ' - ' . $this->consulta->descripcion . ' cantidad de ' . $this->historial->cantidad . ' por un valor de $' . $this->historial->valor . ', Saldo anterior: $' . $saldo_anterior . ', Nuevo saldo disponible: $' . $nuevo_saldo;
$logsGeneral->tipo = 'compra';
$logsGeneral->historial_id = $historial->id;
$logsGeneral->save();
@@ -405,7 +441,7 @@ class ShowPromociones extends Component
$logsGeneral = new Log_general();
$logsGeneral->user_id = Auth::user()->id;
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha comprado la promocion ' . $this->consulta->nombre . ' - ' . $this->consulta->descripcion . ' cantidad de ' . $this->historial->cantidad . ' por un valor de $' . $this->historial->valor ?? '' . ' y algunos han quedado pendientes'.', Saldo anterior: $'.$saldo_anterior.', Nuevo saldo disponible: $'.$nuevo_saldo;
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha comprado la promocion ' . $this->consulta->nombre . ' - ' . $this->consulta->descripcion . ' cantidad de ' . $this->historial->cantidad . ' por un valor de $' . $this->historial->valor ?? '' . ' y algunos han quedado pendientes' . ', Saldo anterior: $' . $saldo_anterior . ', Nuevo saldo disponible: $' . $nuevo_saldo;
$logsGeneral->tipo = 'compra';
$logsGeneral->historial_id = $this->historial->id;
$logsGeneral->save();
@@ -415,9 +451,9 @@ class ShowPromociones extends Component
Historial_cuenta::whereIn('id', $this->n)->delete();
$logsGeneral = new Log_general();
$logsGeneral->user_id = Auth::user()->id;
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo error en el cobro, Comprando la promoción ' . $this->consulta->nombre . ' cantidad de ' . $this->historial->cantidad . ' por un valor de $' . $this->historial->valor .' No se desconto el saldo, ni se asignaron cuentas.';
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo error en el cobro, Comprando la promoción ' . $this->consulta->nombre . ' cantidad de ' . $this->historial->cantidad . ' por un valor de $' . $this->historial->valor . ' No se desconto el saldo, ni se asignaron cuentas.';
$logsGeneral->tipo = 'compra_error';
$logsGeneral->historial_id = $this->historial->id;
$logsGeneral->historial_id = $this->historial->id;
$logsGeneral->save();
$this->alert('warning', 'Error en el cobro, comuniquese con soporte!', [
'position' => 'top'
@@ -439,7 +475,7 @@ class ShowPromociones extends Component
Historial_cuenta::whereIn('id', $this->n)->delete();
$logsGeneral = new Log_general();
$logsGeneral->user_id = Auth::user()->id;
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Cancelo la compra del plan ' . $this->consulta->nombre . ' cantidad de ' . $this->historial->cantidad . ' por un valor de $' . $this->historial->valor.' No se desconto el saldo, ni se asignaron cuentas.';
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Cancelo la compra del plan ' . $this->consulta->nombre . ' cantidad de ' . $this->historial->cantidad . ' por un valor de $' . $this->historial->valor . ' No se desconto el saldo, ni se asignaron cuentas.';
$logsGeneral->tipo = 'compra_cancelada';
$logsGeneral->historial_id = $this->historial->id;
$logsGeneral->save();
+79 -46
View File
@@ -178,21 +178,31 @@ class ShowServicios extends Component
$cantidad_restante = $servicio_tarifa->pantallas - $cantidad_solicitada;
//Traer todas las cuentas por pantalla 'activo' de los ultimos 15 dias. solo disponibles
$cuentas_pantallas = Cuentas::whereDate('inicio', '>=', $this->fecha)
/* $cuentas_pantallas = Cuentas::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')
->having('historiales_count', '<=', $cantidad_restante)
->get();
*/
$cuentas = Cuentas::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')
//->with('perfil')
//->whereDoesntHave('perfil', function ($query) use ($cantidad_restante) {
// $query->select('cuenta_id')
// ->groupBy('cuenta_id')
// ->havingRaw("COUNT(*) >= $cantidad_restante");
//})
->withCount('historiales')
->where('estado', 'activo')
->having('historiales_count', '<=', $cantidad_restante)
->get();
$cuentas_filtradas = $cuentas->filter(function ($cuenta) use ($cantidad_restante) {
$historiales_count = $cuenta->historiales()->count();
return $historiales_count <= $cantidad_restante;
});
$cuentas_pantallas = $cuentas_filtradas->values();
//dd($cuentas_pantallas); // con count da un numero entero
$cuentas_pantallas_tomadas = Historial_cuenta::whereIn('cuenta_id', $cuentas_pantallas
@@ -308,40 +318,50 @@ class ShowServicios extends Component
if ((int) $pantalla_servicio >= (int) $cantidad_solicitada && (int) $pantalla_servicio_completa != (int) $this->pantallas_tarjetaPlan) {
$suma = $pantalla_servicio - $cantidad_solicitada;
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
$cuenta = Cuentas::where('servicio_id', $this->servicio_id)
$cuentas = Cuentas::where('servicio_id', $this->servicio_id)
->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2)
// ->whereDate('vencimiento','>=', $this->fechaFinal)
->Orderby('inicio', 'ASC')
->withCount('historiales')
->where('estado', 'activo')
->having('historiales_count', '<=', $suma)
//->with('perfil')
//->whereDoesntHave('perfil', function ($query) use ($suma) {
// $query->select('cuenta_id')
// ->groupBy('cuenta_id')
// ->havingRaw("COUNT(*) >= $suma");
//})
->orderBy('inicio', 'ASC')
->get();
$cuentas_filtradas = $cuentas->filter(function ($cuenta) use ($suma) {
$historiales_count = $cuenta->historiales()->count();
return $historiales_count <= $suma;
});
$cuenta = $cuentas_filtradas->values();
if ($cuenta->isEmpty()) {
$suma3 = $pantalla_servicio - $pantallas;
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
$cuenta = Cuentas::where('servicio_id', $this->servicio_id)
/* $cuenta = Cuentas::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')
->having('historiales_count', '<=', $suma3)
->get(); */
$consulta_cuentas = Cuentas::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')
->having('historiales_count', '<=', $suma3)
//->with('perfil')
//->whereDoesntHave('perfil', function ($query) use ($suma2) {
//$query->select('cuenta_id')
// ->groupBy('cuenta_id')
// ->havingRaw("COUNT(*) >= $suma2");
//})
->orderBy('inicio', 'ASC')
->get();
$cuentas_filtradas = $consulta_cuentas->filter(function ($cuenta) use ($suma3) {
$historiales_count = $cuenta->historiales()->count();
return $historiales_count <= $suma3;
});
$cuenta = $cuentas_filtradas->values();
}
// dd($cuenta);
} elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) {
@@ -360,21 +380,32 @@ class ShowServicios extends Component
} elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) {
$suma2 = $pantalla_servicio - $pantallas;
$cantidad_necesaria = $this->cantidad_tarjetaPlan;
$cuenta = Cuentas::where('servicio_id', $this->servicio_id)
/* $cuenta = Cuentas::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')
->having('historiales_count', '<=', $suma2)
->get(); */
$consulta_cuentas = Cuentas::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')
->having('historiales_count', '<=', $suma2)
//->with('perfil')
//->whereDoesntHave('perfil', function ($query) use ($suma2) {
//$query->select('cuenta_id')
// ->groupBy('cuenta_id')
// ->havingRaw("COUNT(*) >= $suma2");
//})
->orderBy('inicio', 'ASC')
->get();
$cuentas_filtradas = $consulta_cuentas->filter(function ($cuenta) use ($suma2) {
$historiales_count = $cuenta->historiales()->count();
return $historiales_count <= $suma2;
});
$cuentas = $cuentas_filtradas->values();
} else {
$cuenta = null;
$cantidad_necesaria = null;
@@ -471,15 +502,17 @@ class ShowServicios extends Component
$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 =[];
$this->alert('warning', 'Error al comprar: Esta cuenta ha sido tomada por otro usuario al mismo tiempo, intenta nuevamente' , [
$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'
]);
}
}