fixed inventory
This commit is contained in:
@@ -13,6 +13,15 @@ class OrderItem < ApplicationRecord
|
||||
# validates :qty, numericality: { :greater_than => 0 }
|
||||
validates_associated :order
|
||||
|
||||
after_save :update_stock_journal
|
||||
|
||||
# after_update :update_stock_journal
|
||||
|
||||
# after_save :add_to_journal, if: Proc.new { !self.id_changed? }
|
||||
# after_save :update_stock_journal, if: Proc.new { self.id_changed? }
|
||||
|
||||
|
||||
|
||||
#This Method - handle how items is added into order
|
||||
# order_item : {
|
||||
# order_item_code : "",
|
||||
@@ -101,4 +110,49 @@ class OrderItem < ApplicationRecord
|
||||
self.order_items_id = SeedGenerator.generate_id(self.class.name, "ODI")
|
||||
end
|
||||
end
|
||||
|
||||
def add_to_journal
|
||||
defination = InventoryDefinition.find_by_item_code(self.item_instance_code)
|
||||
stock = StockJournal.where('item_code = ?', self.item_instance_code).order("id DESC").first
|
||||
journal = StockJournal.create(
|
||||
item_code: self.item_instance_code,
|
||||
credit: 0,
|
||||
debit: self.qty,
|
||||
balance: stock.balance - self.qty,
|
||||
inventory_definition_id: defination.id,
|
||||
remark: 'ok',
|
||||
trans_ref: self.order.id,
|
||||
trans_type: StockJournal::SALES_TRANS
|
||||
)
|
||||
end
|
||||
|
||||
def update_stock_journal
|
||||
if self.qty != self.qty_was
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||
if found
|
||||
InventoryDefinition.check_balance(self, inventory_definition)
|
||||
end
|
||||
end
|
||||
# if self.qty > self.qty_was
|
||||
# credit = 0
|
||||
# debit = self.qty.to_i - self.qty_was.to_i
|
||||
# else
|
||||
# credit = self.qty_was.to_i - self.qty.to_i
|
||||
# debit = 0
|
||||
# end
|
||||
# if credit != debit
|
||||
# defination = InventoryDefinition.find_by_item_code(self.item_instance_code)
|
||||
# stock = StockJournal.where('item_code = ?', self.item_instance_code).order("id DESC").first
|
||||
# journal = StockJournal.create(
|
||||
# item_code: self.item_instance_code,
|
||||
# credit: credit,
|
||||
# debit: debit,
|
||||
# balance: stock.balance - debit + credit,
|
||||
# inventory_definition_id: defination.id,
|
||||
# remark: 'ok',
|
||||
# trans_ref: self.order.id,
|
||||
# trans_type: StockJournal::SALES_TRANS
|
||||
# )
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user