Module: StripeInvoice::InvoicesHelper

Defined in:
app/helpers/stripe_invoice/invoices_helper.rb

Instance Method Summary collapse

Instance Method Details

#format_currency(amount, currency) ⇒ Object Also known as: fc

nicely formats the amount and currency



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/stripe_invoice/invoices_helper.rb', line 5

def format_currency(amount, currency)
  
  currency = currency.upcase()
  # assuming that all currencies are split into 100 parts is probably wrong
  # on an i18n scale but it works for USD, EUR and LBP
  # TODO fix this maybe?
  amount = amount / 100.0
  options = {
    # use comma for euro 
    separator: currency == 'EUR' ? ',' : '.', 
    delimiter: currency == 'EUR' ? '.' : ',',
    format: currency == 'EUR' ? '%n %u' : '%u%n',
  }
  case currency
  when 'EUR'
    options[:unit] = '' 
  when 'LBP'
    options[:unit] = '£'
  when 'USD'
    options[:unit] = '$'
  else
    options[:unit] = currency
  end
  return number_to_currency(amount, options)
end

#pdf_date_format(date) ⇒ Object



42
43
44
45
# File 'app/helpers/stripe_invoice/invoices_helper.rb', line 42

def pdf_date_format(date)
  date = Time.at(date)
  date.strftime('%d/%m/%Y')
end

#plan_duration_in_month(invoice) ⇒ Object



47
48
49
50
51
# File 'app/helpers/stripe_invoice/invoices_helper.rb', line 47

def plan_duration_in_month(invoice)
  period_end_date = Time.at(invoice.period_end)
  period_start_date = Time.at(invoice.period_start)
  ((period_end_date.year * 12 + period_end_date.month) - (period_start_date.year * 12 + period_start_date.month)).to_i
end

#plan_public_name(plan) ⇒ Object

checks the plan’s metadata for a custom public name and uses that if available - else it will use plan



35
36
37
38
39
40
# File 'app/helpers/stripe_invoice/invoices_helper.rb', line 35

def plan_public_name(plan)
  plan = plan.with_indifferent_access
  (plan[:metadata] && 
    plan [:metadata][:stripe_invoice] && 
    plan[:metadata][:stripe_invoice][:public_name]) || plan[:name]
end