workarea-avatax 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +20 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +37 -0
  4. data/.github/ISSUE_TEMPLATE/documentation-request.md +17 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. data/.github/workflows/ci.yml +66 -0
  7. data/.gitignore +18 -0
  8. data/.rubocop.yml +8 -0
  9. data/CHANGELOG.md +123 -0
  10. data/CODE_OF_CONDUCT.md +3 -0
  11. data/CONTRIBUTING.md +3 -0
  12. data/Gemfile +10 -0
  13. data/LICENSE.md +3 -0
  14. data/README.md +57 -0
  15. data/Rakefile +50 -0
  16. data/app/models/workarea/avatax/usage_type.rb +17 -0
  17. data/app/models/workarea/pricing/calculators/avalara_tax_calculator.rb +105 -0
  18. data/app/models/workarea/user.decorator +7 -0
  19. data/app/seeds/workarea/avatax_seeds.rb +44 -0
  20. data/app/view_models/workarea/admin/user_view_model.decorator +21 -0
  21. data/app/view_models/workarea/storefront/cart_view_model.decorator +7 -0
  22. data/app/views/workarea/admin/users/_avatax_properties_fields.html.haml +5 -0
  23. data/app/workers/workarea/avatax/tax_invoice_worker.rb +29 -0
  24. data/bin/rails +17 -0
  25. data/config/initializers/appends.rb +4 -0
  26. data/config/initializers/avatax.rb +14 -0
  27. data/config/initializers/seeds.rb +1 -0
  28. data/config/locales/en.yml +6 -0
  29. data/lib/avatax/api.decorator +52 -0
  30. data/lib/workarea/avatax/bogus_gateway/bogus_create_transaction.rb +187 -0
  31. data/lib/workarea/avatax/bogus_gateway.rb +34 -0
  32. data/lib/workarea/avatax/engine.rb +36 -0
  33. data/lib/workarea/avatax/tax_request/addresses.rb +35 -0
  34. data/lib/workarea/avatax/tax_request/line_item.rb +47 -0
  35. data/lib/workarea/avatax/tax_request/order_line_item.rb +64 -0
  36. data/lib/workarea/avatax/tax_request/order_lines.rb +70 -0
  37. data/lib/workarea/avatax/tax_request/response.rb +49 -0
  38. data/lib/workarea/avatax/tax_request/shipping_line_item.rb +34 -0
  39. data/lib/workarea/avatax/tax_request.rb +77 -0
  40. data/lib/workarea/avatax/version.rb +5 -0
  41. data/lib/workarea/avatax.rb +71 -0
  42. data/test/dummy/Rakefile +6 -0
  43. data/test/dummy/bin/bundle +3 -0
  44. data/test/dummy/bin/rails +4 -0
  45. data/test/dummy/bin/rake +4 -0
  46. data/test/dummy/bin/setup +34 -0
  47. data/test/dummy/bin/update +29 -0
  48. data/test/dummy/config/application.rb +23 -0
  49. data/test/dummy/config/boot.rb +5 -0
  50. data/test/dummy/config/cable.yml +9 -0
  51. data/test/dummy/config/environment.rb +5 -0
  52. data/test/dummy/config/environments/development.rb +54 -0
  53. data/test/dummy/config/environments/production.rb +86 -0
  54. data/test/dummy/config/environments/test.rb +44 -0
  55. data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
  56. data/test/dummy/config/initializers/assets.rb +11 -0
  57. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  58. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  59. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  60. data/test/dummy/config/initializers/inflections.rb +16 -0
  61. data/test/dummy/config/initializers/mime_types.rb +4 -0
  62. data/test/dummy/config/initializers/new_framework_defaults.rb +23 -0
  63. data/test/dummy/config/initializers/workarea.rb +5 -0
  64. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  65. data/test/dummy/config/locales/en.yml +23 -0
  66. data/test/dummy/config/puma.rb +47 -0
  67. data/test/dummy/config/routes.rb +5 -0
  68. data/test/dummy/config/secrets.yml +22 -0
  69. data/test/dummy/config/spring.rb +6 -0
  70. data/test/dummy/config.ru +5 -0
  71. data/test/dummy/db/seeds.rb +2 -0
  72. data/test/dummy/lib/assets/.keep +0 -0
  73. data/test/dummy/log/.keep +0 -0
  74. data/test/dummy/public/404.html +67 -0
  75. data/test/dummy/public/422.html +67 -0
  76. data/test/dummy/public/500.html +66 -0
  77. data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
  78. data/test/dummy/public/apple-touch-icon.png +0 -0
  79. data/test/dummy/public/favicon.ico +0 -0
  80. data/test/factories/workarea/avatax_factories.rb +142 -0
  81. data/test/lib/workarea/avatax/tax_request/order_line_items_test.rb +114 -0
  82. data/test/lib/workarea/avatax/tax_request/response_test.rb +42 -0
  83. data/test/lib/workarea/avatax/tax_request_test.rb +51 -0
  84. data/test/lib/workarea/avatax_test.rb +28 -0
  85. data/test/models/workarea/avatax/usage_type_test.rb +13 -0
  86. data/test/models/workarea/pricing/calculators/avalara_tax_calculator_test.rb +113 -0
  87. data/test/support/avatax_test_settings.rb +9 -0
  88. data/test/system/workarea/storefront/avatax_system_test.rb +22 -0
  89. data/test/test_helper.rb +20 -0
  90. data/test/vcr_cassettes/succesful_avatax_create_transaction.yml +60 -0
  91. data/test/vcr_cassettes/succesful_avatax_create_transaction_multiple_shippings.yml +76 -0
  92. data/workarea-avatax.gemspec +20 -0
  93. metadata +176 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 154de54ee351747a48c10892cc8ae997fea1584ccc63388295ad304ff35e195e
4
+ data.tar.gz: 3a06052d7b64aebabc4648482c553ce0b4012a2db1533ec19bf691425477c377
5
+ SHA512:
6
+ metadata.gz: 6b4e6f1e88d2d040c60b3f0b30b020bb31918fa02ad956ca80b58a9a663e30eaac0ab594f6a60a256ccce531e1710b63818b2d0b1696ce852eae16bde60e7dfb
7
+ data.tar.gz: 59a9e7d21976bd0b47e5b3c2d2345c89a793e780981a1ee548715bf173145dbf68d9cabc0775630b47b0cf910f3fb278fb2950144225d9ad202c41684b45adfa
data/.editorconfig ADDED
@@ -0,0 +1,20 @@
1
+ # editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ charset = utf-8
6
+ indent_style = space
7
+ end_of_line = lf
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+
11
+ [{*.rb,*.haml,*.decorator,*.yml,*.yaml,*.jbuilder}]
12
+ indent_size = 2
13
+ indent_style = space
14
+
15
+ [{*.js,*.jst,*.ejs,*.scss}]
16
+ indent_size = 4
17
+
18
+ [*.md]
19
+ indent_size = 4
20
+ trim_trailing_whitespace = false
@@ -0,0 +1,37 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve Workarea
4
+ title: ''
5
+ labels: bug
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ ⚠️**Before you create**⚠️
11
+ Please verify the issue you're experiencing is not part of your Workarea project customizations. The best way to do this is with a [vanilla Workarea installation](https://developer.workarea.com/articles/create-a-new-host-application.html). This will help us spend time on fixes/improvements for the whole community. Thank you!
12
+
13
+ **Describe the bug**
14
+ A clear and concise description of what the bug is.
15
+
16
+ **To Reproduce**
17
+ Steps to reproduce the behavior:
18
+ 1. Go to '...'
19
+ 2. Click on '....'
20
+ 3. Scroll down to '....'
21
+ 4. See error
22
+
23
+ **Expected behavior**
24
+ A clear and concise description of what you expected to happen.
25
+
26
+ **Workarea Setup (please complete the following information):**
27
+ - Workarea Version: [e.g. v3.4.6]
28
+ - Plugins [e.g. workarea-blog, workarea-sitemaps]
29
+
30
+ **Attachments**
31
+ If applicable, add any attachments to help explain your problem, things like:
32
+ - screenshots
33
+ - Gemfile.lock
34
+ - test cases
35
+
36
+ **Additional context**
37
+ Add any other context about the problem here.
@@ -0,0 +1,17 @@
1
+ ---
2
+ name: Documentation request
3
+ about: Suggest documentation
4
+ title: ''
5
+ labels: documentation
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your documentation related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm confused by [...]
12
+
13
+ **Describe the article you'd like**
14
+ A clear and concise description of what would be in the documentation article.
15
+
16
+ **Additional context**
17
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for Workarea
4
+ title: ''
5
+ labels: enhancement
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,66 @@
1
+ name: CI
2
+ on: [push]
3
+
4
+ jobs:
5
+ static_analysis:
6
+ runs-on: ubuntu-latest
7
+ steps:
8
+ - uses: actions/checkout@v1
9
+ - uses: workarea-commerce/ci/bundler-audit@v1
10
+ env:
11
+ GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_ACCESS_TOKEN }}
12
+ - uses: workarea-commerce/ci/rubocop@v1
13
+ env:
14
+ GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_ACCESS_TOKEN }}
15
+
16
+ admin_tests:
17
+ runs-on: ubuntu-latest
18
+ steps:
19
+ - uses: actions/checkout@v1
20
+ - uses: actions/setup-ruby@v1
21
+ with:
22
+ ruby-version: 2.6.x
23
+ - uses: workarea-commerce/ci/test@v1
24
+ with:
25
+ command: bin/rails app:workarea:test:admin
26
+ env:
27
+ GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_ACCESS_TOKEN }}
28
+
29
+ core_tests:
30
+ runs-on: ubuntu-latest
31
+ steps:
32
+ - uses: actions/checkout@v1
33
+ - uses: actions/setup-ruby@v1
34
+ with:
35
+ ruby-version: 2.6.x
36
+ - uses: workarea-commerce/ci/test@v1
37
+ with:
38
+ command: bin/rails app:workarea:test:core
39
+ env:
40
+ GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_ACCESS_TOKEN }}
41
+
42
+ storefront_tests:
43
+ runs-on: ubuntu-latest
44
+ steps:
45
+ - uses: actions/checkout@v1
46
+ - uses: actions/setup-ruby@v1
47
+ with:
48
+ ruby-version: 2.6.x
49
+ - uses: workarea-commerce/ci/test@v1
50
+ with:
51
+ command: bin/rails app:workarea:test:storefront
52
+ env:
53
+ GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_ACCESS_TOKEN }}
54
+
55
+ plugins_tests:
56
+ runs-on: ubuntu-latest
57
+ steps:
58
+ - uses: actions/checkout@v1
59
+ - uses: actions/setup-ruby@v1
60
+ with:
61
+ ruby-version: 2.6.x
62
+ - uses: workarea-commerce/ci/test@v1
63
+ with:
64
+ command: bin/rails app:workarea:test:plugins
65
+ env:
66
+ GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_ACCESS_TOKEN }}
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/db/*.sqlite3-journal
6
+ spec/dummy/log/*.log
7
+ spec/dummy/tmp/
8
+ spec/dummy/.sass-cache
9
+ test/dummy/db/*.sqlite3
10
+ test/dummy/db/*.sqlite3-journal
11
+ test/dummy/log/*.log
12
+ test/dummy/tmp/
13
+ test/dummy/.sass-cache
14
+ test/dummy/public/system/workarea
15
+ Gemfile.lock
16
+ coverage
17
+ .DS_Store
18
+ .byebug_history
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ inherit_from:
2
+ - https://raw.githubusercontent.com/workarea-commerce/workarea/master/.rubocop.yml
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - 'test/dummy/**/*'
7
+ - 'lib/workarea/payflow_pro.rb'
8
+ - 'lib/workarea/payflow_pro/**/*'
data/CHANGELOG.md ADDED
@@ -0,0 +1,123 @@
1
+ Workarea Avatax 4.2.0 (2019-11-26)
2
+ --------------------------------------------------------------------------------
3
+
4
+ * Support tax on non-shipping items
5
+
6
+ Since v3.5 supports non-shipping items natively, this checks those to
7
+ assign taxes from Avalara.
8
+ Ben Crouse
9
+
10
+
11
+
12
+ Workarea Avatax 4.1.2 (2019-09-17)
13
+ --------------------------------------------------------------------------------
14
+
15
+ * Fix when to display taxes in cart
16
+
17
+ Display taxes in cart if they have already been calculated.
18
+
19
+ AVATAX-38
20
+ Eric Pigeon
21
+
22
+
23
+
24
+ Workarea Avatax 4.1.1 (2019-07-23)
25
+ --------------------------------------------------------------------------------
26
+
27
+ * Pass addresses on order line items in tax request
28
+
29
+ Pass the addresses.shipTo adress on order line items when using the
30
+ split shipping plugin
31
+
32
+ AVATAX-34
33
+ Eric Pigeon
34
+
35
+
36
+
37
+ Workarea Avatax 4.1.0 (2018-12-20)
38
+ --------------------------------------------------------------------------------
39
+
40
+ * Add support for partial shippings
41
+
42
+ Update the tax request to split up order line items by
43
+ `Workarea::Shipping#quantities` for orders with multiple shippings
44
+
45
+ AVATAX-30
46
+ Eric Pigeon
47
+
48
+
49
+
50
+ Workarea Avatax 4.0.1 (2018-04-24)
51
+ --------------------------------------------------------------------------------
52
+
53
+ * Update seeds for Educational org
54
+
55
+ AVATAX-28
56
+ Eric Pigeon
57
+
58
+
59
+ Workarea Avatax 4.0.0 (2018-02-20)
60
+ --------------------------------------------------------------------------------
61
+
62
+ * Remove vendored AvaTax code, use gem from RubyGems
63
+
64
+ This had the potential to cause some issues when combining usage of the
65
+ workarea-avatax plugin with that of the workarea-address_verification
66
+ plugin.
67
+
68
+ AVATAX-27
69
+ Tom Scott
70
+
71
+ * Add timeout during pricing calculator
72
+
73
+ update vendored avatax gem files
74
+ add company code to request
75
+
76
+ AVATAX-25
77
+ Eric Pigeon
78
+
79
+
80
+ Workarea Avatax 3.0.1 (2017-09-06)
81
+ --------------------------------------------------------------------------------
82
+
83
+ * Update vendored version of avatax gem
84
+
85
+ Update vendored avatax gem. there's still some problems preventing use
86
+ of the cut version, notable it returning hashie mash responses instead
87
+ of faraday responses. memoize .resposne on a tax request to stop it
88
+ from creating a transaction everytime the response is accessed.
89
+
90
+ AVATAX-22
91
+ Eric Pigeon
92
+
93
+ * Fix bugs with commiting a tax transaction.
94
+
95
+ Commit fixes to tax worker to use correct symbolized keys on transaction.
96
+ fix bug with improper response reference on the invoice worker
97
+
98
+ AVATAX-22
99
+ Jeff Yucis
100
+
101
+
102
+ Workarea Avatax 3.0.0 (2017-07-06)
103
+ --------------------------------------------------------------------------------
104
+
105
+ * Update configuration and readme
106
+
107
+ Allow the endpoint to be set via secrets so apps and easily configure
108
+ their staging environments. Fix error sending money object instead of
109
+ string in rest call. Add timeout to faraday
110
+
111
+ AVATAX-20
112
+ Eric Pigeon
113
+
114
+ * Upgrade avatax for Workarea v3
115
+
116
+ Upgrade the avatax to use the newer version 2 rest api. They haven't
117
+ publicly released them gem so it's currently vendored into lib/avatax.
118
+ Update the tax calculator to build from price adjustments on the order
119
+ and store tax adjustments on the shipping to mirror how it works in
120
+ version 3.
121
+
122
+ AVATAX-20
123
+ Eric Pigeon
@@ -0,0 +1,3 @@
1
+ View this plugin's code of conduct here:
2
+
3
+ <https://github.com/workarea-commerce/workarea/blob/master/CODE_OF_CONDUCT.md>
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,3 @@
1
+ View this plugin's contribution guidelines here:
2
+
3
+ <https://github.com/workarea-commerce/workarea/blob/master/CONTRIBUTING.md>
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'workarea', git: 'https://github.com/workarea-commerce/workarea.git'
6
+ gem 'workarea-oms', git: "https://x-access-token:#{ENV['GITHUB_ACCESS_TOKEN']}@github.com/workarea-commerce/workarea-oms.git"
7
+
8
+ group :test do
9
+ gem "simplecov"
10
+ end
data/LICENSE.md ADDED
@@ -0,0 +1,3 @@
1
+ View this plugin's license here:
2
+
3
+ <https://github.com/workarea-commerce/workarea/blob/master/LICENSE.md>
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Avatax
2
+
3
+ Avatax by Avalara provides a centralized service for tracking sales tax bills.
4
+ This plugin integrates that service into the Workarea platform. Instead of using
5
+ the Workarea platform's included sales tax caclulation system, it uses Avatax to
6
+ calculate sales tax. After an order is placed, that order's sales tax bill is
7
+ transmitted to Avatax. Users may choose to commit (save them so that they are
8
+ reflected in tax liability) posted documents either via Workarea platform, or
9
+ via their own order management system.
10
+
11
+ ## Features
12
+
13
+ ### Out of the Box
14
+
15
+ * Full reporting feature with summarized and detailed reports
16
+ * Constant real time updates with the latest rules and regulations for accuracy
17
+
18
+ ## Requirements
19
+
20
+ * Avalara Avatax account (These values can be found in your Avalara administration console upon Registration)
21
+ * Account Number
22
+ * License Key
23
+ * API URL
24
+ * Shipping Item Code or Tax Code
25
+
26
+ ## Installation and Configuration
27
+
28
+ ### 1) Add the Application gem to your gemfile in host application
29
+
30
+ ```ruby
31
+ gem 'workarea-avatax', '~> <version>'
32
+ ```
33
+
34
+ ### 2) Add Avatax Secrets
35
+
36
+ ```ruby
37
+ avatax:
38
+ username: AVATAX_USERNAME
39
+ password: AVATAX_PASSWORD
40
+ ```
41
+
42
+ Optionally set the endpoint to sandbox for testing.
43
+
44
+ ### 3) Configure the avatax plugin with the merchant's distribution center inside the host app's `config/initializers/workarea.rb` file
45
+
46
+ ```ruby
47
+ Workarea::Avatax.configure do |config|
48
+ config.dist_center = {
49
+ Line1: '1234 Your St',
50
+ Line2: '',
51
+ City: 'YourCity',
52
+ Region: 'Your State',
53
+ Country: 'US',
54
+ PostalCode: '123456'
55
+ }
56
+ end
57
+ ```
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require "bundler/setup"
4
+ rescue LoadError
5
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
6
+ end
7
+
8
+ require "rdoc/task"
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = "rdoc"
11
+ rdoc.title = "Moneris"
12
+ rdoc.options << "--line-numbers"
13
+ rdoc.rdoc_files.include("README.md")
14
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load "rails/tasks/engine.rake"
19
+ load "rails/tasks/statistics.rake"
20
+ load "workarea/changelog.rake"
21
+
22
+ require "rake/testtask"
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << "lib"
25
+ t.libs << "test"
26
+ t.pattern = "test/**/*_test.rb"
27
+ t.verbose = false
28
+ end
29
+ task default: :test
30
+
31
+ $LOAD_PATH.unshift File.expand_path("lib", __dir__)
32
+ require "workarea/avatax/version"
33
+
34
+ desc "Release version #{Workarea::Avatax::VERSION} of the gem"
35
+ task :release do
36
+ host = "https://#{ENV['BUNDLE_GEMS__WEBLINC__COM']}@gems.weblinc.com"
37
+
38
+ Rake::Task["workarea:changelog"].execute
39
+ system "git add CHANGELOG.md"
40
+ system 'git commit -m "Update CHANGELOG"'
41
+ system "git push origin HEAD"
42
+
43
+ system "git tag -a v#{Workarea::Avatax::VERSION} -m 'Tagging #{Workarea::Avatax::VERSION}'"
44
+ system "git push --tags"
45
+
46
+ system "gem build workarea-avatax.gemspec"
47
+ system "gem push workarea-avatax-#{Workarea::Avatax::VERSION}.gem"
48
+ system "gem push workarea-avatax-#{Workarea::Avatax::VERSION}.gem --host #{host}"
49
+ system "rm workarea-avatax-#{Workarea::Avatax::VERSION}.gem"
50
+ end
@@ -0,0 +1,17 @@
1
+ module Workarea
2
+ module Avatax
3
+ class UsageType
4
+ include ApplicationDocument
5
+
6
+ field :code, type: String
7
+ field :name, type: String, localize: true
8
+ field :country_codes, type: Array
9
+
10
+ validates_presence_of :code, :name, :country_codes
11
+
12
+ def countries
13
+ country_codes.map { |code| Country[code] }.compact
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,105 @@
1
+ module Workarea
2
+ module Pricing
3
+ module Calculators
4
+ # Calculates order/shipping sales tax via the Avalara Avatax API
5
+ #
6
+ class AvalaraTaxCalculator
7
+ include Calculator
8
+
9
+ def adjust
10
+ response = Avatax::TaxRequest.new(
11
+ order: order,
12
+ shippings: shippings,
13
+ **request_options
14
+ ).response
15
+
16
+ return unless response.success?
17
+
18
+ shippings.each do |shipping|
19
+ next unless shipping.address.present?
20
+
21
+ price_adjustments_for(shipping).each do |adjustment|
22
+ tax_line = response.tax_line_for_adjustment adjustment, shipping: shipping
23
+ next unless tax_line.present?
24
+
25
+ adjust_pricing(
26
+ shipping,
27
+ tax_line,
28
+ "order_item_id" => adjustment._parent.id,
29
+ "adjustment" => adjustment.id
30
+ )
31
+ end
32
+
33
+ shipping_tax_line = response.tax_line_for_shipping(shipping)
34
+ adjust_pricing(shipping, shipping_tax_line, "shipping_service_tax" => true)
35
+ end
36
+
37
+ order.items.reject(&:shipping?).each do |non_shipped_item|
38
+ non_shipped_item.price_adjustments.each do |adjustment|
39
+ tax_line = response.tax_line_for_adjustment(adjustment)
40
+ next unless tax_line.present? && tax_line.tax.to_m > 0
41
+
42
+ data = {
43
+ 'adjustment' => adjustment.id,
44
+ 'order_item_id' => adjustment._parent.id,
45
+ 'tax_code' => adjustment.data['tax_code']
46
+ }
47
+
48
+ non_shipped_item.adjust_pricing(
49
+ price: 'tax',
50
+ calculator: self.class.name,
51
+ description: 'Item Tax',
52
+ amount: tax_line.tax.to_m,
53
+ data: data.merge(find_line_details(tax_line))
54
+ )
55
+ end
56
+ end
57
+
58
+ rescue Faraday::Error => error
59
+ Raven.capture_exception(error) if defined?(Raven)
60
+ avatax_fallback(error)
61
+ end
62
+
63
+ private
64
+
65
+ def request_options
66
+ { timeout: 2 }
67
+ end
68
+
69
+ def avatax_fallback(_error)
70
+ Pricing::Calculators::TaxCalculator.new(request).adjust
71
+ end
72
+ alias_method :handle_timeout_error, :avatax_fallback
73
+
74
+ # If doing split shipping (different items go to different shipping
75
+ # addresses), decorate this method to return the proper price
76
+ # adjustments that match the shipping. (This will have to be added to
77
+ # the UI and saved, probably on the Shipping object)
78
+ #
79
+ # @return [PriceAdjustmentSet]
80
+ #
81
+ def price_adjustments_for(shipping)
82
+ order.price_adjustments
83
+ end
84
+
85
+ def adjust_pricing(shipping, tax_line, data = {})
86
+ return if tax_line.tax.to_m.zero?
87
+
88
+ shipping.adjust_pricing(
89
+ price: "tax",
90
+ calculator: self.class.name,
91
+ description: "Sales Tax",
92
+ amount: tax_line.tax.to_m,
93
+ data: data.merge(find_line_details(tax_line))
94
+ )
95
+ end
96
+
97
+ def find_line_details(tax_line)
98
+ tax_line.details.each_with_object({}) do |detail, memo|
99
+ memo[detail.taxName] = detail.rate
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,7 @@
1
+ module Workarea
2
+ decorate User, with: :avatax do
3
+ decorated do
4
+ field :customer_usage_type, type: String, default: "TAXABLE"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,44 @@
1
+ module Workarea
2
+ class AvataxSeeds
3
+ def perform
4
+ return unless AvaTax.config[:username].present? && AvaTax.config[:password].present?
5
+ puts "Adding Avalara Usage Types..."
6
+
7
+ result = ::AvaTax.list_entity_use_codes
8
+ raise "Failed to get avatax usage codes from api" unless result.success?
9
+
10
+ result.body["value"].each do |entity_use_code|
11
+ Workarea::Avatax::UsageType.find_or_create_by(
12
+ code: entity_use_code["code"],
13
+ name: formatted_names.fetch(entity_use_code["name"], entity_use_code["name"]),
14
+ country_codes: entity_use_code["validCountries"]
15
+ )
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def formatted_names
22
+ {
23
+ "FEDERAL GOV" => "Federal Government",
24
+ "STATE GOV" => "State Government",
25
+ "TRIBAL GOVERNMENT" => "Tribal Government",
26
+ "FOREIGN DIPLOMAT" => "Foreign diplomat",
27
+ "CHARITABLE/EXEMPT ORG" => "Charitable or excempt org",
28
+ "RELIGIOUS ORG" => "Religious or educational org",
29
+ "EDUCATIONAL ORG" => "Educational org",
30
+ "RESALE" => "Resale",
31
+ "AGRICULTURE" => "Commercial agricultural production",
32
+ "INDUSTRIAL PROD/MANUFACTURERS" => "Industrial production / manufacturer",
33
+ "DIRECT PAY" => "Direct pay permit",
34
+ "DIRECT MAIL" => "Direct mail",
35
+ "OTHER/CUSTOM" => "Other or custom",
36
+ "LOCAL GOVERNMENT" => "Local Government",
37
+ "COMMERCIAL AQUACULTURE" => "Commercial aquaculture",
38
+ "COMMERCIAL FISHERY" => "Commercial fishery",
39
+ "NON-RESIDENT" => "Non-resident",
40
+ "NON-EXEMPT TAXABLE CUSTOMER" => "Non-excempt taxable customer"
41
+ }
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,21 @@
1
+ module Workarea
2
+ decorate Admin::UserViewModel, with: :avatax do
3
+ def avatax_usage_options
4
+ Avatax::UsageType.all.map do |usage_type|
5
+ [usage_type_display(usage_type), usage_type.code]
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def usage_type_display(usage_type)
12
+ countries =
13
+ if usage_type.countries.empty?
14
+ ""
15
+ else
16
+ "(#{usage_type.countries.map(&:local_name).join(', ')})"
17
+ end
18
+ [usage_type.name, countries].compact.join(' ')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module Workarea
2
+ decorate Storefront::CartViewModel, with: :avatax do
3
+ def show_taxes?
4
+ super && tax_total.present?
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ .property
2
+ = label_tag 'user[customer_usage_type]', nil, class: 'property__name' do
3
+ %span.property__text= t('workarea.admin.users.edit.avatax_entity_use_code')
4
+ .value
5
+ = select_tag 'user[customer_usage_type]', options_for_select(@user.avatax_usage_options, @user.customer_usage_type)