<?php
function calc_NetArea(&$walls, &$roofs, $openings)
{
	for ($i = 0; $i < count($walls); $i++) {
		$walls[$i]['Net-Area'] = $walls[$i]['Gross-Area'];

		for ($j = 0; $j < count($openings); $j++) {
			if ($openings[$j]['Location'] == $walls[$i]['Name']) {
				$walls[$i]['Net-Area'] -= $openings[$j]['Area'];
			}
		}

		if ($walls[$i]['Net-Area'] < 0.0) {
			$walls[$i]['Net-Area'] = 0.0;
		}
	}

	for ($i = 0; $i < count($roofs); $i++) {
		$roofs[$i]['Net-Area'] = $roofs[$i]['Gross-Area'];

		for ($j = 0; $j < count($openings); $j++) {
			if ($openings[$j]['Location'] == $roofs[$i]['Name']) {
				$roofs[$i]['Net-Area'] -= $openings[$j]['Area'];
			}
		}

		if ($roofs[$i]['Net-Area'] < 0.0) {
			$roofs[$i]['Net-Area'] = 0.0;
		}
	}
}

function set_OpeningSpecifications(&$openings, $opening_types)
{
	for ($i = 0; $i < count($openings); $i++) {
		$openings[$i]['U-value'] = 0.0;
		$openings[$i]['Description'] = '';

		for ($j = 0; $j < count($opening_types); $j++) {
			if ($openings[$i]['Opening-Type'] == $opening_types[$j]['Name']) {
				$openings[$i]['U-value'] = $opening_types[$j]['U-value'];
				$openings[$i]['Type'] = $opening_types[$j]['Type'];
				$openings[$i]['Frame-Factor'] = $opening_types[$j]['Frame-Factor'];
				$openings[$i]['Description'] = $opening_types[$j]['Description'];
			}
		}
	}
}

function calc_E_WALLS($walls, &$average_U_value, &$maximum_U_value, &$maximum_U_name)
{
	$heat_loss = 0.0;
	$heat_loss_area = 0.0;

	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]['Curtain-Wall'] == 'false')) {
			$heat_loss += $walls[$i]['Net-Area'] * $walls[$i]['U-value'];
			$heat_loss_area += $walls[$i]['Net-Area'];

			if ($maximum_U_value < $walls[$i]['U-value']) {
				$maximum_U_value = $walls[$i]['U-value'];
				$maximum_U_name = $walls[$i]['Name'];
				$maximum_U_name .= ' (' . $walls[$i]['U-value'] . ')';
			}
		}
	}

	if (0.0 < $heat_loss_area) {
		$average_U_value = round($heat_loss / $heat_loss_area, 2);
	}
	else {
		$average_U_value = 'N/A';
		$maximum_U_value = 'N/A';
		$maximum_U_name = 'N/A';
	}
}

function calc_P_WALLS($walls, &$average_U_value, &$maximum_U_value, &$maximum_U_name)
{
	$heat_loss = 0.0;
	$heat_loss_area = 0.0;

	for ($i = 0; $i < count($walls); $i++) {
		if (($walls[$i]['Type'] == 'Party wall') && ($walls[$i]['Curtain-Wall'] == 'false')) {
			$heat_loss += $walls[$i]['Net-Area'] * $walls[$i]['U-value'];
			$heat_loss_area += $walls[$i]['Net-Area'];

			if ($maximum_U_value < $walls[$i]['U-value']) {
				$maximum_U_value = $walls[$i]['U-value'];
				$maximum_U_name = $walls[$i]['Name'];
				$maximum_U_name .= ' (' . $walls[$i]['U-value'] . ')';
			}
		}
	}

	if (0.0 < $heat_loss_area) {
		$average_U_value = round($heat_loss / $heat_loss_area, 2);
	}
	else {
		$average_U_value = 'N/A';
		$maximum_U_value = 'N/A';
		$maximum_U_name = 'N/A';
	}
}

function calc_C_WALLS($walls, &$average_U_value, &$maximum_U_value, &$maximum_U_name)
{
	$heat_loss = 0.0;
	$heat_loss_area = 0.0;

	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]['Curtain-Wall'] == 'true')) {
			$heat_loss += $walls[$i]['Net-Area'] * $walls[$i]['U-value'];
			$heat_loss_area += $walls[$i]['Net-Area'];

			if ($maximum_U_value < $walls[$i]['U-value']) {
				$maximum_U_value = $walls[$i]['U-value'];
				$maximum_U_name = $walls[$i]['Name'];
				$maximum_U_name .= ' (' . $walls[$i]['U-value'] . ')';
			}
		}
	}

	if (0.0 < $heat_loss_area) {
		$average_U_value = round($heat_loss / $heat_loss_area, 2);
	}
	else {
		$average_U_value = 'N/A';
		$maximum_U_value = 'N/A';
		$maximum_U_name = 'N/A';
	}
}

function calc_FLOORS($floors, &$average_U_value, &$maximum_U_value, &$maximum_U_name)
{
	$heat_loss = 0.0;
	$heat_loss_area = 0.0;

	for ($i = 0; $i < count($floors); $i++) {
		if (0.0 < $floors[$i]['Heat-Loss-Area']) {
			$heat_loss += $floors[$i]['Heat-Loss-Area'] * $floors[$i]['U-value'];
			$heat_loss_area += $floors[$i]['Heat-Loss-Area'];

			if ($maximum_U_value < $floors[$i]['U-value']) {
				$maximum_U_value = $floors[$i]['U-value'];
				$maximum_U_name = $floors[$i]['Name'];
				$maximum_U_name .= ' (' . $floors[$i]['U-value'] . ')';
			}
		}
	}

	if (0.0 < $heat_loss_area) {
		$average_U_value = round($heat_loss / $heat_loss_area, 2);
	}
	else {
		$average_U_value = 'N/A';
		$maximum_U_value = 'N/A';
		$maximum_U_name = 'N/A';
	}
}

function calc_ROOFS($roofs, &$average_U_value, &$maximum_U_value, &$maximum_U_name)
{
	$heat_loss = 0.0;
	$heat_loss_area = 0.0;

	for ($i = 0; $i < count($roofs); $i++) {
		if ($roofs[$i]['Type'] == 'Exposed roof') {
			$heat_loss += $roofs[$i]['Net-Area'] * $roofs[$i]['U-value'];
			$heat_loss_area += $roofs[$i]['Net-Area'];

			if ($maximum_U_value < $roofs[$i]['U-value']) {
				$maximum_U_value = $roofs[$i]['U-value'];
				$maximum_U_name = $roofs[$i]['Name'];
				$maximum_U_name .= ' (' . $roofs[$i]['U-value'] . ')';
			}
		}
	}

	if (0.0 < $heat_loss_area) {
		$average_U_value = round($heat_loss / $heat_loss_area, 2);
	}
	else {
		$average_U_value = 'N/A';
		$maximum_U_value = 'N/A';
		$maximum_U_name = 'N/A';
	}
}

function calc_OPENINGS($openings, &$average_U_value, &$maximum_U_value, &$maximum_U_name)
{
	$heat_loss = 0.0;
	$heat_loss_area = 0.0;

	for ($i = 0; $i < count($openings); $i++) {
		if ($openings[$i]['Type'] != 'Rooflight') {
			$heat_loss += $openings[$i]['Area'] * $openings[$i]['U-value'];
			$heat_loss_area += $openings[$i]['Area'];

			if ($maximum_U_value < $openings[$i]['U-value']) {
				$maximum_U_value = $openings[$i]['U-value'];
				$maximum_U_name = $openings[$i]['Name'];
				$maximum_U_name .= ' (' . $openings[$i]['U-value'] . ')';
			}
		}
	}

	if (0.0 < $heat_loss_area) {
		$average_U_value = round($heat_loss / $heat_loss_area, 2);
	}
	else {
		$average_U_value = 'N/A';
		$maximum_U_value = 'N/A';
		$maximum_U_name = 'N/A';
	}
}

function calc_ROOFLIGHTS($openings, &$average_U_value, &$maximum_U_value, &$maximum_U_name)
{
	$heat_loss = 0.0;
	$heat_loss_area = 0.0;

	for ($i = 0; $i < count($openings); $i++) {
		if ($openings[$i]['Type'] == 'Rooflight') {
			$heat_loss += $openings[$i]['Area'] * $openings[$i]['U-value'];
			$heat_loss_area += $openings[$i]['Area'];

			if ($maximum_U_value < $openings[$i]['U-value']) {
				$maximum_U_value = $openings[$i]['U-value'];
				$maximum_U_name = $openings[$i]['Name'];

				if ($openings[$i]['Orientation'] != 'unknown or unspecified') {
					$maximum_U_name .= ', ' . $openings[$i]['Orientation'];
				}

				$maximum_U_name .= ' (' . $openings[$i]['U-value'] . ')';
			}
		}
	}

	if (0.0 < $heat_loss_area) {
		$average_U_value = round($heat_loss / $heat_loss_area, 2);
	}
	else {
		$average_U_value = 'N/A';
		$maximum_U_value = 'N/A';
		$maximum_U_name = 'N/A';
	}
}

function calc_AverageEfficacy($lights, &$average_efficacy)
{
	$total_power = 0.0;
	$total_lumens = 0.0;

	for ($i = 0; $i < count($lights); $i++) {
		$total_power += $lights[$i]['Outlets'] * $lights[$i]['Power'];
		$total_lumens += $lights[$i]['Outlets'] * $lights[$i]['Power'] * $lights[$i]['Efficacy'];
	}

	if (0.0 < $total_power) {
		$average_efficacy = round($total_lumens / $total_power, 1);
	}
}

function calc_MinimumEfficacy($lights, &$minimum_efficacy)
{
	$minimum_efficacy = 5000;

	for ($i = 0; $i < count($lights); $i++) {
		if ($lights[$i]['Efficacy'] < $minimum_efficacy) {
			$minimum_efficacy = $lights[$i]['Efficacy'];
		}
	}
}

function calc_PV($pv_panel, &$pv_output)
{
	$peak_power = $pv_panel['Peak-Power'];
	$orientation = $pv_panel['Orientation'];
	$pitch = $pv_panel['Pitch'];
	$pitches = ['Horizontal' => 0, '30&deg;' => 30, '45&deg;' => 45, '60&deg;' => 60, 'Vertical' => 90];
	$overshadings = ['None or very little' => 1.0, 'Modest' => 0.8, 'Significant' => 0.5, 'Heavy' => 0.35];

	if (!empty($pv_panel['Overshading-MCS'])) {
		$overshading = $pv_panel['Overshading-MCS'];
	}
	else if (!empty($pv_panel['Overshading'])) {
		$overshading = $overshadings[$pv_panel['Overshading']];
	}

	$uk_average_flux = [26, 54, 96, 150, 192, 200, 189, 157, 115, 66, 33, 21];
	$orientations = ['N/A' => 0, 'NA' => 0, 'North' => 0, 'North East' => 1, 'East' => 2, 'South East' => 3, 'South' => 4, 'South West' => 3, 'West' => 2, 'North West' => 1, 'Horizontal' => 0, 'horiz' => 0];
	$theorientation = $orientations[$orientation];
	$thepitch = $pitches[$pitch];
	$k1 = [26.3, 0.165, 1.44, -2.95, -0.66];
	$k2 = [-38.5, -3.68, -2.36, 2.89, -0.106];
	$k3 = [14.8, 3.0, 1.07, 1.17, 2.93];
	$k4 = [-16.5, 6.38, -0.514, 5.67, 3.63];
	$k5 = [27.3, -4.53, 1.89, -3.54, -0.374];
	$k6 = [-11.9, -0.405, -1.64, -4.28, -7.4];
	$k7 = [-1.06, -4.38, -0.542, -2.72, -2.71];
	$k8 = [0.0872, 4.89, -0.757, -0.25, -0.991];
	$k9 = [-0.191, -1.99, 0.604, 3.07, 4.59];
	$sin_pitch = sin(deg2rad($thepitch / 2));
	$a = round((pow($sin_pitch, 3) * $k1[$theorientation]) + (pow($sin_pitch, 2) * $k2[$theorientation]) + ($sin_pitch * $k3[$theorientation]), 6);
	$b = round((pow($sin_pitch, 3) * $k4[$theorientation]) + (pow($sin_pitch, 2) * $k5[$theorientation]) + ($sin_pitch * $k6[$theorientation]), 6);
	$c = round((pow($sin_pitch, 3) * $k7[$theorientation]) + (pow($sin_pitch, 2) * $k8[$theorientation]) + ($sin_pitch * $k9[$theorientation]), 6) + 1;
	$latitude = 53.5;
	$declination = [-20.7, -12.8, -1.8, 9.8, 18.8, 23.1, 21.2, 13.7, 2.9, -8.7, -18.4, -23.0];
	$radiation = [12];

	for ($j = 0; $j < 12; $j++) {
		$lat_minus_decl = round((pi() / 180) * ($latitude - $declination[$j]), 6);
		$rhinc[$j] = ($a * pow(cos($lat_minus_decl), 2)) + ($b * cos($lat_minus_decl)) + $c;
		$radiation[$j] = $rhinc[$j] * $uk_average_flux[$j];
	}

	$daysinmonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	$solar_rad = 0.0;
	$pv_output = 0.0;

	for ($j = 0; $j < 12; $j++) {
		$pv_output = $pv_output + ($overshading * ($peak_power * 0.8) * ($daysinmonth[$j] * 0.024 * $radiation[$j]));
	}

	return $pv_output;
}

function calc_SAP103_PVFloorArea($SAP_data)
{
	$property_details = $SAP_data['SAP10-Data']['SAP-Property-Details'];

	// SAP 10.3 PV provision is based on floor area, not floor heat-loss area.
	// Kaizen writes the UI's lowest-storey floor area here, so use it first.
	if (!empty($property_details['Lowest-Storey-Area'])) {
		return (float)$property_details['Lowest-Storey-Area'];
	}

	$lowest_storey = null;
	$floor_area = null;
	$heat_loss_fallback_area = null;

	if (!empty($property_details['SAP-Building-Parts']['SAP-Building-Part'])) {
		$building_parts = $property_details['SAP-Building-Parts']['SAP-Building-Part'];
		if (!isset($building_parts[0])) {
			$building_parts = [$building_parts];
		}

		for ($i = 0; $i < count($building_parts); $i++) {
			if (empty($building_parts[$i]['SAP-Floor-Dimensions']['SAP-Floor-Dimension'])) {
				continue;
			}

			$floor_dimensions = $building_parts[$i]['SAP-Floor-Dimensions']['SAP-Floor-Dimension'];
			if (!isset($floor_dimensions[0])) {
				$floor_dimensions = [$floor_dimensions];
			}

			for ($j = 0; $j < count($floor_dimensions); $j++) {
				$floor = $floor_dimensions[$j];
				$storey = isset($floor['Storey']) ? (float)$floor['Storey'] : 0.0;

				if ($lowest_storey === null || $storey < $lowest_storey) {
					$lowest_storey = $storey;
					$floor_area = !empty($floor['Total-Floor-Area']) ? (float)$floor['Total-Floor-Area'] : null;
					$heat_loss_fallback_area = !empty($floor['Heat-Loss-Area']) ? (float)$floor['Heat-Loss-Area'] : null;
				}
			}
		}
	}

	if ($floor_area !== null && $floor_area > 0) {
		return $floor_area;
	}

	// Last resort only: this can be different from floor area, which was the source of the mismatch.
	if ($heat_loss_fallback_area !== null && $heat_loss_fallback_area > 0) {
		return $heat_loss_fallback_area;
	}

	return 0.0;
}

function calc_SAP103_RecommendedPVOutput($SAP_data)
{
	$property_details = $SAP_data['SAP10-Data']['SAP-Property-Details'];
	$pv_floor_area = calc_SAP103_PVFloorArea($SAP_data);
	if ($pv_floor_area <= 0) {
		return 0.0;
	}

	$num_storeys = 1;
	$property_type = convert_property_type($property_details['Property-Type']);
	if (($property_type == 'Flat') || ($property_type == 'Maisonette')) {
		if (!empty($property_details['SAP-Flat-Details']['Storeys'])) {
			$num_storeys = (float)$property_details['SAP-Flat-Details']['Storeys'];
		}
	}

	if ($num_storeys <= 0) {
		$num_storeys = 1;
	}

	$required_power_array['Peak-Power'] = round(($pv_floor_area * 0.4 / $num_storeys) * 0.22, 2);
	$required_power_array['Orientation'] = 'South West';
	$required_power_array['Pitch'] = '45&deg;';
	$required_power_array['Overshading'] = 'None or very little';

	return round(calc_PV($required_power_array, $pv_output_calc), 2);
}

?>