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)

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::EventPublishable

#publish_event

Instance Attribute Details

#item_idObject (readonly)



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

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

#locationObject (readonly)



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

validates :cycle_count, :location, presence: true

Instance Method Details

#current_qty_committedObject



61
62
63
# File 'app/models/cycle_count_item.rb', line 61

def current_qty_committed
  store_item&.qty_committed
end

#current_qty_on_handObject



57
58
59
# File 'app/models/cycle_count_item.rb', line 57

def current_qty_on_hand
  store_item&.qty_on_hand_with_held
end

#cycle_countCycleCount

Returns:

See Also:

Validations:



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

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

#evaluate_and_processObject



65
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
# File 'app/models/cycle_count_item.rb', line 65

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:

See Also:



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

belongs_to :item, optional: true

#item_ledger_entriesActiveRecord::Relation<ItemLedgerEntry>

Returns:

See Also:



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

has_many :item_ledger_entries

#qty_not_lower_than_qty_committedObject



118
119
120
121
122
# File 'app/models/cycle_count_item.rb', line 118

def qty_not_lower_than_qty_committed
  if item && qty_on_hand && current_qty_committed && (qty_on_hand < current_qty_committed)
    errors.add :base, "Quantity entered for #{item.sku} is lower than quantity already committed (#{current_qty_committed})"
  end
end

#storage_locationsActiveRecord::Relation<StorageLocation>

Returns:

See Also:



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

has_and_belongs_to_many :storage_locations

#store_itemObject



53
54
55
# File 'app/models/cycle_count_item.rb', line 53

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