Class: AccountingDocumentTransmitter

Inherits:
Object
  • Object
show all
Defined in:
app/services/accounting_document_transmitter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(documents, options = {}) ⇒ AccountingDocumentTransmitter

Returns a new instance of AccountingDocumentTransmitter.



4
5
6
7
8
9
10
# File 'app/services/accounting_document_transmitter.rb', line 4

def initialize(documents, options = {})
  @invoices = Invoice.where(id: documents[:INV])
  @credit_memos = CreditMemo.where(id: documents[:CM])
  @statement_of_accounts = StatementOfAccount.where(id: documents[:SOA])
  @customer = options[:customer]
  @options = options
end

Instance Attribute Details

#credit_memosObject (readonly)

Returns the value of attribute credit_memos.



2
3
4
# File 'app/services/accounting_document_transmitter.rb', line 2

def credit_memos
  @credit_memos
end

#customerObject (readonly)

Returns the value of attribute customer.



2
3
4
# File 'app/services/accounting_document_transmitter.rb', line 2

def customer
  @customer
end

#invoicesObject (readonly)

Returns the value of attribute invoices.



2
3
4
# File 'app/services/accounting_document_transmitter.rb', line 2

def invoices
  @invoices
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'app/services/accounting_document_transmitter.rb', line 2

def options
  @options
end

#statement_of_accountsObject (readonly)

Returns the value of attribute statement_of_accounts.



2
3
4
# File 'app/services/accounting_document_transmitter.rb', line 2

def statement_of_accounts
  @statement_of_accounts
end

Instance Method Details

#billing_entityObject



37
38
39
# File 'app/services/accounting_document_transmitter.rb', line 37

def billing_entity
  customer.billing_entity
end

#build_communicationObject



83
84
85
86
87
88
89
90
91
92
# File 'app/services/accounting_document_transmitter.rb', line 83

def build_communication
  CommunicationBuilder.new(
    recipient_party: default_party || customer,
    upload_ids: collect_documents,
    recipient_contact_point_id: default_contact_point.try(:id),
    email_template_id: options[:template_id],
    merge_options: { customer: customer, customer_reference_number: customer.reference_number, total_open_amount: total_open_amount },
    save_merge_options: true
  ).build
end

#collect_documentsObject



55
56
57
58
59
60
61
# File 'app/services/accounting_document_transmitter.rb', line 55

def collect_documents
  uploads = []
  uploads.concat(uploads_for(invoices, 'invoice_pdf', 'Invoice'))
  uploads.concat(uploads_for(credit_memos, 'credit_memo_pdf', 'CreditMemo'))
  uploads.concat(uploads_for(statement_of_accounts, 'statement_of_account_pdf', 'StatementOfAccount'))
  return uploads
end

#default_contact_pointObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/accounting_document_transmitter.rb', line 12

def default_contact_point
  if statement_of_accounts.any?
    nt = ['statement_of_accounts', 'invoices']
    nt_order = "notification_type DESC"
  else
    nt = ['invoices', 'statement_of_accounts']
    nt_order = "notification_type ASC"
  end
  if notification_channels.where(notification_type: nt, transmission_type: 'Email/Fax').any?
    # if the customer has a notification channel set up, use that
    nc = notification_channels.where(notification_type: nt, transmission_type: 'Email/Fax').order(nt_order).first
  elsif billing_entity and billing_entity.notification_channels.where(notification_type: nt, transmission_type: 'Email/Fax').any?
    # or else use the billing entity notification channel
    nc = billing_entity.notification_channels.where(notification_type: nt, transmission_type: 'Email/Fax').order(nt_order).first
  else
    nc = nil
  end
  return nil if nc.nil?
  ContactPoint::CAN_TRANSMIT.include?(nc.contact_point.category) ? nc.contact_point : nil
end

#default_partyObject



41
42
43
# File 'app/services/accounting_document_transmitter.rb', line 41

def default_party
  default_contact_point.nil? ? nil : default_contact_point.party
end

#mark_transmittedObject



78
79
80
81
# File 'app/services/accounting_document_transmitter.rb', line 78

def mark_transmitted
  all_documents.each(&:transmit)
  true
end

#notification_channelsObject



33
34
35
# File 'app/services/accounting_document_transmitter.rb', line 33

def notification_channels
  customer.notification_channels
end


45
46
47
48
49
50
51
52
53
# File 'app/services/accounting_document_transmitter.rb', line 45

def print_docs
  uploads = collect_documents
  file_name = "accounting_documents_#{Time.current.strftime("%m_%d_%Y_%I_%M%p")}.pdf".downcase
  output_file_path = Upload.temp_location(file_name)
  combined_docs_path = PdfTools.combine(uploads, output_file_path: output_file_path, remove_originals: false)
  return nil if !File.exist?(combined_docs_path)
  upload = Upload.uploadify(combined_docs_path, 'accounting_documents', nil, file_name)
  return upload
end

#transmitObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/services/accounting_document_transmitter.rb', line 63

def transmit
  not_queued = []
  queued = 0
  all_documents.each do |doc|
    if doc.queue_for_transmission
      queued += 1
    else
      not_queued << doc.reference_number
    end
  end
  error_message = not_queued.any? ? "#{not_queued.join(', ')} cannot be transmitted, no transmittable contact points present." : nil
  success_message = "#{queued} documents queued for transmission."
  return {success_message: success_message, error_message: error_message}
end