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:
parent
c13f640e81
commit
0cb0ff3a06
@ -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
|
||||
|
@ -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}
|
||||
|
22
app/services/coupon_apply_service.rb
Normal file
22
app/services/coupon_apply_service.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user