Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented method to query for existing bills.

  • Loading branch information...
commit 85b496acb59a657fab9c33f2280f67843ff07e57 1 parent d7aadbe
@miloops authored
View
1  Gemfile
@@ -3,6 +3,7 @@ source "http://rubygems.org"
platforms :ruby_19 do
gem "httpi", "0.7.9"
end
+gem "curb"
gem "savon"
gem "active_support", ">= 3.0.0"
gem "tzinfo"
View
2  Gemfile.lock
@@ -8,6 +8,7 @@ GEM
builder (3.0.0)
columnize (0.3.2)
crack (0.1.8)
+ curb (0.7.15)
diff-lcs (1.1.2)
git (1.2.5)
gyoku (0.3.1)
@@ -60,6 +61,7 @@ PLATFORMS
DEPENDENCIES
active_support (>= 3.0.0)
bundler (>= 1.0.0)
+ curb
httpi (= 0.7.9)
jeweler (~> 1.6.3)
rcov
View
1  lib/bravo.rb
@@ -1,6 +1,7 @@
require "bundler/setup"
require "bravo/version"
require "bravo/constants"
+require "curb"
require "savon"
require "bravo/core_ext/float"
require "bravo/core_ext/hash"
View
117 lib/bravo/bill.rb
@@ -2,12 +2,12 @@ module Bravo
class Bill
attr_reader :client, :base_imp, :total
attr_accessor :net, :doc_num, :iva_cond, :documento, :concepto, :moneda,
- :due_date, :aliciva_id, :fch_serv_desde, :fch_serv_hasta,
- :body, :response
+ :due_date, :aliciva_id, :fch_serv_desde, :fch_serv_hasta,
+ :body, :response
def initialize(attrs = {})
Bravo::AuthData.fetch
- @client = Savon::Client.new do
+ @client = Savon::Client.new do
wsdl.document = Bravo.service_url
http.auth.ssl.cert_key_file = Bravo.pkey
http.auth.ssl.cert_file = Bravo.cert
@@ -16,7 +16,6 @@ def initialize(attrs = {})
http.open_timeout = 90
http.headers = { "Accept-Encoding" => "gzip, deflate", "Connection" => "Keep-Alive" }
end
-
@body = {"Auth" => Bravo.auth_hash}
@net = attrs[:net] || 0
self.documento = attrs[:documento] || Bravo.default_documento
@@ -63,44 +62,44 @@ def setup_bill
today = Time.new.in_time_zone('Buenos Aires').strftime('%Y%m%d')
fecaereq = {"FeCAEReq" => {
- "FeCabReq" => Bravo::Bill.header(cbte_type),
- "FeDetReq" => {
- "FECAEDetRequest" => {
- "Concepto" => Bravo::CONCEPTOS[concepto],
- "DocTipo" => Bravo::DOCUMENTOS[documento],
- "CbteFch" => today,
- "ImpTotConc" => 0.00,
- "MonId" => Bravo::MONEDAS[moneda][:codigo],
- "MonCotiz" => exchange_rate,
- "ImpOpEx" => 0.00,
- "ImpTrib" => 0.00,
- "Iva" => {
- "AlicIva" => {
- "Id" => "5",
- "BaseImp" => net,
- "Importe" => iva_sum}}}}}}
-
- detail = fecaereq["FeCAEReq"]["FeDetReq"]["FECAEDetRequest"]
-
- detail["DocNro"] = doc_num
- detail["ImpNeto"] = net.to_f
- detail["ImpIVA"] = iva_sum
- detail["ImpTotal"] = total
-
- if bill_number = next_bill_number
- detail["CbteDesde"] = detail["CbteHasta"] = bill_number
- else
- return false
- end
+ "FeCabReq" => Bravo::Bill.header(cbte_type),
+ "FeDetReq" => {
+ "FECAEDetRequest" => {
+ "Concepto" => Bravo::CONCEPTOS[concepto],
+ "DocTipo" => Bravo::DOCUMENTOS[documento],
+ "CbteFch" => today,
+ "ImpTotConc" => 0.00,
+ "MonId" => Bravo::MONEDAS[moneda][:codigo],
+ "MonCotiz" => exchange_rate,
+ "ImpOpEx" => 0.00,
+ "ImpTrib" => 0.00,
+ "Iva" => {
+ "AlicIva" => {
+ "Id" => "5",
+ "BaseImp" => net,
+ "Importe" => iva_sum}}}}}}
+
+ detail = fecaereq["FeCAEReq"]["FeDetReq"]["FECAEDetRequest"]
+
+ detail["DocNro"] = doc_num
+ detail["ImpNeto"] = net.to_f
+ detail["ImpIVA"] = iva_sum
+ detail["ImpTotal"] = total
+
+ if bill_number = next_bill_number
+ detail["CbteDesde"] = detail["CbteHasta"] = bill_number
+ else
+ return false
+ end
- unless concepto == 0
- detail.merge!({"FchServDesde" => fch_serv_desde || today,
- "FchServHasta" => fch_serv_hasta || today,
- "FchVtoPago" => due_date || today})
- end
+ unless concepto == 0
+ detail.merge!({"FchServDesde" => fch_serv_desde || today,
+ "FchServHasta" => fch_serv_hasta || today,
+ "FchVtoPago" => due_date || today})
+ end
- body.merge!(fecaereq)
- true
+ body.merge!(fecaereq)
+ true
end
def next_bill_number
@@ -120,6 +119,36 @@ def authorized?
!response.nil? && response.header_result == "A" && response.detail_result == "A"
end
+ def query_bill(iva_cond, cbte_number)
+ cbte_type = Bravo::BILL_TYPE[Bravo.own_iva_cond][iva_cond] ||
+ raise(NullOrInvalidAttribute.new, "Please choose a valid document type.")
+
+ response = client.request :fe_comp_consultar do
+ soap.namespaces["xmlns"] = "http://ar.gov.afip.dif.FEV1/"
+ soap.body = {"Auth" => Bravo.auth_hash,
+ "FeCompConsReq" => { "PtoVta" => Bravo.sale_point, "CbteTipo" => cbte_type, "CbteNro" => cbte_number}}
+ end
+
+ result = response.to_hash[:fe_comp_consultar_response][:fe_comp_consultar_result][:result_get]
+
+ response_hash = {:header_result => result[:resultado],
+ :authorized_on => result[:fch_proceso],
+ :detail_result => result[:resultado],
+ :cae_due_date => result[:fch_vto],
+ :cae => result[:cod_autorizacion],
+ :iva_id => result[:iva][:alic_iva][:id],
+ :iva_importe => result[:iva][:alic_iva][:importe],
+ :moneda => result[:mon_id],
+ :cotizacion => result[:mon_cotiz],
+ :iva_base_imp => result[:iva][:alic_iva][:base_imp],
+ :doc_num => result[:doc_nro]
+ }
+
+ keys, values = response_hash.to_a.transpose
+
+ self.response = (defined?(Struct::Response) ? Struct::Response : Struct.new("Response", *keys)).new(*values)
+ end
+
private
class << self
@@ -142,11 +171,13 @@ def setup_response(response)
rescue NoMethodError
if defined?(RAILS_DEFAULT_LOGGER) && logger = RAILS_DEFAULT_LOGGER
logger.error "[BRAVO] NoMethodError: Response #{response}"
+ else
+ puts "[BRAVO] NoMethodError: Response #{response}"
end
- return false
+ return false
end
- iva = request_detail.delete(:iva)["AlicIva"].underscore_keys.symbolize_keys
+ iva = request_detail.delete(:iva)["AlicIva"].underscore_keys.symbolize_keys
request_detail.merge!(iva)
@@ -161,7 +192,7 @@ def setup_response(response)
:cotizacion => request_detail.delete(:mon_cotiz),
:iva_base_imp => request_detail.delete(:base_imp),
:doc_num => request_detail.delete(:doc_nro)
- }.merge!(request_header).merge!(request_detail)
+ }.merge!(request_header).merge!(request_detail)
keys, values = response_hash.to_a.transpose
self.response = (defined?(Struct::Response) ? Struct::Response : Struct.new("Response", *keys)).new(*values)
View
9 spec/bravo/bill_spec.rb
@@ -138,5 +138,14 @@
response.cae.length.should == 14
response.cbte_tipo.should == "08"
end
+
+ it "should query for a bill given a type and a number" do
+ @bill.query_bill(:consumidor_final, 1)
+
+ @bill.authorized?.should == true
+
+ response = @bill.response
+ response.cae.length.should == 14
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.