1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-12 06:54:19 +01:00

[to test] compute prices/invoices using cash coupons

This commit is contained in:
Sylvain 2016-11-24 17:57:48 +01:00
parent 0a08c4c32d
commit 63b0f0c061
6 changed files with 70 additions and 28 deletions

View File

@ -153,7 +153,9 @@ class Invoice < ActiveRecord::Base
if avoir.coupon.type == 'percent_off' if avoir.coupon.type == 'percent_off'
discount = avoir.total * avoir.coupon.percent_off / 100.0 discount = avoir.total * avoir.coupon.percent_off / 100.0
elsif avoir.coupon.type == 'amount_off' elsif avoir.coupon.type == 'amount_off'
discount = avoit.coupon.amount_off discount = avoir.coupon.amount_off
else
raise InvalidCouponError
end end
avoir.total -= discount avoir.total -= discount
end end

View File

@ -134,16 +134,25 @@ class Reservation < ActiveRecord::Base
# === Coupon === # === Coupon ===
unless coupon_code.nil? unless coupon_code.nil?
cp = Coupon.find_by(code: coupon_code) @coupon = Coupon.find_by(code: coupon_code)
if not cp.nil? and cp.status(user.id) == 'active' if not @coupon.nil? and @coupon.status(user.id) == 'active'
@coupon = cp
total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+) total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
discount = 0
if @coupon.type == 'percent_off'
discount = (total * @coupon.percent_off / 100).to_i
elsif @coupon.type == 'amount_off'
discount = @coupon.amount_off
else
raise InvalidCouponError
end
unless on_site unless on_site
invoice_items << Stripe::InvoiceItem.create( invoice_items << Stripe::InvoiceItem.create(
customer: user.stp_customer_id, customer: user.stp_customer_id,
amount: -(total * cp.percent_off / 100).to_i, amount: -discount,
currency: Rails.application.secrets.stripe_currency, currency: Rails.application.secrets.stripe_currency,
description: "coupon #{cp.code} - reservation" description: "coupon #{@coupon.code} - reservation"
) )
end end
else else
@ -395,7 +404,7 @@ class Reservation < ActiveRecord::Base
total += plan.amount total += plan.amount
end end
if @coupon if @coupon
total = (total - (total * @coupon.percent_off / 100.0)).to_i total = CouponApplyService.new.(total, @coupon, user.id)
end end
wallet_amount = (user.wallet.amount * 100).to_i wallet_amount = (user.wallet.amount * 100).to_i
@ -423,7 +432,7 @@ class Reservation < ActiveRecord::Base
unless coupon_code.nil? unless coupon_code.nil?
cp = Coupon.find_by(code: coupon_code) cp = Coupon.find_by(code: coupon_code)
if not cp.nil? and cp.status(user.id) == 'active' if not cp.nil? and cp.status(user.id) == 'active'
total = total - (total * cp.percent_off / 100.0) total = CouponApplyService.new.(total, cp, user.id)
self.invoice.coupon_id = cp.id self.invoice.coupon_id = cp.id
else else
raise InvalidCouponError raise InvalidCouponError

View File

@ -25,15 +25,24 @@ class Subscription < ActiveRecord::Base
invoice_items = [] invoice_items = []
unless coupon_code.nil? unless coupon_code.nil?
cp = Coupon.find_by(code: coupon_code) @coupon = Coupon.find_by(code: coupon_code)
if not cp.nil? and cp.status(user.id) == 'active' if not @coupon.nil? and @coupon.status(user.id) == 'active'
@coupon = cp
total = plan.amount total = plan.amount
discount = 0
if @coupon.type == 'percent_off'
discount = (total * @coupon.percent_off / 100).to_i
elsif @coupon.type == 'amount_off'
discount = @coupon.amount_off
else
raise InvalidCouponError
end
invoice_items << Stripe::InvoiceItem.create( invoice_items << Stripe::InvoiceItem.create(
customer: user.stp_customer_id, customer: user.stp_customer_id,
amount: -(total * cp.percent_off / 100.0).to_i, amount: -discount,
currency: Rails.application.secrets.stripe_currency, currency: Rails.application.secrets.stripe_currency,
description: "coupon #{cp.code} - subscription" description: "coupon #{@coupon.code} - subscription"
) )
else else
raise InvalidCouponError raise InvalidCouponError
@ -159,11 +168,11 @@ class Subscription < ActiveRecord::Base
total = plan.amount total = plan.amount
unless coupon_code.nil? unless coupon_code.nil?
cp = Coupon.find_by(code: coupon_code) @coupon = Coupon.find_by(code: coupon_code)
if not cp.nil? and cp.status(user.id) == 'active'
@coupon = cp unless @coupon.nil?
coupon_id = cp.id total = CouponApplyService.new.(plan.amount, @coupon, user.id)
total = plan.amount - (plan.amount * cp.percent_off / 100.0) coupon_id = @coupon.id
end end
end end
@ -293,7 +302,7 @@ class Subscription < ActiveRecord::Base
def get_wallet_amount_debit def get_wallet_amount_debit
total = plan.amount total = plan.amount
if @coupon if @coupon
total = (total - (total * @coupon.percent_off / 100.0)).to_i total = CouponApplyService.new.(total, @coupon, user.id)
end end
wallet_amount = (user.wallet.amount * 100).to_i wallet_amount = (user.wallet.amount * 100).to_i
return wallet_amount >= total ? total : wallet_amount return wallet_amount >= total ? total : wallet_amount

View File

@ -143,7 +143,15 @@ module PDF
# subtract the coupon, if any # subtract the coupon, if any
unless invoice.coupon_id.nil? unless invoice.coupon_id.nil?
cp = invoice.coupon cp = invoice.coupon
discount = 0
if cp.type == 'percent_off'
discount = total_calc * cp.percent_off / 100.0 discount = total_calc * cp.percent_off / 100.0
elsif cp.type == 'amount_off'
discount = cp.amount_off
else
raise InvalidCouponError
end
total_calc = total_calc - discount total_calc = total_calc - discount
# add a row for the coupon # add a row for the coupon

View File

@ -1,11 +1,25 @@
class CouponApplyService class CouponApplyService
def call(total, coupon_code, user_id = nil) ##
# Apply the provided coupon, if active, to the given price. Usability tests will be run depending on the
# provided parameters.
# If no coupon/coupon code or if the code does not match, return origin price without change
#
# @param total {Number} invoice total, before any coupon is applied
# @param coupon {String|Coupon} Coupon's code OR Coupon object
# @param user_id {Number} user's id against the coupon will be tested for usability
# @return {Number}
##
def call(total, coupon, user_id = nil)
price = total price = total
# if no coupon code or if code does not match, return origin price without change if coupon.instance_of? Coupon
unless coupon_code.nil? _coupon = coupon
_coupon = Coupon.find_by(code: coupon_code) elsif coupon.instance_of? String
if not _coupon.nil? and _coupon.status(user_id) == 'active' _coupon = Coupon.find_by(code: coupon)
end
unless coupon.nil?
if _coupon.status(user_id, total) == 'active'
if _coupon.type == 'percent_off' if _coupon.type == 'percent_off'
price = price - (price * _coupon.percent_off / 100.0) price = price - (price * _coupon.percent_off / 100.0)
elsif _coupon.type == 'amount_off' elsif _coupon.type == 'amount_off'

View File

@ -127,7 +127,7 @@ class StatisticService
if sub if sub
ca = i.amount.to_i / 100.0 ca = i.amount.to_i / 100.0
unless i.invoice.coupon_id.nil? unless i.invoice.coupon_id.nil?
ca = ca - ( ca * i.invoice.coupon.percent_off / 100.0 ) ca = CouponApplyService.new(ca, i.invoice.coupon)
end end
u = sub.user u = sub.user
p = sub.plan p = sub.plan
@ -353,7 +353,7 @@ class StatisticService
end end
# subtract coupon discount from invoices and refunds # subtract coupon discount from invoices and refunds
unless invoice.coupon_id.nil? unless invoice.coupon_id.nil?
ca = ca - ( ca * invoice.coupon.percent_off / 100.0 ) ca = CouponApplyService.new(ca, invoice.coupon)
end end
# divide the result by 100 to convert from centimes to monetary unit # divide the result by 100 to convert from centimes to monetary unit
ca == 0 ? ca : ca / 100.0 ca == 0 ? ca : ca / 100.0
@ -366,7 +366,7 @@ class StatisticService
end end
# subtract coupon discount from the refund # subtract coupon discount from the refund
unless invoice.coupon_id.nil? unless invoice.coupon_id.nil?
ca = ca - ( ca * invoice.coupon.percent_off / 100.0 ) ca = CouponApplyService.new(ca, invoice.coupon)
end end
ca == 0 ? ca : ca / 100.0 ca == 0 ? ca : ca / 100.0
end end