Skip to content

Instantly share code, notes, and snippets.

@FotoVerite
Created August 16, 2019 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FotoVerite/2c976f8533a6d44f52b311a8ac38d3d5 to your computer and use it in GitHub Desktop.
Save FotoVerite/2c976f8533a6d44f52b311a8ac38d3d5 to your computer and use it in GitHub Desktop.
def apply_coupons
applied_coupons = []
active_coupons = family.family_coupons.active.order(:created_at).map(&:coupon).flatten
total = self.subtotal
while !total.zero? && !active_coupons.empty?
active_coupons.filter { |c| c.coupon_type == "permanent_percent" }.each do |coupon|
total = total * coupon.amount / 100.0.to_i
applied_coupons.push(coupon)
active_coupons -= [coupon]
end
active_coupons.filter { |c| c.coupon_type == "percent" }.each do |coupon|
total = total * coupon.amount / 100.0.to_i
applied_coupons.push(coupon)
active_coupons -= [coupon]
end
active_coupons.filter { |c| c.coupon_type == "amount_in_cents" }.each do |coupon|
if total >= 1
total -= coupon.amount
applied_coupons.push(coupon)
active_coupons -= [coupon]
end
end
end
return total
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment