Class: Tang::Invoice
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tang::Invoice
- Defined in:
- app/models/tang/invoice.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#charge ⇒ Object
scope :paid, -> { joins(:charge) }.
- #period_end ⇒ Object
- #period_end=(val) ⇒ Object
- #period_start ⇒ Object
- #period_start=(val) ⇒ Object
- #status ⇒ Object
- #update_from_stripe(stripe_invoice) ⇒ Object
Class Method Details
.from_stripe(stripe_invoice) ⇒ Object
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 72 73 74 75 |
# File 'app/models/tang/invoice.rb', line 46 def self.from_stripe(stripe_invoice) customer = Tang.customer_class.find_by(stripe_id: stripe_invoice.customer) subscription = Subscription.find_by(stripe_id: stripe_invoice.subscription) invoice = Invoice.find_or_create_by(stripe_id: stripe_invoice.id) do |i| i.subscription = subscription i.customer = customer i.period_start = DateTime.strptime(stripe_invoice.period_start.to_s, '%s') i.period_end = DateTime.strptime(stripe_invoice.period_end.to_s, '%s') i.date = DateTime.strptime(stripe_invoice.created.to_s, '%s') # changed from date i.currency = stripe_invoice.currency i.subtotal = stripe_invoice.subtotal # i.tax_percent = stripe_invoice.tax_percent # removed from api, replaced with tax rates i.tax = stripe_invoice.tax i.total = stripe_invoice.total i.amount_due = stripe_invoice.amount_due i.invoice_pdf = stripe_invoice.invoice_pdf if stripe_invoice.discount.present? # coupon = Coupon.from_stripe(stripe_invoice.discount.coupon) coupon = Coupon.find_by(stripe_id: stripe_invoice.discount.coupon.id) i.coupon = coupon end end if invoice.update_from_stripe(stripe_invoice) invoice.save end return invoice end |
.search(query) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/tang/invoice.rb', line 106 def self.search(query) invoices = Invoice.none if query.present? q = "%#{query.downcase}%" customer_table = connection.quote_table_name(Customer.table_name) invoices = Invoice.joins("left join tang_charges on tang_invoices.id = tang_charges.invoice_id"). joins("left join tang_subscriptions on tang_subscriptions.id = tang_invoices.subscription_id"). joins("left join #{customer_table} on #{customer_table}.id = tang_invoices.customer_id"). where("lower(tang_charges.stripe_id) like ? or lower(tang_subscriptions.stripe_id) like ? or lower(#{customer_table}.stripe_id) like ?", q, q, q). distinct end return invoices end |
Instance Method Details
#charge ⇒ Object
scope :paid, -> { joins(:charge) }
14 15 16 |
# File 'app/models/tang/invoice.rb', line 14 def charge self.charges.order(:created_at).last end |
#period_end ⇒ Object
26 27 28 29 30 31 32 |
# File 'app/models/tang/invoice.rb', line 26 def period_end if subscription.present? self[:period_end] || subscription.plan.period_days_from(period_start) else self.period_start end end |
#period_end=(val) ⇒ Object
34 35 36 |
# File 'app/models/tang/invoice.rb', line 34 def period_end=(val) self[:period_end] = val end |
#period_start ⇒ Object
18 19 20 |
# File 'app/models/tang/invoice.rb', line 18 def period_start self[:period_start] || created_at end |
#period_start=(val) ⇒ Object
22 23 24 |
# File 'app/models/tang/invoice.rb', line 22 def period_start=(val) self[:period_start] = val end |
#status ⇒ Object
38 39 40 41 42 43 44 |
# File 'app/models/tang/invoice.rb', line 38 def status if charge.present? 'paid' else 'unpaid' end end |
#update_from_stripe(stripe_invoice) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/tang/invoice.rb', line 77 def update_from_stripe(stripe_invoice) changed = false stripe_period_start = DateTime.strptime(stripe_invoice.period_start.to_s, '%s') if self[:period_start] != stripe_period_start self.period_start = stripe_period_start changed = true end stripe_period_end = DateTime.strptime(stripe_invoice.period_end.to_s, '%s') if self[:period_end] != stripe_period_end self.period_end = stripe_period_end changed = true end stripe_created = DateTime.strptime(stripe_invoice.created.to_s, '%s') if self.date != stripe_created self.date = stripe_created changed = true end if self.invoice_pdf != stripe_invoice.invoice_pdf self.invoice_pdf = stripe_invoice.invoice_pdf changed = true end return changed end |