Class: FE::CLI::Generate

Inherits:
Thor
  • Object
show all
Defined in:
lib/facturacr/cli/generate.rb

Instance Method Summary collapse

Instance Method Details

#credit_noteObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/facturacr/cli/generate.rb', line 44

def credit_note
  data = YAML.load_file(options[:data_path]).with_indifferent_access
  builder = FE::Builder.new
  
  credit_note = builder.credit_note(data[:document])
  if options[:invoice_number].present?
    date = DateTime.parse(options[:invoice_date])
    credit_note.references = [FE::Document::Reference.new(document_type: "01", code: "01", reason: "Anula documento", number: options[:invoice_number], date: date)]
  end
  
  credit_note.date = Time.now
  credit_note.number = options[:number].to_i if options[:number].present?
  
  if options[:output_path]
    output_path = options[:output_path]
  else
    output_path = "tmp/#{credit_note.key}.xml"
  end
  
  write(credit_note, output_path)
  print_details(credit_note)
  sign(output_path, options) if options[:sign]
  send_document("#{output_path}.signed.xml") if options[:sign] && options[:send]
  
end

#debit_noteObject



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
# File 'lib/facturacr/cli/generate.rb', line 79

def debit_note
  data = YAML.load_file(options[:data_path]).with_indifferent_access
  builder = FE::Builder.new
  
  debit_note = builder.debit_note(data[:document])
  if options[:invoice_number].present?
    date = DateTime.parse(options[:invoice_date])
    debit_note.references = [FE::Document::Reference.new(document_type: "01", code: "01", reason: "Anula documento", number: options[:invoice_number], date: date)]
  end
  
  debit_note.date = Time.now
  debit_note.number = options[:number].to_i if options[:number].present?
  
  if options[:output_path]
    output_path = options[:output_path]
  else
    output_path = "tmp/#{debit_note.key}.xml"
  end
  
  write(debit_note, output_path)
  print_details(debit_note)
  sign(output_path, options) if options[:sign]
  send_document("#{output_path}.signed.xml") if options[:sign] && options[:send]
  
end

#invoiceObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/facturacr/cli/generate.rb', line 13

def invoice
  data = YAML.load_file(options[:data_path]).with_indifferent_access
  builder = FE::Builder.new
  
  invoice = builder.invoice(data[:document])
  invoice.date = Time.now
  invoice.number = options[:number].to_i if options[:number].present?
  
  if options[:output_path]
    output_path = options[:output_path]
  else
    output_path = "tmp/#{invoice.key}.xml"
  end
  
  write(invoice, output_path)
  print_details(invoice)
  sign(output_path, options) if options[:sign]
  send_document("#{output_path}.signed.xml") if options[:sign] && options[:send]
  
end

#reception_messageObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/facturacr/cli/generate.rb', line 142

def reception_message
  doc = XmlDocument.new(options[:xml_in]) 
  
  builder = FE::Builder.new

  # Roles of receiver and issuer are inverted because they are extracted from the document.
  # The ISSUER of the reception message is the RECEIVER of the document        
  message = builder.reception_message({
    key: doc.document.key, 
    date: doc.document.date, 
    number: options[:number],
    issuer_id_number: doc.document.receiver.identification_document.id_number,
    issuer_id_type: doc.document.receiver.identification_document.document_type,
    receiver_id_number: doc.document.issuer.identification_document.id_number,
    receiver_id_type: doc.document.issuer.identification_document.document_type,
    message: options[:action],
    tax: doc.document.summary.tax_total,
    total: doc.document.summary.net_total,
    document_situation: options[:document_situation]
  })
  
  message.security_code = (SecureRandom.random_number * (10**8)).round.to_s
  if options[:output_path]
    output_path = options[:output_path]
  else
    output_path = "tmp/msj-receptor-#{message.key}.xml"
  end
  write(message, output_path)
  puts "Mensaje Receptor"
  puts "Accion: #{FE::ReceptionMessage::MESSAGE_TYPES[message.message]}"
  puts "Factura: #{doc.document.key}"
  puts "Ced. Receptor: #{message.receiver_id_number}"
  puts "Ced. Emisor: #{message.issuer_id_number}"

  sign(output_path, options) if options[:sign]
  send_document("#{output_path}.signed.xml") if options[:sign] && options[:send]
  
end

#ticketObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/facturacr/cli/generate.rb', line 112

def ticket      
  data = YAML.load_file(options[:data_path]).with_indifferent_access
  builder = FE::Builder.new
  
  ticket = builder.ticket(data[:document])
  ticket.date = Time.now
  ticket.number = options[:number].to_i if options[:number].present?
  
  if options[:output_path]
    output_path = options[:output_path]
  else
    output_path = "tmp/#{ticket.key}.xml"
  end
  
  write(ticket, output_path)
  print_details(ticket)
  sign(output_path, options) if options[:sign]
  send_document("#{output_path}.signed.xml") if options[:sign] && options[:send]
  
end