valken-shipping 0.1.3 → 1.1.1

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