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