diff --git a/app/Http/Livewire/ShowServicios.php b/app/Http/Livewire/ShowServicios.php index fa8b7b7..6de5d36 100755 --- a/app/Http/Livewire/ShowServicios.php +++ b/app/Http/Livewire/ShowServicios.php @@ -26,6 +26,7 @@ use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Notification; use Twilio\Rest\Client; use AshAllenDesign\ShortURL\Facades\ShortURL; +use Illuminate\Support\Facades\DB; class ShowServicios extends Component { @@ -239,89 +240,125 @@ class ShowServicios extends Component /* ---------------- Btn Comprar ---------------- */ public function comprar() { - $this->validate([ - 'cantidad_tarjetaPlan' => 'required|numeric|min:1', - ]); + DB::beginTransaction(); + try { - if ($this->cantidad_tarjetaPlan == 0) { - $this->alert('error', 'La cantidad debe ser mayor a 0', [ - 'position' => 'top' + $this->validate([ + 'cantidad_tarjetaPlan' => 'required|numeric|min:1', ]); - return; - } - if (auth()->user()->saldo->valor < $this->valor_tarjetaPlan * $this->cantidad_tarjetaPlan) { - $this->alert('error', 'Saldo insuficiente', [ - 'position' => 'top' - ]); - return; - } + if ($this->cantidad_tarjetaPlan == 0) { + $this->alert('error', 'La cantidad debe ser mayor a 0', [ + 'position' => 'top' + ]); + return; + } - $cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan; - $total_valor = (int) $this->valor_tarjetaPlan * (int) $this->cantidad_tarjetaPlan; - $utilidad_total = (int) $this->utilidad * (int) $this->cantidad_tarjetaPlan; + if (auth()->user()->saldo->valor < $this->valor_tarjetaPlan * $this->cantidad_tarjetaPlan) { + $this->alert('error', 'Saldo insuficiente', [ + 'position' => 'top' + ]); + return; + } - $usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null; - $usuario_id = $usuario_existe; + $cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan; + $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 = new User; - $usuario->name = $this->nombre_tarjetaPlan; - $usuario->email = $this->email_tarjetaPlan; - $usuario->password = '123'; - $usuario->celular = '+57' . $this->celular_tarjetaPlan; - $usuario->save(); + $usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null; + $usuario_id = $usuario_existe; - $usuario_id = $usuario->id; - } + 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(); - //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; + } - $pantallas = Tarifas::where('id', $this->tarifa_id)->with('servicio')->first(); + //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; - $servicio_id = $pantallas->servicio_id; - $pantalla_servicio_completa = $pantallas->servicio->completa; - $pantalla_servicio = $pantallas->servicio->pantallas; + $servicioNombre = $pantallas->servicio->nombre; + $servicio_id = $pantallas->servicio_id; + $pantalla_servicio_completa = $pantallas->servicio->completa; + $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) { - $suma = $pantalla_servicio - $cantidad_solicitada; - $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) - // ->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"); - //}) - ->get(); - if ($cuenta->isEmpty()) { - $suma3 = $pantalla_servicio - $pantallas; + 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) + ->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"); + //}) + ->get(); + 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) + //->with('perfil') + //->whereDoesntHave('perfil', function ($query) use ($suma2) { + //$query->select('cuenta_id') + // ->groupBy('cuenta_id') + // ->havingRaw("COUNT(*) >= $suma2"); + //}) + ->get(); + } + // 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') + ->withCount('historiales') + ->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) @@ -330,7 +367,7 @@ class ShowServicios extends Component ->Orderby('inicio', 'ASC') ->withCount('historiales') ->where('estado', 'activo') - ->having('historiales_count', '<=', $suma3) + ->having('historiales_count', '<=', $suma2) //->with('perfil') //->whereDoesntHave('perfil', function ($query) use ($suma2) { //$query->select('cuenta_id') @@ -338,133 +375,109 @@ class ShowServicios extends Component // ->havingRaw("COUNT(*) >= $suma2"); //}) ->get(); - } - // 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') - ->withCount('historiales') - ->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) - //->with('perfil') - //->whereDoesntHave('perfil', function ($query) use ($suma2) { - //$query->select('cuenta_id') - // ->groupBy('cuenta_id') - // ->havingRaw("COUNT(*) >= $suma2"); - //}) - ->get(); - } else { - $cuenta = null; - $cantidad_necesaria = null; - } - - $cuentasMostar = ''; - for ($h = 0; $h < (int) $cantidad_necesaria; $h++) { - //dd($cuenta); - - if (!empty($cuenta) && isset($cuenta[$h])) { - $esta_error = false; - $cuentasMostar = ''; - - $perfil = $cuenta[$h]->historiales_count; - - for ($y = 0; $y < (int) $pantallas; $y++) { - - $historial_cuenta = new Historial_cuenta; - $historial_cuenta->cuenta_id = $cuenta[$h]->id; - $historial_cuenta->historial_id = $historial->id; - $historial_cuenta->perfil = (int) $perfil + (int) $y + 1; - $historial_cuenta->save(); - - if ($cuenta[$h]->correo != $cuentasMostar) { - $cuentasMostar .= $cuenta[$h]->correo . ' '; - } - } } else { - $esta_error = true; - $historial->update(['estado' => 'cancelado']); + $cuenta = null; + $cantidad_necesaria = null; + } + + $cuentasMostar = ''; + for ($h = 0; $h < (int) $cantidad_necesaria; $h++) { + //dd($cuenta); + + if (!empty($cuenta) && isset($cuenta[$h])) { + $esta_error = false; + $cuentasMostar = ''; + + $perfil = $cuenta[$h]->historiales_count; + + for ($y = 0; $y < (int) $pantallas; $y++) { + + $historial_cuenta = new Historial_cuenta; + $historial_cuenta->cuenta_id = $cuenta[$h]->id; + $historial_cuenta->historial_id = $historial->id; + $historial_cuenta->perfil = (int) $perfil + (int) $y + 1; + $historial_cuenta->save(); + + if ($cuenta[$h]->correo != $cuentasMostar) { + $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']); + + $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 { + $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->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->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']); - - $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 { + DB::commit(); - $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!', [ + $this->tarjetaPlan = false; + $this->cantidad_tarjetaPlan = 1; + } catch (Exception $e) { + + DB::rollBack(); + $this->alert('error', 'Error al comprar: ' . $e->getMessage(), [ 'position' => 'top' ]); + } - - - $this->tarjetaPlan = false; - $this->cantidad_tarjetaPlan = 1; } public function credenciales($historial)