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 ⇒ Object
- #collect_documents ⇒ Object
- #default_contact_point ⇒ Object
- #default_party ⇒ Object
-
#initialize(documents, options = {}) ⇒ AccountingDocumentTransmitter
constructor
A new instance of AccountingDocumentTransmitter.
- #mark_transmitted ⇒ Object
- #print_docs ⇒ Object
- #transmit ⇒ Object
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, = {}) @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
38 |
# File 'app/services/accounting_document_transmitter.rb', line 38 delegate :billing_entity, to: :customer |
#build_communication ⇒ Object
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: [: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 ⇒ Object
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_point ⇒ Object
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_party ⇒ Object
40 41 42 |
# File 'app/services/accounting_document_transmitter.rb', line 40 def default_party default_contact_point&.party end |
#mark_transmitted ⇒ Object
77 78 79 80 |
# File 'app/services/accounting_document_transmitter.rb', line 77 def mark_transmitted all_documents.each(&:transmit) true end |
#notification_channels ⇒ Object
Alias for Customer#notification_channels
36 |
# File 'app/services/accounting_document_transmitter.rb', line 36 delegate :notification_channels, to: :customer |
#print_docs ⇒ Object
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 |
#transmit ⇒ Object
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 = 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 |