valken-shipping 0.2.0 → 1.1.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: 3a7479a62094f25d2a3bed078783fa64e87d1df2589126a5428139389d16a584
4
- data.tar.gz: 62dbd43d9ecc4df5474ef1dd9a3e544885c72704f10c5d4611ed526ac1a5ebf7
3
+ metadata.gz: 836864919e7e6f3dcb1762f14ea620afd0c56429040ad133fa7b01bf032cc6a3
4
+ data.tar.gz: 1f0addf9f86025e5f59232cbce1cdefd9dd7b56709969823264c3e50578251b8
5
5
  SHA512:
6
- metadata.gz: 2d6e57218303abfe50301f1c1d4d000f890af7f15293ed93c6ec735e16f57682c2a2b80a4d46eff1218d69f7632bc1782d7d7585faa566da097592be761c411f
7
- data.tar.gz: bb00c7c168791983e2ab1d557de2d296b37a32bf5edf6ca0cfa5d8ca9ff5b78bbaf80a0fabdb51646af1a6d612d4209c215694863628735408003c689f4153f2
6
+ metadata.gz: 465f4ab7305bc7c8efa1e8de744ff6a058f05f0afe389d91e8a0f79a126d12c9a2ba5109869fc39bdda63a4027c78742f6711c37a8e7aa57b2c283735ce4b9c6
7
+ data.tar.gz: ba70cb673db53d2c44ec47ffec89785538e9fbe1e78acca8599bcecf7d0233ca1849f979c811b17348631918a953e6f6f3ab34595eb35cda55aa43506c479c37
@@ -20,6 +20,10 @@ module Workarea
20
20
  carrier_data
21
21
  end
22
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
23
27
  def selected_carrier
24
28
  product_ids = order.items.to_a.map(&:product_id)
25
29
  product_details = Catalog::Product.in(id: product_ids).pluck(:details)
@@ -30,31 +34,48 @@ module Workarea
30
34
  return "none" unless is_hazmat && is_gun
31
35
  end
32
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.
33
39
  def collect_carrier_data(carrier)
34
40
  return get_ups_data if carrier == "UPS"
35
41
  return get_fedex_data if carrier == "FedEx"
36
42
  return get_none_data if carrier == "none"
37
43
  end
38
44
 
45
+ # UPS data.
39
46
  def get_ups_data
40
- ups_options = get_options(Workarea.config.gateways.ups_carrier, Workarea.config.ups_options)
47
+ @ups_options ||= get_options(Workarea.config.gateways.ups_carrier, Workarea.config.shipping_rates)
41
48
  end
42
49
 
50
+ # FedEx data.
43
51
  def get_fedex_data
44
- fedex_response = get_options(Workarea.config.gateways.fedex_carrier, Workarea.config.fedex_options)
52
+ @fedex_response ||= get_options(Workarea.config.gateways.fedex_carrier, Workarea.config.shipping_rates)
45
53
  end
46
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.
47
56
  def get_none_data
48
- ups = get_ups_data.first
49
- fedex = get_fedex_data.first
50
- return [] unless ups || fedex
51
- if ups.price > fedex.price
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?
52
69
  return get_fedex_data
53
- else
70
+ elsif fedex.blank?
54
71
  return get_ups_data
72
+ else
73
+ return []
55
74
  end
75
+
56
76
  end
57
77
 
78
+ # Free shipping option.
58
79
  def get_free_shipping
59
80
  ShippingOption.new(
60
81
  carrier: "Free Shipping",
@@ -65,19 +86,20 @@ module Workarea
65
86
  )
66
87
  end
67
88
 
89
+ # Calculating the ltl shipping rates based on the conditions.
68
90
  def get_ltl_shipping
69
91
  weight_price = 0
70
- case total_weight
92
+ case weight_convertion
71
93
  when 0..2
72
94
  weight_price = 5
73
95
  when 2..15
74
96
  weight_price = 15
75
97
  when 15..200
76
- weight_price = (1 * total_weight)
98
+ weight_price = (1 * weight_convertion)
77
99
  when 200..2500
78
100
  weight_price = 200
79
101
  else
80
- weight_price = ((total_weight / 2500).ceil()) * 200
102
+ weight_price = ((weight_convertion / 2500).ceil()) * 200
81
103
  end
82
104
 
83
105
  ShippingOption.new(
@@ -89,49 +111,73 @@ module Workarea
89
111
  )
90
112
  end
91
113
 
92
- def total_weight
114
+ # Returns the total weight.
115
+ def weight_convertion
93
116
  packaging = Packaging.new(order, shipping)
94
117
  return 0 if packaging.packages.blank?
95
- total_weight = 0
118
+ full_weight = 0
96
119
  packaging.packages.each do |item_package|
97
- total_weight += item_package.weight.value
120
+ new_weight = item_package.weight.convert_to(:lb)
121
+ full_weight += new_weight.value
98
122
  end
99
-
100
- total_weight
123
+ full_weight
101
124
  end
102
125
 
126
+ # 1. Sending a call to carrier and get shipping rates
127
+ # 2. Based on service codes in config, filter the shipping rates
128
+ # 3. If error, return empty array
103
129
  def get_options(carrier, service_code)
104
130
  packaging = Packaging.new(order, shipping)
105
131
  return [] if shipping.address.blank? || packaging.packages.blank?
106
132
  shipping_option = carrier || Workarea.config.gateways.shipping
107
-
108
- origin = ActiveShipping::Location.new(Workarea.config.shipping_origin)
109
- response = shipping_option.find_rates(
110
- origin,
111
- shipping.address.to_active_shipping,
112
- packaging.packages
113
- )
114
133
 
115
- if carrier.class == ActiveShipping::Workarea
116
- response.rates.sort_by(&:price).map do |rate|
117
- ShippingOption.from_rate_estimate(rate)
118
- end
119
- else
120
- filtered_options = []
121
- response.rates.sort_by(&:price).each do |rate|
122
- if service_code[rate.service_code].present?
123
- filtered_options.push(create_shipping_options(rate, service_code))
134
+ origin = ActiveShipping::Location.new(Workarea.config.shipping_origin)
135
+ begin
136
+ response = shipping_option.find_rates(
137
+ origin,
138
+ shipping.address.to_active_shipping,
139
+ packaging.packages
140
+ )
141
+ if carrier.class == ActiveShipping::Workarea
142
+ response.rates.sort_by(&:price).map do |rate|
143
+ ShippingOption.from_rate_estimate(rate)
124
144
  end
145
+ else
146
+ filter_shipping_rates(response.rates, service_code)
125
147
  end
126
- filtered_options
148
+ rescue ActiveShipping::ResponseError => e
149
+ return []
150
+ end
151
+ end
152
+
153
+ # Sort rates by delevry date and pick 1st three rates
154
+ # Find cheapest rate for those three rates
155
+ # Push all the cheap option for 1, 2, & 3 days into an array and return
156
+ def filter_shipping_rates(rates, service_code)
157
+ filtered_rates = []
158
+
159
+ # find and sort all delivery dates and pick 1st three
160
+ all_delivery_dates = rates.map { |rate| rate.delivery_date }
161
+ uniq_delivery_dates = all_delivery_dates.compact.sort.uniq[0..2]
162
+
163
+ # For each delivery dates, find cheapest rates and create shipping option and push
164
+ uniq_delivery_dates.each_with_index do |date, index|
165
+ selected_rates = rates.select {|rate| rate.delivery_date.present? && rate.delivery_date == date}
166
+ cheap_rate = selected_rates.sort_by(&:price).first
167
+ description = "#{cheap_rate.delivery_date.strftime('%a, %d %b %Y')}"
168
+ shipping_option = create_shipping_options(cheap_rate, service_code[index], description)
169
+ filtered_rates.push(shipping_option)
127
170
  end
171
+ filtered_rates
128
172
  end
129
173
 
130
- def create_shipping_options(rate, service_code)
174
+ # creating a ShippingOption
175
+ def create_shipping_options(rate, rate_label = nil, description = nil)
131
176
  ShippingOption.new(
132
177
  carrier: rate.carrier,
133
- name: service_code[rate.service_code] || rate.service_name,
178
+ name: rate_label || rate.service_name,
134
179
  service_code: rate.service_code,
180
+ description: description,
135
181
  price: Money.new(rate.price, rate.currency),
136
182
  tax_code: Shipping::Service.find_tax_code(
137
183
  rate.carrier,
@@ -139,5 +185,7 @@ module Workarea
139
185
  )
140
186
  )
141
187
  end
188
+
189
+
142
190
  end
143
191
  end
@@ -0,0 +1,10 @@
1
+ module Workarea
2
+ decorate ShippingOption, with: :valken do
3
+ decorated do
4
+ attr_reader :description
5
+ end
6
+
7
+ class_methods do
8
+ end
9
+ end
10
+ end
@@ -8,23 +8,20 @@ module Workarea
8
8
  end
9
9
 
10
10
  def find_sku_weight(sku)
11
- sku_data = Catalog::Product.find_by_sku(sku).variants
12
- sku = sku_data.detect {|variant| variant.sku == sku }
13
- if is_pounds(sku)
14
- convert_to_lb(sku.weight || 0)
15
- else
16
- sku.weight || 0
17
- end
11
+ product_by_sku = Catalog::Product.find_by_sku(sku).variants
12
+ sku_data = product_by_sku.detect {|variant| variant.sku == sku }
13
+ sku_weight = is_pounds(sku_data) ? convert_to_lb(sku_data.weight || 0) : (sku_data.weight || 0)
14
+ sku_weight
18
15
  end
19
16
 
20
17
  def convert_to_lb(weight)
21
18
  unit = Measured::Weight.new(weight, :lb)
22
- unit.convert_to(:oz)
23
- unit.value
19
+ ounce_unit = unit.convert_to(:oz)
20
+ ounce_unit.value
24
21
  end
25
22
 
26
- def is_pounds(sku)
27
- sku.weight_unit == "lb"
23
+ def is_pounds(sku_data)
24
+ sku_data.weight_unit == "lb"
28
25
  end
29
26
  end
30
27
  end
@@ -3,7 +3,13 @@ Workarea.configure do |config|
3
3
 
4
4
  config.weight_table = {"2": 5, "15": 15, "200": 1, "2500": 200}
5
5
 
6
- config.ups_options = {"01"=> "3 Day", "03"=> "Overnight", "02"=> "2 Day"}
6
+
7
+ config.shipping_rates = [
8
+ "Overnight",
9
+ "2 Day",
10
+ "3 Day"
11
+ ]
12
+ config.ups_options = {"12"=> "3 Day", "01"=> "Overnight", "02"=> "2 Day"}
7
13
  config.fedex_options = {"STANDARD_OVERNIGHT"=> "Overnight", "FEDEX_2_DAY"=> "2 Day", "GROUND_HOME_DELIVERY"=> "3 Day"}
8
14
  end
9
15
  Valken::Shipping.auto_initialize_gateway
@@ -1,5 +1,5 @@
1
1
  module Valken
2
2
  module Shipping
3
- VERSION = '0.2.0'
3
+ VERSION = '1.1.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: 0.2.0
4
+ version: 1.1.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-07-01 00:00:00.000000000 Z
11
+ date: 2020-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -43,6 +43,7 @@ files:
43
43
  - app/assets/config/valken_shipping_manifest.js
44
44
  - app/models/workarea/checkout/shipping_options.decorator
45
45
  - app/models/workarea/shipping.decorator
46
+ - app/models/workarea/shipping_option.decorator
46
47
  - app/services/workarea/packaging.decorator
47
48
  - config/initializers/workarea.rb
48
49
  - config/routes.rb