Created
August 16, 2019 22:43
-
-
Save FotoVerite/2c976f8533a6d44f52b311a8ac38d3d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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