Class: Subscription

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/subscription.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_and_bill_recurring_subscriptionsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/subscription.rb', line 59

def find_and_bill_recurring_subscriptions
  #find_all_subscriptions need billing
  @billable_subscriptions = Subscription.active.all_paid.all_billable_subscriptions(Date.today)
  for subscription in @billable_subscriptions
   
    subscription.downgrade if subscription.downgrade_to_plan.present?
    begin
      subscription.bill!(subscription.balance)
    rescue
      subscription.billing_failed
    end
  end

end

.find_and_invoice_subscriptionsObject



74
75
76
77
78
79
# File 'app/models/subscription.rb', line 74

def find_and_invoice_subscriptions
  @invoiceable_subscriptions = Subscription.active.all_paid.all_invoiceable(Date.today)
  for subscription in @invoiceable_subscriptions
    subscription.invoice!
  end
end

Instance Method Details

#bill!(bill_amount = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'app/models/subscription.rb', line 134

def bill!(bill_amount = nil)
  bill_amount ||= balance
  if charge_amount(bill_amount)
    set_next_billing_date
    set_status_active if balance == 0.0
    send_billing_successful_email(bill_amount)
    return true
  end

end

#billing_failedObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/subscription.rb', line 167

def billing_failed
  set_status_overdue

  if billing_date == Date.today
    send_billing_failed_email
  end

  if billing_date == Date.today - Saasaparilla::CONFIG["grace_period"].days + 1
    send_pending_cancellation_notice_email
  end

  if billing_date < Date.today - Saasaparilla::CONFIG["grace_period"].days
    cancel
  end
end

#cancelObject



155
156
157
158
# File 'app/models/subscription.rb', line 155

def cancel
  set_canceled
  send_subscription_canceled_email
end

#downgradeObject



86
87
88
89
90
91
92
93
# File 'app/models/subscription.rb', line 86

def downgrade
  self.plan = downgrade_to_plan
  self.downgrade_to_plan = nil
  self.balance = plan.price
  
  self.save!
 
end

#downgrade_plan_to(plan_id) ⇒ Object



95
96
97
98
99
100
101
102
# File 'app/models/subscription.rb', line 95

def downgrade_plan_to(plan_id)

  if self.update_attributes(:downgrade_to_plan => Plan.find(plan_id))
    return true
  else
    return false
  end
end

#initial_billObject



125
126
127
128
129
130
131
132
# File 'app/models/subscription.rb', line 125

def initial_bill
  if Saasaparilla::CONFIG["trial_period"] > 0
    set_next_billing_date(Saasaparilla::CONFIG["trial_period"])
  else
    bill!
  end
  
end

#invoice!Object



146
147
148
149
150
151
152
153
# File 'app/models/subscription.rb', line 146

def invoice!

  add_to_balance(plan.price)
  create_invoice
  self.update_attributes!(:invoiced_on => Date.today)
  #if invoice_subscription
  #  set_next_invoice_date()
end

#last_transaction_amountObject



113
114
115
116
117
118
119
120
# File 'app/models/subscription.rb', line 113

def last_transaction_amount
  @transaction = transactions.recent.first
  if @transaction.nil?
    "-"
  else
    @transaction.amount
  end
end

#last_transaction_dateObject



104
105
106
107
108
109
110
111
# File 'app/models/subscription.rb', line 104

def last_transaction_date
  @transaction = transactions.recent.first
  if @transaction.nil?
    "-"
  else
    @transaction.created_at.to_s(:month_day_year)
  end
end

#plan_nameObject



122
123
124
# File 'app/models/subscription.rb', line 122

def plan_name
  plan.name
end

#reactivate!Object



160
161
162
163
164
165
# File 'app/models/subscription.rb', line 160

def reactivate!
  self.update_attributes(:billing_date => nil)
  self.balance = 0
  add_to_balance(plan.price)
  bill!
end