<?php

include "checklog.php";

$maxheight = 400;

$anno = $HTTP_GET_VARS['year'];
$listino = $HTTP_GET_VARS['list'];

if ($anno == "")
{
	$anno = date("Y");
}

$maxvaluetot = 1;
$maxvaluenord = 1;
$importo = array('0','0','0','0','0','0','0','0','0','0','0','0','0');
$tot = array(0,0,0,0,0,0,0,0,0,0,0,0,0);
$nord = array(0,0,0,0,0,0,0,0,0,0,0,0,0);

$totanno = 0;
$nordanno = 0;
for ($m = 1; $m < 13; $m++)
{
	$annomin = addslashes($anno);
	$annomax = $annomin;
	$mesemin = substr("0".$m,-2);
	$mesemax = substr("0".$m+1,-2);
	if ($mesemax == "13")
	{
			$annomax = addslashes($anno + 1);
			$mesemax = "01";
	}
	$datamin = "$annomin-$mesemin-01 00:00";
	$datamax = "$annomax-$mesemax-01 00:00";
	$sql = "SELECT SUM(totale_lordo) AS tot, COUNT(totale_lordo) AS nord FROM ordini WHERE status < 4 AND data >= '$datamin' AND data < '$datamax'";
	if ($listino > 0)
	{
		$sql .= " AND listino=($listino - 1)";
	}
	$result = mysql_query($sql);
	if ($row = mysql_fetch_array($result))
	{
		$tot[$m] = $row['tot'];
		$importo[$m] = number_format($tot[$m], 2, ',', '.');
		$nord[$m] = $row['nord'];
	}
	if ($tot[$m] > $maxvaluetot)
	{
		$maxvaluetot = $tot[$m];
	}
	if ($nord[$m] > $maxvaluenord)
	{
		$maxvaluenord = $nord[$m];
	}
	$totanno = $totanno + $tot[$m];
	$nordanno = $nordanno + $nord[$m];
}

$totale = number_format($totanno, 2, ',', '.');

$ratiotot = $maxheight / $maxvaluetot;
$rationord = $maxheight / $maxvaluenord;

$descrizione = "Anno ".$anno;
if ($listino > 0 )
{
	$descrizione .= " - Listino ".$listino;
}

$annomin = "2000";
$annomax = date("Y");
$sql = "SELECT YEAR(data) as anno FROM ordini ORDER BY data ASC LIMIT 0,1";
$result = mysql_query($sql);
if ($row = mysql_fetch_array($result))
{
	$annomin = $row['anno'];
} else
{
	$annomin = $annomax;
}
$annooption = "<select name=\"year\" class=\"testo2\">";
for ($a = $annomin; $a <= $annomax; $a++)
{
	if ($a == $anno)
	{
		$annooption .= "<option selected value=\"$a\">$a</option>";
	} else
	{
		$annooption .= "<option value=\"$a\">$a</option>";
	}

}
$annooption .= "</select>";

$listname = array('Tutti','1','2','3','4','5','6','7','8','9','10');

$listoption = "<select name=\"list\" class=\"testo2\">";
for ($a = 0; $a <= 10; $a++)
{
	if ($a == $list)
	{
		$listoption .= "<option selected value=\"$a\">$listname[$a]</option>";
	} else
	{
		$listoption .= "<option value=\"$a\">$listname[$a]</option>";
	}

}
$listoption .= "</select>";
?>
<html>
<head>
<style type="text/css">
<!--
.testo2 {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; color: #000000}
.testo1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; color: #000000 ; font-weight: bolder}
.testo3 { font-family: Arial, Helvetica, sans-serif; font-size: 12pt; color: #000000 ; font-weight: bold; background-color: #CCCCCC}
.testo4 { font-family: Arial, Helvetica, sans-serif; font-size: 14pt; color: #000000; font-weight: bold }
-->
</style>
<link rel="stylesheet" href="backoffice.css" type="text/css">
</head>

<body leftmargin="4" topmargin="0" marginwidth="4" marginheight="0">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td width="50%"><span class="titolo">Gestione Ordini</span><span class="sottotitolo"> 
      - Statistiche</span></td>
    <td width="25%"><span class="sottotitolo"><a href="boordini.php" target="_self">&gt;&gt;Elenco 
      ordini </a></span></td>
    <td width="25%">&nbsp;</td>
  </tr>
</table>
<form name="form1" method="get" action="">
  <table border="1" cellspacing="0" cellpadding="2" bordercolor="#333333" align="center">
    <tr>
      <td>
        <table border="0" cellspacing="0" cellpadding="0" align="center" class="testo1" bgcolor="#CCCCCC">
          <tr valign="middle"> 
            <td valign="middle">&nbsp;Anno:</td>
            <td valign="middle"> 
              <?php echo $annooption; ?>
            </td>
            <td>&nbsp;</td>
            <td valign="middle">&nbsp;Listino:</td>
            <td valign="middle"> 
              <?php echo $listoption; ?>
            </td>
            <td>&nbsp;</td>
            <td valign="middle"> 
              <input type="submit" name="ok" value="OK" class="testo2">
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
  </form>
<table border="2" cellspacing="0" cellpadding="4" bordercolor="#666666" bgcolor="#FFFFFF" align="center">
  <tr>
    <td align="center" class="testo4">
      <?php echo $descrizione; ?>
      <table border="0" cellspacing="0" cellpadding="0">
        <tr align="center"> 
          <td colspan="12">&nbsp;</td>
        </tr>
        <tr align="center"> 
          <td colspan="12" class="testo3">Fatturato (totale 
            <?php echo $totale; ?>
            )</td>
        </tr>
        <tr align="center" valign="bottom"> 
          <td class="testo2" width="60" nowrap><?php echo $importo[1]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[1] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[2]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[2] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[3]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[3] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[4]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[4] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[5]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[5] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[6]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[6] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[7]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[7] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[8]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[8] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[9]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[9] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[10]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[10] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[11]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[11] * $ratiotot, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $importo[12]; ?><br><img src="tab1.gif" width="30" height="<?php echo number_format($tot[12] * $ratiotot, 0); ?>"></td>
        </tr>
        <tr align="center" valign="top"> 
          <td class="testo1">Gen</td>
          <td class="testo1">Feb</td>
          <td class="testo1">Mar</td>
          <td class="testo1">Apr</td>
          <td class="testo1">Mag</td>
          <td class="testo1">Giu</td>
          <td class="testo1">Lug</td>
          <td class="testo1">Ago</td>
          <td class="testo1">Set</td>
          <td class="testo1">Ott</td>
          <td class="testo1">Nov</td>
          <td class="testo1">Dic</td>
        </tr>
        <tr align="center"> 
          <td colspan="12"> 
            <hr noshade size="1">
          </td>
        </tr>
        <tr align="center"> 
          <td colspan="12">&nbsp;</td>
        </tr>
        <tr align="center"> 
          <td colspan="12" class="testo3">Numero ordini (totale 
            <?php echo $nordanno; ?>
            )</td>
        </tr>
        <tr align="center" valign="bottom">
          <td class="testo2" width="60" nowrap><?php echo $nord[1]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[1] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[2]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[2] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[3]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[3] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[4]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[4] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[5]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[5] * $rationord, 0); ?>"></td>
		  <td class="testo2" width="60" nowrap><?php echo $nord[6]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[6] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[7]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[7] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[8]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[8] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[9]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[9] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[10]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[10] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[11]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[11] * $rationord, 0); ?>"></td>
          <td class="testo2" width="60" nowrap><?php echo $nord[12]; ?><br><img src="tab2.gif" width="30" height="<?php echo number_format($nord[12] * $rationord, 0); ?>"></td>
        </tr>
        <tr align="center" valign="top"> 
          <td class="testo1">Gen</td>
          <td class="testo1">Feb</td>
          <td class="testo1">Mar</td>
          <td class="testo1">Apr</td>
          <td class="testo1">Mag</td>
          <td class="testo1">Giu</td>
          <td class="testo1">Lug</td>
          <td class="testo1">Ago</td>
          <td class="testo1">Set</td>
          <td class="testo1">Ott</td>
          <td class="testo1">Nov</td>
          <td class="testo1">Dic</td>
        </tr>
        <tr align="center" valign="top"> 
          <td class="testo1" colspan="12"> 
            <hr noshade size="1">
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
</table>
<p>&nbsp;</p>
</body>
</html>