fixed inventory

This commit is contained in:
NyanLinHtut
2019-09-11 13:26:01 +06:30
parent 06349e23cc
commit 90d0a4ebb1
5 changed files with 57 additions and 31 deletions

View File

@@ -48,4 +48,3 @@ class Reports::StockCheckController < BaseReportController
end end
end end

View File

@@ -44,9 +44,12 @@ class InventoryDefinition < ApplicationRecord
end end
def self.modify_balance(item, stock, inventory_definition) #saleitemObj 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 if stock.balance.to_i >= item.qty
puts ">> stock is greater than order qty" puts ">> stock is greater than order qty"
StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition) StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition)
check_item.different = check_item.different - item.qty
check_item.save
else else
puts " << stock is less than order qty" puts " << stock is less than order qty"
data = item data = item
@@ -56,8 +59,12 @@ class InventoryDefinition < ApplicationRecord
StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition) StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition)
data.qty = extra_qty data.qty = extra_qty
StockJournal.add_to_journal(data, 0, "out of stock", inventory_definition) StockJournal.add_to_journal(data, 0, "out of stock", inventory_definition)
check_item.different = check_item.different - item.qty
check_item.save
else else
StockJournal.add_to_journal(item, stock.balance, "out of stock", inventory_definition) 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 end
end end
@@ -76,13 +83,20 @@ class InventoryDefinition < ApplicationRecord
end end
def self.get_by_category(filter) def self.get_by_category(filter)
least_stock = "SELECT (CASE WHEN stock_journals.remark != 'out of stock' # THEN (SELECT min(balance) FROM stock_journals
THEN (SELECT min(balance) FROM stock_journals # least_stock = "SELECT (CASE WHEN stock_journals.remark != 'out of stock'
WHERE item_code = inventory_definitions.item_code AND remark != 'out of stock' # THEN (SELECT balance FROM stock_journals
ORDER BY created_at DESC LIMIT 1) ELSE 0 END) # WHERE item_code = inventory_definitions.item_code AND remark != 'out of stock'
FROM stock_journals # ORDER BY created_at DESC LIMIT 1) ELSE 0 END)
WHERE stock_journals.item_code = inventory_definitions.item_code # FROM stock_journals
ORDER BY stock_journals.created_at DESC LIMIT 1" # 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.*, @inventory_definitions = InventoryDefinition.select("inventory_definitions.*,
(CASE WHEN sj.credit IS NULL THEN 0 ELSE sj.credit END) as credit, (CASE WHEN sj.credit IS NULL THEN 0 ELSE sj.credit END) as credit,

View File

@@ -2,7 +2,13 @@ class StockCheckItem < ApplicationRecord
belongs_to :stock_check belongs_to :stock_check
def create(stock_id, item) 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 ) remark, different = StockCheckItem.stock_different(item['qty'], balance )
self.stock_check_id = stock_id self.stock_check_id = stock_id
self.item_code = item['sku'] self.item_code = item['sku']
@@ -24,13 +30,14 @@ class StockCheckItem < ApplicationRecord
end end
def self.stock_different(stock_check_qty, journal_balance) def self.stock_different(stock_check_qty, journal_balance)
if stock_check_qty.to_i == journal_balance.to_i # if stock_check_qty.to_i == journal_balance.to_i
return 'match', stock_check_qty # return 'match', stock_check_qty
elsif stock_check_qty.to_i > journal_balance.to_i # elsif stock_check_qty.to_i > journal_balance.to_i
return 'missing order item', 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 # elsif stock_check_qty.to_i < journal_balance.to_i
return 'missing stock', stock_check_qty.to_i - journal_balance.to_i # return 'missing stock', stock_check_qty.to_i - journal_balance.to_i
end # end
return 'add new stock', stock_check_qty.to_i + journal_balance.to_i
end end
def self.get_transaction(from, to, item_code) def self.get_transaction(from, to, item_code)

View File

@@ -24,13 +24,19 @@ class StockJournal < ApplicationRecord
end end
def self.from_stock_check(item) 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) definition_id = InventoryDefinition.find_by_item_code(item.item_code)
journal = StockJournal.new journal = StockJournal.new
journal.item_code = item.item_code journal.item_code = item.item_code
journal.inventory_definition_id = definition_id.id journal.inventory_definition_id = definition_id.id
journal.debit = 0 journal.debit = 0
journal.credit = item.stock_count 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.remark = StockJournal::STOCK_CHECK_TRANS
journal.trans_ref = item.id journal.trans_ref = item.id
journal.trans_type = StockJournal::STOCK_CHECK_TRANS journal.trans_type = StockJournal::STOCK_CHECK_TRANS

View File

@@ -12,10 +12,10 @@
</tr> </tr>
<% cate_arr = Array.new %> <% cate_arr = Array.new %>
<% <%
count = 0 count = 0
@inventory_definitions.each do |item| @inventory_definitions.each do |item|
count += 1 count += 1
%> %>
<tr> <tr>