Module: Pay::Receipts

Defined in:
lib/pay/receipts.rb

Instance Method Summary collapse

Instance Method Details

#discount_description(discount) ⇒ Object


70
71
72
73
74
75
76
77
78
79
# File 'lib/pay/receipts.rb', line 70

def discount_description(discount)
  coupon = discount.dig("discount", "coupon")
  name = coupon.dig("name")

  if (percent = coupon["percent_off"])
    I18n.t("pay.line_items.percent_discount", name: name, percent: ActiveSupport::NumberHelper.number_to_rounded(percent, strip_insignificant_zeros: true))
  else
    I18n.t("pay.line_items.amount_discount", name: name, amount: Pay::Currency.format(coupon["amount_off"], currency: coupon["currency"]))
  end
end

#invoiceObject


121
122
123
# File 'lib/pay/receipts.rb', line 121

def invoice
  invoice_pdf.render
end

#invoice_detailsObject


125
126
127
128
129
130
131
# File 'lib/pay/receipts.rb', line 125

def invoice_details
  [
    [I18n.t("pay.invoice.number"), invoice_number],
    [I18n.t("pay.invoice.date"), I18n.l(created_at, format: :long)],
    [I18n.t("pay.invoice.payment_method"), charged_to]
  ]
end

#invoice_filenameObject


117
118
119
# File 'lib/pay/receipts.rb', line 117

def invoice_filename
  "invoice-#{created_at.strftime("%Y-%m-%d")}.pdf"
end

#invoice_numberObject


153
154
155
# File 'lib/pay/receipts.rb', line 153

def invoice_number
  id
end

#invoice_pdf(**options) ⇒ Object


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pay/receipts.rb', line 133

def invoice_pdf(**options)
  defaults = {
    details: invoice_details,
    recipient: [
      customer.customer_name,
      customer.email,
      customer.owner.try(:extra_billing_info)
    ],
    company: {
      name: Pay.business_name,
      address: Pay.business_address,
      email: Pay.support_email,
      logo: Pay.
    },
    line_items: pdf_line_items
  }

  ::Receipts::Invoice.new(defaults.deep_merge(options))
end

#pdf_line_itemsObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pay/receipts.rb', line 24

def pdf_line_items
  items = [
    [
      "<b>#{I18n.t("pay.line_items.description")}</b>",
      "<b>#{I18n.t("pay.line_items.quantity")}</b>",
      "<b>#{I18n.t("pay.line_items.unit_price")}</b>",
      "<b>#{I18n.t("pay.line_items.amount")}</b>"
    ]
  ]

  # Unit price is stored with the line item
  # Negative amounts shouldn't display quantity
  # Sort by line_items by period_end? oldest to newest
  if line_items.any?
    line_items.each do |li|
      items << [li["description"], li["quantity"], Pay::Currency.format(li["unit_amount"], currency: currency), Pay::Currency.format(li["amount"], currency: currency)]

      Array.wrap(li["discounts"]).each do |discount_id|
        if (discount = total_discount_amounts.find { |d| d.dig("discount", "id") == discount_id })
          items << [discount_description(discount), nil, nil, Pay::Currency.format(-discount["amount"], currency: currency)]
        end
      end
    end
  else
    items << [product, 1, Pay::Currency.format(amount, currency: currency), Pay::Currency.format(amount, currency: currency)]
  end

  # If no subtotal, we will display the total
  items << [nil, nil, I18n.t("pay.line_items.subtotal"), Pay::Currency.format(subtotal || amount, currency: currency)]

  # Discounts on the invoice
  Array.wrap(discounts).each do |discount_id|
    if (discount = total_discount_amounts.find { |d| d.dig("discount", "id") == discount_id })
      items << [nil, nil, discount_description(discount), Pay::Currency.format(-discount["amount"], currency: currency)]
    end
  end

  # Tax rates
  Array.wrap(total_tax_amounts).each do |tax_amount|
    items << [nil, nil, tax_description(tax_amount), Pay::Currency.format(tax, currency: currency)]
  end

  items << [nil, nil, I18n.t("pay.line_items.total"), Pay::Currency.format(amount, currency: currency)]
  items
end

#productObject


3
4
5
# File 'lib/pay/receipts.rb', line 3

def product
  Pay.application_name
end

#receiptObject


12
13
14
# File 'lib/pay/receipts.rb', line 12

def receipt
  receipt_pdf.render
end

#receipt_detailsObject


16
17
18
19
20
21
22
# File 'lib/pay/receipts.rb', line 16

def receipt_details
  [
    [I18n.t("pay.receipt.number"), receipt_number],
    [I18n.t("pay.receipt.date"), I18n.l(created_at, format: :long)],
    [I18n.t("pay.receipt.payment_method"), charged_to]
  ]
end

#receipt_filenameObject Also known as: filename


7
8
9
# File 'lib/pay/receipts.rb', line 7

def receipt_filename
  "receipt-#{created_at.strftime("%Y-%m-%d")}.pdf"
end

#receipt_numberObject


157
158
159
# File 'lib/pay/receipts.rb', line 157

def receipt_number
  invoice_number
end

#receipt_pdf(**options) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pay/receipts.rb', line 88

def receipt_pdf(**options)
  receipt_line_items = pdf_line_items

  # Include total paid
  receipt_line_items << [nil, nil, I18n.t("pay.receipt.amount_paid"), Pay::Currency.format(amount, currency: currency)]

  if refunded?
    receipt_line_items << [nil, nil, I18n.t("pay.receipt.refunded_on"), Pay::Currency.format(amount_refunded, currency: currency)]
  end

  defaults = {
    details: receipt_details,
    recipient: [
      customer.customer_name,
      customer.email,
      customer.owner.try(:extra_billing_info)
    ],
    company: {
      name: Pay.business_name,
      address: Pay.business_address,
      email: Pay.support_email,
      logo: Pay.
    },
    line_items: receipt_line_items
  }

  ::Receipts::Receipt.new(defaults.deep_merge(options))
end

#tax_description(tax_amount) ⇒ Object


81
82
83
84
85
86
# File 'lib/pay/receipts.rb', line 81

def tax_description(tax_amount)
  tax_rate = tax_amount["tax_rate"]
  percent = "#{ActiveSupport::NumberHelper.number_to_rounded(tax_rate["percentage"], strip_insignificant_zeros: true)}%"
  percent += " inclusive" if tax_rate["inclusive"]
  "#{tax_rate["display_name"]} - #{tax_rate["jurisdiction"]} (#{percent})"
end