function IssueRate($issueQuantity, $itemId)
{
    global $totalOpeningQuantity;
    global $totalOpeningRate;
    global $issueRate;
    global $grnQtyleftCheck;
    global $grntRateleft;
    global $start;
    $leftQuanity = number_format($issueQuantity, 2, '.', '');
    if ($totalOpeningQuantity > 0) {
        if ($totalOpeningQuantity > $issueQuantity) {
            $issueRate = $totalOpeningRate * $issueQuantity;
            $totalOpeningQuantity = $totalOpeningQuantity - $issueQuantity;
            $leftQuanity = $leftQuanity - $issueQuantity;
        } else {
            $leftQuanity = $leftQuanity - $totalOpeningQuantity;
            $issueRate = $totalOpeningRate * $totalOpeningQuantity;
            $totalOpeningQuantity = 0;
        }
    } else {
        if ($grnQtyleftCheck > 0) {
            if ($grnQtyleftCheck < $leftQuanity) {
                $leftQuanity = $leftQuanity - $grnQtyleftCheck;
                $issueRate = $grntRateleft * $grnQtyleftCheck;
                $grnQtyleftCheck = 0;
            } else {
                $issueRate = $grntRateleft * $leftQuanity;
                $grnQtyleftCheck = $grnQtyleftCheck - $leftQuanity;
                $leftQuanity = 0;
            }
        } else {
            $sql_func = "select ms_GRN_master.GRN_id,ms_GRN_master.GRN_date,ms_GRN_transaction.rec_qty,ms_GRN_transaction.net_rate from ms_GRN_master,ms_GRN_transaction,ms_item_master where ms_GRN_master.GRN_id=ms_GRN_transaction.GRN_id and ms_GRN_transaction.item_id=ms_item_master.item_id and ms_GRN_transaction.item_id='" . $itemId . "' order by ms_GRN_transaction.GRN_transaction_id ASC limit {$start},1 ";
            $result_func = mysql_query($sql_func) or die("Error in : " . $sql_func . "<br>" . mysql_errno() . " : " . mysql_error());
            $row_func = mysql_fetch_array($result_func);
            $grnQtyleft = $row_func['rec_qty'];
            $grntRateleft = $row_func['net_rate'];
            $grnQtyleftCheck = $row_func['rec_qty'];
            if ($leftQuanity < $grnQtyleft) {
                $issueRate = $issueRate + $grntRateleft * $leftQuanity;
                $grnQtyleftCheck = $grnQtyleftCheck - $leftQuanity;
            } else {
                $issueRate = $issueRate + $grntRateleft * $grnQtyleft;
                $grnQtyleftCheck = 0;
            }
            $leftQuanity = $leftQuanity - $grnQtyleft;
            $start++;
        }
    }
    if ($leftQuanity > 0 and $leftQuanity > $issueQuantity) {
        IssueRate($leftQuanity, $itemId);
    }
    //echo $issueRate.' : '.$leftQuanity;
    return $issueRate;
}
function IssueRate($issueQuantity, $itemId)
{
    global $totalOpeningQuantity;
    global $totalOpeningRate;
    global $issueRate;
    global $grnQtyleftCheck;
    global $grntRateleft;
    global ${$itemId};
    //echo $itemId. " : ".$$itemId;
    //echo "<br>";
    $leftQuanity = $issueQuantity;
    //echo  $totalOpeningQuantity. ' : '.$totalOpeningRate. ' : '.$issueRate. ' : '.$grnQtyleftCheck. ' : '.$grntRateleft.' : '.$leftQuanity;
    //echo '<br>';
    if ($totalOpeningQuantity > 0) {
        if ($totalOpeningQuantity > $issueQuantity) {
            $issueRate = $totalOpeningRate * $issueQuantity;
            $totalOpeningQuantity = $totalOpeningQuantity - $issueQuantity;
            $leftQuanity = $leftQuanity - $issueQuantity;
        } else {
            $leftQuanity = $leftQuanity - $totalOpeningQuantity;
            $issueRate = $totalOpeningRate * $totalOpeningQuantity;
            $totalOpeningQuantity = 0;
        }
    } else {
        if ($grnQtyleftCheck > 0) {
            if ($grnQtyleftCheck < $leftQuanity) {
                $leftQuanity = $leftQuanity - $grnQtyleftCheck;
                $issueRate = $grntRateleft * $grnQtyleftCheck;
                $grnQtyleftCheck = 0;
            } else {
                $issueRate = $grntRateleft * $leftQuanity;
                $grnQtyleftCheck = $grnQtyleftCheck - $leftQuanity;
                $leftQuanity = 0;
            }
        } else {
            $sql_func = "select ms_GRN_master.GRN_id,ms_GRN_master.GRN_date,ms_GRN_transaction.rec_qty,ms_GRN_transaction.net_rate from ms_GRN_master,ms_GRN_transaction,ms_item_master where ms_GRN_master.GRN_id=ms_GRN_transaction.GRN_id and ms_GRN_transaction.item_id=ms_item_master.item_id and ms_GRN_transaction.item_id='" . $itemId . "' order by ms_GRN_transaction.GRN_transaction_id ASC limit " . ${$itemId} . ",1";
            $result_func = mysql_query($sql_func) or die("Error in : " . $sql_func . "<br>" . mysql_errno() . " : " . mysql_error());
            if (mysql_num_rows($result_func) > 0) {
                $row_func = mysql_fetch_array($result_func);
                $grnQtyleft = $row_func['rec_qty'];
                $grntRateleft = $row_func['net_rate'];
                $grnQtyleftCheck = $row_func['rec_qty'];
                if (round($leftQuanity, 2) <= round($grnQtyleft, 2)) {
                    $issueRate = $issueRate + $grntRateleft * $leftQuanity;
                    $grnQtyleftCheck = $grnQtyleftCheck - $leftQuanity;
                    $leftQuanity = 0;
                } else {
                    $issueRate = $issueRate + $grntRateleft * $grnQtyleft;
                    $grnQtyleftCheck = 0;
                    $leftQuanity = $leftQuanity - $grnQtyleft;
                }
                ${$itemId}++;
            } else {
                return 0;
            }
        }
    }
    if ($leftQuanity > 0) {
        IssueRate($leftQuanity, $itemId);
    }
    //echo $issueRate.' : '.$leftQuanity;
    return $issueRate;
}