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