From e29a4f9a8a4e175d775a90e9061bbfc4959a116b Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Sun, 11 Dec 2016 19:06:48 +1100 Subject: [PATCH] Moving order initialisation logic from StandingOrderForm to ProxyOrder model --- app/forms/standing_order_form.rb | 29 ++++------------------------- app/models/proxy_order.rb | 20 +++++++++++++++++++- spec/models/proxy_order_spec.rb | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/app/forms/standing_order_form.rb b/app/forms/standing_order_form.rb index aeb530c8e3..ac8657af4e 100644 --- a/app/forms/standing_order_form.rb +++ b/app/forms/standing_order_form.rb @@ -65,34 +65,11 @@ class StandingOrderForm @future_and_undated_orders = orders.joins(:order_cycle).merge(OrderCycle.not_closed).readonly(false) end - def create_order_for(order_cycle_id) - order = Spree::Order.create!({ - customer_id: customer_id, - email: customer.email, - order_cycle_id: order_cycle_id, - distributor_id: shop_id, - shipping_method_id: shipping_method_id, - }) - standing_line_items.each do |sli| - 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! - create_payment_for(order) - - order.save - order - end - - def create_payment_for(order) - order.payments.create(payment_method_id: payment_method_id, amount: order.reload.total) - end - def update_payment_for(order) payment = order.payments.with_state('checkout').where(payment_method_id: payment_method_id_was).last if payment payment.andand.void_transaction! - create_payment_for(order) + order.payments.create(payment_method_id: payment_method_id, amount: order.reload.total) end end @@ -106,7 +83,9 @@ class StandingOrderForm def initialise_proxy_orders! uninitialised_order_cycle_ids.each do |order_cycle_id| - proxy_orders << ProxyOrder.new(standing_order: standing_order, order_cycle_id: order_cycle_id, order: create_order_for(order_cycle_id)) + proxy_order = proxy_orders.build(standing_order: standing_order, order_cycle_id: order_cycle_id) + proxy_order.initialise_order! + proxy_order.save! unless standing_order.new_record? end end diff --git a/app/models/proxy_order.rb b/app/models/proxy_order.rb index 8d42f28c82..c6aef6d50b 100644 --- a/app/models/proxy_order.rb +++ b/app/models/proxy_order.rb @@ -3,7 +3,7 @@ class ProxyOrder < ActiveRecord::Base belongs_to :standing_order belongs_to :order_cycle - delegate :number, :order_cycle_id, :completed_at, :total, to: :order + delegate :number, :completed_at, :total, to: :order scope :closed, -> { joins(order: :order_cycle).merge(OrderCycle.closed) } scope :not_closed, -> { joins(order: :order_cycle).merge(OrderCycle.not_closed) } @@ -35,4 +35,22 @@ class ProxyOrder < ActiveRecord::Base true end end + + def initialise_order! + create_order!({ + customer_id: standing_order.customer_id, + email: standing_order.customer.email, + order_cycle_id: order_cycle_id, + distributor_id: standing_order.shop_id, + shipping_method_id: standing_order.shipping_method_id, + }) + standing_order.standing_line_items.each do |sli| + order.line_items.build(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: true) + end + order.update_attributes(bill_address: standing_order.bill_address.dup, ship_address: standing_order.ship_address.dup) + order.update_distribution_charge! + order.payments.create(payment_method_id: standing_order.payment_method_id, amount: order.reload.total) + + order.save! + end end diff --git a/spec/models/proxy_order_spec.rb b/spec/models/proxy_order_spec.rb index 23dbfc4281..766273c565 100644 --- a/spec/models/proxy_order_spec.rb +++ b/spec/models/proxy_order_spec.rb @@ -103,4 +103,23 @@ describe ProxyOrder, type: :model do end end end + + describe "initialising an the order" do + let(:standing_order) { create(:standing_order_with_items) } + let!(:proxy_order) { create(:proxy_order, standing_order: standing_order) } + + it "builds a new order based the standing order" do + expect{ proxy_order.initialise_order! }.to change{Spree::Order.count}.by(1) + order = proxy_order.order + expect(order.line_items.count).to eq standing_order.standing_line_items.count + expect(order.distributor).to eq standing_order.shop + expect(order.order_cycle).to eq proxy_order.order_cycle + expect(order.shipping_method).to eq standing_order.shipping_method + expect(order.shipments.first.shipping_method).to eq standing_order.shipping_method + expect(order.payments.first.payment_method).to eq standing_order.payment_method + expect(order.bill_address).to eq standing_order.bill_address + expect(order.ship_address).to eq standing_order.ship_address + expect(order.complete?).to be false + end + end end