<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");


$projectId = $_GET['idProj'];
$projName = $_GET['projName'];
$id_user = $_SESSION['UserInfo']['id'];

		 	$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);

			$sql = "SELECT a.* FROM quota_layer a INNER JOIN quota_layer_addon b ON b.id=a.layer WHERE id_project = $projectId AND interviewer_show = 1 AND q_name!='user' ORDER BY layer,zap_st";
			//echo "get all layers for quotas: ".$sql."<br>";
			$resLayers = $db->SQLexecute($sql);

			$curLayerId = 0;
			$allGrids = array();
			$tableName = $projName;
			$tableLimits = $projName.'___quotaLimits';
			$tableCalc = $projName.'___quotaCalc';
			$layCount = 0;
			echo '<table border=0><tr><td style="border:solid;border-color:000000;border-bottom-width:2px;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm"><b>Quota statistics - '.$projName.'</b></td></tr></table>';

			while (true)
			{
				$rowLayer = $db->fetchAssoc($resLayers);
				if($rowLayer['layer'] != $curLayerId)
				{

					if($curLayerId)//draw the table
					{
						$layCount++;

						$selectSQL = "SELECT a.*,(SELECT count(*) FROM $tableCalc b WHERE (QT_user_id=".$id_user." OR QT_user_id IS NULL) AND ".implode(' AND ',$countWhere). " ) AS q_done FROM $tableLimits a WHERE a.id_layer = ".$curLayerId." AND (QT_user_id=".$id_user." OR QT_user_id IS NULL) ORDER BY " . implode(',', $columns);
						//echo "selectSQL:$selectSQL<br>";
						$quotaRes = $db->SQLexecute($selectSQL);

						//let's get the labels!
						$labSQL = "SELECT a.q_name,b.rec_label,b.id_reconum FROM quota_layer a INNER JOIN recolabel b ON a.source = b.source AND a.id_question = b.id_question AND a.id_project = b.id_project WHERE a.layer = $curLayerId AND b.id_project = $projectId AND (".implode(' OR ',$fetchLabels).")";
						//echo "labSQL: $labSQL<br>";
						$labelsSQL = $db->SQLexecute($labSQL);

						$recodeLabels = array();
						while ($rowLabels = $db->fetchAssoc($labelsSQL) )
						{
							$recodeLabels[ $rowLabels['q_name'] ][ $rowLabels['id_reconum'] ] = $rowLabels['rec_label'];
						}


						//copied the basic styles from borut :)
						$table = "<table border=0 style=font-family:verdana;font-size:12px; cellpadding=2 cellspacing=0 >";
						$table .= '<tr class=tekst><td colspan=7 height=5px></td></tr><tr>';
						$countCol = count($columns);

						for($i = 0;$i < $countCol; $i++)
						{
							$table .= '<td style="border:solid;border-color:000000;border-bottom-width:2px;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm">
<b>&nbsp;'.$colDisplay[$i].'&nbsp;</b></td>';
						}
						$table .= '<td align=center style="border:solid;border-color:000000;border-bottom-width:2px;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm">
<b>&nbsp;&nbsp;<br>&nbsp;Need&nbsp;</b></td><td align=center style="border:solid;border-color:000000;border-bottom-width:2px;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm">
<b>&nbsp;&nbsp;<br>&nbsp;Done&nbsp;</b></td><td align=center style="border:solid;border-color:000000;border-bottom-width:2px;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm">
<b>&nbsp;&nbsp;<br>&nbsp;To Do&nbsp;</b></td><tr class=tekst><td colspan=11 height=5px></td></tr></tr>';

						$countTotal = 0;
						$countDone = 0;

						while ($quotaRow = $db->fetchAssoc($quotaRes))
						{
							$table .= '<TR>';
							for($i = 0;$i < $countCol; $i++)
							{
								//20160401 Tanis v drugem primeru imamo poleg labele za kvoto napisana tudi vrednost
								$table .= '<td align=left  style="border:solid;border-color:000000;border-bottom-width:0.01cm;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm">'. $recodeLabels[ $colDisplay[$i] ][ $quotaRow[  $columns[$i] ] ] ."</TD>";
								//$table .= '<td align=left  style="border:solid;border-color:000000;border-bottom-width:0.01cm;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm">'.$quotaRow[  $columns[$i] ].' '. $recodeLabels[ $colDisplay[$i] ][ $quotaRow[  $columns[$i] ] ] ."</TD>";
							}
							$table .= '<td align=center  style="border:solid;border-color:000000;border-bottom-width:0.01cm;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm">'.$quotaRow['q_value']."</TD>";
							$table .= '<td align=center  style="border:solid;border-color:000000;border-bottom-width:0.01cm;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm"><font color=gray>'.$quotaRow['q_done']."</TD>";

							$done = ($quotaRow['q_value'] - $quotaRow['q_done'] );
							$colorDone = "green";
							if ($done < 0) $colorDone = "red";
							$table .= '<td align=center  style="border:solid;border-color:000000;border-bottom-width:0.01cm;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm"><font color='.$colorDone.'>'.$done."</TD>";
							$countTotal += $quotaRow['q_value'];
							$countDone += $quotaRow['q_done'];

						}
						$countTodo = $countTotal - $countDone;

						$table .= '</tr><tr valign=bottom><td style="border:solid;border-color:000000;border-bottom-width:00cm;border-top-width:0.01cm;border-left-width:0cm;border-right-width:0cm" height=15px colspan='.$countCol.' align=left><font color=black><b>TOTAL</b>&nbsp;</font></td>
<td style="border:solid;border-color:000000;border-bottom-width:00cm;border-top-width:0.01cm;border-left-width:0cm;border-right-width:0cm" align=center><font color=black><b>&nbsp;'.$countTotal.'&nbsp;</b></font></td>
<td style="border:solid;border-color:000000;border-bottom-width:00cm;border-top-width:0.01cm;border-left-width:0cm;border-right-width:0cm" align=center><font color=black><b>&nbsp;'.$countDone.'&nbsp;</b></font></td>
<td style="border:solid;border-color:000000;border-bottom-width:00cm;border-top-width:0.01cm;border-left-width:0cm;border-right-width:0cm" align=center><font color=black><b>&nbsp;'.$countTodo.'&nbsp;</b></font></td></tr>';


						$table .= "</TABLE>";
						$html .= $table;
					}
					//array that holds the where clouse for inner select count
					$countWhere = array();
					$columns = array();
					$colDisplay = array();
					$fetchLabels = array();

					$curLayerId = $rowLayer['layer'];
				}
				if(!$rowLayer) break;

				if($rowLayer['source'] == 2 )
					$colName = 'QT_'.$rowLayer['q_name'];
				else
					$colName = 'TP_'.$rowLayer['q_name'];



				//the or statement for fetching labels
				$fetchLabels[] = '(b.id_question = '.$rowLayer['id_question'].' AND b.source = '.$rowLayer['source'].')';


				$countWhere[] = "a.$colName = b.$colName";
				$columns[] = $colName;
				$colDisplay[] = $rowLayer['q_name'];



			}
			/*
			if($curLayerId)//finish the grid
					{
						addFinalColumns($grid);
						$grid->selectSQL = "SELECT a.*,(SELECT count(*) FROM $tableCalc b WHERE ".implode(' AND ',$countWhere). " ) AS q_done FROM $tableLimits a";

						echo $grid->createGrid();
						$allGrids[] = $grid->getName();
					}
			*/

					/*
		$objHasChild=0;
		  $numCh = 0;//count($xmlObj->children());
		  $curCh = 0;
		  $childHtml = null;
//		  if($prop['type'] != 6)
//			  foreach($xmlObj->children() as $child) {
//			  	$curCh++;
//			    $childHtml .= $this->displayChilds($child,$prop['type'],$numCh == $curCh, $prop['id']);
//			    $objHasChild = true;
//			  }
		  if(!$objHasChild) // if there aren't any children
		  { //if a hbox and vbox don't have any children we create an empty cell
		  	if($prop['type'] == 1)
		  	{
		  		$html .= "<td $size></td>";
		  	}
		  	else if($prop['type'] == 2)
		  	{
		  		$html .= "<tr><td></td></tr>";
		  	}
		  }
		 // else if(!$childHtml)//if all children of the element are hidden, we don't show it either
		//	return '';
		 // else $html .= $childHtml;
*/

		  $html .= $closeTags;

		  echo $html;
?>
