Class: CycleCount
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- CycleCount
- 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 =
%{select count(cci.id) from cycle_count_items cci where cci.cycle_count_id = cycle_counts.id}.freeze
- ITEM_GROUPS =
You can define skus that must always be counted together, e.g
{}
Constants included from Models::Auditable
Models::Auditable::ALWAYS_IGNORED
Belongs to collapse
Methods included from Models::Auditable
Has many collapse
Class Method Summary collapse
-
.empty_cycle_counts ⇒ ActiveRecord::Relation<CycleCount>
A relation of CycleCounts that are empty cycle counts.
-
.with_items_count ⇒ ActiveRecord::Relation<CycleCount>
A relation of CycleCounts that are with items count.
Instance Method Summary collapse
-
#add_all_group_items ⇒ Object
Evaluate items that are in the cycle count, if any member of a group item are present.
- #all_cycle_count_items_processed? ⇒ Boolean
- #evaluate_and_process ⇒ Object
- #items_count ⇒ Object
-
#missing_group_store_items ⇒ Object
This returns an array of all missing store items from the cycle count.
- #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
Class Method Details
.empty_cycle_counts ⇒ ActiveRecord::Relation<CycleCount>
A relation of CycleCounts that are empty cycle counts. Active Record Scope
36 |
# File 'app/models/cycle_count.rb', line 36 scope :empty_cycle_counts, -> { with_items_count.where("(#{SQ_ITEMS_COUNT_SQL}) = 0") } |
.with_items_count ⇒ ActiveRecord::Relation<CycleCount>
A relation of CycleCounts that are with items count. Active Record Scope
33 34 35 |
# File 'app/models/cycle_count.rb', line 33 scope :with_items_count, -> { all.select_append("(#{SQ_ITEMS_COUNT_SQL}) as sq_items_count".sql_safe) } |
Instance Method Details
#add_all_group_items ⇒ Object
Evaluate items that are in the cycle count, if any member of a group
item are present
80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/models/cycle_count.rb', line 80 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
62 63 64 |
# File 'app/models/cycle_count.rb', line 62 def all_cycle_count_items_processed? cycle_count_items.all?(&:processed?) end |
#cycle_count_items ⇒ ActiveRecord::Relation<CycleCountItem>
29 |
# File 'app/models/cycle_count.rb', line 29 has_many :cycle_count_items, inverse_of: :cycle_count |
#evaluate_and_process ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'app/models/cycle_count.rb', line 108 def evaluate_and_process begin CycleCount.transaction do if (store_items = missing_group_store_items).present? raise GroupedItemsMissingFromCount.new("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 end |
#items_count ⇒ Object
66 67 68 69 70 71 72 |
# File 'app/models/cycle_count.rb', line 66 def items_count if respond_to?(:sq_items_count) sq_items_count else cycle_count_items.size end end |
#missing_group_store_items ⇒ Object
This returns an array of all missing store items from the cycle count
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/cycle_count.rb', line 93 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 |
#to_s ⇒ Object
74 75 76 |
# File 'app/models/cycle_count.rb', line 74 def to_s "Cycle Count # #{id}" end |