<?php 

header("Content-type: text/html; charset=utf-8");
header("Cache-Control: no-cache, must-revalidate"); 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 

ini_set('display_errors', '0');     # don't show any errors...
error_reporting(E_ALL);  # ...but do log them

date_default_timezone_set('Europe/Ljubljana');

// define constants
define("PATH_TO_ROOT","/var/www/warpit/");

include_once(PATH_TO_ROOT.'_config/dbConfig.php');
include_once(PATH_TO_ROOT.'webcati/_class/class.Inbound.php');
	


echo "<FORM ACTION=\"obisk.php\" METHOD=\"POST\">";
	echo "<SELECT class=select_boxi_head NAME=\"project\">";
		echo "<OPTION class=select_boxi_empty value=\"null_none\">- - - - - - - - - - - - - - - - - - -</OPTION>";
		echo "<OPTION class=select_boxi_empty value=\"21___teden_06_2014_structure\">teden_06_2014_structure</OPTION>";
		echo "<OPTION class=select_boxi_empty value=\"22___teden_07_2014_structure\">teden_07_2014_structure</OPTION>";
		echo "<OPTION class=select_boxi_empty value=\"23___teden_08_2014_structure\">teden_08_2014_structure</OPTION>";
		echo "<OPTION class=select_boxi_empty value=\"24___teden_09_2014_structure\">teden_09_2014_structure</OPTION>";
		echo "<OPTION class=select_boxi_empty value=\"25___teden_10_2014_structure\">teden_10_2014_structure</OPTION>";
		echo "<OPTION class=select_boxi_empty value=\"26___teden_11_2014_structure\">teden_11_2014_structure</OPTION>";
	echo "</SELECT>";
	echo "<input name=\"agent_stat\" type=\"submit\" value=\"OK\">";
echo "</FORM>";  

if (isset($_POST['project'])) {
	$project = $_POST['project'];
	$tmp =explode("___",$project);
	$id_project = $tmp[0];
	$name_project = $tmp[1];

	$sql = "SELECT count( * ) AS n, user_id, nacin_pristopa, CONCAT(c.id,' - ',c.Firstname,' ',c.Surname) AS name
			FROM Warpit_Webcati_base.`$name_project` AS b
			LEFT JOIN Warpit_CRM.`_visitStatistic` AS a ON
			a.id_tel = b.id_tel AND a.id_project = $id_project
			LEFT JOIN Warpit_Webcati_base._Interviewer AS c 
			ON b.user_id=c.id
			WHERE b.status =1
			AND b.status_rec =1
			GROUP BY b.user_id, `nacin_pristopa`";
	//echo "$sql<br>";
	$rs = $db->SQLexecute($sql);
	while ($row = $db->fetchArray($rs)) {
		$obisk[$row['user_id']]['all'] += $row['n'];
		if (!$row['nacin_pristopa']) $obisk[$row['user_id']]['nogo'] += $row['n'];
		if ($row['nacin_pristopa']) $obisk[$row['user_id']]['go'] += $row['n'];
		$obisk[$row['user_id']]['agent'] = $row['name'];
	}
	
	echo "<b>".$name_project."</b><br>";
	echo "<table border=1 cellpadding=2 cellspacing=0>";
		echo "<tr>";
			echo "<td>Agent</td>";
			echo "<td>Povabljenih</td>";
			echo "<td>Prišlo</td>";
			echo "<td>Ni prišlo</td>";
			echo "<td>% realizacije</td>";
		echo "</tr>";
		foreach ($obisk AS $ob) {
			echo "<tr>";
			echo "<td>".$ob['agent']."</td>";
			echo "<td>".$ob['all']."</td>";
			echo "<td>".$ob['go']."</td>";
			echo "<td>".$ob['nogo']."</td>";
			echo "<td>".(round(($ob['go']/$ob['all']*100),1))."</td>";
			echo "</tr>";
		}
	echo "</table>";
	
}

echo "</html>";

?>