diff --git a/app/services/voucher_adjustments_service.rb b/app/services/voucher_adjustments_service.rb index 13f818a8dc5..cfc1bab994b 100644 --- a/app/services/voucher_adjustments_service.rb +++ b/app/services/voucher_adjustments_service.rb @@ -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 @@ -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 diff --git a/spec/services/voucher_adjustments_service_spec.rb b/spec/services/voucher_adjustments_service_spec.rb index 555ab746bd1..77e3cc85960 100644 --- a/spec/services/voucher_adjustments_service_spec.rb +++ b/spec/services/voucher_adjustments_service_spec.rb @@ -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