Class: ItemDemandForecastAddition

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable, Models::Lineage
Defined in:
app/models/item_demand_forecast_addition.rb

Overview

== Schema Information

Table name: item_demand_forecast_additions
Database name: primary

id :integer not null, primary key
category :string
date :date
description :string
kit_quantity :integer
po_number :string
quantity :integer
created_at :datetime
updated_at :datetime
creator_id :integer
item_id :integer
order_id :integer
parent_id :integer
quote_id :integer
store_id :integer
updater_id :integer

Indexes

forecast_additions_on_item_and_date (item_id,date)
forecast_additions_on_store_and_item_and_date (store_id,item_id,date)
index_item_demand_forecast_additions_on_date (date)
index_item_demand_forecast_additions_on_order_id (order_id)
index_item_demand_forecast_additions_on_parent_id (parent_id)
index_item_demand_forecast_additions_on_quote_id (quote_id)

Constant Summary collapse

CATEGORIES =
{'amazon' => 'Amazon Bulk Buy', 'costco' => 'Costco Promotion', 'home_depot' => 'Home Depot Promotion', 'order' => 'Order', 'quote' => 'Quote', 'misc' => 'Misc'}
ADDITION_TYPES =
{'order' => 'Order', 'quote' => 'Quote'}

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

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 included from Models::Lineage

#ancestors, #ancestors_ids, #children_and_roots, #descendants, #descendants_ids, #ensure_non_recursive_lineage, #family_members, #generate_full_name, #generate_full_name_array, #lineage, #lineage_array, #lineage_simple, #root, #root_id, #self_ancestors_and_descendants, #self_ancestors_and_descendants_ids, #self_and_ancestors, #self_and_ancestors_ids, #self_and_children, #self_and_descendants, #self_and_descendants_ids, #self_and_siblings, #self_and_siblings_ids, #siblings, #siblings_ids

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#categoryObject (readonly)



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

validates :category, presence: true, inclusion: { in: CATEGORIES.keys, message: "must be either #{CATEGORIES.keys.join(', ')}" }

Class Method Details

.add_order(order, date) ⇒ Object



62
63
64
65
66
# File 'app/models/item_demand_forecast_addition.rb', line 62

def self.add_order(order, date)
  order.line_items.non_shipping.select('line_items.item_id,sum(line_items.quantity) as quantity').group('line_items.item_id').each do |li|
    ItemDemandForecastAddition.create!(store_id: order.store.id, item_id: li.item_id, quantity: li.quantity, date: date, category: 'order', order_id: order.id)
  end
end

.add_quote(quote, date) ⇒ Object



56
57
58
59
60
# File 'app/models/item_demand_forecast_addition.rb', line 56

def self.add_quote(quote, date)
  quote.line_items.non_shipping.select('line_items.item_id,sum(line_items.quantity) as quantity').group('line_items.item_id').each do |li|
    ItemDemandForecastAddition.create!(store_id: quote.store.id, item_id: li.item_id, quantity: li.quantity, date: date, category: 'quote', quote_id: quote.id)
  end
end

Instance Method Details

#category_prettyObject



68
69
70
# File 'app/models/item_demand_forecast_addition.rb', line 68

def category_pretty
  CATEGORIES[category]
end

#create_kit_componentsObject



88
89
90
91
92
93
94
95
96
# File 'app/models/item_demand_forecast_addition.rb', line 88

def create_kit_components
  item.kit_target_item_relations.each do |ir|
    fa = self.dup
    fa.parent_id = self.id
    fa.item_id = ir.target_item_id
    fa.quantity = self.quantity * ir.quantity
    fa.save!
  end
end

#delete_childrenObject



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

def delete_children
  children.delete_all
end

#itemItem

Returns:

See Also:



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

belongs_to :item, optional: true

#orderOrder

Returns:

See Also:



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

belongs_to :order, optional: true

#order_refObject



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

def order_ref
  order.nil? ? nil : order.reference_number
end

#order_ref=(order_ref) ⇒ Object



84
85
86
# File 'app/models/item_demand_forecast_addition.rb', line 84

def order_ref=(order_ref)
  self.order = order_ref.nil? ? nil : Order.where(reference_number: order_ref.upcase).first
end

#quoteQuote

Returns:

See Also:



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

belongs_to :quote, optional: true

#quote_refObject



72
73
74
# File 'app/models/item_demand_forecast_addition.rb', line 72

def quote_ref
  quote.nil? ? nil : quote.reference_number
end

#quote_ref=(quote_ref) ⇒ Object



76
77
78
# File 'app/models/item_demand_forecast_addition.rb', line 76

def quote_ref=(quote_ref)
  self.quote = quote_ref.nil? ? nil : Quote.where(reference_number: quote_ref.upcase.gsub('-R','-r')).first
end

#storeStore

Returns:

See Also:



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

belongs_to :store, optional: true

#update_kit_componentsObject



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

def update_kit_components
  children.delete_all
  create_kit_components
end