From 91fc3f33a0ca52494e9547ca0867a6b00ed04a29 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley Date: Mon, 6 Mar 2017 19:21:06 +0000 Subject: [PATCH] PI reset and save step improvements --- .../controllers/import_options_form.js.coffee | 8 +++++- .../stylesheets/admin/product_import.css.scss | 5 ++-- app/models/product_importer.rb | 21 ++++++++++++---- app/views/admin/product_import/save.html.haml | 6 +++++ spec/features/admin/product_import_spec.rb | 25 +++++++++++++------ 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/admin/product_import/controllers/import_options_form.js.coffee b/app/assets/javascripts/admin/product_import/controllers/import_options_form.js.coffee index 68f12079c4..e1c99a42b5 100644 --- a/app/assets/javascripts/admin/product_import/controllers/import_options_form.js.coffee +++ b/app/assets/javascripts/admin/product_import/controllers/import_options_form.js.coffee @@ -1,7 +1,13 @@ angular.module("ofn.admin").controller "ImportOptionsFormCtrl", ($scope, $timeout, $rootScope, ProductImportService) -> $scope.toggleResetAbsent = () -> - ProductImportService.updateResetAbsent($scope.supplierId, $scope.nonUpdated, $scope.resetAbsent) + confirmed = confirm 'This will set stock level to zero on all products for this \n' + + 'enterprise that are not present in the uploaded file.' if $scope.resetAbsent + + if confirmed or !$scope.resetAbsent + ProductImportService.updateResetAbsent($scope.supplierId, $scope.nonUpdated, $scope.resetAbsent) + else + $scope.resetAbsent = false $scope.resetCount = ProductImportService.resetCount diff --git a/app/assets/stylesheets/admin/product_import.css.scss b/app/assets/stylesheets/admin/product_import.css.scss index 99ed52c758..b878c63d8c 100644 --- a/app/assets/stylesheets/admin/product_import.css.scss +++ b/app/assets/stylesheets/admin/product_import.css.scss @@ -172,15 +172,16 @@ table.import-settings { margin-bottom: 0.5em; strong { - margin-left: 0.5em; margin-right: 0.2em; + min-width: 1.8em; + display: inline-block; + text-align: right; } i { font-size: 1.4em; vertical-align: middle; position: relative; - //margin-bottom: -0.8em; } i.fa-check-circle { diff --git a/app/models/product_importer.rb b/app/models/product_importer.rb index dea26ed720..801fdd724e 100644 --- a/app/models/product_importer.rb +++ b/app/models/product_importer.rb @@ -110,6 +110,10 @@ class ProductImporter @variants_updated end + def products_reset_count + @products_reset_count || 0 + end + def total_saved_count @products_created + @variants_created + @variants_updated end @@ -177,7 +181,12 @@ class ProductImporter AND spree_variants.deleted_at IS NULL', supplier_id). count - @supplier_products[:by_supplier][supplier_id] = {existing_products: products_count} + if @supplier_products[:by_supplier][supplier_id] + @supplier_products[:by_supplier][supplier_id][:existing_products] = products_count + else + @supplier_products[:by_supplier][supplier_id] = {existing_products: products_count} + end + @supplier_products[:total] += products_count end end @@ -340,11 +349,13 @@ class ProductImporter end unless enterprises_to_reset.empty? or @updated_ids.empty? - # Set stock to zero for all products in selected enterprises that were not - # present in the uploaded spreadsheet. - Spree::Variant.joins(:product). + # For selected enterprises; set stock to zero for all products + # that were not present in the uploaded spreadsheet + @products_reset_count = Spree::Variant.joins(:product). where('spree_products.supplier_id IN (?) - AND spree_variants.id NOT IN (?)', enterprises_to_reset, @updated_ids). + AND spree_variants.id NOT IN (?) + AND spree_variants.is_master = false + AND spree_variants.deleted_at IS NULL', enterprises_to_reset, @updated_ids). update_all(count_on_hand: 0) end end diff --git a/app/views/admin/product_import/save.html.haml b/app/views/admin/product_import/save.html.haml index a3cf6b2fd3..06d11e5f35 100644 --- a/app/views/admin/product_import/save.html.haml +++ b/app/views/admin/product_import/save.html.haml @@ -18,6 +18,12 @@ %strong.updated-count= @importer.products_updated_count Products updated + - if @importer.products_reset_count > 0 + %p + %i.fa.fa-check-circle + %strong.reset-count= @importer.products_reset_count + Products had stock level reset to zero + %br - if @importer.errors.count == 0 diff --git a/spec/features/admin/product_import_spec.rb b/spec/features/admin/product_import_spec.rb index a38a32b877..dbe204af33 100644 --- a/spec/features/admin/product_import_spec.rb +++ b/spec/features/admin/product_import_spec.rb @@ -16,9 +16,10 @@ feature "Product Import", js: true do let!(:shipping_category) { create(:shipping_category) } let!(:product) { create(:simple_product, supplier: enterprise2, name: 'Hypothetical Cake') } let!(:variant) { create(:variant, product_id: product.id, price: '8.50', on_hand: '100', unit_value: '500', display_name: 'Preexisting Banana') } - let!(:product2) { create(:simple_product, supplier: enterprise, on_hand: '100', name: 'Beans') } + let!(:product2) { create(:simple_product, supplier: enterprise, on_hand: '100', name: 'Beans', unit_value: '500') } let!(:product3) { create(:simple_product, supplier: enterprise, on_hand: '100', name: 'Sprouts') } - let!(:product4) { create(:simple_product, supplier: enterprise2, on_hand: '100', name: 'Lettuce') } + let!(:product4) { create(:simple_product, supplier: enterprise, on_hand: '100', name: 'Cabbage') } + let!(:product5) { create(:simple_product, supplier: enterprise2, on_hand: '100', name: 'Lettuce') } describe "when importing products from uploaded file" do @@ -251,7 +252,7 @@ feature "Product Import", js: true do csv_data = CSV.generate do |csv| csv << ["name", "supplier", "category", "on_hand", "price", "unit_value", "variant_unit", "variant_unit_scale"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "weight", "1"] - csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1000", "weight", "1000"] + csv << ["Beans", "User Enterprise", "Vegetables", "6", "6.50", "500", "weight", "1"] end File.write('/tmp/test.csv', csv_data) @@ -260,6 +261,13 @@ feature "Product Import", js: true do attach_file 'file', '/tmp/test.csv' click_button 'Import' + expect(page).to have_selector '.item-count', text: "2" + expect(page).to have_selector '.invalid-count', text: "0" + expect(page).to have_selector '.create-count', text: "1" + expect(page).to have_selector '.update-count', text: "1" + + expect(page).to_not have_selector '.reset-count' + within 'div.import-settings' do find('div.header-description').click # Import settings tab check "settings_#{enterprise.id}_reset_all_absent" @@ -269,13 +277,14 @@ feature "Product Import", js: true do click_button 'Save' - expect(page).to have_selector '.created-count', text: '2' - expect(page).to have_selector '.updated-count', text: '0' + expect(page).to have_selector '.created-count', text: '1' + expect(page).to have_selector '.updated-count', text: '1' + expect(page).to have_selector '.reset-count', text: '2' - Spree::Product.find_by_name('Carrots').on_hand.should == 5 # Present in file - Spree::Product.find_by_name('Potatoes').on_hand.should == 6 # Present in file - Spree::Product.find_by_name('Beans').on_hand.should == 0 # In enterprise, not in file + Spree::Product.find_by_name('Carrots').on_hand.should == 5 # Present in file, added + Spree::Product.find_by_name('Beans').on_hand.should == 6 # Present in file, updated Spree::Product.find_by_name('Sprouts').on_hand.should == 0 # In enterprise, not in file + Spree::Product.find_by_name('Cabbage').on_hand.should == 0 # In enterprise, not in file Spree::Product.find_by_name('Lettuce').on_hand.should == 100 # In different enterprise; unchanged end