From 56a01194cdfa06c3d79aa094d95759684f35b1ae Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sat, 20 Feb 2021 13:06:02 +0000 Subject: [PATCH] Update shipping tax adjustments specs --- spec/models/spree/adjustment_spec.rb | 132 ++++++++++++++++++++------- 1 file changed, 99 insertions(+), 33 deletions(-) diff --git a/spec/models/spree/adjustment_spec.rb b/spec/models/spree/adjustment_spec.rb index f1a580c234..3b37c155e7 100644 --- a/spec/models/spree/adjustment_spec.rb +++ b/spec/models/spree/adjustment_spec.rb @@ -197,11 +197,17 @@ module Spree end describe "Shipment adjustments" do + let(:zone) { create(:zone_with_member) } + let(:inclusive_tax) { true } + let(:tax_rate) { + create(:tax_rate, included_in_price: inclusive_tax, zone: zone, amount: 0.25) + } + let(:tax_category) { create(:tax_category, name: "Shipping", tax_rates: [tax_rate] ) } let(:hub) { create(:distributor_enterprise, charges_sales_tax: true) } let(:order) { create(:order, distributor: hub) } let(:line_item) { create(:line_item, order: order) } - let(:shipping_method) { create(:shipping_method_with, :flat_rate) } + let(:shipping_method) { create(:shipping_method_with, :flat_rate, tax_category: tax_category) } let(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order) } describe "the shipping charge" do @@ -212,54 +218,114 @@ module Spree end end - describe "when tax on shipping is disabled" do + context "with tax" do before do - allow(Config).to receive(:shipment_inc_vat).and_return(false) + allow(order).to receive(:tax_zone) { zone } end - it "records 0% tax on shipment adjustments" do - allow(Config).to receive(:shipping_tax_rate).and_return(0) - order.shipments = [shipment] + context "when the shipment has an inclusive tax rate" do + it "calculates the shipment tax from the tax rate" do + order.shipments = [shipment] + order.create_tax_charge! + order.update_totals - expect(order.shipment_adjustments.first.included_tax).to eq(0) + # Finding the tax included in an amount that's already inclusive of tax: + # total - ( total / (1 + rate) ) + # 50 - ( 50 / (1 + 0.25) ) + # = 10 + expect(order.shipment_adjustments.tax.first.amount).to eq(10) + expect(order.shipment_adjustments.tax.first.included).to eq true + + expect(shipment.reload.cost).to eq(50) + expect(shipment.included_tax_total).to eq(10) + expect(shipment.additional_tax_total).to eq(0) + + expect(order.included_tax_total).to eq(10) + expect(order.additional_tax_total).to eq(0) + end end - it "records 0% tax on shipments when a rate is set but shipment_inc_vat is false" do - allow(Config).to receive(:shipping_tax_rate).and_return(0.25) - order.shipments = [shipment] + context "when the shipment has an added tax rate" do + let(:inclusive_tax) { false } - expect(order.shipment_adjustments.first.included_tax).to eq(0) - end - end + # Current behaviour. Will be replaced by the pending test below + it "records the tax on the order's adjustments" do + order.shipments = [shipment] + order.create_tax_charge! + order.update_totals - describe "when tax on shipping is enabled" do - before do - allow(Config).to receive(:shipment_inc_vat).and_return(true) + expect(order.shipment_adjustments.tax.count).to be_zero + + # Finding the added tax for an amount: + # total * rate + # 50 * 0.25 + # = 12.5 + expect(order.adjustments.tax.first.amount).to eq(12.50) + expect(order.adjustments.tax.first.included).to eq false + + expect(shipment.reload.cost).to eq(50) + expect(shipment.included_tax_total).to eq(0) + expect(shipment.additional_tax_total).to eq(0) + + expect(order.included_tax_total).to eq(0) + expect(order.additional_tax_total).to eq(12.50) + end + + xit "records the tax on the shipment's adjustments" do + order.shipments = [shipment] + order.create_tax_charge! + order.update_totals + + # Finding the added tax for an amount: + # total * rate + # 50 * 0.25 + # = 12.5 + expect(order.shipment_adjustments.tax.first.amount).to eq(12.50) + expect(order.shipment_adjustments.tax.first.included).to eq false + + expect(shipment.reload.cost).to eq(50) + expect(shipment.included_tax_total).to eq(0) + expect(shipment.additional_tax_total).to eq(12.50) + + expect(order.included_tax_total).to eq(0) + expect(order.additional_tax_total).to eq(12.50) + end end - it "takes the shipment adjustment tax included from the system setting" do - allow(Config).to receive(:shipping_tax_rate).and_return(0.25) - order.shipments = [shipment] + context "when the distributor does not charge sales tax" do + it "records 0% tax on shipments" do + order.distributor.update! charges_sales_tax: false + order.shipments = [shipment] + order.create_tax_charge! + order.update_totals - # Finding the tax included in an amount that's already inclusive of tax: - # total - ( total / (1 + rate) ) - # 50 - ( 50 / (1 + 0.25) ) - # = 10 - expect(order.shipment_adjustments.first.included_tax).to eq(10.00) + expect(order.shipment_adjustments.tax.count).to be_zero + + expect(shipment.reload.cost).to eq(50) + expect(shipment.included_tax_total).to eq(0) + expect(shipment.additional_tax_total).to eq(0) + + expect(order.included_tax_total).to eq(0) + expect(order.additional_tax_total).to eq(0) + end end - it "records 0% tax on shipments when shipping_tax_rate is not set" do - allow(Config).to receive(:shipping_tax_rate).and_return(0) - order.shipments = [shipment] + context "when the shipment has no applicable tax rate" do + it "records 0% tax on shipments" do + allow(shipment).to receive(:tax_category) { nil } + order.shipments = [shipment] + order.create_tax_charge! + order.update_totals - expect(order.shipment_adjustments.first.included_tax).to eq(0) - end + expect(order.shipment_adjustments.tax.count).to be_zero - it "records 0% tax on shipments when the distributor does not charge sales tax" do - order.distributor.update! charges_sales_tax: false - order.shipments = [shipment] + expect(shipment.reload.cost).to eq(50) + expect(shipment.included_tax_total).to eq(0) + expect(shipment.additional_tax_total).to eq(0) - expect(order.shipment_adjustments.first.included_tax).to eq(0) + expect(order.included_tax_total).to eq(0) + expect(order.additional_tax_total).to eq(0) + end end end end