Class: Snoopy::AuthorizeAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/snoopy_afip/authorize_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ AuthorizeAdapter

Returns a new instance of AuthorizeAdapter.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/snoopy_afip/authorize_adapter.rb', line 6

def initialize(attrs)
  @bill              = attrs[:bill]
  @cuit              = attrs[:cuit]
  @sign              = attrs[:sign]
  @pkey              = attrs[:pkey]
  @cert              = attrs[:cert]
  @token             = attrs[:token]
  @errors            = {}
  @request           = nil
  @response          = nil
  @afip_errors       = {}
  @afip_events       = {}
  @afip_observations = {}
  @client            = Snoopy::Client.new(client_configuration)
end

Instance Attribute Details

#afip_errorsObject

Returns the value of attribute afip_errors.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def afip_errors
  @afip_errors
end

#afip_eventsObject

Returns the value of attribute afip_events.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def afip_events
  @afip_events
end

#afip_observationsObject

Returns the value of attribute afip_observations.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def afip_observations
  @afip_observations
end

#billObject

Returns the value of attribute bill.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def bill
  @bill
end

#certObject

Returns the value of attribute cert.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def cert
  @cert
end

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def client
  @client
end

#cuitObject

Returns the value of attribute cuit.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def cuit
  @cuit
end

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def errors
  @errors
end

#pkeyObject

Returns the value of attribute pkey.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def pkey
  @pkey
end

#requestObject

Returns the value of attribute request.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def request
  @request
end

#responseObject

Returns the value of attribute response.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def response
  @response
end

#signObject

Returns the value of attribute sign.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def sign
  @sign
end

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/snoopy_afip/authorize_adapter.rb', line 4

def token
  @token
end

Instance Method Details

#authObject



22
23
24
# File 'lib/snoopy_afip/authorize_adapter.rb', line 22

def auth
  { "Token" => token, "Sign" => sign, "Cuit" => cuit }
end

#authorize!Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/snoopy_afip/authorize_adapter.rb', line 36

def authorize!
  return false unless bill.valid?
  @afip_errors = {}

  build_body_request
  @response = client.call(:fecae_solicitar, :message => @request)
  parse_fecae_solicitar_response

  !@response.nil?
end

#build_body_comp_cons_requestObject



106
107
108
109
# File 'lib/snoopy_afip/authorize_adapter.rb', line 106

def build_body_comp_cons_request
  fecompconsreq = { "FeCompConsReq" => { "CbteTipo" => bill.cbte_type, "CbteNro" => bill.number.to_i, "PtoVta" => bill.sale_point.to_i } }
  @comp_cons_request = { "Auth" => auth }.merge!(fecompconsreq)
end

#build_body_requestObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/snoopy_afip/authorize_adapter.rb', line 61

def build_body_request
  # today = Time.new.in_time_zone('Buenos Aires').strftime('%Y%m%d')
  today = Date.today.strftime('%Y%m%d')
  fecaereq = {"FeCAEReq" => { "FeCabReq" => { "CantReg" => "1", "CbteTipo" => bill.cbte_type, "PtoVta" => bill.sale_point },
                              "FeDetReq" => { "FECAEDetRequest" => { "Concepto"   => Snoopy::CONCEPTS[bill.concept],
                                                                     "DocTipo"    => Snoopy::DOCUMENTS[bill.document_type],
                                                                     "CbteFch"    => today,
                                                                     "ImpTotConc" => 0.00,
                                                                     "MonId"      => Snoopy::CURRENCY[bill.currency][:code],
                                                                     "MonCotiz"   => bill.exchange_rate,
                                                                     "ImpOpEx"    => 0.00,
                                                                     "ImpTrib"    => 0.00 }}}}

  unless bill.issuer_iva_cond.to_sym == Snoopy::RESPONSABLE_MONOTRIBUTO
    _alicivas = bill.alicivas.collect do |aliciva|
      { 'Id' => Snoopy::ALIC_IVA[aliciva[:id].to_f], 'Importe' => aliciva[:amount], 'BaseImp' => aliciva[:taxeable_base] }
    end
    fecaereq["FeCAEReq"]["FeDetReq"]["FECAEDetRequest"]["Iva"] = { "AlicIva" => _alicivas }
  end

  detail = fecaereq["FeCAEReq"]["FeDetReq"]["FECAEDetRequest"]

  detail["DocNro"]    = bill.document_num
  detail["ImpNeto"]   = bill.total_net.to_f
  detail["ImpIVA"]    = bill.iva_sum
  detail["ImpTotal"]  = bill.total
  detail["CbteDesde"] = detail["CbteHasta"] = bill.number.to_s

  unless bill.concept == "Productos"
    detail.merge!({ "FchServDesde" => bill.service_date_from || today,
                    "FchServHasta" => bill.service_date_to   || today,
                    "FchVtoPago"   => bill.due_date          || today})
  end

  if bill.receiver_iva_cond.to_s.include?("nota_credito")
    detail.merge!({"CbtesAsoc" => {"CbteAsoc" => {"Nro"    => bill.cbte_asoc_num,
                                                  "PtoVta" => bill.cbte_asoc_to_sale_point,
                                                  "Tipo"   => bill.cbte_asoc_type }}})
  end

  @request = { "Auth" => auth }.merge!(fecaereq)
rescue => e
  raise Snoopy::Exception::AuthorizeAdapter::BuildBodyRequest.new(e.message, e.backtrace)
end

#client_configurationObject



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/snoopy_afip/authorize_adapter.rb', line 184

def client_configuration
  { :wsdl              => Snoopy.service_url,
    :headers           => { "Accept-Encoding" => "gzip, deflate", "Connection" => "Keep-Alive" },
    :namespaces        => {"xmlns" => "http://ar.gov.afip.dif.FEV1/"},
    :ssl_version       => ::Snoopy::SNOOPY_SSL_VERSION,
    :read_timeout      => Snoopy.read_timeout,
    :open_timeout      => Snoopy.open_timeout,
    :ssl_cert_file     => cert,
    :ssl_cert_key_file => pkey,
    :pretty_print_xml  => true }
end

#invoice_informed?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
# File 'lib/snoopy_afip/authorize_adapter.rb', line 26

def invoice_informed?
  return false unless bill.valid?

  build_body_comp_cons_request
  @response = client.call(:fe_comp_consultar, :message => @comp_cons_request)
  parse_fe_comp_consultar_response

  afip_errors.keys.empty?
end

#parse_errors(fecae_errors) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/snoopy_afip/authorize_adapter.rb', line 127

def parse_errors(fecae_errors)
  fecae_errors.each_value do |errores|
    [errores].flatten.each { |error| afip_errors[error[:code]] = error[:msg] }
  end
rescue => e
  errors << Snoopy::Exception::AuthorizeAdapter::ErrorParser.new(e.message, e.backtrace)
end

#parse_events(fecae_events) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/snoopy_afip/authorize_adapter.rb', line 119

def parse_events(fecae_events)
  fecae_events.each_value do |events|
    [events].flatten.each { |event| afip_events[event[:code]] = event[:msg] }
  end
rescue => e
  errors << Snoopy::Exception::AuthorizeAdapter::EventsParser.new(e.message, e.backtrace)
end

#parse_fe_comp_consultar_responseObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/snoopy_afip/authorize_adapter.rb', line 160

def parse_fe_comp_consultar_response
  fe_comp_consultar_result = response[:fe_comp_consultar_response][:fe_comp_consultar_result]
  result_get               = fe_comp_consultar_result[:result_get]

  unless result_get.nil?
    bill.result              = result_get[:resultado]
    bill.number              = result_get[:cbte_desde]
    bill.cae                 = result_get[:cod_autorizacion]
    bill.due_date_cae        = result_get[:fch_vto]
    # bill.imp_iva           = result_get[:imp_iva]
    # bill.document_num      = result_get[:doc_numero]
    # bill.process_date      = result_get[:fch_proceso]
    # bill.voucher_date      = result_get[:cbte_fch]
    # bill.service_date_to   = result_get[:fch_serv_hasta]
    # bill.service_date_from = result_get[:fch_serv_desde]
    parse_events(result_get[:observaciones]) if result_get.has_key? :observaciones
  end

  self.parse_errors(fe_comp_consultar_result[:errors]) if fe_comp_consultar_result and fe_comp_consultar_result.has_key? :errors
  self.parse_events(fe_comp_consultar_result[:events]) if fe_comp_consultar_result and fe_comp_consultar_result.has_key? :events
rescue => e
  @errors << Snoopy::Exception::FecompConsultResponseParser.new(e.message, e.backtrace)
end

#parse_fecae_solicitar_responseObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/snoopy_afip/authorize_adapter.rb', line 135

def parse_fecae_solicitar_response
  begin
    fecae_result   = response[:fecae_solicitar_response][:fecae_solicitar_result]
    fecae_response = fecae_result[:fe_det_resp][:fecae_det_response]

    bill.number       = fecae_response[:cbte_desde]
    bill.cae          = fecae_response[:cae]
    bill.due_date_cae = fecae_response[:cae_fch_vto]
    bill.result       = fecae_response[:resultado]
  rescue => e
    raise Snoopy::Exception::AuthorizeAdapter::FecaeSolicitarResultParser.new(e.message, e.backtrace)
  end

  begin
    bill.voucher_date = fecae_response[:cbte_fch]
    bill.process_date = fecae_result[:fe_cab_resp][:fch_proceso]

    parse_observations(fecae_response.delete(:observaciones)) if fecae_response.has_key? :observaciones
    parse_errors(fecae_result[:errors])                       if fecae_result.has_key? :errors
    parse_events(fecae_result[:events])                       if fecae_result.has_key? :events
  rescue => e
    @errors << Snoopy::Exception::AuthorizeAdapter::FecaeResponseParser.new(e.message, e.backtrace)
  end
end

#parse_observations(fecae_observations) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/snoopy_afip/authorize_adapter.rb', line 111

def parse_observations(fecae_observations)
  fecae_observations.each_value do |obs|
    [obs].flatten.each { |ob| afip_observations[ob[:code]] = ob[:msg] }
  end
rescue => e
  errors << Snoopy::Exception::AuthorizeAdapter::ObservationParser.new(e.message, e.backtrace)
end

#set_bill_number!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/snoopy_afip/authorize_adapter.rb', line 47

def set_bill_number!
  message = { "Auth" => auth, "PtoVta" => bill.sale_point, "CbteTipo" => bill.cbte_type }
  resp = client.call( :fe_comp_ultimo_autorizado, :message =>  message )

  begin
    resp_errors = resp[:fe_comp_ultimo_autorizado_response][:fe_comp_ultimo_autorizado_result][:errors]
    resp_errors.each_value { |value| errors[value[:code]] = value[:msg] } unless resp_errors.nil?
    bill.number = (resp[:fe_comp_ultimo_autorizado_response][:fe_comp_ultimo_autorizado_result][:cbte_nro].to_i + 1).to_s if errors.empty?
    bill.number
  rescue => e
    raise Snoopy::Exception::AuthorizeAdapter::SetBillNumberParser.new(e.message, e.backtrace)
  end
end