valken-shipping 1.1.7 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21a15ea0992134b70026f5e873cce4752bb2d208b45c33f89f6fd105bfb09a01
4
- data.tar.gz: a975398b63c62662d7dc1d5de4238298e12377b9d10867dd56cadc0e5fc7149f
3
+ metadata.gz: f9818be8a62cad5c315d34ab0f92ad3329e92cc783239a7e79eb2d0a1ef57ed6
4
+ data.tar.gz: b4ce33dbb4c6521fd0bba0aa7be81d14141bd44d6f9e98ab3b05580b418c8027
5
5
  SHA512:
6
- metadata.gz: 600c74fd671573e899bd8a8a527a04ba19057fd0f9049828336ca9261898501a1d876ff87257ea4b54cdfde99e949524f4a8e21fdbc17047e1457b5a896df84c
7
- data.tar.gz: 875c6a24365e57ebd4374ac33cf4ce9f6af884414afd083bed38c9a737adc0a83989cd96a658ca828df0dab6a0b70417e795b4829bf5e631bc5a178fee7222b8
6
+ metadata.gz: aefdbcffb8b51ff670231ad598a339fb4f8184f3670252d0d982136c5b13c834a913dd8db3b581b72bf89b9e393c1f77411e4e805b149042dd4ed72048453652
7
+ data.tar.gz: 676daa8bcf724b0c134e66bfc95ace4fa8be4ad3745c422badfd794c8ff64bae749e48ed87664a2896a54f00f52b33f3c8fb659f6aa2e804530a198befc3689c
@@ -0,0 +1,59 @@
1
+ module Workarea
2
+ module AddressesHelper
3
+ def country_options
4
+ return [[Country['US'].name, Country['US'].alpha2]] if is_usa_only?
5
+ Workarea.config.countries.map do |country|
6
+ [country.name, country.alpha2]
7
+ end
8
+ end
9
+
10
+ def region_options
11
+ @region_options ||= Workarea.config.countries.reduce([]) do |memo, country|
12
+ regions = country.subdivisions
13
+ .map { |id, region| [region.translations[I18n.locale.to_s] || region.name, id] }
14
+ .sort_by { |name, id| name || id }
15
+
16
+ memo << [country.name, regions]
17
+ memo
18
+ end
19
+ end
20
+
21
+ def is_usa_only?
22
+
23
+ orders = current_order rescue nil
24
+ return false if orders.blank? || orders.items.blank?
25
+
26
+ usa_only_attribute = Workarea.config.shipping_attributes[:usa_only]
27
+ product_attributes = orders.items.map(&:product_attributes)
28
+ return product_attributes.map{ |item| item[:details][:en][usa_only_attribute] }.flatten.include?("true")
29
+ end
30
+
31
+ def formatted_address(address)
32
+ pieces = {
33
+ recipient: "#{address.first_name} #{address.last_name}\n#{address.company}".strip,
34
+ street: "#{address.street} #{address.street_2}".strip,
35
+ city: address.city,
36
+ region: address.region_name,
37
+ region_short: address.region,
38
+ postalcode: address.postal_code,
39
+ country: address.country.alpha2
40
+ }
41
+
42
+ address_format = address.country.address_format || Country['US'].address_format
43
+ result = pieces.reduce(address_format) do |memo, (name, value)|
44
+ memo.gsub(/{{#{name}}}/, html_escape(value.to_s))
45
+ end
46
+
47
+ if address.phone_number.present?
48
+ formatted_phone = number_to_phone(
49
+ address.phone_number,
50
+ extension: address.phone_extension
51
+ )
52
+
53
+ result << "\n#{formatted_phone}"
54
+ end
55
+
56
+ result.gsub(/\n/, tag(:br)).html_safe
57
+ end
58
+ end
59
+ end
@@ -1,192 +1,16 @@
1
1
  module Workarea
2
2
  decorate Checkout::ShippingOptions, with: :valken do
3
3
  decorated do
4
+ class NoAvailableShippingOption < RuntimeError; end
4
5
  end
5
6
 
6
7
  def all_options
7
8
  @all_options ||=
8
9
  begin
9
10
  packaging = Packaging.new(order, shipping)
10
- shipping_option
11
+ shipping.find_method_options(order, shipping)
11
12
  end
12
13
  end
13
14
 
14
- def shipping_option
15
- carrier_data = collect_carrier_data(selected_carrier)
16
- if Workarea.config.gateways.ups_carrier.class != ActiveShipping::Workarea
17
- carrier_data.push(get_ltl_shipping)
18
- carrier_data.push(get_free_shipping) if Workarea.config.show_free_shipping
19
- end
20
- carrier_data
21
- end
22
-
23
- # Selecting the shipping carrier based on order items
24
- #
25
- # If the product is type hazmat then it returns ups...or if product is type gun then it returns fedex
26
- # else if product is not both type then it returns none
27
- def selected_carrier
28
- product_ids = order.items.to_a.map(&:product_id)
29
- product_details = Catalog::Product.in(id: product_ids).pluck(:details)
30
- is_ups = product_details.any? {|item| item["en"]["Type"] && item["en"]["Type"].first == "UPS"}
31
- is_fedex = product_details.any? {|item| item["en"]["Type"] && item["en"]["Type"].first == "FEDEX"}
32
- return "UPS" if is_ups
33
- return "FedEx" if is_fedex
34
- return "none" unless is_ups && is_fedex
35
- end
36
-
37
- # If the carrier is ups then it will print ups data...or if carrier is fedex then prinnt fedex data...
38
- # else if it is not both then choose cheapest price.
39
- def collect_carrier_data(carrier)
40
- return get_ups_data if carrier == "UPS"
41
- return get_fedex_data if carrier == "FedEx"
42
- return get_none_data if carrier == "none"
43
- end
44
-
45
- # UPS data.
46
- def get_ups_data
47
- @ups_options ||= get_options(Workarea.config.gateways.ups_carrier, Workarea.config.shipping_rates)
48
- end
49
-
50
- # FedEx data.
51
- def get_fedex_data
52
- @fedex_response ||= get_options(Workarea.config.gateways.fedex_carrier, Workarea.config.shipping_rates)
53
- end
54
-
55
- # If the product is not ups or fedex then it will choose the cheapest price among ups and fedex annd print the value.
56
- def get_none_data
57
- ups = get_ups_data.sort_by(&:price).first
58
- fedex = get_fedex_data.sort_by(&:price).first
59
-
60
- if ups.present? && fedex.present?
61
- if ups.price > fedex.price
62
- return get_fedex_data
63
- elsif ups.price < fedex.price
64
- return get_ups_data
65
- else
66
- return get_ups_data
67
- end
68
- elsif ups.blank?
69
- return get_fedex_data
70
- elsif fedex.blank?
71
- return get_ups_data
72
- else
73
- return []
74
- end
75
-
76
- end
77
-
78
- # Free shipping option.
79
- def get_free_shipping
80
- ShippingOption.new(
81
- carrier: "Free Shipping",
82
- name: "Valken Economy",
83
- sub_name: "5 - 9 Business Days",
84
- service_code: "Free",
85
- price: Money.new(0.0, "USD"),
86
- tax_code: "TAX01"
87
- )
88
- end
89
-
90
- # Calculating the ltl shipping rates based on the conditions.
91
- def get_ltl_shipping
92
- weight_price = 0
93
- case weight_convertion
94
- when 0..2
95
- weight_price = 5
96
- when 2..15
97
- weight_price = 15
98
- when 15..200
99
- weight_price = (1 * weight_convertion)
100
- when 200..2500
101
- weight_price = 200
102
- else
103
- weight_price = ((weight_convertion / 2500).ceil()) * 200
104
- end
105
-
106
- ShippingOption.new(
107
- carrier: "LTL Shipping",
108
- name: "Valken Standard",
109
- sub_name: "3 - 5 Business Days",
110
- service_code: "LTL01",
111
- price: Money.new(weight_price * 100, "USD"),
112
- tax_code: "TAX01"
113
- )
114
- end
115
-
116
- # Returns the total weight.
117
- def weight_convertion
118
- packaging = Packaging.new(order, shipping)
119
- return 0 if packaging.packages.blank?
120
- full_weight = 0
121
- packaging.packages.each do |item_package|
122
- new_weight = item_package.weight.convert_to(:lb)
123
- full_weight += new_weight.value
124
- end
125
- full_weight
126
- end
127
-
128
- # 1. Sending a call to carrier and get shipping rates
129
- # 2. Based on service codes in config, filter the shipping rates
130
- # 3. If error, return empty array
131
- def get_options(carrier, service_code)
132
- packaging = Packaging.new(order, shipping)
133
- return [] if shipping.address.blank? || packaging.packages.blank?
134
- shipping_option = carrier || Workarea.config.gateways.shipping
135
-
136
- origin = ActiveShipping::Location.new(Workarea.config.shipping_origin)
137
- begin
138
- response = shipping_option.find_rates(
139
- origin,
140
- shipping.address.to_active_shipping,
141
- packaging.packages
142
- )
143
- if carrier.class == ActiveShipping::Workarea
144
- response.rates.sort_by(&:price).map do |rate|
145
- ShippingOption.from_rate_estimate(rate)
146
- end
147
- else
148
- filter_shipping_rates(response.rates, service_code)
149
- end
150
- rescue ActiveShipping::ResponseError => e
151
- return []
152
- end
153
- end
154
-
155
- # Sort rates by delevry date and pick 1st three rates
156
- # Find cheapest rate for those three rates
157
- # Push all the cheap option for 1, 2, & 3 days into an array and return
158
- def filter_shipping_rates(rates, service_code)
159
- filtered_rates = []
160
-
161
- # find and sort all delivery dates and pick 1st three
162
- all_delivery_dates = rates.map { |rate| rate.delivery_date }
163
- uniq_delivery_dates = all_delivery_dates.compact.sort.uniq[0..2]
164
-
165
- # For each delivery dates, find cheapest rates and create shipping option and push
166
- uniq_delivery_dates.each_with_index do |date, index|
167
- selected_rates = rates.select {|rate| rate.delivery_date.present? && rate.delivery_date == date}
168
- cheap_rate = selected_rates.sort_by(&:price).first
169
- description = "#{cheap_rate.delivery_date.strftime('%A, %B %d, %Y')}"
170
- shipping_option = create_shipping_options(cheap_rate, service_code[index], description, Workarea.config.shipping_rates[index])
171
- filtered_rates.push(shipping_option)
172
- end
173
- filtered_rates
174
- end
175
-
176
- # creating a ShippingOption
177
- def create_shipping_options(rate, rate_label = nil, description = nil, sub_name)
178
- ShippingOption.new(
179
- carrier: rate.carrier,
180
- name: rate_label.keys.join || rate.service_name,
181
- sub_name: sub_name.values.join,
182
- service_code: rate.service_code,
183
- description: description,
184
- price: Money.new(rate.price, rate.currency),
185
- tax_code: Shipping::Service.find_tax_code(
186
- rate.carrier,
187
- rate.service_name
188
- )
189
- )
190
- end
191
15
  end
192
16
  end
@@ -1,26 +1,262 @@
1
1
  module Workarea
2
2
  decorate Shipping, with: :valken do
3
- decorated do
3
+ decorated do
4
+ field :tracking_number, type: String
4
5
  end
5
6
 
6
7
  class_methods do
7
8
  end
8
9
 
9
- def find_method_options(packages, shipping_option)
10
+ def order
11
+ @order
12
+ end
13
+
14
+ def shipping
15
+ @shipping
16
+ end
17
+
18
+
19
+ def default_options(packages)
10
20
  return [] if address.blank? || packages.blank?
11
- shipping_option = shipping_option || Workarea.config.gateways.shipping
12
-
21
+ shipping_option = Workarea.config.gateways.shipping
22
+
13
23
  origin = ActiveShipping::Location.new(Workarea.config.shipping_origin)
14
24
  response = shipping_option.find_rates(
15
25
  origin,
16
26
  address.to_active_shipping,
17
27
  packages
18
28
  )
19
-
20
29
  response.rates.sort_by(&:price).map do |rate|
21
30
  ShippingOption.from_rate_estimate(rate)
22
31
  end
32
+ end
33
+
34
+ def find_method_options(order, shipping)
35
+ @order ||= order
36
+ @shipping ||= shipping
37
+ carrier_data = []
38
+
39
+ packaging = Packaging.new(order, shipping)
40
+ return [] if packaging.packages.blank?
41
+
42
+ return default_options(packaging.packages) if Workarea.config.gateways.ups_carrier.class == ActiveShipping::Workarea
43
+
44
+ shipping_options_api = get_final_shipping_options(collect_carrier_data)
45
+ return [] if @error.present?
46
+
47
+ carrier_data.push(shipping_options_api) if shipping_options_api.present? && shipping_options_api.any?
48
+
49
+ if Workarea.config.gateways.ups_carrier.class != ActiveShipping::Workarea
50
+ carrier_data.push(get_ltl_shipping)
51
+ carrier_data.push(get_free_shipping) if Workarea.config.show_free_shipping
52
+ else
53
+ return []
54
+ end
55
+
56
+ carrier_data.flatten.reverse
57
+ end
58
+
59
+ def get_final_shipping_options(rates)
60
+ options = []
61
+
62
+ if is_ground
63
+ ground_rate = rates.detect{|rate| rate.service_code == "GROUND_HOME_DELIVERY" || rate.service_code == "03" }
64
+ return [] if ground_rate.blank?
65
+
66
+ description = "#{ground_rate.delivery_date.strftime('%A, %B %d, %Y')}"
67
+ days_to_deliver = (ground_rate.delivery_date - Date.today).to_i
68
+
69
+ label = Workarea.config.shipping_rates[(days_to_deliver - 1)]
70
+ return options.push(create_shipping_options(ground_rate, label, description))
71
+ end
72
+
73
+ rates.each_with_index do |rate, index|
74
+ description = "#{rate.delivery_date.strftime('%A, %B %d, %Y')}"
75
+ label = Workarea.config.shipping_rates[index]
76
+ options.push(create_shipping_options(rate, label, description))
77
+ end
78
+ options
79
+ end
80
+
81
+ # If the carrier is ups then it will print ups data...or if carrier is fedex then prinnt fedex data...
82
+ # else if it is not both then choose cheapest price.
83
+ def collect_carrier_data
84
+ return get_ups_data if is_ups
85
+ return get_fedex_data if is_fedex
86
+ return get_none_data unless is_ups && is_fedex
87
+ end
88
+
89
+ # UPS data.
90
+ def get_ups_data
91
+ @ups_options ||= get_options(Workarea.config.gateways.ups_carrier, Workarea.config.shipping_rates)
92
+ end
93
+
94
+ # FedEx data.
95
+ def get_fedex_data
96
+ @fedex_response ||= get_options(Workarea.config.gateways.fedex_carrier, Workarea.config.shipping_rates)
97
+ end
98
+
99
+ # If the product is not ups or fedex then it will choose the cheapest price among ups and fedex annd print the value.
100
+ def get_none_data
101
+ return [] if get_ups_data.blank? || get_fedex_data.blank?
102
+ final_options = []
103
+ temp_options = get_fedex_data + get_ups_data
104
+ return [] unless temp_options.any?
105
+
106
+ delivery_dates = temp_options.map(&:delivery_date).uniq.sort
107
+ delivery_dates.each_with_index do |date, index|
108
+ selected_rates = temp_options.select {|rate| rate.delivery_date == date}
109
+ final_options.push(selected_rates.sort_by(&:price).first)
110
+ end
111
+ return final_options
112
+
113
+ end
114
+
115
+ # Free shipping option.
116
+ def get_free_shipping
117
+ ShippingOption.new(
118
+ carrier: "Free Shipping",
119
+ name: "Valken Economy",
120
+ sub_name: "5 - 9 Business Days",
121
+ service_code: "Free",
122
+ price: Money.new(0.0, "USD"),
123
+ tax_code: "TAX01"
124
+ )
125
+ end
126
+
127
+ # Calculating the ltl shipping rates based on the conditions.
128
+ def get_ltl_shipping
129
+ weight_price = 0
130
+ case weight_convertion
131
+ when 0..2
132
+ weight_price = 5
133
+ when 2..15
134
+ weight_price = 15
135
+ when 15..200
136
+ weight_price = (1 * weight_convertion)
137
+ when 200..2500
138
+ weight_price = 200
139
+ else
140
+ weight_price = ((weight_convertion / 2500).ceil()) * 200
141
+ end
142
+
143
+ ShippingOption.new(
144
+ carrier: "LTL Shipping",
145
+ name: "Valken Standard",
146
+ sub_name: "3 - 5 Business Days",
147
+ service_code: "LTL01",
148
+ price: Money.new(weight_price * 100, "USD"),
149
+ tax_code: "TAX01"
150
+ )
151
+ end
152
+
153
+ # Returns the total weight.
154
+ def weight_convertion
155
+ packaging = Packaging.new(order, shipping)
156
+ return 0 if packaging.packages.blank?
157
+ full_weight = 0
158
+ packaging.packages.each do |item_package|
159
+ new_weight = item_package.weight.convert_to(:lb)
160
+ full_weight += new_weight.value
161
+ end
162
+ full_weight
163
+ end
164
+
165
+ # 1. Sending a call to carrier and get shipping rates
166
+ # 2. Based on service codes in config, filter the shipping rates
167
+ # 3. If error, return empty array
168
+ def get_options(carrier, service_code)
169
+ packaging = Packaging.new(order, shipping)
170
+ return [] if @shipping.address.blank? || packaging.packages.blank?
171
+ shipping_option = carrier || Workarea.config.gateways.shipping
172
+
173
+ origin = ActiveShipping::Location.new(Workarea.config.shipping_origin)
174
+ begin
175
+ response = shipping_option.find_rates(
176
+ origin,
177
+ @shipping.address.to_active_shipping,
178
+ packaging.packages
179
+ )
180
+ if carrier.class == ActiveShipping::Workarea
181
+ response.rates.sort_by(&:price).map do |rate|
182
+ ShippingOption.from_rate_estimate(rate)
183
+ end
184
+ else
185
+ filter_shipping_rates(response.rates, service_code)
186
+ end
187
+ rescue ActiveShipping::ResponseError => e
188
+ @error = e
189
+ return []
190
+ end
191
+ end
192
+
193
+ # Sort rates by delevry date and pick 1st three rates
194
+ # Find cheapest rate for those three rates
195
+ # Push all the cheap option for 1, 2, & 3 days into an array and return
196
+ def filter_shipping_rates(rates, service_code)
197
+ filtered_rates = []
198
+ # find and sort all delivery dates and pick 1st three
199
+ all_delivery_dates = rates.map { |rate| rate.delivery_date }
200
+ # sorting the uniq delivery dates and reversing those delivery dates.
201
+ uniq_delivery_dates = all_delivery_dates.compact.sort.uniq[0..2]
202
+
203
+ # removing overnight. Assuming the 3rd option is always overnight
204
+ uniq_delivery_dates.pop if uniq_delivery_dates.length == 3
205
+
206
+ # For each delivery dates, find cheapest rates and create shipping option and push
207
+ uniq_delivery_dates.each_with_index do |date, index|
208
+ selected_rates = rates.select {|rate| rate.delivery_date.present? && rate.delivery_date == date}
209
+ cheap_rate = selected_rates.sort_by(&:price).first
210
+ description = "#{cheap_rate.delivery_date.strftime('%A, %B %d, %Y')}"
211
+
212
+ filtered_rates.push(cheap_rate)
213
+ end
214
+ filtered_rates
215
+ end
216
+
217
+ # creating a ShippingOption
218
+ def create_shipping_options(rate, rate_label = nil, description = nil)
219
+ ShippingOption.new(
220
+ carrier: rate.carrier,
221
+ name: rate_label.keys.join || rate.service_name,
222
+ sub_name: rate_label.values.join,
223
+ service_code: rate.service_code,
224
+ description: description,
225
+ delivery_date: rate.delivery_date,
226
+ price: Money.new(rate.price, rate.currency),
227
+ tax_code: Shipping::Service.find_tax_code(
228
+ rate.carrier,
229
+ rate.service_name
230
+ )
231
+ )
232
+ end
233
+
234
+ # Selecting the shipping carrier based on order items
235
+ #
236
+ # If the product is type hazmat then it returns ups...or if product is type gun then it returns fedex
237
+ # else if product is not both type then it returns none
238
+ def is_fedex
239
+ carrier_type_attribute = Workarea.config.shipping_attributes[:carrier_type]
240
+ product_carrier_type = product_details.map{ |item| item[:en][carrier_type_attribute] }.flatten
241
+ product_carrier_type.include?("FEDEX")
242
+ end
243
+
244
+ def is_ups
245
+ carrier_type_attribute = Workarea.config.shipping_attributes[:carrier_type]
246
+ product_carrier_type = product_details.map{ |item| item[:en][carrier_type_attribute] }.flatten
247
+ product_carrier_type.include?("UPS")
248
+ end
249
+
250
+ def is_ground
251
+ ground_ship_attribute = Workarea.config.shipping_attributes[:ground_shipping_only]
252
+ product_carrier_type = product_details.map{ |item| item[:en][ground_ship_attribute] }.flatten
253
+ product_carrier_type.include?("true")
254
+ end
255
+
256
+ def product_details
257
+ # product_attributes = @order.items.map(&:product_attributes)
23
258
 
259
+ @product_details ||= Catalog::Product.in(id: @order.items.to_a.map(&:product_id)).pluck(:details)
24
260
  end
25
261
  end
26
262
  end
@@ -1,7 +1,7 @@
1
1
  module Workarea
2
2
  decorate ShippingOption, with: :valken do
3
3
  decorated do
4
- attr_reader :description, :sub_name
4
+ attr_reader :description, :sub_name, :delivery_date
5
5
  end
6
6
 
7
7
  class_methods do
@@ -0,0 +1,12 @@
1
+ Workarea::Configuration.define_fields do
2
+ fieldset 'Shipping', namespaced: false do
3
+ field 'Shipping Attributes',
4
+ type: :hash,
5
+ default: {
6
+ 'usa_only' => 'usa only',
7
+ 'carrier_type' => 'Type',
8
+ 'ground_shipping_only' => 'ground shipping only'
9
+ },
10
+ description: 'Mapping of product details keys with shipping logic. Note: This maps only the keys not the values.'
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  module Valken
2
2
  module Shipping
3
- VERSION = '1.1.7'
3
+ VERSION = '2.0.3'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valken-shipping
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.7
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - sushmitha02
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-11 00:00:00.000000000 Z
11
+ date: 2020-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -41,10 +41,12 @@ files:
41
41
  - README.md
42
42
  - Rakefile
43
43
  - app/assets/config/valken_shipping_manifest.js
44
+ - app/helpers/workarea/addresses_helper.rb
44
45
  - app/models/workarea/checkout/shipping_options.decorator
45
46
  - app/models/workarea/shipping.decorator
46
47
  - app/models/workarea/shipping_option.decorator
47
48
  - app/services/workarea/packaging.decorator
49
+ - config/initializers/shipping_configuration.rb
48
50
  - config/initializers/workarea.rb
49
51
  - config/routes.rb
50
52
  - lib/tasks/valken/shipping_tasks.rake
@@ -70,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
72
  - !ruby/object:Gem::Version
71
73
  version: '0'
72
74
  requirements: []
73
- rubygems_version: 3.0.6
75
+ rubygems_version: 3.0.8
74
76
  signing_key:
75
77
  specification_version: 4
76
78
  summary: shipping options