Recentlry i have worked on one report, The main purpose of the report is , based on item, oraganizarion and subiventory it need to display the onhand qty for a gievn date. i used below code in formula column of report to calculate onhand qty on a given perticular date.
FUNCTION CF_LAST_BALFormula RETURN NUMBER IS
v_onhand_qty NUMBER;
v_last_qty NUMBER;
v_target_qty NUMBER;
BEGIN
SELECT SUM(transaction_quantity)
INTO v_onhand_qty
FROM mtl_onhand_quantities_detail
WHERE inventory_item_id = :inventory_item_id
AND organization_id = :organization_id
AND subinventory_code = :Subinventory;
SELECT NVL(SUM(Transaction_quantity),0)
INTO v_last_qty
FROM mtl_material_transactions
WHERE inventory_item_id = :inventory_item_id
AND organization_id = :organization_id
AND subinventory_code = :Subinventory
AND trunc(NVL(transaction_date,creation_date))>= TRUNC(TO_DATE(:p_last_date,'RRRR/MM/DD'));
v_target_qty:=(v_onhand_qty)-(v_last_qty);
RETURN(v_target_qty);
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;
FUNCTION CF_LAST_BALFormula RETURN NUMBER IS
v_onhand_qty NUMBER;
v_last_qty NUMBER;
v_target_qty NUMBER;
BEGIN
SELECT SUM(transaction_quantity)
INTO v_onhand_qty
FROM mtl_onhand_quantities_detail
WHERE inventory_item_id = :inventory_item_id
AND organization_id = :organization_id
AND subinventory_code = :Subinventory;
SELECT NVL(SUM(Transaction_quantity),0)
INTO v_last_qty
FROM mtl_material_transactions
WHERE inventory_item_id = :inventory_item_id
AND organization_id = :organization_id
AND subinventory_code = :Subinventory
AND trunc(NVL(transaction_date,creation_date))>= TRUNC(TO_DATE(:p_last_date,'RRRR/MM/DD'));
v_target_qty:=(v_onhand_qty)-(v_last_qty);
RETURN(v_target_qty);
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;