fixed inventory
This commit is contained in:
@@ -3,6 +3,9 @@ class InventoryDefinition < ApplicationRecord
|
||||
scope :active, -> {where(:is_active => true)}
|
||||
|
||||
def self.calculate_product_count(saleObj=nil,saleobj_after_req_bill=nil)
|
||||
# check_sj = StockJournal.where('item_code=?', item.item_instance_code).order("id DESC").first
|
||||
logger.debug 'saleObj<<<<<<<<<<<<<<<<<<<<<<<<<<<'
|
||||
logger.debug saleObj.sale_items.to_json
|
||||
if !saleObj.nil?
|
||||
saleObj.sale_items.each do |item|
|
||||
found, inventory_definition = find_product_in_inventory(item)
|
||||
@@ -34,81 +37,41 @@ class InventoryDefinition < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def self.check_balance(item,inventory_definition) # item => saleItemOBj
|
||||
def self.check_balance(item, inventory_definition) # item => saleItemOBj
|
||||
stock = StockJournal.where('item_code=?', item.item_instance_code).order("id DESC").first
|
||||
unless stock.nil?
|
||||
modify_balance(item, stock, inventory_definition)
|
||||
else
|
||||
StockJournal.add_to_journal(item, 0, "out of stock", inventory_definition)
|
||||
puts "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< OUT OF STOCK >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
|
||||
StockJournal.add_to_journal(item.item_instance_code, item.qty, 0, "out of stock", inventory_definition, item.id, StockJournal::SALES_TRANS)
|
||||
end
|
||||
end
|
||||
|
||||
def self.modify_balance(item, stock, inventory_definition) #saleitemObj
|
||||
void_qty = 0
|
||||
if item.product_name.downcase.include?("void")
|
||||
void_qty = item.qty.abs.to_i
|
||||
end
|
||||
check_item = StockCheckItem.where('item_code=?', item.item_instance_code).order("id DESC").first
|
||||
if stock.balance.to_i >= item.qty
|
||||
puts ">> stock is greater than order qty"
|
||||
if item.qty.to_i > 0
|
||||
StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition)
|
||||
if !void_qty.nil? | void_qty != 0
|
||||
check_item.different = check_item.different - item.qty
|
||||
check_item.save
|
||||
end
|
||||
else
|
||||
if item.product_name.downcase.include?("void")
|
||||
StockJournal.add_to_journal(item, stock.balance, "void", inventory_definition)
|
||||
check_item.different = check_item.different + void_qty
|
||||
check_item.save
|
||||
end
|
||||
end
|
||||
else
|
||||
puts " << stock is less than order qty"
|
||||
data = item
|
||||
extra_qty = item.qty.to_i - stock.balance.to_i
|
||||
if stock.balance.to_i > 0
|
||||
item.qty = stock.balance.to_i
|
||||
if item.qty.to_i > 0
|
||||
StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition)
|
||||
check_item.different = check_item.different - item.qty
|
||||
check_item.save
|
||||
data.qty = extra_qty
|
||||
StockJournal.add_to_journal(data, 0, "out of stock", inventory_definition)
|
||||
if !void_qty.nil? | void_qty != 0
|
||||
check_item.different = check_item.different - item.qty
|
||||
check_item.save
|
||||
else
|
||||
check_item.different = check_item.different + void_qty
|
||||
check_item.save
|
||||
end
|
||||
else
|
||||
if item.product_name.downcase.include?("void")
|
||||
StockJournal.add_to_journal(item, stock.balance, "void", inventory_definition)
|
||||
check_item.different = check_item.different + void_qty
|
||||
check_item.save
|
||||
end
|
||||
end
|
||||
else
|
||||
if item.qty.to_i > 0
|
||||
StockJournal.add_to_journal(item, stock.balance, "out of stock", inventory_definition)
|
||||
if !void_qty.nil? | void_qty != 0
|
||||
check_item.different = check_item.different - item.qty
|
||||
check_item.save
|
||||
else
|
||||
check_item.different = check_item.different + void_qty
|
||||
check_item.save
|
||||
end
|
||||
else
|
||||
if item.product_name.downcase.include?("void")
|
||||
StockJournal.add_to_journal(item, stock.balance, "void", inventory_definition)
|
||||
check_item.different = check_item.different + void_qty
|
||||
check_item.save
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if item.qty.to_i >= 0
|
||||
qty = item.qty - item.qty_was
|
||||
if !item.id_was.nil?
|
||||
remark = "edit"
|
||||
elsif stock.balance.to_i >= qty
|
||||
puts ">> stock is greater than order qty"
|
||||
remark = "ok"
|
||||
else
|
||||
puts " << stock is less than order qty"
|
||||
remark = "out of stock"
|
||||
end
|
||||
else # item.qty < 0
|
||||
qty = item.qty
|
||||
remark = "void"
|
||||
end
|
||||
if item.is_a? OrderItem
|
||||
trans_type = StockJournal::ORDER_TRANS
|
||||
elsif item.is_a? SaleItem
|
||||
trans_type = StockJournal::SALES_TRANS
|
||||
end
|
||||
StockJournal.add_to_journal(item.item_instance_code, qty, stock.balance, remark, inventory_definition, item.id, trans_type)
|
||||
check_item.different = check_item.different - qty
|
||||
check_item.save
|
||||
end
|
||||
|
||||
def self.search_by_category(cat_id)
|
||||
|
||||
Reference in New Issue
Block a user