Restore adjustments state after updating

We don't know exactly if all adjustments are closed in all
circumstances, so it's better to be conservative until we figure out.
Chances are that non-completed orders have them open.
This commit is contained in:
Pau Perez
2019-01-22 13:22:43 +01:00
parent 8eab3c5ec1
commit f4b790eefa
2 changed files with 33 additions and 2 deletions

View File

@@ -138,11 +138,19 @@ Spree::OrdersController.class_eval do
@order.shipment.ensure_correct_adjustment_with_included_tax if @order.shipment
end
# Sets the adjustments to open to perform the block's action and closes them back again
# Sets the adjustments to open to perform the block's action and restores
# their state to whatever the they had.
def with_open_adjustments
previous_states = @order.adjustments.each_with_object({}) do |adjustment, hash|
hash[adjustment.id] = adjustment.state
end
@order.adjustments.each(&:open)
yield
@order.adjustments.each(&:close)
@order.adjustments.each_with_index do |adjustment, index|
adjustment.update_attribute(:state, previous_states[adjustment.id])
end
end
def discard_empty_line_items

View File

@@ -164,6 +164,18 @@ describe Spree::OrdersController, type: :controller do
"1" => {quantity: "99", id: li.id}
})
end
it "keeps the adjustments' previous state" do
order = subject.current_order(true)
line_item = order.add_variant(create(:simple_product, on_hand: 110).variants.first)
adjustment = create(:adjustment, adjustable: order)
spree_get :update, order: { line_items_attributes: {
"1" => { quantity: "99", id: line_item.id }
}}
expect(adjustment.state).to eq('open')
end
end
describe "removing items from a completed order" do
@@ -203,6 +215,17 @@ describe Spree::OrdersController, type: :controller do
expect(order.adjustment_total).to eq((item_num - 1) * (shipping_fee + payment_fee))
expect(order.shipment.adjustment.included_tax).to eq 0.6
end
it "keeps the adjustments' previous state" do
spree_post :update, {
order: { line_items_attributes: {
"0" => { id: line_item1.id, quantity: 1 },
"1" => { id: line_item2.id, quantity: 0 }
} }
}
expect(order.adjustments.map(&:state)).to eq(['closed', 'closed', 'closed'])
end
end
context "with enterprise fees" do