Class: AccountingDocumentTransmitter
- Inherits:
-
Object
- Object
- AccountingDocumentTransmitter
- Defined in:
- app/services/accounting_document_transmitter.rb
Overview
Service object: accounting document transmitter.
Instance Attribute Summary collapse
-
#credit_memos ⇒ Object
readonly
Returns the value of attribute credit_memos.
-
#customer ⇒ Object
readonly
Returns the value of attribute customer.
-
#invoices ⇒ Object
readonly
Returns the value of attribute invoices.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#statement_of_accounts ⇒ Object
readonly
Returns the value of attribute statement_of_accounts.
Delegated Instance Attributes collapse
-
#billing_entity ⇒ Object
Alias for Customer#billing_entity.
-
#notification_channels ⇒ Object
Alias for Customer#notification_channels.
Instance Method Summary collapse
-
#build_communication ⇒ Communication
Builds the email communication carrying the document uploads.
-
#collect_documents ⇒ Array<Upload>
Latest PDF upload for each document in the batch.
-
#default_contact_point ⇒ ContactPoint?
Best email/fax contact point to transmit documents to, preferring the customer's own notification channel over the billing entity's.
-
#default_party ⇒ Party?
Party to address communications to, derived from the default contact point.
- #initialize(documents, options = {}) ⇒ void constructor
-
#mark_transmitted ⇒ Boolean
Marks every document as transmitted.
-
#print_docs ⇒ Upload?
Combines all document PDFs into one uploaded file.
-
#transmit ⇒ Hash{Symbol => String, nil}
Queues every document for transmission to the default contact point.
Constructor Details
#initialize(documents, options = {}) ⇒ void
11 12 13 14 15 16 17 |
# File 'app/services/accounting_document_transmitter.rb', line 11 def initialize(documents, = {}) @invoices = Invoice.where(id: documents[:INV]) @credit_memos = CreditMemo.where(id: documents[:CM]) @statement_of_accounts = StatementOfAccount.where(id: documents[:SOA]) @customer = [:customer] @options = end |
Instance Attribute Details
#credit_memos ⇒ Object (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 |
#customer ⇒ Object (readonly)
Returns the value of attribute customer.
4 5 6 |
# File 'app/services/accounting_document_transmitter.rb', line 4 def customer @customer end |
#invoices ⇒ Object (readonly)
Returns the value of attribute invoices.
4 5 6 |
# File 'app/services/accounting_document_transmitter.rb', line 4 def invoices @invoices end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'app/services/accounting_document_transmitter.rb', line 4 def @options end |
#statement_of_accounts ⇒ Object (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_entity ⇒ Object
Alias for Customer#billing_entity
46 |
# File 'app/services/accounting_document_transmitter.rb', line 46 delegate :billing_entity, to: :customer |
#build_communication ⇒ Communication
Builds the email communication carrying the document uploads.
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: [: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_documents ⇒ Array<Upload>
Latest PDF upload for each document in the batch.
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_point ⇒ ContactPoint?
Best email/fax contact point to transmit documents to, preferring the
customer's own notification channel over the billing entity's.
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_party ⇒ Party?
Party to address communications to, derived from the default contact point.
50 51 52 |
# File 'app/services/accounting_document_transmitter.rb', line 50 def default_party default_contact_point&.party end |
#mark_transmitted ⇒ Boolean
Marks every document as transmitted.
95 96 97 98 |
# File 'app/services/accounting_document_transmitter.rb', line 95 def mark_transmitted all_documents.each(&:transmit) true end |
#notification_channels ⇒ Object
Alias for Customer#notification_channels
44 |
# File 'app/services/accounting_document_transmitter.rb', line 44 delegate :notification_channels, to: :customer |
#print_docs ⇒ Upload?
Combines all document PDFs into one uploaded file.
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 |
#transmit ⇒ Hash{Symbol => String, nil}
Queues every document for transmission to the default contact point.
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 = not_queued.any? ? "#{not_queued.join(', ')} cannot be transmitted, no transmittable contact points present." : nil = "#{queued} documents queued for transmission." { success_message: , error_message: } end |