mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Ensuring that new standing_line_items are added to orders and totals are updated
This commit is contained in:
@@ -35,6 +35,15 @@ class StandingOrderForm
|
||||
updateable_line_items(sli).update_all(quantity: sli.quantity) # Avoid validation
|
||||
end
|
||||
|
||||
new_standing_line_items.each do |sli|
|
||||
future_and_undated_orders.each do |order|
|
||||
line_item = order.line_items.create(variant_id: sli.variant_id, quantity: 0)
|
||||
line_item.update_attribute(:quantity, sli.quantity) # Avoid validation
|
||||
end
|
||||
end
|
||||
|
||||
future_and_undated_orders.each(&:save)
|
||||
|
||||
standing_order.save
|
||||
end
|
||||
end
|
||||
@@ -50,7 +59,7 @@ class StandingOrderForm
|
||||
|
||||
def future_and_undated_orders
|
||||
return @future_and_undated_orders unless @future_and_undated_orders.nil?
|
||||
@future_and_undated_orders = orders.joins(:order_cycle).merge(OrderCycle.not_closed)
|
||||
@future_and_undated_orders = orders.joins(:order_cycle).merge(OrderCycle.not_closed).readonly(false)
|
||||
end
|
||||
|
||||
def create_order_for(order_cycle_id)
|
||||
@@ -121,7 +130,11 @@ class StandingOrderForm
|
||||
end
|
||||
|
||||
def changed_standing_line_items
|
||||
standing_line_items.select(&:changed?)
|
||||
standing_line_items.select{ |sli| sli.changed? && sli.persisted? }
|
||||
end
|
||||
|
||||
def new_standing_line_items
|
||||
standing_line_items.select(&:new_record?)
|
||||
end
|
||||
|
||||
def updateable_line_items(sli)
|
||||
|
||||
@@ -210,11 +210,35 @@ module OpenFoodNetwork
|
||||
let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 4} ] } }
|
||||
let(:form) { StandingOrderForm.new(standing_order, params) }
|
||||
|
||||
before { form.save }
|
||||
before { form.send(:initialise_orders!) }
|
||||
|
||||
it "updates the quantity on all orders" do
|
||||
it "updates the line_item quantities and totals on all orders" do
|
||||
expect(standing_order.orders.first.reload.total.to_f).to eq 59.97
|
||||
form.save
|
||||
line_items = Spree::LineItem.where(order_id: standing_order.orders, variant_id: sli.variant_id)
|
||||
expect(line_items.map(&:quantity)).to eq [4]
|
||||
expect(standing_order.orders.first.reload.total.to_f).to eq 119.94
|
||||
end
|
||||
end
|
||||
|
||||
describe "adding a new line item" do
|
||||
let!(:standing_order) { create(:standing_order_with_items) }
|
||||
let!(:variant) { create(:variant) }
|
||||
let!(:order_cycle) { standing_order.schedule.order_cycles.first }
|
||||
let!(:params) { { standing_line_items_attributes: [ { id: nil, variant_id: variant.id, quantity: 1} ] } }
|
||||
let!(:form) { StandingOrderForm.new(standing_order, params) }
|
||||
|
||||
before do
|
||||
order_cycle.variants << variant
|
||||
form.send(:initialise_orders!)
|
||||
end
|
||||
|
||||
it "add the line item and updates the total on all orders" do
|
||||
expect(standing_order.orders.first.reload.total.to_f).to eq 59.97
|
||||
form.save
|
||||
line_items = Spree::LineItem.where(order_id: standing_order.orders, variant_id: variant.id)
|
||||
expect(line_items.map(&:quantity)).to eq [1]
|
||||
expect(standing_order.orders.first.reload.total.to_f).to eq 79.96
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user