Class: CycleCountItem
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- CycleCountItem
- Defined in:
- app/models/cycle_count_item.rb
Overview
== Schema Information
Table name: cycle_count_items
Database name: primary
id :integer not null, primary key
date_processed :date
difference :integer
location :string(255)
qty_on_hand :integer
remark :text
state :string(255)
created_at :datetime
updated_at :datetime
cycle_count_id :integer
item_id :integer
Indexes
by_iid_loc_st_ccid (item_id,location,state,cycle_count_id)
index_cycle_count_items_on_cycle_count_id (cycle_count_id)
Constant Summary
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #item_id ⇒ Object readonly
- #location ⇒ Object readonly
Belongs to collapse
Has many collapse
Has and belongs to many collapse
Instance Method Summary collapse
- #current_qty_committed ⇒ Object
- #current_qty_on_hand ⇒ Object
- #evaluate_and_process ⇒ Object
- #qty_not_lower_than_qty_committed ⇒ Object
- #store_item ⇒ Object
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#item_id ⇒ Object (readonly)
31 |
# File 'app/models/cycle_count_item.rb', line 31 validates :item_id, numericality: { only_integer: true }, presence: true |
#location ⇒ Object (readonly)
32 |
# File 'app/models/cycle_count_item.rb', line 32 validates :cycle_count, :location, presence: true |
Instance Method Details
#current_qty_committed ⇒ Object
62 63 64 |
# File 'app/models/cycle_count_item.rb', line 62 def current_qty_committed store_item&.qty_committed end |
#current_qty_on_hand ⇒ Object
58 59 60 |
# File 'app/models/cycle_count_item.rb', line 58 def current_qty_on_hand store_item&.qty_on_hand_with_held end |
#cycle_count ⇒ CycleCount
Validations:
26 |
# File 'app/models/cycle_count_item.rb', line 26 belongs_to :cycle_count, inverse_of: :cycle_count_items, optional: true |
#evaluate_and_process ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/models/cycle_count_item.rb', line 66 def evaluate_and_process CycleCountItem.transaction do # qty_on_hand is what's counted diff = qty_on_hand - store_item.qty_on_hand_with_held # e.g Gizmo X has 100 in avail, 20 in held, and i'm counting 10. I need to remove 100 from avail, 10 from held # in my example, diff is -110 self.difference = diff if diff != 0 # If this is an available location count, then we have to deduct from available first # But imagine the difference is -110, in that case available only gets -100 and -10 to held # if difference is positive doesn't matter always goes to AVAILABLE if store_item.available_location? && diff.negative? held_diff = store_item.qty_on_hand + diff # 100 - 110 = -10 from held # if we end up tapping into held stock, just empty the stock in AVAILABLE diff = -store_item.qty_on_hand if held_diff.negative? end ItemLedgerEntry.inventory_adjustment_individual({ store_id: cycle_count.store.id, item_id: item_id, gl_date: Date.current, quantity: diff, location: location, currency: cycle_count.store.company.currency, unit_cost: store_item.unit_cogs, total_cost: store_item.unit_cogs * diff, description: remark, cycle_count_item_id: id, category: 'CYCLE_COUNT' }) if held_diff&.negative? store_item_held = item.store_items.where(location: 'HELD').first raise "HELD store item was not found for #{item.id}" unless store_item_held # Find the store item for held ItemLedgerEntry.inventory_adjustment_individual({ store_id: cycle_count.store.id, item_id: item_id, gl_date: Date.current, quantity: held_diff, location: 'HELD', currency: cycle_count.store.company.currency, unit_cost: store_item_held.unit_cogs, total_cost: store_item_held.unit_cogs * held_diff, description: "#{remark} (HELD overflow)", cycle_count_item_id: id, category: 'CYCLE_COUNT' }) end end end end |
#item_ledger_entries ⇒ ActiveRecord::Relation<ItemLedgerEntry>
28 |
# File 'app/models/cycle_count_item.rb', line 28 has_many :item_ledger_entries |
#qty_not_lower_than_qty_committed ⇒ Object
119 120 121 |
# File 'app/models/cycle_count_item.rb', line 119 def qty_not_lower_than_qty_committed errors.add :base, "Quantity entered for #{item.sku} is lower than quantity already committed (#{current_qty_committed})" if item && qty_on_hand && current_qty_committed && (qty_on_hand < current_qty_committed) end |
#storage_locations ⇒ ActiveRecord::Relation<StorageLocation>
29 |
# File 'app/models/cycle_count_item.rb', line 29 has_and_belongs_to_many :storage_locations |
#store_item ⇒ Object
54 55 56 |
# File 'app/models/cycle_count_item.rb', line 54 def store_item StoreItem.where(store_id: cycle_count.store_id, item_id: item_id, location: location).first end |