From e357a7b401f13690b5cb5f7d61d17b2d8d43d552 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 16 Dec 2016 10:38:57 +1100 Subject: [PATCH] StandingOrderForm: Storing problematic orders for reporting to user --- app/forms/standing_order_form.rb | 15 ++++++++++++--- spec/forms/standing_order_form_spec.rb | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/forms/standing_order_form.rb b/app/forms/standing_order_form.rb index 67ec7bc67a..65f0ebc53c 100644 --- a/app/forms/standing_order_form.rb +++ b/app/forms/standing_order_form.rb @@ -3,7 +3,7 @@ class StandingOrderForm include ActiveModel::Conversion include ActiveModel::Validations - attr_accessor :standing_order, :params, :fee_calculator + attr_accessor :standing_order, :params, :fee_calculator, :problematic_orders delegate :orders, :order_cycles, :bill_address, :ship_address, :standing_line_items, to: :standing_order delegate :shop, :shop_id, :customer, :customer_id, :begins_at, :ends_at, :proxy_orders, to: :standing_order @@ -14,6 +14,7 @@ class StandingOrderForm @standing_order = standing_order @params = params @fee_calculator = fee_calculator + @problematic_orders = [] end def save @@ -51,8 +52,12 @@ class StandingOrderForm update_payment_for(order) if payment_method_id_changed? changed_standing_line_items.each do |sli| - order.line_items.find_by_variant_id(sli.variant_id) - .update_attributes(quantity: sli.quantity, skip_stock_check: true) + line_item = order.line_items.find_by_variant_id(sli.variant_id) + if line_item.quantity == sli.quantity_was + line_item.update_attributes(quantity: sli.quantity, skip_stock_check: true) + else + @problematic_orders |= [order] + end end new_standing_line_items.each do |sli| @@ -75,6 +80,8 @@ class StandingOrderForm if payment payment.andand.void_transaction! order.payments.create(payment_method_id: payment_method_id, amount: order.reload.total) + else + @problematic_orders |= [order] end end @@ -83,6 +90,8 @@ class StandingOrderForm if shipment shipment.update_attributes(shipping_method_id: shipping_method_id) order.update_attribute(:shipping_method_id, shipping_method_id) + else + @problematic_orders |= [order] end end diff --git a/spec/forms/standing_order_form_spec.rb b/spec/forms/standing_order_form_spec.rb index fc8b8d9ade..237eff0fc6 100644 --- a/spec/forms/standing_order_form_spec.rb +++ b/spec/forms/standing_order_form_spec.rb @@ -136,6 +136,7 @@ describe StandingOrderForm do it "does not update the shipping_method on the standing order or on the pre-altered shipment" do expect(order.reload.shipping_method).to eq changed_shipping_method expect(order.reload.shipments.first.shipping_method).to eq changed_shipping_method + expect(form.problematic_orders).to include order end end end @@ -173,6 +174,7 @@ describe StandingOrderForm do payments = order.reload.payments expect(payments.count).to be 1 expect(payments.first.payment_method).to eq changed_payment_method + expect(form.problematic_orders).to include order end end end @@ -230,6 +232,23 @@ describe StandingOrderForm do expect(order.reload.total.to_f).to eq 99.95 end end + + context "where the quantity of the item on an initialised order has already been changed" do + let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 3} ] } } + let(:form) { StandingOrderForm.new(standing_order, params) } + let(:changed_line_item) { order.line_items.find_by_variant_id(sli.variant_id) } + + before { changed_line_item.update_attributes(quantity: 2) } + + it "does not change the quantity, and adds the order to the problematic_orders list" do + expect(order.reload.total.to_f).to eq 79.96 + expect(form.save).to be true + line_items = Spree::LineItem.where(order_id: standing_order.orders, variant_id: sli.variant_id) + expect(line_items.map(&:quantity)).to eq [2] + expect(order.reload.total.to_f).to eq 79.96 + expect(form.problematic_orders).to include order + end + end end describe "adding a new line item" do