|
| 1 | +# Normalizes the data needed to render an event invoice from either source it |
| 2 | +# can be generated for: a single person's EventRegistration, or an |
| 3 | +# organization's bulk-payment FormSubmission. Both resolve to the same shape |
| 4 | +# (bill-to, attention, line items, total) so one view renders both. |
| 5 | +class EventInvoice |
| 6 | + ISSUER_NAME = "A Window Between Worlds".freeze |
| 7 | + ISSUER_ADDRESS_LINES = [ "1029 1/2 W 24th St", "Los Angeles, CA 90007" ].freeze |
| 8 | + ISSUER_EMAIL = "info@awbw.org".freeze |
| 9 | + PAYABLE_TO_NOTE = "Please make checks payable to A Window Between Worlds".freeze |
| 10 | + |
| 11 | + LineItem = Struct.new(:date, :description, :quantity, :unit_price_cents, keyword_init: true) do |
| 12 | + def amount_cents |
| 13 | + unit_price_cents.to_i * quantity.to_i |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + attr_reader :event, :number, :date, :reference, :client_id, |
| 18 | + :bill_to_name, :bill_to_address_lines, :bill_to_email, |
| 19 | + :attention, :line_items |
| 20 | + |
| 21 | + # One registration → one attendee billed at the event cost. The registrant's |
| 22 | + # snapshotted organization (if any) is the bill-to; otherwise bill the person. |
| 23 | + def self.from_registration(registration) |
| 24 | + event = registration.event |
| 25 | + registrant = registration.registrant |
| 26 | + organization = registration.organizations.first |
| 27 | + addressable = organization || registrant |
| 28 | + |
| 29 | + new( |
| 30 | + event: event, |
| 31 | + number: "R-#{registration.id}", |
| 32 | + date: registration.created_at.to_date, |
| 33 | + client_id: organization&.id || registrant.id, |
| 34 | + bill_to_name: organization&.name.presence || registrant.full_name, |
| 35 | + bill_to_address_lines: address_lines_for(addressable), |
| 36 | + bill_to_email: organization&.email.presence || registrant.preferred_email, |
| 37 | + attention: registrant.full_name, |
| 38 | + line_items: [ |
| 39 | + LineItem.new( |
| 40 | + date: registration.created_at.to_date, |
| 41 | + description: event.title, |
| 42 | + quantity: 1, |
| 43 | + unit_price_cents: event.cost_cents.to_i |
| 44 | + ) |
| 45 | + ] |
| 46 | + ) |
| 47 | + end |
| 48 | + |
| 49 | + # A blank invoice template carrying only the event's content (one attendee at |
| 50 | + # the event cost). The bill-to and attention are left empty to be filled in. |
| 51 | + def self.from_event(event) |
| 52 | + new( |
| 53 | + event: event, |
| 54 | + number: nil, |
| 55 | + date: Date.current, |
| 56 | + client_id: nil, |
| 57 | + bill_to_name: nil, |
| 58 | + bill_to_address_lines: [], |
| 59 | + bill_to_email: nil, |
| 60 | + attention: nil, |
| 61 | + line_items: [ |
| 62 | + LineItem.new( |
| 63 | + date: nil, |
| 64 | + description: event.title, |
| 65 | + quantity: 1, |
| 66 | + unit_price_cents: event.cost_cents.to_i |
| 67 | + ) |
| 68 | + ] |
| 69 | + ) |
| 70 | + end |
| 71 | + |
| 72 | + # A bulk payment bills the payer's organization for every attendee submitted. |
| 73 | + # The form captures the payer/org as free text (no Organization record), so |
| 74 | + # there is no structured address to show. |
| 75 | + def self.from_bulk_payment(submission) |
| 76 | + event = submission.resolved_event |
| 77 | + answers = submission.answers_by_identifier |
| 78 | + payer = submission.person |
| 79 | + payer_name = [ answers["payer_first_name"], answers["payer_last_name"] ] |
| 80 | + .map(&:presence).compact.join(" ").presence || payer&.full_name |
| 81 | + quantity = [ submission.bulk_payment_attendee_count, 1 ].max |
| 82 | + |
| 83 | + new( |
| 84 | + event: event, |
| 85 | + number: "B-#{submission.id}", |
| 86 | + date: submission.created_at.to_date, |
| 87 | + client_id: submission.id, |
| 88 | + bill_to_name: answers["payer_organization"].presence || payer_name, |
| 89 | + bill_to_address_lines: [], |
| 90 | + bill_to_email: answers["payer_email"].presence || payer&.preferred_email, |
| 91 | + attention: payer_name, |
| 92 | + line_items: [ |
| 93 | + LineItem.new( |
| 94 | + date: submission.created_at.to_date, |
| 95 | + description: event&.title, |
| 96 | + quantity: quantity, |
| 97 | + unit_price_cents: event&.cost_cents.to_i |
| 98 | + ) |
| 99 | + ] |
| 100 | + ) |
| 101 | + end |
| 102 | + |
| 103 | + def initialize(event:, number:, date:, client_id:, bill_to_name:, |
| 104 | + bill_to_address_lines:, bill_to_email:, attention:, line_items:, |
| 105 | + reference: nil) |
| 106 | + @event = event |
| 107 | + @number = number |
| 108 | + @date = date |
| 109 | + @client_id = client_id |
| 110 | + @bill_to_name = bill_to_name |
| 111 | + @bill_to_address_lines = bill_to_address_lines |
| 112 | + @bill_to_email = bill_to_email |
| 113 | + @attention = attention |
| 114 | + @line_items = line_items |
| 115 | + @reference = reference |
| 116 | + end |
| 117 | + |
| 118 | + def total_cents |
| 119 | + line_items.sum(&:amount_cents) |
| 120 | + end |
| 121 | + |
| 122 | + def issuer_name = ISSUER_NAME |
| 123 | + def issuer_address_lines = ISSUER_ADDRESS_LINES |
| 124 | + def issuer_email = ISSUER_EMAIL |
| 125 | + def payable_to_note = PAYABLE_TO_NOTE |
| 126 | + |
| 127 | + def self.address_lines_for(addressable) |
| 128 | + return [] unless addressable.respond_to?(:addresses) |
| 129 | + |
| 130 | + address = addressable.addresses.active.first |
| 131 | + return [] unless address |
| 132 | + |
| 133 | + city_line = [ address.city.presence, |
| 134 | + [ address.state.presence, address.zip_code.presence ].compact.join(" ").presence ] |
| 135 | + .compact.join(", ") |
| 136 | + [ address.street_address.presence, city_line.presence ].compact |
| 137 | + end |
| 138 | + private_class_method :address_lines_for |
| 139 | +end |
0 commit comments