<?php
include "checklog.php";

$rpp = 25;

$sql = "SELECT COUNT(ID) AS no FROM ordini LEFT JOIN clienti ON ordini.cliente = clienti.cust_id";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$no = $row["no"];
$no1 = $no;
if ($no1 < 1) {$no1 = 1;}

$maxp = intval(($no1 - 1) / $rpp) + 1;
if (!isset($HTTP_COOKIE_VARS["boord_pagina"])) {
	$actp = 1;
} else {
	$actp = $HTTP_COOKIE_VARS["boord_pagina"];
}
$actp = intval($actp);
if ($actp > $maxp || $actp < 1) {$actp = 1;}
$lolim = ($actp -1) * $rpp;

$ords = Array("ordini.codice DESC","ordini.data DESC","ordini.listino ASC, ordini.data DESC","ordini.totale_lordo DESC, ordini.data DESC","ordini.status ASC, ordini.data DESC");
$ordsname = Array("Codice","Data","Listino","Totale","Stato");

if (!isset($HTTP_COOKIE_VARS["boord_ord"])) {
	$ordina = $ords[0];
} else {
	$ordina = $HTTP_COOKIE_VARS["boord_ord"];
}
?>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<base target="_self">
<link rel="stylesheet" href="backoffice.css" type="text/css">

<script language="JavaScript">
<!--
function EditOrd(id) {
	document.view.ordid.value = id;
	document.view.submit();
}

function PageGoTo(np) {
	selnum = document.navigazione.ordinamento.selectedIndex;
	ord = document.navigazione.ordinamento.options[selnum].value;
	document.cookie = "boord_ord=" + ord;
	if (np < 1) {
		selnum = document.navigazione.pagina.selectedIndex;
		np = document.navigazione.pagina.options[selnum].value;
	}
	np = parseInt(np);
	document.cookie = "boord_pagina=" + np;
	self.location = self.location;
}

function OrderGoTo() {
	ordcode = document.navigazione.ordcode.value;
	if (ordcode != "") {
		self.location = "boordedit.php?ordcode=" + escape(ordcode);
	}
}

//-->
</script>
</head>

<body topmargin="0" leftmargin="4" marginwidth="4" marginheight="0" class="pagina">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td width="50%"><span class="titolo">Gestione Ordini</span><span class="sottotitolo"> 
      - Elenco ordini</span></td>
    <td width="25%"><span class="sottotitolo"><a href="bostats.php" target="_self">&gt;&gt;Statistiche</a></span></td>
    <td width="25%">&nbsp;</td>
  </tr>
</table>
<form name="navigazione" method="get" action="">
  <table width="100%" border="0" cellspacing="0" cellpadding="2">
    <tr> 
      <td class="intestazione" nowrap> 
        <?php
if ($actp > 1) {
?>
        <input type="button" name="back" value="|<<" class="pulsante" onClick="PageGoTo(1);">
        <input type="button" name="back" value="<" class="pulsante" onClick="PageGoTo(<?=$actp?> - 1);">
        <?php
}
?>
      </td>
      <td class="intestazione" nowrap>&nbsp;&nbsp;Pagina: 
        <?=$actp?>
        / 
        <?=$maxp?>
        &nbsp;&nbsp; </td>
      <td class="intestazione" nowrap> 
        <?php
if ($actp < $maxp) {
?>
        <input type="button" name="forward" value=">" class="pulsante" onClick="PageGoTo(<?=$actp?> + 1);">
        <input type="button" name="forward" value=">>|" class="pulsante" onClick="PageGoTo(<?=$maxp?>);">
        <?php
}
?>
      </td>
      <td class="intestazione" nowrap> &nbsp;&nbsp;Vai a pagina:</td>
      <td class="intestazione" nowrap> 
        <select name="pagina" class="pulsante" onChange="PageGoTo(0);">
          <?php
for ($i=1; $i <= $maxp; $i++) {
	$sel = "";
	if ($actp == $i) {$sel="selected";}
	echo "<option $sel value=$i>$i</option>";
}
?>
        </select>
      </td>
      <td class="intestazione" nowrap> &nbsp;&nbsp;Ordina per:</td>
      <td class="intestazione" nowrap> 
        <select name="ordinamento" class="pulsante" onChange="PageGoTo(0);">
          <?php
for ($i=0; $i<sizeof($ords); $i++) {
	$oval = $ords[$i];
	$oname = $ordsname[$i];
	$sel = "";
	if ($ordina == $oval) {$sel="selected";}
	echo "<option $sel value=\"$oval\">$oname</option>";
}
?>
        </select>
      </td>
      <td class="intestazione" nowrap width="33%" align="right">Vai all'ordine: 
      </td>
      <td class="intestazione" nowrap width="33%" align="left"> 
        <input type="text" name="ordcode" class="pulsante" maxlength="5" size="4">
        <input type="button" name="Go" value="Vai" class="pulsante" onClick="OrderGoTo();">
      </td>
      <td class="intestazione" nowrap width="34%" align="right">Ordini: 
        <?=$no?>
      </td>
    </tr>
  </table>
</form>

<form name="view" metohd="GET" action="boordedit.php">
  <input type="hidden" name="pagina" value="0">
  <input type="hidden" name="ordid">
  <table width="100%" border="0" cellspacing="1" cellpadding="2">
    <tr> 
      <td class="intestazione">Codice</td>
      <td class="intestazione" nowrap>Data</td>
      <td class="intestazione" nowrap>Utente</td>
      <td class="intestazione" nowrap>Nome e cognome</td>
      <td class="intestazione" width="100%" nowrap>Ragione sociale</td>
      <td class="intestazione" nowrap>E-mail</td>
      <td class="intestazione">Listino</td>
      <td class="intestazione">Totale</td>
      <td class="intestazione">Stato</td>
      <td>&nbsp;</td>
    </tr>
    <?php
$sql = "SELECT ordini.* , DATE_FORMAT(data, '%d/%m/%Y %H.%i') AS data2, clienti.user FROM ordini LEFT JOIN clienti ON ordini.cliente = clienti.cust_id ORDER BY $ordina LIMIT $lolim, $rpp";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
	$ordid = $row["ID"];
	$codice = htmlentities($row["codice"]);
	$data = htmlentities($row["data2"]);
	$clid = $row["cliente"];
	$user = htmlentities($row["user"]);
	$fullname = htmlentities($row["nome"]);
	$corp = htmlentities($row["ragione_sociale"]);
	$email = $row["email"];
	$list = $row["listino"] + 1;
	$status = $row["status"];
	$totale = htmlentities(number_format($row["totale_lordo"],2,",","."));
	if ($rowclass == "row1") {
		$rowclass = "row2";
	} else {
		$rowclass = "row1";
	}
?>
    <tr> 
      <td class="<?=$rowclass?>" align="center"> 
        <?=$codice?>
      </td>
      <td class="<?=$rowclass?>" nowrap> 
        <?=$data?>
      </td>
      <td class="<?=$rowclass?>" nowrap> 
        <a href="bocledit.php?clid=<?=$clid?>" target="_self"><?=$user?></a>
      </td>
      <td class="<?=$rowclass?>" nowrap> 
        <?=$fullname?>
      </td>
      <td class="<?=$rowclass?>"> 
        <?=$corp?>
      </td>
      <td class="<?=$rowclass?>"><a href="mailto:<?=$email?>"> 
        <?=$email?>
        </a></td>
      <td class="<?=$rowclass?>" align="center"> 
        <?=$list?>
      </td>
      <td class="<?=$rowclass?>" align="center">
        <?=$totale?>
      </td>
      <td class="<?=$rowclass?>" align="center">
        <?=$statusexpl[$status]?>
      </td>
      <td class="<?=$rowclass?>"> 
        <input type="button" value="Dettagli" class="pulsante" onClick="EditOrd(<?=$ordid?>);">
      </td>
    </tr>
    <?php
}
?>
  </table>
</form>
<form name="help"><input type="hidden" name="pagina" value="ordini.htm"></form>
</body>
</html>