Class: FE::ReceptionMessage

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/facturacr/reception_message.rb

Constant Summary collapse

MESSAGE_TYPES =
{
  "1" => "Aceptado",
  "2" => "Aceptacion Parcial",
  "3" => "Rechazado"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ReceptionMessage

Returns a new instance of ReceptionMessage.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/facturacr/reception_message.rb', line 23

def initialize(args = {})
  @key = args[:key]
  @date = args[:date]
  @issuer_id_number = args[:issuer_id_number]
  @receiver_id_type = args[:receiver_id_type]
  @receiver_id_number = args[:receiver_id_number]
  @message = args[:message].to_s
  @details = args[:details]
  @tax = args[:tax]
  @total = args[:total]
  @number = args[:number].to_i
  @security_code = args[:security_code]
  @document_situation = args[:document_situation]
  @namespaces = {
    "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", 
    "xmlns:xsd"=>"http://www.w3.org/2001/XMLSchema",
    "xmlns"=>"https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2/mensajeReceptor"
  }
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def date
  @date
end

#detailsObject

Returns the value of attribute details.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def details
  @details
end

#document_situationObject

Returns the value of attribute document_situation.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def document_situation
  @document_situation
end

#issuer_id_numberObject

Returns the value of attribute issuer_id_number.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def issuer_id_number
  @issuer_id_number
end

#keyObject

Returns the value of attribute key.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def key
  @key
end

#messageObject

Returns the value of attribute message.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def message
  @message
end

#numberObject

Returns the value of attribute number.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def number
  @number
end

#receiver_id_numberObject

Returns the value of attribute receiver_id_number.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def receiver_id_number
  @receiver_id_number
end

#receiver_id_typeObject

Returns the value of attribute receiver_id_type.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def receiver_id_type
  @receiver_id_type
end

#security_codeObject

Returns the value of attribute security_code.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def security_code
  @security_code
end

#taxObject

Returns the value of attribute tax.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def tax
  @tax
end

#totalObject

Returns the value of attribute total.



12
13
14
# File 'lib/facturacr/reception_message.rb', line 12

def total
  @total
end

Instance Method Details

#api_payloadObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/facturacr/reception_message.rb', line 105

def api_payload
  payload = {}
  payload[:clave] = key
  payload[:fecha] = @date.xmlschema
  payload[:emisor] = {
    tipoIdentificacion: @receiver_id_type,
    numeroIdentificacion: @receiver_id_number
  }
  payload
end

#build_xmlObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/facturacr/reception_message.rb', line 82

def build_xml
  raise "Documento inválido: #{errors.messages}" unless valid?
  builder  = Nokogiri::XML::Builder.new
  
  builder.MensajeReceptor(@namespaces) do |xml|
    xml.Clave key
    xml.NumeroCedulaEmisor @issuer_id_number
    xml.FechaEmisionDoc @date.xmlschema
    xml.Mensaje @message
    xml.DetalleMensaje @details if @details
    xml.MontoTotalImpuesto @tax if @tax
    xml.TotalFactura @total
    xml.NumeroCedulaReceptor @receiver_id_number
    xml.NumConsecutivoReceptor sequence
  end
  
  builder
end

#generateObject



101
102
103
# File 'lib/facturacr/reception_message.rb', line 101

def generate
  build_xml.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML)
end

#headquartersObject



60
61
62
# File 'lib/facturacr/reception_message.rb', line 60

def headquarters
  @headquarters ||= "001"
end

#sequenceObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/facturacr/reception_message.rb', line 68

def sequence
  if @message.eql?("1")
    @document_type = "05"
  elsif @message.eql?("2")
    @document_type = "06"
  elsif @message.eql?("3")
    @document_type = "07"
  end
  cons = ("%010d" % @number)
  "#{headquarters}#{terminal}#{@document_type}#{cons}"
end

#terminalObject



64
65
66
# File 'lib/facturacr/reception_message.rb', line 64

def terminal
  @terminal ||= "00001"
end