1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/models/coupon.rb

112 lines
3.7 KiB
Ruby
Raw Normal View History

2019-01-16 11:07:09 +01:00
# frozen_string_literal: true
# Coupon is a textual code associated with a discount rate or an amount of discount
2020-03-25 10:16:47 +01:00
class Coupon < ApplicationRecord
2022-12-28 17:51:27 +01:00
has_many :invoices, dependent: :restrict_with_error
has_many :payment_schedule, dependent: :restrict_with_error
has_many :orders, dependent: :restrict_with_error
has_many :cart_item_coupons, class_name: 'CartItem::Coupon', dependent: :destroy
2016-08-03 17:25:00 +02:00
after_create :create_gateway_coupon
before_destroy :delete_gateway_coupon
2016-08-04 18:13:19 +02:00
validates :name, presence: true
2016-08-03 17:25:00 +02:00
validates :code, presence: true
2022-12-28 17:51:27 +01:00
validates :code, format: { with: /\A[A-Z0-9\-]+\z/, message: I18n.t('coupon.code_format_error') }
validates :code, uniqueness: true
2016-08-08 15:43:02 +02:00
validates :validity_per_user, presence: true
validates :validity_per_user, inclusion: { in: %w[once forever] }
validates_with CouponDiscountValidator
2016-12-13 12:01:34 +01:00
validates_with CouponExpirationValidator
2016-08-03 17:25:00 +02:00
scope :disabled, -> { where(active: false) }
scope :expired, -> { where('valid_until IS NOT NULL AND valid_until < ?', Time.current) }
scope :sold_out, lambda {
joins(:invoices).select('coupons.*, COUNT(invoices.id) as invoices_count').group('coupons.id')
.where.not(max_usages: nil).having('COUNT(invoices.id) >= coupons.max_usages')
}
scope :active, lambda {
joins('LEFT OUTER JOIN invoices ON invoices.coupon_id = coupons.id')
.select('coupons.*, COUNT(invoices.id) as invoices_count')
.group('coupons.id')
.where('active = true AND (valid_until IS NULL OR valid_until >= ?)', Time.current)
.having('COUNT(invoices.id) < coupons.max_usages OR coupons.max_usages IS NULL')
}
2016-08-08 12:25:27 +02:00
def safe_destroy
if usages.zero?
2016-08-08 12:25:27 +02:00
destroy
else
false
end
end
def usages
2021-02-08 15:28:47 +01:00
invoices.count
end
##
# Check the status of the current coupon. The coupon:
# - may have been disabled by an admin,
# - may has expired because the validity date has been reached,
# - may have been used the maximum number of times it was allowed
# - may have already been used by the provided user, if the coupon is configured to allow only one use per user,
# - may exceed the current cart's total amount, if the coupon is configured to discount an amount (and not a percentage)
# - may be available for use
# @param [user_id] {Number} if provided and if the current coupon's validity_per_user == 'once', check that the coupon
# was already used by the provided user
# @param [amount] {Number} if provided and if the current coupon's type == 'amont_off' check that the coupon
# does not exceed the cart total price
# @return {String} status identifier
##
def status(user_id = nil, amount = nil)
if !active?
'disabled'
elsif !valid_until.nil? && valid_until.at_end_of_day < Time.current
'expired'
elsif !max_usages.nil? && invoices.count >= max_usages
'sold_out'
elsif !user_id.nil? && validity_per_user == 'once' && users_ids.include?(user_id.to_i)
'already_used'
elsif !amount.nil? && type == 'amount_off' && amount_off > amount.to_f
'amount_exceeded'
else
'active'
end
end
def type
2019-01-16 11:07:09 +01:00
if amount_off.nil? && !percent_off.nil?
'percent_off'
2019-01-16 11:07:09 +01:00
elsif percent_off.nil? && !amount_off.nil?
'amount_off'
end
end
def users
# compact to user removed
2022-10-26 09:37:31 +02:00
invoices.map(&:user).compact.uniq(&:id)
end
def users_ids
users.map(&:id)
end
2016-08-16 18:12:13 +02:00
def send_to(user_id)
NotificationCenter.call type: 'notify_member_about_coupon',
receiver: User.find(user_id),
attached_object: self
end
2016-12-13 12:01:34 +01:00
private
def create_gateway_coupon
PaymentGatewayService.new.create_coupon(id)
end
def delete_gateway_coupon
PaymentGatewayService.new.delete_coupon(id)
end
2016-08-03 17:25:00 +02:00
end