<?php
include_once('../../../php/sessionStart.php');
include_once('../../../php/db/dbExec.php');
include_once('../../../php/functions.php');
include_once('../../../php/timezone.php');
include_once('../../../php/privileges.php');
include_once('../../../php/maxExecTime.php');

# QUERYS
$sUsuarios = "SELECT * FROM usuarios";

# EJECUCION DE QUERYS
$db = new dbExec();
$select["usuarios"] = $db->query($sUsuarios);

# PARA VER LOS MODULOS QUE TIENE PERMITIDO EL USUARIO
$priv = new privileges();
while ($rec = $db->fetchAssoc($select['usuarios'])) {
	$pr = $rec['usu_priv'];
    echo 'cod='.$rec['usu_cod'].'  nom='.utf8_encode($rec['usu_nom']).'  priv=';
    echo $pr;
    echo "\n".$priv->deleteUser($pr);
    echo "\n".$priv->modifyUser($pr);
    echo "\n".$priv->delete($pr);
    echo "\n".$priv->modify($pr);
    echo "\n".$priv->consult($pr);
}
//$db->close();


// This is just an example. In application this will come from Javascript (via an AJAX or something)
$timezone_offset_minutes = 330;  // $_GET['timezone_offset_minutes']

// Convert minutes to seconds
$timezone_name = timezone_name_from_abbr("", $timezone_offset_minutes*60, false);

// Asia/Kolkata
echo $timezone_name."<br><br>";

$fec = date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa')));
echo $fec."<br><br>";





// ERROR EN MULTIQUERY
$current_year = 2020;
$weeknum = 2;
$link = mysqli_connect("192.168.101.47", "root", "silvita123", "prueba");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$agentsquery = "CREATE TEMPORARY TABLE LeaderBoard (
    `agent_name` varchar(20) NOT NULL,
    `job_number` int(5) NOT NULL,
    `job_value` decimal(3,1) NOT NULL,
    `points_value` decimal(8,2) NOT NULL
);";
$agentsquery .= "SELECT agent_name, SUM(job_value), SUM(points_value) FROM leaderboard GROUP BY agent_name ORDER BY SUM(points_value) DESC;";
$agentsquery .= "INSERT INTO LeaderBoard (`agent_name`) SELECT DISTINCT agent_name FROM apps WHERE YEAR(booked_date) = $current_year && WEEKOFYEAR(booked_date) = $weeknum;";
$agentsquery .= "INSERT INTO LeaderBoard (`agent_name`, `job_number`, `job_value`, `points_value`) SELECT agent_name, job_number, job_value, points_value FROM jobs WHERE YEAR(booked_date) = $current_year && WEEKOFYEAR(booked_date) = $weeknum;";

if(mysqli_multi_query($link,$agentsquery)){
    do{
        if($result=mysqli_store_result($link)){ // ignore if no record set
            var_dump($result);
            while($row=mysqli_fetch_array($result)){
                echo "<tr class=\"tr",(++$i & 1),"\">";
                    echo "<td style=\"font-weight:bold;\">",$row['agent_name'],"</td>";
                    echo "<td>",$row['SUM(job_value)'],"</td>";
                    echo "<td>",$row['SUM(points_value)'],"</td>";
                    echo "<td>",getAgentAppsWeek($row['agent_name'],$weeknum,$current_year),"</td>";
                echo "</tr>";
            }
            mysqli_free_result($result);
        }
    } while(mysqli_more_results($link) && mysqli_next_result($link));
}
if($error_mess=mysqli_error($link)){
    echo "<br><tr class=\"error\"><td colspan=\"4\">Error: $error_mess</td></tr>";
}
echo "<br>".$db->multiQueryOtro($agentsquery);

?>