<?php
  ///////////////////////////////////////////////////
  //Логи печати
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang.".php")) include "../lang/".$lang.".php";
     else include "../lang/ru.php";

  $title=$titlepage=$TEXT['prn-title'];
  require_once "../config.php";
  require_once "prn_top.php";

  if(strlen($_GET['k'])>8) puterror2($TEXT['error-paging1']);
  if(empty($_GET['k'])) $_GET['k']=0;
  $stop_r = $_GET['k'];
  if($stop_r == 0) $lim_ogr = "10";
   else $lim_ogr = $stop_r.",10";

  $ktblvis = $stop_r+10;
  $str = $ktblvis/10;

  if(empty($_POST['search'])) $_POST['search'] = "";
  if(empty($_POST['typepar'])) $_POST['typepar'] = "";

  if(strlen($_POST['search'])>20) puterror2($TEXT['search-err2']);
  if(strlen($_POST['typepar'])>2) puterror2($TEXT['search-err3']);
  $_POST['search'] = rep($_POST['search']);
  $_POST['typepar'] = rep($_POST['typepar']);
  if(!empty($_GET['search'])) {
   $_POST['typepar'] = $_GET['typepar'];
   $_POST['search'] = $_GET['search'];
   }

    switch($_POST['typepar'])
      {
      case 1: //кол-во страниц
      $action = " AND tbl_prn_logs.pages >= '".$_POST['search']."'";
      break;
      case 2: //дата
      $delimiter = ",";
      $srch_trim = explode($delimiter, $_POST['search'], 2);
      $dt = trim($srch_trim[0]);
      $lst = trim($srch_trim[1]);
      if($lst !== "") $action = " AND tbl_prn_logs.prn_data LIKE '%".$dt."%' AND tbl_prn_logs.pages >= '".$lst."'";
       else $action = " AND tbl_prn_logs.prn_data LIKE '%".$dt."%'";
      break;
      }

  if(!empty($_GET['srv_comp_id'])) $_POST['srv_comp_id'] = $_GET['srv_comp_id'];
   else $_POST['srv_comp_id'] = $_POST['srv_comp_id'];

   if(!empty($_GET['port_id'])) $_POST['port_id'] = $_GET['port_id'];
   else $_POST['port_id'] = $_POST['port_id'];

   if(!empty($_GET['prn_id'])) $_POST['prn_id'] = $_GET['prn_id'];
   else $_POST['prn_id'] = $_POST['prn_id'];

   if(!empty($_GET['usr_name_id'])) $_POST['usr_name_id'] = $_GET['usr_name_id'];
   else $_POST['usr_name_id'] = $_POST['usr_name_id'];

   if(!empty($_GET['usr_login_id'])) $_POST['usr_login_id'] = $_GET['usr_login_id'];
   else $_POST['usr_login_id'] = $_POST['usr_login_id'];

   if(!empty($_GET['prn_pc_id'])) $_POST['prn_pc_id'] = $_GET['prn_pc_id'];
   else $_POST['prn_pc_id'] = $_POST['prn_pc_id'];

  if($_POST['srv_name_ch'] == "on" || $_GET['srv_name_ch'] == "on") {
    $srv_comp_id = " AND tbl_prn_logs.srv_comp_id='".$_POST['srv_comp_id']."'";
    $on_srv_name_ch = "checked";
    $a1="&amp;srv_comp_id=".$_POST['srv_comp_id']."&amp;srv_name_ch=on";
  }
   else {
          $srv_comp_id = "";
          $on_srv_name_ch = "";
          $a1 = "";
          }

  if($_POST['port_name_ch'] == "on" || $_GET['port_name_ch'] == "on") {
    $port_id = " AND tbl_prn_logs.port_id='".$_POST['port_id']."'";
    $on_port_name_ch = "checked";
    $a2="&amp;port_id=".$_POST['port_id']."&amp;port_name_ch=on";
  }
   else {
          $port_id = "";
          $on_port_name_ch = "";
          $a2 = "";
          }

  if($_POST['prn_name_ch'] == "on" || $_GET['prn_name_ch'] == "on") {
    $prn_id = " AND tbl_prn_logs.prn_id='".$_POST['prn_id']."'";
    $on_prn_name_ch = "checked";
    $a3="&amp;prn_id=".$_POST['prn_id']."&amp;prn_name_ch=on";
  }
   else {
          $prn_id = "";
          $on_prn_name_ch = "";
          $a3 = "";
          }

  if($_POST['usr_name_ch'] == "on" || $_GET['usr_name_ch'] == "on") {
    $usr_name_id = " AND tbl_prn_logs.usr_name_id='".$_POST['usr_name_id']."'";
    $on_usr_name_ch = "checked";
    $a4="&amp;usr_name_id=".$_POST['usr_name_id']."&amp;usr_name_ch=on";
  }
   else {
          $usr_name_id = "";
          $on_usr_name_ch = "";
          $a4 = "";
          }

  if($_POST['login_name_ch'] == "on" || $_GET['login_name_ch'] == "on") {
    $usr_login_id = " AND tbl_prn_logs.usr_login_id='".$_POST['usr_login_id']."'";
    $on_login_name_ch = "checked";
    $a5="&amp;usr_login_id=".$_POST['usr_login_id']."&amp;login_name_ch=on";
  }
   else {
          $usr_login_id = "";
          $on_login_name_ch = "";
          $a5 = "";
          }

  if($_POST['pc_name_ch'] == "on" || $_GET['pc_name_ch'] == "on") {
    $pc_id = " AND tbl_prn_logs.pc_id='".$_POST['prn_pc_id']."'";
    $on_pc_name_ch = "checked";
    $a6="&amp;prn_pc_id=".$_POST['prn_pc_id']."&amp;pc_name_ch=on";
  }
   else {
          $pc_id = "";
          $on_pc_name_ch = "";
          $a6 = "";
          }

  if(!empty($_POST['search'])) $a7="&amp;search=".$_POST['search']."&amp;typepar=".$_POST['typepar']."";
   else $a7="";

  if($_POST['typepar'] == 1) {
   $type_chk1 = "checked";
   $type_chk2 = "";
  }
  elseif($_POST['typepar'] == 2) {
   $type_chk1 = "";
   $type_chk2 = "checked";
  }
  else {
   $type_chk1 = "checked";
   $type_chk2 = "";
  }
?>
<div id="fixedtop">
<table class="topadmin" border="0" cellspacing="0">
    <tr align="center">
        <td class="supp_iniz2"><?php echo $TEXT['prn-title']; ?></td>
    </tr>
  </table>
<form name="rprt" action=prn_main.php method=post>
<table class="tabledata" border="0" cellpadding="2" cellspacing="0" width="100%">
<tr align="center">
  <td>
    <table width=100% class="topheader" border="0" cellspacing="3">
     <tr align="center" valign="middle">
     <td align="right"><input class="inputsearch" size=30 maxlength=30 type=text name=search title="<?php echo $TEXT['search-btn-title']; ?>" value='<?php echo $_POST['search']; ?>' >&nbsp;<input class="button" type=submit value='<?php echo $TEXT['search-btn']; ?>'></td>
     </tr>
     <tr>
       <td align="right" class="menu">
         <input type=radio name=typepar value=1 <?php echo $type_chk1; ?> /><?php echo $TEXT['prn-pages']; ?>
         <input type=radio name=typepar value=2 <?php echo $type_chk2; ?> /><?php echo $TEXT['search-date']; ?>
         <script type="text/javascript" language="JavaScript">
	       var o_cal = new tcal ({
		   // form name
		   'formname': 'rprt',
		    // input name
		   'controlname': 'search'
	});
	</script>
         <input type=checkbox name=srv_name_ch <?php echo $on_srv_name_ch; ?> />
         <select name=srv_comp_id class="button">
    <?php
    $query9 = "SELECT * FROM `tbl_prn_srv_comp` ORDER BY srv_name";
        $srv_name_tmp = mysql_query($query9);
          if(!$srv_name_tmp) puterror2(mysql_error());
              while($res_srv_name = mysql_fetch_array($srv_name_tmp,MYSQL_ASSOC))
                {
                 if($res_srv_name['srv_comp_id'] == $_POST['srv_comp_id'])
                   {
                   $chk6 = "<option selected value=".$res_srv_name['srv_comp_id'].">".$res_srv_name['srv_name']."";
                   }
                 else $chk6 = "<option value=".$res_srv_name['srv_comp_id'].">".$res_srv_name['srv_name']."";
                 echo $chk6;
                }
    ?>
    </select>
         <input type=checkbox name=port_name_ch <?php echo $on_port_name_ch; ?> />
         <select name=port_id class="button">
    <?php
    $query8 = "SELECT * FROM `tbl_prn_port` ORDER BY port_name";
        $port_name_tmp = mysql_query($query8);
          if(!$port_name_tmp) puterror2(mysql_error());
              while($res_port_name = mysql_fetch_array($port_name_tmp,MYSQL_ASSOC))
                {
                 if($res_port_name['port_id'] == $_POST['port_id'])
                   {
                   $chk5 = "<option selected value=".$res_port_name['port_id'].">".$res_port_name['port_name']."";
                   }
                 else $chk5 = "<option value=".$res_port_name['port_id'].">".$res_port_name['port_name']."";
                 echo $chk5;
                }
    ?>
    </select>
         <input type=checkbox name=prn_name_ch <?php echo $on_prn_name_ch; ?> />
         <select name=prn_id class="button">
    <?php
    $query7 = "SELECT * FROM `tbl_prn_name` ORDER BY prn_name";
        $prn_name_tmp = mysql_query($query7);
          if(!$prn_name_tmp) puterror2(mysql_error());
              while($res_prn_name = mysql_fetch_array($prn_name_tmp,MYSQL_ASSOC))
                {
                 if($res_prn_name['prn_id'] == $_POST['prn_id'])
                   {
                   $chk4 = "<option selected value=".$res_prn_name['prn_id'].">".$res_prn_name['prn_name']."";
                   }
                 else $chk4 = "<option value=".$res_prn_name['prn_id'].">".$res_prn_name['prn_name']."";
                 echo $chk4;
                }
    ?>
    </select>
         <input type=checkbox name=usr_name_ch <?php echo $on_usr_name_ch; ?> />
         <select name=usr_name_id class="button">
    <?php
    $query6 = "SELECT * FROM `tbl_prn_usr_name` ORDER BY usr_name";
        $usr_name_tmp = mysql_query($query6);
          if(!$usr_name_tmp) puterror2(mysql_error());
              while($res_usr_name = mysql_fetch_array($usr_name_tmp,MYSQL_ASSOC))
                {
                 if($res_usr_name['usr_name_id'] == $_POST['usr_name_id'])
                   {
                   $chk3 = "<option selected value=".$res_usr_name['usr_name_id'].">".$res_usr_name['usr_name']."";
                   }
                 else $chk3 = "<option value=".$res_usr_name['usr_name_id'].">".$res_usr_name['usr_name']."";
                 echo $chk3;
                }
    ?>
    </select>
         <input type=checkbox name=login_name_ch <?php echo $on_login_name_ch; ?> />
         <select name=usr_login_id class="button">
    <?php
    $query5 = "SELECT * FROM `tbl_prn_usr_login` ORDER BY login_name";
        $login_name_tmp = mysql_query($query5);
          if(!$login_name_tmp) puterror2(mysql_error());
              while($res_login_name = mysql_fetch_array($login_name_tmp,MYSQL_ASSOC))
                {
                 if($res_login_name['usr_login_id'] == $_POST['usr_login_id'])
                   {
                   $chk2 = "<option selected value=".$res_login_name['usr_login_id'].">".$res_login_name['login_name']."";
                   }
                 else $chk2 = "<option value=".$res_login_name['usr_login_id'].">".$res_login_name['login_name']."";
                 echo $chk2;
                }
    ?>
    </select>
         <input type=checkbox name=pc_name_ch <?php echo $on_pc_name_ch; ?> />
         <select name=prn_pc_id class="button">
    <?php
    $query4 = "SELECT * FROM `tbl_prn_pc` ORDER BY pc_name";
        $pc_name_tmp = mysql_query($query4);
          if(!$pc_name_tmp) puterror2(mysql_error());
              while($res_pc_name = mysql_fetch_array($pc_name_tmp,MYSQL_ASSOC))
                {
                 if($res_pc_name['prn_pc_id'] == $_POST['prn_pc_id'])
                   {
                   $chk = "<option selected value=".$res_pc_name['prn_pc_id'].">".$res_pc_name['pc_name']."";
                   }
                 else $chk = "<option value=".$res_pc_name['prn_pc_id'].">".$res_pc_name['pc_name']."";
                 echo $chk;
                }
    ?>
    </select>
       </td>
     </tr>
    </table>
</td>
</tr>
</table>
</form>
       <?php
       if($on_srv_name_ch == "checked" || $on_port_name_ch  == "checked" || $on_prn_name_ch  == "checked" || $on_usr_name_ch == "checked" || $on_login_name_ch == "checked" || $on_pc_name_ch  == "checked" || !empty($_POST['search'])) {
        $query_lst_cnt = mysql_query("SELECT SUM(tbl_prn_logs.pages) AS sum_pages, SUM(tbl_prn_logs.size) AS sum_size, COUNT(tbl_prn_logs.prn_log_id) AS sum_zad FROM tbl_prn_logs WHERE tbl_prn_logs.prn_log_id != 0 ".$srv_comp_id." ".$port_id." ".$prn_id." ".$usr_name_id." ".$usr_login_id." ".$pc_id." ".$action." ");
         if(!$query_lst_cnt) puterror2(mysql_error());
          $res_lst_cnt = mysql_fetch_array($query_lst_cnt,MYSQL_ASSOC);

           $result_lst_cnt = $res_lst_cnt['sum_size'];
           if($result_lst_cnt > 1024)
              {
              $result_lst_cnt = (float)($result_lst_cnt/1024);
              if($result_lst_cnt > 1024)
                {
                 $result_lst_cnt = (float)($result_lst_cnt/1024);
                 if($result_lst_cnt > 1024)
                 {
                     $result_lst_cnt = (float)($result_lst_cnt/1024);
                     $result_lst_cnt = round($result_lst_cnt, 2);
                     $result_lst_cnt = $result_lst_cnt.$TEXT['size-gb'];
                 } else
                    {
                       $result_lst_cnt = (float)($result_lst_cnt);
                       $result_lst_cnt = round($result_lst_cnt, 2);
                       $result_lst_cnt = $result_lst_cnt.$TEXT['size-mb'];
                    }
                }
                 else
                  {
                  $result_lst_cnt = round($result_lst_cnt, 2);
                  $result_lst_cnt = $result_lst_cnt.$TEXT['size-kb'];
                  }
              }
              else $result_lst_cnt = $result_lst_cnt.$TEXT['size-b'];

        $img_cl = "<a href=prn_export.php?k=".$k.$a1.$a2.$a3.$a4.$a5.$a6.$a7." title='".$TEXT['exp-csv']."' ><img class='menu_print_mesto_exp' alt='".$TEXT['know-prim-close']."' src='../img/export.png' /></a>";
        echo "<div class='menu_print_mesto_reslt'>";
        echo "<table border='0' width='100%' align='right' class='formainbtn'>";
        echo "<tr>";
        echo "<td class='newsdesch'>".$TEXT['size-itogo']."<span class='news3'>".$res_lst_cnt['sum_zad']."</span>".$img_cl."</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td class='newsdesch'>".$TEXT['prn-page']."<span class='news3'>".$res_lst_cnt['sum_pages']."</span></td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td class='newsdesch'>".$TEXT['prn-size']."<span class='news3'>".$result_lst_cnt."</span></td>";
        echo "</tr>";
       }
       else {
       echo "<div class='menu_print_mesto'>";
       echo "<table border='0' width='100%' align='right' class='formainbtn'>";
       echo "<tr><td></td><td></td><td></td><td></td></tr>";
       echo "<tr>";
       echo "<td></td>";
       echo "<td>&nbsp;<img class='formainimg' alt='add' src='../img/charts.png' /></td>";
       echo "<td class='newsdesch'>&nbsp;<a href='prn_stat.php' class='news3' title=".$TEXT['srv-stat'].">".$TEXT['srv-stat']."</a></td>";
       echo "<td></td>";
       echo "</tr>";
       echo "<tr><td></td><td></td><td></td><td></td></tr>";
       }
       ?>
    </table>
<?php
  $query4 = "SELECT 
    tbl_prn_logs.*,
    YEAR(tbl_prn_logs.prn_data) AS prn_year,
    MONTH(tbl_prn_logs.prn_data) AS prn_month,
    DAY(tbl_prn_logs.prn_data) AS prn_day,
    TIME(tbl_prn_logs.prn_data) AS prn_hour,
    tbl_prn_pc.pc_name,
    tbl_prn_usr_login.login_name,
    tbl_prn_usr_name.usr_name,
    tbl_prn_name.prn_name,
    tbl_prn_port.port_name,
    tbl_prn_srv_comp.srv_name
    FROM tbl_prn_logs
    JOIN tbl_prn_pc ON tbl_prn_logs.pc_id=tbl_prn_pc.prn_pc_id
    JOIN tbl_prn_usr_login ON tbl_prn_logs.usr_login_id=tbl_prn_usr_login.usr_login_id
    JOIN tbl_prn_usr_name ON tbl_prn_logs.usr_name_id=tbl_prn_usr_name.usr_name_id
    JOIN tbl_prn_name ON tbl_prn_logs.prn_id=tbl_prn_name.prn_id
    JOIN tbl_prn_port ON tbl_prn_logs.port_id=tbl_prn_port.port_id
    JOIN tbl_prn_srv_comp ON tbl_prn_logs.srv_comp_id=tbl_prn_srv_comp.srv_comp_id
    WHERE tbl_prn_logs.prn_log_id != 0 ".$srv_comp_id." ".$port_id." ".$prn_id." ".$usr_name_id." ".$usr_login_id." ".$pc_id." ".$action."
    ORDER BY tbl_prn_logs.prn_data DESC LIMIT $lim_ogr";
    $tmp4 = mysql_query($query4);
     if(!$tmp4) puterror2(mysql_error());
      $num_rows = mysql_num_rows($tmp4);
        if($num_rows == 0)
          {
           echo " </div></div>";
           echo "<div id='fixedbodyprn'>";
           echo "<table border='0' width='95%' align='center' class='formain'>";
           echo "<tr><td align='center' class='zagolovok'>".$TEXT['search-err4']."</td></tr>";
           echo "</table>";
          }
           else
           {
 ?>
 </div>
</div>
<div id="fixedbodyprn">
      <table border="0" width="95%"  align="center" class="formain">
       <tr>
         <td colspan="5"></td>
       </tr>
       <?php
           while($sost4 = mysql_fetch_array($tmp4,MYSQL_ASSOC))
           {

           $date_day = $sost4['prn_day'];
           $date_month = $sost4['prn_month'];
           $date_year = $sost4['prn_year'];
           $date_time = $sost4['prn_hour'];
           $year = $TEXT['year'];
           if($sost4['prn_year'] == "" || $sost4['prn_year'] == "0") $data_r = "---";
            else $data_r = $date_day." ".$month[$date_month]." ".$date_year." ".$year." ".$date_time;

           $result_tb = $sost4['size'];
           if($result_tb > 1024)
              {
              $result_tb = (float)($result_tb/1024);
              if($result_tb > 1024)
                {
                 $result_tb = (float)($result_tb/1024);
                 if($result_tb > 1024)
                 {
                     $result_tb = (float)($result_tb/1024);
                     $result_tb = round($result_tb, 2);
                     $result_tb = $result_tb.$TEXT['size-gb'];
                 } else
                    {
                       $result_tb = (float)($result_tb);
                       $result_tb = round($result_tb, 2);
                       $result_tb = $result_tb.$TEXT['size-mb'];
                    }
                }
                 else
                  {
                  $result_tb = round($result_tb, 2);
                  $result_tb = $result_tb.$TEXT['size-kb'];
                  }
              }
              else $result_tb = $result_tb.$TEXT['size-b'];

             echo "<tr>
              <td></td>
              <td colspan=3 >";
              ?>
             <fieldset class='zayavka' >
              <legend align="left" ><?php echo $sost4['login_name']." (".$sost4['usr_name'].")"; ?></legend>
               <table align='center' border='0' width='100%' cellpadding='0' cellspacing='0'>
                <tr>
                  <td colspan="2" class="support" width="100%"><?php echo $sost4['document']; ?></td>
                </tr>
                <tr>
                  <td colspan="2" class="support"><?php echo $TEXT['prn-page']."<span  class='autor'>".$sost4['pages']."</span> (".$TEXT['prn-size']."<span  class='autor'>".$result_tb."</span>".$TEXT['prn-pc']."<span  class='autor'>".$sost4['pc_name']."</span>)" ?></td>
                </tr>
                <tr>
                  <td colspan="2" class="support"><?php echo $TEXT['prn-printer']."<span  class='autor'>".$sost4['prn_name']."</span>" ?> (<?php echo $TEXT['prn-port']."<span  class='autor'>".$sost4['port_name']."</span>" ?>)</td>
                </tr>
                <tr>
                  <td class="support"><?php echo $TEXT['prn-srv']."<span  class='autor'>".$sost4['srv_name']."</span>" ?></td>
                  <td class="data"><?php echo $data_r; ?></td>
                </tr>
              </table>
            </fieldset>
       <?php
              echo "</td><td></td>
             </tr>";
            }
            ?>
             <tr>
              <td colspan="5"></td>
             </tr>
            </table>
           <?php
         }
            ?>
<form>
     <?php
       $query_count = mysql_query("SELECT COUNT(tbl_prn_logs.prn_log_id) FROM tbl_prn_logs WHERE tbl_prn_logs.prn_log_id != 0 ".$srv_comp_id." ".$port_id." ".$prn_id." ".$usr_name_id." ".$usr_login_id." ".$pc_id." ".$action." ");
       $coun_rows = mysql_result($query_count, 0);
               $pag = $coun_rows/10;
               if($pag<=1) $pag = ceil($pag);
                else
                {
                 ?>
                  <table align="center" border="0" cellpadding="0" cellspacing="0" width="60%" >
                   <tr align="center">
                    <td>
                <?php
                $pag=ceil($pag);
                $strn = $str-5;
                if($strn<1) $strn=1;
                $strk = $str+5;
                for($i=$strn; $i<=$pag; $i++)
                 {
                  if($i<=$strk)
                  {
                    if($i==$str)
                    {
                      $k = $i*10-10;
                      $kvis=$k+10;
                      $nvis=$k;
                      echo $pg = " <a class='zagolovok' href='prn_main.php?k=".$k.$a1.$a2.$a3.$a4.$a5.$a6.$a7."' title='".$TEXT['zhurn-details2'].$nvis.$TEXT['zhurn-details1'].$kvis."'>".$i."</a>";
                    }
                    else
                    {
                    $k = $i*10-10;
                    $kvis=$k+10;
                    $nvis=$k;
                    echo $pg = " <a href='prn_main.php?k=".$k.$a1.$a2.$a3.$a4.$a5.$a6.$a7."' title='".$TEXT['zhurn-details2'].$nvis.$TEXT['zhurn-details1'].$kvis."'>[".$i."]</a>";
                    }
                  }
                 }
                 ?>
                    <select name='k' onchange=window.location.href=this.options[this.selectedIndex].value;>
                    <?php
                    for($j=1; $j<=$pag; $j++)
                    {
                     $k = $j*10-10;
                     if($k == $_GET['k']) echo "<option selected value=prn_main.php?k=".$k.$a1.$a2.$a3.$a4.$a5.$a6.$a7.">".$j."</option>";
                       else echo "<option value=prn_main.php?k=".$k.$a1.$a2.$a3.$a4.$a5.$a6.$a7.">".$j."</option>";
                    }
                    ?>
                    </select>
                    </td>
                   </tr>
                  </table>
                 <?php
                }
               ?>
</form>
</div>
</body>
</html>