Method: Pay::Receipts#pdf_line_items

Defined in:
lib/pay/receipts.rb

#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
69
70
71
# 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>"
    ]
  ]

  if try(:stripe_invoice)
    stripe_invoice.lines.auto_paging_each do |line|
      items << [line.description, line.quantity, Pay::Currency.format(line.pricing.unit_amount_decimal, currency: line.currency), Pay::Currency.format(line.amount, currency: line.currency)]

      line.discounts.each do |discount_id|
        discount = stripe_invoice.total_discount_amounts.find { |d| d.discount.id == discount_id }
        items << [discount_description(discount), nil, nil, Pay::Currency.format(-discount.amount, currency: currency)]
      end
    end
  else
    items << [pdf_product_name, 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(try(:stripe_invoice)&.subtotal || amount, currency: currency)]

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

  # Total excluding tax
  if try(:stripe_invoice)
    items << [nil, nil, I18n.t("pay.line_items.total"), Pay::Currency.format(stripe_invoice.total_excluding_tax, currency: currency)]
  end

  # Tax rates
  try(:stripe_invoice)&.total_taxes&.each do |tax|
    next if tax.amount.zero?
    # tax_rate = ::Stripe::TaxRate.retrieve(tax.tax_rate_details.tax_rate)
    items << [nil, nil, I18n.t("pay.line_items.tax"), Pay::Currency.format(tax.amount, currency: currency)]
  end

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