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 = {}) ⇒ void

Parameters:

  • documents (Hash)

    document ids grouped by type (:INV, :CM, :SOA)

  • options (Hash) (defaults to: {})

    transmitter options

Options Hash (options):

  • customer (Customer)

    the customer the documents belong to

  • template_id (Integer)

    email template id used when building the communication



11
12
13
14
15
16
17
# File 'app/services/accounting_document_transmitter.rb', line 11

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:



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

delegate :billing_entity, to: :customer

#build_communicationCommunication

Builds the email communication carrying the document uploads.

Returns:



102
103
104
105
106
107
108
109
110
111
# File 'app/services/accounting_document_transmitter.rb', line 102

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_documentsArray<Upload>

Latest PDF upload for each document in the batch.

Returns:

  • (Array<Upload>)

    upload ids/records for all documents



68
69
70
71
72
73
74
# File 'app/services/accounting_document_transmitter.rb', line 68

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_pointContactPoint?

Best email/fax contact point to transmit documents to, preferring the
customer's own notification channel over the billing entity's.

Returns:

  • (ContactPoint, nil)

    transmittable contact point, or nil when none qualifies



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/accounting_document_transmitter.rb', line 22

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_partyParty?

Party to address communications to, derived from the default contact point.

Returns:



50
51
52
# File 'app/services/accounting_document_transmitter.rb', line 50

def default_party
  default_contact_point&.party
end

#mark_transmittedBoolean

Marks every document as transmitted.

Returns:

  • (Boolean)

    always true



95
96
97
98
# File 'app/services/accounting_document_transmitter.rb', line 95

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

#notification_channelsObject

Alias for Customer#notification_channels

Returns:

  • (Object)

    Customer#notification_channels

See Also:



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

delegate :notification_channels, to: :customer

Combines all document PDFs into one uploaded file.

Returns:

  • (Upload, nil)

    the combined upload, or nil when combining failed



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

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

#transmitHash{Symbol => String, nil}

Queues every document for transmission to the default contact point.

Returns:

  • (Hash{Symbol => String, nil})

    :success_message and :error_message



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

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