1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

[ongoing] using cash coupon in reservation logic

This commit is contained in:
Sylvain 2016-11-23 17:17:34 +01:00
parent c13f640e81
commit 0cb0ff3a06
3 changed files with 30 additions and 6 deletions

View File

@ -149,7 +149,12 @@ class Invoice < ActiveRecord::Base
end
# handle coupon
unless avoir.coupon_id.nil?
discount = avoir.total * avoir.coupon.percent_off / 100.0
discount = avoir.total
if avoir.coupon.type == 'percent_off'
discount = avoir.total * avoir.coupon.percent_off / 100.0
elsif avoir.coupon.type == 'amount_off'
discount = avoit.coupon.amount_off
end
avoir.total -= discount
end
avoir

View File

@ -45,7 +45,7 @@ class Price < ActiveRecord::Base
if machine_credit
hours_available = machine_credit.hours
if !new_plan_being_bought
user_credit = user.users_credits.find_by_credit_id(machine_credit.id)
user_credit = user.users_credits.find_by(credit_id: machine_credit.id)
if user_credit
hours_available = machine_credit.hours - user_credit.hours_used
end
@ -111,10 +111,7 @@ class Price < ActiveRecord::Base
end
# === apply Coupon if any ===
unless coupon_code.nil?
_coupon = Coupon.find_by_code(coupon_code)
_amount = _amount - (_amount * _coupon.percent_off / 100.0)
end
_amount = CouponApplyService.new.(_amount, coupon_code)
# return result
{elements: _elements, total: _amount}

View File

@ -0,0 +1,22 @@
class CouponApplyService
def call(total, coupon_code)
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 _coupon
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