Class: Shipping::Freightquote

Inherits:
Base
  • Object
show all
Defined in:
app/services/shipping/freightquote.rb

Overview

Freightquote (CH Robinson Navisphere) LTL rating, label, void, and tracking
service. Wraps the Navisphere REST API documented at
https://developer.chrobinson.com/api-reference and the legacy SOAP service at
https://b2b.freightquote.com/WebService/QuoteService.asmx.

The per-method ABC/length metrics here exceed the project's general
thresholds on purpose: the request/response builders mirror the API's flat
JSON shape and the get_*_hash names match sibling carrier classes, so they
are part of the public surface used by subclasses, presenters, and tests.
rubocop:disable Metrics/ClassLength, Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/BlockLength, Naming/AccessorMethodName, Style/OptionalBooleanParameter

Constant Summary collapse

BLACKLIST_SERVICE_CODES =

Blacklist R+L Carriers because our direct integration with them is much
cheaper and we don't want to inadvertently book a more costly version of
it through Freightquote.

[
  'Pilot Freight Services- Economy',
  'Valley Cartage',
  'Midland Transport',
  'R+L Carriers',
  'YRC Freight',
  'Panther Deferred (LTL)',
  'US Road Freight Express',
  'US Special Delivery',
  'TST Overland Express Canada',
  'Sutton Transport',
  'Estes Express Lines'
].freeze
WHITELIST_SERVICE_CODES =
['UPS Freight'].freeze
COST_DISCREPANCY_THRESHOLD =
200.0
COST_DISCREPANCY_THRESHOLD_RATIO =

max ratio of discrepancy by total shipping cost

0.5
COST_DISCREPANCY_THRESHOLD_BY_TOTAL_VALUE_RATIO =

max ratio of discrepancy by total delivery value

0.05

Instance Attribute Summary

Attributes inherited from Base

#address, #address2, #address3, #address_residential, #attention_name, #billing_account, #billing_country, #billing_zip, #ci_comments, #city, #close_report_only, #cod_amount, #cod_collection_type, #company, #country, #currency_code, #data, #debug, #declared_value, #delivery_instructions, #delivery_total_value, #description, #discount_price, #dropoff_type, #email, #eta, #export_reason, #freight_class, #freightquote_authorization_url, #freightquote_client_id, #freightquote_client_secret, #freightquote_customer_code, #freightquote_events_url, #freightquote_rating_url, #freightquote_shipping_url, #freightquote_voiding_url, #handling_instructions, #has_loading_dock, #image_type, #include_first_class_mail_options, #insured_value, #is_construction_site, #is_trade_show, #label_type, #limited_access, #line_items, #master_tracking_number, #measure_height, #measure_length, #measure_units, #measure_width, #media_mail, #multiple_piece_shipping, #negotiated_rates, #package, #package_count, #package_sequence_number, #package_total, #packages, #packaging_type, #pay_type, #phone, #pickup_datetime, #pickup_instructions, #plain_response, #price, #rate_data, #reference_number_1, #reference_number_2, #reference_number_3, #reference_number_code_1, #reference_number_code_2, #required, #requires_appointment, #requires_inside_delivery, #requires_liftgate, #response, #return_to_address, #return_to_address2, #return_to_address3, #return_to_address_residential, #return_to_attention_name, #return_to_city, #return_to_company, #return_to_country, #return_to_email, #return_to_has_loading_dock, #return_to_is_construction_site, #return_to_is_trade_show, #return_to_limited_access, #return_to_name, #return_to_phone, #return_to_requires_appointment, #return_to_requires_inside_delivery, #return_to_requires_liftgate, #return_to_state, #return_to_zip, #rl_carriers_api_key, #rl_carriers_shipping_url, #saturday_delivery, #sender_address, #sender_address2, #sender_address3, #sender_address_residential, #sender_attention_name, #sender_city, #sender_company, #sender_country, #sender_email, #sender_has_loading_dock, #sender_is_construction_site, #sender_is_trade_show, #sender_limited_access, #sender_name, #sender_phone, #sender_requires_appointment, #sender_requires_inside_delivery, #sender_requires_liftgate, #sender_state, #sender_tax_identification_number, #sender_zip, #service_code, #service_type, #services, #ship_date, #shipengine_api_key, #shipengine_canadapost_account_id, #shipengine_canadapost_parent_account_number, #shipengine_canpar_account_id, #shipengine_dhl_express_account_id, #shipengine_fed_ex_account_id, #shipengine_fed_ex_ca_account_id, #shipengine_purolator_account_id, #shipengine_ups_account_id, #shipengine_ups_ca_account_id, #shipengine_usps_account_id, #shipper_address, #shipper_address2, #shipper_address3, #shipper_address_residential, #shipper_attention_name, #shipper_city, #shipper_company, #shipper_country, #shipper_email, #shipper_has_loading_dock, #shipper_is_construction_site, #shipper_is_trade_show, #shipper_limited_access, #shipper_name, #shipper_phone, #shipper_requires_appointment, #shipper_requires_inside_delivery, #shipper_requires_liftgate, #shipper_state, #shipper_zip, #signature_confirmation, #skip_png_download, #skip_rate_test, #special_instructions, #state, #tax_identification_number, #time_in_transit, #total_shipment_weight, #transaction_type, #weight, #weight_units, #zip

Instance Method Summary collapse

Methods inherited from Base

#fedex, #initialize, #purolator, state_from_zip, #ups, #ups_freight

Constructor Details

This class inherits a constructor from Shipping::Base

Instance Method Details

#check_events_for_load_number(freight_order_number) ⇒ Object

Raises:



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'app/services/shipping/freightquote.rb', line 600

def check_events_for_load_number(freight_order_number)
  logger ||= Rails.logger
  @required += %i[freightquote_authorization_url freightquote_events_url freightquote_client_id freightquote_client_secret]
  raise ShippingError, 'Freight Order Number required to get Load Number' if freight_order_number.blank?

  access_token = fetch_access_token # must do it this way because fetch_access_token populates @data
  events_url = "#{freightquote_events_url}?to=#{DateTime.current.utc.iso8601}&orderNumber=#{freight_order_number}"

  get_simple_get_response(events_url, { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' })
  logger.info 'Shipping Freightquote events Request:'
  logger.info events_url.to_s
  logger.info 'Shipping Freightquote events Response:'
  logger.info @response.to_s

  resp_hash = @response.with_indifferent_access if @response.is_a?(Hash)
  resp_hash = @response.first.with_indifferent_access if @response.is_a?(Array)

  # We must get the load number from the LOAD CREATED event
  load_number = resp_hash[:results].detect { |r| r.dig(:event, :eventType) == 'LOAD CREATED' }&.dig(:event, :loadNumber)

  # Annoyingly LOAD CREATED has inconsistent or just wrong ebolEnabledWithCarrier and eBolEnabled flags, use LOAD BOOKED to see if PRO numbers will quickly get populated
  e_bol_enabled = resp_hash[:results].detect { |r| r.dig(:event, :eventType) == 'LOAD BOOKED' }&.dig(:event, :customers)&.any? { |ch| ch[:ebolEnabledWithCarrier].to_b } && resp_hash[:results].detect do |r|
    r.dig(:event, :eventType) == 'LOAD BOOKED'
  end&.dig(:event, :carrier, :eBolEnabled)&.to_b

  # We must get the pro number from the PRO NUMBER ADDED event
  pro_number = resp_hash[:results].detect { |r| r.dig(:event, :eventType) == 'PRO NUMBER ADDED' }&.dig(:event, :carrier, :proNumber)

  { load_number:, e_bol_enabled:, pro_number: }
end

#check_events_for_pro_number(freight_order_number) ⇒ Object

Raises:



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'app/services/shipping/freightquote.rb', line 631

def check_events_for_pro_number(freight_order_number)
  logger ||= Rails.logger
  @required += %i[freightquote_authorization_url freightquote_events_url freightquote_client_id freightquote_client_secret]
  raise ShippingError, 'Freight Order Number required to get Load Number' if freight_order_number.blank?

  access_token = fetch_access_token # must do it this way because fetch_access_token populates @data
  events_url = "#{freightquote_events_url}?to=#{DateTime.current.utc.iso8601}&orderNumber=#{freight_order_number}"

  get_simple_get_response(events_url, { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' })
  logger.info 'Shipping Freightquote events Request:'
  logger.info events_url.to_s
  logger.info 'Shipping Freightquote events Response:'
  logger.info @response.to_s

  resp_hash = @response.with_indifferent_access if @response.is_a?(Hash)
  resp_hash = @response.first.with_indifferent_access if @response.is_a?(Array)

  # Use LOAD BOOKED to see if eBolEnabled is true and thus if PRO numbers will quickly get populated
  e_bol_enabled = resp_hash[:results].detect { |r| r.dig(:event, :eventType) == 'LOAD BOOKED' }&.dig(:event, :customers)&.any? { |ch| ch[:ebolEnabledWithCarrier].to_b } && resp_hash[:results].detect do |r|
    r.dig(:event, :eventType) == 'LOAD BOOKED'
  end&.dig(:event, :carrier, :eBolEnabled)&.to_b

  pro_number = resp_hash[:results].detect { |r| r.dig(:event, :eventType) == 'PRO NUMBER ADDED' }&.dig(:event, :carrier, :proNumber) # PRO NUMBER ADDED will happen quickly if eBolEnabled

  { e_bol_enabled:, pro_number: }
end

#cull_rate_estimates(rate_estimates) ⇒ Object



697
698
699
# File 'app/services/shipping/freightquote.rb', line 697

def cull_rate_estimates(rate_estimates)
  (rate_estimates.sort_by { |e| e[:price] }.reject { |e| BLACKLIST_SERVICE_CODES.include?(e[:service_code]) }.slice(0..5) + rate_estimates.select { |e| WHITELIST_SERVICE_CODES.include?(e[:service_code]) }).uniq
end

#fetch_access_tokenObject



34
35
36
37
38
39
# File 'app/services/shipping/freightquote.rb', line 34

def fetch_access_token
  # CH Robinson access token expires in 24 hours and they ask you to limit access token calls to once per 24 hours
  Rails.cache.fetch(:freightquote_access_token, expires_in: 24.hours) do
    fetch_fresh_access_token
  end
end

#fetch_fresh_access_tokenObject



41
42
43
44
45
46
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/services/shipping/freightquote.rb', line 41

def fetch_fresh_access_token
  logger ||= Rails.logger

  # See https://developer.chrobinson.com/api-reference#tag/Authentication
  auth_payload_hash = {
    client_id: @freightquote_client_id,
    client_secret: @freightquote_client_secret,
    audience: 'https://inavisphere.chrobinson.com',
    grant_type: 'client_credentials'
  }
  @data = auth_payload_hash.to_json
  headers = {
    'Content-Type' => 'application/json', # NOTE: headers need this rocket form
    'Cache-Control' => 'no-cache'
  }

  get_response(@freightquote_authorization_url, headers)

  logger.debug 'Shipping Freightquote fetch_fresh_access_token Request:'
  logger.debug @freightquote_authorization_url.to_s
  logger.debug auth_payload_hash.merge(client_secret: '[FILTERED]').to_json
  logger.debug 'Shipping Freightquote fetch_fresh_access_token Response:'
  logger.debug(
    if @response.is_a?(Hash)
      @response.except('access_token').merge(access_token: '[FILTERED]').to_json
    else
      '[FILTERED]'
    end
  )

  @response['access_token']
end

#find_rates(logger = nil) ⇒ Object



74
75
76
77
78
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
# File 'app/services/shipping/freightquote.rb', line 74

def find_rates(logger = nil)
  logger ||= Rails.logger
  @required = %i[zip country sender_state sender_zip sender_country packages]
  @required += %i[freightquote_authorization_url freightquote_rating_url freightquote_customer_code freightquote_client_id freightquote_client_secret insured_value]

  @insured_value = 50_000.0 if @insured_value.to_f > 50_000.0 # UPS limits this to 50000.0 for domestic
  @insured_value = 1.00 if @insured_value < 1.0 # Navisphere requires a non zero value here

  @country ||= 'US'
  @sender_country ||= 'US'

  access_token = fetch_access_token # must do it this way because fetch_access_token populates @data
  @data = get_rate_hash.to_json

  get_response(@freightquote_rating_url, { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' })
  logger.debug('Shipping Freightquote find_rates Request', url: @freightquote_rating_url)
  logger.debug('Shipping Freightquote find_rates Response')

  resp_hash = @response.with_indifferent_access if @response.is_a?(Hash)
  resp_hash = @response.first.with_indifferent_access if @response.is_a?(Array)

  rate_estimates = get_rate_estimates_from_response_hash(resp_hash)

  successful = true
  msg = ''
  if rate_estimates.empty?
    successful = false
    err_types = []
    err_msgs = []
    errs = []
    # handle response either an array (400?, 401) or a hash (400?, 403, 404), sheesh
    resp_arr = [resp_hash]
    resp_arr = resp_hash if resp_hash.is_a?(Array)
    resp_arr.each do |err_hsh|
      err_types << (err_hsh['type'] || err_hsh['error'])
      err_msgs << err_hsh['message']
    end
    err_types.each_with_index do |err_type, i|
      errs << "#{err_type}: #{err_msgs[i]}"
    end
    msg = "No shipping rates could be found for the destination address: #{errs.join(', ')}" if msg.blank?
  end

  response = {}
  response[:success] = successful
  response[:message] = "Freightquote: #{msg}" if msg.present?
  response[:request] = @data.to_s
  response[:xml] = @response.to_s
  response[:rates] = cull_rate_estimates(rate_estimates)

  # allows for things like fedex.success?
  def response.method_missing(name, *args)
    key?(name) ? self[name] : super
  end

  def response.respond_to_missing?(name, include_private = false)
    key?(name) || super
  end

  response
end

#get_freight_class_from_package(package) ⇒ Object



685
686
687
688
689
690
691
692
693
694
695
# File 'app/services/shipping/freightquote.rb', line 685

def get_freight_class_from_package(package)
  # get total weight in lbs and cubic feet of package, and figure out density in lbs per cubic foot or PCF
  total_weight = package.lbs.to_f
  total_cubic_ft = package.inches(:length).to_f * package.inches(:width).to_f * package.inches(:height).to_f / 1728.0
  pcf = total_weight / total_cubic_ft
  UpsFreight::FREIGHT_CLASS_BY_PCF.each do |freight_class, pcf_limits|
    return freight_class if pcf >= pcf_limits[:lower] && pcf < pcf_limits[:upper]
  end
  # worst case return the highest freight class
  UpsFreight::FREIGHT_CLASS_BY_PCF.to_a.last.first.to_f
end

#get_label_hash(quote_id:) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'app/services/shipping/freightquote.rb', line 443

def get_label_hash(quote_id:)
  # See: https://developer.chrobinson.com/api-reference#operation/Order%20Create%20with%20Quote%20ID

  # the handling of cross-border shipping in not part of the API but these following things are based on suggestions by the Freightquote/CH Roinson API contacts Andrew Erwin and Micah Tedford
  notes = 'N/A'
  notes_arr = []
  notes_arr << @special_instructions if @special_instructions
  customer_contacts = []
  bill_to_contacts = []

  sender_contact_hash = {
    name: @sender_company,
    type: 'Contact',
    companyName: @sender_company,
    contactMethods: [
      {
        method: 'Phone',
        value: @sender_phone
      },
      {
        method: 'Email',
        value: @sender_email
      }
    ]
  }

  customer_contacts << sender_contact_hash
  bill_to_contacts << sender_contact_hash
  reference_hash_array = get_reference_number_hash_array
  shid = reference_hash_array.detect { |rh| rh[:type] == 'SHID' }&.dig(:value)
  customer_reference_number_hash_array = [
    {
      type: 'MBOL',
      value: shid
    }
  ]
  bill_to_reference_number_hash_array = reference_hash_array + [{
      type: 'CRID',
      value: freightquote_customer_code
    }]

  origin_instructions_arr = []
  origin_instructions_arr << "#{Delivery::CROSS_BORDER_BROKER_INSTRUCTIONS}, #{Delivery::CROSS_BORDER_COUNTRY_SPECIFIC_BROKER_TEXT[@country]}" if @sender_country != @country

  if @sender_requires_liftgate || @sender_requires_inside_delivery || @sender_limited_access
    reqs = []
    reqs << 'ORIGIN LIFTGATE' if @sender_requires_liftgate
    reqs << 'INSIDE PICKUP' if @sender_requires_inside_delivery
    reqs << 'LIMITED ACCESS PICKUP' if @sender_limited_access
    origin_instructions_arr << "#{reqs.join(', ')} REQUESTED"
  end
  origin_instructions = origin_instructions_arr.join('. ')

  notes_arr << origin_instructions if origin_instructions

  if @requires_liftgate || @requires_inside_delivery || @limited_access || @requires_appointment || (@sender_country != @country)
    reqs = []
    reqs << 'DESTINATION LIFTGATE' if @requires_liftgate
    reqs << 'INSIDE DELIVERY' if @requires_inside_delivery
    reqs << 'LIMITED ACCESS DELIVERY' if @limited_access
    reqs << 'DELIVERY APPOINTMENT' if @requires_appointment
    destination_instructions = "#{reqs.join(', ')} REQUESTED"
    notes_arr << destination_instructions
  end

  notes = notes_arr.join('. ') if notes_arr.any?

  origin_open_datetime = nil
  origin_close_datetime = nil
  tz = @sender_timezone || 'America/Chicago'
  Time.use_zone(tz) do
    origin_open_datetime = Time.zone.parse('10:00:00')
    origin_close_datetime = Time.zone.parse('16:30:00')
    pickup_datetime = 1.hour.from_now
    if pickup_datetime > Time.zone.parse('2:00pm')
      pickup_datetime = 1.working.day.since(Time.zone.parse('10:00:00'))
      origin_open_datetime = pickup_datetime
      origin_close_datetime = 1.working.day.since(Time.zone.parse('16:30:00'))
    end
  end

  origin_address = {
    address1: @sender_address,
    city: @sender_city,
    stateProvinceCode: @sender_state,
    country: @sender_country,
    postalCode: @sender_zip
  }
  origin_address['address2'] = @sender_address2 if @sender_address2.present?
  origin_hash = {
    name: @sender_company,
    address: origin_address,
    phone: @sender_phone,
    emailAddress: @sender_email,
    contactName: @sender_company,
    openDateTime: origin_open_datetime.iso8601,
    closeDateTime: origin_close_datetime.iso8601
  }
  origin_hash['specialInstructions'] = origin_instructions if origin_instructions.present?
  origin_hash['referenceNumbers'] = reference_hash_array

  destination_address = {
    address1: @address,
    city: @city,
    stateProvinceCode: @state,
    country: @country,
    postalCode: @zip
  }
  destination_address['address2'] = @address2 if @address2.present?
  destination_hash = {
    name: @attention_name || @company,
    address: destination_address,
    phone: @phone,
    emailAddress: @email,
    contactName: @attention_name
  }
  destination_hash['referenceNumbers'] = reference_hash_array

  [{
    quoteId: quote_id,
    customer: {
      'customerCode' => @freightquote_customer_code,
      contacts: customer_contacts,
      referenceNumbers: customer_reference_number_hash_array
    },
    billTo: {
      currencyCode: 'USD',
      contacts: bill_to_contacts,
      referenceNumbers: bill_to_reference_number_hash_array
    },
    service: {
      referenceNumbers: reference_hash_array
    },
    origin: origin_hash,
    destination: destination_hash,
    # "onlineParties": [],
    notes: notes
  }]
end

#get_nmfc_code_to_navishpere_format(nmfc_code) ⇒ Object



720
721
722
723
724
# File 'app/services/shipping/freightquote.rb', line 720

def get_nmfc_code_to_navishpere_format(nmfc_code)
  res = nmfc_code
  res = "#{nmfc_code}-01" if nmfc_code.index('-0').blank?
  res
end

#get_package_type(container_type, package_length, package_width, _package_height) ⇒ Object



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'app/services/shipping/freightquote.rb', line 658

def get_package_type(container_type, package_length, package_width, _package_height)
  # Allowed package types: Unknown, Pallets_48x40, Pallets_other, Bags, Bales, Boxes, Bundles, Carpets,
  # Coils, Crates, Cylinders, Drums, Pails, Reels, Rolls, TubesPipes, Motorcycle, ATV, Pallets_120x120,
  # Pallets_120x100, Pallets_120x80, Pallets_europe, Pallets_48x48, Pallets_60x48, Slipsheets, Unit.
  if container_type == 'pallet'
    package_type = 'Pallets_other'
    if package_length == 48 && package_width == 48
      package_type = 'Pallets_48x48'
    elsif package_length == 48 && package_width == 40
      package_type = 'Pallets_48x40'
    elsif package_length == 60 && package_width == 60
      package_type = 'Pallets_60x48'
    elsif package_length == 120 && package_width == 120
      package_type = 'Pallets_120x120'
    elsif package_length == 120 && package_width == 80
      package_type = 'Pallets_120x80'
    elsif package_length == 120 && package_width == 100
      package_type = 'Pallets_120x100'
    end
  elsif container_type == 'crate'
    package_type = 'Crates'
  else
    package_type = 'Boxes'
  end
  package_type
end

#get_rate_estimates_from_response_hash(resp_hash) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'app/services/shipping/freightquote.rb', line 247

def get_rate_estimates_from_response_hash(resp_hash)
  # quoteSummaries:
  # - quoteId: 399662687
  #   carrier:
  #     carrierCode: T5302800
  #     carrierName: Forward Air, Inc.
  #     scac: FWDA
  #   totalCharge: 138.45
  #   totalFreightCharge: 114.94
  #   totalAccessorialCharge: 23.51
  #   transit:
  #     minimumTransitDays: 1
  #     originService: Direct
  #     destinationService: Direct
  #   rates:
  #   - rateId: 3738410707
  #     totalRate: 114.94
  #     unitRate: 114.94
  #     quantity: 1
  #     rateCode: '400'
  #     rateCodeValue: Line Haul
  #     currencyCode: USD
  #     isOptional: false
  #   - rateId: 3738410707
  #     totalRate: 23.51
  #     unitRate: 23.51
  #     quantity: 1
  #     rateCode: '405'
  #     rateCodeValue: Fuel Surcharge
  #     currencyCode: USD
  #     isOptional: false
  #   transportModeType: LTL
  #   equipmentType: Van
  #   quoteSource: MarketRate
  # - quoteId: 399662689 ...

  rate_estimates = []
  (resp_hash[:quoteSummaries] || []).each do |quote_hash|
    quote_id = quote_hash[:quoteId]
    quote_expiration = (Date.current + 1.day).to_s # let's just assume it expires in 1 day
    carrier_hash = quote_hash[:carrier]
    carrier_option_id = carrier_hash&.dig(:carrierCode)
    service_code = carrier_hash&.dig(:carrierName)
    scac = carrier_hash&.dig(:scac)
    days_in_transit = (quote_hash.dig(:transit, :minimumTransitDays) || 3).to_i + 3 # per Christian and JJ
    price = quote_hash[:totalCharge]
    service_options_charges = quote_hash[:totalAccessorialCharge]
    transportation_charges = quote_hash[:totalFreightCharge] || (price - service_options_charges)
    currency = quote_hash[:rates].first&.dig(:currencyCode) # Not sure why they don't put currencyCode as a sibling to totalCharge but oh well

    next unless price && currency && service_code

    estimate = {}
    estimate[:service_code] = service_code
    estimate[:price] = price
    estimate[:total_charges] = price
    estimate[:transportation_charges] = transportation_charges
    estimate[:service_options_charges] = service_options_charges
    estimate[:insured_value] = @insured_value.to_f.round(2)
    estimate[:currency] = currency
    rate_data = {
      quote_id: quote_id,
      carrier_option_id: carrier_option_id,
      total_price: price,
      carrier_name: service_code,
      scac: scac,
      days_in_transit: days_in_transit,
      quote_expiration: quote_expiration
    }
    estimate[:rate_data] = rate_data
    # allows for things like estimate.service_code
    def estimate.method_missing(name, *args)
      key?(name) ? self[name] : super
    end

    def estimate.respond_to_missing?(name, include_private = false)
      key?(name) || super
    end
    rate_estimates << estimate
  end
  rate_estimates
end

#get_rate_hashObject



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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/services/shipping/freightquote.rb', line 136

def get_rate_hash
  # See https://developer.chrobinson.com/api-reference#tag/Rating
  if @pickup_datetime
    datetime = @pickup_datetime
  else
    tz = @sender_timezone || 'America/Chicago'
    datetime = Time.current
    Time.use_zone(tz) do
      datetime = 1.hour.from_now
      datetime = 1.working.day.since(Time.zone.parse('10:00:00')) if datetime > Time.zone.parse('3:00pm') || datetime.on_weekend?
    end
  end

  items = []
  @packages.each_with_index do |package, _i|
    freight_class = get_freight_class_from_package(package)
    value = package.lbs
    package_weight = [value, 1.0].max.round
    value = package.inches(:length)
    package_length = [value, 0.1].max.round
    value = package.inches(:width)
    package_width = [value, 0.1].max.round
    value = package.inches(:height)
    package_height = [value, 0.1].max.round
    container_type = (if package.pallet?
                        'PLT'
                      else
                        (package.crate? ? 'CRT' : 'CTN')
                      end)

    items << {
      description: 'Radiant Heating Elements and Controls',
      weight: package_weight,
      freightClass: freight_class,
      nationalMotorFreightClass: get_nmfc_code_to_navishpere_format(package.nmfc_code || ProductCategoryConstants::FALLBACK_NMFC_CODE),
      weightUnitOfMeasure: 'Pounds',
      pallets: 1,
      palletSpaces: 1,
      quantity: 1,
      insuranceValue: @insured_value,
      packagingType: container_type,
      packagingUnitOfMeasure: 'Inches',
      packagingLength: package_length,
      packagingWidth: package_width,
      packagingHeight: package_height
    }
  end

  origin_hash = {
    name: @sender_company,
    address1: @sender_address,
    city: @sender_city,
    stateProvinceCode: @sender_state,
    countryCode: @sender_country,
    postalCode: @sender_zip,
    specialRequirement: {
      liftGate: (@sender_requires_liftgate || false).to_s,
      insidePickup: (@sender_requires_inside_delivery || false).to_s,
      insideDelivery: (@sender_requires_inside_delivery || false).to_s,
      residentialNonCommercial: (@sender_address_residential || false).to_s,
      limitedAccess: (@sender_limited_access || false).to_s,
      tradeShoworConvention: (@sender_is_trade_show || false).to_s,
      constructionSite: (@sender_is_construction_site || false).to_s,
      dropOffAtCarrierTerminal: 'false',
      pickupAtCarrierTerminal: 'false'
    }
  }
  origin_hash['address2'] = @sender_address2 if @sender_address2.present?

  destination_hash = {
    name: @attention_name.presence || @company,
    address1: @address,
    city: @city,
    stateProvinceCode: @state,
    countryCode: @country,
    postalCode: @zip,
    specialRequirement: {
      liftGate: (@requires_liftgate || false).to_s,
      insidePickup: (@requires_inside_delivery || false).to_s,
      insideDelivery: (@requires_inside_delivery || false).to_s,
      residentialNonCommercial: (@address_residential || false).to_s,
      limitedAccess: (@limited_access || false).to_s,
      tradeShoworConvention: (@is_trade_show || false).to_s,
      constructionSite: (@is_construction_site || false).to_s,
      dropOffAtCarrierTerminal: 'false',
      pickupAtCarrierTerminal: 'false'
    }
  }
  destination_hash['address2'] = @address2 if @address2.present?

  {
    items: items,
    origin: origin_hash,
    destination: destination_hash,
    shipDate: DateTime.current.utc.iso8601,
    customerCode: @freightquote_customer_code,
    transportModes: [
      {
        mode: 'LTL',
        equipments: [
          {
            equipmentType: 'Van',
            quantity: 1
          }
        ]
      }
    ] # ,
    # "referenceNumbers": []
  }
end

#get_reference_number_hash_arrayObject



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'app/services/shipping/freightquote.rb', line 701

def get_reference_number_hash_array
  # See: https://developer.chrobinson.com/api-reference#operation/Rating%20Request

  delivery = Shipment.where(id: @packages&.first&.shipment_id)&.last&.delivery
  shid = delivery&.id || @reference_number_1 || @packages&.first&.shipment_id || @packages&.first&.container_code

  refhash = {}
  refhash['SHID'] = shid.to_s if shid # Need Unique Shipment ID here as a string
  refhash['CON'] = @reference_number_1 if @reference_number_1.presence # Customer Order Number
  refhash['XXXX'] = @reference_number_2 if @reference_number_2.presence # Miscelleaneous (for label instructions if any)
  refhash['CUSTPO'] = @reference_number_3 if @reference_number_3.presence # Customer PO
  refhash.map do |type, value|
    {
      type: type,
      value: value
    }
  end
end

#label(_return_label = false, logger = nil) ⇒ Object

Raises:



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'app/services/shipping/freightquote.rb', line 330

def label(_return_label = false, logger = nil)
  logger ||= Rails.logger
  @required = %i[zip country sender_state sender_zip sender_country packages service_code]
  @required += %i[freightquote_authorization_url freightquote_shipping_url freightquote_customer_code freightquote_client_id freightquote_client_secret rate_data delivery_total_value insured_value]

  check_required

  quote_id = @rate_data['quote_id'] # quote id fallback will be original quote_id
  carrier_option_id = @rate_data['carrier_option_id'] # carrier_option_id fallback will be original carrier_option_id
  scac = @rate_data['scac'] # scac fallback will be original scac
  quote_expiration = @rate_data['quote_expiration']

  # Here we need to re-rate quote, because you need to buy a specific Freightquote quote id and all the
  # relevant criteria like packages, weights, addresses and options have to match the quote id you are
  # 'buying'. This pattern is different than our other carriers which give you an estimate and then you
  # buy the label using the same (one hopes) criteria.

  original_scac = scac
  original_total_price = @rate_data['total_price'].to_f
  # So let's re-find rates, but with the new criteria for the label
  estimate_res = find_rates
  # find the matching rate based on SCAC (if we have it) or Carrier Name
  estimate = nil
  estimate = estimate_res[:rates].detect { |e| e.dig(:rate_data, :scac) == original_scac } if original_scac.present? # match on SCAC if we have it
  new_total_price = estimate&.dig(:price)&.to_f
  diff = (new_total_price && original_total_price) ? (new_total_price - original_total_price).abs : nil
  use_rerated_quote = false

  if estimate.present? # we have a matching rate using the SCAC code matching
    # When the new rate is proportionately close to the original (or @skip_rate_test bypasses the check),
    # grab the new quote id, carrier option id, and SCAC. Otherwise fall through and reuse the originals.
    if (diff && original_total_price.positive? &&
        (diff < COST_DISCREPANCY_THRESHOLD) &&
        (diff / original_total_price < COST_DISCREPANCY_THRESHOLD_RATIO)
       ) ||
       (diff && (@delivery_total_value.to_f > COST_DISCREPANCY_THRESHOLD * 10.0) &&
        (diff / @delivery_total_value.to_f < COST_DISCREPANCY_THRESHOLD_BY_TOTAL_VALUE_RATIO)
       ) ||
       @skip_rate_test
      use_rerated_quote = true
      quote_id = estimate.dig(:rate_data, :quote_id)
      carrier_option_id = estimate.dig(:rate_data, :carrier_option_id)
      scac = estimate.dig(:rate_data, :scac)
    end
  elsif quote_expiration && Date.current >= Date.parse(quote_expiration)
    # no match or pricing threshold exceeded, test for quote expiration and let it go
    raise ShippingError, 'Rate has expired, please HOLD order and refresh shipping rates/methods'
  end

  logger.debug "Shipping Freightquote @rate_data: #{@rate_data}"

  raise ShippingError, 'Missing data, please HOLD order and refresh shipping rates/methods' if quote_id.blank? || carrier_option_id.blank? || scac.blank?

  access_token = fetch_access_token # must do it this way because fetch_access_token populates @data
  # carrier_option_id and scac are validated above but not part of the label
  # payload — Freightquote derives the carrier from the original quote_id.
  @data = get_label_hash(quote_id: quote_id).to_json

  get_response(@freightquote_shipping_url, { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' })
  logger.info 'Shipping Freightquote label Request:'
  logger.info @freightquote_shipping_url.to_s
  logger.info @data.to_s
  logger.info 'Shipping Freightquote label Response:'
  logger.info @response.to_s

  resp_hash = @response.with_indifferent_access if @response.is_a?(Hash)
  resp_hash = @response.first.with_indifferent_access if @response.is_a?(Array)

  # Freightquote seems to use the quote ID as the BOL, and also returns a tracking number, so use this.
  quote_id = resp_hash['quoteId']
  order_number = resp_hash['orderNumber']
  tracking_number = resp_hash['trackingNumber']

  successful = true
  msg = ''
  if quote_id == '0' || quote_id.nil? || order_number == '0' || order_number.nil? || tracking_number == '0' || tracking_number.nil?
    successful = false
    err_types = []
    err_msgs = []
    errs = []
    # handle response either an array (400?, 401) or a hash (400?, 403, 404), sheesh
    resp_arr = [resp_hash]
    resp_arr = resp_hash if resp_hash.is_a?(Array)
    resp_arr.each do |err_hsh|
      err_types << (err_hsh[:type] || err_hsh[:error])
      err_msgs << err_hsh[:message]
    end
    err_types.each_with_index do |err_type, i|
      errs << "#{err_type}: #{err_msgs[i]}"
    end
    msg = "Shipping could not be confirmed: #{errs.join(', ')}"
  end

  response = {}
  raise ShippingError, msg unless successful

  total_price = use_rerated_quote ? new_total_price : original_total_price
  response[:tracking_number] = tracking_number
  # we will generate BOL from the delivery

  # allows for things like fedex.success?
  def response.method_missing(name, *args)
    key?(name) ? self[name] : super
  end

  def response.respond_to_missing?(name, include_private = false)
    key?(name) || super
  end

  { labels: [response], shipment_identification_number: tracking_number, carrier_bol: quote_id, freight_order_number: order_number, pickup_confirmation_number: quote_id, total_charges: total_price, ship_request_xml: @data.to_s,
    ship_reply_xml: @response.to_s }
end

#void(freight_order_number) ⇒ Object

Raises:



583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'app/services/shipping/freightquote.rb', line 583

def void(freight_order_number)
  logger ||= Rails.logger
  @required += %i[freightquote_authorization_url freightquote_voiding_url freightquote_client_id freightquote_client_secret]
  raise ShippingError, 'Freight Order Number required to void a Freightquote delivery' if freight_order_number.blank?

  access_token = fetch_access_token # must do it this way because fetch_access_token populates @data
  void_url = "#{@freightquote_voiding_url}/#{freight_order_number}"

  get_delete_response(void_url, { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' })
  logger.info 'Shipping Freightquote void Request:'
  logger.info void_url.to_s
  logger.info 'Shipping Freightquote void Response:'
  logger.info @response.to_s

  { void_request_xml: void_url, void_response_xml: @response }
end