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

[bug] when a cash coupon was used, an invalid amount is shown in the statistics

This commit is contained in:
Sylvain 2020-11-04 15:53:40 +01:00
parent 21bd1312bc
commit 7d37174b51
5 changed files with 33 additions and 7 deletions

View File

@ -4,6 +4,7 @@
- Fix a bug: unable to build homepage custom stylesheet
- Fix a bug: unable to access embedded plan views
- Fix a bug: warning message overflow in credit wallet modal
- Fix a bug: when a cash coupon was used, an invalid amount is shown in the statistics
- [TODO DEPLOY] `rails fablab:stripe:plans_prices`
- [TODO DEPLOY] `rails fablab:maintenance:rebuild_stylesheet`

View File

@ -53,8 +53,8 @@ class CouponService
##
# Ventilate the discount of the provided coupon over the given amount proportionately to the invoice's total
# @param total {Number} total amount of the invoice expressed in monetary units
# @param amount {Number} price of the invoice's sub-item expressed in monetary units
# @param total {Number} total amount of the invoice expressed in centimes
# @param amount {Number} price of the invoice's sub-item expressed in centimes
# @param coupon {Coupon} coupon applied to the invoice, amount_off expressed in centimes if applicable
##
def ventilate(total, amount, coupon)
@ -64,7 +64,7 @@ class CouponService
price = amount - (amount * coupon.percent_off / 100.00)
elsif coupon.type == 'amount_off'
ratio = (coupon.amount_off / 100.00) / total
discount = amount * ratio.abs
discount = (amount * ratio.abs) * 100
price = amount - discount
else
raise InvalidCouponError
@ -77,9 +77,9 @@ class CouponService
# Compute the total amount of the given invoice, without the applied coupon
# Invoice.total stores the amount payed by the customer, coupon deducted
# @param invoice {Invoice} invoice object, its total before discount will be computed
# @return {Number} total in centimes
##
def invoice_total_no_coupon(invoice)
total = (invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+) or 0)
total / 100.0
invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+) or 0
end
end

View File

@ -134,9 +134,10 @@ class StatisticService
next unless sub
ca = i.amount.to_i / 100.0
ca = i.amount.to_i
cs = CouponService.new
ca = cs.ventilate(cs.invoice_total_no_coupon(i.invoice), ca, i.invoice.coupon) unless i.invoice.coupon_id.nil?
ca /= 100.00
profile = sub.statistic_profile
p = sub.plan
result.push OpenStruct.new({

View File

@ -25,4 +25,13 @@ cash:
valid_until: <%= 1.year.from_now.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
max_usages: 1
active: true
validity_per_user: once
validity_per_user: once
cash2:
name: Small Cash Code
code: GIME3EUR
amount_off: 300
valid_until: <%= 1.year.from_now.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
max_usages: nil
active: true
validity_per_user: once

View File

@ -1,5 +1,8 @@
# frozen_string_literal: true
require 'test_helper'
# In the following tests, amounts are expressed in centimes, ie. 1000 = 1000 cts = 10,00 EUR
class CouponServiceTest < ActiveSupport::TestCase
setup do
@jdupond = User.find_by(username: 'jdupond')
@ -25,4 +28,16 @@ class CouponServiceTest < ActiveSupport::TestCase
total = CouponService.new.apply(1000, 'XMAS10', @jdupond.id)
assert_equal 1000, total
end
test 'ventilate 15 percent coupon' do
coupon = Coupon.find_by(code: 'SUNNYFABLAB')
total = CouponService.new.ventilate(1000, 500, coupon)
assert_equal 425, total
end
test 'ventilate 100 euros coupon' do
coupon = Coupon.find_by(code: 'GIME3EUR')
total = CouponService.new.ventilate(1000, 500, coupon)
assert_equal 350, total
end
end