change query for inventory

This commit is contained in:
phyusin
2018-08-16 17:40:49 +06:30
parent 5d6c0d5922
commit b91ad06f35
2 changed files with 14 additions and 5 deletions

View File

@@ -1,12 +1,21 @@
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 balance > 0
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 '-' ELSE sj.credit END) as credit,
(CASE WHEN sj.debit IS NULL THEN '-' ELSE sj.debit END) as debit,
(CASE WHEN sj.balance IS NULL THEN '-' ELSE sj.balance END) as balance")
(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")
.where("(CASE WHEN sj.balance > 0 THEN sj.balance=(SELECT MIN(balance) FROM stock_journals WHERE stock_journals.item_code = inventory_definitions.item_code ORDER BY created_at DESC) ELSE 1 END)")
.group("inventory_definitions.item_code")
.order("(CASE WHEN sj.balance > 0 THEN MIN(sj.balance) ELSE NULL END )")

View File

@@ -82,7 +82,7 @@
<% end %>
</td>
<td><%= result.stock_count rescue '-' %></td>
<td><%= number_with_precision(result.stock_balance, precision:precision.to_i,delimiter:delimiter) rescue '-' %></td>
<td><%= result.stock_balance rescue '-' %></td>
<td><%= result.different rescue '-' %></td>
<td><%= result.remark rescue '-' %></td>
<td><%= Employee.find(result.stock_check.check_by).name rescue '-' %></td>