<?php
# ID: 25
# Faktury_edit_filter_module ver.1.0
# last modification 17.5.2010
# Formulář pro filtraci a vyhledání trunků
#
?>
<script language="JavaScript">
 function check(theForm){
    if (document.getElementById("chck1").checked==false){
  document.getElementById('valid').disabled= false;
   }
 return true;
 }
 
 function RefreshForm(theForm){
      document.forms.form.submit();
  
  }
</script>


<?php


  
      if ($_GET['idACCOUNT']!=''){
          $idACC=$_GET['idACCOUNT'];
      }else{
          $idACC=$_POST['idACCOUNT'];
      }
      $query="SELECT U.jmeno,U.prijmeni,SB.name
              FROM user_account UA
              JOIN user U ON U.idUSER=UA.idUSER
              JOIN sip_buddies SB ON SB.id=UA.id
              WHERE UA.id=".$idACC." 
              AND UA.validity=1 
              AND UA.idTECHNOLOGY=1";
              
      $result = mysql_query($query);
      if (!$result) {
            die('1Období nelze načíst:' . mysql_error());
      }         
      list($jmeno,$prijmeni,$ucet)=mysql_fetch_row($result);
      
      $query="SELECT P.idObdobi,LEFT(P.since,10),LEFT(P.to,10)
              FROM period P
              WHERE P.since<=now()
              ";
      
       
      
      
        if($_POST['FPF_PAYED']==1 || (IsSet($_GET['FPF_PAYED']) && $_GET['FPF_PAYED']==0)  )
                    {
                        $_SESSION['FPF_TEMP'][FPF_PAYED]=0;
                    }else{
                        $_SESSION['FPF_TEMP'][FPF_PAYED]= 1;
                    }       
       if(IsSet($_POST['FPF_SINCE']) )
      {
        $_SESSION['FPF_TEMP'][FPF_SINCE]= $_POST['FPF_SINCE'];
      }
      if($_SESSION['FPF_TEMP'][FPF_SINCE]!=''){
         $query=$query." AND P.since>=(SELECT since FROM period WHERE idObdobi=".$_SESSION['FPF_TEMP'][FPF_SINCE].")"   ;
      }
      
      if(IsSet($_POST['FPF_TO']) )
      {
       $_SESSION['FPF_TEMP'][FPF_TO]= $_POST['FPF_TO'];
      }
      if ($_SESSION['FPF_TEMP'][FPF_TO]!=''){
         $query=$query." AND P.to<=(SELECT P1.to FROM period P1 WHERE idObdobi=".$_SESSION['FPF_TEMP'][FPF_TO].")"   ;
      }
                
              
   
                                       
      
      /*
      if(IsSet($_POST['FF_FIRSTNAME']) )
      {
        $_SESSION['FF_TEMP'][FF_FIRSTNAME]= $_POST['FF_FIRSTNAME'];
      }
      if ($_SESSION['FF_TEMP'][FF_FIRSTNAME]!=''){
          $query=$query." AND U.jmeno like \"".$_SESSION['FF_TEMP'][FF_FIRSTNAME]."%\""   ;
      }
      if(IsSet($_POST['FF_LASTNAME']) )
      {
        $_SESSION['FF_TEMP'][FF_LASTNAME]= $_POST['FF_LASTNAME'];
      }
      if ($_SESSION['FF_TEMP'][FF_LASTNAME]!=''){
          $query=$query." AND U.prijmeni like \"".$_SESSION['FF_TEMP'][FF_LASTNAME]."%\""   ;
      }
       if(IsSet($_POST['FF_ACCOUNT']) )
      {
        $_SESSION['FF_TEMP'][FF_ACCOUNT]= $_POST['FF_ACCOUNT'];
      }
      if ($_SESSION['FF_TEMP'][FF_ACCOUNT]!=''){
          $query=$query." AND SB.Name like \"".$_SESSION['FF_TEMP'][FF_ACCOUNT]."%\""   ;
      }
      $query.=" GROUP BY BDF.id 
      
                ORDER BY U.jmeno,U.prijmeni ";
      
      
    */
      $query.=" ORDER BY P.since ASC";
##########################################################################
      #zjisteni poctu stran
        $itemperpage=15;
            $result = mysql_query($query);
        $remainder= (mysql_num_rows($result) % $itemperpage);
        $pages=(mysql_num_rows($result)-$remainder)/$itemperpage ;
        if(IsSet($_GET['PAGE']))
        {
          $query=$query." LIMIT ".($_GET['PAGE']*$itemperpage).",".$itemperpage.""   ;
        
        }
        else
        {
          $query=$query." LIMIT ".(0*$itemperpage).",".$itemperpage.""  ;
        }
      
        $result = mysql_query($query);
        if (!$result) {
            die('Could not query:' . mysql_error());
        }    
      #kod pro vypocet počtu stran
        if ($remainder!=0) $pages++; 
        
        $result = mysql_query($query);
        if (!$result) {
           die('Could not query:' . mysql_error());
        }
         
         $result = mysql_query($query);
      if (!$result) {
           die('Období nelze načíst:' . mysql_error());
      }
?>


<div class="forms">
<h3>Výběr faktur uživatele <?php echo "<u>".$jmeno." ".$prijmeni."</u> pro učet <u>".$ucet."</u>"?></h3> <span class="backtoform"><a href="admin_page.php?NEXTFORM=25">Zpět</a> </span>
<fieldset>
  <legend>Seznam uživatelů a jejich účtů</legend>
    <div class="userlist">
    <form name="form" action="admin_page.php" onSubmit="return check(this)" method="post">
        <table align="center">
           <tr>
              <td class="tableculombhead" colspan=3 style="text-align:left">
                  <?php
                    for ($i=0 ;$i!=$pages;$i++) {
                  if ($i==$_POST['PAGE'])
                  {
                      $styl="<span class=\"pagelistselected\">";
                  }else{
                      $styl="<span class=\"pagelist\">";
                  }
                  echo $styl;
                  unset ($styl);
                      
                      echo ("<a href=\"".selfURL()."?NEXTFORM=26&idACCOUNT=".$idACC."&FPF_PAYED=".$_SESSION['FPF_TEMP'][FPF_PAYED]."&PAGE=".$i."\">".($i+1)." </a>");
                      echo "</span>";   
                  }
                  ?>
                
                </td>
                   <input type="hidden" name="FPF_PAYED" id="valid" value="1" >
                    <input type="hidden" name="idACCOUNT" id="valid" value="<?php echo $idACC;?>" >
                <td class="tableculombhead" colspan=2 style="text-align:right">Jen nezaplacené <input type="checkbox" name="FPF_PAYED" id="chck1" value="0" OnChange="RefreshForm(this)" <?php if ($_SESSION['FPF_TEMP'][FPF_PAYED]==1 ) echo "checked"; ?>></td>   
           </tr><tr>   
                <td class="tableculombhead" colspan=5> Rozmezí :
                    <select name="FPF_SINCE" onChange="RefreshForm()">
                                          <?php
                                              include "build/db_connect.inc.php"  ;
                                              $subquery="SELECT idObdobi,LEFT(since,10)
                                                      FROM period
                                                      WHERE since<=now()                                                      
                                                      ORDER BY since ";                                                    
                                              $subresult = mysql_query($subquery);        
                                              if (!$subresult) {
                                                die('Problém s načtením technologie :' . mysql_error());
                                              }
                                              echo("<option value=\"\">vše</option>"); 
                                              while (list($id_obdobi,$_since) = mysql_fetch_row($subresult)){
                                              #podmínka pro nastavení defaultní hodnoty skupiny na User
                                                    if ($_POST['FPF_SINCE']==$id_obdobi){
                                                        echo("<option value=\"".$id_obdobi."\" selected>".$_since."</option>"); 
                                                    }else{
                                                        echo("<option value=\"".$id_obdobi."\">".$_since."</option>");
                                                    }
                                              }         
                                          ?>
                                        </select>
                    <select name="FPF_TO" onChange="RefreshForm()">
                                          <?php
                                              include "build/db_connect.inc.php"  ;
                                              $subquery="SELECT idObdobi,LEFT(P.to,10)
                                                      FROM period P     
                                                      WHERE P.to <=now()                                                
                                                      ORDER BY P.to ";                                                    
                                              $subresult = mysql_query($subquery);        
                                              if (!$subresult) {
                                                die('Problém s načtením technologie :' . mysql_error());
                                              }
                                              echo("<option value=\"\">vše</option>"); 
                                              while (list($id_obdobi,$_to) = mysql_fetch_row($subresult)){
                                              #podmínka pro nastavení defaultní hodnoty skupiny na User
                                                    if ($_POST['FPF_TO']==$id_obdobi){
                                                        echo("<option value=\"".$id_obdobi."\" selected>".$_to."</option>"); 
                                                    }else{
                                                        echo("<option value=\"".$id_obdobi."\">".$_to."</option>");
                                                    }
                                              }         
                                          ?>
                                        </select>
                </td>  
       <!--       <td class="tableculombhead"><input size="1" type="text" name="FF_FIRSTNAME" value="<?php echo($_SESSION['FF_TEMP'][FF_FIRSTNAME]);?>" OnChange="RefreshForm(this)">
              <input size="1" type="text" name="FF_LASTNAME" value="<?php echo($_SESSION['FF_TEMP'][FF_LASTNAME]);?>" OnChange="RefreshForm(this)"></td></td>
              <td class="tableculombhead"><input size="8" type="text" name="FF_ACCOUNT" value="<?php  echo($_SESSION['FF_TEMP'][FF_ACCOUNT]);?>" OnChange="RefreshForm(this)"></td>  -->   
           </tr><tr>
              <td class="tablehead" style="width:20px"></td>
              <td class="tablehead" style="width:80px">Období</td>
              <td class="tablehead" style="width:40px" >Počet zpoplatněných hovorů</td>
              <td class="tablehead">Provolané sekundy</td>
              <td class="tablehead">Provolaná částka</td>
           </tr><tr>
              
           </tr>


<?php 
          while(list($idobdobi,$od,$do)=mysql_fetch_row($result)){
                  $subquery="SELECT count(1),SUM(billsec),SUM(price)
                          FROM bill_date_faktura_view
                          WHERE id=".$idACC."
                          AND idObdobi=".$idobdobi."
                          ";
                     
      
                    if($_SESSION['FPF_TEMP'][FPF_PAYED]==1)
                    {
                        $subquery=$subquery." AND Zaplaceno=0 ";
                    }      
                        $subquery.=" HAVING COUNT(1)>0 ";   
                          
                          
                  $subresult = mysql_query($subquery);
                  if (!$subresult) {
                      die('3Období nelze načíst:' . mysql_error());
                  } 
                  $a=1;
                 while(list($pocet,$doba,$cena)=mysql_fetch_row($subresult)){
                  #Stridani barev řádků tabulky
                  if($a==1){$a=0; $barva="white";}else{$a=1;$barva="lightgrey"; }
                  #####################################################################
                  echo "<tr bgcolor=$barva>";
                  echo "<td style=\"width:20px\"><a href=\"".selfURL()."?NEXTFORM=27&NEWWIN=&idACCOUNT=".$idACC."&IDOBDOBI=".$idobdobi."\" target=\"_blank\"><img src=\"files/pictures/vyber.gif\"></a></td><td style=\"width:50px\">".$od." -> ".$do."</td><td style=\"width:40px\">".$pocet."</td><td>".($doba)."</td><td>".$cena."</td>";
                  echo "</tr>";
                }
          } 
      
?>
        </table>
        <input type="hidden" name="TREF_SUBMIT">
      </form>
    </div>
  </fieldset>
</div>