Class: Reji::Invoice

Inherits:
Object
  • Object
show all
Defined in:
lib/reji/invoice.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, invoice) ⇒ Invoice

Returns a new instance of Invoice.



7
8
9
10
11
12
13
14
# File 'lib/reji/invoice.rb', line 7

def initialize(owner, invoice)
  raise Reji::InvalidInvoiceError.invalid_owner(invoice, owner) if owner.stripe_id != invoice.customer

  @owner = owner
  @invoice = invoice
  @items = nil
  @taxes = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key) ⇒ Object

Dynamically get values from the Stripe invoice.



208
209
210
# File 'lib/reji/invoice.rb', line 208

def method_missing(key)
  @invoice[key]
end

Instance Attribute Details

#ownerObject (readonly)

Get the Stripe model instance.



200
201
202
# File 'lib/reji/invoice.rb', line 200

def owner
  @owner
end

Instance Method Details

#amount_offObject

Get the discount amount for the invoice.



88
89
90
# File 'lib/reji/invoice.rb', line 88

def amount_off
  format_amount(raw_amount_off)
end

#as_stripe_invoiceObject

Get the Stripe invoice instance.



203
204
205
# File 'lib/reji/invoice.rb', line 203

def as_stripe_invoice
  @invoice
end

#couponObject

Get the coupon code applied to the invoice.



71
72
73
# File 'lib/reji/invoice.rb', line 71

def coupon
  return @invoice[:discount][:coupon][:id] if @invoice[:discount]
end

#dateObject

Get a date for the invoice.



17
18
19
# File 'lib/reji/invoice.rb', line 17

def date
  Time.zone.at(@invoice.created || @invoice.date)
end

#discountObject

Get the discount amount.



57
58
59
# File 'lib/reji/invoice.rb', line 57

def discount
  format_amount(raw_discount)
end

#discount?Boolean

Determine if the invoice has a discount.

Returns:

  • (Boolean)


52
53
54
# File 'lib/reji/invoice.rb', line 52

def discount?
  raw_discount > 0
end

#discount_is_percentageObject

Determine if the discount is a percentage.



76
77
78
79
80
# File 'lib/reji/invoice.rb', line 76

def discount_is_percentage
  return false unless @invoice[:discount]

  !!@invoice[:discount][:coupon][:percent_off]
end

#download(data) ⇒ Object

Create an invoice download response.



181
182
183
184
185
# File 'lib/reji/invoice.rb', line 181

def download(data)
  filename = "#{data[:product]}_#{date.month}_#{date.year}"

  download_as(filename, data)
end

#download_as(filename, data) ⇒ Object

Create an invoice download response with a specific filename.



188
189
190
# File 'lib/reji/invoice.rb', line 188

def download_as(filename, data)
  { data: pdf(data), filename: filename }
end

#invoice_itemsObject

Get all of the “invoice item” line items.



141
142
143
# File 'lib/reji/invoice.rb', line 141

def invoice_items
  invoice_line_items_by_type('invoiceitem')
end

#invoice_line_items_by_type(type) ⇒ Object

Get all of the invoice items by a given type.



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/reji/invoice.rb', line 151

def invoice_line_items_by_type(type)
  if @items.nil?
    refresh_with_expanded_tax_rates

    @items = @invoice.lines.auto_paging_each
  end

  @items
    .select { |item| item.type == type }
    .map { |item| InvoiceLineItem.new(self, item) }
end

#not_tax_exempt?Boolean

Determine if the customer is not exempted from taxes.

Returns:

  • (Boolean)


126
127
128
# File 'lib/reji/invoice.rb', line 126

def not_tax_exempt?
  @invoice[:customer_tax_exempt] == 'none'
end

#pdf(data) ⇒ Object

Capture the invoice as a PDF and return the raw bytes.



176
177
178
# File 'lib/reji/invoice.rb', line 176

def pdf(data)
  WickedPdf.new.pdf_from_string(view(data))
end

#percent_offObject

Get the discount percentage for the invoice.



83
84
85
# File 'lib/reji/invoice.rb', line 83

def percent_off
  coupon ? @invoice[:discount][:coupon][:percent_off] : 0
end

#raw_amount_offObject

Get the raw discount amount for the invoice.



93
94
95
96
97
# File 'lib/reji/invoice.rb', line 93

def raw_amount_off
  amount_off = @invoice[:discount][:coupon][:amount_off]

  amount_off || 0
end

#raw_discountObject

Get the raw discount amount.



62
63
64
65
66
67
68
# File 'lib/reji/invoice.rb', line 62

def raw_discount
  return 0 unless @invoice.discount

  return (@invoice.subtotal * (percent_off / 100)).round.to_i if discount_is_percentage

  raw_amount_off
end

#raw_starting_balanceObject

Get the raw starting balance for the invoice.



47
48
49
# File 'lib/reji/invoice.rb', line 47

def raw_starting_balance
  @invoice[:starting_balance] || 0
end

#raw_totalObject

Get the raw total amount that was paid (or will be paid).



27
28
29
# File 'lib/reji/invoice.rb', line 27

def raw_total
  @invoice.total + raw_starting_balance
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/reji/invoice.rb', line 212

def respond_to_missing?(method_name, include_private = false)
  super
end

#reverse_charge_appliesObject

Determine if reverse charge applies to the customer.



136
137
138
# File 'lib/reji/invoice.rb', line 136

def reverse_charge_applies
  @invoice[:customer_tax_exempt] == 'reverse'
end

#starting_balanceObject

Get the starting balance for the invoice.



42
43
44
# File 'lib/reji/invoice.rb', line 42

def starting_balance
  Reji.format_amount(raw_starting_balance)
end

#starting_balance?Boolean

Determine if the account had a starting balance.

Returns:

  • (Boolean)


37
38
39
# File 'lib/reji/invoice.rb', line 37

def starting_balance?
  raw_starting_balance < 0
end

#subscriptionsObject

Get all of the “subscription” line items.



146
147
148
# File 'lib/reji/invoice.rb', line 146

def subscriptions
  invoice_line_items_by_type('subscription')
end

#subtotalObject

Get the total of the invoice (before discounts).



32
33
34
# File 'lib/reji/invoice.rb', line 32

def subtotal
  Reji.format_amount(@invoice[:subtotal])
end

#taxObject

Get the total tax amount.



100
101
102
# File 'lib/reji/invoice.rb', line 100

def tax
  format_amount(@invoice.tax)
end

#tax?Boolean

Determine if the invoice has tax applied.

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/reji/invoice.rb', line 105

def tax?
  line_items = invoice_items + subscriptions

  line_items.any?(&:tax_rates?)
end

#tax_exempt?Boolean

Determine if the customer is exempted from taxes.

Returns:

  • (Boolean)


131
132
133
# File 'lib/reji/invoice.rb', line 131

def tax_exempt?
  @invoice[:customer_tax_exempt] == 'exempt'
end

#taxesObject

Get the taxes applied to the invoice.



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/reji/invoice.rb', line 112

def taxes
  return @taxes unless @taxes.nil?

  refresh_with_expanded_tax_rates

  @taxes = @invoice.total_tax_amounts
    .sort_by(&:inclusive)
    .reverse
    .map { |tax_amount| Tax.new(tax_amount.amount, @invoice.currency, tax_amount.tax_rate) }

  @taxes
end

#totalObject

Get the total amount that was paid (or will be paid).



22
23
24
# File 'lib/reji/invoice.rb', line 22

def total
  Reji.format_amount(raw_total)
end

#view(data) ⇒ Object

Get the View instance for the invoice.



164
165
166
167
168
169
170
171
172
173
# File 'lib/reji/invoice.rb', line 164

def view(data)
  ActionController::Base.new.render_to_string(
    template: 'receipt',
    locals: data.merge({
      invoice: self,
      owner: owner,
      user: owner,
    })
  )
end

#void(options = {}) ⇒ Object

Void the Stripe invoice.



193
194
195
196
197
# File 'lib/reji/invoice.rb', line 193

def void(options = {})
  @invoice = @invoice.void_invoice(options, @owner.stripe_options)

  self
end