/**
* Function to approve an incident, this adds a transaction and confirms the 'bill' is correct.
* @author Paul Heaney
* @param incidentid ID of the incident to approve
*/
function approve_incident_transaction($transactionid)
{
    global $dbLinks, $sit, $CONFIG, $strUnits;
    $rtnvalue = TRUE;
    // Check transaction exists, and is awaiting approval and is an incident
    $sql = "SELECT l.linkcolref, t.serviceid FROM `{$GLOBALS['dbLinks']}` AS l, `{$GLOBALS['dbTransactions']}` AS t ";
    $sql .= "WHERE t.transactionid = l.origcolref AND t.transactionstatus = " . BILLING_AWAITINGAPPROVAL . " AND l.linktype = 6 AND t.transactionid = {$transactionid}";
    $result = mysql_query($sql);
    if (mysql_error()) {
        trigger_error("Error identify incident transaction. " . mysql_error(), E_USER_WARNING);
    }
    if (mysql_num_rows($result) > 0) {
        list($incidentid, $serviceid) = mysql_fetch_row($result);
        $bills = get_incident_billable_breakdown_array($incidentid);
        $multipliers = get_all_available_multipliers();
        $totalunits = 0;
        $totalbillableunits = 0;
        $totalrefunds = 0;
        foreach ($bills as $bill) {
            foreach ($multipliers as $m) {
                $a[$m] += $bill[$m]['count'];
            }
        }
        foreach ($multipliers as $m) {
            $s .= sprintf($GLOBALS['strXUnitsAtX'], $a[$m], $m);
            $totalbillableunits += $m * $a[$m];
            $totalunits += $a[$m];
        }
        $unitrate = get_unit_rate(incident_maintid($incidentid));
        $totalrefunds += $bills['refunds'];
        $cost = ($totalbillableunits += $totalrefunds) * $unitrate * -1;
        $desc = trim("Incident {$incidentid}: {$totalbillableunits}: {$strUnits} @ {$CONFIG['currency_symbol']}{$unitrate}. {$s}");
        //FIXME i18n
        $rtn = update_contract_balance(incident_maintid($incidentid), $desc, $cost, $serviceid, $transactionid, $totalunits, $totalbillableunits, $totalrefunds);
        if ($rtn == FALSE) {
            $rtnvalue = FALSE;
        }
    } else {
        $rtnvalue = FALSE;
    }
    return $rtnvalue;
}
 $bills = get_incident_billable_breakdown_array($incidentid);
 $multipliers = get_all_available_multipliers();
 $totalunits = 0;
 $totalbillableunits = 0;
 $totalrefunds = 0;
 foreach ($bills as $bill) {
     foreach ($multipliers as $m) {
         $a[$m] += $bill[$m]['count'];
     }
 }
 foreach ($multipliers as $m) {
     $s .= sprintf($GLOBALS['strXUnitsAtX'], $a[$m], $m);
     $totalunits += $a[$m];
     $totalbillableunits += $m * $a[$m];
 }
 $unitrate = get_unit_rate(incident_maintid($incidentid));
 $totalrefunds = $bills['refunds'];
 // $numberofunits += $bills['refunds'];
 $cost = ($totalbillableunits + $totalrefunds) * $unitrate * -1;
 $desc = trim("{$numberofunits} {$strUnits} @ {$CONFIG['currency_symbol']}{$unitrate} {$strForIncident} {$incidentid}. {$s}");
 $transactionid = get_incident_transactionid($incidentid);
 if ($transactionid != FALSE) {
     $r = update_transaction($transactionid, $cost, $desc, BILLING_AWAITINGAPPROVAL);
     if ($r) {
         html_redirect('billable_incidents.php', TRUE, $strUpdateSuccessful);
     } else {
         html_redirect('billable_incidents.php', FALSE, $strUpdateFailed);
     }
 } else {
     html_redirect('billable_incidents.php', FALSE, $strUpdateFailed);
 }