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 Schedulable

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#item_idObject (readonly)



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

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

#locationObject (readonly)



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

validates :cycle_count, :location, presence: true

Instance Method Details

#current_qty_committedObject



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

def current_qty_committed
  store_item&.qty_committed
end

#current_qty_on_handObject



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_countCycleCount

Returns:

See Also:

Validations:



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

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

#evaluate_and_processObject



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

#itemItem

Returns:

See Also:



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

belongs_to :item, optional: true

#item_ledger_entriesActiveRecord::Relation<ItemLedgerEntry>

Returns:

See Also:



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

has_many :item_ledger_entries

#qty_not_lower_than_qty_committedObject



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

Returns:

See Also:



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

has_and_belongs_to_many :storage_locations

#store_itemObject



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