2016-11-23 17:17:34 +01:00
|
|
|
class CouponApplyService
|
2016-11-24 11:33:45 +01:00
|
|
|
def call(total, coupon_code, user_id = nil)
|
2016-11-23 17:17:34 +01:00
|
|
|
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)
|
2016-11-24 11:33:45 +01:00
|
|
|
if not _coupon.nil? and _coupon.status(user_id) == 'active'
|
2016-11-23 17:17:34 +01:00
|
|
|
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
|