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

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 Models::EventPublishable

#publish_event

Instance Attribute Details

#billing_zipObject (readonly)



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

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

Class Method Details

.activeActiveRecord::Relation<ShippingAccountNumber>

A relation of ShippingAccountNumbers that are active. Active Record Scope

Returns:

See Also:



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

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

.customer_address_options_for_select(customer) ⇒ Object



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

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



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

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:



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

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

.inactiveActiveRecord::Relation<ShippingAccountNumber>

A relation of ShippingAccountNumbers that are inactive. Active Record Scope

Returns:

See Also:



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

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

Instance Method Details

#addressAddress

Returns:

See Also:



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

belongs_to :address, optional: true

#check_ok_to_destroyObject



107
108
109
110
# File 'app/models/shipping_account_number.rb', line 107

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



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

def country_iso
  iso3166_country&.alpha2
end

#customerCustomer

Returns:

See Also:



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

belongs_to :customer, optional: true

#deactivateObject



99
100
101
# File 'app/models/shipping_account_number.rb', line 99

def deactivate
  update_attribute(:is_inactive, true)
end

#deactivate_or_destroyObject



112
113
114
115
116
117
118
119
120
# File 'app/models/shipping_account_number.rb', line 112

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

#deliveriesActiveRecord::Relation<Delivery>

Returns:

See Also:



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

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

#iso3166_countryObject



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

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

#nameObject



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

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

#ok_to_delete?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/shipping_account_number.rb', line 103

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

#phoneContactPoint



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

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

#shipmentsActiveRecord::Relation<Shipment>

Returns:

See Also:



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

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

#validate_ship_nativelyObject



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

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