inventory for set items
This commit is contained in:
@@ -163,7 +163,7 @@ class Origami::SaleEditController < BaseOrigamiController
|
|||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
sale.compute_by_sale_items(saleObj.total_discount, nil, order_source)
|
# sale.compute_by_sale_items(saleObj.total_discount, nil, order_source) FOR WHAT????
|
||||||
|
|
||||||
ProductCommission.edit_product_commission(saleitemObj)
|
ProductCommission.edit_product_commission(saleitemObj)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -24,20 +24,6 @@ class InventoryDefinition < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.find_product_in_inventory(item)
|
def self.find_product_in_inventory(item)
|
||||||
# unless instance_code.nil?
|
|
||||||
# instance_code = instance_code.to_s
|
|
||||||
# instance_code[0] = ""
|
|
||||||
# instance_code = instance_code.chomp("]")
|
|
||||||
# else
|
|
||||||
# instance_code = '"0"'
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# if prod = InventoryDefinition.where("item_code IN(#{instance_code})")
|
|
||||||
# if stock_check_item = StockCheckItem.where("item_code IN(#{instance_code})")
|
|
||||||
# return true, prod
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
|
|
||||||
if product = InventoryDefinition.find_by_item_code(item.item_instance_code)
|
if product = InventoryDefinition.find_by_item_code(item.item_instance_code)
|
||||||
if stock_check_item = StockCheckItem.find_by_item_code(item.item_instance_code)
|
if stock_check_item = StockCheckItem.find_by_item_code(item.item_instance_code)
|
||||||
return true, product
|
return true, product
|
||||||
@@ -46,13 +32,12 @@ 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
|
stock = StockJournal.where('item_code=?', item.item_instance_code).order("id DESC").first
|
||||||
unless stock.nil?
|
unless stock.nil?
|
||||||
modify_balance(item, stock, inventory_definition)
|
modify_balance(item, stock, inventory_definition)
|
||||||
else
|
else
|
||||||
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)
|
||||||
StockJournal.add_to_journal(item.item_instance_code, item.qty, 0, "out of stock", inventory_definition, item.id, StockJournal::SALES_TRANS)
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.modify_balance(item, stock, inventory_definition) #saleitemObj
|
def self.modify_balance(item, stock, inventory_definition) #saleitemObj
|
||||||
@@ -77,6 +62,7 @@ class InventoryDefinition < ApplicationRecord
|
|||||||
elsif item.is_a? SaleItem
|
elsif item.is_a? SaleItem
|
||||||
trans_type = StockJournal::SALES_TRANS
|
trans_type = StockJournal::SALES_TRANS
|
||||||
end
|
end
|
||||||
|
# StockJournal.add_to_journal(instance_code, qty, stock.balance, remark, inventory_definition, item.id, trans_type)
|
||||||
StockJournal.add_to_journal(item.item_instance_code, qty, stock.balance, remark, inventory_definition, item.id, trans_type)
|
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.different = check_item.different - qty
|
||||||
check_item.save
|
check_item.save
|
||||||
|
|||||||
@@ -125,41 +125,13 @@ class OrderItem < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update_stock_journal
|
def update_stock_journal
|
||||||
# if self.set_menu_items.present?
|
unless MenuItemInstance.where("item_instance_name <> ''").pluck(:item_instance_code).include?(self.item_instance_code)
|
||||||
# items = JSON.parse(self.set_menu_items)
|
if self.qty != self.qty_before_last_save
|
||||||
# instance_code = Array.new
|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||||
# count = 0
|
if found
|
||||||
# items.each { |i|
|
InventoryDefinition.check_balance(self, inventory_definition)
|
||||||
# instance_code.push(i["item_instance_code"])
|
end
|
||||||
# }
|
|
||||||
# end
|
|
||||||
|
|
||||||
if self.qty != self.qty_before_last_save
|
|
||||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
|
||||||
if found
|
|
||||||
InventoryDefinition.check_balance(self, inventory_definition)
|
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2468,7 +2468,11 @@ private
|
|||||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item)
|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item)
|
||||||
if found
|
if found
|
||||||
if stock_journal = StockJournal.find_by_trans_ref(item.order_items_id)
|
if stock_journal = StockJournal.find_by_trans_ref(item.order_items_id)
|
||||||
stock_journal.update(remark: self.sale_status)
|
if self.payment_status == "foc" && self.payment_status_was != "foc"
|
||||||
|
stock_journal.update(remark: self.payment_status)
|
||||||
|
else
|
||||||
|
stock_journal.update(remark: self.sale_status)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ class SaleItem < ApplicationRecord
|
|||||||
|
|
||||||
before_validation :round_to_precision
|
before_validation :round_to_precision
|
||||||
after_update :update_stock_journal
|
after_update :update_stock_journal
|
||||||
|
after_save :update_stock_journal_set_item
|
||||||
|
|
||||||
# Add Sale Items
|
# Add Sale Items
|
||||||
def self.add_sale_items(sale_items)
|
def self.add_sale_items(sale_items)
|
||||||
@@ -304,40 +305,42 @@ class SaleItem < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update_stock_journal
|
def update_stock_journal
|
||||||
is_void = self.status == "void" && self.status_before_last_save != "void"
|
unless MenuItemInstance.where("item_instance_name <> ''").pluck(:item_instance_code).include?(self.item_instance_code)
|
||||||
cancel_void = self.status_before_last_save == "void" && self.status.nil?
|
is_void = self.status == "void" && self.status_before_last_save != "void"
|
||||||
is_edit = self.qty >= 0 && self.qty != self.qty_before_last_save
|
cancel_void = self.status_before_last_save == "void" && self.status.nil?
|
||||||
is_foc = self.status == "foc" && self.status_before_last_save != "foc"
|
is_edit = self.qty >= 0 && self.qty != self.qty_before_last_save
|
||||||
cancel_foc = self.status_before_last_save == "foc"
|
is_foc = self.status == "foc" && self.status_before_last_save != "foc"
|
||||||
|
cancel_foc = self.status_before_last_save == "foc"
|
||||||
|
|
||||||
if is_void or cancel_void or is_edit or is_foc or cancel_foc
|
if is_void or cancel_void or is_edit or is_foc or cancel_foc
|
||||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||||
if found
|
if found
|
||||||
stock = StockJournal.where('item_code=?', self.item_instance_code).order("id DESC").first
|
stock = StockJournal.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||||
unless stock.nil?
|
unless stock.nil?
|
||||||
check_item = StockCheckItem.where('item_code=?', self.item_instance_code).order("id DESC").first
|
check_item = StockCheckItem.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||||
if is_void or cancel_void or is_edit
|
if is_void or cancel_void or is_edit
|
||||||
if is_void
|
if is_void
|
||||||
qty = -self.qty
|
qty = -self.qty
|
||||||
remark = "void"
|
remark = "void"
|
||||||
elsif cancel_void
|
elsif cancel_void
|
||||||
qty = self.qty
|
qty = self.qty
|
||||||
remark = "cancel void"
|
remark = "cancel void"
|
||||||
elsif is_edit
|
elsif is_edit
|
||||||
qty = self.qty - self.qty_before_last_save
|
qty = self.qty - self.qty_before_last_save
|
||||||
remark = "edit"
|
remark = "edit"
|
||||||
end
|
end
|
||||||
StockJournal.add_to_journal(self.item_instance_code, qty, stock.balance, remark, inventory_definition, self.id, StockJournal::SALES_TRANS)
|
StockJournal.add_to_journal(self.item_instance_code, qty, stock.balance, remark, inventory_definition, self.id, StockJournal::SALES_TRANS)
|
||||||
check_item.different = check_item.different + qty
|
check_item.different = check_item.different + qty
|
||||||
check_item.save
|
check_item.save
|
||||||
else is_foc or cancel_foc
|
else is_foc or cancel_foc
|
||||||
qty = StockJournal.where(trans_ref: self.sale_item_id).sum("credit-debit")
|
qty = StockJournal.where(trans_ref: self.sale_item_id).sum("credit-debit")
|
||||||
if order_item_id = self.sale.bookings.first.order_items.where(item_instance_code: self.item_instance_code, qty: self.qty + qty).select(:order_items_id).first.order_items_id
|
if order_item_id = self.sale.bookings.first.order_items.where(item_instance_code: self.item_instance_code, qty: self.qty + qty).select(:order_items_id).first.order_items_id
|
||||||
if stock_journal = StockJournal.find_by_trans_ref(order_item_id)
|
if stock_journal = StockJournal.find_by_trans_ref(order_item_id)
|
||||||
if is_foc
|
if is_foc
|
||||||
stock_journal.update(remark: "foc")
|
stock_journal.update(remark: "foc")
|
||||||
elsif cancel_foc
|
elsif cancel_foc
|
||||||
stock_journal.update(remark: "cancel_foc")
|
stock_journal.update(remark: "cancel_foc")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -346,4 +349,78 @@ class SaleItem < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_stock_journal_set_item
|
||||||
|
is_void = self.status == "void" && self.status_before_last_save != "void" && self.qty > 0
|
||||||
|
cancel_void = self.status_before_last_save == "void" && self.status.nil?
|
||||||
|
is_edit = self.qty >= 0 && self.qty != self.qty_before_last_save
|
||||||
|
is_foc = self.status == "foc" && self.status_before_last_save != "foc"
|
||||||
|
cancel_foc = self.status_before_last_save == "foc"
|
||||||
|
is_waste = self.status == "waste"
|
||||||
|
is_spoile = self.status == "spoile"
|
||||||
|
|
||||||
|
if MenuItemInstance.where("item_instance_name <> ''").pluck(:item_instance_code).include?(self.item_instance_code)
|
||||||
|
if self.qty == 1 && self.qty != self.qty_before_last_save
|
||||||
|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||||
|
if found
|
||||||
|
stock = StockJournal.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||||
|
unless stock.nil?
|
||||||
|
check_item = StockCheckItem.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||||
|
if self.qty.to_i >= 0
|
||||||
|
qty = self.qty - self.qty_was
|
||||||
|
if 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
|
||||||
|
end
|
||||||
|
StockJournal.add_to_journal(self.item_instance_code, qty, stock.balance, remark, inventory_definition, self.id, StockJournal::SALES_TRANS)
|
||||||
|
check_item.different = check_item.different - qty
|
||||||
|
check_item.save
|
||||||
|
else
|
||||||
|
StockJournal.add_to_journal(self.item_instance_code, self.qty, 0, "out of stock", inventory_definition, self.id, StockJournal::SALES_TRANS)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elsif is_void or cancel_void or is_edit
|
||||||
|
if is_void
|
||||||
|
qty = -self.qty
|
||||||
|
remark = "void"
|
||||||
|
elsif cancel_void
|
||||||
|
qty = self.qty
|
||||||
|
remark = "cancel void"
|
||||||
|
elsif is_edit
|
||||||
|
qty = self.qty - self.qty_before_last_save
|
||||||
|
remark = "edit"
|
||||||
|
end
|
||||||
|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||||
|
if found
|
||||||
|
stock = StockJournal.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||||
|
unless stock.nil?
|
||||||
|
check_item = StockCheckItem.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||||
|
end
|
||||||
|
end
|
||||||
|
StockJournal.add_to_journal(self.item_instance_code, qty, stock.balance, remark, inventory_definition, self.id, StockJournal::SALES_TRANS)
|
||||||
|
check_item.different = check_item.different + qty
|
||||||
|
check_item.save
|
||||||
|
elsif is_foc or cancel_foc
|
||||||
|
qty = StockJournal.where(trans_ref: self.sale_item_id).sum("credit-debit")
|
||||||
|
if stock_journal = StockJournal.where(trans_ref: self.sale_item_id, item_code: self.item_instance_code).order(id: :desc).first
|
||||||
|
if is_foc
|
||||||
|
stock_journal.update(remark: "foc")
|
||||||
|
elsif cancel_foc
|
||||||
|
stock_journal.update(remark: "cancel_foc")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elsif is_waste or is_spoile
|
||||||
|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||||
|
if found
|
||||||
|
if stock_journal = StockJournal.where(trans_ref: self.sale_item_id, item_code: self.item_instance_code)
|
||||||
|
stock_journal.update(remark: self.status)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user