Class: SpeedeeService

Inherits:
ApplicationRecord show all
Defined in:
app/models/speedee_service.rb

Overview

== Schema Information

Table name: speedee_services
Database name: primary

id :integer not null, primary key
days_in_transit_from_60047 :integer not null
is_active :boolean default(TRUE), not null
sort_code :integer not null
zip :integer not null

Indexes

zip_is_active (zip,is_active)

Constant Summary collapse

FUEL_SURCHARGE_API_URL =

Speedee has a basic web call (not quite API/web service) that provides a weekly fuel surcharge rate, with some history.

"http://speedeedelivery.com/files/fs.ini"
FALLBACK_FUEL_SURCHARGE_RATE =
0.07

Class Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.by_zipActiveRecord::Relation<SpeedeeService>

A relation of SpeedeeServices that are by zip. Active Record Scope

Returns:

See Also:



24
# File 'app/models/speedee_service.rb', line 24

scope :by_zip, ->(zip) { where(zip: zip).is_active }

.delivery_type_code(options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/speedee_service.rb', line 79

def self.delivery_type_code(options)

# Specifications for Delivery Type Codes
# The following is a list of defined Delivery Type Codes. The placement of the two digit code should be directly after the shipper number in the 9th and 10th position. For shipper number 6-981, delivery type 03, package ID 12345678. The barcode should read.
# SP 006981 03 12345678
# 72 - COD (No special code for type of cash) (Oversized/Hazardous Available)
#   Print on Service Detail of Label – Line 1: COD $500.00
# 73 – COD, CASHIERS CK/MO ONLY (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00
#                               Line 2: CASHIERS CK/MO ONLY
# 74 – COD/AOD, Adult Signature Required (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00
#                           Line 2: AOD ADULT SIGNATURE REQUIRED
# 75 - COD/AOD, CASHIERS CK/MO, Adult Signature Required (Oversized/Hazardous Available)
#   Print on Service Detail of Label – Line 1: COD $500.00     CASHIERS CK/MO ONLY
#                               Line 2: AOD ADULT SIGNATURE REQUIRED
# 76 – COD/AOD, Indirect Signature Required (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00
#                               Line 2: INDIRECT AOD-SIGNATURE REQ'D
# 77 – COD/AOD, CASHIERS CK/MO ONLY, Indirect Signature Required (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00     CASHIERS CK/MO ONLY
#                                                       Line 2: INDIRECT AOD-SIGNATURE REQ'D
# 78 – COD/AOD, Direct Signature Required (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00
#                               Line 2: DIRECT AOD-SIG REQ'D AT ADDR
# 79 – COD/AOD, CASHIERS CK/MO ONLY, Direct Signature Required (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00     CASHIERS CK/MO ONLY
#                                                       Line 2: DIRECT AOD-SIG REQ'D AT ADDR
# C4 – COD/AOD, Adult Signature Required - With Signature Card (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00
#                           Line 2: AOD ADULT SIGNATURE REQ’D W/TAG
# C5 - COD/AOD, CASHIERS CK/MO, Adult Signature Required - With Signature Card
#   (Oversized/Hazardous Available)
#   Print on Service Detail of Label – Line 1: COD $500.00     CASHIERS CK/MO ONLY
#                               Line 2: AOD ADULT SIGNATURE REQ’D W/TAG
# C6 – COD/AOD, Indirect Signature Required - With Signature Card
#   (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00
#                               Line 2: INDIRECT AOD SIGNATURE REQ’D W/TAG
# C7 – COD/AOD, CASHIERS CK/MO ONLY, Indirect Signature Required - With Signature Card
#   (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00     CASHIERS CK/MO ONLY
#                                                       Line 2: INDIRECT AOD SIGNATURE REQ’D W/TAG
# C8 – COD/AOD, Direct Signature Required - With Signature Card
#   (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00
#                               Line 2: DIRECT AOD-SIG REQ'D AT ADDR W/TAG
# C9 – COD/AOD, CASHIERS CK/MO ONLY, Direct Signature Required - With Signature Card
#   (Oversized/Hazardous Available)
# Print on Service Detail of Label – Line 1: COD $500.00     CASHIERS CK/MO ONLY
#                                                       Line 2: DIRECT AOD-SIG REQ'D AT ADDR WTAG
# 41 – Acknowledgement of Delivery - Adult Signature Required (Oversized/Hazardous Available)
#   ** THIS DELIVERY TYPE IS DEPRICATED – USE TYPE 47 **
#   Print on Service Detail of Label – Line 1: AOD
#                                       Line 2: ADULT SIGNATURE REQUIRED
# 42 - Acknowledgement of Delivery - Signature Required (Oversized/Hazardous Available)
#   ** THIS DELIVERY TYPE IS DEPRICATED – USE TYPE 43 or 45 **
#   Print on Service Detail of Label – Line 1: AOD
#                           Line 2: SIGNATURE REQUIRED
# 43 - Acknowledgement of Delivery – Indirect Delivery
#   Print on Service Detail of Label – Line 1: AOD
#                           Line 2: SIGNATURE REQ’D
# 44 - Acknowledgement of Delivery – Indirect Delivery – With Signature Card
#   Print on Service Detail of Label – Line 1: AOD
#                           Line 2: SIGNATURE REQ’D W/TAG
# 45 - Acknowledgement of Delivery – Direct Delivery
#   Print on Service Detail of Label – Line 1: AOD
#                           Line 2: SIGNATURE REQ’D AT ADDR
# 46 - Acknowledgement of Delivery – Direct Delivery – With Signature Card
#   Print on Service Detail of Label – Line 1: AOD
#                           Line 2: SIGNATURE REQ’D AT ADDR W/TAG
# 47 - Acknowledgement of Delivery – Adult Signature Required
#   Print on Service Detail of Label – Line 1: AOD
#                           Line 2: ADULT SIGNATURE REQ’D
# 48 - Acknowledgement of Delivery – Adult Signature Required – With Signature Card
#   Print on Service Detail of Label – Line 1: AOD
#                           Line 2: ADULT SIGNATURE REQ’D W/TAG
# 26 - 3 Attempts Pickup Tag (Oversized/Hazardous Available, Declared Value/COD/AOD not Available)
# 52 – Only Hazardous Materials: (Oversized Available)
# 51 – Only Oversized Packages: (No Other Options)
# 12 – LTL order (No Other Options)
# 06 - EZR: Easy Return Label available by special arrangement through Sales Dept. (No Other Options)
# 03 - Standard Package (No Other Options)
# The following identifiers need to be printed in the Service Summary area on the label if used: COD, AOD, HAZ, OVR. (Explained in more detail on Delivery Label Documentation)

  code = "03" # default
  # we do NOT support COD for Speedee or any other carrier
  # if options[:cod_amount].to_f > 0.0
  #   # COD
  #   code = 72 # COD (No special code for type of cash)
  #   if options[:cod_collection_type] == 'no_check'
  #     # CASHIERS CK/MO ONLY
  #     code = 73 # COD, CASHIERS CK/MO ONLY
  #     if options[:signature_confirmation]
  #       # AOD
  #       code = 79 # COD/AOD, CASHIERS CK/MO ONLY, Direct Signature Required
  #     end
  #   else
  #     if options[:signature_confirmation]
  #       # AOD
  #       code = 78 # COD/AOD, Direct Signature Required
  #     end
  #   end
  # else
  if options[:signature_confirmation]
    code = 45 # Acknowledgement of Delivery – Direct Delivery
  else
    if options[:oversize]
      code = 51 # Only Oversized Packages: (No Other Options)
    end
  end
  # end
  code
end

.generate_speedee_barcode(tracking_number) ⇒ Object



194
195
196
197
198
199
200
201
202
203
# File 'app/models/speedee_service.rb', line 194

def self.generate_speedee_barcode(tracking_number)
  raise "Tracking number is invalid, must be 32 digits or fewer long" unless tracking_number&.size <= 32
  #file_path ||= Rails.root.join(Rails.application.config.x.temp_storage_path.to_s, "gs1-128-#{id}-#{Time.current.to_i}#{rand(1000)}.png")
  require 'barby'
  require 'barby/barcode/code_128'
  require 'barby/outputter/png_outputter'
  barcode = Barby::Code128.new(tracking_number)
  png = barcode.to_png(xdim: 2)
  png
end

.get_fuel_surcharge_rateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/speedee_service.rb', line 26

def self.get_fuel_surcharge_rate
  res = {}
  cache_key = "speedee_delivery_fuel_surcharge_rate_data:#{Date.current.year}_week_#{Date.current.cweek}"
  fuel_surcharge_rate_data = Rails.cache.fetch(cache_key, expires_in: 1.week) do
    rate = nil
    rate_res = SpeedeeService.get_latest_fuel_surcharge_rate
    rate = rate_res[:rate] if rate_res[:status] == :ok
    rate ||= FALLBACK_FUEL_SURCHARGE_RATE
    res['rate'] = rate
    res['status'] = rate_res[:status].to_s
    res['error_message'] = "#{rate_res[:message]}" if rate_res[:status] != :ok
    res
  end
  if fuel_surcharge_rate_data['status'] != "ok"
    # error, do not cache
    fuel_surcharge_rate_data['error_message'] ||= "Could not get DOE weekly national fuel rate"
    Rails.cache.delete(cache_key)
  end
  return fuel_surcharge_rate_data
end

.get_latest_fuel_surcharge_rateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/speedee_service.rb', line 47

def self.get_latest_fuel_surcharge_rate
  response_json = ""
  response = ""
  begin
    Timeout::timeout(20) do
      Rails.logger.info "get_latest_fuel_surcharge_rate: attempting to get FUEL_SURCHARGE_API_URL: #{SpeedeeService::FUEL_SURCHARGE_API_URL}"
      response = HTTP.get("#{SpeedeeService::FUEL_SURCHARGE_API_URL}").body
      Rails.logger.info "get_latest_fuel_surcharge_rate: response: #{response}"
      rate = nil
      status = :error
      message = "No rate returned"
      if (rate = (response.to_s.split("=").last).to_f) > 0.0
        status = :ok
        message = ""
      end
      result = {:rate => rate, :status => status, :message => message}
      return result
    end
  rescue StandardError => exc
    error_message = "Timeout trying to reach Speedee Service #{SpeedeeService::FUEL_SURCHARGE_API_URL}, response  (if any): #{response}, Error: #{exc}. "
    Rails.logger.error " * #{error_message}"
    ErrorReporting.warning(error_message)
    result = {:rate => nil, :status => :timeout, :message => "Error: #{exc}. Speedee Service servers are not responding."}
    return result
  end
end

.get_tracking_number(container_code, options) ⇒ Object



74
75
76
77
# File 'app/models/speedee_service.rb', line 74

def self.get_tracking_number(container_code, options)
  code = container_code.presence || "UNKNOWN"
  "SP#{('000000'+Heatwave::Configuration.fetch(:speedee_delivery_account_number).to_s).last(6)}#{SpeedeeService.delivery_type_code(options).to_s.last(2)}#{code.last(10)}"
end

.is_activeActiveRecord::Relation<SpeedeeService>

A relation of SpeedeeServices that are is active. Active Record Scope

Returns:

See Also:



23
# File 'app/models/speedee_service.rb', line 23

scope :is_active, -> { where(is_active: true) }