40 lines
1.2 KiB
Ruby
40 lines
1.2 KiB
Ruby
class StockJournal < ApplicationRecord
|
|
|
|
SALES_TRANS = "sale"
|
|
STOCK_CHECK_TRANS = "stock_check"
|
|
|
|
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.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
|