mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Moving order initialisation logic from StandingOrderForm to ProxyOrder model
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user