Class: ShippingAccountNumber
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
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
#creator, #updater
Class Method Summary
collapse
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
#publish_event
Instance Attribute Details
#billing_zip ⇒ Object
65
|
# File 'app/models/shipping_account_number.rb', line 65
validates :billing_zip, zipcode: { country_code_attribute: :country_iso }
|
Class Method Details
A relation of ShippingAccountNumbers that are active. Active Record Scope
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
|
A relation of ShippingAccountNumbers that are for country. Active Record Scope
71
|
# File 'app/models/shipping_account_number.rb', line 71
scope :for_country, ->(country) { where(country_iso3: country) }
|
A relation of ShippingAccountNumbers that are inactive. Active Record Scope
70
|
# File 'app/models/shipping_account_number.rb', line 70
scope :inactive, -> { where(is_inactive: true) }
|
Instance Method Details
58
|
# File 'app/models/shipping_account_number.rb', line 58
belongs_to :address, optional: true
|
#check_ok_to_destroy ⇒ Object
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_iso ⇒ Object
73
74
75
|
# File 'app/models/shipping_account_number.rb', line 73
def country_iso
iso3166_country&.alpha2
end
|
57
|
# File 'app/models/shipping_account_number.rb', line 57
belongs_to :customer, optional: true
|
#deactivate ⇒ Object
99
100
101
|
# File 'app/models/shipping_account_number.rb', line 99
def deactivate
update_attribute(:is_inactive, true)
end
|
#deactivate_or_destroy ⇒ Object
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
|
#deliveries ⇒ ActiveRecord::Relation<Delivery>
60
|
# File 'app/models/shipping_account_number.rb', line 60
has_many :deliveries, -> { order(:origin_address_id) }
|
#iso3166_country ⇒ Object
77
78
79
|
# File 'app/models/shipping_account_number.rb', line 77
def iso3166_country
ISO3166::Country.find_country_by_alpha3(country_iso3)
end
|
#name ⇒ Object
81
82
83
|
# File 'app/models/shipping_account_number.rb', line 81
def name
"#{carrier} shipping account number #{account_number} for #{customer.name}"
end
|
#ok_to_delete? ⇒ Boolean
103
104
105
|
# File 'app/models/shipping_account_number.rb', line 103
def ok_to_delete?
deliveries.empty? || address.nil? || phone.nil?
end
|
59
|
# File 'app/models/shipping_account_number.rb', line 59
belongs_to :phone, class_name: 'ContactPoint', optional: true
|
#shipments ⇒ ActiveRecord::Relation<Shipment>
61
|
# File 'app/models/shipping_account_number.rb', line 61
has_many :shipments, -> { order(:id) }, through: :deliveries
|
#validate_ship_natively ⇒ Object
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
|