simulador_aplicacao 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4671cc366b1055f5411ec85ffbfdf12fab95c39e7379a30fa25848877f8b5726
4
+ data.tar.gz: e095c7ad8658331e3aca68ee54754cb571e4d5441416e5ec0f802eb65fb7d0a1
5
+ SHA512:
6
+ metadata.gz: 01c192b2a739a86c09bff3f2b8c74a943ff97e73dcaf779160934a6c58ba4b485bb62f058da923c7d4f447d8b60260e270954d15580e23095c20b10eda93d026
7
+ data.tar.gz: 31313e598373bccbb6bb3fe46550b4fc9292e294800bf6ec1f3fed8e5e673ac4edc2fdf9e9f442462ab2c4450bfcd5bc71f5399e2b8b999bf596b8f694f0c37b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 vinifmatos
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Simulador de Aplicacao
2
+
3
+ Uma gem para simular uma aplicação financeira com o CDI, SELIC ou IPCA.
4
+
5
+ ## Instalação
6
+
7
+ gem install simulador_aplicacao
8
+
9
+ ## Como Usar
10
+
11
+ Execute o comando `simulador_aplicacao`
12
+ E depois informe os dados requisitados
13
+
14
+ ## Contriuindo
15
+
16
+ Bug reports e pull requests serão bem vindos no GitHub https://github.com/vinifmatos/simulador_aplicacao.
17
+
18
+ ## Licença
19
+
20
+ Essa gem está publicada sob os termos sa licença [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tty-prompt"
4
+ require "tty-table"
5
+ require "simulador_aplicacao"
6
+
7
+ prompt = TTY::Prompt.new
8
+
9
+ parametros = {
10
+ aporte_inicial: prompt.ask(
11
+ "Informe o valor da aplicação inicial:",
12
+ convert: :float,
13
+ default: 1000.0,
14
+ required: true
15
+ ),
16
+ indice: prompt.select("Qual vai ser o indice da aplicação?") do |menu|
17
+ SimuladorAplicacao::URL_API_BACEN.keys.map do |k|
18
+ menu.choice k.to_s.upcase, k
19
+ end
20
+ menu.default 1
21
+ end,
22
+ periodo: prompt.ask(
23
+ "Informe o tempo de aplicação (em meses):",
24
+ convert: :int,
25
+ default: 12,
26
+ required: true
27
+ )
28
+ }
29
+
30
+ tera_aporte_mensal = prompt.yes?("Deseja fazer aportes mensais?") do |q|
31
+ q.default false
32
+ q.positive "Sim"
33
+ q.negative "Não"
34
+ end
35
+
36
+ if tera_aporte_mensal
37
+ parametros[:aporte_mensal] = prompt.ask(
38
+ "Informe o valor do aporte mensal:",
39
+ convert: :float,
40
+ required: true
41
+ )
42
+ else
43
+ parametros[:aporte_mensal] = 0.0
44
+ end
45
+
46
+ tera_resgate_mensal = prompt.yes?("Deseja fazer resgates mensais?") do |q|
47
+ q.default false
48
+ q.positive "Sim"
49
+ q.negative "Não"
50
+ end
51
+
52
+ if tera_resgate_mensal
53
+ parametros[:resgate_mensal] = prompt.ask(
54
+ "Informe o valor do resgate mensal:",
55
+ convert: :float,
56
+ required: true
57
+ )
58
+ parametros[:inicio_resgate] = prompt.ask(
59
+ "Informe o mês de iníco do resgate:",
60
+ default: 1,
61
+ required: true,
62
+ convert: :int
63
+ ) do |q|
64
+ q.in("1-#{parametros[:periodo]}")
65
+ end
66
+ else
67
+ parametros[:resgate_mensal] = 0.0
68
+ parametros[:inicio_resgate] = parametros[:periodo]
69
+ end
70
+
71
+ simulacao = SimuladorAplicacao.simulacao(
72
+ parametros[:aporte_inicial],
73
+ parametros[:periodo],
74
+ parametros[:indice],
75
+ parametros[:aporte_mensal],
76
+ parametros[:resgate_mensal],
77
+ parametros[:inicio_resgate]
78
+ )
79
+
80
+ tabela_simulacao = TTY::Table.new(
81
+ header: [
82
+ "Mês",
83
+ "Saldo Inicial",
84
+ "Taxa",
85
+ "Rendimento Bruto",
86
+ "Alíquota I.R.",
87
+ "I.R",
88
+ "Rendimento Líquido",
89
+ "Aporte",
90
+ "Resgate",
91
+ "Total Investido",
92
+ "Resultado",
93
+ "Total Acumulado"
94
+ ],
95
+ rows: simulacao.map do |mes|
96
+ mes.map do |k, v|
97
+ if k == :aliquota_imposto_renda
98
+ "#{format("%.2f", v * 100)}%"
99
+ else
100
+ v.is_a?(Float) ? format("%.2f", v).gsub(".", ",").gsub(/(\d)(?=(\d{3})+,)/, '\1.') : v
101
+ end
102
+ end
103
+ end
104
+ )
105
+
106
+ puts tabela_simulacao.render(:unicode, padding: [0, 1], alignment: :center, resize: true, multiline: false)
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimuladorAplicacao
4
+ VERSION = "0.2.0"
5
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "simulador_aplicacao/version"
4
+ require "net/http"
5
+ require "uri"
6
+ require "json"
7
+ require "optparse"
8
+
9
+ module SimuladorAplicacao
10
+ URL_API_BACEN = {
11
+ cdi: "https://api.bcb.gov.br/dados/serie/bcdata.sgs.4392/dados/ultimos/1?formato=json",
12
+ selic: "https://api.bcb.gov.br/dados/serie/bcdata.sgs.1178/dados/ultimos/1?formato=json",
13
+ ipca: "https://api.bcb.gov.br/dados/serie/bcdata.sgs.13522/dados/ultimos/1?formato=json"
14
+ }.freeze
15
+
16
+ def self.simulacao(aporte_inicial, periodo, indice, aporte_mensal, resgate_mensal, inicio_resgate)
17
+ taxa_anual = obter_taxa_anual_atualizada(indice)
18
+ taxa_juros_mensal = taxa_anual_para_mensal(taxa_anual)
19
+ total_acumulado = aporte_inicial
20
+ total_investido = aporte_inicial
21
+ rendimento_acumulado = 0
22
+ rendimento_a_tributar = 0
23
+ imposto_renda_pago = 0
24
+ puts "Simulando com #{indice.to_s.upcase} #{format("%.2f", taxa_anual * 100)}% a.a"
25
+ resultado_mensal = [
26
+ {
27
+ mes: 0,
28
+ inicial: total_acumulado,
29
+ taxa: "#{taxa_juros_mensal * 100}% a.m",
30
+ rendimento_bruto: 0.0,
31
+ aliquota_imposto_renda: 0.0,
32
+ imposto_renda: 0.0,
33
+ rendimento_liquido: 0.0,
34
+ aporte: 0.0,
35
+ resgate: 0.0,
36
+ total_investido: total_investido,
37
+ resultado: 0.0,
38
+ acumulado: total_acumulado
39
+ }
40
+ ]
41
+ (1..periodo).each do |mes|
42
+ incial = total_acumulado
43
+ rendimento = incial * taxa_juros_mensal
44
+ rendimento_acumulado += rendimento
45
+ total_acumulado += rendimento
46
+ rendimento_a_tributar += rendimento
47
+ if mes >= inicio_resgate
48
+ imposto_renda = rendimento_a_tributar * aliquota_ir(mes)
49
+ resgate = resgate_mensal
50
+ total_acumulado -= (resgate + imposto_renda)
51
+ imposto_renda_pago += imposto_renda
52
+ rendimento_a_tributar = 0
53
+ else
54
+ imposto_renda = 0.0
55
+ resgate = 0.0
56
+ end
57
+ total_acumulado += aporte_mensal
58
+ total_investido += aporte_mensal
59
+ resultado_mensal << {
60
+ mes: mes,
61
+ inicial: total_acumulado,
62
+ taxa: "#{taxa_juros_mensal * 100}% a.m",
63
+ rendimento_bruto: rendimento,
64
+ aliquota_imposto_renda: aliquota_ir(mes),
65
+ imposto_renda: imposto_renda,
66
+ rendimento_liquido: rendimento - imposto_renda,
67
+ aporte: aporte_mensal,
68
+ resgate: resgate,
69
+ total_investido: total_investido,
70
+ resultado: rendimento - imposto_renda - resgate + aporte_mensal,
71
+ acumulado: total_acumulado
72
+ }
73
+ end
74
+ resultado_mensal
75
+ end
76
+
77
+ def self.aliquota_ir(mes)
78
+ case
79
+ when mes < 3 then 22.5
80
+ when mes < 12 then 20
81
+ when mes < 24 then 17.5
82
+ else 15
83
+ end / 100.0
84
+ end
85
+
86
+ def self.taxa_anual_para_mensal(taxa_anual)
87
+ ((1.0 + taxa_anual)**(1.0 / 12.0) - 1.0).round(4)
88
+ end
89
+
90
+ def self.obter_taxa_anual_atualizada(indice)
91
+ puts "Obtendo #{indice.to_s.upcase} atual..."
92
+ taxa = JSON.parse(Net::HTTP.get(URI.parse(URL_API_BACEN[indice]))).first.fetch("valor").to_f
93
+ puts "Concluído"
94
+ taxa / 100.0
95
+ rescue StandardError => e
96
+ puts "Erro ao consultar a API"
97
+ puts e
98
+ exit
99
+ end
100
+ end
@@ -0,0 +1,4 @@
1
+ module SimuladorAplicacao
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/simulador_aplicacao/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "simulador_aplicacao"
7
+ spec.version = SimuladorAplicacao::VERSION
8
+ spec.authors = ["vinifmatos"]
9
+ spec.email = ["viniciusfreire4@gmail.com"]
10
+
11
+ spec.summary = "Simulador de Aplicação Financeira"
12
+ spec.description = "Essa gem faz uma simulação de uma aplicação financeira utilizando como índice o CDI, o SELIC ou o IPCA, para um montante inicial pelo periodo desejado."
13
+ spec.homepage = "https://github.com/vinifmatos"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/vinifmatos/simulador_aplicacao"
19
+ spec.metadata["changelog_uri"] = "https://github.com/vinifmatos/simulador_aplicacao/README.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) ||
26
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = ["simulador_aplicacao"]
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ spec.add_dependency "tty-prompt", "~> 0.23"
35
+ spec.add_dependency "tty-table", "~> 0.12"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simulador_aplicacao
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - vinifmatos
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tty-prompt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.23'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.23'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.12'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.12'
41
+ description: Essa gem faz uma simulação de uma aplicação financeira utilizando como
42
+ índice o CDI, o SELIC ou o IPCA, para um montante inicial pelo periodo desejado.
43
+ email:
44
+ - viniciusfreire4@gmail.com
45
+ executables:
46
+ - simulador_aplicacao
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - exe/simulador_aplicacao
56
+ - lib/simulador_aplicacao.rb
57
+ - lib/simulador_aplicacao/version.rb
58
+ - sig/simulador_aplicacao.rbs
59
+ - simulador_aplicacao.gemspec
60
+ homepage: https://github.com/vinifmatos
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ homepage_uri: https://github.com/vinifmatos
65
+ source_code_uri: https://github.com/vinifmatos/simulador_aplicacao
66
+ changelog_uri: https://github.com/vinifmatos/simulador_aplicacao/README.md
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.6.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.4.21
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Simulador de Aplicação Financeira
86
+ test_files: []