if ($array_expense['ts_expense_verified'] > 0) {
     $ts_expense_verified = TimeFormatBrief($array_expense['ts_expense_verified']);
 } else {
     $ts_expense_verified = "-";
 }
 $ts_expense_value = CashFormat($array_expense['ts_expense_value']);
 $ts_expense_vat = CashFormat($array_expense['ts_expense_vat']);
 $ts_expense_receipt = $array_expense['ts_expense_receipt'];
 $ts_expense_reimburse = $array_expense['ts_expense_reimburse'];
 $proj_num = $array_expense['proj_num'];
 if ($ts_expense_invoiced == NULL) {
     $ts_expense_invoiced = 0;
 }
 if ($ts_expense_reimburse == 1 and $ts_expense_invoiced == 0) {
     $bg = 1;
     $pdf->SetFillColor(255, 255, 220);
 } else {
     $bg = 0;
 }
 if ($ts_expense_p11d == "1") {
     $pdf->SetTextColor(255, 0, 255);
 } else {
     $pdf->SetTextColor(0, 0, 0);
 }
 $pdf->SetFont($format_font, '', 7);
 $pdf->Cell(10, 4, $ts_expense_id, 0, 0, L, $bg);
 $pdf->SetFont($format_font, '', 9);
 $pdf->Cell(20, 4, $ts_expense_date, 0, 0, L, $bg);
 $pdf->Cell(15, 4, $proj_num, 0, 0, L, $bg);
 $pdf->MultiCell(55, 4, $ts_expense_desc, 0, L, $bg);
 $max_y = $pdf->GetY();
$pdf->SetXY(10, 70);
$pdf->SetLineWidth(0.5);
$sql_checklist = "SELECT * FROM intranet_project_checklist_items LEFT JOIN intranet_project_checklist ON checklist_item = item_id AND checklist_project = {$proj_id} WHERE checklist_required != 1 ORDER BY item_group, item_order, checklist_date DESC, item_name";
$result_checklist = mysql_query($sql_checklist, $conn) or die(mysql_error());
$y = $pdf->GetY() + 10;
$pdf->SetY($y);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetLineWidth(0.3);
$pdf->SetFont("Helvetica", 'B', 7);
$pdf->Cell(65, 5, "Item", B, 0, L, 0);
$pdf->Cell(15, 5, "Required", B, 0, L, 0);
$pdf->Cell(30, 5, "Date Completed", B, 0, L, 0);
$pdf->Cell(80, 5, "Comment", B, 1, L, 0);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont($format_font, '', 7);
$pdf->SetFillColor(240, 240, 240);
$pdf->SetLineWidth(0.1);
$fill = 0;
$group = NULL;
while ($array_checklist = mysql_fetch_array($result_checklist)) {
    $item_id = $array_checklist['item_id'];
    $item_name = $array_checklist['item_name'];
    $item_date = $array_checklist['item_date'];
    $item_group = $array_checklist['item_group'];
    $item_required = $array_checklist['item_required'];
    $item_notes = $array_checklist['item_notes'];
    $checklist_id = $array_checklist['checklist_id'];
    $checklist_required = $array_checklist['checklist_required'];
    $checklist_date = $array_checklist['checklist_date'];
    $checklist_comment = htmlentities($array_checklist['checklist_comment']);
    $checklist_user = $array_checklist['checklist_user'];
 // Printed by, and on...
 $pdf->SetFont($format_font, '', 12);
 $pdf->SetTextColor(0, 0, 0);
 $sql = "SELECT user_name_first, user_name_second FROM intranet_user_details WHERE user_id = {$_COOKIE['user']}";
 $result = mysql_query($sql, $conn) or die(mysql_error());
 $array = mysql_fetch_array($result);
 $user_name_first = $array['user_name_first'];
 $user_name_second = $array['user_name_second'];
 $pdf->SetFont($format_font, '', 8);
 $printed_on = "Generated " . date("r") . " by " . $user_name_first . " " . $user_name_second;
 $pdf->SetFont($format_font, '', 6);
 $pdf->Cell(0, 4, $printed_on, 0, 1, L, 0);
 $pdf->Ln();
 $legend = "Cost (Fa) = Factored cost\nCost (Ho) = Hourly cost\nCost (Fa Acc) = Factored accumulative cost\nCost (Ho Acc) = Hourly accumulative cost\n";
 $pdf->MultiCell(0, 3, $legend);
 $pdf->SetFillColor(220, 220, 220);
 // Begin the array through all users
 $sql = "SELECT * FROM intranet_user_details, intranet_timesheet LEFT JOIN intranet_timesheet_fees ON ts_project = ts_fee_stage WHERE ts_user = user_id AND ts_project = '{$proj_submit}' AND ts_entry BETWEEN '{$time_submit_begin}' AND '{$time_submit_end}' ORDER BY ts_stage_fee, ts_fee_time_begin";
 $result = mysql_query($sql, $conn) or die(mysql_error());
 $current_fee_stage = NULL;
 $running_cost = 0;
 $current_id = NULL;
 $stage_total = 0;
 $hours_total = 0;
 function StageTotal()
 {
     global $pdf;
     global $stage_total;
     global $hours_total;
     global $running_cost_nf;
     global $ts_fee_target;
$result_drawings = mysql_query($sql_drawings, $conn) or die(mysql_error());
$y = $pdf->GetY() + 10;
$pdf->SetY($y);
$pdf->SetTextColor(200, 200, 200);
$pdf->SetLineWidth(0.3);
$pdf->SetFont("Helvetica", 'B', 6);
$pdf->Cell(25, 5, "Drawing Number", B, 0, L, 0);
$pdf->Cell(10, 5, "Size", B, 0, L, 0);
$pdf->Cell(20, 5, "Scale", B, 0, L, 0);
$pdf->Cell(80, 5, "Drawing Title", B, 0, L, 0);
$pdf->Cell(20, 5, "Target Date", B, 0, L, 0);
$pdf->Cell(15, 5, "Current Rev.", B, 0, L, 0);
$pdf->Cell(20, 5, "Date", B, 1, L, 0);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont($format_font, '', 7);
$pdf->SetFillColor(240, 240, 240);
$fill = 0;
unset($current_type);
while ($array_drawings = mysql_fetch_array($result_drawings)) {
    $drawing_number = $array_drawings['drawing_number'];
    $drawing_id = $array_drawings['drawing_id'];
    $drawing_title = str_replace("\n", ", ", $array_drawings['drawing_title']);
    $drawing_title = preg_replace('/[^(\\x20-\\x7F)]*/', '', $drawing_title);
    $drawing_date = $array_drawings['drawing_date'];
    $drawing_targetdate = $array_drawings['drawing_targetdate'];
    $drawing_comment = $array_drawings['drawing_comment'];
    $scale_desc = $array_drawings['scale_desc'];
    $paper_size = $array_drawings['paper_size'];
    $this_type_array = explode("-", $drawing_number);
    $this_type = $this_type_array[2];
    if ($current_type != $this_type && $current_type != NULL) {
 $pdf->useTemplate($tplidx);
 $pdf->SetAutoPageBreak(0, 1.5);
 $pdf->AddFont($format_font, '', $format_font_2);
 // Page headers
 $project_counter = 1;
 $page_count = 1;
 $pdf->SetY(35);
 $pdf->SetFont('Helvetica', 'b', 24);
 $pdf->SetTextColor($format_bg_r, $format_bg_g, $format_bg_b);
 $pdf->Cell(0, 10, "Timesheet Analysis");
 $pdf->SetTextColor(0, 0, 0);
 $pdf->SetY(50);
 $pdf->SetFont('Helvetica', 'b', 18);
 $print_title = "Generated " . TimeFormatDetailed(time());
 $print_envelope = "Timesheet Datum: " . TimeFormat($settings_timesheetstart);
 $pdf->SetFillColor(220, 220, 220);
 $pdf->MultiCell(0, 8, $print_title, 0, L, 0);
 $pdf->SetFont('Helvetica', 'b', 12);
 $pdf->MultiCell(0, 5, $print_envelope, 0, L, 0);
 $pdf->MultiCell(0, 5, $print_profit_text, 0, L, 0);
 $pdf->Cell(0, 5, '', 0, 2);
 $pdf->SetFont($format_font, '', 10);
 $pdf->SetTextColor(0, 0, 0);
 // Array through the projects and fee stages
 $bar_width_standard = 130;
 $effectively_zero = 0.1;
 // Sets the minimum bar size if the quantity is zero
 $page_number = 0;
 $complete_profit_total = 0;
 $complete_fee_total = 0;
 $complete_cost_total = 0;
$proj_address_postcode = $array['proj_address_postcode'];
$proj_client_contact_id = $array['proj_client_contact_id'];
$proj_planning_ref = $array['proj_planning_ref'];
$proj_buildingcontrol_ref = $array['proj_buildingcontrol_ref'];
$print_title = $proj_num . " " . $proj_name;
$pdf->MultiCell(0, 8, $print_title, 0, L, 0);
// Printed by, and on...
$pdf->SetFont($format_font, '', 10);
$sql = "SELECT user_name_first, user_name_second FROM intranet_user_details WHERE user_id = {$_COOKIE['user']}";
$result = mysql_query($sql, $conn) or die(mysql_error());
$array = mysql_fetch_array($result);
$user_name_first = $array['user_name_first'];
$user_name_second = $array['user_name_second'];
$printed_on = "Current at " . date("g:ia, j F Y");
$pdf->Cell(0, 6, $printed_on, 0, 1, L, 0);
$pdf->SetFillColor(255, 255, 255);
$y_current = 70;
$x_current = 10;
// Project Address
$proj_address = $proj_address . AddLine($proj_address_1);
$proj_address = $proj_address . AddLine($proj_address_2);
$proj_address = $proj_address . AddLine($proj_address_3);
$proj_address = $proj_address . AddLine($proj_address_town);
$proj_address = $proj_address . AddLine($proj_address_county);
$proj_address = $proj_address . AddLine($proj_address_postcode);
StyleHeading("Site Address", $proj_address);
// Begin the conditions array
$sql_conditions = "SELECT * FROM intranet_projects_planning LEFT JOIN contacts_companylist ON company_id = condition_responsibility WHERE condition_project = {$proj_id} ORDER BY condition_decision_date, condition_ref, condition_number";
$result_conditions = mysql_query($sql_conditions, $conn) or die(mysql_error());
if (mysql_num_rows($result_conditions) > 0) {
    unset($current_ref);
    $section = "Section " . $_GET[s1] . " only - " . $qms_firstpage;
    $s1 = " WHERE qms_toc1 = {$s1} ";
} else {
    unset($s1);
}
if ($_GET[s2] > 0) {
    $s2 = intval($_GET[s2]);
    $section = "Section " . $_GET[s1] . "." . $_GET[s2] . " only - " . $qms_firstpage;
    $s2 = " AND qms_toc2 = {$s2} ";
} else {
    unset($s2);
}
$pdf->AddFont($format_font, '', $format_font_2);
$pdf->SetXY(10, 175);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(220, 220, 220);
$pdf->SetFont('Helvetica', '', 18);
$pdf->MultiCell(0, 10, $settings_companyname, 0, L);
$pdf->SetFont('Helvetica', 'B', 32);
$pdf->MultiCell(0, 20, 'Quality Management System', 0, L);
$pdf->SetFont($format_font, '', 14);
$printed_date = "Current at " . date("g.ia, jS F Y", time());
$pdf->MultiCell(0, 25, '', 0, L);
$pdf->MultiCell(0, 8, $printed_date, 0, L);
$pdf->SetLineWidth(0.1);
$pdf->SetFont($format_font, '', 11);
$width = $pdf->GetStringWidth($printed_date) + 20;
if ($s1 != NULL or $s2 != NULL) {
    $pdf->MultiCell(0, 8, $section, T, L);
}
$sql = "SELECT * FROM intranet_qms {$s1} {$s2} ORDER BY qms_toc1, qms_toc2, qms_toc3, qms_toc4";
 $ts_expense_value = CashFormat($array_expense['ts_expense_value']);
 $ts_expense_vat = CashFormat($array_expense['ts_expense_vat']);
 $ts_expense_receipt = $array_expense['ts_expense_receipt'];
 $ts_expense_reimburse = $array_expense['ts_expense_reimburse'];
 $ts_expense_p11d = $array_expense['ts_expense_p11d'];
 $user_intials = $array_expense['user_initials'];
 $proj_num = $array_expense['proj_num'];
 if ($ts_expense_p11d == 1) {
     $expense_p11d_total = $expense_p11d_total + $array_expense['ts_expense_vat'];
 }
 if ($ts_expense_invoiced == NULL) {
     $ts_expense_invoiced = 0;
 }
 if ($ts_expense_p11d == 1 and $ts_expense_invoiced > 0) {
     $bg = 1;
     $pdf->SetFillColor(255, 0, 0);
 } elseif ($ts_expense_p11d == 1) {
     $bg = 1;
     $pdf->SetFillColor(180, 230, 130);
 } elseif ($ts_expense_invoiced > 0) {
     $bg = 1;
     $pdf->SetFillColor(255, 255, 200);
 } else {
     $bg = 0;
 }
 $pdf->SetX(55);
 $pdf->MultiCell(45, 4, $ts_expense_desc, 0, L, $bg);
 $max_y = $pdf->GetY();
 $cellheight = $max_y - $y;
 $pdf->SetXY(10, $y);
 $link = $pdf_location . "index2.php?page=timesheet_expense_view&ts_expense_id=" . $ts_expense_id;
 $invoice_due = $array_invoice['invoice_due'];
 $invoice_project = $array_invoice['invoice_project'];
 $invoice_paid = $array_invoice['invoice_paid'];
 $invoice_account = $array_invoice['invoice_account'];
 $invoice_month = date("n", $invoice_date);
 $invoice_year = date("Y", $invoice_date);
 $invoice_id_print = $invoice_id;
 $invoice_ref_print = strtoupper($invoice_ref);
 $invoice_date_print = TimeFormat($invoice_date);
 $invoice_due_print = TimeFormat($invoice_due);
 if ($invoice_paid > 0) {
     $invoice_paid_print = TimeFormat($invoice_paid);
 } else {
     $invoice_paid_print = "-";
 }
 $pdf->SetFillColor(255, 150, 150);
 if ($invoice_paid == 0 and $invoice_due < time()) {
     $highlight = 1;
 } else {
     $highlight = 0;
 }
 if ($invoice_month != $thismonth and $thismonth > 0) {
     // Month Total
     $pdf->SetXY($x, $y);
     $month_total_print = $invoice_date - 2592000;
     $total_print = "Total for " . $month;
     $pdf->SetTextColor(150, 150, 150);
     $pdf->Cell(130, 6, $total_print, 'T');
     $invoice_novat_month_print = CashFormat($invoice_novat_month);
     $invoice_vat_month_print = CashFormat($invoice_vat_month);
     $pdf->Cell(30, 6, $invoice_novat_month_print, 'T', 0, 'R');
 $ts_expense_user = $array_expense['ts_expense_user'];
 if ($array_expense['ts_expense_verified'] > 0) {
     $ts_expense_verified = TimeFormatBrief($array_expense['ts_expense_verified']);
 } else {
     $ts_expense_verified = "-";
 }
 $ts_expense_value = CashFormat($array_expense['ts_expense_value']);
 $ts_expense_vat = CashFormat($array_expense['ts_expense_vat']);
 $ts_expense_receipt = $array_expense['ts_expense_receipt'];
 $ts_expense_reimburse = $array_expense['ts_expense_reimburse'];
 $proj_num = $array_expense['proj_num'];
 if ($ts_expense_invoiced == NULL) {
     $ts_expense_invoiced = 0;
 }
 if ($_POST[include_p11d] == "both" and $ts_expense_p11d == 1) {
     $pdf->SetFillColor(255, 210, 150);
     $bg = 1;
 } else {
     $pdf->SetFillColor(255, 255, 255);
     $bg = 0;
 }
 $pdf->SetFont($format_font, '', 7);
 $pdf->Cell(10, 4, $ts_expense_id, 0, 0, L, $bg);
 $pdf->SetFont($format_font, '', 9);
 $pdf->Cell(20, 4, $ts_expense_date, 0, 0, L, $bg);
 $pdf->Cell(15, 4, $proj_num, 0, 0, L, $bg);
 $pdf->MultiCell(55, 4, $ts_expense_desc, 0, L, $bg);
 $max_y = $pdf->GetY();
 $x = $x + 100;
 $y = $y;
 $pdf->SetXY($x, $y);
     $pdf->SetTextColor(0, 0, 0);
     $pdf->Cell(50, 6, $proj_num, 0, 1, L);
 }
 if ($ts_fee_conclude >= BeginWeek($current_time)) {
     $pdf->SetFont('Helvetica', '', 7);
     $pdf->Cell(40, $rowheight, $ts_fee_text, 0, 0, L);
     if ($proj_riba == $ts_fee_id) {
         $color = array(0.07000000000000001, 0.82, 0.72);
     } else {
         $color = array(0.47, 0.75, 0.9399999999999999);
     }
     //$color = array(0.47,0.75,0.94);
     $color1 = Colors($color[0], $ts_fee_prospect);
     $color2 = Colors($color[1], $ts_fee_prospect);
     $color3 = Colors($color[2], $ts_fee_prospect);
     $pdf->SetFillColor($color1, $color2, $color3);
     $stage_start = $ts_fee_commence - BeginWeek($current_time);
     if ($stage_start < 0) {
         $stage_start = 0;
         $ts_fee_time_end = $ts_fee_time_end - ($current_time - $ts_fee_commence);
         $noborder = 1;
     } else {
         $noborder = 0;
     }
     $stage_start = Weeks($stage_start);
     if ($stage_start > 0 & $stage_start < 230) {
         $pdf->Cell($stage_start, $rowheight, '', 0, 0, L);
     }
     $stage_width = Weeks($ts_fee_time_end);
     $pdf->SetFont('Helvetica', '', 5);
     $count = 0;