Class: SupplierItem
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- SupplierItem
- Includes:
- Models::Auditable
- Defined in:
- app/models/supplier_item.rb
Overview
== Schema Information
Table name: supplier_items
Database name: primary
id :integer not null, primary key
active :boolean default(TRUE), not null
auto_receive :boolean
stock_item :boolean default(TRUE), not null
supplier_description :string(255)
supplier_sku :string(255)
uom :string(255)
uom_quantity :integer
created_at :datetime
updated_at :datetime
creator_id :integer
item_id :integer
supplier_id :integer
updater_id :integer
Indexes
idx_item_id_active (item_id,active)
idx_supplier_id_item_id (supplier_id,item_id)
Foreign Keys
supplier_items_item_id_fk (item_id => items.id)
Constant Summary collapse
- SUPPLIER_IDS_WHOSE_DROPSHIPS_COST_FOR_CUSTOMER_IS_ZERO =
per Venu Heatlay Custom Mats dropship free
[363010].freeze
Constants included from Models::Auditable
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
- #item_id ⇒ Object readonly
-
#item_sku ⇒ Object
Returns the value of attribute item_sku.
- #uom ⇒ Object readonly
- #uom_quantity ⇒ Object readonly
Belongs to collapse
Methods included from Models::Auditable
Has many collapse
- #items ⇒ ActiveRecord::Relation<Item>
- #price_thresholds ⇒ ActiveRecord::Relation<PriceThreshold>
- #supplier_item_prices ⇒ ActiveRecord::Relation<SupplierItemPrice>
Class Method Summary collapse
-
.active ⇒ ActiveRecord::Relation<SupplierItem>
A relation of SupplierItems that are active.
- .uom_for_select ⇒ Object
Instance Method Summary collapse
- #current_price ⇒ Object
- #deep_dup ⇒ Object
- #dropships_cost_for_customer_is_zero? ⇒ Boolean
- #get_price_for_qty(qty) ⇒ Object
- #item_description ⇒ Object
- #to_s ⇒ Object
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
Instance Attribute Details
#item_id ⇒ Object (readonly)
57 58 59 60 |
# File 'app/models/supplier_item.rb', line 57 validates :item_id, uniqueness: { scope: :supplier_id, message: 'is already present on the price list for this supplier', unless: -> { item_id.nil? or supplier_id.nil? } } |
#item_sku ⇒ Object
Returns the value of attribute item_sku.
66 67 68 |
# File 'app/models/supplier_item.rb', line 66 def item_sku @item_sku end |
#uom ⇒ Object (readonly)
56 |
# File 'app/models/supplier_item.rb', line 56 validates :supplier, :item, :uom, :uom_quantity, presence: true |
#uom_quantity ⇒ Object (readonly)
56 |
# File 'app/models/supplier_item.rb', line 56 validates :supplier, :item, :uom, :uom_quantity, presence: true |
Class Method Details
.active ⇒ ActiveRecord::Relation<SupplierItem>
A relation of SupplierItems that are active. Active Record Scope
70 |
# File 'app/models/supplier_item.rb', line 70 scope :active, -> { where(active: true) } |
.uom_for_select ⇒ Object
107 108 109 |
# File 'app/models/supplier_item.rb', line 107 def self.uom_for_select SupplierItem.where.not(uom: nil).distinct.order(:uom).pluck(:uom).map{|e| e.squish.upcase}.uniq end |
Instance Method Details
#current_price ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/supplier_item.rb', line 72 def current_price if supplier_item_prices.loaded? # Use in-memory filtering when the association is preloaded (e.g. via # includes(supplier_items: :supplier_item_prices) in calculate_declared_value) # to avoid a per-supplier-item ORDER+LIMIT SQL query. supplier_item_prices .select { |p| p.effective_from <= Date.current } .max_by { |p| [p.effective_from, p.created_at] } else supplier_item_prices.current.last end end |
#deep_dup ⇒ Object
37 38 39 |
# File 'app/models/supplier_item.rb', line 37 def deep_dup deep_clone(include: [:supplier_item_prices, :price_thresholds]) end |
#dropships_cost_for_customer_is_zero? ⇒ Boolean
111 112 113 |
# File 'app/models/supplier_item.rb', line 111 def dropships_cost_for_customer_is_zero? SUPPLIER_IDS_WHOSE_DROPSHIPS_COST_FOR_CUSTOMER_IS_ZERO.include?(supplier_id) end |
#get_price_for_qty(qty) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'app/models/supplier_item.rb', line 85 def get_price_for_qty(qty) price = current_price.unit_cost if price_thresholds.any? price_thresholds.each do |pt| price = pt.price if (qty >= pt.min_quantity) && (pt.price < price) end end price end |
#item ⇒ Item
Validations:
42 |
# File 'app/models/supplier_item.rb', line 42 belongs_to :item, inverse_of: :supplier_items, optional: true |
#item_description ⇒ Object
99 100 101 |
# File 'app/models/supplier_item.rb', line 99 def item_description item.try(:name) end |
#items ⇒ ActiveRecord::Relation<Item>
46 |
# File 'app/models/supplier_item.rb', line 46 has_many :items, inverse_of: :supplier_item |
#price_thresholds ⇒ ActiveRecord::Relation<PriceThreshold>
45 |
# File 'app/models/supplier_item.rb', line 45 has_many :price_thresholds |
#supplier ⇒ Supplier
Validations:
41 |
# File 'app/models/supplier_item.rb', line 41 belongs_to :supplier, optional: true |
#supplier_item_prices ⇒ ActiveRecord::Relation<SupplierItemPrice>
44 |
# File 'app/models/supplier_item.rb', line 44 has_many :supplier_item_prices, -> { order('effective_from ASC, created_at ASC') }, inverse_of: :supplier_item |
#to_s ⇒ Object
103 104 105 |
# File 'app/models/supplier_item.rb', line 103 def to_s "#{id} (#{supplier.name})" end |