diff --git a/app/controllers/admin/enterprise_fees_controller.rb b/app/controllers/admin/enterprise_fees_controller.rb index 2aa2a3aa30..3d4acb587e 100644 --- a/app/controllers/admin/enterprise_fees_controller.rb +++ b/app/controllers/admin/enterprise_fees_controller.rb @@ -49,7 +49,7 @@ module Admin end def collection - super.order('enterprise_id', 'fee_type', 'name') + EnterpriseFee.managed_by(spree_current_user).order('enterprise_id', 'fee_type', 'name') end end end diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index 05f3828ecd..8f3f038ded 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -13,6 +13,13 @@ class EnterpriseFee < ActiveRecord::Base scope :for_enterprise, lambda { |enterprise| where(enterprise_id: enterprise) } + scope :managed_by, lambda { |user| + if user.has_spree_role?('admin') + scoped + else + where('enterprise_id IN (?)', user.enterprises) + end + } def self.clear_all_adjustments_for(line_item) line_item.order.adjustments.where(originator_type: 'EnterpriseFee', source_id: line_item, source_type: 'Spree::LineItem').destroy_all diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 88bcdefd7e..e52bb01dc8 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -24,7 +24,7 @@ class OrderCycle < ActiveRecord::Base if user.has_spree_role?('admin') scoped else - where('coordinator_id IN (?)', user.enterprises.map {|enterprise| enterprise.id }) + where('coordinator_id IN (?)', user.enterprises) end } diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 34bd9679a4..363a469bd8 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -40,7 +40,10 @@ class AbilityDecorator can [:create], OrderCycle - can [:index, :read], EnterpriseFee + can [:admin, :index, :read], EnterpriseFee do |enterprise_fee| + user.enterprises.include? enterprise_fee.enterprise + end + can [:admin, :index, :read, :create, :edit, :update], ExchangeVariant can [:admin, :index, :read, :create, :edit, :update], Exchange can [:admin, :index, :read, :create, :edit, :update], ExchangeFee diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index f3ae861c84..5c28f5f510 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -20,7 +20,7 @@ Spree::Order.class_eval do if user.has_spree_role?('admin') scoped else - where('distributor_id IN (?)', user.enterprises.map {|enterprise| enterprise.id }) + where('distributor_id IN (?)', user.enterprises) end } diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index f8759ac06b..6e0c6658bd 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -10,7 +10,7 @@ Spree::PaymentMethod.class_eval do if user.has_spree_role?('admin') scoped else - where('distributor_id IN (?)', user.enterprises.map {|enterprise| enterprise.id }) + where('distributor_id IN (?)', user.enterprises) end } end @@ -18,4 +18,4 @@ end # Ensure that all derived classes also allow distributor_id Spree::Gateway.providers.each do |p| p.attr_accessible :distributor_id -end \ No newline at end of file +end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index dc0b1bbf35..b60bd2f7cd 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -61,7 +61,7 @@ Spree::Product.class_eval do if user.has_spree_role?('admin') scoped else - where('supplier_id IN (?)', user.enterprises.map {|enterprise| enterprise.id }) + where('supplier_id IN (?)', user.enterprises) end } diff --git a/app/overrides/spree/shared/_order_details/summarise_distribution_charges.html.haml.deface b/app/overrides/spree/shared/_order_details/summarise_distribution_charges.html.haml.deface new file mode 100644 index 0000000000..c4e7ed5dfd --- /dev/null +++ b/app/overrides/spree/shared/_order_details/summarise_distribution_charges.html.haml.deface @@ -0,0 +1,8 @@ +/ replace_contents 'tfoot#order-charges' + +- checkout_adjustments_for_summary(@order).each do |adjustment| + %tr.total + %td{:colspan => "4"} + %strong= adjustment.label + %td.total + %span= adjustment.display_amount.to_html 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 aa3b59de8c..10dc7ace3b 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 @@ -11,7 +11,7 @@ Order for: <%= @order.bill_address.full_name %> <% end %> ============================================================ Subtotal: <%= @order.display_item_total %> -<% @order.adjustments.eligible.each do |adjustment| %> +<% checkout_adjustments_for_summary(@order).each do |adjustment| %> <%= raw(adjustment.label) %> <%= adjustment.display_amount %> <% end %> Order Total: <%= @order.display_total %> diff --git a/lib/chili/enterprises_distributor_info_rich_text_feature/lib/enterprises_distributor_info_rich_text_feature/engine.rb b/lib/chili/enterprises_distributor_info_rich_text_feature/lib/enterprises_distributor_info_rich_text_feature/engine.rb index 36a13fe94a..538c64e30e 100644 --- a/lib/chili/enterprises_distributor_info_rich_text_feature/lib/enterprises_distributor_info_rich_text_feature/engine.rb +++ b/lib/chili/enterprises_distributor_info_rich_text_feature/lib/enterprises_distributor_info_rich_text_feature/engine.rb @@ -7,6 +7,8 @@ module EnterprisesDistributorInfoRichTextFeature initializer 'enterprises_distributor_info_rich_text_feature.mailer', :after => :load_config_initializers do |app| if OpenFoodWeb::FeatureToggle.enabled? :enterprises_distributor_info_rich_text ::Spree::OrderMailer.class_eval do + helper CheckoutHelper + def confirm_email(order, resend = false) find_order(order) subject = (resend ? "[#{t(:resend).upcase}] " : '') diff --git a/spec/features/admin/order_cycles_spec.rb b/spec/features/admin/order_cycles_spec.rb index 9697504901..716894272b 100644 --- a/spec/features/admin/order_cycles_spec.rb +++ b/spec/features/admin/order_cycles_spec.rb @@ -349,7 +349,7 @@ feature %q{ let(:supplier2) { create(:supplier_enterprise, name: 'Another Supplier') } let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') } let(:distributor2) { create(:distributor_enterprise, name: 'Another Distributor') } - + let!(:distributor1_fee) { create(:enterprise_fee, enterprise: distributor1, name: 'First Distributor Fee') } before(:each) do product = create(:product, supplier: supplier1) product.distributors << distributor1 @@ -384,6 +384,8 @@ feature %q{ click_button 'Add supplier' select 'First Distributor', from: 'order_cycle_coordinator_id' + click_button 'Add coordinator fee' + select 'First Distributor Fee', from: 'order_cycle_coordinator_fee_0_id' select 'First Distributor', from: 'new_distributor_id' click_button 'Add distributor' diff --git a/spec/features/consumer/checkout_spec.rb b/spec/features/consumer/checkout_spec.rb index 562ea7ad36..882b1f0977 100644 --- a/spec/features/consumer/checkout_spec.rb +++ b/spec/features/consumer/checkout_spec.rb @@ -63,7 +63,8 @@ feature %q{ @zone = create(:zone) c = Spree::Country.find_by_name('Australia') Spree::ZoneMember.create(:zoneable => c, :zone => @zone) - create(:shipping_method, zone: @zone) + sm = create(:shipping_method, zone: @zone, calculator: Spree::Calculator::FlatRate.new) + sm.calculator.set_preference(:amount, 0); sm.calculator.save! @payment_method_all = create(:payment_method, :name => 'Cheque payment method', :description => 'Cheque payment method') #valid for any distributor @payment_method_distributor = create(:payment_method, :name => 'Edible Garden payment method', :distributor => @distributor) @@ -324,13 +325,19 @@ feature %q{ click_checkout_continue_button # -- Checkout: Order complete - page.should have_content('Your order has been processed successfully') - page.should have_content(@payment_method_all.description) + page.should have_content 'Your order has been processed successfully' + page.should have_content @payment_method_all.description + page.should have_selector 'tfoot#order-charges tr.total td', text: 'Distribution' + page.should have_selector 'tfoot#order-charges tr.total td', text: '$3.00' # page.should have_content('Your order will be available on:') # page.should have_content('On Tuesday, 4 PM') # page.should have_content('12 Bungee Rd, Carion') + + # -- Checkout: Email + email = ActionMailer::Base.deliveries.last + email.body.should =~ /Distribution \$3.00/ end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 0d9bb31cc3..05bb1d39ad 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -136,6 +136,10 @@ module Spree it "should be able to create OrderCycles" do should have_ability([:create], for: OrderCycle) end + + it "should be able to read EnterpriseFees" do + should have_ability([:admin, :index, :read], for: EnterpriseFee) + end end context 'Enterprise manager' do @@ -160,4 +164,4 @@ module Spree end end end -end \ No newline at end of file +end