Skip to content

Commit

Permalink
initial commit, using daniel's code with spree extension generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Kim committed Nov 20, 2012
1 parent 8d0f358 commit 27fc06b
Show file tree
Hide file tree
Showing 19 changed files with 338 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'http://rubygems.org'

gemspec
26 changes: 26 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2012 [name of plugin creator]
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name Spree nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
require 'spree/core/testing_support/common_rake'

RSpec::Core::RakeTask.new

task :default => [:spec]

desc 'Generates a dummy app for testing'
task :test_app do
ENV['LIB_NAME'] = 'spree_avatax'
Rake::Task['common:test_app'].invoke
end
11 changes: 11 additions & 0 deletions Versionfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is used to designate compatibilty with different versions of Spree
# Please see http://spreecommerce.com/documentation/extensions.html#versionfile for details

# Examples
#
# '1.2.x' => { :branch => 'master' }
# '1.1.x' => { :branch => '1-1-stable' }
# '1.0.x' => { :branch => '1-0-stable' }
# '0.70.x' => { :branch => '0-70-stable' }
# '0.40.x' => { :tag => 'v1.0.0', :version => '1.0.0' }

1 change: 1 addition & 0 deletions app/assets/javascripts/admin/spree_avatax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= require admin/spree_core
1 change: 1 addition & 0 deletions app/assets/javascripts/store/spree_avatax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= require store/spree_core
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin/spree_avatax.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
*= require admin/spree_core
*/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/store/spree_avatax.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
*= require store/spree_core
*/
20 changes: 20 additions & 0 deletions app/avalara/avalara_tax_rate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Avalara
class AvalaraTaxRate < Spree::TaxRate
# Creates necessary tax adjustments for the order.
after_initialize :init
def init
self.name="AvaTax"
end



def adjust(order, response)
order.adjustments.create({:amount => response.total_tax,
:source => self,
:originator => self,
:locked => true,
:label => "Sales Tax"}, :without_protection => true)
end
end

end
12 changes: 12 additions & 0 deletions app/spree/line_item_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Spree::LineItem.class_eval do




def update_order
order.create_tax_charge!
order.update!
end


end
104 changes: 104 additions & 0 deletions app/spree/order_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
Spree::Order.class_eval do


# Finalizes an in progress order after checkout is complete.
# Called after transition to complete state when payments will have been processed
def finalize!
touch :completed_at
Spree::InventoryUnit.assign_opening_inventory(self)
# lock any optional adjustments (coupon promotions, etc.)
adjustments.optional.each { |adjustment| adjustment.update_column('locked', true) }
deliver_order_confirmation_email
response=post_order_to_avalara(true)

self.state_changes.create({
:previous_state => 'cart',
:next_state => 'complete',
:name => 'order' ,
:user_id => self.user_id
}, :without_protection => true)
end


def update_adjustments
#create_tax_charge!
self.adjustments.reload.each { |adjustment| adjustment.update!(self) }
end


def create_tax_charge!
self.clear_adjustments!
#create tax estimate and required adjustments.
#commit =false, do not create transaction on Avalara side.

#only relevant if we have a billing address
if self.billing_address != nil
response=post_order_to_avalara
create_avalara_tax_adjustments(response)
end
end


private
def post_order_to_avalara(commit=false)


#Create array for line items
tax_line_items=Array.new

self.line_items.each_with_index do |line_item, i|
line_item_total=line_item.price*line_item.quantity
line=Avalara::Request::Line.new(:line_no => i, :origin_code => 1, :destination_code => 1, :qty => line_item.quantity, :amount => line_item_total)
tax_line_items<<line
end
#Billing Address
address=Avalara::Request::Address.new(:address_code => 1)
address.line_1=self.billing_address.address1
address.postal_code=self.billing_address.zipcode
addresses=Array.new
addresses<<address
invoice=Avalara::Request::Invoice.new
invoice.doc_code=self.number
invoice.customer_code="TheRealReal"
invoice.addresses=addresses
invoice.lines=tax_line_items
#A record is created when commit is true + doc_type is SalesInvoice
if commit
invoice.commit=true
invoice.doc_type="SalesInvoice"
end
response=Avalara.get_tax(invoice)
response
end
#def create_avalara_tax_adjustments(tax_lines)
# puts "TAX LINE"
# #Spree::TaxRate.adjust(self)
# tax_lines.each { |tax_line|
# puts tax_line.tax_details.each { |tax_detail|
# self.adjustments.create({:amount => tax_detail.tax,
# :source => self,
# :originator => Spree::TaxRate.first,
# :locked => true,
# :label => tax_detail.tax_name}, :without_protection => true)
# }
# }
#end
def create_avalara_tax_adjustments(response)
puts "Creating tax adjustment"
#Spree::TaxRate.adjust(self)
self.adjustments.create({:amount => response.total_tax,
:source => self,
:originator => Spree::TaxRate.first,
:locked => true,
:label => "Sales Tax"}, :without_protection => true)
end
end
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
hello: "Hello world"
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Spree::Core::Engine.routes.draw do
# Add your extension routes here
end
29 changes: 29 additions & 0 deletions lib/generators/spree_avatax/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module SpreeAvatax
module Generators
class InstallGenerator < Rails::Generators::Base

def add_javascripts
append_file 'app/assets/javascripts/store/all.js', "//= require store/spree_avatax\n"
append_file 'app/assets/javascripts/admin/all.js', "//= require admin/spree_avatax\n"
end

def add_stylesheets
inject_into_file 'app/assets/stylesheets/store/all.css', " *= require store/spree_avatax\n", :before => /\*\//, :verbose => true
inject_into_file 'app/assets/stylesheets/admin/all.css', " *= require admin/spree_avatax\n", :before => /\*\//, :verbose => true
end

def add_migrations
run 'bundle exec rake railties:install:migrations FROM=spree_avatax'
end

def run_migrations
res = ask 'Would you like to run the migrations now? [Y/n]'
if res == '' || res.downcase == 'y'
run 'bundle exec rake db:migrate'
else
puts 'Skipping rake db:migrate, don\'t forget to run it!'
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/spree_avatax.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'spree_core'
require 'spree_avatax/engine'
22 changes: 22 additions & 0 deletions lib/spree_avatax/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module SpreeAvatax
class Engine < Rails::Engine
require 'spree/core'
isolate_namespace Spree
engine_name 'spree_avatax'

config.autoload_paths += %W(#{config.root}/lib)

# use rspec for tests
config.generators do |g|
g.test_framework :rspec
end

def self.activate
Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end

config.to_prepare &method(:activate).to_proc
end
end
7 changes: 7 additions & 0 deletions script/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/spree_avatax/engine', __FILE__)

require 'rails/all'
require 'rails/engine/commands'
44 changes: 44 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Configure Rails Environment
ENV['RAILS_ENV'] = 'test'

require File.expand_path('../dummy/config/environment.rb', __FILE__)

require 'rspec/rails'
require 'ffaker'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }

# Requires factories defined in spree_core
require 'spree/core/testing_support/factories'
require 'spree/core/url_helpers'

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods

# == URL Helpers
#
# Allows access to Spree's routes in specs:
#
# visit spree.admin_path
# current_path.should eql(spree.products_path)
config.include Spree::Core::UrlHelpers

# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
end
27 changes: 27 additions & 0 deletions spree_avatax.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# encoding: UTF-8
Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'spree_avatax'
s.version = '1.2.0'
s.summary = 'TODO: Add gem summary here'
s.description = 'TODO: Add (optional) gem description here'
s.required_ruby_version = '>= 1.8.7'

# s.author = 'You'
# s.email = '[email protected]'
# s.homepage = 'http://www.spreecommerce.com'

#s.files = `git ls-files`.split("\n")
#s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.require_path = 'lib'
s.requirements << 'none'

s.add_dependency 'spree_core', '~> 1.2.0'
s.add_dependency 'avalara'

s.add_development_dependency 'capybara', '1.0.1'
s.add_development_dependency 'factory_girl', '~> 2.6.4'
s.add_development_dependency 'ffaker'
s.add_development_dependency 'rspec-rails', '~> 2.9'
s.add_development_dependency 'sqlite3'
end

0 comments on commit 27fc06b

Please sign in to comment.