Class: Reji::Invoice
- Inherits:
-
Object
- Object
- Reji::Invoice
- Defined in:
- lib/reji/invoice.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
Get the Stripe model instance.
Instance Method Summary collapse
-
#amount_off ⇒ Object
Get the discount amount for the invoice.
-
#as_stripe_invoice ⇒ Object
Get the Stripe invoice instance.
-
#coupon ⇒ Object
Get the coupon code applied to the invoice.
-
#date ⇒ Object
Get a date for the invoice.
-
#discount ⇒ Object
Get the discount amount.
-
#discount? ⇒ Boolean
Determine if the invoice has a discount.
-
#discount_is_percentage ⇒ Object
Determine if the discount is a percentage.
-
#download(data) ⇒ Object
Create an invoice download response.
-
#download_as(filename, data) ⇒ Object
Create an invoice download response with a specific filename.
-
#initialize(owner, invoice) ⇒ Invoice
constructor
A new instance of Invoice.
-
#invoice_items ⇒ Object
Get all of the “invoice item” line items.
-
#invoice_line_items_by_type(type) ⇒ Object
Get all of the invoice items by a given type.
-
#method_missing(key) ⇒ Object
Dynamically get values from the Stripe invoice.
-
#not_tax_exempt? ⇒ Boolean
Determine if the customer is not exempted from taxes.
-
#pdf(data) ⇒ Object
Capture the invoice as a PDF and return the raw bytes.
-
#percent_off ⇒ Object
Get the discount percentage for the invoice.
-
#raw_amount_off ⇒ Object
Get the raw discount amount for the invoice.
-
#raw_discount ⇒ Object
Get the raw discount amount.
-
#raw_starting_balance ⇒ Object
Get the raw starting balance for the invoice.
-
#raw_total ⇒ Object
Get the raw total amount that was paid (or will be paid).
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#reverse_charge_applies ⇒ Object
Determine if reverse charge applies to the customer.
-
#starting_balance ⇒ Object
Get the starting balance for the invoice.
-
#starting_balance? ⇒ Boolean
Determine if the account had a starting balance.
-
#subscriptions ⇒ Object
Get all of the “subscription” line items.
-
#subtotal ⇒ Object
Get the total of the invoice (before discounts).
-
#tax ⇒ Object
Get the total tax amount.
-
#tax? ⇒ Boolean
Determine if the invoice has tax applied.
-
#tax_exempt? ⇒ Boolean
Determine if the customer is exempted from taxes.
-
#taxes ⇒ Object
Get the taxes applied to the invoice.
-
#total ⇒ Object
Get the total amount that was paid (or will be paid).
-
#view(data) ⇒ Object
Get the View instance for the invoice.
-
#void(options = {}) ⇒ Object
Void the Stripe invoice.
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
#owner ⇒ Object (readonly)
Get the Stripe model instance.
200 201 202 |
# File 'lib/reji/invoice.rb', line 200 def owner @owner end |
Instance Method Details
#amount_off ⇒ Object
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_invoice ⇒ Object
Get the Stripe invoice instance.
203 204 205 |
# File 'lib/reji/invoice.rb', line 203 def as_stripe_invoice @invoice end |
#coupon ⇒ Object
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 |
#date ⇒ Object
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 |
#discount ⇒ Object
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.
52 53 54 |
# File 'lib/reji/invoice.rb', line 52 def discount? raw_discount > 0 end |
#discount_is_percentage ⇒ Object
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_items ⇒ Object
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? @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.
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_off ⇒ Object
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_off ⇒ Object
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_discount ⇒ Object
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_balance ⇒ Object
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_total ⇒ Object
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
212 213 214 |
# File 'lib/reji/invoice.rb', line 212 def respond_to_missing?(method_name, include_private = false) super end |
#reverse_charge_applies ⇒ Object
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_balance ⇒ Object
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.
37 38 39 |
# File 'lib/reji/invoice.rb', line 37 def starting_balance? raw_starting_balance < 0 end |
#subscriptions ⇒ Object
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 |
#subtotal ⇒ Object
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 |
#tax ⇒ Object
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.
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.
131 132 133 |
# File 'lib/reji/invoice.rb', line 131 def tax_exempt? @invoice[:customer_tax_exempt] == 'exempt' end |
#taxes ⇒ Object
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? @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 |
#total ⇒ Object
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( = {}) @invoice = @invoice.void_invoice(, @owner.) self end |