diff --git a/app/forms/standing_order_form.rb b/app/forms/standing_order_form.rb index 1d26aa9b34..23c06b4303 100644 --- a/app/forms/standing_order_form.rb +++ b/app/forms/standing_order_form.rb @@ -32,13 +32,12 @@ class StandingOrderForm end changed_standing_line_items.each do |sli| - updateable_line_items(sli).update_all(quantity: sli.quantity) # Avoid validation + updateable_line_items(sli).each{ |li| li.update_attributes(quantity: sli.quantity, skip_stock_check: true)} 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 + order.line_items.create(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: true) end end @@ -75,8 +74,7 @@ class StandingOrderForm shipping_method_id: shipping_method_id, }) standing_line_items.each do |sli| - line_item = order.line_items.create(variant_id: sli.variant_id, quantity: 0) - line_item.update_attribute(:quantity, sli.quantity) # Avoid validation + order.line_items.build(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: true) end order.update_attributes(bill_address: bill_address.dup, ship_address: ship_address.dup) order.update_distribution_charge! diff --git a/spec/forms/standing_order_form_spec.rb b/spec/forms/standing_order_form_spec.rb index 0daf3724c3..0819cfcde9 100644 --- a/spec/forms/standing_order_form_spec.rb +++ b/spec/forms/standing_order_form_spec.rb @@ -206,17 +206,36 @@ describe StandingOrderForm do describe "changing the quantity of a line item" do let(:standing_order) { create(:standing_order_with_items) } let(:sli) { standing_order.standing_line_items.first } - let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 4} ] } } - let(:form) { StandingOrderForm.new(standing_order, params) } + let(:variant) { sli.variant } - before { form.send(:initialise_orders!) } + before { variant.update_attribute(:count_on_hand, 2) } - 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 + context "when quantity is less than available stock" do + let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 2} ] } } + let(:form) { StandingOrderForm.new(standing_order, params) } + before { form.send(:initialise_orders!) } + + 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 [2] + expect(standing_order.orders.first.reload.total.to_f).to eq 79.96 + end + end + + context "when quantity is greater than available stock" do + let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 3} ] } } + let(:form) { StandingOrderForm.new(standing_order, params) } + before { form.send(:initialise_orders!) } + + 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 [3] + expect(standing_order.orders.first.reload.total.to_f).to eq 99.95 + end end end