<?php
function project_BRUKLD_PDFreport($SAPoutputXML, $filename, $project_type)
{
	$PID = 1;

	if (empty($PID)) {
		$PID = $_GET['PID'];
	}

	include_once __DIR__ . '/xx_xml.class_brukld.php';
	$InputBRUKLD = xml2array(str_replace('\'', '', $SAPoutputXML));
	$compliance_report = $InputBRUKLD['SAP-Compliance-Report'];
	$SAP_data = $InputBRUKLD['SAP-Compliance-Report']['SAP-Report'];
	include_once __DIR__ . '/library/TCPDF-6.2.26/tcpdf.php';
        // Allow user-supplied values to override XML defaults
        if (!empty($_REQUEST['assessed_by'])) {
                $SAP_data['Report-Header']['Home-Inspector']['Name'] = $_REQUEST['assessed_by'];
        }
        if (!empty($_REQUEST['ocdea_reg'])) {
                $SAP_data['Report-Header']['Home-Inspector']['Identification-Number']['Certificate-Number'] = $_REQUEST['ocdea_reg'];
        }
        if (!empty($_REQUEST['assessment_date'])) {
                $SAP_data['Energy-Assessment']['Assessment-Date'] = $_REQUEST['assessment_date'];
        }
        if (!empty($_REQUEST['client_name'])) {
                $compliance_report['Client-Name'] = $_REQUEST['client_name'];
        }
        if (!empty($_REQUEST['client_company'])) {
                $compliance_report['Client-Company'] = $_REQUEST['client_company'];
        }
        if (!empty($_REQUEST['client_address1']) || !empty($_REQUEST['client_address2']) || !empty($_REQUEST['client_address3'])) {
                $compliance_report['Client-Address'] = [
                        'Address-Line-1' => $_REQUEST['client_address1'] ?? '',
                        'Address-Line-2' => $_REQUEST['client_address2'] ?? '',
                        'Address-Line-3' => $_REQUEST['client_address3'] ?? ''
                ];
        }
	include_once __DIR__ . '/mypdf_class.php';
        $todaydate = gmdate('D d M Y   H:i:s');
        if (!empty($_REQUEST['date_generated'])) {
                $ts = strtotime($_REQUEST['date_generated']);
                if ($ts !== false) {
                        $todaydate = gmdate('D d M Y   H:i:s', $ts);
                }
        }
	$USER_ID = 1;

	if (!$PID) {
		$PID = $_GET['root_key'];
	}

	$pdfFileName = $filename . '.pdf';
	$e_version = ['001', ''];
	$GLOBALS['_e_version'] = $e_version;
	$temp_folder = $GLOBALS['_SITE'] . '/brukld/PDF/';
	$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	$pdf->SetCreator(PDF_CREATOR);
	$pdf->SetAuthor('BRE');
	$pdf->SetTitle($pdfFileName);
	$pdf->SetSubject('BRUKLD Report');
	$pdf->SetKeywords('BRUKLD, report, energy, co2, compliance, emissions, heat loss');
	$pdf->setPrintHeader(false);
	$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
	$pdf->SetMargins(PDF_MARGIN_LEFT, 20, PDF_MARGIN_RIGHT);
	$pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
	$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

	if (@file_exists(dirname(__FILE__) . '/lang/eng.php')) {
		require_once dirname(__FILE__) . '/lang/eng.php';
		$pdf->setLanguageArray($l);
	}

	$pdf->setFontSubsetting(true);
	$pdf->SetFont('helvetica', '', 9, '', false);
	$uk_fill = [47, 84, 150];
	$pdf->SetFillColorArray($uk_fill);
	$pdf->AddPage();
	$style_thick = [
		'width' => 0.3,
		'cap'   => 'butt',
		'join'  => 'miter',
		'dash'  => 0,
		'color' => [0, 0, 0]
	];
	$pdf->setJPEGQuality(100);

	if (!empty($SAP_data['Report-Header']['Country-Code'])) {
		$country_code = $SAP_data['Report-Header']['Country-Code'];
	}
	else {
		throw new Exception('"Country-Code" is missing.');
	}

	switch ($country_code) {
	case 'ENG':
		$country_name = 'England';
		$country_doc = 'BREL';
		break;
	case 'WLS':
		$country_name = 'Wales';
		$country_doc = 'BRWL';
		break;
	default:
		throw new Exception('"Country-Code" is invalid.');
		break;
	}

	$y_ref = 13;
	$doc_title = "\t\t\t\t" . '<p style="font-size:19px; color:#000000;"><b>Building Regulations ' . $country_name . ' Part L (' . $country_doc . ') Compliance Report</b></p>';
	$pdf->writeHTMLCell(0, 7, PDF_MARGIN_LEFT, $y_ref, $doc_title, 0, 1, 0, true, 'C', true);
	$y_ref += 10;
	$n_software = $SAP_data['Calculation-Software-Name'];
	$v_software = $SAP_data['Calculation-Software-Version'];
    $doc_intro = '               <p>Approved Document L1 2021 Edition, ' . $country_name . ' assessed by ' . $n_software . ' SAP 10 program, 10.2.03</p>';
	$pdf->writeHTMLCell(0, 8.5, PDF_MARGIN_LEFT, $y_ref, $doc_intro, 0, 1, 0, true, 'L', true);
	$y_ref += 7;
	$doc_date = '               <p style="font-size:10px"><b>Date:</b> ' . $todaydate . '</p>';
	$pdf->writeHTMLCell(0, 6, PDF_MARGIN_LEFT, $y_ref, $doc_date, 0, 1, 0, true, 'L', true);
	if (!empty($compliance_report['Is-Multiple-Compliance']) && ($compliance_report['Is-Multiple-Compliance'] == 'true')) {
		$multi_assess = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th colspan="1"><b>This report forms part of a multiple dwelling assessment using a floor area-weighted average calculation</b></th>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
		$y_ref = $pdf->getY();
		$pdf->writeHTMLCell(0, 8.5, PDF_MARGIN_LEFT, $y_ref - 4, $multi_assess, 0, 1, 0, true, 'C', true);
	}

	$assessor_name = check_assessor_name($SAP_data['Report-Header']['Home-Inspector']);
	$assessor_number = check_assessor_number($SAP_data['Report-Header']['Home-Inspector']);
	$property_type = convert_property_type($SAP_data['SAP10-Data']['SAP-Property-Details']['Property-Type']);
	$built_form = convert_built_form($SAP_data['SAP10-Data']['SAP-Property-Details']['Built-Form']);
	$assessment_date = $SAP_data['Energy-Assessment']['Assessment-Date'];
	$project_info = "\t\t\t\t\t" . '<br><br>' . "\n\t\t\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="4"><b>Project Information</b></th>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Assessed By</td>' . "\n\t\t\t\t" . '                <td style="color:' . $assessor_name['colour'] . '">' . $assessor_name['text'] . '</td>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Building Type</td>' . "\n\t\t\t\t\t\t" . '<td>' . $property_type . ', ' . $built_form . '</td>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">OCDEA Registration</td>' . "\n\t\t\t\t" . '                <td style="color:' . $assessor_number['colour'] . '">' . $assessor_number['text'] . '</td>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Assessment Date</td>' . "\n\t\t\t\t\t\t" . '<td>' . $assessment_date . '</td>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 4, $project_info, 0, 1, 0, true, 'L', true);
	if (($SAP_data['SAP10-Data']['Data-Type'] == 1) || ($SAP_data['SAP10-Data']['Data-Type'] == 2)) {
		$assessment_type = convert_data_type($SAP_data['SAP10-Data']['Data-Type']);
	}
	else {
		throw new Exception('"Data-Type" is invalid.');
	}

	$total_floor_area = $SAP_data['Energy-Assessment']['Property-Summary']['Total-Floor-Area'];
	$address_all = obtain_address($SAP_data['Report-Header']['Property']['Address']);

	if (!empty($SAP_data['Report-Header']['Property']['Site-Reference'])) {
		$site_reference = $SAP_data['Report-Header']['Property']['Site-Reference'];
	}
	else {
		$site_reference = 'Not provided';
	}

	if (!empty($SAP_data['Report-Header']['Property']['Plot-Reference'])) {
		$plot_reference = $SAP_data['Report-Header']['Property']['Plot-Reference'];
	}
	else {
		$plot_reference = 'Not provided';
	}

	$dwelling_details = "\t\t\t\t\t" . '<br><br>' . "\n\t\t\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="4"><b>Dwelling Details</b></th>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Assessment Type</td>' . "\n\t\t\t\t\t\t" . '<td>' . $assessment_type . '</td>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Total Floor Area</td>' . "\n\t\t\t\t\t\t" . '<td>' . $total_floor_area . ' m<sup>2</sup></td>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Site Reference</td>' . "\n\t\t\t\t\t\t" . '<td>' . $site_reference . '</td>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Plot Reference</td>' . "\n\t\t\t\t\t\t" . '<td>' . $plot_reference . '</td>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Address</td>' . "\n\t\t\t\t\t\t" . '<td colspan="3">' . $address_all . '</td>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref + 1, $dwelling_details, 0, 1, 0, true, 'L', true);
	$client_name = $compliance_report['Client-Name'];
	$client_company = $compliance_report['Client-Company'];
	$client_address = obtain_address($compliance_report['Client-Address']);
	$client_details = "\t\t\t\t\t" . '<br><br>' . "\n\t\t\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="4"><b>Client Details</b></th>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Name</td>' . "\n\t\t\t\t\t\t" . '<td colspan="3">' . $client_name . '</td>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Company</td>' . "\n\t\t\t\t\t\t" . '<td colspan="3">' . $client_company . '</td>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '  <tr>' . "\n\t\t\t\t\t\t" . '<td style="background-color:rgb(189,214,238)">Address</td>' . "\n\t\t\t\t\t\t" . '<td colspan="3">' . $client_address . '</td>' . "\n\t\t\t\t\t" . '  </tr>' . "\n\t\t\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref + 1, $client_details, 0, 1, 0, true, 'L', true);
	$doc_descrip = '               <p style="font-size:11px";><b>This report covers items included within the SAP calculations. It is not a complete report of regulations compliance.<b></p>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, 8.5, PDF_MARGIN_LEFT, $y_ref + 3, $doc_descrip, 0, 1, 0, true, '', true);
	$main_heat_fuel = obtainMainHeatFuel($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Heating']['Main-Heating-Details']);

	if (!empty($SAP_data['Energy-Assessment']['Energy-Use']['TER'])) {
		$target_emission_rate = $SAP_data['Energy-Assessment']['Energy-Use']['TER'];
	}
	else {
		throw new Exception('"TER" is missing.');
	}

	if (!empty($SAP_data['Energy-Assessment']['Energy-Use']['DER'])) {
		$dwelling_emission_rate = $SAP_data['Energy-Assessment']['Energy-Use']['DER'];
	}
	else {
		throw new Exception('"DER" is missing.');
	}

	$emissions_check = check_compliance($dwelling_emission_rate, $target_emission_rate);
	$table_1a = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>1a Target emission rate and dwelling emission rate</b></th>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3">Fuel for main heating system</td>' . "\n\t\t\t\t" . '<td colspan="3">' . $main_heat_fuel . '</td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3"><i>Target carbon dioxide emission rate</i></td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $target_emission_rate . ' kgCO<sub>2</sub>/m<sup>2</sup></i></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3">Dwelling carbon dioxide emission rate</td>' . "\n\t\t\t\t" . '<td colspan="2">' . $dwelling_emission_rate . ' kgCO<sub>2</sub>/m<sup>2</sup></td>' . "\n\t\t\t\t" . '<td style="color:' . $emissions_check['colour'] . '"><b>' . $emissions_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 5, $table_1a, 0, 1, 0, true, 'L', true);

	if (!empty($SAP_data['Energy-Assessment']['Energy-Use']['TPER'])) {
		$target_primary_energy = $SAP_data['Energy-Assessment']['Energy-Use']['TPER'];
	}
	else {
		throw new Exception('"TPER" is missing.');
	}

	if (!empty($SAP_data['Energy-Assessment']['Energy-Use']['DPER'])) {
		$dwelling_primary_energy = $SAP_data['Energy-Assessment']['Energy-Use']['DPER'];
	}
	else {
		throw new Exception('"DPER" is missing.');
	}

	$primary_energy_check = check_compliance($dwelling_primary_energy, $target_primary_energy);
	$table_1b = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>1b Target primary energy rate and dwelling primary energy</b></th>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3"><i>Target primary energy</i></td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $target_primary_energy . ' kWh<sub>PE</sub>/m<sup>2</sup></i></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3">Dwelling primary energy</td>' . "\n\t\t\t\t" . '<td colspan="2">' . $dwelling_primary_energy . ' kWh<sub>PE</sub>/m<sup>2</sup></td>' . "\n\t\t\t\t" . '<td style="color:' . $primary_energy_check['colour'] . '"><b>' . $primary_energy_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 4, $table_1b, 0, 1, 0, true, 'L', true);

	switch ($country_code) {
	case 'ENG':
		if (!empty($SAP_data['Energy-Assessment']['Energy-Use']['TFEE'])) {
			$target_fabric_efficiency = $SAP_data['Energy-Assessment']['Energy-Use']['TFEE'];
		}
		else {
			throw new Exception('"TFEE" element is missing.');
		}

		if (!empty($SAP_data['Energy-Assessment']['Energy-Use']['DFEE'])) {
			$dwelling_fabric_efficiency = $SAP_data['Energy-Assessment']['Energy-Use']['DFEE'];
		}
		else {
			throw new Exception('"DFEE" element is missing.');
		}

		$fabric_efficiency_check = check_compliance($dwelling_fabric_efficiency, $target_fabric_efficiency);
		$table_1c = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>1c Target fabric energy efficiency and dwelling fabric energy efficiency</b></th>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3"><i>Target fabric energy efficiency</i></td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $target_fabric_efficiency . ' kWh/m<sup>2</sup></i></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3">Dwelling fabric energy efficiency</td>' . "\n\t\t\t\t" . '<td colspan="2">' . $dwelling_fabric_efficiency . ' kWh/m<sup>2</sup></td>' . "\n\t\t\t\t" . '<td style="color:' . $fabric_efficiency_check['colour'] . '"><b>' . $fabric_efficiency_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
		break;
	case 'WLS':
		$dwelling_energy_cost_rating = $SAP_data['Energy-Assessment']['Energy-Use']['Energy-Rating-Current'];
		$minimum_energy_cost_rating = 81;
		$energy_cost_check = check_cost_rating($dwelling_energy_cost_rating, $minimum_energy_cost_rating);
		$table_1c = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>1c Minimum energy efficiency rating and dwelling energy efficiency rating</b></th>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3"><i>Minimum energy efficiency rating</i></td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $minimum_energy_cost_rating . '</i></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3">Dwelling energy efficiency rating</td>' . "\n\t\t\t\t" . '<td colspan="2">' . $dwelling_energy_cost_rating . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $energy_cost_check['colour'] . '"><b>' . $energy_cost_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
		break;
	default:
		throw new Exception('"Country-Code" is invalid.');
		break;
	}

	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 4, $table_1c, 0, 1, 0, true, 'L', true);

	switch ($country_code) {
	case 'ENG':
		$eWalls_limit_U_value = 0.26;
		$pWalls_limit_U_value = 0.2;
		$cWalls_limit_U_value = 1.6;
		$floors_limit_U_value = 0.18;
		$roofs_limit_U_value = 0.16;
		$openings_limit_U_value = 1.6;
		$rooflights_limit_U_value = 2.2;
		break;
	case 'WLS':
		if (($property_type == 'Flat') || ($property_type == 'Maisonette')) {
			$eWalls_limit_U_value = 0.21;
		}
		else {
			$eWalls_limit_U_value = 0.18;
		}

		$pWalls_limit_U_value = 0.2;
		$cWalls_limit_U_value = 1.4;
		$floors_limit_U_value = 0.15;
		$roofs_limit_U_value = 0.13;
		$openings_limit_U_value = 1.4;
		$rooflights_limit_U_value = 2.2;
		break;
	default:
		throw new Exception('"Country-Code" is invalid.');
		break;
	}

	$walls = obtainWALLS($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Building-Parts']);
	$floors = obtainFLOORS($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Building-Parts']);
	$roofs = obtainROOFS($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Building-Parts']);
	$openings = obtainOPENINGS($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Building-Parts']);
	$opening_types = [];

	if (!empty($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Opening-Types'])) {
		$opening_types = obtainOPENING_TYPES($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Opening-Types']);
	}

	calc_NetArea($walls, $roofs, $openings);
	set_OpeningSpecifications($openings, $opening_types);
	$eWalls_average_U_value = -5555;
	$eWalls_maximum_U_value = -5555;
	$eWalls_maximum_U_name = '';
	calc_E_WALLS($walls, $eWalls_average_U_value, $eWalls_maximum_U_value, $eWalls_maximum_U_name);
	$eWalls_check = check_U_value($eWalls_average_U_value, $eWalls_limit_U_value);
	$pWalls_average_U_value = -5555;
	$pWalls_maximum_U_value = -5555;
	$pWalls_maximum_U_name = '';
	calc_P_WALLS($walls, $pWalls_average_U_value, $pWalls_maximum_U_value, $pWalls_maximum_U_name);
	$pWalls_check = check_U_value($pWalls_average_U_value, $pWalls_limit_U_value);
	$cWalls_average_U_value = -5555;
	$cWalls_maximum_U_value = -5555;
	$cWalls_maximum_U_name = '';
	calc_C_WALLS($walls, $cWalls_average_U_value, $cWalls_maximum_U_value, $cWalls_maximum_U_name);
	$cWalls_check = check_U_value($cWalls_average_U_value, $cWalls_limit_U_value);
	$floors_average_U_value = -5555;
	$floors_maximum_U_value = -5555;
	$floors_maximum_U_name = '';
	calc_FLOORS($floors, $floors_average_U_value, $floors_maximum_U_value, $floors_maximum_U_name);
	$floors_check = check_U_value($floors_average_U_value, $floors_limit_U_value);
	$roofs_average_U_value = -5555;
	$roofs_maximum_U_value = -5555;
	$roofs_maximum_U_name = '';
	calc_ROOFS($roofs, $roofs_average_U_value, $roofs_maximum_U_value, $roofs_maximum_U_name);
	$roofs_check = check_U_value($roofs_average_U_value, $roofs_limit_U_value);
	$openings_average_U_value = -5555;
	$openings_maximum_U_value = -5555;
	$openings_maximum_U_name = '';
	calc_OPENINGS($openings, $openings_average_U_value, $openings_maximum_U_value, $openings_maximum_U_name);
	$openings_check = check_U_value($openings_average_U_value, $openings_limit_U_value);
	$rooflights_average_U_value = -5555;
	$rooflights_maximum_U_value = -5555;
	$rooflights_maximum_U_name = '';
	calc_ROOFLIGHTS($openings, $rooflights_average_U_value, $rooflights_maximum_U_value, $rooflights_maximum_U_name);
	$rooflights_check = check_U_value($rooflights_average_U_value, $rooflights_limit_U_value);
	$table_2a = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="12"><b>2a Fabric U-values</b></th>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="2"><b>Element</b></td>' . "\n\t\t\t\t" . '<td colspan="3"><b><i>Maximum permitted average U-Value [W/m<sup>2</sup>K]</i></b></td>' . "\n\t\t\t\t" . '<td colspan="3"><b>Dwelling average U-Value [W/m<sup>2</sup>K]</b></td>' . "\n\t\t\t\t" . '<td colspan="3"><b>Element with highest individual U-Value</b></td>' . "\n\t\t\t\t" . '<td></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="2">External walls</td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $eWalls_limit_U_value . '</i></td>' . "\n\t\t\t\t" . '<td colspan="3">' . $eWalls_average_U_value . '</td>' . "\n\t\t\t\t" . '<td colspan="3">' . $eWalls_maximum_U_name . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $eWalls_check['colour'] . '"><b>' . $eWalls_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="2">Party walls</td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $pWalls_limit_U_value . '</i></td>' . "\n\t\t\t\t" . '<td colspan="3">' . $pWalls_average_U_value . '</td>' . "\n\t\t\t\t" . '<td colspan="3">' . $pWalls_maximum_U_name . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $pWalls_check['colour'] . '"><b>' . $pWalls_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="2">Curtain walls</td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $cWalls_limit_U_value . '</i></td>' . "\n\t\t\t\t" . '<td colspan="3">' . $pWalls_average_U_value . '</td>' . "\n\t\t\t\t" . '<td colspan="3">' . $cWalls_maximum_U_name . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $cWalls_check['colour'] . '"><b>' . $cWalls_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="2">Floors</td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $floors_limit_U_value . '</i></td>' . "\n\t\t\t\t" . '<td colspan="3">' . $floors_average_U_value . '</td>' . "\n\t\t\t\t" . '<td colspan="3">' . $floors_maximum_U_name . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $floors_check['colour'] . '"><b>' . $floors_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="2">Roofs</td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $roofs_limit_U_value . '</i></td>' . "\n\t\t\t\t" . '<td colspan="3">' . $roofs_average_U_value . '</td>' . "\n\t\t\t\t" . '<td colspan="3">' . $roofs_maximum_U_name . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $roofs_check['colour'] . '"><b>' . $roofs_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="2">Windows, doors, and roof windows</td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $openings_limit_U_value . '</i></td>' . "\n\t\t\t\t" . '<td colspan="3">' . $openings_average_U_value . '</td>' . "\n\t\t\t\t" . '<td colspan="3">' . $openings_maximum_U_name . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $openings_check['colour'] . '"><b>' . $openings_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="2">Rooflights</td>' . "\n\t\t\t\t" . '<td colspan="3"><i>' . $rooflights_limit_U_value . '</i></td>' . "\n\t\t\t\t" . '<td colspan="3">' . $rooflights_average_U_value . '</td>' . "\n\t\t\t\t" . '<td colspan="3">' . $rooflights_maximum_U_name . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $rooflights_check['colour'] . '"><b>' . $rooflights_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_2a, 0, 1, 0, true, 'L', true);
	$too_good_text = ' (better than typically expected values are flagged with a subsequent (!))';

	switch ($country_code) {
	case 'ENG':
		$eWalls_best_U_value = 0.15;
		$pWalls_best_U_value = 0.2;
		$floors_best_U_value = 0.11;
		$roofs_best_U_value = 0.11;
		$openings_best_U_value = 1.2;
		$air_permeability_best = 4;
		$thermal_bridge_best_psi = 0.04;
		break;
	case 'WLS':
		$eWalls_best_U_value = 0.13;
		$pWalls_best_U_value = 0.2;
		$floors_best_U_value = 0.11;
		$roofs_best_U_value = 0.11;
		$openings_best_U_value = 1.2;
		$air_permeability_best = 4;
		$thermal_bridge_best_psi = 0.04;
		break;
	default:
		throw new Exception('"Country-Code" is invalid.');
		break;
	}

	$table_2b = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>2b Envelope elements' . $too_good_text . '</b></th>' . "\n\t\t\t" . '  </tr>' . "\n" . '            <thead>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="4"><b>Name</b></td>' . "\n\t\t\t\t" . '<td><b>Net area [m<sup>2</sup>]</b></td>' . "\n\t\t\t\t" . '<td><b>U-Value [W/m<sup>2</sup>K]</b></td>' . "\n\t\t\t" . '  </tr>' . "\n" . '            </thead>';

	for ($i = 0; $i < count($walls); $i++) {
		if (($walls[$i]['Type'] == 'Basement wall') || ($walls[$i]['Type'] == 'Exposed wall') || ($walls[$i]['Type'] == 'Sheltered wall') || ($walls[$i]['Type'] == 'Party wall')) {
			$walls_name = $walls[$i]['Name'];

			if (!empty($walls[$i]['Description'])) {
				$walls_name .= ', ' . $walls[$i]['Description'];
			}

			$too_good_flag = '';

			if ($walls[$i]['Type'] == 'Party wall') {
				if ($walls[$i]['U-value'] < $pWalls_best_U_value) {
					$too_good_flag = '&nbsp;&nbsp;<b>(!)</b>';
				}
			}
			else if ($walls[$i]['U-value'] < $eWalls_best_U_value) {
				$too_good_flag = '&nbsp;&nbsp;<b>(!)</b>';
			}

			$table_2b .= '  ' . "\t\t\t" . '    <tr>' . "\n" . '  ' . "\t\t\t\t" . '  <td colspan="4">' . $walls[$i]['Type'] . ': ' . $walls_name . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $walls[$i]['Net-Area'] . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $walls[$i]['U-value'] . $too_good_flag . '</td>' . "\n" . '   ' . "\t\t\t" . '    </tr>';
		}
	}

	for ($i = 0; $i < count($floors); $i++) {
		if (0.0 < $floors[$i]['Heat-Loss-Area']) {
			$floors_name = $floors[$i]['Name'];

			if (!empty($floors[$i]['Description'])) {
				$floors_name .= ', ' . $floors[$i]['Description'];
			}

			$too_good_flag = '';

			if ($floors[$i]['U-value'] < $floors_best_U_value) {
				$too_good_flag = '&nbsp;&nbsp;<b>(!)</b>';
			}

			$table_2b .= '  ' . "\t\t\t" . '    <tr>' . "\n" . '  ' . "\t\t\t\t" . '  <td colspan="4">' . $floors[$i]['Type'] . ': ' . $floors_name . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $floors[$i]['Heat-Loss-Area'] . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $floors[$i]['U-value'] . $too_good_flag . '</td>' . "\n" . '   ' . "\t\t\t" . '    </tr>';
		}
	}

	for ($i = 0; $i < count($roofs); $i++) {
		if ($roofs[$i]['Type'] == 'Exposed roof') {
			$roofs_name = $roofs[$i]['Name'];

			if (!empty($roofs[$i]['Description'])) {
				$roofs_name .= ', ' . $roofs[$i]['Description'];
			}

			$too_good_flag = '';

			if ($roofs[$i]['U-value'] < $roofs_best_U_value) {
				$too_good_flag = '&nbsp;&nbsp;<b>(!)</b>';
			}

			$table_2b .= '  ' . "\t\t\t" . '    <tr>' . "\n" . '  ' . "\t\t\t\t" . '  <td colspan="4">' . $roofs[$i]['Type'] . ': ' . $roofs_name . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $roofs[$i]['Net-Area'] . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $roofs[$i]['U-value'] . $too_good_flag . '</td>' . "\n" . '   ' . "\t\t\t" . '    </tr>';
		}
	}

	$table_2b .= "\t\t\t" . '     </table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_2b, 0, 1, 0, true, 'L', true);
	$table_2c = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n" . ' ' . "\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>2c Openings' . $too_good_text . '</b></th>' . "\n\t\t\t" . '  </tr>' . "\n" . '            <thead>' . "\n" . ' ' . "\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="2"><b>Name</b></td>' . "\n\t\t\t\t" . '<td><b>Area [m<sup>2</sup>]</b></td>' . "\n\t\t\t\t" . '<td><b>Orientation</b></td>' . "\n\t\t\t\t" . '<td><b>Frame factor</b></td>' . "\n\t\t\t\t" . '<td><b>U-Value [W/m<sup>2</sup>K]</b></td>' . "\n\t\t\t" . '  </tr>' . "\n" . '            </thead>';

	for ($i = 0; $i < count($openings); $i++) {
		$opening_name = $openings[$i]['Name'] . ', ' . $openings[$i]['Opening-Type'];

		if (!empty($openings[$i]['Description'])) {
			$opening_name .= ', ' . $openings[$i]['Description'];
		}

		$too_good_flag = '';

		if ($openings[$i]['Type'] != 'Rooflight') {
			if ($openings[$i]['U-value'] < $openings_best_U_value) {
				$too_good_flag = '&nbsp;&nbsp;<b>(!)</b>';
			}
		}

		$table_2c .= '  ' . "\t\t\t" . '    <tr>' . "\n" . '  ' . "\t\t\t\t" . '  <td colspan="2">' . $opening_name . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $openings[$i]['Area'] . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $openings[$i]['Orientation'] . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $openings[$i]['Frame-Factor'] . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $openings[$i]['U-value'] . $too_good_flag . '</td>' . "\n" . '   ' . "\t\t\t" . '    </tr>';
	}

	$table_2c .= "\t\t\t" . '     </table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_2c, 0, 1, 0, true, 'L', true);
	$building_parts = obtainBUILDING_PARTS($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Building-Parts']);
	$thermal_bridges = obtainTHERMAL_BRIDGES($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Building-Parts']);

	for ($i = 0; $i < count($building_parts); $i++) {
		$bpName = $building_parts[$i]['Number'];

		if (!empty($building_parts[$i]['Identifier'])) {
			$bpName .= ' - ' . $building_parts[$i]['Identifier'];
		}

		if ($i == 0) {
			$table_2d_Heading = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left"><b>2d Thermal bridging' . $too_good_text . '</b></th>' . "\n\t\t\t" . '  </tr>';
			$y_ref = $pdf->getY() - 2;
		}
		else {
			$table_2d_Heading = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">';
			$y_ref = $pdf->getY() - 4;
		}

		if ($building_parts[$i]['Thermal-Bridging-Type'] == 'Default y-value') {
			$table_2d_Heading .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="7">Building part <b>' . $bpName . '</b>: SAP default y-value (0.2 W/m<sup>2</sup>K) used for thermal bridging</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
			$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref, $table_2d_Heading, 0, 1, 0, true, 'L', true);
			continue;
		}

		$table_2d_Heading .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="7">Building part <b>' . $bpName . '</b>: Thermal bridging calculated from linear thermal transmittances for each junction</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref, $table_2d_Heading, 0, 1, 0, true, 'L', true);
		$table_2d = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n" . '              <thead>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td><b>Main element</b></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2"><b>Junction detail</b></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2"><b>Source</b></td>' . "\n" . '  ' . "\t\t\t\t" . '<td><b>Psi value [W/mK]</b></td>' . "\n" . '  ' . "\t\t\t\t" . '<td><b>Drawing / reference</b></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '              </thead>';
		$bp = $building_parts[$i]['Number'];

		for ($k = 0; $k < count($thermal_bridges[$bp]); $k++) {
			$too_good_flag = '';

			if (abs($thermal_bridges[$bp][$k]['Psi']) < $thermal_bridge_best_psi) {
				$too_good_flag = '&nbsp;&nbsp;<b>(!)</b>';
			}

			$table_2d .= '  ' . "\t\t\t" . '    <tr>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $thermal_bridges[$bp][$k]['Location'] . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td colspan="2">' . $thermal_bridges[$bp][$k]['Type'] . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td colspan="2">' . $thermal_bridges[$bp][$k]['Source'] . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $thermal_bridges[$bp][$k]['Psi'] . $too_good_flag . '</td>' . "\n" . '  ' . "\t\t\t\t" . '  <td>' . $thermal_bridges[$bp][$k]['Reference'] . '</td>' . "\n" . '   ' . "\t\t\t" . '    </tr>';
		}

		$table_2d .= "\t\t\t" . '     </table>';
		$y_ref = $pdf->getY();
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 4, $table_2d, 0, 1, 0, true, 'L', true);
	}

	$ventilation = [];

	if (!empty($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Ventilation'])) {
		$ventilation = addVENTILATION($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Ventilation']);
	}
	else {
		throw new Exception('"SAP-Ventilation" element is missing.');
	}

	$limit_air_permeability = 8;
	$air_permeability_check = check_compliance($ventilation['Air-Permeability'], $limit_air_permeability);
	$value_source = $ventilation['Value-Source'];

	if ($ventilation['Value-Source'] == 'Measured value - low-pressure pulse') {
		$value_source .= ' (' . $ventilation['Air-Permeability-4Pa'] . ' m<sup>3</sup>/hm<sup>2</sup> at 4Pa)';
	}

	$too_good_flag = '';

	if ($ventilation['Air-Permeability'] < $air_permeability_best) {
		$too_good_flag = '&nbsp;&nbsp;<b>(!)</b>';
	}

	$table_3 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="7"><b>3 Air permeability' . $too_good_text . '</b></th>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3"><i>Maximum permitted air permeability at 50Pa</i></td>' . "\n\t\t\t\t" . '<td colspan="4"><i>' . $limit_air_permeability . ' m<sup>3</sup>/hm<sup>2</sup></i></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3">Dwelling air permeability at 50Pa</td>' . "\n\t\t\t\t" . '<td colspan="3">' . $ventilation['Air-Permeability'] . ' m<sup>3</sup>/hm<sup>2</sup>, ' . $value_source . $too_good_flag . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $air_permeability_check['colour'] . '"><b>' . $air_permeability_check['status'] . '</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="3">Air permeability test certificate reference</td>' . "\n\t\t\t\t" . '<td colspan="4">' . $ventilation['Test-Certificate'] . '</td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_3, 0, 1, 0, true, 'L', true);
	$main_heating_systems = obtainMAIN_HEATING($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Heating']['Main-Heating-Details']);

	for ($i = 0; $i < count($main_heating_systems); $i++) {
		if ($main_heating_systems[$i]['Efficiency'] != '') {
			$main_heating_systems[$i]['Efficiency'] .= '%';
		}

		if ($i == 0) {
			$table_4 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="3"><b>4 Space heating</b></th>' . "\n\t\t\t" . '  </tr>';
			$y_ref = $pdf->getY() - 2;
		}
		else {
			$table_4 = '            <br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">';
			$y_ref = $pdf->getY() - 4;
		}

		$table_4 .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><b>Main heating system ' . $main_heating_systems[$i]['Number'] . '</b>: ' . $main_heating_systems[$i]['Category'] . ' - ' . $main_heating_systems[$i]['Fuel-Type'] . '</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Efficiency</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Efficiency'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Emitter type</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Emitter-Type'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Flow temperature</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Distribution-Temperature'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">System type</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['System-Type'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Manufacturer</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Manufacturer-Brand'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Model</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Model-Identifier'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Commissioning</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Commissioning'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n\t\t" . '   </table>';
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref, $table_4, 0, 1, 0, true, 'L', true);
	}

	$secondary_heating_system = addSECONDARY_HEATING($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Heating']);

	if ($secondary_heating_system['Efficiency'] != 'N/A') {
		$secondary_heating_system['Efficiency'] .= '%';
	}

	$table_4 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><b>Secondary heating system</b>: ' . $secondary_heating_system['System-Type'] . '</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Fuel</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $secondary_heating_system['Fuel-Type'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Efficiency</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $secondary_heating_system['Efficiency'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Commissioning</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $secondary_heating_system['Commissioning'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n\t\t" . '   </table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 4, $table_4, 0, 1, 0, true, 'L', true);
	$water_heating_system = addWATER_HEATING($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Heating']);
	$heatingDetails = $SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Heating'];
	$isHIUSelected = !empty($heatingDetails['HIU-Index-Number']) ||
		(!empty($heatingDetails['Is-HIU-In-Dwelling']) &&
			($heatingDetails['Is-HIU-In-Dwelling'] == 'true' || $heatingDetails['Is-HIU-In-Dwelling'] === true));

	if (!$isHIUSelected && !empty($heatingDetails['SAP-Community-Heating-Systems']['SAP-Community-Heating-System'])) {
		$communitySystems = $heatingDetails['SAP-Community-Heating-Systems']['SAP-Community-Heating-System'];
		if (empty($communitySystems[0])) {
			$communitySystems = [$communitySystems];
		}

		foreach ($communitySystems as $communitySystem) {
			$isHIUSelected = !empty($communitySystem['HIU-Index-Number']) ||
				(!empty($communitySystem['Is-HIU-In-Dwelling']) &&
					($communitySystem['Is-HIU-In-Dwelling'] == 'true' || $communitySystem['Is-HIU-In-Dwelling'] === true));
			if ($isHIUSelected) {
				break;
			}
		}
	}

	if ($isHIUSelected) {
		$water_heating_system['Control-Model-Identifier'] = 'HIU';
	}

	if ($water_heating_system['Store-Heat-Loss'] != 'N/A') {
		$water_heating_system['Store-Heat-Loss'] .= ' kWh/day';
	}

	$table_5 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="3"><b>5 Hot water</b></th>' . "\n\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><b>Cylinder/store</b> - type: ' . $water_heating_system['Store-Type'] . '</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Capacity</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $water_heating_system['Store-Size'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Declared heat loss</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $water_heating_system['Store-Heat-Loss'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Primary pipework insulated</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $water_heating_system['Primary-Pipework-Insulation'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Manufacturer</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $water_heating_system['Store-Manufacturer-Brand'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Model</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $water_heating_system['Store-Model-Identifier'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Commissioning</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $water_heating_system['Commissioning'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_5, 0, 1, 0, true, 'L', true);
	$wwhrs = obtainWWHRS($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Heating']);

	for ($i = 0; $i < count($wwhrs); $i++) {
		$number = $i + 1;

		if ($wwhrs[$i]['Efficiency'] != '') {
			$wwhrs[$i]['Efficiency'] .= '%';
		}

		$table_5 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><b>Waste water heat recovery system ' . $number . '</b> - type: ' . $wwhrs[$i]['Type'] . '</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Efficiency</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $wwhrs[$i]['Efficiency'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Manufacturer</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $wwhrs[$i]['Manufacturer-Brand'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Model</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $wwhrs[$i]['Model-Identifier'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . ' ' . "\t\t" . '    </table>';
		$y_ref = $pdf->getY();
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 4, $table_5, 0, 1, 0, true, 'L', true);
	}

	for ($i = 0; $i < count($main_heating_systems); $i++) {
		if ($i == 0) {
			$table_6 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="3"><b>6 Controls</b></th>' . "\n\t\t\t" . '  </tr>';
			$y_ref = $pdf->getY() - 2;
		}
		else {
			$table_6 = '            <br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">';
			$y_ref = $pdf->getY() - 4;
		}

		$table_6 .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><b>Main heating ' . $main_heating_systems[$i]['Number'] . '</b> - type: ' . $main_heating_systems[$i]['Control-Type'] . '</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Function</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Controller-Function'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Ecodesign class</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Controller-Ecodesign-Class'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Manufacturer</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Control-Manufacturer-Brand'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Model</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $main_heating_systems[$i]['Control-Model-Identifier'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n\t\t" . '   </table>';
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref, $table_6, 0, 1, 0, true, 'L', true);
	}

	$hot_water_controls = 'N/A';

	if ($water_heating_system['Cylinder-Thermostat'] != 'N/A') {
		$hot_water_controls = $water_heating_system['Cylinder-Thermostat'];
	}

	if ($water_heating_system['Separately-Timed'] != 'N/A') {
		if ($hot_water_controls == 'N/A') {
			$hot_water_controls = $water_heating_system['Separately-Timed'];
		}
		else {
			$hot_water_controls .= ' and ' . $water_heating_system['Separately-Timed'];
		}
	}

	if ($water_heating_system['Store-Type'] == 'N/A' && $water_heating_system['Control-Model-Identifier'] != 'HIU') {
		$hot_water_controls = 'N/A';
	}

	$isCombiBoiler = false;
	for ($i = 0; $i < count($main_heating_systems); $i++) {
		if (!empty($main_heating_systems[$i]['System-Type']) && $main_heating_systems[$i]['System-Type'] == 'Combi boiler') {
			$isCombiBoiler = true;
			break;
		}
	}

	if ($isCombiBoiler && $water_heating_system['Store-Size'] == 'N/A') {
		$hot_water_controls = 'Combi boiler';
	}

	if (!empty($heatingDetails['Has-Hot-Water-Cylinder']) &&
		($heatingDetails['Has-Hot-Water-Cylinder'] == 'true' || $heatingDetails['Has-Hot-Water-Cylinder'] === true)) {
		$hot_water_controls = 'N/A';
	}

	$table_6 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><b>Water heating</b> - type: ' . $hot_water_controls . '</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Manufacturer</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $water_heating_system['Control-Manufacturer-Brand'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Model</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $water_heating_system['Control-Model-Identifier'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n\t\t" . '   </table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 4, $table_6, 0, 1, 0, true, 'L', true);
	$lighting = obtainLIGHTING($SAP_data['SAP10-Data']['SAP-Property-Details']);
	$limit_lamp_efficacy = 75;
	$luminous_efficacy_check = check_efficiency($lighting['Minimum-Lamp-Efficacy'], $limit_lamp_efficacy);
	$limit_lamp_efficacy .= ' lm/W';

	if ($lighting['Minimum-Lamp-Efficacy'] != '') {
		$lighting['Minimum-Lamp-Efficacy'] .= ' lm/W';
	}

	$table_7 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>7 Lighting</b></th>' . "\n\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2"><i>Minimum permitted light source efficacy</i></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="4"><i>' . $limit_lamp_efficacy . '</i></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">Lowest light source efficacy</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $lighting['Minimum-Lamp-Efficacy'] . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $luminous_efficacy_check['colour'] . '"><b>' . $luminous_efficacy_check['status'] . '</b></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">External lights control</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="4">' . $lighting['External-Light-Control'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_7, 0, 1, 0, true, 'L', true);
	$limit_specific_fan_power = 'N/A';
	$limit_heat_recovery_efficiency = 'N/A';

	if ($ventilation['Type'] != 'N/A') {
		switch ($ventilation['Type-Code']) {
		case '1':
			$limit_specific_fan_power = '0.5';
			break;
		case '4':
			$limit_specific_fan_power = '0.5';
			break;
		case '5':
		case '6':
			$limit_specific_fan_power = '0.7';
			break;
		case '7':
			$limit_specific_fan_power = '1.5';
			break;
		case '8':
			$limit_specific_fan_power = '1.5';
			$limit_heat_recovery_efficiency = '73';
			break;
		}
	}

	$specific_fan_power_check = check_specific_fan_power($ventilation['Specific-Fan-Power'], $limit_specific_fan_power);
	$heat_recovery_efficiency_check = check_efficiency($ventilation['Heat-Recovery-Efficiency'], $limit_heat_recovery_efficiency);

	if ($ventilation['Specific-Fan-Power'] != 'N/A') {
		$ventilation['Specific-Fan-Power'] .= ' W/(l/s)';
	}

	if ($limit_specific_fan_power != 'N/A') {
		$limit_specific_fan_power .= ' W/(l/s)';
	}

	if ($ventilation['Heat-Recovery-Efficiency'] != 'N/A') {
		$ventilation['Heat-Recovery-Efficiency'] .= '%';
	}

	if ($limit_heat_recovery_efficiency != 'N/A') {
		$limit_heat_recovery_efficiency .= '%';
	}

	$table_8 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>8 Mechanical ventilation</b></th>' . "\n\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="6"><b>System type: </b>' . $ventilation['Type'] . '</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2"><i>Maximum permitted specific fan power</i></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="4"><i>' . $limit_specific_fan_power . '</i></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">Specific fan power</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $ventilation['Specific-Fan-Power'] . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $specific_fan_power_check['colour'] . '"><b>' . $specific_fan_power_check['status'] . '</b></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2"><i>Minimum permitted heat recovery efficiency</i></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="4"><i>' . $limit_heat_recovery_efficiency . '</i></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">Heat recovery efficiency</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $ventilation['Heat-Recovery-Efficiency'] . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $heat_recovery_efficiency_check['colour'] . '"><b>' . $heat_recovery_efficiency_check['status'] . '</b></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">Manufacturer/Model</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="4">' . $ventilation['Make-Model'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">Commissioning</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="4">' . $ventilation['Commissioning'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_8, 0, 1, 0, true, 'L', true);
	$solar_heating = obtainSOLAR_HEATING($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Heating']);

	for ($i = 0; $i < count($solar_heating); $i++) {
		$number = $i + 1;

		if ($i == 0) {
			$table_9 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="3"><b>9 Local generation</b></th>' . "\n\t\t\t" . '  </tr>';
			$y_ref = $pdf->getY() - 2;
		}
		else {
			$table_9 = '            <br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">';
			$y_ref = $pdf->getY() - 4;
		}

		$table_9 .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Technology type: <b>Solar thermal system (' . $number . ')</b></td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Service provision</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $solar_heating[$i]['Service'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Collector type</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $solar_heating[$i]['Type'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '   ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Aperture area</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $solar_heating[$i]['Area'] . ' m<sup>2</sub></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Orientation</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $solar_heating[$i]['Orientation'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Pitch</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $solar_heating[$i]['Pitch'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Overshading</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $solar_heating[$i]['Overshading'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Manufacturer</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $solar_heating[$i]['Manufacturer'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Certificate</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $solar_heating[$i]['Certificate'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . ' ' . "\t\t" . '    </table>';
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref, $table_9, 0, 1, 0, true, 'L', true);
	}

	$pv = [];

	if (!empty($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Energy-Source']['PV-Arrays'])) {
		$pv = obtainPV($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Energy-Source']['PV-Arrays']);
	}

	for ($i = 0; $i < count($pv); $i++) {
		$number = $i + 1;
		if (($i == 0) && empty($solar_heating)) {
			$table_9 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="3"><b>9 Local generation</b></th>' . "\n\t\t\t" . '  </tr>';
			$y_ref = $pdf->getY() - 2;
		}
		else {
			$table_9 = '            <br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">';
			$y_ref = $pdf->getY() - 4;
		}

		$table_9 .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Technology type: <b>Photovoltaic system (' . $number . ')</b></td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Peak power</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $pv[$i]['Peak-Power'] . ' kWp</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Orientation</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $pv[$i]['Orientation'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Pitch</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $pv[$i]['Pitch'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Overshading</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $pv[$i]['Overshading'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Manufacturer</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $pv[$i]['Manufacturer'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">MCS certificate</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $pv[$i]['Certificate'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . ' ' . "\t\t" . '    </table>';
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref, $table_9, 0, 1, 0, true, 'L', true);
	}

	$wind_turbines = [];

	if (!empty($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Energy-Source']['Wind-Turbines'])) {
		$wind_turbines = obtainWIND_TURBINE($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Energy-Source']['Wind-Turbines']);
	}

	for ($i = 0; $i < count($wind_turbines); $i++) {
		$number = $i + 1;
		if (($i == 0) && empty($solar_heating) && empty($pv)) {
			$table_9 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="3"><b>9 Local generation</b></th>' . "\n\t\t\t" . '  </tr>';
			$y_ref = $pdf->getY() - 2;
		}
		else {
			$table_9 = '            <br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">';
			$y_ref = $pdf->getY() - 4;
		}

		$table_9 .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Technology type: <b>Wind turbine (' . $number . ')</b></td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Rotor diameter</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $wind_turbines[$i]['Rotor-Diameter'] . ' m</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Hub height</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $wind_turbines[$i]['Hub-Height'] . ' m</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Manufacturer</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $wind_turbines[$i]['Manufacturer'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Certificate</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $wind_turbines[$i]['Certificate'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . ' ' . "\t\t" . '    </table>';
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref, $table_9, 0, 1, 0, true, 'L', true);
	}

	$hydro = [];

	if (!empty($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Energy-Source'])) {
		$hydro = addHYDRO_ELECTRIC($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Energy-Source']);
	}
	if (empty($solar_heating) && empty($pv) && empty($wind_turbines)) {
		$table_9 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="3"><b>9 Local generation</b></th>' . "\n\t\t\t" . '  </tr>';
		$y_ref = $pdf->getY() - 2;
	}
	else {
		$table_9 = '            <br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">';
		$y_ref = $pdf->getY() - 4;
	}

	if (!empty($hydro)) {
		$table_9 .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Technology type: <b>Hydro-electric generator</b></td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Annual electricity generation</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $hydro['Electric-Generation'] . ' kWh</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="1">Certificate</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $hydro['Certificate'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . ' ' . "\t\t" . '    </table>';
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref, $table_9, 0, 1, 0, true, 'L', true);
	}
	if (empty($solar_heating) && empty($pv) && empty($wind_turbines) && empty($hydro)) {
		$table_9 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left"><b>9 Local generation</b></th>' . "\n\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td>N/A</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . ' ' . "\t\t" . '    </table>';
		$y_ref = $pdf->getY();
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_9, 0, 1, 0, true, 'L', true);
	}

	$heat_networks = [];

	if (!empty($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Heating']['SAP-Community-Heating-Systems'])) {
		$heat_networks = obtainHEAT_NETWORK($SAP_data['SAP10-Data']['SAP-Property-Details']['SAP-Heating']['SAP-Community-Heating-Systems']);
	}

	$maximum_network_emission_factor = [];
	$maximum_network_primary_energy_factor = [];
	$heat_network_emission_check = [];
	$heat_network_primary_energy_check = [];

	for ($i = 0; $i < count($heat_networks); $i++) {
		$maximum_network_emission_factor[$i] = 0.35;
		$heat_network_emission_check[$i] = check_compliance($heat_networks[$i]['Emission-Factor'], $maximum_network_emission_factor[$i]);
		$heat_networks[$i]['Emission-Factor'] .= ' kgCO<sub>2</sub>/kWh';
		$heat_networks[$i]['Primary-Energy-Factor'] .= ' kWh<sub>PE</sub>/kWh';
		$maximum_network_emission_factor[$i] .= ' kgCO<sub>2</sub>/kWh';

		if ($i == 0) {
			$table_10 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>10 Heat networks</b></th>' . "\n\t\t\t" . '  </tr>';
			$y_ref = $pdf->getY() - 2;
		}
		else {
			$table_10 = '            <br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">';
			$y_ref = $pdf->getY() - 4;
		}
		if (($heat_networks[$i]['Status'] == 'New heat network') || ($country_code == 'WLS')) {
			$table_10 .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="6"><b>Network name</b>: ' . $heat_networks[$i]['Name'] . '</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Service provision</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $heat_networks[$i]['Service'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Status</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $heat_networks[$i]['Status'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Carbon dioxide emission factor for delivered heat</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $heat_networks[$i]['Emission-Factor'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Primary energy factor for delivered heat</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $heat_networks[$i]['Primary-Energy-Factor'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n\t\t" . '   </table>';
		}
		else {
			$table_10 .= '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="6"><b>Network name</b>: ' . $heat_networks[$i]['Name'] . '</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Service provision</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $heat_networks[$i]['Service'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Status</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $heat_networks[$i]['Status'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><i>Maximum permitted carbon dioxide emission factor</i></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><i>' . $maximum_network_emission_factor[$i] . '</i></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Carbon dioxide emission factor for delivered heat</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="2">' . $heat_networks[$i]['Emission-Factor'] . '</td>' . "\n\t\t\t\t" . '<td style="color:' . $heat_network_emission_check[$i]['colour'] . '"><b>' . $heat_network_emission_check[$i]['status'] . '</b></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><i>Maximum permitted primary energy factor</i></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><i>N/A</i></td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">Primary energy factor for delivered heat</td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3">' . $heat_networks[$i]['Primary-Energy-Factor'] . '</td>' . "\n" . '   ' . "\t\t\t" . '  </tr>' . "\n\t\t" . '   </table>';
		}

		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref, $table_10, 0, 1, 0, true, 'L', true);
	}

	if (empty($heat_networks)) {
		$table_10 = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left"><b>10 Heat networks</b></th>' . "\n\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td>N/A</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . ' ' . "\t\t" . '    </table>';
		$y_ref = $pdf->getY();
		$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_10, 0, 1, 0, true, 'L', true);
	}

        $table_11_header = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>11 Supporting documentary evidence</b></th>' . "\n\t\t\t" . '  </tr>';

        if ($assessment_type === 'As built') {
                $table_11_body = "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="5">Documentary evidence identified in 11.1 and 11.2 is needed to confirm the data values used for any calculations undertaken, manufacturer declarations made, and tests performed as reflected in this<br>"As built" ' . $country_doc . ' Compliance Report are correct.<br>&nbsp;11.1 SAP Conventions, Appendix 1 (documentary evidence) schedules the minimum<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;documentary evidence required.<br>&nbsp;11.2 Indicative photographic evidence of key stages during construction (guidance within Approved<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Document L, Volume 1 - Appendix B) that confirms the products identified in this ' . $country_doc . ' Compliance Report are used in this dwelling, and workmanship is of sufficient quality to support<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the calculated values claimed in 2a to 2d.</td>' . "\n\t\t\t\t" . '<td></td>' . "\n\t\t\t" . '  </tr>';
        }
        else {
                $table_11_body = "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="6">N/A</td>' . "\n\t\t\t" . '  </tr>';
        }

        $table_11 = $table_11_header . $table_11_body . "\n\t\t\t" . '</table>';

	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_11, 0, 1, 0, true, 'L', true);
	$y_ref = $pdf->getY();

	if ($assessment_type == 'As built') {
	}

	switch ($assessment_type) {
	case 'As designed':
		$table_12a = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>12 Declarations</b></th>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td style="background-color:rgb(189,214,238)" colspan="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>a. Assessor Declaration</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This declaration by the assessor is confirmation that the contents of this ' . $country_doc . ' ' . "\n" . '                    Compliance Report <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;are a true and accurate reflection based upon the design information submitted ' . "\n" . '                    for this dwelling for<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the purpose of carrying out the "' . $assessment_type . '" assessment, and that ' . "\n" . '                    the supporting documentary <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evidence (SAP Conventions, Appendix 1 (documentary evidence) ' . "\n" . '                    schedules the minimum<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;documentary evidence required) has been reviewed in the course of preparing ' . "\n" . '                    this ' . $country_doc . '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Compliance Report.</td>' . "\n\t\t\t\t" . '<td></td>' . "\n\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><br><br><br>Signed:<br><br><br>Name:<br></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><br><br><br>Assessor ID:<br><br><br>Date:<br></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
		break;
	case 'As built':
		$table_12a = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t" . '    <th style="color:white; background-color:rgb(46,116,181)" align="left" colspan="6"><b>12 Declarations</b></th>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td style="background-color:rgb(189,214,238)" colspan="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>a. Assessor Declaration</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This declaration by the assessor is confirmation that the contents of this ' . $country_doc . ' ' . "\n" . '                    Compliance Report <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;are a true and accurate reflection based upon the design and construction ' . "\n" . '                    information submitted for <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this dwelling for the purpose of carrying out the assessment, and that ' . "\n" . '                    the supporting documentary <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evidence (identified in 11.1 and 11.2) pursuant to Part L of the Building ' . "\n" . '                    Regulations 2010 (as <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amended) has been reviewed in the course of preparing this ' . $country_doc . ' ' . "\n" . '                    Compliance Report.</td>' . "\n\t\t\t\t" . '<td></td>' . "\n\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><br><br><br>Signed:<br><br><br>Name:<br></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><br><br><br>Assessor ID:<br><br><br>Date:<br></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
		break;
	}

	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 2, $table_12a, 0, 1, 0, true, 'L', true);
	$y_ref = $pdf->getY();
        $pdf->SetFont('helvetica', '', 22);
	$pdf->TextField('assessor_signature', 70, 17, [], [], PDF_MARGIN_LEFT + 15, $y_ref - 26);
        $pdf->SetFont('helvetica', '', 9);
	$pdf->TextField('assessor_name', 70, 5, [], [], PDF_MARGIN_LEFT + 15, $y_ref - 8);
	$pdf->TextField('assessor_date', 60, 5, [], [], PDF_MARGIN_LEFT + 110, $y_ref - 8);
	$pdf->TextField('assessor_id', 60, 5, [], [], PDF_MARGIN_LEFT + 110, $y_ref - 20);
	$pdf->TextField('assessor_declaration', 23, 5, [], [], PDF_MARGIN_LEFT + 147, $y_ref - 43);

	switch ($assessment_type) {
	case 'As designed':
		$table_12b = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td style="background-color:rgb(189,214,238)" colspan="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>b. Client Declaration</b></td>' . "\n\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N/A</td>' . "\n" . '  ' . "\t\t\t" . '  </tr>' . "\n" . ' ' . "\t\t" . '    </table>';
		break;
	case 'As built':
		$table_12b = "\t\t\t" . '<br><br>' . "\n\t\t\t" . '<table border="0.5" style="font-size:11px;" nobr="true">' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td style="background-color:rgb(189,214,238)" colspan="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>b. Client Declaration</b></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '  <tr>' . "\n\t\t\t\t" . '<td colspan="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This declaration by the client is confirmation that the dwelling has been constructed ' . "\n" . '                    and completed <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;according to the specifications set out in this ' . $country_doc . ' Compliance Report, ' . "\n" . '                    and that photographic <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evidence of key stages, as described in 11.2, has been provided to the ' . "\n" . '                    Assessor for this dwelling.</td>' . "\n\t\t\t\t" . '<td></td>' . "\n\t\t\t" . '  </tr>' . "\n" . '  ' . "\t\t\t" . '  <tr>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><br><br><br>Signed:<br><br><br>Name:<br></td>' . "\n" . '  ' . "\t\t\t\t" . '<td colspan="3"><br><br><br>Organisation:<br><br><br>Date:<br></td>' . "\n\t\t\t" . '  </tr>' . "\n\t\t\t" . '</table>';
		break;
	}

	$y_ref = $pdf->getY();
	$pdf->writeHTMLCell(0, '', PDF_MARGIN_LEFT, $y_ref - 4, $table_12b, 0, 1, 0, true, 'L', true);

if ($assessment_type == 'As built') {
    $y_ref = $pdf->getY();
        $pdf->SetFont('helvetica', '', 22);
    $pdf->TextField('client_signature', 70, 17, [], [], PDF_MARGIN_LEFT + 15, $y_ref - 26);
        $pdf->SetFont('helvetica', '', 9);
    $pdf->TextField('client_name', 70, 5, ['value' => $client_name], [], PDF_MARGIN_LEFT + 15, $y_ref - 8);
    $pdf->TextField('client_date', 60, 5, [], [], PDF_MARGIN_LEFT + 110, $y_ref - 8);
    $pdf->TextField('client_organisation', 60, 5, ['value' => $client_company], [], PDF_MARGIN_LEFT + 110, $y_ref - 20);
    $pdf->TextField('client_declaration', 23, 5, [], [], PDF_MARGIN_LEFT + 147, $y_ref - 37);
}

	return $pdf->Output('', 'S');
}
