emites-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a213b50870e6b05371f20d7e8844f5def43dfeba
4
+ data.tar.gz: a69c85217ca4d3043bc59ae65baf35d19c6b34bf
5
+ SHA512:
6
+ metadata.gz: a38ceb6a2e1b4ff078c7cf191d61926fff3f680eee945b7cde10d2bb87948a0f18a04b9c4211552b513f6f95e005aebbf681220619dcdb8d1853749ae99ff218
7
+ data.tar.gz: 6478c4e5167752bb5ea0ce3343a4bd2bcf449b608526af5f7b91a835e9baa37f37a828986aeadfd8c61fa160a150f107b39f4e9dc6b016d8068c0476f33445d0
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in emites-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 José Lopes Neto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Emites::Rails
2
+
3
+ API integration with EMITES (www.emites.com.br)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'emites-rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install emites-rails
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/emites-rails/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'emites/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'emites-rails'
8
+ spec.version = Emites::VERSION
9
+ spec.authors = ['José Lopes Neto']
10
+ spec.email = ['jose.neto@taxweb.com.br']
11
+ spec.summary = %q{Emites API wrapper gem}
12
+ spec.description = %q{Encapsulates emites api in a rails gem www.emites.com.br}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_dependency 'httparty','~> 0.13.1'
24
+ end
@@ -0,0 +1,3 @@
1
+ module Emites
2
+ VERSION = '0.0.1'
3
+ end
data/lib/emites.rb ADDED
@@ -0,0 +1,252 @@
1
+ # require 'emites/version'
2
+ require 'httparty'
3
+
4
+ module Emites
5
+
6
+ include HTTParty
7
+
8
+ attr_accessor :token, :endpoint
9
+
10
+ def self.servico_ccde
11
+ 'Custódia de XMLs'
12
+ end
13
+
14
+ def self.setup(token,production=false)
15
+ if production
16
+ @endpoint = 'https://sandbox.emites.com.br'
17
+ else
18
+ @endpoint = 'https://app.emites.com.br'
19
+ end
20
+ base_uri @endpoint
21
+ @token = token
22
+ end
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
+ def self.clean_cnpj(cnpj)
224
+ cnpj.gsub('.','').gsub('/','').gsub('-','').gsub(' ','')
225
+ end
226
+
227
+ private
228
+
229
+ def self.lget(url)
230
+ options = {
231
+ :basic_auth => {:username => @token, :password => 'x'},
232
+ }
233
+ get url, options
234
+ end
235
+
236
+ def self.lpost(url,data)
237
+ options = {
238
+ :basic_auth => {:username => @token, :password => 'x'},
239
+ :body => data.to_json,
240
+ :headers => { 'Content-Type' => 'application/json' }
241
+ }
242
+ response = post url, options
243
+ response
244
+ end
245
+
246
+ def self.format_time(dt)
247
+ dt.strftime('%FT%H:%MZ') rescue nil
248
+ end
249
+
250
+
251
+
252
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: emites-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - José Lopes Neto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.13.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.13.1
55
+ description: Encapsulates emites api in a rails gem www.emites.com.br
56
+ email:
57
+ - jose.neto@taxweb.com.br
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - emites-rails.gemspec
68
+ - lib/emites.rb
69
+ - lib/emites/version.rb
70
+ homepage: ''
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.2.2
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Emites API wrapper gem
94
+ test_files: []