fixed inventory
This commit is contained in:
@@ -48,4 +48,3 @@ class Reports::StockCheckController < BaseReportController
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -44,9 +44,12 @@ class InventoryDefinition < ApplicationRecord
|
||||
end
|
||||
|
||||
def self.modify_balance(item, stock, inventory_definition) #saleitemObj
|
||||
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"
|
||||
StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition)
|
||||
check_item.different = check_item.different - item.qty
|
||||
check_item.save
|
||||
else
|
||||
puts " << stock is less than order qty"
|
||||
data = item
|
||||
@@ -56,8 +59,12 @@ class InventoryDefinition < ApplicationRecord
|
||||
StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition)
|
||||
data.qty = extra_qty
|
||||
StockJournal.add_to_journal(data, 0, "out of stock", inventory_definition)
|
||||
check_item.different = check_item.different - item.qty
|
||||
check_item.save
|
||||
else
|
||||
StockJournal.add_to_journal(item, stock.balance, "out of stock", inventory_definition)
|
||||
check_item.different = check_item.different - item.qty
|
||||
check_item.save
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -76,13 +83,20 @@ class InventoryDefinition < ApplicationRecord
|
||||
end
|
||||
|
||||
def self.get_by_category(filter)
|
||||
least_stock = "SELECT (CASE WHEN stock_journals.remark != 'out of stock'
|
||||
THEN (SELECT min(balance) FROM stock_journals
|
||||
WHERE item_code = inventory_definitions.item_code AND remark != 'out of stock'
|
||||
ORDER BY created_at DESC LIMIT 1) ELSE 0 END)
|
||||
FROM stock_journals
|
||||
WHERE stock_journals.item_code = inventory_definitions.item_code
|
||||
ORDER BY stock_journals.created_at DESC LIMIT 1"
|
||||
# THEN (SELECT min(balance) FROM stock_journals
|
||||
# least_stock = "SELECT (CASE WHEN stock_journals.remark != 'out of stock'
|
||||
# THEN (SELECT balance FROM stock_journals
|
||||
# WHERE item_code = inventory_definitions.item_code AND remark != 'out of stock'
|
||||
# ORDER BY created_at DESC LIMIT 1) ELSE 0 END)
|
||||
# FROM stock_journals
|
||||
# WHERE stock_journals.item_code = inventory_definitions.item_code
|
||||
# ORDER BY stock_journals.created_at DESC LIMIT 1"
|
||||
least_stock = "(SELECT
|
||||
(SELECT balance FROM stock_journals WHERE item_code = inventory_definitions.item_code
|
||||
ORDER BY created_at DESC LIMIT 1)
|
||||
FROM stock_journals
|
||||
WHERE stock_journals.item_code = inventory_definitions.item_code
|
||||
ORDER BY stock_journals.created_at DESC LIMIT 1)"
|
||||
|
||||
@inventory_definitions = InventoryDefinition.select("inventory_definitions.*,
|
||||
(CASE WHEN sj.credit IS NULL THEN 0 ELSE sj.credit END) as credit,
|
||||
|
||||
@@ -2,7 +2,13 @@ class StockCheckItem < ApplicationRecord
|
||||
belongs_to :stock_check
|
||||
|
||||
def create(stock_id, item)
|
||||
journal_id, balance = StockCheckItem.find_journal(item['sku'])
|
||||
check_item = StockCheckItem.find_by_item_code(item['sku'])
|
||||
if check_item.nil?
|
||||
balance = 0
|
||||
else
|
||||
balance = StockCheckItem.last.different
|
||||
end
|
||||
journal_id = StockCheckItem.find_journal(item['sku'])
|
||||
remark, different = StockCheckItem.stock_different(item['qty'], balance )
|
||||
self.stock_check_id = stock_id
|
||||
self.item_code = item['sku']
|
||||
@@ -24,13 +30,14 @@ class StockCheckItem < ApplicationRecord
|
||||
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
|
||||
# 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
|
||||
return 'add new stock', stock_check_qty.to_i + journal_balance.to_i
|
||||
end
|
||||
|
||||
def self.get_transaction(from, to, item_code)
|
||||
|
||||
@@ -24,13 +24,19 @@ class StockJournal < ApplicationRecord
|
||||
end
|
||||
|
||||
def self.from_stock_check(item)
|
||||
stock_journal = StockJournal.where('item_code=?', item.item_code).order("id DESC").first
|
||||
if stock_journal.nil?
|
||||
old_blance = 0
|
||||
else
|
||||
old_blance = stock_journal.balance
|
||||
end
|
||||
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.balance = item.stock_count + old_blance.to_i
|
||||
journal.remark = StockJournal::STOCK_CHECK_TRANS
|
||||
journal.trans_ref = item.id
|
||||
journal.trans_type = StockJournal::STOCK_CHECK_TRANS
|
||||
|
||||
@@ -12,10 +12,10 @@
|
||||
</tr>
|
||||
<% cate_arr = Array.new %>
|
||||
<%
|
||||
count = 0
|
||||
@inventory_definitions.each do |item|
|
||||
count = 0
|
||||
@inventory_definitions.each do |item|
|
||||
count += 1
|
||||
%>
|
||||
%>
|
||||
|
||||
<tr>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user