Merge branch 'master' of github.com:openfoodfoundation/openfoodnetwork into bulk-product-edit

This commit is contained in:
Maikel Linke
2015-05-01 15:05:14 +10:00
13 changed files with 224 additions and 5 deletions

View File

@@ -9,6 +9,13 @@ Spree::Admin::OrdersController.class_eval do
# in an auth failure as the @order object is nil for collection actions
before_filter :check_authorization, :except => :bulk_management
# After updating an order, the fees should be updated as well
# Currently, adding or deleting line items does not trigger updating the
# fees! This is a quick fix for that.
# TODO: update fees when adding/removing line items
# instead of the update_distribution_charge method.
after_filter :update_distribution_charge, :only => :update
respond_override :index => { :html =>
{ :success => lambda {
# Filter orders to only show those distributed by current user (or all for admin user)
@@ -17,4 +24,17 @@ Spree::Admin::OrdersController.class_eval do
page(params[:page]).
per(params[:per_page] || Spree::Config[:orders_per_page])
} } }
# Overwrite to use confirm_email_for_customer instead of confirm_email.
# This uses a new template. See mailers/spree/order_mailer_decorator.rb.
def resend
Spree::OrderMailer.confirm_email_for_customer(@order.id, true).deliver
flash[:success] = t(:order_email_resent)
respond_with(@order) { |format| format.html { redirect_to :back } }
end
def update_distribution_charge
@order.update_distribution_charge!
end
end

View File

@@ -6,15 +6,18 @@ Spree::Admin::VariantsController.class_eval do
@variants = Spree::Variant.ransack(search_params.merge(:m => 'or')).result
if params[:distributor_id].present?
distributor = Enterprise.find params[:distributor_id]
@variants = @variants.in_distributor(distributor)
end
if params[:order_cycle_id].present?
order_cycle = OrderCycle.find params[:order_cycle_id]
@variants = @variants.in_order_cycle(order_cycle)
end
if params[:distributor_id].present?
distributor = Enterprise.find params[:distributor_id]
@variants = @variants.in_distributor(distributor)
# Perform scoping after all filtering is done.
# Filtering could be a problem on scoped variants.
@variants.each { |v| v.scope_to_hub(distributor) }
end
end
def destroy

View File

@@ -198,6 +198,18 @@ Spree::Order.class_eval do
end
end
# Does this order have shipments that can be shipped?
def ready_to_ship?
self.shipments.any?{|s| s.can_ship?}
end
# Ship all pending orders
def ship
self.shipments.each do |s|
s.ship if s.can_ship?
end
end
def available_shipping_methods(display_on = nil)
Spree::ShippingMethod.all_available(self, display_on)
end

View File

@@ -74,6 +74,12 @@ Spree::Variant.class_eval do
self.option_values.destroy ovs
end
# Used like "product.name - full_name". If called like this, a product with
# name "Bread" would be displayed as one of these:
# Bread - 1kg # if display_name blank
# Bread - Spelt Sourdough, 1kg # if display_name is "Spelt Sourdough, 1kg"
# Bread - 1kg Spelt Sourdough # if unit_to_display is "1kg Spelt Sourdough"
# Bread - Spelt Sourdough (1kg) # if display_name is "Spelt Sourdough" and unit_to_display is "1kg"
def full_name
return unit_to_display if display_name.blank?
return display_name if display_name.downcase.include? unit_to_display.downcase

View File

@@ -0,0 +1,3 @@
/ insert_before "code[erb-loud]:contains('button_link_to t(:resend)')"
- if @order.ready_to_ship?
%li= button_link_to t(:ship), fire_admin_order_url(@order, :e => 'ship'), :method => :put, :data => { :confirm => t(:are_you_sure) }

View File

@@ -0,0 +1,6 @@
/ insert_bottom "[data-hook='admin_orders_index_row_actions']"
-# See also: app/overrides/add_capture_order_shortcut.rb
- if order.ready_to_ship?
- # copied from backend/app/views/spree/admin/payments/_list.html.erb
= link_to_with_icon "icon-road", t(:ship), fire_admin_order_url(order, :e => 'ship'), :method => :put, :no_text => true, :data => {:action => 'ship', :confirm => t(:are_you_sure)}

View File

@@ -0,0 +1,6 @@
/ insert_bottom "[data-hook='admin_orders_index_rows'] td:nth-child(3)"
- if order.special_instructions.present?
%br
%span{class: "icon-warning-sign with-tip", title: order.special_instructions}
notes

View File

@@ -0,0 +1,6 @@
/ insert_after "code[erb-silent]:contains('content_for :page_title')"
- if @order.bill_address.present?
= @order.bill_address.firstname
= @order.bill_address.lastname
\-

View File

@@ -0,0 +1,33 @@
<script type='text/template' id='variant_autocomplete_template'>
<div class='variant-autocomplete-item'>
<figure class='variant-image'>
{{#if variant.image }}
<img src='{{variant.image}}' />
{{ else }}
<img src='/assets/noimage/mini.png' />
{{/if}}
</figure>
<div class='variant-details'>
<h6 class="variant-name">{{variant.name}}</h6>
<ul>
<li><strong>Producer:</strong> {{variant.producer_name}}</li>
</ul>
<ul class='variant-data'>
<li class='variant-sku'><strong>{{t 'sku'}}:</strong> {{variant.sku}}</li>
<li class='variant-on_hand'><strong>{{t 'on_hand' }}:</strong> {{variant.count_on_hand}}</li>
</ul>
{{#if variant.option_values}}
<ul class='variant-options'>
{{#each variant.option_values}}
<li><strong>{{this.option_type.presentation}}:</strong> {{this.presentation}}</li>
{{/each}}
</ul>
{{/if}}
</div>
</div>
</script>

View File

@@ -0,0 +1,34 @@
#
# overriding spree/core/app/views/spree/admin/variants/search.rabl
#
collection @variants
attributes :sku, :options_text, :count_on_hand, :id, :cost_price
node(:name) do |v|
# TODO: when products must have a unit, full_name will always be present
variant_specific = v.full_name
if variant_specific.present?
"#{v.name} - #{v.full_name}"
else
v.name
end
end
node(:full_name) do |v|
v.full_name
end
node(:producer_name) do |v|
v.product.supplier.name
end
child(:images => :images) do
attributes :mini_url
end
child(:option_values => :option_values) do
child(:option_type => :option_type) do
attributes :name, :presentation
end
attributes :name, :presentation
end

View File

@@ -0,0 +1,30 @@
require 'spec_helper'
describe Spree::Admin::OrdersController do
let!(:order) { create(:order) }
context "updating an order with line items" do
let(:line_item) { create(:line_item) }
before { login_as_admin }
it "updates distribution charges" do
order.line_items << line_item
order.save
Spree::Order.any_instance.should_receive(:update_distribution_charge!)
spree_put :update, {
id: order,
order: {
number: order.number,
distributor_id: order.distributor_id,
order_cycle_id: order.order_cycle_id,
line_items_attributes: [
{
id: line_item.id,
quantity: line_item.quantity
}
]
}
}
end
end
end

View File

@@ -8,6 +8,7 @@ module Spree
describe "search action" do
let!(:p1) { create(:simple_product, name: 'Product 1') }
let!(:p2) { create(:simple_product, name: 'Product 2') }
let!(:vo) { create(:variant_override, variant: p1.master, hub: d, count_on_hand: 44) }
let!(:d) { create(:distributor_enterprise) }
let!(:oc) { create(:simple_order_cycle, distributors: [d], variants: [p1.master]) }
@@ -16,6 +17,12 @@ module Spree
assigns(:variants).should == [p1.master]
end
it "applies variant overrides" do
spree_get :search, q: 'Prod', distributor_id: d.id.to_s
assigns(:variants).should == [p1.master]
assigns(:variants).first.count_on_hand.should == 44
end
it "filters by order cycle" do
spree_get :search, q: 'Prod', order_cycle_id: oc.id.to_s
assigns(:variants).should == [p1.master]

View File

@@ -186,6 +186,59 @@ describe Spree::Order do
end
end
describe "an order without shipping method" do
let(:order) { create(:order) }
it "cannot be shipped" do
order.ready_to_ship?.should == false
end
end
describe "an unpaid order with a shipment" do
let(:order) { create(:order, shipping_method: shipping_method) }
let(:shipping_method) { create(:shipping_method) }
before do
order.create_shipment!
order.reload
order.state = 'complete'
order.shipment.update!(order)
end
it "cannot be shipped" do
order.ready_to_ship?.should == false
end
end
describe "a paid order without a shipment" do
let(:order) { create(:order) }
before do
order.payment_state = 'paid'
order.state = 'complete'
end
it "cannot be shipped" do
order.ready_to_ship?.should == false
end
end
describe "a paid order with a shipment" do
let(:order) { create(:order, shipping_method: shipping_method) }
let(:shipping_method) { create(:shipping_method) }
before do
order.create_shipment!
order.payment_state = 'paid'
order.state = 'complete'
order.shipment.update!(order)
end
it "can be shipped" do
order.ready_to_ship?.should == true
end
end
describe "getting the shipping tax" do
let(:order) { create(:order, shipping_method: shipping_method) }
let(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 50.0)) }