<?php

include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");

include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once(PATH_TO_ROOT."CRM/class/class.Language-Pack.php");
include_once(PATH_TO_ROOT."CRM/class/class.checkString.php");

$LangPack = new LanguagePack($db, $aLanguage);
	
$table = "_visitStatistic";

if($_POST['action'] == 'get'){
	
	$projectId = StringChecker::Check($_POST['project_id']);
	$cati_location = StringChecker::Check($_POST['cati_location']);
	$dan = StringChecker::Check($_POST['dan']);
	$ura = StringChecker::Check($_POST['ura']);
	
	$filter = " id_project = ".$projectId." AND dan = ".$dan." AND ura = ".$ura." AND cati_location = '".$cati_location."'";
	
	//prodobitev: project name
	$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = $projectId";
    $res = $db->SQLexecute($sql);
 	$dat = $db->fetchAssoc($res);
 	$projectName = $dat['proj_name'];
	
	
	$sql = "SELECT COUNT(id) AS st FROM ".DB_WARPIT_WEBCATI_BASE.".".$projectName."_structure WHERE dan = $dan AND ura = $ura AND smpl_var = ".$cati_location." AND `status` = 1 AND status_rec = 1";
	$res = $db->SQLexecute($sql);
	$dat = $db->fetchAssoc($res);
	$data[] = array('Statistika' => $LangPack->GetLabel("StatisticsMsg", "number_of_invited", "Število povabljenih"), 'Stevilo' => $dat['st']);
	
	//koliko ljudi je prišlo na prezentacijo
	$sql = "SELECT COUNT(id) AS st FROM ". WARPIT_CRM.".".$table ." WHERE id_sub_tel = 0 AND nacin_pristopa <> 0 AND $filter";
	
	$res = $db->SQLexecute($sql);
	$dat = $db->fetchAssoc($res);
	$data[] = array('Statistika' => $LangPack->GetLabel("StatisticsMsg", "attended_number","Število prisotnih"), 'Stevilo' => $dat['st']);
	
	$_st = $dat['st']; 
	
	$sql = "SELECT COUNT(id) AS st FROM ". WARPIT_CRM.".".$table ." WHERE id_sub_tel <> 0 AND $filter";
	
	$res = $db->SQLexecute($sql);
	$dat = $db->fetchAssoc($res);
	
	$data[] = array('Statistika' => $LangPack->GetLabel("StatisticsMsg", "escort_number","Število spremljevalcev"), 'Stevilo' => $dat['st'] );
	
	
	
	$data[] = array('Statistika' => "<b>".$LangPack->GetLabel("StatisticsMsg", "all_attended_number","Število vseh prisotnih")."</b>", 'Stevilo' => "<b>".($dat['st'] + $_st)."</b>");
	$data[] = array('Statistika' => "", "Stevilo" => "");
	
	
	//koliko moških
	$sql = "SELECT COUNT(id) AS st FROM ". WARPIT_CRM.".".$table ." WHERE gender = '1' AND $filter";
	$res = $db->SQLexecute($sql);
	$dat = $db->fetchAssoc($res);
	$data[] = array('Statistika' => $LangPack->GetLabel("StatisticsMsg", "male_number","Število moških"), 'Stevilo' => $dat['st']);
	
	//koliko žensk
	$sql = "SELECT COUNT(id) AS st FROM ". WARPIT_CRM.".".$table ." WHERE gender = '2' AND $filter";
	$res = $db->SQLexecute($sql);
	$dat = $db->fetchAssoc($res);
	$data[] = array('Statistika' => $LangPack->GetLabel("StatisticsMsg", "female_number","Število žensk"), 'Stevilo' => $dat['st']);
	
	//način pristopa
	$polje = array( 2 => $LangPack->GetLabel("StatisticsMsg", "single_number","Število ljudi, ki so prišli sami"),
					3 => $LangPack->GetLabel("StatisticsMsg", "spouse_number","Število ljudi, ki so prišli z zakonci"),
					4 => $LangPack->GetLabel("StatisticsMsg", "life_companion_number","Število ljudi, ki so prišli z življenskim sopotnikom"),
					5 => $LangPack->GetLabel("StatisticsMsg", "sibling_number","Število ljudi, ki so prišli z bratom/sestro"),
					6 => $LangPack->GetLabel("StatisticsMsg", "friend_number","Število ljudi, ki so prišli s prijateljem"),
					7 => $LangPack->GetLabel("StatisticsMsg", "other_number","Število ljudi, ki so prišli z drugo"));
	
	foreach($polje as $key => $value){
		$invited = 0;
		for ($i=0; $i < 2; $i++) {
			if($i%2 == 0){ 
				$sql = "SELECT COUNT(id) AS st FROM ". WARPIT_CRM.".".$table ." WHERE id_sub_tel = 0 AND nacin_pristopa = ".($key-1)." AND $filter";
			}else{
				$sql = "SELECT COUNT(id) AS st FROM ". WARPIT_CRM.".".$table ." WHERE id_sub_tel > 0 AND nacin_pristopa = ".$key." AND $filter";	
			}
			$res = $db->SQLexecute($sql);
			$dat = $db->fetchAssoc($res);
			$invited += $dat['st'];
		}
		
		$data[] = array('Statistika' => $value, 'Stevilo' => $invited);
	}
	
	$result['success'] = TRUE;
    $result['results'] = sizeof($data);
    $result['rows'] = $data;
    print json_encode($result);
    exit;
}elseif($_POST['action'] == 'getVabila'){
	//ini_set("display_errors", 1);
	$projectId = StringChecker::Check($_POST['projectId']);
	$dan = StringChecker::Check($_POST['dan']);
	$ura = StringChecker::Check($_POST['ura']);
	$cati_location = StringChecker::Check($_POST['cati_location']);
	
	
	$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = $projectId";
    $res = $db->SQLexecute($sql);
 	$dat = $db->fetchAssoc($res);
 	$projectName = $dat['proj_name'];
	
	
	$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI.".`".$projectName."_language` WHERE `id_question` IN ( 9, 10 ) AND `id_text_type` = 3";
	$res = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($res)){
		$lang[] = explode("/", $row['lngText']);
	}
	foreach ($lang[0] as $day) {
		$tmp = explode(" ", $day);
		$days[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $day);	
	}
	foreach ($lang[1] as $t) {
		$tmp = explode(" ", $t);
		$time[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $t);
	}
	if(isset($cati_location) && !empty($cati_location)){
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$projectName."_structure 
					WHERE id_tel IN (SELECT id_tel FROM ".WARPIT_CRM."._actionLogger WHERE id_project = ".$projectId." AND id_filter = ".$cati_location.") AND status = 1 AND status_rec = 1";
		
			
		$all = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$projectName."_structure WHERE status = 1 AND status_rec = 1 AND smpl_var = ".$cati_location." AND ura = ".$ura." AND dan=".$dan;
		$res = $db->SQLexecute($sql);
		
		$data[] = array("Statistika" => $LangPack->GetLabel("StatisticsMsg", "number_of_people","Število oseb"), "Stevilo" => $db->NumRows($db->SQLexecute($all))-$db->NumRows($res)."/".$db->NumRows($res));
	}else{
		$all = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$projectName."_structure WHERE status = 1 AND status_rec = 1 GROUP BY dan, ura, smpl_var";	
		$res = $db->SQLexecute($all);
		$labels = array();
		while($row = $db->fetchAssoc($res)){
			$cati_location = $row['smpl_var'];
			$dan = $row['dan'] != null ? $row['dan'] : 0;
			$ura = $row['ura'];
			
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$projectName."_structure WHERE id_tel IN (SELECT id_tel FROM ".WARPIT_CRM."._actionLogger WHERE id_project = ".$projectId." AND id_filter = '".$row['smpl_var']."') AND dan = '".$dan."' AND ura = '".$ura."' AND status = 1 AND status_rec = 1";
			$all = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$projectName."_structure WHERE status = 1 AND status_rec = 1 AND dan = '$dan' AND ura = '$ura' AND smpl_var = '".$cati_location."'";
			$rez = $db->SQLexecute($sql);
			
			$sql = "SELECT DISTINCT(presentation_location) FROM ".WARPIT_CRM."._PresentatorPermissions WHERE cati_location = ".$cati_location." AND id_project=".$projectId;
			
			$resu = $db->SQLexecute($sql);
			$dataH = $db->fetchAssoc($resu);
			$hotel = $dataH['presentation_location'];
			
			$data[] = array("Statistika" => $days[$dan]['label'].", ".$time[$ura]['label'].", ".$hotel, "Stevilo" => $db->NumRows($db->SQLexecute($all))-$db->NumRows($rez)."/".$db->NumRows($rez));
		}
	}
	$result['success'] = TRUE;
    $result['results'] = sizeof($data);
    $result['rows'] = $data;
    print json_encode($result);
    exit;
}

?>