<?php


function L_APPOITMENT_OVERWIEV2() {
global $thiss,$error,$SQL_DATABASE,$INTERFACE_ADDR,$SQL_USER_PASS,$SQL_USER,$MENU_INIT,$CURRENT_MENU ;
MENU();
DETECT_TABLE_COLUMN();



//1FORMAT_INST: ADRES | STATUS | Problem | Status_INFO | DATA_UMOWY | L_APP_DATE |PERIOD | L_APP_TYE |  L_APP_INFO |Ilosc Terminow/LAST_3-mce |
//2FORMAT_SERW: ULICA | STATUS | Problem | Opis_PROBLE |DATA PROBLE | L_APP_DATE |PERIOD | L_APP_TYE |  L_APP_INFO |Ilosc Terminow/LAST_3-mce |
//2FORMAT_TELB: T_NAME | STATUS | Problem| Opis_PROBLE |DATA PROBLE | L_APP_DATE |PERIOD | L_APP_TYE |  L_APP_INFO |Ilosc Problemow/Last_3-mce |


$APP_FIELDS[FIELDS][0]= array("ADRES","A_STATUS","A_PROBLEM","INFO","DATA","APP_U","APP_D","APP_P","APP_I");
$APP_FIELDS[FIELDS][1]= array("S_ADDRESS_STREET","A_STATUS","A_PROBLEM","A_STATUS_INFO","P_DEAL_DATE","L_APPOITMENT_USER","L_APPOITMENT_DATE","L_APPOITMENT_PERIOD","L_APPOITMENT_INFO");
$APP_FIELDS[FIELDS][2]= array("S_ADDRESS_STREET","A_STATUS","A_PROBLEM","A_PROBLEM_DESC","A_PROBLEM_DATE","L_APPOITMENT_USER","L_APPOITMENT_DATE","L_APPOITMENT_PERIOD","L_APPOITMENT_INFO");
$APP_FIELDS[FIELDS][3]= array("T_TELBOX_NAME","A_STATUS","A_PROBLEM","A_PROBLEM_DESC","A_PROBLEM_DATE","L_APPOITMENT_USER","L_APPOITMENT_DATE","L_APPOITMENT_PERIOD","L_APPOITMENT_INFO");
$APP_FIELDS[DESC]= array("TABLE","DESC","S_ADDRESS_STREET","A_STATUS","A_PROBLEM","A_PROBLEM_DESC","A_PROBLEM_DATE","L_APPOITMENT_USER","L_APPOITMENT_DATE","L_APPOITMENT_PERIOD","L_APPOITMENT_INFO");
$APP_FIELDS[4][SELECT]=" select * from tmp order by APP_U, UNIX_TIMESTAMP(APP_D)";
$APP_FIELDS[5][SELECT]=" DROP TABLE tmp ";


 $APP_FIELDS[0][SELECT]=" CREATE TEMPORARY TABLE tmp ( TABELA char(10), OPIS char(240)";
 foreach ($APP_FIELDS[FIELDS][0] as $field) {
  $APP_FIELDS[0][SELECT]=$APP_FIELDS[0][SELECT].", $field char(240)";
 }
 $APP_FIELDS[0][SELECT]=$APP_FIELDS[0][SELECT]." )";


$APP_FIELDS[F][1][SELECT]=" INSERT INTO tmp ( TABELA,OPIS";
$APP_FIELDS[V][1][SELECT]=" select  'USERS2','INSTALACJE'";
 foreach ($APP_FIELDS[FIELDS][0] as $field) {
  $APP_FIELDS[F][1][SELECT]=$APP_FIELDS[F][1][SELECT].",$field"; }
 foreach ($APP_FIELDS[FIELDS][1] as $field) {
  $APP_FIELDS[V][1][SELECT]=$APP_FIELDS[V][1][SELECT].",$field"; }
 $APP_FIELDS[1][SELECT]=$APP_FIELDS[F][1][SELECT]." ) ".$APP_FIELDS[V][1][SELECT]." from USERS2 where A_ADM_COMPANY like '".$_SESSION[ADM_COMPANY]."' and L_APPOITMENT_TYPE!='' and A_STATUS='WAITING'  ";


$APP_FIELDS[F][1][SELECT]=" INSERT INTO tmp ( TABELA,OPIS";
$APP_FIELDS[V][1][SELECT]=" select  'USERS2','SERWISY'";
 foreach ($APP_FIELDS[FIELDS][0] as $field) {
  $APP_FIELDS[F][1][SELECT]=$APP_FIELDS[F][1][SELECT].",$field"; }
 foreach ($APP_FIELDS[FIELDS][2] as $field) {
  $APP_FIELDS[V][1][SELECT]=$APP_FIELDS[V][1][SELECT].",$field"; }
 $APP_FIELDS[2][SELECT]=$APP_FIELDS[F][1][SELECT]." ) ".$APP_FIELDS[V][1][SELECT]." from USERS2 where A_ADM_COMPANY like '".$_SESSION[ADM_COMPANY]."' and L_APPOITMENT_TYPE!='' and A_STATUS!='WAITING' and A_PROBLEM!='' ";

$APP_FIELDS[F][1][SELECT]=" INSERT INTO tmp ( TABELA,OPIS";
$APP_FIELDS[V][1][SELECT]=" select  'TELBOXES','MAGISTRALE'";
 foreach ($APP_FIELDS[FIELDS][0] as $field) {
  $APP_FIELDS[F][1][SELECT]=$APP_FIELDS[F][1][SELECT].",$field"; }
 foreach ($APP_FIELDS[FIELDS][3] as $field) {
  $APP_FIELDS[V][1][SELECT]=$APP_FIELDS[V][1][SELECT].",$field"; }
 $APP_FIELDS[3][SELECT]=$APP_FIELDS[F][1][SELECT]." ) ".$APP_FIELDS[V][1][SELECT]." from TELBOXES where A_ADM_COMPANY like '".$_SESSION[ADM_COMPANY]."' and L_APPOITMENT_TYPE!='' and A_PROBLEM!='' ";





$conn=mysql_connect($INTERFACE_ADDR,$SQL_USER,$SQL_USER_PASS);
if (!$conn) die("ERROR podczas tworzenia polaczenia do bazy $SQL_DATABASE !\n");
mysql_select_db($SQL_DATABASE) or die("ERROR podczas wyboru bazy R50!");
//echo $APP_FIELDS[1][SELECT];


mysql_query($APP_FIELDS[0][SELECT],$conn) or die("Blad zapytania0 ZAP_SQL $APP_FIELDS[0][SELECT] *$zapSQL*!");
mysql_query($APP_FIELDS[1][SELECT],$conn) or die("Blad zapytania1 ZAP_SQL $APP_FIELDS[1][SELECT] *$zapSQL*!");
mysql_query($APP_FIELDS[2][SELECT],$conn) or die("Blad zapytania2 ZAP_SQL $APP_FIELDS[2][SELECT] *$zapSQL*!");
mysql_query($APP_FIELDS[3][SELECT],$conn) or die("Blad zapytania3 ZAP_SQL $APP_FIELDS[3][SELECT] *$zapSQL*!");
$result = mysql_query($APP_FIELDS[4][SELECT],$conn) or die("Blad zapytania4 ZAP_SQL $APP_FIELDS[4][SELECT] *$zapSQL*!");
mysql_query($APP_FIELDS[5][SELECT],$conn) or die("Blad zapytania5 ZAP_SQL $APP_FIELDS[5][SELECT] *$zapSQL*!");

    echo "<table border=1 cellspacing=1 cellpadding=1><tr>";
      foreach ($APP_FIELDS[DESC] as $field) {
	echo "<td>$field</td>";
        } echo "</tr>";
     while ($h=mysql_fetch_row($result)) { 
    echo "<tr>";
        $i=0;
      foreach ($APP_FIELDS[DESC] as $field) {
	echo "<td>";
        echo "$h[$i]";
        echo "</td>";
         $i++;
        }

    echo "</tr>";
       }
   echo "</table>";

mysql_free_result ($result); 

}
