Class: CycleCountItem

Inherits:
ApplicationRecord show all
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 Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Has many collapse

Has and belongs to many collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#item_idInteger (readonly)

Returns:

  • (Integer)


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

validates :item_id, numericality: { only_integer: true }, presence: true

#locationString? (readonly)

Returns:

  • (String, nil)


38
# File 'app/models/cycle_count_item.rb', line 38

validates :location, presence: true

Instance Method Details

#current_qty_committedInteger?

Returns:

  • (Integer, nil)


73
74
75
# File 'app/models/cycle_count_item.rb', line 73

def current_qty_committed
  store_item&.qty_committed
end

#current_qty_on_handInteger?

Returns:

  • (Integer, nil)


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

def current_qty_on_hand
  store_item&.qty_on_hand_with_held
end

#cycle_countCycleCount?

Returns:



27
# File 'app/models/cycle_count_item.rb', line 27

belongs_to :cycle_count, inverse_of: :cycle_count_items, optional: true

#evaluate_and_processvoid

This method returns an undefined value.



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
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/cycle_count_item.rb', line 78

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

#itemItem?

Returns:



29
# File 'app/models/cycle_count_item.rb', line 29

belongs_to :item, optional: true

#item_ledger_entriesActiveRecord::Relation<ItemLedgerEntry>

Returns:



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

has_many :item_ledger_entries

#qty_not_lower_than_qty_committedvoid

This method returns an undefined value.



132
133
134
# File 'app/models/cycle_count_item.rb', line 132

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_locationsActiveRecord::Relation<StorageLocation>

Returns:



33
# File 'app/models/cycle_count_item.rb', line 33

has_and_belongs_to_many :storage_locations

#store_itemStoreItem?

Returns:



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

def store_item
  StoreItem.where(store_id: cycle_count.store_id, item_id: item_id, location: location).first
end