Class: ShippingOption

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

Overview

== Schema Information

Table name: shipping_options
Database name: primary

id :integer not null, primary key
carrier :string(255)
country :string(255)
days_commitment :float default(3.5), not null
delivery_commitment :text
description :string(255)
information :text
is_freight :boolean default(FALSE), not null
is_inactive :boolean default(FALSE), not null
is_third_party_only :boolean default(FALSE), not null
jde_shipping_stop_code :string(255)
name :string(255)
service_code :string(255)
service_level :string(255)
supported_for_st :boolean default(FALSE), not null
tax_class :string(3) not null
created_at :datetime
updated_at :datetime
item_id :integer

Indexes

index_shipping_options_on_service_level (service_level)

Constant Summary collapse

SERVICE_LEVELS =
{ standard: 3, expedited: 2, express: 1 }
OPTIONS_FOR_RMAS =
[1, 139, 207, 4, 30, 63, 146]

Has many collapse

Belongs to collapse

Class Method Summary collapse

Instance 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

.:CANActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are :CAN. Active Record Scope

Returns:

See Also:



43
# File 'app/models/shipping_option.rb', line 43

scope :CAN, -> { for_country_iso('CA') }

.:NLDActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are :NLD. Active Record Scope

Returns:

See Also:



44
# File 'app/models/shipping_option.rb', line 44

scope :NLD, -> { for_country_iso('NL') }

.:USAActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are :USA. Active Record Scope

Returns:

See Also:



42
# File 'app/models/shipping_option.rb', line 42

scope :USA, -> { for_country_iso('US') }

.activeActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are active. Active Record Scope

Returns:

See Also:



38
# File 'app/models/shipping_option.rb', line 38

scope :active, -> { where('shipping_options.is_inactive IS NOT TRUE') }

.by_days_commitmentActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are by days commitment. Active Record Scope

Returns:

See Also:



41
# File 'app/models/shipping_option.rb', line 41

scope :by_days_commitment, -> { order('shipping_options.days_commitment DESC') }

.carrier_options_for_select(country_iso3 = nil) ⇒ Object



84
85
86
87
88
# File 'app/models/shipping_option.rb', line 84

def self.carrier_options_for_select(country_iso3 = nil)
  opts = ShippingOption.active.where.not(carrier: nil).order(:carrier)
  opts = opts.where(country: country_iso3[0..1]) if country_iso3.present?
  opts.pluck(:carrier).uniq
end

.for_country_isoActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are for country iso. Active Record Scope

Returns:

See Also:



45
# File 'app/models/shipping_option.rb', line 45

scope :for_country_iso, ->(country_iso) { where(country: country_iso).by_days_commitment }

.for_country_iso3ActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are for country iso3. Active Record Scope

Returns:

See Also:



46
# File 'app/models/shipping_option.rb', line 46

scope :for_country_iso3, ->(country_iso3) { for_country_iso(Country.iso3_to_iso_map[country_iso3]) }

.for_rmasActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are for rmas. Active Record Scope

Returns:

See Also:



47
# File 'app/models/shipping_option.rb', line 47

scope :for_rmas, -> { where('shipping_options.id IN (?)', OPTIONS_FOR_RMAS) }

.freightActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are freight. Active Record Scope

Returns:

See Also:



48
# File 'app/models/shipping_option.rb', line 48

scope :freight, -> { where("shipping_options.carrier = 'UPSFreight'") }

.get_appropriate(country_code, is_residential, purolator_ltl = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/shipping_option.rb', line 51

def self.get_appropriate(country_code, is_residential, purolator_ltl = nil)
  opts = []
  if country_code.in?(Country::EU_COUNTRIES_ISO)
    opts = ShippingOption.active.NLD.to_a # This should be more generic and add any european country?
  elsif country_code == 'CA'
    # puts "ShippingOption.get_appropriate: country_code: CA, purolator_ltl: #{purolator_ltl}"
    opts = ShippingOption.active.CAN.to_a
    unless purolator_ltl
      # puts "ShippingOption.get_appropriate: purolator_ltl IS NOT TRUE"
      purolator_freight_sos = ShippingOption.where(carrier: 'PurolatorFreight')
      purolator_freight_sos.each do |purolator_freight_so|
        opts.delete(purolator_freight_so)
      end
    end
  else
    opts = ShippingOption.active.USA.to_a
  end
  opts
end

.non_freightActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are non freight. Active Record Scope

Returns:

See Also:



49
# File 'app/models/shipping_option.rb', line 49

scope :non_freight, -> { where("shipping_options.carrier <> 'UPSFreight'") }

.non_overrideActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are non override. Active Record Scope

Returns:

See Also:



40
# File 'app/models/shipping_option.rb', line 40

scope :non_override, -> { where.not(shipping_options: { carrier: nil }) }

.options_for_preferred_shipping_method_select(country = 'USA') ⇒ Object



79
80
81
82
# File 'app/models/shipping_option.rb', line 79

def self.options_for_preferred_shipping_method_select(country = 'USA')
  # must only use supported shipping options here or free shipping becomes almost default
  ShippingOption.active.supported.for_country_iso3(country).where.not(carrier: nil).order(days_commitment: :desc).map { |so| ["#{so.description} #{so.country}, code: #{so.service_code}", so.name] }
end

.select_options(country_iso3 = nil, supported = nil, include_override = false) ⇒ Object



71
72
73
74
75
76
77
# File 'app/models/shipping_option.rb', line 71

def self.select_options(country_iso3 = nil, supported = nil, include_override = false)
  opts = ShippingOption.active
  opts = opts.non_override unless include_override == true
  opts = opts.supported if supported == true
  opts = opts.send(country_iso3.to_sym) if country_iso3.present?
  opts.order('carrier asc,days_commitment desc,description asc,country desc').map { |so| ["#{so.carrier}: #{so.description} (#{so.country}), code: #{so.service_code}", so.id] }
end

.supportedActiveRecord::Relation<ShippingOption>

A relation of ShippingOptions that are supported. Active Record Scope

Returns:

See Also:



39
# File 'app/models/shipping_option.rb', line 39

scope :supported, -> { where('shipping_options.is_third_party_only IS NOT TRUE and shipping_options.carrier IS NOT NULL') }

.supported_carrier_options_for_rmas(country = 'US') ⇒ Object



102
103
104
105
106
# File 'app/models/shipping_option.rb', line 102

def self.supported_carrier_options_for_rmas(country = 'US')
  supported_carriers = supported_carrier_options_for_select
  opts = ShippingOption.active.for_rmas.where(carrier: supported_carriers).where(country: country)
  opts.order('carrier asc,days_commitment desc,description asc,country desc').map { |so| ["#{so.carrier}: #{so.description} (#{so.country}), code: #{so.service_code}", so.id] }
end

.supported_carrier_options_for_select(country = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/shipping_option.rb', line 90

def self.supported_carrier_options_for_select(country = nil)
  if country
    SUPPORTED_SHIPPING_CARRIERS[country.to_sym] || []
  else
    carrier_options = []
    SUPPORTED_SHIPPING_CARRIERS.each do |country, carrier_options_arr|
      carrier_options.concat(carrier_options_arr)
    end
    carrier_options.uniq
  end
end

Instance Method Details

#country_iso3Object



130
131
132
# File 'app/models/shipping_option.rb', line 130

def country_iso3
  Country.find_by(iso: country).iso3
end

#is_economy?Boolean

Returns:

  • (Boolean)


108
109
110
111
# File 'app/models/shipping_option.rb', line 108

def is_economy?
  %w[ground standard fedex_ground fedex_ground_residential usps_media_mail usps_standard_post usps_parcel_select usps_priority_mail canada_post_regular_parcel canpar_ground purolator_ground
     purolator_ground_evening].include? name
end

#is_fedex_ca?Boolean

Returns:

  • (Boolean)


117
118
119
120
# File 'app/models/shipping_option.rb', line 117

def is_fedex_ca?
  nm = name.to_s.downcase
  nm.index('fedex_').present? && nm.index('_ca').present?
end

#is_freightquote?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'app/models/shipping_option.rb', line 122

def is_freightquote?
  carrier.to_s.downcase.index('freightquote').present?
end

#is_override?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'app/models/shipping_option.rb', line 126

def is_override?
  name.to_s.downcase == 'override'
end

#is_postal?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/shipping_option.rb', line 113

def is_postal?
  POSTAL_CARRIERS.include? carrier
end

#itemItem

Returns:

See Also:



33
# File 'app/models/shipping_option.rb', line 33

belongs_to :item, optional: true

#roomsActiveRecord::Relation<Room>

Returns:

  • (ActiveRecord::Relation<Room>)

See Also:



32
# File 'app/models/shipping_option.rb', line 32

has_many :rooms, through: :shipping_costs

#shipping_costsActiveRecord::Relation<ShippingCost>

Returns:

See Also:



31
# File 'app/models/shipping_option.rb', line 31

has_many :shipping_costs