/**
    * @author Ivan Lucas
    * @param int $contractid. Contract ID of the contract to show a balance for
    * @return int. Number of available units according to the service balances and unit rates
    * @todo Check this is correct
**/
function contract_balance($contractid, $includenonapproved = FALSE, $includereserved = TRUE, $showonlycurrentlyvalid = TRUE)
{
    global $now, $dbService;
    $unitbalance = 0;
    $sql = "SELECT * FROM `{$dbService}` WHERE contractid = {$contractid} ";
    if ($showonlycurrentlyvalid) {
        $sql .= "AND UNIX_TIMESTAMP(startdate) <= {$now} ";
        $sql .= "AND UNIX_TIMESTAMP(enddate) >= {$now}  ";
    }
    $sql .= "ORDER BY enddate DESC";
    $result = mysql_query($sql);
    if (mysql_error()) {
        trigger_error("MySQL Query Error " . mysql_error(), E_USER_WARNING);
    }
    if (mysql_num_rows($result) > 0) {
        while ($service = mysql_fetch_object($result)) {
            $balance += round($service->balance);
        }
    }
    if ($includenonapproved) {
        $awaiting = contract_transaction_total($contractid, BILLING_AWAITINGAPPROVAL);
        if ($awaiting != 0) {
            $balance += round($awaiting);
        }
    }
    if ($includereserved) {
        $reserved = contract_transaction_total($contractid, BILLING_RESERVED);
        if ($reserved != 0) {
            $balance += round($reserved);
        }
    }
    return $balance;
}
 echo "<tr><th>{$strWebsite}:</th><td>";
 if (!empty($siterow['websiteurl'])) {
     echo "<a href=\"{$siterow['websiteurl']}\">{$siterow['websiteurl']}</a>";
 }
 echo "</td></tr>";
 echo "<tr><th>{$strNotes}:</th><td>" . nl2br($siterow['notes']) . "</td></tr>";
 echo "<tr><td colspan='2'>&nbsp;</td></tr>";
 echo "<tr><th>{$strIncidents}:</th>";
 echo "<td>" . site_count_incidents($id) . " <a href=\"contact_support.php?id=" . $siterow['id'] . "&amp;mode=site\">{$strSeeHere}</a></td></tr>";
 echo "<tr><th>{$strBillableIncidents}:</th><td><a href='transactions.php?site={$siterow['id']}'>{$strSeeHere}</a></td></tr>";
 $balance = $awaiting = $reserved = 0;
 $billable_contract = get_site_billable_contract_id($id);
 if ($billable_contract != -1) {
     $balance = contract_balance($billable_contract, TRUE, TRUE, TRUE);
     $awaiting = contract_transaction_total($billable_contract, BILLING_AWAITINGAPPROVAL);
     $reserved = contract_transaction_total($billable_contract, BILLING_RESERVED);
 }
 echo "<tr><th>{$strServiceBalance}</th><td>";
 echo "{$GLOBALS['strBalance']}: {$CONFIG['currency_symbol']}" . number_format($balance, 2);
 if ($awaiting > 0) {
     echo "<br />{$GLOBALS['strAwaitingApproval']}: {$CONFIG['currency_symbol']}" . number_format($awaiting, 2);
 }
 if ($reserved > 0) {
     echo "<br />{$GLOBALS['strReserved']}: {$CONFIG['currency_symbol']}" . number_format($reserved, 2);
 }
 echo "</td></tr>";
 echo "<tr><th>{$strActivities}:</th><td>" . open_activities_for_site($siterow['id']) . " <a href='tasks.php?siteid={$siterow['id']}'>{$strSeeHere}</a></td></tr>";
 echo "<tr><th>{$strInventory}:</th>";
 echo "<td>" . site_count_inventory_items($id);
 echo " <a href='inventory.php?site={$id}'>{$strSeeHere}</a></td></tr>";
 $billableunits = billable_units_site($siterow['id'], $now - 2678400);