mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #5375 from luisramos0/sample_order
[Spree 2.1] OFN v3 comes with sample orders :-)
This commit is contained in:
@@ -10,6 +10,7 @@ require "tasks/sample_data/product_factory"
|
||||
require "tasks/sample_data/shipping_method_factory"
|
||||
require "tasks/sample_data/taxon_factory"
|
||||
require "tasks/sample_data/user_factory"
|
||||
require "tasks/sample_data/order_factory"
|
||||
|
||||
# The sample data generated by this task is supposed to save some time during
|
||||
# manual testing. It is not meant to be complete, but we try to improve it
|
||||
@@ -49,6 +50,8 @@ namespace :ofn do
|
||||
CustomerFactory.new.create_samples(users)
|
||||
|
||||
GroupFactory.new.create_samples
|
||||
|
||||
OrderFactory.new.create_samples
|
||||
end
|
||||
|
||||
def seeded?
|
||||
|
||||
86
lib/tasks/sample_data/order_factory.rb
Normal file
86
lib/tasks/sample_data/order_factory.rb
Normal file
@@ -0,0 +1,86 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "tasks/sample_data/logging"
|
||||
require "tasks/sample_data/addressing"
|
||||
|
||||
class OrderFactory
|
||||
include Logging
|
||||
include Addressing
|
||||
|
||||
def create_samples
|
||||
log "Creating orders"
|
||||
@order_cycle = OrderCycle.find_by(name: "Fredo's Farm Hub OC")
|
||||
@distributor = Enterprise.find_by(name: "Fredo's Farm Hub")
|
||||
@email = "new.customer@example.org"
|
||||
|
||||
log "- cart order"
|
||||
create_cart_order
|
||||
|
||||
log "- complete order - not paid"
|
||||
create_complete_order
|
||||
|
||||
log "- complete order - paid"
|
||||
order = create_complete_order
|
||||
order.payments.first.capture!
|
||||
|
||||
log "- complete order - delivery"
|
||||
order = create_complete_order
|
||||
order.select_shipping_method(delivery_shipping_method_id)
|
||||
order.save
|
||||
|
||||
log "- complete order - shipped"
|
||||
order = create_complete_order
|
||||
order.payments.first.capture!
|
||||
order.save
|
||||
order.shipment.reload.ship!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_cart_order
|
||||
order = create_order
|
||||
order.save
|
||||
order
|
||||
end
|
||||
|
||||
def create_complete_order
|
||||
order = create_cart_order
|
||||
AdvanceOrderService.new(order).call
|
||||
order
|
||||
end
|
||||
|
||||
def create_order
|
||||
order = Spree::Order.create!(
|
||||
email: @email,
|
||||
order_cycle: @order_cycle,
|
||||
distributor: @distributor,
|
||||
bill_address: order_address,
|
||||
ship_address: order_address
|
||||
)
|
||||
order.line_items.create( variant_id: first_variant.id, quantity: 5 )
|
||||
order.payments.create(payment_method_id: first_payment_method_id)
|
||||
order
|
||||
end
|
||||
|
||||
def first_variant
|
||||
# First variant on the first outgoing exchange of the OC
|
||||
@order_cycle.exchanges.outgoing.first.variants.first
|
||||
end
|
||||
|
||||
def first_payment_method_id
|
||||
# First payment method of the distributor
|
||||
@distributor.payment_methods.first.id
|
||||
end
|
||||
|
||||
def delivery_shipping_method_id
|
||||
@distributor.shipping_methods.find_by(name: "Home delivery").id
|
||||
end
|
||||
|
||||
def order_address
|
||||
address = address("25 Myrtle Street, Bayswater, 3153")
|
||||
address.firstname = "John"
|
||||
address.lastname = "Mistery"
|
||||
address.phone = "0987654321"
|
||||
address
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user