Class: Reji::Invoice
- Inherits:
-
Object
- Object
- Reji::Invoice
- Defined in:
- lib/reji/invoice.rb
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_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.
-
#has_discount ⇒ Object
Determine if the invoice has a discount.
-
#has_starting_balance ⇒ Object
Determine if the account had a starting balance.
-
#has_tax ⇒ Object
Determine if the invoice has tax applied.
-
#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.
-
#is_not_tax_exempt ⇒ Object
Determine if the customer is not exempted from taxes.
-
#is_tax_exempt ⇒ Object
Determine if the customer is exempted from taxes.
-
#method_missing(key) ⇒ Object
Dynamically get values from the Stripe invoice.
-
#owner ⇒ Object
Get the Stripe model instance.
-
#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).
-
#reverse_charge_applies ⇒ Object
Determine if reverse charge applies to the customer.
-
#starting_balance ⇒ Object
Get the starting balance for the invoice.
-
#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.
-
#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.
210 211 212 |
# File 'lib/reji/invoice.rb', line 210 def method_missing(key) @invoice[key] 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 self.format_amount(self.raw_amount_off) end |
#as_stripe_invoice ⇒ Object
Get the Stripe invoice instance.
205 206 207 |
# File 'lib/reji/invoice.rb', line 205 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.at(@invoice.created ? @invoice.created : @invoice.date) end |
#discount ⇒ Object
Get the discount amount.
57 58 59 |
# File 'lib/reji/invoice.rb', line 57 def discount self.format_amount(self.raw_discount) 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]}_#{self.date.month}_#{self.date.year}" self.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 => self.pdf(data), :filename => filename} end |
#has_discount ⇒ Object
Determine if the invoice has a discount.
52 53 54 |
# File 'lib/reji/invoice.rb', line 52 def has_discount self.raw_discount > 0 end |
#has_starting_balance ⇒ Object
Determine if the account had a starting balance.
37 38 39 |
# File 'lib/reji/invoice.rb', line 37 def has_starting_balance self.raw_starting_balance < 0 end |
#has_tax ⇒ Object
Determine if the invoice has tax applied.
105 106 107 108 109 |
# File 'lib/reji/invoice.rb', line 105 def has_tax line_items = self.invoice_items + self.subscriptions line_items.any? { |item| item.has_tax_rates } 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 self.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? self. @items = @invoice.lines.auto_paging_each end @items .select { |item| item.type == type } .map { |item| InvoiceLineItem.new(self, item) } end |
#is_not_tax_exempt ⇒ Object
Determine if the customer is not exempted from taxes.
126 127 128 |
# File 'lib/reji/invoice.rb', line 126 def is_not_tax_exempt @invoice[:customer_tax_exempt] == 'none' end |
#is_tax_exempt ⇒ Object
Determine if the customer is exempted from taxes.
131 132 133 |
# File 'lib/reji/invoice.rb', line 131 def is_tax_exempt @invoice[:customer_tax_exempt] == 'exempt' end |
#owner ⇒ Object
Get the Stripe model instance.
200 201 202 |
# File 'lib/reji/invoice.rb', line 200 def owner @owner 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(self.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 self.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 ? 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 * (self.percent_off / 100)).round.to_i if self.discount_is_percentage self.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] ? @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 + self.raw_starting_balance 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(self.raw_starting_balance) end |
#subscriptions ⇒ Object
Get all of the “subscription” line items.
146 147 148 |
# File 'lib/reji/invoice.rb', line 146 def subscriptions self.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 self.format_amount(@invoice.tax) 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? self. @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(self.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: self.owner, user: self.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 |