spree_correios_shipping 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +5 -0
  5. data/Gemfile.lock +270 -0
  6. data/LICENSE +26 -0
  7. data/README.md +45 -0
  8. data/Rakefile +29 -0
  9. data/Versionfile +14 -0
  10. data/app/assets/javascripts/spree/backend/product_packages/edit.js.coffee +4 -0
  11. data/app/assets/javascripts/spree/backend/product_packages/index.js.coffee +28 -0
  12. data/app/assets/javascripts/spree/backend/product_packages/new.js.coffee +7 -0
  13. data/app/controllers/spree/admin/correios_shipping_settings_controller.rb +18 -0
  14. data/app/controllers/spree/admin/product_packages_controller.rb +12 -0
  15. data/app/controllers/spree/checkout_controller_decorator.rb +9 -0
  16. data/app/models/spree/calculator/shipping/correios/base.rb +165 -0
  17. data/app/models/spree/calculator/shipping/correios/e_sedex.rb +16 -0
  18. data/app/models/spree/calculator/shipping/correios/e_sedex_express.rb +17 -0
  19. data/app/models/spree/calculator/shipping/correios/e_sedex_grupo1.rb +17 -0
  20. data/app/models/spree/calculator/shipping/correios/e_sedex_grupo2.rb +17 -0
  21. data/app/models/spree/calculator/shipping/correios/e_sedex_grupo3.rb +17 -0
  22. data/app/models/spree/calculator/shipping/correios/e_sedex_prioritario.rb +17 -0
  23. data/app/models/spree/calculator/shipping/correios/overweight.rb +25 -0
  24. data/app/models/spree/calculator/shipping/correios/pac.rb +17 -0
  25. data/app/models/spree/calculator/shipping/correios/pac_com_contrato.rb +17 -0
  26. data/app/models/spree/calculator/shipping/correios/sedex.rb +17 -0
  27. data/app/models/spree/calculator/shipping/correios/sedex10.rb +17 -0
  28. data/app/models/spree/calculator/shipping/correios/sedex_a_cobrar.rb +17 -0
  29. data/app/models/spree/calculator/shipping/correios/sedex_a_cobrar_com_contrato.rb +17 -0
  30. data/app/models/spree/calculator/shipping/correios/sedex_com_contrato1.rb +17 -0
  31. data/app/models/spree/calculator/shipping/correios/sedex_com_contrato2.rb +17 -0
  32. data/app/models/spree/calculator/shipping/correios/sedex_com_contrato3.rb +17 -0
  33. data/app/models/spree/calculator/shipping/correios/sedex_com_contrato4.rb +17 -0
  34. data/app/models/spree/calculator/shipping/correios/sedex_com_contrato5.rb +17 -0
  35. data/app/models/spree/calculator/shipping/correios/sedex_hoje.rb +17 -0
  36. data/app/models/spree/product_package.rb +9 -0
  37. data/app/models/spree/stock/splitter/correios_package.rb +51 -0
  38. data/app/models/spree/stock/splitter/correios_weight.rb +31 -0
  39. data/app/models/spree/stock_location_decorator.rb +10 -0
  40. data/app/overrides/spree/admin/shared/sub_menu/_configuration/add_correios_shipping_settings_tab.html.erb.deface +3 -0
  41. data/app/views/spree/admin/correios_shipping_settings/edit.html.erb +28 -0
  42. data/app/views/spree/admin/product_packages/_form.html.erb +31 -0
  43. data/app/views/spree/admin/product_packages/edit.html.erb +20 -0
  44. data/app/views/spree/admin/product_packages/index.html.erb +49 -0
  45. data/app/views/spree/admin/product_packages/new.html.erb +14 -0
  46. data/app/views/spree/admin/shared/_correios_tabs.html.erb +11 -0
  47. data/config/locales/en.yml +7 -0
  48. data/config/locales/pt-BR.yml +19 -0
  49. data/config/routes.rb +7 -0
  50. data/db/migrate/20150303111955_create_product_packages.rb +11 -0
  51. data/lib/generators/spree_correios_shipping/install/install_generator.rb +19 -0
  52. data/lib/spree/correios_shipping_configuration.rb +10 -0
  53. data/lib/spree/shipping_error.rb +3 -0
  54. data/lib/spree_correios_shipping/engine.rb +69 -0
  55. data/lib/spree_correios_shipping/version.rb +3 -0
  56. data/lib/spree_correios_shipping.rb +3 -0
  57. data/script/rails +7 -0
  58. data/spec/spec_helper.rb +44 -0
  59. data/spree_correios_shipping.gemspec +30 -0
  60. metadata +201 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c412c1b1303a8d164512c2a132cbc0dc9ef375d7
4
+ data.tar.gz: 1118dc4a8cf8a87e95aae84bd3414e2c80b6390a
5
+ SHA512:
6
+ metadata.gz: 199ed99b11cb4e2b41d8a4a812d39e6dea7a72951c04e57f8151d3cd134e1171bc487d4772b3fb2dc9655820fc9ea3da57a046c46f00ffa12c56346be16976cf
7
+ data.tar.gz: 04d021a16b87a948d61cc9d8ca71a365213f247a5b4d9bdd3e9daa361ec86f3505128d986197a693e65ef7d012e836f61be9dcdeeb2e0a4870bba65568a382c4
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ \#*
2
+ *~
3
+ .#*
4
+ .DS_Store
5
+ .idea
6
+ .project
7
+ tmp
8
+ nbproject
9
+ *.swp
10
+ spec/dummy
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ #gem 'correios-frete'
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,270 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ spree_correios_shipping (1.1.8)
5
+ correios-frete
6
+ spree_core (~> 3.0.0.beta)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ actionmailer (4.2.0)
12
+ actionpack (= 4.2.0)
13
+ actionview (= 4.2.0)
14
+ activejob (= 4.2.0)
15
+ mail (~> 2.5, >= 2.5.4)
16
+ rails-dom-testing (~> 1.0, >= 1.0.5)
17
+ actionpack (4.2.0)
18
+ actionview (= 4.2.0)
19
+ activesupport (= 4.2.0)
20
+ rack (~> 1.6.0)
21
+ rack-test (~> 0.6.2)
22
+ rails-dom-testing (~> 1.0, >= 1.0.5)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.1)
24
+ actionview (4.2.0)
25
+ activesupport (= 4.2.0)
26
+ builder (~> 3.1)
27
+ erubis (~> 2.7.0)
28
+ rails-dom-testing (~> 1.0, >= 1.0.5)
29
+ rails-html-sanitizer (~> 1.0, >= 1.0.1)
30
+ activejob (4.2.0)
31
+ activesupport (= 4.2.0)
32
+ globalid (>= 0.3.0)
33
+ activemerchant (1.46.0)
34
+ activesupport (>= 3.2.14, < 5.0.0)
35
+ builder (>= 2.1.2, < 4.0.0)
36
+ i18n (>= 0.6.9)
37
+ nokogiri (~> 1.4)
38
+ activemodel (4.2.0)
39
+ activesupport (= 4.2.0)
40
+ builder (~> 3.1)
41
+ activerecord (4.2.0)
42
+ activemodel (= 4.2.0)
43
+ activesupport (= 4.2.0)
44
+ arel (~> 6.0)
45
+ activesupport (4.2.0)
46
+ i18n (~> 0.7)
47
+ json (~> 1.7, >= 1.7.7)
48
+ minitest (~> 5.1)
49
+ thread_safe (~> 0.3, >= 0.3.4)
50
+ tzinfo (~> 1.1)
51
+ acts_as_list (0.6.0)
52
+ activerecord (>= 3.0)
53
+ addressable (2.3.7)
54
+ arel (6.0.0)
55
+ awesome_nested_set (3.0.2)
56
+ activerecord (>= 4.0.0, < 5)
57
+ builder (3.2.2)
58
+ camertron-eprun (1.1.0)
59
+ cancancan (1.10.1)
60
+ capybara (1.0.1)
61
+ mime-types (>= 1.16)
62
+ nokogiri (>= 1.3.3)
63
+ rack (>= 1.0.0)
64
+ rack-test (>= 0.5.4)
65
+ selenium-webdriver (~> 2.0)
66
+ xpath (~> 0.1.4)
67
+ carmen (1.0.1)
68
+ unicode_utils (~> 1.4.0)
69
+ childprocess (0.5.5)
70
+ ffi (~> 1.0, >= 1.0.11)
71
+ cldr-plurals-runtime-rb (1.0.0)
72
+ climate_control (0.0.3)
73
+ activesupport (>= 3.0)
74
+ cocaine (0.5.5)
75
+ climate_control (>= 0.0.3, < 1.0)
76
+ colorize (0.7.5)
77
+ correios-frete (1.9.3)
78
+ log-me (= 0.0.7)
79
+ nokogiri (~> 1.6.2)
80
+ sax-machine (~> 0.2.1)
81
+ css_parser (1.3.6)
82
+ addressable
83
+ deface (1.0.1)
84
+ colorize (>= 0.5.8)
85
+ nokogiri (~> 1.6.0)
86
+ polyglot
87
+ rails (>= 3.1)
88
+ diff-lcs (1.2.5)
89
+ erubis (2.7.0)
90
+ factory_girl (2.6.4)
91
+ activesupport (>= 2.3.9)
92
+ ffaker (1.32.1)
93
+ ffi (1.9.6)
94
+ font-awesome-rails (4.3.0.0)
95
+ railties (>= 3.2, < 5.0)
96
+ friendly_id (5.1.0)
97
+ activerecord (>= 4.0.0)
98
+ globalid (0.3.3)
99
+ activesupport (>= 4.1.0)
100
+ highline (1.6.21)
101
+ hike (1.2.3)
102
+ htmlentities (4.3.3)
103
+ httparty (0.13.3)
104
+ json (~> 1.8)
105
+ multi_xml (>= 0.5.2)
106
+ i18n (0.7.0)
107
+ json (1.8.2)
108
+ kaminari (0.16.3)
109
+ actionpack (>= 3.0.0)
110
+ activesupport (>= 3.0.0)
111
+ log-me (0.0.7)
112
+ loofah (2.0.1)
113
+ nokogiri (>= 1.5.9)
114
+ mail (2.6.3)
115
+ mime-types (>= 1.16, < 3)
116
+ mime-types (2.4.3)
117
+ mini_portile (0.6.2)
118
+ minitest (5.5.1)
119
+ monetize (1.1.0)
120
+ money (~> 6.5.0)
121
+ money (6.5.1)
122
+ i18n (>= 0.6.4, <= 0.7.0)
123
+ multi_json (1.10.1)
124
+ multi_xml (0.5.5)
125
+ nokogiri (1.6.6.2)
126
+ mini_portile (~> 0.6.0)
127
+ paperclip (4.2.1)
128
+ activemodel (>= 3.0.0)
129
+ activesupport (>= 3.0.0)
130
+ cocaine (~> 0.5.3)
131
+ mime-types
132
+ paranoia (2.1.0)
133
+ activerecord (~> 4.0)
134
+ pg (0.18.1)
135
+ polyamorous (1.1.0)
136
+ activerecord (>= 3.0)
137
+ polyglot (0.3.5)
138
+ premailer (1.8.4)
139
+ css_parser (>= 1.3.6)
140
+ htmlentities (>= 4.0.0)
141
+ premailer-rails (1.8.0)
142
+ actionmailer (>= 3, < 5)
143
+ premailer (~> 1.7, >= 1.7.9)
144
+ rack (1.6.0)
145
+ rack-test (0.6.3)
146
+ rack (>= 1.0)
147
+ rails (4.2.0)
148
+ actionmailer (= 4.2.0)
149
+ actionpack (= 4.2.0)
150
+ actionview (= 4.2.0)
151
+ activejob (= 4.2.0)
152
+ activemodel (= 4.2.0)
153
+ activerecord (= 4.2.0)
154
+ activesupport (= 4.2.0)
155
+ bundler (>= 1.3.0, < 2.0)
156
+ railties (= 4.2.0)
157
+ sprockets-rails
158
+ rails-deprecated_sanitizer (1.0.3)
159
+ activesupport (>= 4.2.0.alpha)
160
+ rails-dom-testing (1.0.5)
161
+ activesupport (>= 4.2.0.beta, < 5.0)
162
+ nokogiri (~> 1.6.0)
163
+ rails-deprecated_sanitizer (>= 1.0.1)
164
+ rails-html-sanitizer (1.0.1)
165
+ loofah (~> 2.0)
166
+ railties (4.2.0)
167
+ actionpack (= 4.2.0)
168
+ activesupport (= 4.2.0)
169
+ rake (>= 0.8.7)
170
+ thor (>= 0.18.1, < 2.0)
171
+ rake (10.4.2)
172
+ ransack (1.4.1)
173
+ actionpack (>= 3.0)
174
+ activerecord (>= 3.0)
175
+ activesupport (>= 3.0)
176
+ i18n
177
+ polyamorous (~> 1.1)
178
+ responders (2.1.0)
179
+ railties (>= 4.2.0, < 5)
180
+ rspec-collection_matchers (1.1.2)
181
+ rspec-expectations (>= 2.99.0.beta1)
182
+ rspec-core (2.99.2)
183
+ rspec-expectations (2.99.2)
184
+ diff-lcs (>= 1.1.3, < 2.0)
185
+ rspec-mocks (2.99.3)
186
+ rspec-rails (2.99.0)
187
+ actionpack (>= 3.0)
188
+ activemodel (>= 3.0)
189
+ activesupport (>= 3.0)
190
+ railties (>= 3.0)
191
+ rspec-collection_matchers
192
+ rspec-core (~> 2.99.0)
193
+ rspec-expectations (~> 2.99.0)
194
+ rspec-mocks (~> 2.99.0)
195
+ rubyzip (1.1.7)
196
+ sax-machine (0.2.1)
197
+ nokogiri (~> 1.6.0)
198
+ selenium-webdriver (2.45.0)
199
+ childprocess (~> 0.5)
200
+ multi_json (~> 1.0)
201
+ rubyzip (~> 1.0)
202
+ websocket (~> 1.0)
203
+ spree_core (3.0.0.rc3)
204
+ activemerchant (~> 1.46.0)
205
+ acts_as_list (~> 0.6)
206
+ awesome_nested_set (~> 3.0.1)
207
+ cancancan (~> 1.10.1)
208
+ carmen (~> 1.0.0)
209
+ deface (~> 1.0.0)
210
+ ffaker (~> 1.16)
211
+ font-awesome-rails (~> 4.0)
212
+ friendly_id (~> 5.1.0)
213
+ highline (~> 1.6.18)
214
+ httparty (~> 0.11)
215
+ json (~> 1.7)
216
+ kaminari (~> 0.15, >= 0.15.1)
217
+ monetize (~> 1.1)
218
+ paperclip (~> 4.2.0)
219
+ paranoia (~> 2.1.0)
220
+ premailer-rails
221
+ rails (~> 4.2.0)
222
+ ransack (~> 1.4.1)
223
+ responders
224
+ state_machines-activerecord (~> 0.2)
225
+ stringex
226
+ truncate_html (= 0.9.2)
227
+ twitter_cldr (~> 3.0)
228
+ sprockets (2.12.3)
229
+ hike (~> 1.2)
230
+ multi_json (~> 1.0)
231
+ rack (~> 1.0)
232
+ tilt (~> 1.1, != 1.3.0)
233
+ sprockets-rails (2.2.4)
234
+ actionpack (>= 3.0)
235
+ activesupport (>= 3.0)
236
+ sprockets (>= 2.8, < 4.0)
237
+ state_machines (0.2.2)
238
+ state_machines-activemodel (0.1.2)
239
+ activemodel (~> 4.1)
240
+ state_machines (~> 0.2.0)
241
+ state_machines-activerecord (0.2.0)
242
+ activerecord (~> 4.1)
243
+ state_machines-activemodel (~> 0.1.0)
244
+ stringex (2.5.2)
245
+ thor (0.19.1)
246
+ thread_safe (0.3.4)
247
+ tilt (1.4.1)
248
+ truncate_html (0.9.2)
249
+ twitter_cldr (3.1.2)
250
+ camertron-eprun
251
+ cldr-plurals-runtime-rb (~> 1.0.0)
252
+ json
253
+ tzinfo
254
+ tzinfo (1.2.2)
255
+ thread_safe (~> 0.1)
256
+ unicode_utils (1.4.0)
257
+ websocket (1.2.1)
258
+ xpath (0.1.4)
259
+ nokogiri (~> 1.3)
260
+
261
+ PLATFORMS
262
+ ruby
263
+
264
+ DEPENDENCIES
265
+ capybara (= 1.0.1)
266
+ factory_girl (~> 2.6.4)
267
+ ffaker
268
+ pg
269
+ rspec-rails (~> 2.9)
270
+ spree_correios_shipping!
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2012-2015 Locomotiva.pro
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name Spree nor the names of its contributors may be used to
13
+ endorse or promote products derived from this software without specific
14
+ prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ SpreeCorreiosShipping
2
+ =====================
3
+ [![Code Climate](https://codeclimate.com/github/locomotivapro/spree_correios_shipping/badges/gpa.svg)](https://codeclimate.com/github/locomotivapro/spree_correios_shipping)
4
+
5
+ This is a gem to allow use Correios service for shipping methods in spree
6
+ Currently just tested and working with spree > 3.1
7
+
8
+ ## Installation
9
+
10
+ 1. Add the following to your Gemfile
11
+
12
+ gem 'spree_correios_shipping'
13
+
14
+ 2. Run `bundle install`
15
+
16
+ 3. To copy and apply migrations run: `rails g spree_correios_shipping:install`
17
+
18
+ You will need setup some confs in your app
19
+
20
+ ## Configuring
21
+
22
+ 1. Go to settings and add preferences of settings to Correios.
23
+
24
+ 2. You need to add addresses to every stock location you create/have.
25
+
26
+ 3. Add a shipping method and select the apropriated delivery method.
27
+
28
+ ## To-Do
29
+
30
+ - Write tests
31
+ - Write docs
32
+ - Implement package calculation
33
+
34
+ Testing
35
+ -------
36
+
37
+ Currently there are no tests!!
38
+
39
+ Be sure to bundle your dependencies and then create a dummy test app for the specs to run against.
40
+
41
+ $ bundle
42
+ $ bundle exec rake test_app
43
+ $ bundle exec rspec spec
44
+
45
+ Copyright (c) 2012-2015 [Locomotiva.pro](http://locomotiva.pro), released under the New BSD License
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/packagetask'
4
+ require 'rubygems/package_task'
5
+ require 'rspec/core/rake_task'
6
+ require 'spree/core/testing_support/common_rake'
7
+
8
+ RSpec::Core::RakeTask.new
9
+
10
+ task :default => [:spec]
11
+
12
+ spec = eval(File.read('spree_correios_shipping.gemspec'))
13
+
14
+ Gem::PackageTask.new(spec) do |p|
15
+ p.gem_spec = spec
16
+ end
17
+
18
+ desc 'Release to gemcutter'
19
+ task :release => :package do
20
+ require 'rake/gemcutter'
21
+ Rake::Gemcutter::Tasks.new(spec).define
22
+ Rake::Task['gem:push'].invoke
23
+ end
24
+
25
+ desc 'Generates a dummy app for testing'
26
+ task :test_app do
27
+ ENV['LIB_NAME'] = 'spree_correios_shipping'
28
+ Rake::Task['common:test_app'].invoke
29
+ end
data/Versionfile ADDED
@@ -0,0 +1,14 @@
1
+ # This file is used to designate compatibilty with different versions of Spree
2
+ # Please see http://spreecommerce.com/documentation/extensions.html#versionfile for details
3
+
4
+ # Examples
5
+ #
6
+ # '1.2.x' => { :branch => 'master' }
7
+ # '1.1.x' => { :branch => '1-1-stable' }
8
+ # '1.0.x' => { :branch => '1-0-stable' }
9
+ # '0.70.x' => { :branch => '0-70-stable' }
10
+ # '0.40.x' => { :tag => 'v1.0.0', :version => '1.0.0' }
11
+
12
+ '3.0.x' => { :branch => '3-0-stable'}
13
+ '1.2.x' => { :branch => '1-2-stable'}
14
+
@@ -0,0 +1,4 @@
1
+ ($ '#cancel_link').click (event) ->
2
+ event.preventDefault()
3
+
4
+ ($ '#product_packages').html('')
@@ -0,0 +1,28 @@
1
+ $ ->
2
+ ($ '#new_product_package_link').click (event) ->
3
+ event.preventDefault()
4
+
5
+ ($ '.no-objects-found').hide()
6
+
7
+ ($ this).hide()
8
+ $.ajax
9
+ type: 'GET'
10
+ url: @href
11
+ data: (
12
+ authenticity_token: AUTH_TOKEN
13
+ )
14
+ success: (r) ->
15
+ ($ '#product_packages').html r
16
+
17
+ ($ 'a.edit').click (event) ->
18
+ event.preventDefault()
19
+
20
+ ($ '#product_packages').html('')
21
+ $.ajax
22
+ type: 'GET'
23
+ url: @href
24
+ data: (
25
+ authenticity_token: AUTH_TOKEN
26
+ )
27
+ success: (r) ->
28
+ ($ '#product_packages').html r
@@ -0,0 +1,7 @@
1
+ ($ '#cancel_link').click (event) ->
2
+ event.preventDefault()
3
+
4
+ ($ '.no-objects-found').show()
5
+
6
+ ($ '#new_product_package_link').show()
7
+ ($ '#product_packages').html('')
@@ -0,0 +1,18 @@
1
+ class Spree::Admin::CorreiosShippingSettingsController < Spree::Admin::BaseController
2
+ def edit
3
+ @preferences = [:id_correios, :password_correios, :services, :default_item_weight, :max_shipping_weight, :split_shipments]
4
+ @config = Spree::CorreiosShippingConfiguration.new
5
+ end
6
+
7
+ def update
8
+ config = Spree::CorreiosShippingConfiguration.new
9
+
10
+ params.each do |name, value|
11
+ next unless config.has_preference? name
12
+ config[name] = value
13
+ end
14
+
15
+ redirect_to edit_admin_correios_shipping_settings_path
16
+ end
17
+
18
+ end
@@ -0,0 +1,12 @@
1
+ module Spree
2
+ module Admin
3
+ class ProductPackagesController < ResourceController
4
+
5
+ private
6
+
7
+ def permitted_product_package_attributes
8
+ [:length, :width, :height, :weight]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ Spree::CheckoutController.class_eval do
2
+ rescue_from Spree::ShippingError, :with => :handle_shipping_error
3
+
4
+ private
5
+ def handle_shipping_error(e)
6
+ flash[:error] = e.message
7
+ redirect_to checkout_state_path(:address)
8
+ end
9
+ end
@@ -0,0 +1,165 @@
1
+ require 'digest/md5'
2
+ require 'ostruct'
3
+ require_dependency 'spree/shipping_calculator'
4
+
5
+ module Spree
6
+ module Calculator::Shipping
7
+ module Correios
8
+ class Base < ShippingCalculator
9
+
10
+ def available?(package)
11
+ is_package_shippable?(package)
12
+
13
+ !compute(package).nil?
14
+ rescue Spree::ShippingError
15
+ false
16
+ end
17
+
18
+ def self.description
19
+ raise StandardError, "Subclass must implement description"
20
+ end
21
+
22
+ def self.service_code
23
+ raise StandardError, "Subclass must implement service code"
24
+ end
25
+
26
+ def compute_package(package)
27
+ order = package.order
28
+ stock_location = package.stock_location
29
+
30
+ origin = build_location(stock_location)
31
+ destination = build_location(order.ship_address)
32
+ service_code = self.class.service_code
33
+
34
+ rates_result = retrieve_rates_from_cache(package, origin, destination)
35
+
36
+ return nil if rates_result.kind_of?(Spree::ShippingError)
37
+ return nil if rates_result.empty?
38
+ return nil unless rates_result.is_a?(Hash)
39
+ rate = rates_result[service_code][:price]
40
+
41
+ return nil unless rate
42
+ return nil if rate.to_f == 0.0
43
+
44
+ return rate
45
+ end
46
+
47
+ private
48
+ def is_package_shippable?(package)
49
+ if Spree::CorreiosShipping::Config[:split_shipments]
50
+ heavy_items = package.contents.select { |content_item| content_item.variant.weight.to_f >= max_allowed_weight }
51
+ heavy_items.empty?
52
+ else
53
+ package_weight(package) <= max_allowed_weight
54
+ end
55
+ end
56
+
57
+ def max_allowed_weight
58
+ @max_allowed_weight ||= (Spree::CorreiosShipping::Config[:max_shipping_weight] || 30.0).to_f
59
+ end
60
+
61
+ def package_weight(package)
62
+ default_weight = Spree::CorreiosShipping::Config[:default_item_weight].to_f
63
+
64
+ package_weight = package.contents.inject(0.0) do |total_weight, content_item|
65
+ item_weight = content_item.variant.weight.to_f
66
+ item_weight = default_weight if item_weight <= 0
67
+ total_weight += item_weight * content_item.quantity
68
+ end
69
+
70
+ package_weight
71
+ end
72
+
73
+ def cache_key(package)
74
+ stock_location = package.stock_location.nil? ? "" : "#{package.stock_location.id}-"
75
+ order = package.order
76
+ ship_address = package.order.ship_address
77
+ contents_hash = Digest::MD5.hexdigest(package.contents.map {|content_item| content_item.variant.id.to_s + "_" + content_item.quantity.to_s }.join("|"))
78
+ @cache_key = "#{stock_location}-#{order.number}-#{ship_address.country.iso}-#{fetch_best_state_from_address(ship_address)}-#{ship_address.city}-#{ship_address.zipcode}-#{contents_hash}-#{I18n.locale}".gsub(" ","")
79
+ end
80
+
81
+ def fetch_best_state_from_address address
82
+ address.state ? address.state.abbr : address.state_name
83
+ end
84
+
85
+ def build_location address
86
+ location = Struct.new(:country, :state, :city, :zipcode)
87
+
88
+ location.new(address.country.iso,
89
+ fetch_best_state_from_address(address),
90
+ address.city,
91
+ address.zipcode)
92
+ end
93
+
94
+ def map_from_shipping_methods
95
+ available_shipping_methods = Spree::ShippingMethod.all
96
+ correios_shipping_methods = available_shipping_methods.select { |sm| sm.calculator.type.constantize.superclass == Spree::Calculator::Shipping::Correios::Base }
97
+ correios_shipping_methods.map! { |sm| sm.calculator.type.constantize.service_code }
98
+ end
99
+
100
+ def retrieve_rates(origin, destination, package)
101
+ #begin
102
+ services = Spree::CorreiosShipping::Config[:services].split(',')
103
+ services = map_from_shipping_methods if services.empty?
104
+ services.map! { |s| s.is_a?(Symbol) ? s : s.strip.to_sym }
105
+ webservice = ::Correios::Frete::Calculador.new :cep_origem => origin.zipcode,
106
+ :cep_destino => destination.zipcode,
107
+ :peso => package_weight(package),
108
+ :comprimento => 30,
109
+ :largura => 15,
110
+ :altura => 2,
111
+ :codigo_empresa => Spree::CorreiosShipping::Config[:id_correios],
112
+ :senha => Spree::CorreiosShipping::Config[:password_correios]
113
+
114
+ response = webservice.calculate(*services)
115
+
116
+ unless response.nil?
117
+ return {response.nome.downcase.to_sym => {price: response.valor, delivery_time: response.prazo_entrega}} unless response.is_a?(Hash)
118
+ response_hash = {}
119
+ services.each do |service|
120
+ response_hash[service] = {price: response[service].valor, delivery_time: response[service].prazo_entrega}
121
+ end
122
+ end
123
+
124
+ response_hash
125
+ #rescue => e
126
+ #message = e.message
127
+ #error = Spree::ShippingError.new("#{I18n.t(:shipping_error)}: #{message}")
128
+ #Rails.cache.write @cache_key, error #write error to cache to prevent constant re-lookups
129
+ #raise error
130
+ #end
131
+ end
132
+
133
+ def retrieve_rates_from_cache package, origin, destination
134
+ Rails.cache.fetch(cache_key(package)) do
135
+ retrieve_rates(origin, destination, package)
136
+ end
137
+ end
138
+
139
+ #def retrieve_timings(origin, destination, packages)
140
+ #begin
141
+ #if carrier.respond_to?(:find_time_in_transit)
142
+ #response = carrier.find_time_in_transit(origin, destination, packages)
143
+ #return response
144
+ #end
145
+ #rescue ActiveMerchant::Shipping::ResponseError => re
146
+ #if re.response.is_a?(ActiveMerchant::Shipping::Response)
147
+ #params = re.response.params
148
+ #if params.has_key?("Response") && params["Response"].has_key?("Error") && params["Response"]["Error"].has_key?("ErrorDescription")
149
+ #message = params["Response"]["Error"]["ErrorDescription"]
150
+ #else
151
+ #message = re.message
152
+ #end
153
+ #else
154
+ #message = re.message
155
+ #end
156
+
157
+ #error = Spree::ShippingError.new("#{I18n.t(:shipping_error)}: #{message}")
158
+ #Rails.cache.write @cache_key+"-timings", error #write error to cache to prevent constant re-lookups
159
+ #raise error
160
+ #end
161
+ #end
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,16 @@
1
+ module Spree
2
+ module Calculator::Shipping
3
+ module Correios
4
+ class ESedex < Spree::Calculator::Shipping::Correios::Base
5
+
6
+ def self.description
7
+ "e-Sedex - 81019"
8
+ end
9
+
10
+ def self.service_code
11
+ :e_sedex
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end