diff --git a/app/Http/Controllers/MenuController.php b/app/Http/Controllers/MenuController.php index ff602e1..c27bdba 100755 --- a/app/Http/Controllers/MenuController.php +++ b/app/Http/Controllers/MenuController.php @@ -285,13 +285,37 @@ class MenuController extends Controller //dd($cantidad_tarjetaPlan); if((int)$pantalla_servicio_completa >= (int)$cantidad_solicitada ){ $suma = $pantalla_servicio-$cantidad_solicitada; + $cuenta = Cuentas::where('servicio_id', $servicio_id) + ->whereDate('inicio', '>=', $fecha) + ->where('estado', 'activo') + ->orderBy('created_at', 'ASC') + ->withCount('historiales') // Contar los historiales + ->first(); - $cuenta = Cuentas::where('servicio_id',$servicio_id)->whereDate('inicio','>=',$fecha)->Orderby('created_at', 'ASC')->withCount('historiales')->where('estado','activo')->having('historiales_count', '<=', $suma)->first(); + if ($cuenta && $cuenta->historiales_count <= $suma) { + // La cuenta cumple con el filtro de cantidad de historiales. + } else { + // La cuenta no cumple con el filtro (tiene más historiales de los permitidos o no existe). + $cuenta = null; + } + }elseif((int)$pantalla_servicio_completa == (int)$cantidad_solicitada){ $this->fecha_final_1 = $today = Carbon::now()->addDays($pantallas->dias-15); $this->fecha_final_2 = $today = Carbon::now()->addDays($pantallas->dias+15); //completas - $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('created_at', 'ASC')->withCount('historiales')->where('estado','pendiente')->having('historiales_count', '==', 0)->first(); + $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('created_at', 'ASC') + ->withCount('historiales') // Contar los historiales + ->where('estado', 'pendiente') // Filtrar por estado pendiente + ->get(); // Obtener todas las cuentas que coincidan con los filtros + + $cuenta = $cuentas->first(function ($cuenta) { + return $cuenta->historiales_count == 0; // Filtrar solo cuentas que no tengan historiales + }); + }else{ $cuenta=null; } @@ -332,7 +356,17 @@ class MenuController extends Controller for ($h=0; $h < (int)$cantidad_tarjetaPlan ; $h++) { //dd($n); - $cuenta = Cuentas::where('servicio_id',$servicio_id)->whereDate('inicio','>=',$fecha)->Orderby('created_at', 'ASC')->with('historiales')->withCount('historiales')->where('estado','activo')->having('historiales_count', '<=', (int)$suma2)->first(); + $cuentas = Cuentas::where('servicio_id', $servicio_id) + ->whereDate('inicio', '>=', $fecha) + ->orderBy('created_at', 'ASC') + ->with('historiales') // Obtener los historiales relacionados + ->withCount('historiales') // Contar los historiales asociados + ->where('estado', 'activo') // Filtrar por estado 'activo' + ->get(); // Obtener todas las cuentas que coincidan con los filtros + $cuenta = $cuentas->first(function ($cuenta) use ($suma2) { + return $cuenta->historiales_count <= (int)$suma2; // Filtrar por el número de historiales + }); + //dd($cuenta); if (!empty($cuenta)) { $perfil = $cuenta->historiales_count; @@ -381,9 +415,34 @@ class MenuController extends Controller if ($pantalla_servicio_completa == $pantallas_tarifa) { $this->fecha_final_1 = $today = Carbon::now()->addDays($tarifa->dias-15); $this->fecha_final_2 = $today = Carbon::now()->addDays($tarifa->dias+15); - $cuenta = Cuentas::where('servicio_id',$servicio_id)->whereDate('inicio','>=',$fecha)->whereDate('vencimiento','>=',$this->fecha_final_1)->whereDate('vencimiento','<=',$this->fecha_final_2)->Orderby('created_at', 'ASC')->with('historiales')->withCount('historiales')->where('estado','pendiente')->having('historiales_count', '==', 0)->first(); + $cuentas = Cuentas::where('servicio_id', $servicio_id) + ->whereDate('inicio', '>=', $fecha) + ->whereDate('vencimiento', '>=', $this->fecha_final_1) + ->whereDate('vencimiento', '<=', $this->fecha_final_2) + ->orderBy('created_at', 'ASC') + ->with('historiales') // Relación con los historiales + ->withCount('historiales') // Contar los historiales + ->where('estado', 'pendiente') // Filtrar por estado 'pendiente' + ->get(); // Obtener todas las cuentas que coincidan con los filtros + + $cuenta = $cuentas->first(function ($cuenta) { + return $cuenta->historiales_count == 0; // Filtrar solo cuentas que no tengan historiales + }); + }else{ - $cuenta = Cuentas::where('servicio_id', $servicio_id)->whereDate('inicio','>=',$fecha)->Orderby('created_at', 'ASC')->with('historiales')->withCount('historiales')->where('estado', 'activo')->having('historiales_count', '<=', (int)$faltante)->first(); + $cuentas = Cuentas::where('servicio_id', $servicio_id) + ->whereDate('inicio', '>=', $fecha) + ->orderBy('created_at', 'ASC') + ->with('historiales') // Relación con los historiales + ->withCount('historiales') // Contar los historiales asociados + ->where('estado', 'activo') // Filtrar por el estado 'activo' + ->get(); // Obtener todas las cuentas que cumplan con los filtros + + $cuenta = $cuentas->first(function ($cuenta) use ($faltante) { + return $cuenta->historiales_count <= (int) $faltante; + }); + + } //dd($tarifa); diff --git a/app/Http/Livewire/ShowPromociones.php b/app/Http/Livewire/ShowPromociones.php index 3c61994..5cc5a32 100755 --- a/app/Http/Livewire/ShowPromociones.php +++ b/app/Http/Livewire/ShowPromociones.php @@ -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(); diff --git a/app/Http/Livewire/ShowServicios.php b/app/Http/Livewire/ShowServicios.php index f110fdd..5cd10cc 100755 --- a/app/Http/Livewire/ShowServicios.php +++ b/app/Http/Livewire/ShowServicios.php @@ -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' ]); - } }