echo "<table><tr><td>ID</td><td>Date</td><td>Project</td><td>Description</td><td>Category</td><td>User</td><td>Date Verified</td><td>Invoice (ID) / Personal</td><td>Net.</td><td>VAT</td><td>Gross</td></tr>";
$expense_invoice_net = 0;
$expense_noinvoice_net = 0;
$expense_invoice_vat = 0;
$expense_noinvoice_vat = 0;
$expense_invoice_gross = 0;
$expense_noinvoice_gross = 0;
// Get the relevant infomation from the Invoice Database
$sql_expense = "SELECT * FROM intranet_timesheet_expense, intranet_projects, intranet_user_details LEFT JOIN intranet_timesheet_expense_category ON ts_expense_category = expense_cat_id WHERE ts_expense_user = user_id AND ts_expense_project = proj_id AND ts_expense_date BETWEEN {$date_begin} AND {$date_end} ORDER BY {$expense_sortorder}";
$result_expense = mysql_query($sql_expense, $conn) or die(mysql_error());
while ($array_expense = mysql_fetch_array($result_expense)) {
    $ts_expense_id = $array_expense['ts_expense_id'];
    $ts_expense_value = round($array_expense['ts_expense_value'], 2);
    $expense_cat_name = $array_expense['expense_cat_name'];
    $ts_expense_date = TimeFormatBrief($array_expense['ts_expense_date']);
    $ts_expense_desc = RemoveShit($array_expense['ts_expense_desc']);
    $ts_expense_desc = html_entity_decode($ts_expense_desc);
    $ts_expense_desc = str_replace("\n\n", "\n", $ts_expense_desc);
    $ts_expense_p11d = $array_expense['ts_expense_p11d'];
    $user_name_first = $array_expense['user_name_first'];
    $user_name_second = $array_expense['user_name_second'];
    $user_name = $user_name_first . "&nbsp;" . $user_name_second;
    $invoice_ref = $array_expense['invoice_ref'];
    $ts_expense_diff = $array_expense['ts_expense_vat'] - $array_expense['ts_expense_value'];
    $ts_expense_diff_print = round($ts_expense_diff, 2);
    $ts_expense_invoiced = $array_expense['ts_expense_invoiced'];
    if ($ts_expense_invoiced > 0) {
        $sql_invoice = "SELECT invoice_ref, invoice_id FROM intranet_timesheet_invoice WHERE invoice_id = {$ts_expense_invoiced} LIMIT 1";
        $result_invoice = mysql_query($sql_invoice, $conn) or die(mysql_error());
        $array_invoice = mysql_fetch_array($result_invoice);
        $invoice_ref = $array_invoice['invoice_ref'];
 $ts_expense_vat = $array_expense['ts_expense_vat'];
 $ts_expense_verified = $array_expense['ts_expense_verified'];
 $ts_expense_reimburse = $array_expense['ts_expense_reimburse'];
 $ts_expense_invoice = $array_expense['ts_expense_invoice'];
 $ts_expense_p11d = $array_expense['ts_expense_p11d'];
 $proj_fee_track = $array_expense['proj_fee_track'];
 $proj_num = $array_expense['proj_num'];
 $expense_cat_name = $array_expense['expense_cat_name'];
 //check for duplicates
 if ($ts_expense_vat == $ts_expense_vat_duplicate and $ts_expense_verified == 0 and $show_non_personal == NULL or array_search($ts_expense_vat, $array_duplicate) == TRUE and $ts_expense_verified == 0 and $show_non_personal == NULL) {
     $ts_expense_desc = $ts_expense_desc . " - <strong>DUPLICATE ENTRY?</strong>";
 }
 if ($array_expense['ts_expense_notes'] != NULL) {
     $ts_expense_desc = $ts_expense_desc . "<br />[Note: " . $array_expense['ts_expense_notes'] . "]";
 }
 $ts_expense_desc = DeCode(RemoveShit($ts_expense_desc));
 if ($ts_expense_p11d == 1) {
     if ($ts_expense_reimburse == 0 and $ts_expense_verified == 0) {
         $style_add = "background-color: #f6d1d1; color: #e24141";
         $total_queried = $total_queried + $ts_expense_vat;
     } else {
         $style_add = NULL;
     }
     echo "<tr><td style=\"{$style_add}\"><a href=\"index2.php?page=timesheet_expense_view&amp;ts_expense_id={$ts_expense_id}\">{$ts_expense_id}</a></td><td style=\"{$style_add}\">{$ts_expense_date}</td><td colspan=\"2\" style=\"{$style_add}\">{$ts_expense_desc}</td><td style=\"{$style_add}\">{$expense_cat_name}</td>";
     echo "<td style=\"text-align: right; width: 15%; {$style_add}\">" . CashFormat($ts_expense_vat) . "</td>";
     $col1 = $col1 + $ts_expense_vat;
     if ($ts_expense_reimburse > 0 and $ts_expense_verified == 0) {
         echo "<td style=\"text-align: right; width: 15%; {$style_add}\">" . CashFormat($ts_expense_vat) . "</td>";
         $col2 = $col2 + $ts_expense_vat;
     } else {
         echo "<td style=\"text-align: right; width: 15%; {$style_add}\"> - </td>";
$format_bg_g = "220";
$format_bg_b = "220";
$format_ln_r = "220";
$format_ln_g = "220";
$format_ln_b = "220";
// Begin creating the page
//Page Title
$sql_project = "SELECT * FROM intranet_projects_blog, intranet_user_details, intranet_projects WHERE blog_id = {$blog_id} AND blog_proj = proj_id AND blog_user = user_id LIMIT 1";
$result_project = mysql_query($sql_project, $conn) or die(mysql_error());
$array_project = mysql_fetch_array($result_project);
$proj_num = $array_project['proj_num'];
$proj_name = $array_project['proj_name'];
$blog_title = $array_project['blog_title'];
$blog_date = $array_project['blog_date'];
$blog_type = $array_project['blog_type'];
$blog_text = strip_tags(nl2br(RemoveShit($array_project['blog_text'])));
$user_name_first = $array_project['user_name_first'];
$user_name_second = $array_project['user_name_second'];
if ($blog_type == "phone") {
    $blog_type_view = "Telephone Call";
} elseif ($blog_type == "filenote") {
    $blog_type_view = "File Note";
} elseif ($blog_type == "meeting") {
    $blog_type_view = "Meeting Note";
} elseif ($blog_type == "email") {
    $blog_type_view = "Email Message";
} else {
    $blog_type_view = NULL;
    $type = 0;
}
$blog_type_view = $blog_type_view . " - " . $user_name_first . " " . $user_name_second;
 $cost_stage_uf = $array_cost_stage_uf['SUM(ts_rate * ts_hours)'];
 $cost_total = $cost_stage;
 $bar_units = $bar_width_standard / $bar_scale;
 $this_bar_width = $fee_stage * $bar_units;
 $cost_bar_width = $cost_total * $bar_units;
 if ($this_bar_width == 0) {
     $this_bar_width = $effectively_zero;
 }
 if ($cost_bar_width == 0) {
     $cost_bar_width = $effectively_zero;
 }
 if ($this_bar_width > 0 or $cost_bar_width > 0) {
     $fee_stage_print = str_replace("&pound;", "£", MoneyFormat($fee_stage));
     $cost_total = str_replace("&pound;", "£", MoneyFormat($cost_total)) . " [" . $ts_fee_text . "]";
     // $cost_total = $ts_fee_text . "(";
     $cost_total = RemoveShit($cost_total);
     SetBarLBlue();
     // Now make sure the project header appears if necessary on the next loop
     $current_project = $proj_id;
     if ($proj_riba == $ts_fee_id) {
         $pdf->SetTextColor(0, 0, 0);
     } else {
         $pdf->SetTextColor(150, 150, 150);
     }
     $pdf->Cell(13, 8, $fee_stage_print, 0, R, 0);
     $this_bar_width_1 = $this_bar_width / $ts_fee_target;
     $this_bar_width_2 = $this_bar_width - $this_bar_width_1;
     // Bar showing unfactored amount
     $cost_unfactored = $cost_stage_uf * $bar_units;
     if ($cost_unfactored > $this_bar_width) {
         SetBarRed();