1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/services/coupon_apply_service.rb

22 lines
700 B
Ruby
Raw Normal View History

class CouponApplyService
def call(total, coupon_code, user_id = nil)
price = total
# if no coupon code or if code does not match, return origin price without change
unless coupon_code.nil?
_coupon = Coupon.find_by(code: coupon_code)
if not _coupon.nil? and _coupon.status(user_id) == 'active'
if _coupon.type == 'percent_off'
price = price - (price * _coupon.percent_off / 100.0)
elsif _coupon.type == 'amount_off'
# do not apply cash coupon unless it has a lower amount that the total price
if _coupon.amount_off <= price
price -= _coupon.amount_off
end
end
end
end
price
end
end