Module: ReceiptTransmission

Extended by:
ActiveSupport::Concern
Included in:
Receipt
Defined in:
app/models/concerns/receipt_transmission.rb

Overview

Customer-facing receipt email + transmission addressing: recipient
resolution, notification channels, and the contact points/first document
used to address communications.

See Also:

Instance Method Summary collapse

Instance Method Details

#billing_addressAddress?

Returns customer billing address used for emails/PDFs.

Returns:

  • (Address, nil)

    customer billing address used for emails/PDFs



39
40
41
# File 'app/models/concerns/receipt_transmission.rb', line 39

def billing_address
  customer&.billing_address
end

#determine_emailString

Recipient list for the receipt email. Online orders fan out to every
invoice notification address on the customer; back-office receipts
use the single email column captured at create time.

Returns:

  • (String)


30
31
32
33
34
35
36
# File 'app/models/concerns/receipt_transmission.rb', line 30

def determine_email
  if (payment&.order&.order_reception_type == 'Online') && notification_emails.any?
    notification_emails.join(',')
  else
    email
  end
end

#first_documentInvoice, ...

First Invoice or CreditMemo this receipt touches, used as the
canonical "what does this receipt represent" object for emails and
contact resolution.

Returns:



76
77
78
79
80
# File 'app/models/concerns/receipt_transmission.rb', line 76

def first_document
  return nil if receipt_details.empty?

  receipt_details.first.invoice || receipt_details.first.credit_memo
end

#notification_emailsArray<String>

Email contact points the customer marked as receiving invoice
notifications.

Returns:

  • (Array<String>)


47
48
49
# File 'app/models/concerns/receipt_transmission.rb', line 47

def notification_emails
  customer.all_notification_channels.email.invoices.distinct.pluck(ContactPoint[:detail])
end

#primary_transmission_contactContactPoint?

Primary contact (per the first applied document) used when sending
transmission/email events.

Returns:



55
56
57
58
59
# File 'app/models/concerns/receipt_transmission.rb', line 55

def primary_transmission_contact
  return nil if first_document.nil?

  first_document.primary_transmission_contact
end

#send_emailObject

Send the customer-facing receipt email. Sender is the customer's
primary sales rep when present (BCC'd on the outgoing message);
otherwise the generic info inbox is used.



14
15
16
17
18
19
20
21
22
23
# File 'app/models/concerns/receipt_transmission.rb', line 14

def send_email
  sender = customer.try(:primary_sales_rep)
  CommunicationBuilder.new(
    resource: self,
    sender_party: sender,
    sender: (sender.nil? ? INFO_EMAIL : nil),
    emails: determine_email,
    bcc: sender&.email
  ).create
end

#transmission_contact_pointsArray<ContactPoint>

All contact points associated with the first applied document, used
to address transmission communications.

Returns:



65
66
67
68
69
# File 'app/models/concerns/receipt_transmission.rb', line 65

def transmission_contact_points
  return [] if first_document.nil?

  first_document.transmission_contact_points
end