<?php
# Billing_periods_edit_module ver.1.0
# last modification 17.2.2010
# Formulář pro stanovení a úpravu účtovacích intervalů 
# 
 function BPE_checkForm()
  {
    //if ($_GET['BPE_COST']=="" ) 
      // return false;
    return true;
  }
  if (IsSet($_GET['BPE_DELETE'])){
    $query="UPDATE billing_period SET validity=0 
            WHERE idBILLINGPERIOD=".$_GET['idBILLINGPERIOD']."
            ";
    $result = mysql_query($query);
    if (!$result) {
      die('1Aktualizace dat nebyla zapsána:' . mysql_error());
    }
  
  
  }


 if (IsSet($_GET['BPE_SUBMITED'])){
  $vysledek=BPE_checkForm();
  if ($vysledek===TRUE){
    include "build/db_connect.inc.php"  ;
    $query="SELECT idBILLINGPERIOD 
              FROM billing_period
              WHERE uptotime=".$_GET['BPE_INTERVAL']."
              AND unionnum=".$_GET['BPE_UNION']."";
    $result = mysql_query($query);
    if (mysql_num_rows($result)==0){
    //  $trans="START TRANSACTION";
      $query="INSERT INTO billing_period (uptotime,unionnum) 
              VALUES (".$_GET['BPE_INTERVAL'].",".$_GET['BPE_UNION'].")";
      $result = mysql_query($query);
      if (!$result) {
        die('2Aktualizace dat nebyla zapsána:' . mysql_error());
      }
    //  $query="SELECT last_insert_id()";
    //  $result = mysql_query($query);
    //  if (!$result) {
    //    die('3Aktualizace dat nebyla zapsána:' . mysql_error());
     // }
   //   list($idBILLINGPERIOD) = mysql_fetch_row($result);
    } else{ # jestli položka už existuje, tak se pouze načte
        list($idBILLINGPERIOD) = mysql_fetch_row($result)  ;
        $query="UPDATE billing_period SET validity=1 WHERE idBILLINGPERIOD=".$idBILLINGPERIOD."";
        $result = mysql_query($query);
        if (!$result) {
            die('3Aktualizace dat nebyla zapsána:' . mysql_error());
        }
  //    }
 //     include "db_connect.php"  ;
      # Tabulka obsahuje triger, který v případě existujícího záznamu 
      # s validitou 0 obnoví validitu na 1. V případě úplné duplicity
      # se neprovede díky triggeru nic!
 //     $query="INSERT INTO spec_billing_period (idTARIF,idBILLINGPERIOD) 
//              VALUES (".$_SESSION['idTARIF'].",".$idBILLINGPERIOD.")";
//      $result = mysql_query($query);
//      if (!$result) {
//        die('1Aktualizace dat nebyla zapsána:' . mysql_error());
      }
//      $trans="COMMIT";
//      $result =  mysql_query($trans);
 //     unset($trans);
              
      
      
    
    
    
    
    }
  
  
  
  }
 
 
 
 


?>
<center>
<div class="forms">
<h3>Intervaly účtovací mapy</h3><span class="backtoform"><a href="admin_page.php?NEXTFORM=10">Zpět</a> </span>
  <div class="userlist">
<form action="admin_page.php" method="get">
  <table>
    <tr>
        <td class="tablehead"></td>
        <td class="tablehead">Interval</td>
        <td class="tablehead">Počet jednotek</td>
        <td class="tablehead"></td>
    </tr><tr>
        <td class="tablehead">Nový interval:</td>
        <td class="tableculombhead"><input type="text" size=2 name="BPE_INTERVAL"></td>
        <td class="tableculombhead"><input type="text" size=2 name="BPE_UNION"></td>
        <td class="tableculombhead"><input type="submit" value="Vytvoř"></td>
  <input type="hidden" name="NEXTFORM" value="12">
  <input type="hidden" name="BPE_SUBMITED" value="TRUE">
  </tr>

<?php
include "build/db_connect.inc.php"  ;
/*$query="SELECT SBP.idBILLINGPERIOD,BP.uptotime,BP.unionnum 
        FROM spec_billing_period SBP
        JOIN billing_period BP ON BP.idBILLINGPERIOD=SBP.idBILLINGPERIOD 
             AND BP.validity=1
        WHERE SBP.validity=1
        AND SBP.idTARIF=".$_SESSION['idTARIF']."  
        ORDER BY uptotime ASC";
 */       
$query="SELECT BP.idBILLINGPERIOD,BP.uptotime,BP.unionnum 
        FROM billing_period BP  
        WHERE BP.validity=1
        ORDER BY uptotime ASC";        
        
        
$result = mysql_query($query);
if (!$result) {
      die('Data o intervalech nelze načíst:' . mysql_error());
   }
echo "<center>" ;   
echo "<div class=\"forms\">" ;


      echo "<table width=\"400px\" frame=\"border\" cellspacing=\"0px\">" ;
      $a=1;
      while (list($idBILLINGPERIOD,$uptotime,$union) = mysql_fetch_row($result))
      {
      #Stridani barev řádků tabulky
      
      if($a==1){$a=0; $barva="white";}else{$a=1;$barva="lightgrey"; }
      #####################################################################
        
        echo "<tr bgcolor=$barva>";
          
           
         echo "<td><a href=\"".selfURL()."?NEXTFORM=12&BPE_DELETE=True&idBILLINGPERIOD=".$idBILLINGPERIOD."\"><img src=\"files/pictures/no1.gif\"</a></td><td>".$uptotime."</td><td>".$union."</td><td></td>";
        echo "</tr>";
        
      
      
      }
      echo "</table>";
      
      
   echo "</div>" ;
  echo "</center>" ;          
          

?>
</form>
 

</center>
    </div>
</div>
