<?php
# ID: -4-     IdFunction v tabulce Function   
# User_detail_module ver.1.0
# last modification 28.2.2010
# Formulář pro náhled záznamu o aktuálním uživateli
#   
# 
# 
  require "build/db_connect.inc.php"  ;
  $query="SELECT jmeno,prijmeni,login,email,last_change
          FROM user
          WHERE login=\"".$_SESSION['USER']."\"
          ";
  $result = mysql_query($query);
  if (!$result) {
      die('Údaje o uživateli nelze načíst:' . mysql_error());
  }
  list($jmeno,$prijmeni,$login,$email,$last_change) = mysql_fetch_row($result);  
?>
<div class="forms">
<h3>Informace o přihlášeném uživateli</h3>
<fieldset>
<legend>Obecné informace</legend>
    <form >
      <table style="width:500px">
          <tr>
              <td class="tablehead">Login:</td><td class="tableculombhead"><input type="text" size="8" id="UI_LOGIN" value="<?php echo ($login);?>" disabled></td>
              <td class="tablehead"> Poslední změna:</td><td class="tableculombhead"><input type="text" id="UI_LASTCHANGE" value="<?php echo ($last_change);?>" disabled></td>
          </tr><tr>
              <td class="tablehead">Jméno uživatele:</td><td class="tableculombhead"><input  size=8 type="text" id="UI_NAME" value="<?php echo ($jmeno." ".$prijmeni);?>" disabled></td>
              <td class="tablehead">Email:</td><td class="tableculombhead"><input type="text" id="UI_MAIL" value="<?php echo ($email);?>" disabled></td>
          </tr><tr>         
      </table>
  </form>
</fieldset>

<fieldset>
  <legend>Vlastňené účty</legend>
  <div class="list">
  <table >
    
      <tr>
        <td class="tablehead"></td>
        <td class="tablehead">Login</td>
        <td class="tablehead">Kontext</td>
        <td class="tablehead">Tarifní skupina</td>
        <td class="tablehead">Způsob platby</td>
        <td class="tablehead">Credit</td>
        <td class="tablehead">Cena za toto období</td>
        <td class="tablehead">Čas za toto období</td>
      </tr>
      <tr>
        <td class="tableculombhead"></td>
        <td class="tableculombhead"></td>
        <td class="tableculombhead"></td>
        <td class="tableculombhead"></td>
        <td class="tableculombhead"></td>
        <td class="tableculombhead">[kč]</td>
        <td class="tableculombhead">[kč]</td>
        <td class="tableculombhead">[sek]</td>
      </tr>
    
<?php
  include "build/db_connect.inc.php"  ;
  $query="SELECT UA.id,T.table_name,T.technology,T.idTECHNOLOGY
          FROM user U
          JOIN user_account UA ON U.idUSER=UA.idUSER AND U.validity=1
          JOIN technology T ON T.idTECHNOLOGY=UA.idTECHNOLOGY
          WHERE U.login=\"".$_SESSION['USER']."\" 
          ";
############### strankovani #########################  
   #zjisteni poctu stran
      #nastaveni radku na stranku   
      $itemperpage=10;
            $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('Údaje o účtu nelze načíst:' . mysql_error());
      } 
  #kod pro vypocet počtu stran
      if ($remainder!=0) $pages++;
     # echo "<div class=\"pagelist\">"  ;
      for ($i=0 ;$i!=$pages;$i++) {
        if ($i==$_GET['PAGE'])
        {
          echo "<span class=\"pagelistselected\">";
           echo ("<a href=\"".selfURL()."?PAGE=".$i."\">".($i+1)." </a>");
          echo "</span>"; 
        }
        else
        {
          echo "<span class=\"pagelist\">";
          echo ("<a  href=\"".selfURL()."?PAGE=".$i."\">".($i+1)." </a>");
          echo "</span>";   
        }
      }
      echo "<div class=\"userlist\">";
     
      $a=1;
###########################################  
  
  
  
  
  
  while(list($idT,$table_name,$technology,$idTECHNOLOGY)=mysql_fetch_row($result)){
    $subquery="SELECT TAB.id,TAB.name,TAB.context,TM.name,PT.name,BC.value
            FROM user_account UA
            JOIN ".$table_name." TAB ON TAB.id=UA.id
            LEFT JOIN assign_tarif_mix ATM ON ATM.id=UA.id AND ATM.idTECHNOLOGY=UA.idTECHNOLOGY AND ATM.validity=1
            LEFT JOIN tarif_mix TM ON TM.idTARIFMIX=ATM.idTARIFMIX AND TM.validity=1
            LEFT JOIN spec_pay_type SPT ON TM.idTARIFMIX=SPT.idTARIFMIX AND SPT.validity=1
            LEFT JOIN pay_type PT ON PT.idPAYTYPE=SPT.idPAYTYPE AND PT.validity=1
            LEFT JOIN bill_credit BC ON UA.idBILL=BC.idBILL
            WHERE UA.idTECHNOLOGY=".$idTECHNOLOGY."
            AND TM.start_date < now() 
            AND UA.validity=1
            AND UA.id=".$idT."
            AND TM.start_date=(SELECT TM1.start_date
                                                  FROM assign_tarif_mix ATM1
                                                  JOIN tarif_mix TM1 ON TM1.idTARIFMIX=ATM1.idTARIFMIX AND TM1.validity=1
                                                  WHERE ATM1.id=".$idT."
                                                  AND ATM1.idTECHNOLOGY=".$idTECHNOLOGY."
                                                  AND ATM1.validity=1
                                                  AND TM1.start_date < now()
                                                  ORDER BY start_date DESC
                                                  LIMIT 1)
            "; 
     $subresult = mysql_query($subquery);
   if (!$subresult) {
      die('Údaje o účtu nelze načíst1:' . mysql_error());
    }
    
    
######### Zjisteni informaci o provolane dobe a částky konkrétního účtu            
            $subsubquery="SELECT idObdobi FROM period P WHERE P.since<=now() AND P.to>=now()  ";
            $subsubresult = mysql_query($subsubquery);
            if (!$subsubresult) {
                die('Údaje o účtu nelze načíst1:' . mysql_error());
            }             
            list($idobdobi)=mysql_fetch_row($subsubresult);


            $subsubquery="SELECT SUM(billsec),SUM(price)
                          FROM bill_date_faktura_view
                          WHERE id=".$idT."
                          AND idObdobi=".$idobdobi."  ";
            $subsubresult = mysql_query($subsubquery);
            if (!$subsubresult) {
                die('Údaje o účtu nelze načíst1:' . mysql_error());
            }             
            list($billsec,$price)=mysql_fetch_row($subsubresult);
            
            
            
            
            
##############V připadě že účet není aktivován žádným tarifem, vypiš náhradní informace
    if (mysql_num_rows($subresult)==0){
        $subquery="SELECT TAB.id,TAB.name,TAB.context,'N/A','N/A',BC.value
                    FROM user_account UA
                    JOIN ".$table_name." TAB ON TAB.id=UA.id
                    LEFT JOIN bill_credit BC ON UA.idBILL=BC.idBILL
                    WHERE UA.idTECHNOLOGY=".$idTECHNOLOGY." 
                    AND UA.id=".$idT."
                    AND UA.validity=1";
        $subresult = mysql_query($subquery);
        if (!$subresult) {
            die('Údaje o účtu nelze načíst1:' . mysql_error());
        }
    }
    
    list($id,$name,$context,$targroupname,$paytype,$credit)=mysql_fetch_row($subresult);
    if($a==1){$a=0; $barva="white";}else{$a=1;$barva="lightgrey"; }
      echo "<tr bgcolor=$barva>";
    echo ("<td><a href=\"".selfURL()."?NEXTFORM=28&NEWWIN=&idACCOUNT=".$id."&IDOBDOBI=".$idobdobi."\" target=\"_blank\"><img src=\"files/pictures/vyber.gif\"></a></td><td>".$name."</td><td>".$context."</td><td >".$targroupname."</td><td >".$paytype."</td><td >".$credit."</td><td>".$price."</td><td>".$billsec."</td>");
    echo "</tr>";
  }        

?>
  </table>

  </div>
  </fieldset>
</div>
