Skip to content

Commit

Permalink
Finish calculation for percentage voucher
Browse files Browse the repository at this point in the history
Add calculation when tax is not included in price
  • Loading branch information
rioug committed May 9, 2023
1 parent b7df2e9 commit 4fb198c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
14 changes: 8 additions & 6 deletions app/services/voucher_adjustments_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ def self.calculate(order)
# We only support one voucher per order right now, we could just loop on voucher_adjustments
adjustment = order.voucher_adjustments.first

# Recalculate value
voucher = adjustment.originator
# TODO: see if we can remove this and do it in handle_tax_excluded_from_price
amount = voucher.compute_amount(order)

# It is quite possible to have an order with both tax included in and tax excluded from price.
# We should be able to caculate the relevant amount apply the current calculation.
#
# For now we just assume it is either all tax included in price or all tax excluded from price.
if order.additional_tax_total.positive?
handle_tax_excluded_from_price(order, amount)
handle_tax_excluded_from_price(order, voucher)
else
handle_tax_included_in_price(order, voucher)
end
Expand All @@ -29,8 +26,13 @@ def self.calculate(order)
adjustment.close
end

def self.handle_tax_excluded_from_price(order, amount)
voucher_rate = amount / order.total
def self.handle_tax_excluded_from_price(order, voucher)
if voucher.voucher_type == Voucher::FLAT_RATE
amount = voucher.compute_amount(order)
voucher_rate = amount / order.total
else
voucher_rate = -voucher.amount / 100
end

# Adding the voucher tax part
tax_amount = voucher_rate * order.additional_tax_total
Expand Down
24 changes: 12 additions & 12 deletions spec/services/voucher_adjustments_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,22 @@
VoucherAdjustmentsService.calculate(order)
end

pending 'includes amount withou tax' do
it 'includes amount without tax' do
adjustment = order.voucher_adjustments.first
# voucher_rate = amount / order.total
# -10 / 161 = -0.062111801
# amount = voucher_rate * (order.total - order.additional_tax_total)
# -0.062111801 * (161 -11) = -9.32
expect(adjustment.amount.to_f).to eq(-9.32)
# voucher_rate = voucher.amount / 100
# 10 / 100 = 0.1
# amount = -voucher_rate * (order.total - order.additional_tax_total)
# -0.1 * (166 -11) = -15.5
expect(adjustment.amount.to_f).to eq(-15.5)
end

pending 'creates a tax adjustment' do
# voucher_rate = amount / order.total
# -10 / 161 = -0.062111801
# amount = voucher_rate * order.additional_tax_total
# -0.0585 * 11 = -0.68
it 'creates a tax adjustment' do
# voucher_rate = voucher.amount / 100
# 10 / 100 = 0.1
# amount = -voucher_rate * order.additional_tax_total
# -0.1 * 11 = -1.1
tax_adjustment = order.voucher_adjustments.second
expect(tax_adjustment.amount.to_f).to eq(-0.68)
expect(tax_adjustment.amount.to_f).to eq(-1.1)
expect(tax_adjustment.label).to match("Tax")
end

Expand Down

0 comments on commit 4fb198c

Please sign in to comment.