<?php
# ID: 27
# Faktura_detail obdobi .1.0
# last modification 18.5.2010
# Formulář pro filtraci a vyhledání trunků
#
?>
<script language="JavaScript">

 
 function RefreshForm(theForm){
      document.forms.form.submit();
  
  }
</script>


<?php
       if ($_GET['idACCOUNT']!=''){
          $idACC=$_GET['idACCOUNT'];
      }else{
          $idACC=$_POST['idACCOUNT'];
      }

  
      if ($_GET['IDOBDOBI']!=''){
          $_SESSION['FPD_TEMP'][IDOBDOBI]=$_GET['IDOBDOBI'];
      }
      if ($_POST['IDOBDOBI']!=''){
          $_SESSION['FPD_TEMP'][IDOBDOBI]=$_POST['IDOBDOBI'];
      }
     
    
    
            
      
      
      $query="SELECT CR.idCALL_RECORDS,CDR.calldate, CDR.dst, 1, T.name, CDR.billsec, CR.price
              
              FROM call_records CR 
              JOIN cdr CDR ON CDR.idCDR=CR.idCDR AND CR.validity=1
              
              LEFT JOIN tarif T ON T.idTARIF=CR.idTARIF
              WHERE CR.idTECHNOLOGY=1
              AND CR.validity=1 ";
              
      if(IsSet($_POST['DIRECTION']) )
      {
        $_SESSION['FPD_TEMP'][DIRECTION]= $_POST['DIRECTION'];
      }else{
        $_SESSION['FPD_TEMP'][DIRECTION]='';
      }
      if ($_SESSION['FPD_TEMP'][DIRECTION]!=2){
       $query.=" AND CR.id=".$idACC." "; 
      }
      if ($_SESSION['FPD_TEMP'][DIRECTION]==2){
          $query=$query." AND (CDR.dst=(SELECT name FROM sip_buddies WHERE id=".$idACC.")) "   ;
      }else{
         $query=$query." AND CR.idDIRECTION=1 ";
      }      
              
   
/*                              
      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['NULOVE']) )
      {
        $_SESSION['FPD_TEMP'][NULOVE]= $_POST['NULOVE'];
      }else{
        $_SESSION['FPD_TEMP'][NULOVE]='';
      }
      if ($_SESSION['FPD_TEMP'][NULOVE]!="1"){
          $query=$query." AND (CDR.billsec<>0 OR CDR.billsec<>'') "   ;
      }
      
      if(IsSet($_POST['UNLIMITTIME']) )
      {
        $_SESSION['FPD_TEMP'][UNLIMITTIME]= $_POST['UNLIMITTIME'];
      }else{
        $_SESSION['FPD_TEMP'][UNLIMITTIME]='';
      }
      if ($_SESSION['FPD_TEMP'][UNLIMITTIME]!="1"){
            $query.="AND EXISTS(SELECT 1 FROM bill_date_faktura_view WHERE idCALL_RECORDS=CR.idCALL_RECORDS 
                                AND idObdobi=".$_SESSION['FPD_TEMP'][IDOBDOBI].") ";
      }
          
      
       if(IsSet($_POST['idCDR']) )
      {
        $_SESSION['FPF_TEMP'][idCDR]= $_POST['idCDR'];
      }
      if ($_SESSION['FPF_TEMP'][idCDR]!=''){
          $query=$query." AND CR.idcall_records=\"".$_SESSION['FPF_TEMP'][idCDR]."\""   ;
      } 
      
      if(IsSet($_POST['CALLDEST']) )
      {
        $_SESSION['FPF_TEMP'][CALLDEST]= $_POST['CALLDEST'];
      }
      
      if ($_SESSION['FPF_TEMP'][CALLDEST]!=''){
      
          if ($_SESSION['FPD_TEMP'][DIRECTION]==2){
              $query=$query." AND CDR.src=\"".$_SESSION['FPF_TEMP'][CALLDEST]."\""   ;
           }else{
             $query=$query." AND CDR.dst=\"".$_SESSION['FPF_TEMP'][CALLDEST]."\""   ;
           
           }   
      }
      
      if(IsSet($_POST['CALLDATETO']) )
      {
        $_SESSION['FPF_TEMP'][CALLDATETO]= $_POST['CALLDATETO'];
      }
      if ($_SESSION['FPF_TEMP'][CALLDATETO]!=''){
          $query=$query." AND CDR.calldate<=\"".$_SESSION['FPF_TEMP'][CALLDATETO]."\""   ;
      }
      if(IsSet($_POST['CALLDATESINCE']) )
      {
        $_SESSION['FPF_TEMP'][CALLDATESINCE]= $_POST['CALLDATESINCE'];
      }
      if ($_SESSION['FPF_TEMP'][CALLDATESINCE]!=''){
          $query=$query." AND CDR.calldate>=\"".$_SESSION['FPF_TEMP'][CALLDATESINCE]."\""   ;
      }
      

##########################################################################
      #zjisteni poctu stran
        $itemperpage=50;
            $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());
      }   
      
################################################################################
# nacteni informaci o uzivateli    
  $querymenu="SELECT U.jmeno,U.prijmeni,SB.name
              FROM user U
              JOIN user_account UA ON UA.idUSER=U.idUSER AND U.validity=1
              JOIN sip_buddies SB ON SB.id=UA.id
              WHERE UA.idTECHNOLOGY=1
              AND UA.validity=1
              AND UA.id=".$idACC."" ;
  $resultmenu = mysql_query($querymenu);              
  if (!$resultmenu) {
           die('Nelze načíst informace o uživateli:' . mysql_error());
      }
   list($ujmeno,$uprijmeni,$unazev)=mysql_fetch_row($resultmenu);    
      
        
    
?>

<center>
<div id="calllist">
<h2>Call list</h2>
<h3><?php echo "Uživatel - ".$ujmeno." ".$uprijmeni.", účet - ".$unazev." "; ?>
<fieldset>
  <legend>Výpis hovorů</legend>
    
    <form name="form" action="index.php"  method="post">
        <table align="center" width="800px" cellpadding="0px" cellspacing="0px">
           <tr>
              <td class="tableculombhead" colspan=5 style="text-align:left">
                  <?php
                    for ($i=0 ;$i<$pages;$i++) {
                  if ($i==$_GET['PAGE'])
                  {
                      $styl="<span class=\"pagelistselected\">";
                  }else{
                      $styl="<span class=\"pagelist\">";
                  }
                  echo $styl;
                  unset ($styl);
                      
                      echo ("<a href=\"".selfURL()."?NEXTFORM=27&idACCOUNT=".$idACC."&NEWWIN=&PAGE=".$i."\">".($i+1)." </a>");
                      echo "</span>";   
                  }
                  ?>
                
                </td>
                <td colspan=2 class="tableculombhead">Neomezené časové období:<input type="checkbox" name="UNLIMITTIME" OnChange="RefreshForm(this);" value="1" <?php if($_SESSION['FPD_TEMP'][UNLIMITTIME]==1){echo "checked"; }?>></td>   
                 
           </tr><tr>   
                <td class="tableculombhead" colspan=5>
                    Odchozí :<input type="radio" name="DIRECTION" OnChange="RefreshForm(this);" value="1" <?php if($_SESSION['FPD_TEMP'][DIRECTION]==1 || $_SESSION['FPD_TEMP'][DIRECTION]==''){echo "checked"; }?>>
                    Příchozí :<input type="radio" name="DIRECTION" OnChange="RefreshForm(this);" value="2" <?php if($_SESSION['FPD_TEMP'][DIRECTION]==2){echo "checked"; }?> >
                    
                    
                </td>  
                <td class="tableculombhead" colspan=2>Zobrazit nespojené hovory:<input type="checkbox" name="NULOVE" value="1" OnChange="RefreshForm(this);"<?php if($_SESSION['FPD_TEMP'][NULOVE]==1){echo "checked"; }?>></td>
   
           </tr><tr>
              <td class="tablehead">idCALL_RECORDS</td>
              <td class="tablehead">Čas volání</td>
             <?php
                  if ($_SESSION['FPD_TEMP'][DIRECTION]==2) {echo "<td class=\"tablehead\">Zdroj</td>";}else{echo "<td class=\"tablehead\">Cíl</td>";}
              ?>
              <td class="tablehead"></td>
              <td class="tablehead">Tarif</td>
              <td class="tablehead">Délka hovoru [sek.]</td>
              <td class="tablehead">Cena hovoru[Kč]</td>
           </tr><tr>
              <td class="tableculombhead"><input type="text" size=2 name="idCDR" OnChange="RefreshForm(this);" value="<?php echo $_SESSION['FPF_TEMP'][idCDR] ;?>"></td>
              <td class="tableculombhead">
                                <input id="f_rangeStart" name="CALLDATESINCE" <?php if($_SESSION['FPF_TEMP'][CALLDATESINCE]=='') echo "disabled";?> OnChange="RefreshForm(this);" value="<?php echo $_SESSION['FPF_TEMP'][CALLDATESINCE] ; ?>" />
                                  <button type="button" id="f_rangeStart_trigger"  >Od</button>
                                      <script type="text/javascript">
                                        new Calendar.setup({
                                                showTime : 24,
                                                inputField: "f_rangeStart",
                                                dateFormat: "%Y-%m-%d %H:%M:%S",
                                                trigger: "f_rangeStart_trigger",
                                                bottomBar: false,
                                                onSelect: function() {
                                                document.getElementById("f_rangeStart").disabled=false;
                                                    this.hide();
                                                }
                                        });
                  
                                      </script>
                                     
              
              </td>
              
              <td class="tableculombhead"><input type="text" size=8 name="CALLDEST" OnChange="RefreshForm(this);" value="<?php echo $_SESSION['FPF_TEMP'][CALLDEST] ;?>"></td>
              <td class="tableculombhead"></td>
              <td class="tableculombhead"></td>
              <td class="tableculombhead"></td>
              <td class="tableculombhead"></td>
           </tr><tr>
              <td class="tableculombhead"> </td>
              <td class="tableculombhead">       
                            <input id="f_rangeStop" name="CALLDATETO" <?php if($_SESSION['FPF_TEMP'][CALLDATETO]=='') echo "disabled";?>  OnChange="RefreshForm(this);" value="<?php echo $_SESSION['FPF_TEMP'][CALLDATETO] ; ?>" />
                              <button type="button" id="f_rangeStop_trigger">Do</button><br><input type="submit" value="Potvrdit"/>
                                      <script type="text/javascript">
                                        new Calendar.setup({
                                                showTime : 24,
                                                inputField: "f_rangeStop",
                                                dateFormat: "%Y-%m-%d %H:%M:%S",
                                                trigger: "f_rangeStop_trigger",
                                                bottomBar: false,
                                                onSelect: function() {
                                                document.getElementById("f_rangeStop").disabled=false;
                                                    this.hide();
                                                }
                                        });
                  
                                      </script>
            </td>
              <td class="tableculombhead"></td>
              <td class="tableculombhead"></td>
              <td class="tableculombhead"></td>
               <td class="tableculombhead"></td>
                <td class="tableculombhead"></td>
              </tr><tr>
           </tr>


<?php    
          $result = mysql_query($query);
          if (!$result) {
             die('Záznamy volání nelze načíst:' . mysql_error());
          }
          
                  $a=1;
                 while(list($idCALLRECORDS,$calldate,$dest,$runkname,$idtarif,$billsec,$price)=mysql_fetch_row($result)){
                  #Stridani barev řádků tabulky
                  if($a==1){$a=0; $barva="white";}else{$a=1;$barva="lightgrey"; }
                  #####################################################################
                  echo "<tr bgcolor=$barva>";
                  if($price==''){$price='-';}
                  if ($_SESSION['FPD_TEMP'][DIRECTION]==2){
                      echo "<td>".$idCALLRECORDS."</td><td>".$calldate."</td><td  style=\"text-align:center\">".$dest."</td><td>".$trunkname."</td><td></td><td style=\"text-align:center\">".$billsec."</td><td style=\"text-align:center\">-</td>";
                  }else{
                      echo "<td>".$idCALLRECORDS."</td><td>".$calldate."</td><td  style=\"text-align:center\">".$dest."</td><td>".$trunkname."</td><td>".$idtarif."</td><td style=\"text-align:center\">".$billsec."</td><td style=\"text-align:center\">".$price."</td>";
                  }
                  echo "</tr>";
                }
          
            
?>
        </table>
        <input type="hidden" name="idACCOUNT" value="<?php  echo $idACC; ?>">
        <input type="hidden" name="NEWWIN">
      </form>
  
  </fieldset>
</div>
