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 =

Categories.

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

Recognised addition types.

{ 'order' => 'Order', 'quote' => 'Quote' }.freeze

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

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#categoryObject (readonly)



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

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

#dateObject (readonly)



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

validates :item_id, :date, :quantity, :store_id, presence: true

#descriptionObject (readonly)



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

validates :description, presence: { if: proc { |fa| %w[costco home_depot].include?(fa.category) }, message: 'must be present if category is Home Depot or Costco' }

#item_idObject (readonly)



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

validates :item_id, :date, :quantity, :store_id, presence: true

#order_idObject (readonly)



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

validates :order_id, presence: { if: proc { |fa| fa.category == "order" }, message: 'must be present if category is Order' }

#quantityObject (readonly)



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

validates :item_id, :date, :quantity, :store_id, presence: true

#quote_idObject (readonly)



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

validates :quote_id, presence: { if: proc { |fa| fa.category == "quote" }, message: 'must be present if category is Quote' }

#store_idObject (readonly)



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

validates :item_id, :date, :quantity, :store_id, presence: true

Class Method Details

.add_order(order, date) ⇒ Object



64
65
66
67
68
# File 'app/models/item_demand_forecast_addition.rb', line 64

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



58
59
60
61
62
# File 'app/models/item_demand_forecast_addition.rb', line 58

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



70
71
72
# File 'app/models/item_demand_forecast_addition.rb', line 70

def category_pretty
  CATEGORIES[category]
end

#create_kit_componentsObject



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

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

#delete_childrenObject



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

def delete_children
  children.delete_all
end

#itemItem

Returns:

See Also:



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

belongs_to :item, optional: true

#orderOrder

Returns:

See Also:



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

belongs_to :order, optional: true

#order_refObject



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

def order_ref
  order&.reference_number
end

#order_ref=(order_ref) ⇒ Object



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

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

#quoteQuote

Returns:

See Also:



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

belongs_to :quote, optional: true

#quote_refObject



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

def quote_ref
  quote&.reference_number
end

#quote_ref=(quote_ref) ⇒ Object



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

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:



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

belongs_to :store, optional: true

#update_kit_componentsObject



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

def update_kit_components
  children.delete_all
  create_kit_components
end