$pdf->SetXY(10, 45);
$pdf->SetFont($format_font, '', 24);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetDrawColor(0, 0, 0);
$pdf->Cell(0, 10, $sheet_title);
$pdf->SetXY(10, 55);
$pdf->SetFont($format_font, '', 14);
$sheet_subtitle = $proj_num . " " . $proj_name;
$sheet_date = "Current at " . $current_date;
$pdf->Cell(0, 7.5, $sheet_subtitle, 0, 1, L, 0);
$pdf->Cell(0, 7.5, $sheet_date, 0, 1, L, 0);
$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)) {
     $pdf->MultiCell(190, 15, $account_name, '', R);
 }
 //Heading Titles Text
 $pdf->SetTextColor(0, 0, 0);
 $pdf->SetFont($format_font, '', 9);
 $pdf->SetXY(10, 88);
 $company_address = $settings_companyname . "\n" . $settings_companyaddress;
 $pdf->MultiCell(0, 4, $company_address);
 $pdf->SetXY(57.5, 88);
 if (trim($contact_address_print) == "") {
     $pdf->SetTextColor(255, 0, 0);
     $contact_address_print = "Client Details Required";
 }
 $pdf->MultiCell(45, 4, $contact_address_print, 0, L);
 $pdf->SetTextColor(0, 0, 0);
 $y = $pdf->GetY();
 $pdf->SetXY(105, 88);
 $pdf->Cell(0, 4, $invoice_ref);
 $pdf->SetXY(152.5, 88);
 $pdf->Cell(0, 4, $invoice_date_print);
 //if ($invoice_purchase_order != NULL) {
 $pdf->SetXY(105, 108);
 $pdf->Cell(0, 4, $invoice_purchase_order);
 //}
 $y = $y + 10;
 $pdf->SetDrawColor(0, 0, 0);
 $pdf->SetLineWidth(0.1);
 $pdf->SetXY(40, $y);
 $pdf->SetFont($format_font, '', 10);
 $pdf->Cell(150, 6, 'Description of Work Undertaken', 'B', '', '', false);
 $x = 40;
     $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();
 $x = $x + 100;
 $y = $y;
 $pdf->SetXY($x, $y);
 $pdf->Cell(20, 4, $ts_expense_verified, 0, 0, L, $bg);
 $pdf->Cell(25, 4, $invoice_ref_print, 0, 0, L, $bg);
 $pdf->Cell(15, 4, $ts_expense_value, 0, 0, R, $bg);
 $pdf->Cell(15, 4, $ts_expense_diff_print, 0, 0, R, $bg);
 $pdf->Cell(15, 4, $ts_expense_vat, 0, 1, R, $bg);
 $x = 10;
 $y = $max_y;
 $expense_total_value = $expense_total_value + $array_expense['ts_expense_value'];
 $expense_total_diff = $expense_total_diff + $ts_expense_diff;
 $expense_total_vat = $expense_total_vat + $array_expense['ts_expense_vat'];
 // Work out the totals for invoiced, non-invoiced values
 if ($ts_expense_invoiced > 0) {
 // Add the stage total if necessary
 if ($current_fee_stage != NULL and $_POST[separate_pages] != 1) {
     StageTotal($stage_total);
     StageFee($ts_stage_fee);
 } elseif ($current_fee_stage != NULL) {
     $pdf->SetFont($format_font, '', 8);
     $pdf->Cell(20, 7, 'Total', T, 0, L);
     $hours_total_print = number_format($hours_total);
     $pdf->Cell(8, 7, $hours_total_print, T, 0, R);
     $pdf->Cell(0, 7, 'hours', T, 1, L);
     $hours_total = 0;
 }
 if ($_POST[separate_pages] == 1 && $current_fee_stage != NULL) {
     $pdf->AddPage();
 }
 $current_y = $pdf->GetY();
 if ($current_y > 240) {
     $pdf->addPage();
 }
 $pdf->SetFont($format_font, '', 8);
 $pdf->Ln();
 $pdf->Cell(0, 5, $ts_fee_text, 0, 1, L, true);
 $pdf->SetFont($format_font, '', 6, true);
 $pdf->Cell(20, 4, 'Date', 0, 0, L, true);
 $pdf->Cell(13, 4, 'Hours', 0, 0, C, true);
 $pdf->Cell(15, 4, 'Cost (Fa)', 0, 0, R, true);
 $pdf->Cell(15, 4, 'Cost (Ho)', 0, 0, R, true);
 $pdf->Cell(8, 4, 'Initials', 0, 0, C, true);
 $pdf->Cell(20, 4, 'Cost (Fa Acc)', 0, 0, R, true);
 $pdf->Cell(20, 4, 'Cost (Ho Acc)', 0, 0, R, true);
 $pdf->Cell(0, 4, 'Description', 0, 0, L, true);
$pdf->SetFont($format_font, '', 24);
$pdf->SetTextColor(150, 150, 150);
$pdf->SetDrawColor(150, 150, 150);
$pdf->Cell(0, 10, $sheet_title);
$pdf->SetXY(10, 55);
$pdf->SetFont($format_font, '', 14);
$sheet_subtitle = $proj_num . " " . $proj_name . ", printed " . $current_date;
$pdf->Cell(0, 10, $sheet_subtitle, 0, 1, L, 0);
$pdf->SetXY(10, 60);
$pdf->SetLineWidth(0.5);
// And now the list of drawings issued
unset($current_drawing);
//$sql_drawings = "SELECT * FROM intranet_drawings_issued, intranet_drawings LEFT JOIN intranet_drawings_revision ON revision_drawing = drawing_id WHERE issue_set = $set_id AND issue_drawing = drawing_id ORDER BY drawing_number";
$sql_drawings = "SELECT * FROM intranet_drawings, intranet_drawings_scale, intranet_drawings_paper WHERE drawing_project = {$proj_id} AND drawing_scale = scale_id AND drawing_paper = paper_id order by drawing_number";
$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;
 $bar_width_cost = $cost_total / $maximum_total_fee * $this_bar_width;
 if ($cost_total > 0 && $fee_total > 0) {
     $cost_total_print = str_replace("£", "£", MoneyFormat($cost_total));
     $fee_total_print = str_replace("£", "£", MoneyFormat($fee_total));
     if ($current_project != $proj_id) {
         if ($project_cost_total > 0) {
             $project_cost_total = 0;
             $project_fee_total_target = 0;
             $project_fee_total_profit = 0;
         }
         // Check that all of the project data will fit on this page by establishing the number of fee stages
         $sql_fee_stage_quantity = "SELECT ts_fee_stage FROM intranet_timesheet_fees WHERE ts_fee_project = {$proj_id}";
         $result_fee_stage_quantity = mysql_query($sql_fee_stage_quantity, $conn) or die(mysql_error());
         $fee_stage_quantity = mysql_num_rows($result_fee_stage_quantity);
         $height_fee_stage = 45 + $fee_stage_quantity * 6;
         $current_location_y = $pdf->GetY();
         if ($height_fee_stage + $current_location_y > 285 && $height_fee_stage < 275) {
             $height_fee_stage = 0;
             $pdf->addPage();
         }
         $proj_link = "http://intranet.rcka.co.uk/timesheet_pdf_2.php?proj_id=" . $proj_id;
         $pdf->Cell(0, 8, '', 0, 1);
         $current_y = $pdf->GetY() + 1;
         // Create light grey bar
         SetBarLGray();
         $pdf->SetDrawColor(175, 175, 175);
         $pdf->SetTextColor(175, 175, 175);
         $pdf->Cell(0, 7, '', BT, 1, L, true);
         $pdf->SetDrawColor(175, 175, 175);
         $pdf->SetTextColor(175, 175, 175);
         if ($cost_total > $fee_total) {
$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);
    while ($array_conditions = mysql_fetch_array($result_conditions)) {
        if ($pdf->GetY() > 200) {
            $pdf->addPage();
        }
        if ($array_conditions['condition_type'] == "Informative Only") {
            $condition_approved = "Not required";
        } elseif ($array_conditions['condition_approved'] != "0000-00-00") {
            $condition_approved = date("j M Y", AssessDays($array_conditions['condition_approved']));
        } elseif ($array_conditions['condition_submitted'] != "0000-00-00") {
            $condition_approved = "Submitted";
        } else {
            $condition_approved = "Not approved";
        }
        if ($array_conditions['condition_type'] == "Informative Only") {
            $condition_submitted = "Not applicable";
            $pdf->SetFillColor(225, 225, 225);
        } elseif ($array_conditions['condition_submitted'] != "0000-00-00") {
 $qms_toc2 = $array['qms_toc2'];
 $qms_toc3 = $array['qms_toc3'];
 $qms_toc4 = $array['qms_toc4'];
 $qms_type = $array['qms_type'];
 $qms_text = strip_tags($array['qms_text']);
 $qms_timestamp = $array['qms_timestamp'];
 $qms_date = date("d M Y", $qms_timestamp);
 $number = $qms_toc4;
 if ($_GET[proj_id] != NULL && $qms_text == "[project name]") {
     $qms_text = ProjectData($_GET[proj_id], $type);
 }
 if ($_GET[proj_id] != NULL && substr($qms_text, 0, 1) == "#") {
     $qms_text = ChecklistDate($_GET[proj_id], $qms_text);
 }
 if ($qms_toc4 > 0 && $qms_type == "code") {
     if ($pdf->GetY() > 270) {
         $pdf->addPage();
     }
     UpDate($qms_date);
     $pdf->SetTextColor(180);
     $pdf->SetFont('Helvetica', '', 5);
     $pdf->Cell(15, 5, $number, 0, 0, R);
     $pdf->SetFont('Courier', '', 10);
     $pdf->SetTextColor(0);
     $pdf->Cell(150, 2, '', 0, 2, '', 1);
     $pdf->MultiCell(150, 4.5, $qms_text, 0, '', true);
     $pdf->SetX(25);
     $pdf->Cell(150, 2, '', 0, 2, '', 1);
     $pdf->Cell(0, 3, '', 0, 1);
 } elseif ($qms_toc4 > 0 && $qms_type == "comp") {
     if ($pdf->GetY() > 260) {
//Page Title
$sheet_title = "Expenses Summary, " . date("j F Y", time());
$pdf->SetXY(10, 45);
$pdf->SetFont($format_font, '', 26);
$pdf->SetTextColor(200, 200, 200);
$pdf->Cell(0, 10, $sheet_title);
$pdf->SetXY(10, 55);
$pdf->SetFont($format_font, '', 12);
$sheet_subtitle = "For period " . TimeFormat($date_begin) . " to " . TimeFormat($date_end);
$pdf->Cell(0, 6, $sheet_subtitle, 0, 1, L, 0);
if ($_POST[show_p11d_only] == "1") {
    $pdf->Cell(0, 6, "Showing P11d Items Only", 0, 1, L, 0);
    $p11d = " AND ts_expense_p11d = 1 ";
}
$x = 10;
$y = $pdf->GetY() + 10;
$pdf->SetY($y);
// Columnn Headings
$pdf->SetTextColor(220, 220, 220);
$pdf->SetFont($format_font, '', 8);
$pdf->Cell(10, 4, "ID", 0, 0, L, 0);
$pdf->Cell(20, 4, "Date", 0, 0, L, 0);
$pdf->Cell(15, 4, "Project", 0, 0, L, 0);
$pdf->Cell(55, 4, "Description", 0, 0, L, 0);
$pdf->Cell(20, 4, "Date Verified", 0, 0, L, 0);
$pdf->Cell(25, 4, "Invoice (ID)", 0, 0, L, 0);
$pdf->Cell(15, 4, "Net.", 0, 0, R, 0);
$pdf->Cell(15, 4, "VAT", 0, 0, R, 0);
$pdf->Cell(15, 4, "Gross.", 0, 1, R, 0);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont($format_font, '', 9);
    $page_number_total = $pdf->AliasNbPages();
    $sheet_subtitle_print = $sheet_subtitle . ", Sheet " . $page_number_current;
    // . " of " . $page_number_total ;
    $sheet_title = "Drawing Issue Matrix";
    $pdf->SetFont($format_font, '', 16);
    $pdf->SetTextColor(150, 150, 150);
    $pdf->SetDrawColor(150, 150, 150);
    $pdf->Cell(0, 8, $sheet_title, 0, 1);
    $pdf->SetFont($format_font, '', 12);
    $pdf->Cell(0, 6, $sheet_subtitle_print, 0, 1);
    $pdf->SetLineWidth(0.5);
}
$pdf->SetXY(10, 45);
// And now the list of drawings issued
unset($current_drawing);
$y = $pdf->GetY();
$pdf->SetY($y);
function HeadingLine()
{
    global $pdf;
    global $proj_id;
    global $conn;
    global $format_font;
    $array_issued = array();
    $pdf->SetTextColor(200, 200, 200);
    $pdf->SetLineWidth(0.15);
    $pdf->SetFont("Helvetica", 'B', 6);
    $pdf->Cell(20, 8, "Drawing", B, 0, L, 0);
    $pdf->Cell(60, 8, "Drawing Title", BR, 0, L, 0);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFont($format_font, '', 5);
$pdf->Cell(30, 7.5, $set_format, T, 0, L, 0);
$pdf->Cell(30, 7.5, $user_name, T, 0, L, 0);
if ($user_name_checked == "None") {
    $pdf->SetTextColor(255, 0, 0);
}
$pdf->Cell(30, 7.5, $user_checked_initials, T, 0, L, 0);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(30, 7.5, $set_id, T, 1, L, 0);
$pdf->Cell(0, 0.5, '', T, 1, L, 0);
$pdf->SetLineWidth(0.4);
// And now the list of drawings issued
unset($current_drawing);
//$sql_drawings = "SELECT * FROM intranet_drawings_issued, intranet_drawings LEFT JOIN intranet_drawings_revision ON revision_drawing = drawing_id WHERE issue_set = $set_id AND issue_drawing = drawing_id ORDER BY drawing_number";
$sql_drawings = "SELECT * FROM intranet_drawings, intranet_drawings_issued LEFT JOIN intranet_drawings_revision ON revision_id = issue_revision WHERE drawing_id = issue_drawing AND issue_set = {$set_id} ORDER BY drawing_number";
$result_drawings = mysql_query($sql_drawings, $conn) or die(mysql_error());
$y = $pdf->GetY() + 5;
$pdf->SetY($y);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont($format_font, '', 14);
$pdf->Cell(0, 7.5, "Drawings Issued", 0, 1, L, 0);
$pdf->SetFont("Helvetica", 'B', 8);
$pdf->Cell(50, 5, "Drawing Number", 0, 0, L, 0);
$pdf->Cell(10, 5, "Rev.", 0, 0, L, 0);
$pdf->Cell(25, 5, "Date", 0, 0, L, 0);
$pdf->Cell(0, 5, "Drawing Title", 0, 1, L, 0);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont($format_font, '', 9);
$pdf->SetLineWidth(0.3);
$pdf->SetFillColor(240, 240, 240);
$fill = 1;
while ($array_drawings = mysql_fetch_array($result_drawings)) {
                 $arraycount++;
             }
             $pdf->Cell(0, 0, '', 0, 1, L);
         }
         $pdf->Cell(0, $rowheight, '', 0, 1, L);
         //if ((BeginWeek(AssessDays($ts_datum_commence)) + $ts_datum_length) < $ts_fee_conclude) { $color_array = array(255,0,0); } else { unset($color_array); }
         if ($array_proj['ts_fee_commence'] != $array_proj['ts_datum_commence'] or $array_proj['ts_fee_time_end'] > $array_proj['ts_datum_length']) {
             $color_array = array(225, 0, 0);
         } else {
             unset($color_array);
         }
         Datum($ts_datum_commence, $ts_datum_length, $color_array);
         $pdf->Cell(0, 1, '', 0, 1, L);
     }
     $current_proj = $proj_id;
     if ($pdf->GetY() > 170) {
         $pdf->addPage(L);
         DrawGrid();
     }
 }
 $pdf->addPage(L);
 DrawGrid();
 // Now add the totals at the end
 $x = 0;
 $pdf->SetFont('Helvetica', 'B', 8);
 $pdf->Cell(0, 5, '', 0, 1, L);
 $pdf->Cell(40, 5, "TOTAL", 0, 0, L);
 $pdf->SetFont('Helvetica', '', 6);
 $arrayname = 0;
 while ($x <= 220) {
     $x = $x + $colwidth;