Class: ACube::Invoicer

Inherits:
Object
  • Object
show all
Defined in:
lib/acube/invoicer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#customerObject

Returns the value of attribute customer.



3
4
5
# File 'lib/acube/invoicer.rb', line 3

def customer
  @customer
end

#documentObject

Returns the value of attribute document.



4
5
6
# File 'lib/acube/invoicer.rb', line 4

def document
  @document
end

#headerObject

Returns the value of attribute header.



4
5
6
# File 'lib/acube/invoicer.rb', line 4

def header
  @header
end

#invoiceObject

Returns the value of attribute invoice.



3
4
5
# File 'lib/acube/invoicer.rb', line 3

def invoice
  @invoice
end

#supplierObject

Returns the value of attribute supplier.



3
4
5
# File 'lib/acube/invoicer.rb', line 3

def supplier
  @supplier
end

Class Method Details

.from(supplier:, customer:, invoice:, format: :FPR12) ⇒ Object



6
7
8
9
# File 'lib/acube/invoicer.rb', line 6

def self.from(supplier:, customer:, invoice:, format: :FPR12)
  raise "Format #{format} not supported" unless ACube::InvoiceRecord.formats.include?(format)
  new(supplier, customer, invoice, format)
end

.retry_invoice_sending(invoice_record_id) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/acube/invoicer.rb', line 59

def self.retry_invoice_sending(invoice_record_id)
  invoice_record = ACube::InvoiceRecord.find(invoice_record_id)
  raise "This Invoice was already sent to ACube" if invoice_record.status != "creation_error"

  begin
    uuid = ACube::Endpoint::Invoices.new.create(invoice_record.xml_body)
    invoice_record.update_column(:webhook_uuid, uuid)
  rescue => e
    invoice_record.update_column(:status, :creation_error)
    raise e
  end
end

.udate_invoice_attributes(invoice_id, json_body) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/acube/invoicer.rb', line 72

def self.udate_invoice_attributes(invoice_id, json_body)
  invoice_record = ACube::InvoiceRecord.find_by(webhook_uuid: invoice_id)
  invoice_record.update_column(:json_body, json_body)
  
  begin
    downloaded_pdf = ACube::Endpoint::Invoices.new.download(invoice_record.webhook_uuid)
    downloaded_pdf.rewind
    invoice_record.pdf.attach(io: downloaded_pdf, filename: "#{invoice_record.webhook_uuid}-invoice.pdf", content_type: 'application/pdf')
    invoice_record.update_column(:status, :downloaded)
  rescue => e
    invoice_record.update_column(:status, :download_error)
    raise e
  end
end

.update_invoice_status(webhook_body) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/acube/invoicer.rb', line 87

def self.update_invoice_status(webhook_body)
  notification = JSON.parse(webhook_body, symbolize_names: true)
  invoice_record = ACube::InvoiceRecord.find_by(webhook_uuid: notification[:notification][:invoice_uuid])

  status = case notification[:notification][:type]
  when "MC" then :not_received
  when "AT" then :not_received
  when "RC" then :delivered
  when "NS" then :rejected
  else :notification_error
  end

  invoice_record.update_column(:status, status)
end

Instance Method Details

#create_invoice(invoice_base_record, name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/acube/invoicer.rb', line 11

def create_invoice(invoice_base_record, name)
  progressive_val = ACube::InvoiceRecord.connection.execute("SELECT nextval('acube_invoice_records_progressive_seq') FROM acube_invoice_records_progressive_seq").first["nextval"]
  progressive_string = ACube.progressive_string.call(progressive_val)
  document.fill_with(transmission_format: @format, progressive: progressive_string)
  xml_body = document.to_xml

  invoice_record = ACube::InvoiceRecord.create!(
    record: invoice_base_record,
    name: name,
    format: @format,
    kind: invoice.document_kind,
    status: :created,
    progressive: progressive_string,
    xml_body: xml_body,
  )

  begin
    uuid = ACube::Endpoint::Invoices.new.create(xml_body)
    invoice_record.update_column(:webhook_uuid, uuid)
  rescue => e
    invoice_record.update_column(:status, :creation_error)
    raise e
  end
end

#regenerate_invoice(invoice_record_id, also_send: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/acube/invoicer.rb', line 36

def regenerate_invoice(invoice_record_id, also_send: false)
  invoice_record = ACube::InvoiceRecord.find(invoice_record_id)
  raise "This Invoice was already sent to ACube" if invoice_record.status != "creation_error"

  document.fill_with(transmission_format: invoice_record.format, progressive: invoice_record.progressive)
  xml_body = document.to_xml

  invoice_record.update_columns(
    status: :created,
    xml_body: xml_body,
  )

  if also_send
    begin
      uuid = ACube::Endpoint::Invoices.new.create(xml_body)
      invoice_record.update_column(:webhook_uuid, uuid)
    rescue => e
      invoice_record.update_column(:status, :creation_error)
      raise e
    end
  end
end