<?php			

	unset($totalCondition);
	foreach ($colTypesStructure[$key] as $key_code => $keyVar) {

		$sqlUnion = array();
		
		foreach ($grp[$grKey] as $proj1) {
			$sql = "SELECT count(*) as n, ".$keyVar." as varName  FROM ".$proj1."_structure WHERE flag_bad = 101 AND ".$keyVar." > 0 ";
				if ($filter) $sql .=" AND ( ".$filter." )";					
			$sql .=" GROUP BY ".$keyVar;
			$sqlUnion[] = $sql;			
		} 	
		$sql = "(".implode(") UNION ALL (",$sqlUnion)." ) ";
			
		//echo $sql."<Br>";
		$res = $db->SQLexecute($sql);
		while ($zad = $db->fetchAssoc($res)) {
			$freq[$keyVar][$zad['varName']] += $zad['n'];	
			$totalCol += $zad['n'];
			$colTotal[$zad['varName']] += $zad['n'] ;
			if ($_GET['showPonders']) {
					//if (!$scoreFactors[$key][$keyVar][$zad['varName']]) $scoreFactors[$key][$keyVar][$zad['varName']] = 1;				
					$freqPonder[$keyVar][$zad['varName']] += $zad['n'] * $scoreFactors[$key][$keyVar][$zad['varName']] ;
					$colTotalPonder[$zad['varName']] += $zad['n'] * $scoreFactors[$key][$keyVar][$zad['varName']] ;	
					//echo $key." -> ".$keyVar." || ".$zad['varName']." N= ".$zad['n']." P=".$freqPonder[$keyVar]."<br>";
			}	
		}	
		$totalCondition[$keyVar] = $keyVar ." > 0 ";					
	}

	$sqlUnion = array();
	foreach ($grp[$grKey] as $proj1) {
		$sql = "SELECT count(*) as n  FROM ".$proj1."_structure WHERE  flag_bad = 101 AND ( ";
		$sql .= implode(" OR ",$totalCondition);
		$sql .= " ) ";
		if ($filter) $sql .=" AND ( ".$filter." )";	
		$sqlUnion[] = $sql;				
	//echo $sql;
	}
	$sql = "(".implode(") UNION ALL (",$sqlUnion)." ) ";
	//echo $sql;
	$res = $db->SQLexecute($sql);
	//$zad = $db->fetchAssoc($res);
	while ($zad = $db->fetchAssoc($res)) $total += $zad['n'];	
			
			
	
	
	$dataArray['header']['data'][] = $val;
	
	foreach ($colTypesLegend[$key] as $legendLabs) $dataArray['header']['data'][] = $legendLabs[1];
	
	foreach ($colAns[$key] as $colKey=>$labVal) {
		unset($dat);
		
		$dat[] 	= $labVal[1];
		foreach ($colTypesLegend[$key] as $legendLabs) {
			$dat[] = 	($freq[$key."_".$labVal[0]][$legendLabs[0]]) ? $freq[$key."_".$labVal[0]][$legendLabs[0]] : "0";
			$dat[] = 	($freq[$key."_".$labVal[0]][$legendLabs[0]]) ? number_format(( round( ($freq[$key."_".$labVal[0]][$legendLabs[0]]/$total) * 100,2)),1) : "0";
			if ($_GET['showPonders']) {
				$dat[] = 	($freqPonder[$key."_".$labVal[0]][$legendLabs[0]]) ? $freqPonder[$key."_".$labVal[0]][$legendLabs[0]] : "0";
				$dat[] = 	($freqPonder[$key."_".$labVal[0]][$legendLabs[0]]) ? number_format(( round( ($freqPonder[$key."_".$labVal[0]][$legendLabs[0]]/$total) * 100,2)),1) : "0";			
			}
		}
		
		$dataArray['body']['data'][] = $dat;
	}
	
	unset($dat);
	$dat[] = "TOTAL";
	foreach ($colTypesLegend[$key] as $legendLabs) {
		
		if ($colTotal[$legendLabs[0]]) {
			$dat[] = $colTotal[$legendLabs[0]];
			$dat[] = number_format(($colTotal[$legendLabs[0]]) / $totalCol * 100,2);
		} else {
			$dat[] = "0";
			$dat[] = "";
		}
								
		if ($_GET['showPonders']) {
			
			if ($colTotalPonder[$legendLabs[0]]) {
				$dat[] = $colTotalPonder[$legendLabs[0]];
				$dat[] = number_format(($colTotalPonder[$legendLabs[0]]) / $totalCol * 100,2);
			} else {
				$dat[] = "0";
				$dat[] = "";
			}
		}

	}
	$dataArray['footer']['data'] = $dat;
	
	if ($CurrentPosInExcel) {
		$y = $CurrentPosInExcel[1] + 3;
	} else $y = 1;
						
	$CurrentPosInExcel = CreateTableScale($objPHPExcel,1,$y,$dataArray);

	
?>