fixed inventory
This commit is contained in:
@@ -27,7 +27,7 @@ class Origami::SaleEditController < BaseOrigamiController
|
||||
saleitemObj.save
|
||||
@newsaleitem = SaleItem.new
|
||||
@newsaleitem = saleitemObj.dup
|
||||
@newsaleitem.save
|
||||
# @newsaleitem.save
|
||||
@newsaleitem.qty = saleitemObj.qty * -1
|
||||
@newsaleitem.price = saleitemObj.price * -1
|
||||
@newsaleitem.is_taxable = 1
|
||||
|
||||
@@ -8,6 +8,7 @@ class Booking < ApplicationRecord
|
||||
belongs_to :sale, :optional => true
|
||||
has_many :booking_orders
|
||||
has_many :orders, :through => :booking_orders
|
||||
has_many :order_items, :through => :orders
|
||||
scope :active, -> {where("booking_status != 'moved'")}
|
||||
scope :today, -> {where("created_at >= #{Time.now.utc}")}
|
||||
|
||||
|
||||
@@ -24,14 +24,8 @@ class InventoryDefinition < ApplicationRecord
|
||||
end
|
||||
|
||||
def self.find_product_in_inventory(item)
|
||||
product = InventoryDefinition.find_by_item_code(item.item_instance_code)
|
||||
if product.nil?
|
||||
return false, nil
|
||||
else
|
||||
stock_check_item = StockCheckItem.find_by_item_code(item.item_instance_code)
|
||||
if stock_check_item.nil?
|
||||
return false, nil
|
||||
else
|
||||
if product = InventoryDefinition.find_by_item_code(item.item_instance_code)
|
||||
if stock_check_item = StockCheckItem.find_by_item_code(item.item_instance_code)
|
||||
return true, product
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2974,34 +2974,28 @@ private
|
||||
end
|
||||
|
||||
def update_stock_journal
|
||||
# if self.sale_status == "void"
|
||||
# remark = "void"
|
||||
# elsif self.sale_status == "waste"
|
||||
# remark = "waste"
|
||||
# elsif self.sale_status == "spoile"
|
||||
# remark = "spoile"
|
||||
# elsif self.payment_status == "foc"
|
||||
# remark = "foc"
|
||||
# end
|
||||
self.sale_items.each do |item|
|
||||
if self.sale_status == "void" && self.sale_status_was != "void"
|
||||
if self.sale_status == "void" && self.sale_status_was != "void"
|
||||
self.sale_items.each do |item|
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item)
|
||||
if found
|
||||
stock = StockJournal.where('item_code=?', item.item_instance_code).order("id DESC").first
|
||||
unless stock.nil?
|
||||
check_item = StockCheckItem.where('item_code=?', item.item_instance_code).order("id DESC").first
|
||||
StockJournal.add_to_journal(item.item_instance_code, -item.qty, stock.balance, "void", inventory_definition, item.id, StockJournal::SALES_TRANS)
|
||||
check_item.different = check_item.different + item.qty
|
||||
check_item.save
|
||||
unless stock.nil?
|
||||
check_item = StockCheckItem.where('item_code=?', item.item_instance_code).order("id DESC").first
|
||||
StockJournal.add_to_journal(item.item_instance_code, -item.qty, stock.balance, "void", inventory_definition, item.id, StockJournal::SALES_TRANS)
|
||||
check_item.different = check_item.different + item.qty
|
||||
check_item.save
|
||||
end
|
||||
end
|
||||
end
|
||||
elsif self.sale_status == "waste" || self.sale_status == "spoile" || (self.payment_status == "foc" && self.payment_status_was != "foc")
|
||||
self.bookings.first.order_items.each do |item|
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item)
|
||||
if found
|
||||
if stock_journal = StockJournal.find_by_trans_ref(item.order_items_id)
|
||||
stock_journal.update(remark: self.sale_status)
|
||||
end
|
||||
end
|
||||
end
|
||||
elsif self.sale_status == "waste"
|
||||
StockJournal.update_stock_journal(item.item_instance_code, "waste")
|
||||
elsif self.sale_status == "spoile"
|
||||
StockJournal.update_stock_journal(item.item_instance_code, "spoile")
|
||||
elsif self.payment_status == "foc" && self.payment_status_was != "foc"
|
||||
StockJournal.update_stock_journal(item.item_instance_code, "foc")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -238,35 +238,43 @@ class SaleItem < ApplicationRecord
|
||||
is_edit = self.qty >= 0 && self.qty != self.qty_was
|
||||
is_foc = self.status == "foc" && self.status_was != "foc"
|
||||
cancel_foc = self.status_was == "foc"
|
||||
if is_foc
|
||||
StockJournal.update_stock_journal(self.item_instance_code, "foc")
|
||||
elsif cancel_foc
|
||||
StockJournal.update_stock_journal(self.item_instance_code, "cancel_foc")
|
||||
end
|
||||
if is_void or cancel_void or is_edit
|
||||
# if is_foc
|
||||
# StockJournal.update_stock_journal(self.item_instance_code, "foc")
|
||||
# else cancel_foc
|
||||
# StockJournal.update_stock_journal(self.item_instance_code, "cancel_foc")
|
||||
# end
|
||||
if is_void or cancel_void or is_edit or is_foc or cancel_foc
|
||||
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 is_void
|
||||
qty = -self.qty
|
||||
remark = "void"
|
||||
elsif cancel_void
|
||||
qty = self.qty
|
||||
remark = "cancel void"
|
||||
elsif is_edit
|
||||
qty = self.qty - self.qty_was
|
||||
remark = "edit"
|
||||
# elsif is_foc
|
||||
# logger.debug 'foc++++++++++++++++++++++++++++'
|
||||
# logger.debug is_foc
|
||||
# remark = "foc"
|
||||
# StockJournal.update_stock_journal(self.item_instance_code, remark)
|
||||
if 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_was
|
||||
remark = "edit"
|
||||
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 is_foc or cancel_foc
|
||||
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 stock_journal = StockJournal.find_by_trans_ref(order_item_id)
|
||||
if is_foc
|
||||
stock_journal.update(remark: "foc")
|
||||
elsif cancel_foc
|
||||
stock_journal.update(remark: "cancel_foc")
|
||||
end
|
||||
end
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -72,10 +72,13 @@ class StockJournal < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def self.update_stock_journal(item_code,remark)
|
||||
stock = self.where("item_code=?", item_code).order("id DESC").first
|
||||
stock.remark = remark
|
||||
stock.save
|
||||
def self.update_stock_journal(item_instance_code,remark)
|
||||
product = InventoryDefinition.find_by_item_code(item_instance_code)
|
||||
if !product.nil?
|
||||
stock = self.where("item_code=?", item_instance_code).order("id DESC").first
|
||||
stock.remark = remark
|
||||
stock.save
|
||||
end
|
||||
end
|
||||
|
||||
def self.delete_stock_journal(item_code)
|
||||
|
||||
Reference in New Issue
Block a user