diff --git a/app/models/spree/app_configuration_decorator.rb b/app/models/spree/app_configuration_decorator.rb new file mode 100644 index 0000000000..f4f4acc5ec --- /dev/null +++ b/app/models/spree/app_configuration_decorator.rb @@ -0,0 +1,9 @@ +Spree::AppConfiguration.class_eval do + # This file decorates the existing preferences file defined by Spree. + # It allows us to add our own global configuration variables, which + # we can allow to be modified in the UI by adding appropriate form + # elements to existing or new configuration pages. + + # Tax Preferences + preference :products_require_tax_category, :boolean, default: false +end \ No newline at end of file diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 5b3460a073..9d7eb18d80 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -20,7 +20,8 @@ Spree::Product.class_eval do validates_associated :master, message: "^Price and On Hand must be valid" validates_presence_of :supplier validates :primary_taxon, presence: { message: "^Product Category can't be blank" } - + validates :tax_category_id, presence: { message: "^Tax Category can't be blank" }, if: "Spree::Config.products_require_tax_category" + validates_presence_of :variant_unit, if: :has_variants? validates_presence_of :variant_unit_scale, if: -> p { %w(weight volume).include? p.variant_unit } diff --git a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface index 7c627eec4e..974e31f3ee 100644 --- a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface +++ b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface @@ -38,6 +38,8 @@ .twelve.columns.alpha .six.columns.alpha = render 'spree/admin/products/primary_taxon_form', f: f + - if Spree::TaxCategory.any? + = render 'spree/admin/products/tax_category_form', f: f .three.columns = f.field_container :price do = f.label :price, t(:price) diff --git a/app/overrides/spree/admin/tax_settings/edit/add_products_require_tax_category.html.haml.deface b/app/overrides/spree/admin/tax_settings/edit/add_products_require_tax_category.html.haml.deface new file mode 100644 index 0000000000..588669f005 --- /dev/null +++ b/app/overrides/spree/admin/tax_settings/edit/add_products_require_tax_category.html.haml.deface @@ -0,0 +1,6 @@ +/ insert_before "[data-hook='shipment_vat']" + +%div.field.align-center{ "data-hook" => "products_require_tax_category" } + = hidden_field_tag 'preferences[products_require_tax_category]', '0' + = check_box_tag 'preferences[products_require_tax_category]', '1', Spree::Config[:products_require_tax_category] + = label_tag nil, t(:products_require_tax_category) \ No newline at end of file diff --git a/app/views/spree/admin/products/_tax_category_form.html.haml b/app/views/spree/admin/products/_tax_category_form.html.haml new file mode 100644 index 0000000000..9ab348636c --- /dev/null +++ b/app/views/spree/admin/products/_tax_category_form.html.haml @@ -0,0 +1,6 @@ += f.field_container :tax_category_id do + = f.label :tax_category_id, t(:tax_category) + * + %br + = f.collection_select(:tax_category_id, Spree::TaxCategory.all, :id, :name, {:include_blank => Spree::TaxCategory.count > 1}, {:class => "select2 fullwidth"}) + = f.error_message_on :tax_category_id diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index 1f61b07d2a..0e27930f6e 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -6,6 +6,8 @@ feature %q{ } do include AuthenticationWorkflow include WebHelper + + let!(:taxon) { create(:taxon) } background do @@ -15,12 +17,15 @@ feature %q{ end describe "creating a product" do - scenario "assigning a important attributes", js: true do + let!(:tax_category) { create(:tax_category, name: 'Test Tax Category') } + + scenario "assigning important attributes", js: true do login_to_admin_section click_link 'Products' click_link 'New Product' + select 'Test Tax Category', from: 'product_tax_category_id' select 'New supplier', from: 'product_supplier_id' fill_in 'product_name', with: 'A new product !!!' select "Weight (kg)", from: 'product_variant_unit_with_scale' @@ -34,6 +39,7 @@ feature %q{ flash_message.should == 'Product "A new product !!!" has been successfully created!' product = Spree::Product.find_by_name('A new product !!!') + product.tax_category_id.should == tax_category.id product.supplier.should == @supplier product.variant_unit.should == 'weight' product.variant_unit_scale.should == 1000 @@ -85,6 +91,7 @@ feature %q{ end context "as an enterprise user" do + let!(:tax_category) { create(:tax_category) } before do @new_user = create_enterprise_user @@ -117,6 +124,7 @@ feature %q{ page.should have_selector('#product_supplier_id') select 'Another Supplier', :from => 'product_supplier_id' select taxon.name, from: "product_primary_taxon_id" + select tax_category.name, from: "product_tax_category_id" # Should only have suppliers listed which the user can manage page.should have_select 'product_supplier_id', with_options: [@supplier2.name, @supplier_permitted.name] @@ -127,6 +135,7 @@ feature %q{ flash_message.should == 'Product "A new product !!!" has been successfully created!' product = Spree::Product.find_by_name('A new product !!!') product.supplier.should == @supplier2 + product.tax_category.should == tax_category end scenario "editing a product" do @@ -135,10 +144,12 @@ feature %q{ visit spree.edit_admin_product_path product select 'Permitted Supplier', from: 'product_supplier_id' + select tax_category.name, from: 'product_tax_category_id' click_button 'Update' flash_message.should == 'Product "a product" has been successfully updated!' product.reload product.supplier.should == @supplier_permitted + product.tax_category.should == tax_category end scenario "editing product distributions" do diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index f9a990830c..3b3f5ec0a0 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -8,23 +8,18 @@ module Spree it { should belong_to(:primary_taxon) } it { should have_many(:product_distributions) } end - + describe "validations and defaults" do - it "is valid when created from factory" do - create(:product).should be_valid + it "is valid when built from factory" do + build(:product).should be_valid end it "requires a primary taxon" do - product = create(:simple_product) - product.taxons = [] - product.primary_taxon = nil - product.should_not be_valid + build(:simple_product, taxons: [], primary_taxon: nil).should_not be_valid end it "requires a supplier" do - product = create(:simple_product) - product.supplier = nil - product.should_not be_valid + build(:simple_product, supplier: nil).should_not be_valid end it "does not save when master is invalid" do @@ -41,6 +36,25 @@ module Spree product.available_on.should == Time.now end + describe "tax category" do + context "when a tax category is required" do + before { Spree::Config.products_require_tax_category = true } + + it "is invalid when a tax category is not provided" do + build(:product, tax_category_id: nil).should_not be_valid + end + end + + context "when a tax category is not required" do + before { Spree::Config.products_require_tax_category = false } + + it "is valid when a tax category is not provided" do + build(:product, tax_category_id: nil).should be_valid + end + end + end + + context "when the product has variants" do let(:product) do product = create(:simple_product)