41 lines
1.7 KiB
Ruby
Executable File
41 lines
1.7 KiB
Ruby
Executable File
class Inventory::InventoryController < BaseInventoryController
|
|
load_and_authorize_resource
|
|
def index
|
|
least_stock = "SELECT (CASE WHEN SIGN(MIN(stock_journals.balance)) > 0
|
|
THEN MIN(stock_journals.balance) WHEN stock_journals.remark NOT LIKE '%out of stock%'
|
|
THEN (SELECT balance FROM stock_journals
|
|
WHERE item_code = inventory_definitions.item_code
|
|
AND remark NOT LIKE '%out of stock%'
|
|
ORDER BY created_at DESC LIMIT 1) ELSE 0 END)
|
|
FROM stock_journals
|
|
WHERE stock_journals.item_code = inventory_definitions.item_code
|
|
ORDER BY stock_journals.created_at DESC"
|
|
|
|
@inventory_definitions = InventoryDefinition.select("inventory_definitions.*,
|
|
(CASE WHEN sj.credit IS NULL THEN 0 ELSE sj.credit END) as credit,
|
|
(CASE WHEN sj.debit IS NULL THEN 0 ELSE sj.debit END) as debit,
|
|
(#{least_stock}) as balance")
|
|
.joins(" LEFT JOIN stock_journals sj ON sj.inventory_definition_id=inventory_definitions.id")
|
|
.group("inventory_definitions.item_code")
|
|
.order("(CASE WHEN sj.balance > 0 THEN MIN(sj.balance) ELSE NULL END )")
|
|
|
|
end
|
|
|
|
def show
|
|
inventory_definition_id = params[:inventory_definition_id]
|
|
inventory = InventoryDefinition.find(inventory_definition_id)
|
|
|
|
@stock_journals = StockJournal.where(item_code: inventory.item_code)
|
|
respond_to do |format|
|
|
format.html
|
|
format.xls
|
|
end
|
|
end
|
|
|
|
#Shop Name in Navbor
|
|
helper_method :shop_detail
|
|
def shop_detail
|
|
@shop = Shop.first
|
|
end
|
|
end
|