Class: ShippingAccountNumber

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/shipping_account_number.rb

Overview

== Schema Information

Table name: shipping_account_numbers
Database name: primary

id :integer not null, primary key
account_number :string(255)
billing_zip :string(255)
carrier :string(255)
country_iso3 :string
is_inactive :boolean default(FALSE), not null
ship_natively :boolean default(FALSE), not null
created_at :datetime
updated_at :datetime
address_id :integer
customer_id :integer
phone_id :integer

Indexes

idx_customer_id (customer_id)
index_shipping_account_numbers_on_address_id (address_id)
index_shipping_account_numbers_on_country_iso3 (country_iso3)
index_shipping_account_numbers_on_is_inactive (is_inactive)
index_shipping_account_numbers_on_ship_natively (ship_natively)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#account_numberObject (readonly)



64
# File 'app/models/shipping_account_number.rb', line 64

validates :carrier, :account_number, :country_iso3, presence: true

#billing_zipObject (readonly)



66
# File 'app/models/shipping_account_number.rb', line 66

validates :billing_zip, zipcode: { country_code_attribute: :country_iso }

#carrierObject (readonly)



64
# File 'app/models/shipping_account_number.rb', line 64

validates :carrier, :account_number, :country_iso3, presence: true

#country_iso3Object (readonly)



64
# File 'app/models/shipping_account_number.rb', line 64

validates :carrier, :account_number, :country_iso3, presence: true

Class Method Details

.activeActiveRecord::Relation<ShippingAccountNumber>

A relation of ShippingAccountNumbers that are active. Active Record Scope

Returns:

See Also:



70
# File 'app/models/shipping_account_number.rb', line 70

scope :active, -> { where.not(is_inactive: true) }

.customer_address_options_for_select(customer) ⇒ Object



86
87
88
# File 'app/models/shipping_account_number.rb', line 86

def self.customer_address_options_for_select(customer)
  customer.addresses.map { |add| [add.full_address(true, ", "), add.id] }
end

.customer_phone_options_for_select(customer) ⇒ Object



90
91
92
# File 'app/models/shipping_account_number.rb', line 90

def self.customer_phone_options_for_select(customer)
  customer.contact_points.voice_callable.map { |phn| ["#{phn.category}: #{phn.detail}", phn.id] }
end

.for_countryActiveRecord::Relation<ShippingAccountNumber>

A relation of ShippingAccountNumbers that are for country. Active Record Scope

Returns:

See Also:



72
# File 'app/models/shipping_account_number.rb', line 72

scope :for_country, ->(country) { where(country_iso3: country) }

.inactiveActiveRecord::Relation<ShippingAccountNumber>

A relation of ShippingAccountNumbers that are inactive. Active Record Scope

Returns:

See Also:



71
# File 'app/models/shipping_account_number.rb', line 71

scope :inactive, -> { where(is_inactive: true) }

Instance Method Details

#addressAddress

Returns:

See Also:



59
# File 'app/models/shipping_account_number.rb', line 59

belongs_to :address, optional: true

#check_ok_to_destroyObject



109
110
111
112
113
# File 'app/models/shipping_account_number.rb', line 109

def check_ok_to_destroy
  return if ok_to_delete?

  errors.add(:base, "Cannot destroy because there are dependent deliveries, address or phone")
end

#country_isoObject



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

def country_iso
  iso3166_country&.alpha2
end

#customerCustomer

Returns:

See Also:



58
# File 'app/models/shipping_account_number.rb', line 58

belongs_to :customer, optional: true

#deactivateObject



101
102
103
# File 'app/models/shipping_account_number.rb', line 101

def deactivate
  update_attribute(:is_inactive, true)
end

#deactivate_or_destroyObject



115
116
117
118
119
120
121
122
123
# File 'app/models/shipping_account_number.rb', line 115

def deactivate_or_destroy
  if ok_to_delete?
    destroy
    { status: :info, message: 'Shipping Account Deleted' }
  else
    deactivate
    { status: :warning, message: 'Shipping Account has dependent deliveries, address or phone.  Deactivated instead' }
  end
end

#deliveriesActiveRecord::Relation<Delivery>

Returns:

See Also:



61
# File 'app/models/shipping_account_number.rb', line 61

has_many :deliveries, -> { order(:origin_address_id) }

#iso3166_countryObject



78
79
80
# File 'app/models/shipping_account_number.rb', line 78

def iso3166_country
  ISO3166::Country.find_country_by_alpha3(country_iso3)
end

#nameObject



82
83
84
# File 'app/models/shipping_account_number.rb', line 82

def name
  "#{carrier} shipping account number #{} for #{customer.name}"
end

#ok_to_delete?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'app/models/shipping_account_number.rb', line 105

def ok_to_delete?
  deliveries.empty? || address.nil? || phone.nil?
end

#phoneContactPoint



60
# File 'app/models/shipping_account_number.rb', line 60

belongs_to :phone, class_name: 'ContactPoint', optional: true

#shipmentsActiveRecord::Relation<Shipment>

Returns:

See Also:



62
# File 'app/models/shipping_account_number.rb', line 62

has_many :shipments, -> { order(:id) }, through: :deliveries

#validate_ship_nativelyObject



94
95
96
97
98
99
# File 'app/models/shipping_account_number.rb', line 94

def validate_ship_natively
  return unless ship_natively
  return if SUPPORTED_SHIPPING_CARRIERS[country_iso3.to_sym].include?(carrier)

  errors.add(:ship_natively, "is only supported for #{SUPPORTED_SHIPPING_CARRIERS[customer.country_iso3.to_sym].join(',')}")
end