diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb
index 43f9163d4e..cbdd2b8007 100644
--- a/app/models/order_cycle.rb
+++ b/app/models/order_cycle.rb
@@ -94,6 +94,14 @@ class OrderCycle < ActiveRecord::Base
self.variants.include? variant
end
+ def exchange_for_distributor(distributor)
+ exchanges.outgoing.to_enterprises([distributor]).first
+ end
+
+ def pickup_time_for(distributor)
+ exchange_for_distributor(distributor).andand.pickup_time || distributor.next_collection_at
+ end
+
# -- Fees
def create_adjustments_for(line_item)
diff --git a/lib/chili/enterprises_distributor_info_rich_text_feature/app/overrides/enterprises/_distributor_details/rich_text.html.haml.deface b/lib/chili/enterprises_distributor_info_rich_text_feature/app/overrides/enterprises/_distributor_details/rich_text.html.haml.deface
index 303d43f62c..1dccb009a1 100644
--- a/lib/chili/enterprises_distributor_info_rich_text_feature/app/overrides/enterprises/_distributor_details/rich_text.html.haml.deface
+++ b/lib/chili/enterprises_distributor_info_rich_text_feature/app/overrides/enterprises/_distributor_details/rich_text.html.haml.deface
@@ -1,4 +1,8 @@
/ replace_contents "[data-hook='distributor-details']"
%h2= distributor.name
= distributor.distributor_info.andand.html_safe
-.next-collection-at= distributor.next_collection_at
+.next-collection-at
+ - if current_order_cycle
+ = current_order_cycle.pickup_time_for(distributor)
+ - else
+ = distributor.next_collection_at
diff --git a/lib/chili/enterprises_distributor_info_rich_text_feature/app/views/spree/order_mailer/confirm_email_with_distributor_info.text.erb b/lib/chili/enterprises_distributor_info_rich_text_feature/app/views/spree/order_mailer/confirm_email_with_distributor_info.text.erb
index 10dc7ace3b..c47cdd7b7b 100644
--- a/lib/chili/enterprises_distributor_info_rich_text_feature/app/views/spree/order_mailer/confirm_email_with_distributor_info.text.erb
+++ b/lib/chili/enterprises_distributor_info_rich_text_feature/app/views/spree/order_mailer/confirm_email_with_distributor_info.text.erb
@@ -27,7 +27,11 @@ Payment Details
Collection / Delivery Details
============================================================
<%= raw strip_html @order.distributor.distributor_info %>
+<% if @order.order_cycle %>
+<%= @order.order_cycle.pickup_time_for(@order.distributor) %>
+<% else %>
<%= @order.distributor.next_collection_at %>
+<% end %>
Thanks for your support.
diff --git a/spec/features/chili/enterprises_distributor_info_rich_text_feature_spec.rb b/spec/features/chili/enterprises_distributor_info_rich_text_feature_spec.rb
index 26446da7c5..7e6738ed9c 100644
--- a/spec/features/chili/enterprises_distributor_info_rich_text_feature_spec.rb
+++ b/spec/features/chili/enterprises_distributor_info_rich_text_feature_spec.rb
@@ -50,7 +50,7 @@ feature "enterprises distributor info as rich text" do
page.should have_selector "tr[data-hook='distributor_info'] td", text: 'Chu ge sai yubi dan bisento tobi ashi yubi ge omote.'
end
- scenario "viewing distributor info", js: true do
+ scenario "viewing distributor info with product distribution", js: true do
ActionMailer::Base.deliveries.clear
d = create(:distributor_enterprise, distributor_info: 'Chu ge sai yubi dan bisento tobi ashi yubi ge omote.', next_collection_at: 'Thursday 2nd May')
@@ -84,6 +84,46 @@ feature "enterprises distributor info as rich text" do
email.body.should =~ /Thursday 2nd May/
end
+ scenario "viewing distributor info with order cycle distribution", js: true do
+ set_feature_toggle :order_cycles, true
+ ActionMailer::Base.deliveries.clear
+
+ d = create(:distributor_enterprise, name: 'Green Grass', distributor_info: 'Chu ge sai yubi dan bisento tobi ashi yubi ge omote.', next_collection_at: 'Thursday 2nd May')
+ p = create(:product)
+ oc = create(:simple_order_cycle, distributors: [d], variants: [p.master])
+ ex = oc.exchanges.outgoing.last
+ ex = Exchange.find ex.id
+ ex.pickup_time = 'Friday 4th May'
+ ex.save!
+
+ setup_shipping_details d
+
+ login_to_consumer_section
+ click_link 'Green Grass'
+
+ # -- Product details page
+ click_link p.name
+ within '#product-distributor-details' do
+ page.should have_content 'Chu ge sai yubi dan bisento tobi ashi yubi ge omote.'
+ page.should have_content 'Friday 4th May'
+ end
+
+ # -- Checkout
+ click_button 'Add To Cart'
+ click_link 'Checkout'
+ within 'fieldset#shipping' do
+ page.should have_content 'Chu ge sai yubi dan bisento tobi ashi yubi ge omote.'
+ page.should have_content 'Friday 4th May'
+ end
+
+ # -- Purchase email
+ complete_purchase_from_checkout_address_page
+ wait_until { ActionMailer::Base.deliveries.length == 1 }
+ email = ActionMailer::Base.deliveries.last
+ email.body.should =~ /Chu ge sai yubi dan bisento tobi ashi yubi ge omote./
+ email.body.should =~ /Friday 4th May/
+ end
+
private
def setup_shipping_details(distributor)
diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb
index dfa7f10374..5695dd436a 100644
--- a/spec/models/order_cycle_spec.rb
+++ b/spec/models/order_cycle_spec.rb
@@ -149,11 +149,11 @@ describe OrderCycle do
@d1 = create(:enterprise)
@d2 = create(:enterprise)
- e0 = create(:exchange,
+ @e0 = create(:exchange,
order_cycle: @oc, sender: create(:enterprise), receiver: @oc.coordinator)
- e1 = create(:exchange,
+ @e1 = create(:exchange,
order_cycle: @oc, sender: @oc.coordinator, receiver: @d1)
- e2 = create(:exchange,
+ @e2 = create(:exchange,
order_cycle: @oc, sender: @oc.coordinator, receiver: @d2)
@p0 = create(:product)
@@ -161,11 +161,11 @@ describe OrderCycle do
@p2 = create(:product)
@p2_v = create(:variant, product: @p2)
- e0.variants << @p0.master
- e1.variants << @p1.master
- e1.variants << @p2.master
- e1.variants << @p2_v
- e2.variants << @p1.master
+ @e0.variants << @p0.master
+ @e1.variants << @p1.master
+ @e1.variants << @p2.master
+ @e1.variants << @p2_v
+ @e2.variants << @p1.master
end
it "reports on the variants exchanged" do
@@ -185,6 +185,34 @@ describe OrderCycle do
end
end
+ describe "exchanges" do
+ before(:each) do
+ @oc = create(:simple_order_cycle)
+
+ @d1 = create(:enterprise)
+ @d2 = create(:enterprise, next_collection_at: '2-8pm Friday')
+
+ @e0 = create(:exchange, order_cycle: @oc, sender: create(:enterprise), receiver: @oc.coordinator)
+ @e1 = create(:exchange, order_cycle: @oc, sender: @oc.coordinator, receiver: @d1, pickup_time: '5pm Tuesday')
+ @e2 = create(:exchange, order_cycle: @oc, sender: @oc.coordinator, receiver: @d2, pickup_time: nil)
+ end
+
+ it "finds the exchange for a distributor" do
+ @oc.exchange_for_distributor(@d1).should == @e1
+ @oc.exchange_for_distributor(@d2).should == @e2
+ end
+
+ describe "finding pickup time for a distributor" do
+ it "looks up the pickup time on the exchange when present" do
+ @oc.pickup_time_for(@d1).should == '5pm Tuesday'
+ end
+
+ it "returns the distributor's default collection time otherwise" do
+ @oc.pickup_time_for(@d2).should == '2-8pm Friday'
+ end
+ end
+ end
+
it "clones itself" do
oc = create(:order_cycle)
occ = oc.clone!