valken-shipping 0.2.0 → 1.0.0

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: 0b0fc21d3ef74613ab62ca3f5993836bc52c8ad229cf50039216de9b9bd01073
4
+ data.tar.gz: 65f121acd97813fa08a0b5d9f405894281cde213e6860c1c7593a8f6e5c10d63
5
5
  SHA512:
6
- metadata.gz: 2d6e57218303abfe50301f1c1d4d000f890af7f15293ed93c6ec735e16f57682c2a2b80a4d46eff1218d69f7632bc1782d7d7585faa566da097592be761c411f
7
- data.tar.gz: bb00c7c168791983e2ab1d557de2d296b37a32bf5edf6ca0cfa5d8ca9ff5b78bbaf80a0fabdb51646af1a6d612d4209c215694863628735408003c689f4153f2
6
+ metadata.gz: ec65e99367d4da03661b12186d24bd29dfe1c44f2c80c5a7ac19388ee67ab60434ada4ba2c8ba120c1518a1c0bf8f0149d8eefb2eb6569e2312c5269cb157775
7
+ data.tar.gz: 7b045821a8865ca84fb2ee297e7717cbbc1309a37ad2fe990a0f6cdcfbca541cd57da8086338ad029937d5c46b35ae0a08af49b9170fea55363c3bba77373436
@@ -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,40 @@ 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.blank? || ups.price > fedex.price
52
61
  return get_fedex_data
53
- else
62
+ elsif fedex.blank? || ups.price < fedex.price
54
63
  return get_ups_data
64
+ else
65
+ return []
55
66
  end
67
+
56
68
  end
57
69
 
70
+ # Free shipping option.
58
71
  def get_free_shipping
59
72
  ShippingOption.new(
60
73
  carrier: "Free Shipping",
@@ -65,19 +78,20 @@ module Workarea
65
78
  )
66
79
  end
67
80
 
81
+ # Calculating the ltl shipping rates based on the conditions.
68
82
  def get_ltl_shipping
69
83
  weight_price = 0
70
- case total_weight
84
+ case weight_convertion
71
85
  when 0..2
72
86
  weight_price = 5
73
87
  when 2..15
74
88
  weight_price = 15
75
89
  when 15..200
76
- weight_price = (1 * total_weight)
90
+ weight_price = (1 * weight_convertion)
77
91
  when 200..2500
78
92
  weight_price = 200
79
93
  else
80
- weight_price = ((total_weight / 2500).ceil()) * 200
94
+ weight_price = ((weight_convertion / 2500).ceil()) * 200
81
95
  end
82
96
 
83
97
  ShippingOption.new(
@@ -89,48 +103,74 @@ module Workarea
89
103
  )
90
104
  end
91
105
 
92
- def total_weight
106
+ # Returns the total weight.
107
+ def weight_convertion
93
108
  packaging = Packaging.new(order, shipping)
94
109
  return 0 if packaging.packages.blank?
95
- total_weight = 0
110
+ full_weight = 0
96
111
  packaging.packages.each do |item_package|
97
- total_weight += item_package.weight.value
112
+ new_weight = item_package.weight.convert_to(:lb)
113
+ full_weight += new_weight.value
98
114
  end
99
-
100
- total_weight
115
+ full_weight
101
116
  end
102
117
 
118
+ # 1. Sending a call to carrier and get shipping rates
119
+ # 2. Based on service codes in config, filter the shipping rates
120
+ # 3. If error, return empty array
103
121
  def get_options(carrier, service_code)
104
122
  packaging = Packaging.new(order, shipping)
105
123
  return [] if shipping.address.blank? || packaging.packages.blank?
106
124
  shipping_option = carrier || Workarea.config.gateways.shipping
107
125
 
108
126
  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
-
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))
127
+ begin
128
+ response = shipping_option.find_rates(
129
+ origin,
130
+ shipping.address.to_active_shipping,
131
+ packaging.packages
132
+ )
133
+ if carrier.class == ActiveShipping::Workarea
134
+ response.rates.sort_by(&:price).map do |rate|
135
+ ShippingOption.from_rate_estimate(rate)
124
136
  end
137
+ else
138
+ filter_shipping_rates(response.rates, service_code)
125
139
  end
126
- filtered_options
140
+ rescue ActiveShipping::ResponseError => e
141
+ return []
127
142
  end
128
143
  end
129
144
 
130
- def create_shipping_options(rate, service_code)
145
+ # Get cheapest option from overnight, 2 day & 3 days shipping rates
146
+ # Push all the cheap option for 1, 2, & 3 days into an array and return
147
+ def filter_shipping_rates(rates, service_code)
148
+ filtered_rates = []
149
+
150
+ overnight_option = get_cheapest_option(rates, (Date.today() + 1), service_code[:overnight])
151
+ two_day_option = get_cheapest_option(rates, (Date.today() + 2), service_code[:two_day])
152
+ three_day_option = get_cheapest_option(rates, (Date.today() + 3), service_code[:three_day])
153
+
154
+ filtered_rates.push(overnight_option) if overnight_option.present?
155
+ filtered_rates.push(two_day_option) if two_day_option.present?
156
+ filtered_rates.push(three_day_option) if three_day_option.present?
157
+
158
+ filtered_rates
159
+ end
160
+
161
+ # Select all rates based on deliver date and given date
162
+ # Find cheapest rate from selected rates
163
+ # Create shipping option for cheatest rate
164
+ def get_cheapest_option(rates, date, label)
165
+ selected_rates = rates.select { |item| item.delivery_date == date }
166
+ cheap_option = create_shipping_options(selected_rates.sort_by(&:price).first, label) if selected_rates.length > 0
167
+ end
168
+
169
+ # creating a ShippingOption
170
+ def create_shipping_options(rate, rate_label = nil)
131
171
  ShippingOption.new(
132
172
  carrier: rate.carrier,
133
- name: service_code[rate.service_code] || rate.service_name,
173
+ name: rate_label || rate.service_name,
134
174
  service_code: rate.service_code,
135
175
  price: Money.new(rate.price, rate.currency),
136
176
  tax_code: Shipping::Service.find_tax_code(
@@ -139,5 +179,7 @@ module Workarea
139
179
  )
140
180
  )
141
181
  end
182
+
183
+
142
184
  end
143
185
  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,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.2.0'
3
+ VERSION = '1.0.0'
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.0.0
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-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails