inventory
This commit is contained in:
@@ -2,7 +2,7 @@ class InventoryDefinition < ApplicationRecord
|
||||
|
||||
scope :active, -> {where(:is_active => true)}
|
||||
|
||||
def calculate_product_count(saleObj)
|
||||
def self.calculate_product_count(saleObj)
|
||||
saleObj.sale_items.each do |item|
|
||||
found, inventory_definition = find_product_in_inventory(item)
|
||||
if found
|
||||
@@ -20,20 +20,22 @@ class InventoryDefinition < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def self.check_balance(item,inventory_definition)
|
||||
stock = StockJournal.where('item_code=?', item.item_code).order('created_at desc').take
|
||||
def self.check_balance(item,inventory_definition) # item => saleItemOBj
|
||||
stock = StockJournal.where('item_code=?', item.product_code).order('created_at desc').take
|
||||
unless stock.nil?
|
||||
modify_balance(item, stock.balance, inventory_definition)
|
||||
modify_balance(item, stock, inventory_definition)
|
||||
else
|
||||
StockJournal.add_to_journal(item, 0, "out of stock", inventory_definition)
|
||||
end
|
||||
end
|
||||
|
||||
def self.modify_balance(item, stock_balance, inventory_definition)
|
||||
def self.modify_balance(item, stock, inventory_definition) #saleitemObj
|
||||
if stock.balance.to_i >= item.qty
|
||||
puts ">> stock is greater than orde qty"
|
||||
add_to_journal(item, balance, "ok", inventory_definition)
|
||||
StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition)
|
||||
else
|
||||
puts " << stock is less than order qty"
|
||||
add_to_journal(item, balance, "out of stock", inventory_definition)
|
||||
StockJournal.add_to_journal(item, stock.balance, "out of stock", inventory_definition)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -118,7 +118,8 @@ class Sale < ApplicationRecord
|
||||
create_saleitem_diningcharges(charges, diningprice, booking.dining_facility.name, dining_time)
|
||||
end
|
||||
|
||||
InventoryJob.perform_later(self.id)
|
||||
InventoryJob.perform_now(self.id)
|
||||
|
||||
return true, self.id
|
||||
end
|
||||
|
||||
|
||||
@@ -1,2 +1,17 @@
|
||||
class StockCheck < ApplicationRecord
|
||||
|
||||
has_many :stock_check_items
|
||||
|
||||
def create(user, eason, item_list)
|
||||
self.reason = reason
|
||||
self.check_by = user.id
|
||||
self.check_start = Time.now
|
||||
self.check_end = Time.now
|
||||
self.save
|
||||
item_list.each do |item|
|
||||
stockItem = StockCheckItem.new
|
||||
stockItem.create(self.id,item)
|
||||
end
|
||||
return self
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,2 +1,36 @@
|
||||
class StockCheckItem < ApplicationRecord
|
||||
|
||||
belongs_to :stock_check
|
||||
|
||||
def create(stock_id, item)
|
||||
journal_id, balance = StockCheckItem.find_journal(item['sku'])
|
||||
remark, different = StockCheckItem.stock_different(item['qty'], balance )
|
||||
self.stock_check_id = stock_id
|
||||
self.item_code = item['sku']
|
||||
self.stock_count = item['qty']
|
||||
self.stock_journal_id = journal_id
|
||||
self.stock_balance = balance
|
||||
self.different = different
|
||||
self.remark = remark
|
||||
self.save
|
||||
end
|
||||
|
||||
def self.find_journal(item_code)
|
||||
journal = StockJournal.where('item_code=?', item_code).order('created_at desc').take
|
||||
if journal
|
||||
return journal.id, journal.balance
|
||||
else
|
||||
return nil, 0
|
||||
end
|
||||
end
|
||||
|
||||
def self.stock_different(stock_check_qty, journal_balance)
|
||||
if stock_check_qty.to_i == journal_balance.to_i
|
||||
return 'match', stock_check_qty
|
||||
elsif stock_check_qty.to_i > journal_balance.to_i
|
||||
return 'missing order item', stock_check_qty.to_i - journal_balance.to_i
|
||||
elsif stock_check_qty.to_i < journal_balance.to_i
|
||||
return 'missing stock', stock_check_qty.to_i - journal_balance.to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,22 +3,37 @@ class StockJournal < ApplicationRecord
|
||||
SALES_TRANS = "sale"
|
||||
STOCK_CHECK_TRANS = "stock_check"
|
||||
|
||||
def add_to_journal(item, balance, stock_message, inventory_definition)
|
||||
def self.add_to_journal(item, balance, stock_message, inventory_definition) # item => saleObj | balance => Stock journal
|
||||
|
||||
balance = calculate_balance(balance, item.qty)
|
||||
|
||||
journal = StockJournal.new
|
||||
journal.item_code = item.item_code
|
||||
journal.item_code = item.product_code
|
||||
journal.inventory_definition_id = inventory_definition.id
|
||||
journal.debit = item.qty
|
||||
journal.balance = balance
|
||||
journal.remark = stock_message
|
||||
journal.trans_ref = item.id
|
||||
journal.trans_type = StockJournal::SALES_TRANS
|
||||
journal.save
|
||||
end
|
||||
|
||||
def self.calculate_balance(balance, qty)
|
||||
return balance.to_i - qty.to_i
|
||||
end
|
||||
|
||||
def self.from_stock_check(item)
|
||||
definition_id = InventoryDefinition.find_by_item_code(item.item_code)
|
||||
journal = StockJournal.new
|
||||
journal.item_code = item.item_code
|
||||
journal.inventory_definition_id = definition_id.id
|
||||
journal.debit = 0
|
||||
journal.credit = item.stock_count
|
||||
journal.balance = item.stock_count
|
||||
journal.remark = StockJournal::STOCK_CHECK_TRANS
|
||||
journal.trans_ref = item.id
|
||||
journal.trans_type = StockJournal::STOCK_CHECK_TRANS
|
||||
journal.save
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user