1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-26 20:54:21 +01:00
fab-manager/app/models/coupon.rb

43 lines
997 B
Ruby
Raw Normal View History

2016-08-03 17:25:00 +02:00
class Coupon < ActiveRecord::Base
has_many :invoices
2016-08-03 17:25:00 +02:00
after_commit :create_stripe_coupon, on: [:create]
after_commit :delete_stripe_coupon, on: [:destroy]
2016-08-04 18:13:19 +02:00
validates :name, presence: true
2016-08-03 17:25:00 +02:00
validates :code, presence: true
2016-08-04 18:13:19 +02:00
validates :code, format: { with: /\A[A-Z0-9]+\z/ ,message: 'only caps letters and numbers'}
validates :code, uniqueness: true
2016-08-03 17:25:00 +02:00
validates :percent_off, presence: true
validates :percent_off, :inclusion => 0..100
2016-08-08 12:25:27 +02:00
def safe_destroy
if self.invoices.size == 0
destroy
else
false
end
end
def status
if not active?
'disabled'
elsif (!valid_until.nil?) and valid_until.at_end_of_day < DateTime.now
'expired'
elsif (!max_usages.nil?) and invoices.count >= max_usages
'sold_out'
else
'active'
end
end
def create_stripe_coupon
StripeWorker.perform_async(:create_stripe_coupon, id)
end
def delete_stripe_coupon
StripeWorker.perform_async(:delete_stripe_coupon, code)
end
2016-08-03 17:25:00 +02:00
end