valken-shipping 0.1.4 → 1.1.2

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: e6737dc3d986324920e209163dbbd483d3c71326f1e648fb1aabce037532951e
4
- data.tar.gz: 61ee46bf6044bc76d3151c4ed82881bc52c239a5852482970057838ef2efef91
3
+ metadata.gz: 14b667ecf912e540effbb2b13c41a18d32022d9468f06645ea7ef521c03d7188
4
+ data.tar.gz: 9b97b46ad99cc5abf8c8eb6f7037f6fe9313e5a12039dc8485beeb4ee20290ce
5
5
  SHA512:
6
- metadata.gz: '05008742de8bd382a1f74d139941db9e2e865f7f7aee9f3b106e80f51c83f672a61d788cbe95d6239be6e49d9ee87534f7e138ebaf30138e79abd25909a14882'
7
- data.tar.gz: 77a5fd5041ba0f282bf5a45897eb410fd5b56cbcc6b813495fb0e965c2413d6972687774d98bbbfe32ed0bab62ec10de3ac2d3b37ba83097bd4ffcdda80e8f03
6
+ metadata.gz: f53ed14462bf4cddf96725b82b902171d1ef96aa23bfe51dba53d2ae14a9adfb05e5c537b658075d6933923998fb240dde077b78fbdb82c2e9e5a62f5346edf1
7
+ data.tar.gz: bfcfad206f8656f9cf38ba0c581b0e74968d669ff0f7d7fd21aad7a169f006fb886c8ad24278213a59645dbc08e0c03b4690520cb85e6d462067e95d87d8b149
@@ -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,48 +111,74 @@ 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 []
127
150
  end
128
151
  end
129
152
 
130
- def create_shipping_options(rate, service_code)
153
+ # Get cheapest option from overnight, 2 day & 3 days shipping rates
154
+ # Push all the cheap option for 1, 2, & 3 days into an array and return
155
+ def filter_shipping_rates(rates, service_code)
156
+ filtered_rates = []
157
+
158
+ overnight_option = get_cheapest_option(rates, (Date.today() + 1), service_code[:overnight])
159
+ two_day_option = get_cheapest_option(rates, (Date.today() + 2), service_code[:two_day])
160
+ three_day_option = get_cheapest_option(rates, (Date.today() + 3), service_code[:three_day])
161
+
162
+ filtered_rates.push(overnight_option) if overnight_option.present?
163
+ filtered_rates.push(two_day_option) if two_day_option.present?
164
+ filtered_rates.push(three_day_option) if three_day_option.present?
165
+
166
+ filtered_rates
167
+ end
168
+
169
+ # Select all rates based on deliver date and given date
170
+ # Find cheapest rate from selected rates
171
+ # Create shipping option for cheatest rate
172
+ def get_cheapest_option(rates, date, label)
173
+ selected_rates = rates.select { |item| item.delivery_date == date }
174
+ cheap_option = create_shipping_options(selected_rates.sort_by(&:price).first, label) if selected_rates.length > 0
175
+ end
176
+
177
+ # creating a ShippingOption
178
+ def create_shipping_options(rate, rate_label = nil)
131
179
  ShippingOption.new(
132
180
  carrier: rate.carrier,
133
- name: service_code[rate.service_code] || rate.service_name,
181
+ name: rate_label || rate.service_name,
134
182
  service_code: rate.service_code,
135
183
  price: Money.new(rate.price, rate.currency),
136
184
  tax_code: Shipping::Service.find_tax_code(
@@ -139,5 +187,7 @@ module Workarea
139
187
  )
140
188
  )
141
189
  end
190
+
191
+
142
192
  end
143
193
  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)
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,12 @@ 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
+ config.shipping_rates = {
7
+ :overnight => "Overnight",
8
+ :two_day => "2 Day",
9
+ :three_day => "3 Day"
10
+ }
11
+ config.ups_options = {"12"=> "3 Day", "01"=> "Overnight", "02"=> "2 Day"}
7
12
  config.fedex_options = {"STANDARD_OVERNIGHT"=> "Overnight", "FEDEX_2_DAY"=> "2 Day", "GROUND_HOME_DELIVERY"=> "3 Day"}
8
13
  end
9
14
  Valken::Shipping.auto_initialize_gateway
@@ -1,5 +1,5 @@
1
1
  module Valken
2
2
  module Shipping
3
- VERSION = '0.1.4'
3
+ VERSION = '1.1.2'
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.1.4
4
+ version: 1.1.2
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-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails