From 3ff42d56f6c741cfe582b8771d191891ea6152bd Mon Sep 17 00:00:00 2001 From: Erik Axel Nielsen Date: Sat, 13 Jul 2013 12:05:24 +0200 Subject: [PATCH] Initial import --- .gitignore | 16 --- .idea/.generators | 8 ++ .idea/.name | 1 + .idea/.rakeTasks | 7 + .idea/compiler.xml | 25 ++++ .idea/copyright/profiles_settings.xml | 5 + .idea/encodings.xml | 5 + .idea/misc.xml | 24 ++++ .idea/modules.xml | 9 ++ .idea/scopes/scope_settings.xml | 5 + .idea/uiDesigner.xml | 125 ++++++++++++++++++ .idea/vcs.xml | 7 + Gemfile | 4 + Gemfile.lock | 168 ++++++++++++++++++++++++ MIT-LICENSE | 20 +++ README.md | 2 - README.rdoc | 3 + Rakefile | 32 +++++ config.ru | 4 + lib/assets/.keep | 0 lib/spree_excel_import.rb | 103 +++++++++++++++ lib/tasks/.keep | 0 lib/tasks/spree_excel_import_tasks.rake | 6 + spree_excel_import.gemspec | 22 ++++ version | 1 + 25 files changed, 584 insertions(+), 18 deletions(-) delete mode 100644 .gitignore create mode 100644 .idea/.generators create mode 100644 .idea/.name create mode 100644 .idea/.rakeTasks create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/scopes/scope_settings.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 MIT-LICENSE delete mode 100644 README.md create mode 100644 README.rdoc create mode 100644 Rakefile create mode 100644 config.ru create mode 100644 lib/assets/.keep create mode 100644 lib/spree_excel_import.rb create mode 100644 lib/tasks/.keep create mode 100644 lib/tasks/spree_excel_import_tasks.rake create mode 100644 spree_excel_import.gemspec create mode 100644 version diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 54cb8bb..0000000 --- a/.gitignore +++ /dev/null @@ -1,16 +0,0 @@ -*.rbc -*.sassc -.sass-cache -capybara-*.html -.rspec -/.bundle -/vendor/bundle -/log/* -/tmp/* -/db/*.sqlite3 -/public/system/* -/coverage/ -/spec/tmp/* -**.orig -rerun.txt -pickle-email-*.html \ No newline at end of file diff --git a/.idea/.generators b/.idea/.generators new file mode 100644 index 0000000..236b5b5 --- /dev/null +++ b/.idea/.generators @@ -0,0 +1,8 @@ + + diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..8b6034d --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +spree_excel_import \ No newline at end of file diff --git a/.idea/.rakeTasks b/.idea/.rakeTasks new file mode 100644 index 0000000..df8b003 --- /dev/null +++ b/.idea/.rakeTasks @@ -0,0 +1,7 @@ + + diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..b22c943 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,25 @@ + + + + + + diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..3572571 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a0087c8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + 1.7 + + + + + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3876e2c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..3b00020 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..c80f219 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f68a903 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "https://rubygems.org" + +gemspec + diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..91277aa --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,168 @@ +PATH + remote: . + specs: + spree_excel_import (0.0.1) + spree_core (>= 1.3.0) + spree_core (>= 1.3.0) + +GEM + remote: https://rubygems.org/ + specs: + actionmailer (3.2.13) + actionpack (= 3.2.13) + mail (~> 2.5.3) + actionpack (3.2.13) + activemodel (= 3.2.13) + activesupport (= 3.2.13) + builder (~> 3.0.0) + erubis (~> 2.7.0) + journey (~> 1.0.4) + rack (~> 1.4.5) + rack-cache (~> 1.2) + rack-test (~> 0.6.1) + sprockets (~> 2.2.1) + active_utils (1.0.5) + activesupport (>= 2.3.11) + i18n + activemerchant (1.34.1) + active_utils (>= 1.0.2) + activesupport (>= 2.3.14) + builder (>= 2.0.0) + i18n + json (>= 1.5.1) + money + nokogiri (< 1.6.0) + activemodel (3.2.13) + activesupport (= 3.2.13) + builder (~> 3.0.0) + activerecord (3.2.13) + activemodel (= 3.2.13) + activesupport (= 3.2.13) + arel (~> 3.0.2) + tzinfo (~> 0.3.29) + activeresource (3.2.13) + activemodel (= 3.2.13) + activesupport (= 3.2.13) + activesupport (3.2.13) + i18n (= 0.6.1) + multi_json (~> 1.0) + acts_as_list (0.2.0) + activerecord (>= 3.0) + arel (3.0.2) + awesome_nested_set (2.1.5) + activerecord (>= 3.0.0) + aws-sdk (1.3.9) + httparty (~> 0.7) + json (~> 1.4) + nokogiri (>= 1.4.4) + uuidtools (~> 2.1) + builder (3.0.4) + cancan (1.6.8) + climate_control (0.0.3) + activesupport (>= 3.0) + cocaine (0.5.1) + climate_control (>= 0.0.3, < 1.0) + deface (0.9.1) + nokogiri (~> 1.5.0) + rails (~> 3.1) + erubis (2.7.0) + ffaker (1.16.2) + highline (1.6.18) + hike (1.2.3) + httparty (0.11.0) + multi_json (~> 1.0) + multi_xml (>= 0.5.2) + i18n (0.6.1) + journey (1.0.4) + json (1.8.0) + kaminari (0.13.0) + actionpack (>= 3.0.0) + activesupport (>= 3.0.0) + railties (>= 3.0.0) + mail (2.5.4) + mime-types (~> 1.16) + treetop (~> 1.4.8) + mime-types (1.23) + money (5.1.1) + i18n (~> 0.6.0) + multi_json (1.7.7) + multi_xml (0.5.4) + nokogiri (1.5.10) + paperclip (3.4.2) + activemodel (>= 3.0.0) + activerecord (>= 3.0.0) + activesupport (>= 3.0.0) + cocaine (~> 0.5.0) + mime-types + polyamorous (0.5.0) + activerecord (~> 3.0) + polyglot (0.3.3) + rack (1.4.5) + rack-cache (1.2) + rack (>= 0.4) + rack-ssl (1.3.3) + rack + rack-test (0.6.2) + rack (>= 1.0) + rails (3.2.13) + actionmailer (= 3.2.13) + actionpack (= 3.2.13) + activerecord (= 3.2.13) + activeresource (= 3.2.13) + activesupport (= 3.2.13) + bundler (~> 1.0) + railties (= 3.2.13) + railties (3.2.13) + actionpack (= 3.2.13) + activesupport (= 3.2.13) + rack-ssl (~> 1.3.2) + rake (>= 0.8.7) + rdoc (~> 3.4) + thor (>= 0.14.6, < 2.0) + rake (10.1.0) + ransack (0.7.2) + actionpack (~> 3.0) + activerecord (~> 3.0) + polyamorous (~> 0.5.0) + rdoc (3.12.2) + json (~> 1.4) + spree_core (2.0.3) + activemerchant (~> 1.31) + acts_as_list (= 0.2.0) + awesome_nested_set (= 2.1.5) + aws-sdk (~> 1.3.4) + cancan (= 1.6.8) + deface (>= 0.9.0) + ffaker (~> 1.16.1) + highline (= 1.6.18) + httparty (~> 0.11) + json (>= 1.7.7) + kaminari (= 0.13.0) + money (= 5.1.1) + paperclip (~> 3.4.1) + rails (~> 3.2.13) + ransack (= 0.7.2) + state_machine (= 1.2.0) + stringex (~> 1.5.1) + truncate_html (= 0.9.2) + sprockets (2.2.2) + hike (~> 1.2) + multi_json (~> 1.0) + rack (~> 1.0) + tilt (~> 1.1, != 1.3.0) + state_machine (1.2.0) + stringex (1.5.1) + thor (0.18.1) + tilt (1.4.1) + treetop (1.4.14) + polyglot + polyglot (>= 0.3.1) + truncate_html (0.9.2) + tzinfo (0.3.37) + uuidtools (2.1.4) + +PLATFORMS + ruby + +DEPENDENCIES + spree_excel_import! diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..5146945 --- /dev/null +++ b/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright 2013 YOURNAME + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md deleted file mode 100644 index daa4bec..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -spree_excel_import -================== diff --git a/README.rdoc b/README.rdoc new file mode 100644 index 0000000..8d5274e --- /dev/null +++ b/README.rdoc @@ -0,0 +1,3 @@ += SpreeExcelImport + +This project rocks and uses MIT-LICENSE. \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..a63a86e --- /dev/null +++ b/Rakefile @@ -0,0 +1,32 @@ +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' +end + +require 'rdoc/task' + +RDoc::Task.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'SpreeExcelImport' + rdoc.options << '--line-numbers' + rdoc.rdoc_files.include('README.rdoc') + rdoc.rdoc_files.include('lib/**/*.rb') +end + + + + +Bundler::GemHelper.install_tasks + +require 'rake/testtask' + +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = false +end + + +task default: :test diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..5bc2a61 --- /dev/null +++ b/config.ru @@ -0,0 +1,4 @@ +# This file is used by Rack-based servers to start the application. + +require ::File.expand_path('../config/environment', __FILE__) +run Rails.application diff --git a/lib/assets/.keep b/lib/assets/.keep new file mode 100644 index 0000000..e69de29 diff --git a/lib/spree_excel_import.rb b/lib/spree_excel_import.rb new file mode 100644 index 0000000..b159921 --- /dev/null +++ b/lib/spree_excel_import.rb @@ -0,0 +1,103 @@ +# encoding: UTF-8 +require 'roo' +require 'date' + +module SpreeExcelImport + class DataImport + def self.import(filename) + book = Roo::Excelx.new filename + puts "Starting import of " + filename + book.default_sheet = book.sheets.first + import_products book + end + + def self.import_products(book, taxon=nil) + properties = Hash.new + + title_row = book.row(1) + + title_row.each_with_index { |str, index| + arr = str.split(":") + if arr[0] == "Property" + properties[arr[1]] = index + end + } + + tax_category = Spree::TaxCategory.where("name = ?", "MVA").first + valid_rows = 0 + puts "Found " + book.last_row.to_s + " rows in the table" + 2.upto(book.last_row) do |row_num| + #2.upto(20) do |row_num| # to test + row = book.row(row_num) + if row[0] == "TRUE" + product = create_or_update_product(row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], tax_category, taxon) + properties.each { |key, value| + add_or_update_property(product, key, row[value]) + } + valid_rows += 1 + end + end + puts "Import successfully finished of " + valid_rows.to_s + " valid rows" + end + + def self.add_or_update_property(product, name, value) + # Because of excel conversion we would like to remove extra .0 added to floats + value = value.to_i if value.is_a?(Numeric) && value.to_i == value + property = Spree::Property.where("name = ?", name).first + return if (!property) + prop = Spree::ProductProperty.where("product_id = ? and property_id = ?", product, property).first + prop = Spree::ProductProperty.new() unless prop + prop.value = value + prop.product = product + prop.property = property + prop.save + end + + def self.create_or_update_product(sku, name, description, available_on, price, cost_price, count_on_hand, width, height, thickness, weight, tax_category, taxon) + master = Spree::Variant.where("sku = ?", sku).first + if master + product = master.product + product.update_column(:count_on_hand, count_on_hand) + else + product = Spree::Product.new() + + product.sku = sku + product.master = Spree::Variant.new( + :sku => sku, + :cost_currency => "NOK" + ) + + default_price = Spree::Price.new( + :currency => "NOK" + ) + product.master.default_price = default_price + end + + if taxon != nil + product.taxons.clear + product.taxons << taxon + end + product.name = name + product.tax_category = tax_category + product.description = description + product.available_on = available_on #DateTime.now - 1.day un + + product.master.is_master = true + product.master.count_on_hand = count_on_hand + product.master.cost_price= cost_price + product.master.default_price.amount = price + + product.height = height + product.width = width + product.depth = thickness + product.weight = weight + + product.save() + product + end + + def self.to_boolean(str) + str == 'TRUE' + end + end +end diff --git a/lib/tasks/.keep b/lib/tasks/.keep new file mode 100644 index 0000000..e69de29 diff --git a/lib/tasks/spree_excel_import_tasks.rake b/lib/tasks/spree_excel_import_tasks.rake new file mode 100644 index 0000000..c46ff74 --- /dev/null +++ b/lib/tasks/spree_excel_import_tasks.rake @@ -0,0 +1,6 @@ +namespace :db do + task :import_products => :environment do + desc "import products from products.xlsx" + SpreeExcelImport::DataImport.import_windows 'products.xlsx' + end +end \ No newline at end of file diff --git a/spree_excel_import.gemspec b/spree_excel_import.gemspec new file mode 100644 index 0000000..5db8494 --- /dev/null +++ b/spree_excel_import.gemspec @@ -0,0 +1,22 @@ +$:.push File.expand_path("../lib", __FILE__) + +# Maintain your gem's version: +version = File.read(File.expand_path("../version",__FILE__)).strip + +# Describe your gem and declare its dependencies: +Gem::Specification.new do |s| + s.name = "spree_excel_import" + s.version = version + s.author = "Erik Axel Nielsen" + s.email = "spree@illumina.no" + s.homepage = "https://github.com/erikaxel/spree_excel_import" + s.summary = "N/A" + s.description = "N/A" + + s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] + s.test_files = Dir["test/**/*"] + + s.has_rdoc = false + s.add_dependency 'spree_core', '>=1.3.0' + s.add_dependency 'roo', '>=1.9.3' +end diff --git a/version b/version new file mode 100644 index 0000000..8a9ecc2 --- /dev/null +++ b/version @@ -0,0 +1 @@ +0.0.1 \ No newline at end of file