diff --git a/backend/app/assets/javascripts/spree/backend/multi_currency.js b/backend/app/assets/javascripts/spree/backend/multi_currency.js index 33b90d0bcfe..7111f1aebc9 100644 --- a/backend/app/assets/javascripts/spree/backend/multi_currency.js +++ b/backend/app/assets/javascripts/spree/backend/multi_currency.js @@ -16,18 +16,8 @@ $(document).ready(function () { .change(function () { var filter = $(this).val() if (filter) { - $(list) - .find('.panel-title:not(:Contains(' + filter + '))') - .parent() - .parent() - .parent() - .hide() - $(list) - .find('.panel-title:Contains(' + filter + ')') - .parent() - .parent() - .parent() - .show() + $(list).find('.panel-title:not(:Contains(' + filter + '))').parent().hide() + $(list).find('.panel-title:Contains(' + filter + ')').parent().show() } else { $(list) .find('.panel') diff --git a/backend/app/controllers/spree/admin/prices_controller.rb b/backend/app/controllers/spree/admin/prices_controller.rb index ca231c093c9..87a64fd2077 100644 --- a/backend/app/controllers/spree/admin/prices_controller.rb +++ b/backend/app/controllers/spree/admin/prices_controller.rb @@ -3,13 +3,17 @@ module Admin class PricesController < ResourceController belongs_to 'spree/product', find_by: :slug + helper_method :supported_currencies_for_all_stores + def create params.require(:vp).permit! params[:vp].each do |variant_id, prices| + next unless variant_id + variant = Spree::Variant.find(variant_id) next unless variant - supported_currencies.each do |currency| + supported_currencies_for_all_stores.each do |currency| price = variant.price_in(currency.iso_code) price.price = (prices[currency.iso_code].blank? ? nil : prices[currency.iso_code]) price.save! if price.new_record? && price.price || !price.new_record? && price.changed? @@ -18,6 +22,17 @@ def create flash[:success] = Spree.t('notice_messages.prices_saved') redirect_to admin_product_path(parent) end + + private + + def supported_currencies_for_all_stores + @supported_currencies_for_all_stores = begin + ( + Spree::Store.pluck(:supported_currencies).map { |c| c.split(',') }.flatten + Spree::Store.pluck(:default_currency) + ). + compact.uniq.map { |code| ::Money::Currency.find(code.strip) } + end + end end end end diff --git a/backend/app/views/spree/admin/prices/_variant_prices.html.erb b/backend/app/views/spree/admin/prices/_variant_prices.html.erb index 8246f346f38..d4c3d9f70a1 100644 --- a/backend/app/views/spree/admin/prices/_variant_prices.html.erb +++ b/backend/app/views/spree/admin/prices/_variant_prices.html.erb @@ -1,28 +1,38 @@ -<% panel_class ||= 'panel-default' %> - -
-
-
-

- <%= variant.sku %> - - - <%= variant.is_master? ? Spree.t(:master) : variant.options_text.blank? ? Spree.t(:variant) : variant.options_text %> - -

-
- -
- <% supported_currencies.each do |currency| %> - <% code = currency.iso_code %> - <% price = variant.price_in(code) %> -
- <%= label_tag "vp[#{variant.id}][#{code}]", code, class: 'control-label col-md-2' %> -
- <%= text_field_tag "vp[#{variant.id}][#{code}]", (price && price.price ? price.display_amount.money : ''), class: 'form-control' %> -
-
- <% end %> -
+
+
+ + + + + + + + + + + + + + + + + <% supported_currencies_for_all_stores.each do |currency| %> + <% code = currency.iso_code %> + <% price = variant.price_in(code) %> + <% @product.variants_including_master.each do |variant| %> + + + + + + + <% end %> + <% end %> + +
<%= Spree.t(:options) %><%= Spree.t(:sku) %><%= Spree.t(:currency) %><%= Spree.t(:price) %>
+ <%= variant.is_master? ? Spree.t(:master) : variant.options_text.blank? ? Spree.t(:variant) : variant.options_text %> + <%= variant.sku %><%= label_tag "vp[#{variant.id}][#{code}]", code %> + <%= text_field_tag "vp[#{variant.id}][#{code}]", (price && price.price ? price.display_amount.money : ''), class: 'form-control' %> +
diff --git a/backend/app/views/spree/admin/prices/index.html.erb b/backend/app/views/spree/admin/prices/index.html.erb index a1037b62107..78334353588 100644 --- a/backend/app/views/spree/admin/prices/index.html.erb +++ b/backend/app/views/spree/admin/prices/index.html.erb @@ -1,20 +1,19 @@ -<%= render 'spree/admin/shared/product_tabs', current: 'Prices' %> +<%= render partial: 'spree/admin/shared/product_tabs', locals: { current: :prices } %> + +<% content_for :page_actions do %> +
+ <%= yield :page_actions %> +
+ <%= product_preview_link(@product) %> +<% end %>
-
- - -
-
-
+ + <%= form_tag nil, { class: 'form-horizontal' } do %> - -
- <%= render 'variant_prices', variant: @product.master, panel_class: 'panel-primary' %> - <% @product.variants.each do |variant| %> - <%= render 'variant_prices', variant: variant %> - <% end %> +
+ <%= render 'variant_prices', variant: @product.master %>
<%= button Spree.t('actions.update'), 'update' %> diff --git a/backend/app/views/spree/admin/shared/_product_tabs.html.erb b/backend/app/views/spree/admin/shared/_product_tabs.html.erb index 7f06998ab1e..c3b81d29d04 100644 --- a/backend/app/views/spree/admin/shared/_product_tabs.html.erb +++ b/backend/app/views/spree/admin/shared/_product_tabs.html.erb @@ -46,11 +46,11 @@ <% end if can?(:admin, Spree::StockItem) && !@product.deleted? %> <%= content_tag :li do %> - <%= link_to_with_icon 'money', + <%= link_to_with_icon 'money', Spree.t(:prices), admin_product_prices_path(@product), - class: "nav-link #{'active' if current == Spree.t(:prices)}" %> + class: "nav-link #{'active' if current == :prices}" %> - <% end if Spree::Config.allow_currency_change == true %> + <% end if can?(:admin, Spree::Price) && !@product.deleted? %> <% end %> diff --git a/backend/spec/features/admin/products/variant_prices_spec.rb b/backend/spec/features/admin/products/variant_prices_spec.rb index eded9d6c8ce..84e9ff6cc0d 100644 --- a/backend/spec/features/admin/products/variant_prices_spec.rb +++ b/backend/spec/features/admin/products/variant_prices_spec.rb @@ -2,27 +2,27 @@ describe 'Variant Prices', type: :feature, js: true do stub_authorization! - let!(:store) { create(:store, default: true) } + let!(:store) { create(:store, default: true, supported_currencies: 'USD,EUR') } + let!(:store2) { create(:store, default: false, default_currency: 'GBP') } let!(:product) { create(:product) } context 'with USD and EUR as currencies' do - before do - reset_spree_preferences do |config| - config.allow_currency_change = true - end - end - it 'allows to save a price for each currency' do visit spree.admin_product_path(product) click_link 'Prices' expect(page).to have_content 'USD' expect(page).to have_content 'EUR' + expect(page).to have_content 'GBP' fill_in "vp_#{product.master.id}_USD", with: '29.95' fill_in "vp_#{product.master.id}_EUR", with: '21.94' + fill_in "vp_#{product.master.id}_GBP", with: '19.94' click_button 'Update' expect(page).to have_content 'Prices successfully saved' + expect(product.master.price_in('USD').amount).to eq(29.95) + expect(product.master.price_in('EUR').amount).to eq(21.94) + expect(product.master.price_in('GBP').amount).to eq(19.94) end end end