emites-rails 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a213b50870e6b05371f20d7e8844f5def43dfeba
4
- data.tar.gz: a69c85217ca4d3043bc59ae65baf35d19c6b34bf
3
+ metadata.gz: 934f8260d2bd17c47df432c18457992ca1159f69
4
+ data.tar.gz: d5499aada97b4c386e12479acc7f80d94ba9b386
5
5
  SHA512:
6
- metadata.gz: a38ceb6a2e1b4ff078c7cf191d61926fff3f680eee945b7cde10d2bb87948a0f18a04b9c4211552b513f6f95e005aebbf681220619dcdb8d1853749ae99ff218
7
- data.tar.gz: 6478c4e5167752bb5ea0ce3343a4bd2bcf449b608526af5f7b91a835e9baa37f37a828986aeadfd8c61fa160a150f107b39f4e9dc6b016d8068c0476f33445d0
6
+ metadata.gz: 173501c9e70707f5aa7ed65018176a4737d2de7d7bdb33c3f87711c366c799120e00132828cb8b38c686094113182c9eb12f0614eb61e9e02849f3cbbd545ab5
7
+ data.tar.gz: be7f195c9065617675c8b19724006b1240a308f34f6f2213a57fa0242c2e5a1eb9100c1ccf5cbb12cc829c6f4da085401cc84fbac0d62b8cc111f00d92263744
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1 @@
1
+ emites-rails
@@ -0,0 +1 @@
1
+ ruby-2.1.2
@@ -1,5 +1,9 @@
1
1
  # require 'emites/version'
2
2
  require 'httparty'
3
+ require 'emites/version'
4
+ require 'emites/emitente'
5
+ require 'emites/tomador'
6
+ require 'emites/emissao'
3
7
 
4
8
  module Emites
5
9
 
@@ -7,219 +11,16 @@ module Emites
7
11
 
8
12
  attr_accessor :token, :endpoint
9
13
 
10
- def self.servico_ccde
11
- 'Custódia de XMLs'
12
- end
13
-
14
- def self.setup(token,production=false)
14
+ def self.setup(token,production)
15
15
  if production
16
- @endpoint = 'https://sandbox.emites.com.br'
17
- else
18
16
  @endpoint = 'https://app.emites.com.br'
17
+ else
18
+ @endpoint = 'https://sandbox.emites.com.br'
19
19
  end
20
20
  base_uri @endpoint
21
21
  @token = token
22
22
  end
23
23
 
24
-
25
- def self.emitter_id(cnpj)
26
- cnpj = clean_cnpj(cnpj)
27
- emitter = lget "/api/v1/emitters?cnpj=#{cnpj}"
28
- # assumindo que volta apenas um registro na collection
29
- emitter["collection"][0]['id']
30
- end
31
-
32
- def self.emitter(cnpj)
33
- id = emitter_id(cnpj)
34
- lget "/api/v1/emitters/#{id}"
35
- end
36
-
37
- def self.taker_id(cnpj)
38
- cnpj = clean_cnpj(cnpj)
39
- emitter = lget "/api/v1/takers?cnpj=#{cnpj}"
40
- id = nil
41
- # assumindo que volta apenas um registro na collection
42
- if emitter and emitter["collection"] and emitter["collection"][0]
43
- id = emitter["collection"][0]['id']
44
- else
45
- puts "Não encontramos o tomador #{cnpj}"
46
- end
47
- id
48
- end
49
-
50
- def self.taker(cnpj)
51
- id = taker_id(cnpj)
52
- lget "/api/v1/emitters/#{id}"
53
- end
54
-
55
- def self.cria_lote(cnpj, nome)
56
- emitente_id = Emites.emitter_id(cnpj)
57
- lote = {
58
- emitter_id: emitente_id,
59
- name: nome
60
- }
61
- response = lpost '/api/v1/batches', lote
62
- response
63
- end
64
-
65
- # Crio um tomador para uma conta
66
- def self.cria_tomador_para_conta(conta_id)
67
- conta = Conta.find(conta_id)
68
- dados = conta.dados_cobranca if conta
69
- raise 'Id de conta invalido' unless dados
70
-
71
- # se não tiver cnpj não crio.
72
- return nil unless dados.cnpj.present?
73
-
74
-
75
- email = dados.email.split(';').first if dados.email
76
- email = 'financeiro@taxweb.com.br' unless email
77
-
78
- taker = {
79
- cnpj: dados.cnpj,
80
- social_reason: dados.razao_social,
81
- city_inscription: dados.im,
82
- # state_inscription: dados.ie,
83
- address: {
84
- street: "#{dados.tipo_lgr} #{dados.lgr}",
85
- number: (dados.nro || 'S/N'),
86
- complement: dados.cpl,
87
- neighborhood: dados.bairro,
88
- state: dados.uf,
89
- zip_code: dados.cep,
90
- city: dados.mun,
91
- city_code: '3550308'
92
- },
93
- contact: {
94
- email: email,
95
- phone: '11 3018-3818'
96
- }
97
- }
98
- response = lpost '/api/v1/takers', taker
99
- response
100
- end
101
-
102
-
103
-
104
- # criar modelo de servico
105
- def self.cria_servico_custodia
106
- servico = {
107
- emitter_id: Emites.emitter_id('11.008.913/0001-70'),
108
- name: Emites.servico_ccde,
109
- description: 'Custódid de XMLs',
110
- deduction_percentage: 0.00,
111
- service_item_code: '0105',
112
- city_code: '3550308',
113
- city_tax_code: '010501'
114
- }
115
- response = lpost '/api/v1/service-values', servico
116
- response
117
- end
118
-
119
- # pegar id de modelo de servico pelo nome
120
- def self.servico_custodia_id
121
- servico_id = nil
122
- response = lget '/api/v1/service-values'
123
- servicos = response['collection']
124
- servicos.each do |servico|
125
- if servico['name'] == Emites.servico_ccde
126
- servico_id = servico['id']
127
- break
128
- end
129
- end
130
- servico_id
131
- end
132
-
133
- # Emito a nota pelos CNPJs e pelo Valor
134
- def self.emite_nfs_custodia_por_cnpj(cnpj_prestador,cnpj_tomador, valor, lote = nil)
135
- emitente_id = emitter_id(cnpj_prestador)
136
- tomador_id = taker_id(cnpj_tomador)
137
- if emitente_id and tomador_id
138
- emite_nfs_custodia emitente_id, tomador_id, valor, lote
139
- end
140
- end
141
-
142
- # emitir nota com o modelo de serviço criado e alterando o valor
143
- def self.emite_nfs_custodia(emitente_id, tomador_id, valor, lote = nil)
144
- servico_id = servico_custodia_id
145
- unless servico_id
146
- cria_servico_custodia
147
- servico_id = servico_custodia_id
148
- end
149
- aliq = 5.0
150
-
151
- nfs = {
152
- emitter_id: emitente_id,
153
- taker_id: tomador_id,
154
- serie: 1,
155
- rps_type: 1,
156
- emission_date: format_time(Time.now),
157
- operation_nature: 1,
158
- competence: format_time(Time.now),
159
- service_values: {
160
- id: servico_id,
161
- service_amount: valor,
162
- calculation_base: valor,
163
- nfse_liquid_amount: valor,
164
- deduction_amount: 0,
165
- iss_percentage: aliq,
166
- iss_amount: (valor * aliq / 100).round(2),
167
- pis_amount: 0,
168
- cofins_amount: 0,
169
- inss_amount: 0,
170
- ir_amount: 0,
171
- csll_amount: 0,
172
- discount_conditioning_amount: 0,
173
- }
174
- }
175
-
176
- nfs['batch_name'] = lote if lote
177
-
178
- response = lpost '/api/v1/nfse', nfs
179
- puts response.inspect unless response['number'].present?
180
- response
181
- end
182
-
183
-
184
-
185
-
186
- def self.emite_nfs_ccde(emitente_id, tomador_id, descricao, valor, cod_servico, aliq, lote=nil)
187
- # Data e hora de emissao vem do prefeitura, não sei se devemos informar
188
-
189
- nfs = {
190
- emitter_id: emitente_id,
191
- taker_id: tomador_id,
192
- rps_type: 1,
193
- emission_date: format_time(Time.now),
194
- operation_nature: 1,
195
- competence: format_time(Time.now),
196
- service_values: {
197
- description: descricao,
198
- service_amount: valor,
199
- calculation_base: valor,
200
- nfse_liquid_amount: valor,
201
- deduction_amount: 0,
202
- iss_percentage: aliq,
203
- iss_amount: (valor * aliq / 100).round(2),
204
- pis_amount: 0,
205
- cofins_amount: 0,
206
- inss_amount: 0,
207
- ir_amount: 0,
208
- csll_amount: 0,
209
- discount_conditioning_amount: 0,
210
- service_item_code: cod_servico,
211
- city_tax_code: cod_servico,
212
- city_code: '3550308',
213
- }
214
- }
215
-
216
- nfs['batch_name'] = lote if lote
217
-
218
- response = lpost '/api/v1/nfse', nfs
219
- response
220
-
221
- end
222
-
223
24
  def self.clean_cnpj(cnpj)
224
25
  cnpj.gsub('.','').gsub('/','').gsub('-','').gsub(' ','')
225
26
  end
@@ -243,10 +44,18 @@ module Emites
243
44
  response
244
45
  end
245
46
 
47
+ def self.lput(url,post_data)
48
+ options = {
49
+ :basic_auth => {:username => @token, :password => 'x'},
50
+ :body => post_data.to_json,
51
+ :headers => { 'Content-Type' => 'application/json' }
52
+ }
53
+ response = put url, options
54
+ response
55
+ end
56
+
246
57
  def self.format_time(dt)
247
58
  dt.strftime('%FT%H:%MZ') rescue nil
248
59
  end
249
60
 
250
-
251
-
252
61
  end
@@ -0,0 +1,20 @@
1
+ module Emites
2
+
3
+ def self.cria_lote(cnpj, nome)
4
+ emitente_id = Emites.emitter_id(cnpj)
5
+ lote = {
6
+ emitter_id: emitente_id,
7
+ name: nome
8
+ }
9
+ response = lpost '/api/v1/batches', lote
10
+ response
11
+ end
12
+
13
+ def self.emite_nfs(nfs, lote = nil)
14
+ nfs['batch_name'] = lote if lote
15
+ response = lpost '/api/v1/nfse', nfs
16
+ puts response.inspect unless response['number'].present?
17
+ response
18
+ end
19
+
20
+ end
@@ -0,0 +1,20 @@
1
+ module Emites
2
+
3
+ def self.emitente_id(cnpj)
4
+ cnpj = clean_cnpj(cnpj)
5
+ emitter = lget "/api/v1/emitters?cnpj=#{cnpj}"
6
+ # assumindo que volta apenas um registro na collection
7
+ emitter["collection"][0]['id']
8
+ end
9
+
10
+ def self.emitente(cnpj)
11
+ id = emitente_id(cnpj)
12
+ response = lget "/api/v1/emitters/#{id}"
13
+ response.parsed_response
14
+ end
15
+
16
+ def self.emitentes
17
+ lget '/api/v1/emitters'
18
+ end
19
+
20
+ end
@@ -0,0 +1,24 @@
1
+ module Emites
2
+
3
+ # pego o id do servico pelo seu nome
4
+ def self.servico_id(nome)
5
+ servico_id = nil
6
+ response = lget '/api/v1/service-values'
7
+ servicos = response['collection']
8
+ servicos.each do |servico|
9
+ if servico['name'] == nome
10
+ servico_id = servico['id']
11
+ break
12
+ end
13
+ end
14
+ servico_id
15
+ end
16
+
17
+ # criar modelo de servico
18
+ def self.cria_servico(servico)
19
+ response = lpost '/api/v1/service-values', servico
20
+ response
21
+ end
22
+
23
+
24
+ end
@@ -0,0 +1,21 @@
1
+ module Emites
2
+
3
+ def self.tomador_id(cnpj)
4
+ cnpj = clean_cnpj(cnpj)
5
+ emitter = lget "/api/v1/takers?cnpj=#{cnpj}"
6
+ id = nil
7
+ # assumindo que volta apenas um registro na collection
8
+ if emitter and emitter["collection"] and emitter["collection"][0]
9
+ id = emitter["collection"][0]['id']
10
+ else
11
+ puts "Não encontramos o tomador #{cnpj}"
12
+ end
13
+ id
14
+ end
15
+
16
+ def self.tomador(cnpj)
17
+ id = taker_id(cnpj)
18
+ lget "/api/v1/emitters/#{id}"
19
+ end
20
+
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Emites
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -0,0 +1,19 @@
1
+ describe 'Manipulando Emitentes', type: :feature do
2
+
3
+ require 'emites'
4
+
5
+ it 'deve poder pegar o id de um emitente pelo seu nome' do
6
+ Emites.setup('3FEC36E110BA7A7F8DC5791F44D06992', false)
7
+ cnpj = '11008913000170'
8
+ id = Emites.emitente_id(cnpj)
9
+ expect(id).to_not be_nil
10
+ end
11
+
12
+ it 'deve poder ler os dados um emitente pelo seu nome' do
13
+ Emites.setup('3FEC36E110BA7A7F8DC5791F44D06992', false)
14
+ cnpj = '11008913000170'
15
+ entidade = Emites.emitente(cnpj)
16
+ expect(entidade['social_reason']).to_not be_nil
17
+ end
18
+
19
+ end
@@ -0,0 +1,89 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # The settings below are suggested to provide a good initial experience
42
+ # with RSpec, but feel free to customize to your heart's content.
43
+ =begin
44
+ # These two settings work together to allow you to limit a spec run
45
+ # to individual examples or groups you care about by tagging them with
46
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
47
+ # get run.
48
+ config.filter_run :focus
49
+ config.run_all_when_everything_filtered = true
50
+
51
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
52
+ # For more details, see:
53
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
54
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
55
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
56
+ config.disable_monkey_patching!
57
+
58
+ # This setting enables warnings. It's recommended, but in some cases may
59
+ # be too noisy due to issues in dependencies.
60
+ config.warnings = true
61
+
62
+ # Many RSpec users commonly either run the entire suite or an individual
63
+ # file, and it's useful to allow more verbose output when running an
64
+ # individual spec file.
65
+ if config.files_to_run.one?
66
+ # Use the documentation formatter for detailed output,
67
+ # unless a formatter has already been configured
68
+ # (e.g. via a command-line flag).
69
+ config.default_formatter = 'doc'
70
+ end
71
+
72
+ # Print the 10 slowest examples and example groups at the
73
+ # end of the spec run, to help surface which specs are running
74
+ # particularly slow.
75
+ config.profile_examples = 10
76
+
77
+ # Run specs in random order to surface order dependencies. If you find an
78
+ # order dependency and want to debug it, you can fix the order by providing
79
+ # the seed, which is printed after each run.
80
+ # --seed 1234
81
+ config.order = :random
82
+
83
+ # Seed global randomization in this process using the `--seed` CLI option.
84
+ # Setting this allows you to use `--seed` to deterministically reproduce
85
+ # test failures related to randomization by passing the same `--seed` value
86
+ # as the one that triggered the failure.
87
+ Kernel.srand config.seed
88
+ =end
89
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emites-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Lopes Neto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-28 00:00:00.000000000 Z
11
+ date: 2015-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,13 +60,22 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
+ - ".rspec"
64
+ - ".ruby-gemset"
65
+ - ".ruby-version"
63
66
  - Gemfile
64
67
  - LICENSE.txt
65
68
  - README.md
66
69
  - Rakefile
67
70
  - emites-rails.gemspec
68
71
  - lib/emites.rb
72
+ - lib/emites/emissao.rb
73
+ - lib/emites/emitente.rb
74
+ - lib/emites/servico.rb
75
+ - lib/emites/tomador.rb
69
76
  - lib/emites/version.rb
77
+ - spec/features/emitentes_spec.rb
78
+ - spec/spec_helper.rb
70
79
  homepage: ''
71
80
  licenses:
72
81
  - MIT
@@ -91,4 +100,6 @@ rubygems_version: 2.2.2
91
100
  signing_key:
92
101
  specification_version: 4
93
102
  summary: Emites API wrapper gem
94
- test_files: []
103
+ test_files:
104
+ - spec/features/emitentes_spec.rb
105
+ - spec/spec_helper.rb