Class: AccountingDocumentTransmitter

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

Overview

Service object: accounting document transmitter.

Instance Attribute Summary collapse

Delegated Instance Attributes collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of AccountingDocumentTransmitter.



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

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.



4
5
6
# File 'app/services/accounting_document_transmitter.rb', line 4

def credit_memos
  @credit_memos
end

#customerObject (readonly)

Returns the value of attribute customer.



4
5
6
# File 'app/services/accounting_document_transmitter.rb', line 4

def customer
  @customer
end

#invoicesObject (readonly)

Returns the value of attribute invoices.



4
5
6
# File 'app/services/accounting_document_transmitter.rb', line 4

def invoices
  @invoices
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'app/services/accounting_document_transmitter.rb', line 4

def options
  @options
end

#statement_of_accountsObject (readonly)

Returns the value of attribute statement_of_accounts.



4
5
6
# File 'app/services/accounting_document_transmitter.rb', line 4

def statement_of_accounts
  @statement_of_accounts
end

Instance Method Details

#billing_entityObject

Alias for Customer#billing_entity

Returns:

  • (Object)

    Customer#billing_entity

See Also:



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

delegate :billing_entity, to: :customer

#build_communicationObject



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

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



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

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'))
  uploads
end

#default_contact_pointObject



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

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

  ContactPoint::CAN_TRANSMIT.include?(nc.contact_point.category) ? nc.contact_point : nil
end

#default_partyObject



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

def default_party
  default_contact_point&.party
end

#mark_transmittedObject



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

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

#notification_channelsObject

Alias for Customer#notification_channels

Returns:

  • (Object)

    Customer#notification_channels

See Also:



36
# File 'app/services/accounting_document_transmitter.rb', line 36

delegate :notification_channels, to: :customer


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

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 unless File.exist?(combined_docs_path)

  Upload.uploadify(combined_docs_path, 'accounting_documents', nil, file_name)
end

#transmitObject



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

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."
  { success_message: success_message, error_message: error_message }
end