diff --git a/app/Http/Livewire/ShowPromociones.php b/app/Http/Livewire/ShowPromociones.php index 5cc5a32..99e2f50 100755 --- a/app/Http/Livewire/ShowPromociones.php +++ b/app/Http/Livewire/ShowPromociones.php @@ -278,62 +278,37 @@ 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) + $baseQuery = Cuentas::select('*')->where('servicio_id', $servicio_id) ->whereDate('inicio', '>=', $this->fecha) ->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '<=', $this->fecha_final_2) ->Orderby('inicio', 'ASC')->with('historiales') ->withCount('historiales') - ->where('estado', 'pendiente') - ->having('historiales_count', '==', 0) - ->first(); */ + ->where('estado', 'pendiente'); - $cuenta = Cuentas::where('servicio_id', $servicio_id) - ->whereDate('inicio', '>=', $this->fecha) - ->whereDate('vencimiento', '>=', $this->fecha_final_1) - ->whereDate('vencimiento', '<=', $this->fecha_final_2) - ->where('estado', 'pendiente') + $cuenta = Cuentas::fromSub($baseQuery, 'alias') + ->where('historiales_count', '==', 0) ->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) + $baseQuery = Cuentas::select('*')->where('servicio_id', $servicio_id) ->whereDate('inicio', '>=', $this->fecha) - ->where('estado', 'activo') + ->Orderby('inicio', 'ASC') + ->with('historiales') + ->withCount('historiales') + ->where('estado', 'activo'); + + $cuenta = Cuentas::fromSub($baseQuery, 'alias') + ->where('historiales_count', '<=', (int) $faltante) ->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)) { $perfil = $cuenta->historiales_count; for ($y = 0; $y < (int) $pantallas_tarifa; $y++) { diff --git a/app/Http/Livewire/ShowServicios.php b/app/Http/Livewire/ShowServicios.php index d9a7c93..dcc7364 100755 --- a/app/Http/Livewire/ShowServicios.php +++ b/app/Http/Livewire/ShowServicios.php @@ -178,31 +178,21 @@ 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) + $baseQuery = Cuentas::select('*')->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(); - */ + ->where('estado', 'activo'); + + $cuentas_pantallas = Cuentas::fromSub($baseQuery, 'alias') + ->where('historiales_count', '<=', $cantidad_restante) + ->orderBy('inicio', 'ASC') + ->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') - ->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 @@ -278,31 +268,30 @@ class ShowServicios extends Component $usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null; $usuario_id = $usuario_existe; - 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(); + 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_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(); + $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(); @@ -316,60 +305,49 @@ class ShowServicios extends Component 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; - $cuentas = Cuentas::where('servicio_id', $this->servicio_id) + + $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') ->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) - ->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) + $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') ->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) { //Cuentas completas $cantidad_necesaria = $this->cantidad_tarjetaPlan; $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') @@ -377,35 +355,25 @@ class ShowServicios extends Component ->where('estado', 'pendiente') ->doesntHave('perfil') ->get(); - + + } elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) { + $suma2 = $pantalla_servicio - $pantallas; $cantidad_necesaria = $this->cantidad_tarjetaPlan; - /* $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) + $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') + ->Orderby('inicio', 'ASC') + ->withCount('historiales') + ->where('estado', 'activo'); + + $cuenta = Cuentas::fromSub($baseQuery, 'alias') + ->where('historiales_count', '<=', $suma2) ->orderBy('inicio', 'ASC') ->get(); - $cuentas_filtradas = $consulta_cuentas->filter(function ($cuenta) use ($suma2) { - $historiales_count = $cuenta->historiales()->count(); - return $historiales_count <= $suma2; - }); - - $cuenta = $cuentas_filtradas->values(); - } else { $cuenta = null; @@ -414,7 +382,7 @@ class ShowServicios extends Component $cuentasMostar = ''; for ($h = 0; $h < (int) $cantidad_necesaria; $h++) { - //dd($cuenta); + if (!empty($cuenta) && isset($cuenta[$h])) { $esta_error = false; @@ -505,11 +473,11 @@ class ShowServicios extends Component } 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'