Class: Bravo::Bill

Inherits:
Object
  • Object
show all
Defined in:
lib/bravo/bill.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Bill

Returns a new instance of Bill.



8
9
10
11
12
13
14
15
16
17
# File 'lib/bravo/bill.rb', line 8

def initialize(attrs = {})
  Bravo::AuthData.fetch
  @client         = Savon::Client.new(Bravo.service_url)
  @body           = {"Auth" => Bravo.auth_hash}
  @net            = attrs[:net] || 0
  self.documento  = attrs[:documento] || Bravo.default_documento
  self.moneda     = attrs[:moneda]    || Bravo.default_moneda
  self.iva_cond   = attrs[:iva_cond]
  self.concepto   = attrs[:concepto]  || Bravo.default_concepto
end

Instance Attribute Details

#aliciva_idObject

Returns the value of attribute aliciva_id.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def aliciva_id
  @aliciva_id
end

#base_impObject (readonly)

Returns the value of attribute base_imp.



3
4
5
# File 'lib/bravo/bill.rb', line 3

def base_imp
  @base_imp
end

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def body
  @body
end

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/bravo/bill.rb', line 3

def client
  @client
end

#conceptoObject

Returns the value of attribute concepto.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def concepto
  @concepto
end

#doc_numObject

Returns the value of attribute doc_num.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def doc_num
  @doc_num
end

#documentoObject

Returns the value of attribute documento.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def documento
  @documento
end

#due_dateObject

Returns the value of attribute due_date.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def due_date
  @due_date
end

#fch_serv_desdeObject

Returns the value of attribute fch_serv_desde.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def fch_serv_desde
  @fch_serv_desde
end

#fch_serv_hastaObject

Returns the value of attribute fch_serv_hasta.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def fch_serv_hasta
  @fch_serv_hasta
end

#iva_condObject

Returns the value of attribute iva_cond.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def iva_cond
  @iva_cond
end

#monedaObject

Returns the value of attribute moneda.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def moneda
  @moneda
end

#netObject

Returns the value of attribute net.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def net
  @net
end

#responseObject

Returns the value of attribute response.



4
5
6
# File 'lib/bravo/bill.rb', line 4

def response
  @response
end

#totalObject (readonly)

Returns the value of attribute total.



3
4
5
# File 'lib/bravo/bill.rb', line 3

def total
  @total
end

Class Method Details

.header(cbte_type) ⇒ Object

todo sacado de la factura



107
108
109
# File 'lib/bravo/bill.rb', line 107

def header(cbte_type)#todo sacado de la factura
  {"CantReg" => "1", "CbteTipo" => cbte_type, "PtoVta" => Bravo.sale_point}
end

Instance Method Details

#authorizeObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/bravo/bill.rb', line 42

def authorize
  setup_bill
  response = client.fecae_solicitar do |soap|
    soap.namespaces["xmlns"] = "http://ar.gov.afip.dif.FEV1/"
    soap.body = body
  end

  setup_response(response.to_hash)
  self.authorized?
end

#authorized?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/bravo/bill.rb', line 100

def authorized?
  !response.nil? && response.header_result == "A" && response.detail_result == "A"
end

#cbte_typeObject



19
20
21
22
# File 'lib/bravo/bill.rb', line 19

def cbte_type
  Bravo::BILL_TYPE[Bravo.own_iva_cond][iva_cond] ||
    raise(NullOrInvalidAttribute.new, "Please choose a valid document type.")
end

#exchange_rateObject



24
25
26
27
28
29
30
31
# File 'lib/bravo/bill.rb', line 24

def exchange_rate
  return 1 if moneda == :peso
  response = client.fe_param_get_cotizacion do |soap|
    soap.namespaces["xmlns"] = "http://ar.gov.afip.dif.FEV1/"
    soap.body = body.merge!({"MonId" => Bravo::MONEDAS[moneda][:codigo]})
  end
  response.to_hash[:fe_param_get_cotizacion_response][:fe_param_get_cotizacion_result][:result_get][:mon_cotiz].to_f
end

#iva_sumObject



37
38
39
40
# File 'lib/bravo/bill.rb', line 37

def iva_sum
  @iva_sum = net * Bravo::ALIC_IVA[aliciva_id][1]
  @iva_sum.round_up_with_precision(2)
end

#next_bill_numberObject



91
92
93
94
95
96
97
98
# File 'lib/bravo/bill.rb', line 91

def next_bill_number
  resp = client.fe_comp_ultimo_autorizado do |s|
    s.namespaces["xmlns"] = "http://ar.gov.afip.dif.FEV1/"
    s.body = {"Auth" => Bravo.auth_hash, "PtoVta" => Bravo.sale_point, "CbteTipo" => cbte_type}
  end

  resp.to_hash[:fe_comp_ultimo_autorizado_response][:fe_comp_ultimo_autorizado_result][:cbte_nro].to_i + 1
end

#setup_billObject



53
54
55
56
57
58
59
60
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
# File 'lib/bravo/bill.rb', line 53

def setup_bill
  today = Time.new.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
  detail["CbteDesde"] = detail["CbteHasta"] = next_bill_number

  unless concepto == 0
    detail.merge!({"FchServDesde" => fch_serv_desde || today,
                  "FchServHasta"  => fch_serv_hasta || today,
                  "FchVtoPago"    => due_date       || today})
  end

  body.merge!(fecaereq)
end