mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Strip shipping method mechanics out of LineItem and Order
This commit is contained in:
@@ -1,28 +1,3 @@
|
||||
Spree::LineItem.class_eval do
|
||||
attr_accessible :max_quantity
|
||||
|
||||
before_create :set_distribution_fee
|
||||
|
||||
|
||||
def update_distribution_fee_without_callbacks!(distributor)
|
||||
set_distribution_fee(distributor)
|
||||
update_column(:distribution_fee, distribution_fee)
|
||||
update_column(:shipping_method_name, shipping_method_name)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def shipping_method(distributor=nil)
|
||||
distributor ||= self.order.distributor
|
||||
self.product.shipping_method_for_distributor(distributor)
|
||||
end
|
||||
|
||||
def set_distribution_fee(distributor=nil)
|
||||
order = OpenStruct.new :line_items => [self]
|
||||
sm = shipping_method(distributor)
|
||||
|
||||
self.distribution_fee = sm.compute_amount(order)
|
||||
self.shipping_method_name = sm.name
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,13 +9,11 @@ Spree::Order.class_eval do
|
||||
belongs_to :distributor, :class_name => 'Enterprise'
|
||||
belongs_to :cart
|
||||
|
||||
before_validation :shipping_address_from_distributor
|
||||
validate :products_available_from_new_distribution, :if => lambda { distributor_id_changed? || order_cycle_id_changed? }
|
||||
attr_accessible :order_cycle_id, :distributor_id
|
||||
|
||||
before_validation :shipping_address_from_distributor
|
||||
before_save :update_line_item_shipping_methods
|
||||
after_create :set_default_shipping_method
|
||||
|
||||
|
||||
# -- Scopes
|
||||
scope :managed_by, lambda { |user|
|
||||
@@ -43,12 +41,6 @@ Spree::Order.class_eval do
|
||||
save!
|
||||
end
|
||||
|
||||
def empty!
|
||||
line_items.destroy_all
|
||||
adjustments.destroy_all
|
||||
set_default_shipping_method
|
||||
end
|
||||
|
||||
def set_distributor!(distributor)
|
||||
self.distributor = distributor
|
||||
self.order_cycle = nil unless self.order_cycle.andand.has_distributor? distributor
|
||||
@@ -86,27 +78,6 @@ Spree::Order.class_eval do
|
||||
|
||||
private
|
||||
|
||||
# On creation of the order (when the first item is added to the user's cart), set the
|
||||
# shipping method to the first one available and create a shipment.
|
||||
# order.create_shipment! creates an adjustment for the shipping cost on the order,
|
||||
# which means that the customer can see their shipping cost at every step of the
|
||||
# checkout process, not just after the delivery step.
|
||||
# This is based on the assumption that there's only one shipping method visible to the user,
|
||||
# which is a method using the itemwise shipping calculator.
|
||||
def set_default_shipping_method
|
||||
self.shipping_method = itemwise_shipping_method
|
||||
if self.shipping_method
|
||||
self.save!
|
||||
self.create_shipment!
|
||||
else
|
||||
raise 'No default shipping method found'
|
||||
end
|
||||
end
|
||||
|
||||
def itemwise_shipping_method
|
||||
Spree::ShippingMethod.all.find { |sm| sm.calculator.is_a? OpenFoodWeb::Calculator::Itemwise }
|
||||
end
|
||||
|
||||
def shipping_address_from_distributor
|
||||
if distributor
|
||||
self.ship_address = distributor.address.clone
|
||||
@@ -119,13 +90,6 @@ Spree::Order.class_eval do
|
||||
end
|
||||
end
|
||||
|
||||
def update_line_item_shipping_methods
|
||||
if %w(cart address delivery resumed).include? state
|
||||
self.line_items.each { |li| li.update_distribution_fee_without_callbacks!(distributor) }
|
||||
self.update!
|
||||
end
|
||||
end
|
||||
|
||||
def product_distribution_for(line_item)
|
||||
line_item.variant.product.product_distribution_for self.distributor
|
||||
end
|
||||
|
||||
@@ -1,19 +1,6 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::Order do
|
||||
it "initialises a default shipping method after creation" do
|
||||
shipping_method_regular = create(:shipping_method)
|
||||
shipping_method_itemwise = create(:itemwise_shipping_method)
|
||||
|
||||
subject.shipping_method.should be_nil
|
||||
subject.adjustments.should be_empty
|
||||
|
||||
subject.save!
|
||||
|
||||
subject.shipping_method.should == shipping_method_itemwise
|
||||
subject.adjustments.where(:label => "Shipping").should be_present
|
||||
end
|
||||
|
||||
it "sets attributes on line items for variants" do
|
||||
d = create(:distributor_enterprise)
|
||||
p = create(:product, :distributors => [d])
|
||||
|
||||
Reference in New Issue
Block a user