Introduce tax totals in Order::Updater

This commit is contained in:
Matt-Yorkley
2021-01-23 05:10:29 +00:00
parent 719b65138f
commit ec7d1d8133
2 changed files with 36 additions and 12 deletions

View File

@@ -5,7 +5,7 @@ module OrderManagement
class Updater
attr_reader :order
delegate :payments, :line_items, :adjustments, :shipments, to: :order
delegate :payments, :line_items, :adjustments, :all_adjustments, :shipments, to: :order
def initialize(order)
@order = order
@@ -58,6 +58,11 @@ module OrderManagement
def update_adjustment_total
order.adjustment_total = adjustments.eligible.sum(:amount)
order.additional_tax_total = all_adjustments.tax.additional.sum(:amount)
order.included_tax_total = order.line_item_adjustments.tax.sum(:included_tax) +
all_adjustments.enterprise_fee.sum(:included_tax) +
adjustments.shipping.sum(:included_tax) +
adjustments.admin.sum(:included_tax)
end
def update_order_total
@@ -70,6 +75,8 @@ module OrderManagement
shipment_state: order.shipment_state,
item_total: order.item_total,
adjustment_total: order.adjustment_total,
included_tax_total: order.included_tax_total,
additional_tax_total: order.additional_tax_total,
payment_total: order.payment_total,
total: order.total,
updated_at: Time.zone.now

View File

@@ -10,19 +10,35 @@ module OrderManagement
before { allow(order).to receive(:backordered?) { false } }
it "updates totals" do
allow(order).to receive_message_chain(:payments, :completed, :sum).and_return(10)
context "updating order totals" do
before do
2.times { create(:line_item, order: order, price: 10) }
end
line_items = [double(amount: 10), double(amount: 20)]
allow(order).to receive_messages line_items: line_items
it "updates payment totals" do
allow(order).to receive_message_chain(:payments, :completed, :sum).and_return(10)
allow(order).to receive_message_chain(:adjustments, :eligible, :sum).and_return(-10)
updater.update_totals
expect(order.payment_total).to eq(10)
end
updater.update_totals
expect(order.payment_total).to eq 10
expect(order.item_total).to eq 30
expect(order.adjustment_total).to eq(-10)
expect(order.total).to eq 20
it "updates item total" do
updater.update_item_total
expect(order.item_total).to eq(20)
end
it "updates adjustment totals" do
allow(order).to receive_message_chain(:adjustments, :eligible, :sum).and_return(-10)
allow(order).to receive_message_chain(:all_adjustments, :tax, :additional, :sum).and_return(20)
allow(order).to receive_message_chain(:all_adjustments, :enterprise_fee, :sum).and_return(10)
allow(order).to receive_message_chain(:adjustments, :shipping, :sum).and_return(5)
allow(order).to receive_message_chain(:adjustments, :admin, :sum).and_return(2)
updater.update_adjustment_total
expect(order.adjustment_total).to eq(-10)
expect(order.additional_tax_total).to eq(20)
expect(order.included_tax_total).to eq(17)
end
end
context "updating shipment state" do
@@ -88,7 +104,7 @@ module OrderManagement
allow(order).to receive_messages shipments: [shipment]
expect(shipment).to receive(:update!).with(order)
updater.update
updater.update_shipments
end
end
@@ -110,6 +126,7 @@ module OrderManagement
allow(order).to receive_messages shipments: [shipment]
expect(shipment).not_to receive(:update!).with(order)
expect(updater).not_to receive(:update_shipments).with(order)
updater.update
end
end