Class: CycleCount

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/cycle_count.rb

Overview

== Schema Information

Table name: cycle_counts
Database name: primary

id :integer not null, primary key
date_processed :date
due_date :date
marketing_only :boolean default(FALSE)
remark :text
state :string(255)
created_at :datetime
updated_at :datetime
creator_id :integer
store_id :integer
updater_id :integer

Indexes

s_id_state_mo (store_id,state,marketing_only)

Defined Under Namespace

Classes: GroupedItemsMissingFromCount

Constant Summary collapse

SQ_ITEMS_COUNT_SQL =

Sq items count sql.

%{select count(cci.id) from cycle_count_items cci where cci.cycle_count_id = cycle_counts.id}
ITEM_GROUPS =

You can define skus that must always be counted together, e.g

{}.freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.empty_cycle_countsActiveRecord::Relation<CycleCount>

A relation of CycleCounts that are empty cycle counts. Active Record Scope

Returns:

See Also:



37
# File 'app/models/cycle_count.rb', line 37

scope :empty_cycle_counts, -> { with_items_count.where("(#{SQ_ITEMS_COUNT_SQL}) = 0") }

.with_items_countActiveRecord::Relation<CycleCount>

A relation of CycleCounts that are with items count. Active Record Scope

Returns:

See Also:



36
# File 'app/models/cycle_count.rb', line 36

scope :with_items_count, -> { all.select_append("(#{SQ_ITEMS_COUNT_SQL}) as sq_items_count".sql_safe) }

Instance Method Details

#add_all_group_itemsObject

Evaluate items that are in the cycle count, if any member of a group
item are present



81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/cycle_count.rb', line 81

def add_all_group_items
  res = { skus_added: [] }
  missing_group_store_items.each do |store_item|
    # Add this missing item to our count, notice how this is location specific
    cycle_count_items.create! item_id: store_item.item_id,
                              location: store_item.location,
                              storage_location_ids: store_item.storage_location_ids
    res[:skus_added] << store_item.item.sku
  end
  res
end

#all_cycle_count_items_processed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/cycle_count.rb', line 63

def all_cycle_count_items_processed?
  cycle_count_items.all?(&:processed?)
end

#cycle_count_itemsActiveRecord::Relation<CycleCountItem>

Returns:

See Also:



31
# File 'app/models/cycle_count.rb', line 31

has_many :cycle_count_items, inverse_of: :cycle_count

#evaluate_and_processObject



109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/cycle_count.rb', line 109

def evaluate_and_process
  CycleCount.transaction do
    if (store_items = missing_group_store_items).present?
      raise GroupedItemsMissingFromCount, "Cycle Count includes item that must be counted in group, add: #{store_items.map { |si| "#{si.item.sku} - #{si.location}" }.join(', ')} to the count"
    end

    cycle_count_items.each(&:process!)
    process!
  end
rescue StandardError => e
  errors.add(:base, e.to_s)
end

#items_countObject



67
68
69
70
71
72
73
# File 'app/models/cycle_count.rb', line 67

def items_count
  if respond_to?(:sq_items_count)
    sq_items_count
  else
    cycle_count_items.size
  end
end

#missing_group_store_itemsObject

This returns an array of all missing store items from the cycle count



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/cycle_count.rb', line 94

def missing_group_store_items
  missing_store_items = []
  ITEM_GROUPS.each do |_group, skus|
    # Find out which items from this group are in our acount, group by location
    # We will need to count all the group items in each specified location
    grouped_items_in_count = cycle_count_items.joins(:item).where(items: { sku: skus }).group_by(&:location)
    grouped_items_in_count.each do |location, cycle_count_items_in_group|
      skus_in_count = cycle_count_items_in_group.map { |cci| cci.item.sku }
      missing_item_skus = skus - skus_in_count
      missing_store_items += store.store_items.eager_load(:item).where(location: location, items: { sku: missing_item_skus }).to_a
    end
  end
  missing_store_items
end

#storeStore

Returns:

See Also:



30
# File 'app/models/cycle_count.rb', line 30

belongs_to :store

#to_sObject



75
76
77
# File 'app/models/cycle_count.rb', line 75

def to_s
  "Cycle Count # #{id}"
end