<?php
  ///////////////////////////////////////////////////
  //2007 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang.".php")) include "../lang/".$lang.".php";
     else include "../lang/ru.php";

  $title=$titlepage=$TEXT['support33'];

  require_once "../config.php";
  require_once "top.php";
  ?>
  <title><?php echo $title; ?></title>
  <link rel="StyleSheet" type="text/css" href="../utils/reader.css" />
  </head>
  <body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" bottommargin="0" topmargin="0" >
  <?php
  if($result2['user'] == 1) puterror2($TEXT['error-supp11']);
  if($result2['master'] == 1) puterror2($TEXT['error-supp11']);
  if(empty($_GET['zay_id'])) puterror2($TEXT['error-supp6']);
  if(strlen($_GET['zay_id'])>11) puterror2($TEXT['error-supp7']);
  $_GET['zay_id'] = rep($_GET['zay_id']);

       $query_zaya = "SELECT 
        tbl_supp_zayavka.*,
        tbl_users.familiya,
        tbl_users.name,
        tbl_users.otchestvo,
        tbl_supp_tema.tema,
        tbl_organizaciya.organizaciya
	FROM tbl_supp_zayavka 
	JOIN tbl_supp_tema ON tbl_supp_zayavka.tema_id=tbl_supp_tema.tema_id 
	LEFT JOIN tbl_users ON tbl_supp_zayavka.usr_id=tbl_users.user_id
	LEFT JOIN tbl_organizaciya ON tbl_supp_zayavka.org_id=tbl_organizaciya.org_id
	WHERE tbl_supp_zayavka.zay_id = '".$_GET['zay_id']."' ";
       $zaya = mysql_query($query_zaya);
         if(!$zaya) puterror2($TEXT['error-tbl26']);
            $zayavka = mysql_fetch_array($zaya,MYSQL_ASSOC);
             $tema2 = $zayavka['tema'];

             $user_full = $zayavka['familiya']." ".$zayavka['name']." ".$zayavka['otchestvo'];
             if(trim($user_full) == "") $user_full = $TEXT['support120'];
                else $user_full = $user_full;

             $res_org = $zayavka['organizaciya'];
             if(trim($res_org) == "") $res_org = $TEXT['support121'];
                else $res_org = $res_org;

             $query6 = "SELECT `login_id`, `fullname` FROM `tbl_logins` WHERE `login_id` = '".$zayavka['man_id']."' ";
                $man_tmp = mysql_query($query6);
                  if(!$man_tmp) puterror2($TEXT['error-tbl20']);
                   $manager = mysql_fetch_array($man_tmp,MYSQL_ASSOC);
                    $manager2 = $manager['fullname'];

             $query7 = "SELECT `login_id`, `fullname` FROM `tbl_logins` WHERE `login_id` = '".$zayavka['mast_id']."' ";
                $mast_tmp = mysql_query($query7);
                  if(!$mast_tmp) puterror2($TEXT['error-tbl20']);
                   $master = mysql_fetch_array($mast_tmp,MYSQL_ASSOC);
                    $master2 = $master['fullname'];

  if($_GET['p'] == 1)
  {
  $query = "INSERT INTO `tbl_supp_archive` (`arch_id` , `data` , `master` ,  `user` ,  `manager` , `tema` , `organizaciya` , `time_isp` , `zay_id`, `yea` , `mon` , `dy`, `problema`, `pic_tr`, `reshenie` )
                                    VALUES (NULL , NOW() , '$master2', '$user_full'  , '$manager2' , '$tema2' , '$res_org' , '".$zayavka['time_isp']."' , '".$_GET['zay_id']."'  , DATE_FORMAT(NOW(),'%Y') , DATE_FORMAT(NOW(),'%m') , DATE_FORMAT(NOW(),'%d'), '".$zayavka['problema']."', '".$zayavka['pic_tr']."', '".$zayavka['reshenie']."' )";
  if(mysql_query($query))
  {
     $query = "DELETE FROM `tbl_supp_zayavka` WHERE `zay_id` = '".$_GET['zay_id']."' LIMIT 1";
      if(mysql_query($query))
       {
       $query_ch = "SELECT `par_zay_id` FROM `tbl_ob_zay` WHERE `root_zay_id`='".$_GET['zay_id']."'";
       $result_clear = mysql_query($query_ch);
        if(!$result_clear) puterror2($TEXT['error-tbl36']);
         if(mysql_num_rows($result_clear)>0)
          {
          while($query_cleartmp = mysql_fetch_array($result_clear,MYSQL_ASSOC))
           {
           $query_clear3 = "SELECT `pic_tr` FROM `tbl_supp_zayavka` WHERE `zay_id`='".$query_cleartmp['par_zay_id']."'";
            $result_clear3 = mysql_query($query_clear3);
             if(!$result_clear3) puterror2($TEXT['error-tbl26']);
              if(mysql_num_rows($result_clear3)>0)
              {
               $query_cleartmp3 = mysql_fetch_array($result_clear3,MYSQL_ASSOC);
                if(file_exists("../".$query_cleartmp3['pic_tr']) && $query_cleartmp3['pic_tr'] !="-")
                 unlink("../".$query_cleartmp3['pic_tr']);
              }
          $query_clear4 = "DELETE FROM `tbl_supp_zayavka` WHERE `zay_id` = '".$query_cleartmp['par_zay_id']."' LIMIT 1";
            $result_clear4 = mysql_query($query_clear4);
              if(!$result_clear4) puterror2($TEXT['error-tbl26']);
           }
           $query_clear4 = "DELETE FROM `tbl_ob_zay` WHERE `root_zay_id`='".$_GET['zay_id']."'";
            if(!mysql_query($query_clear4)) puterror2($TEXT['error-tbl36']);
          }
       }else puterror2($TEXT['error-tbl26']);
    }
  }
  if($_GET['p'] == 2)
    {
    $probl= "";
    $pict = "";
    $resh = "";
    $query = "INSERT INTO `tbl_supp_archive` (`arch_id` , `data` , `master` ,  `user` ,  `manager` , `tema` , `organizaciya` , `time_isp` , `zay_id`, `yea` , `mon` , `dy`, `problema`, `pic_tr`, `reshenie` )
                                    VALUES (NULL , NOW() , '$master2', '$user_full'  , '$manager2' , '$tema2' , '$res_org' , '".$zayavka['time_isp']."' , '".$_GET['zay_id']."'  , DATE_FORMAT(NOW(),'%Y') , DATE_FORMAT(NOW(),'%m') , DATE_FORMAT(NOW(),'%d'), '".$probl."', '".$pict."', '".$resh."' )";
    if(mysql_query($query))
    {
     if(file_exists("../".$zayavka['pic_tr']) && $zayavka['pic_tr'] !="-")
         unlink("../".$zayavka['pic_tr']);

     $query = "DELETE FROM `tbl_supp_zayavka` WHERE `zay_id` = '".$_GET['zay_id']."' LIMIT 1";
      if(mysql_query($query))
       {
       $query_ch = "SELECT `par_zay_id` FROM `tbl_ob_zay` WHERE `root_zay_id`='".$_GET['zay_id']."'";
       $result_clear = mysql_query($query_ch);
        if(!$result_clear) puterror2($TEXT['error-tbl36']);
         if(mysql_num_rows($result_clear)>0)
          {
          while($query_cleartmp = mysql_fetch_array($result_clear,MYSQL_ASSOC))
           {
           $query_clear3 = "SELECT `pic_tr` FROM `tbl_supp_zayavka` WHERE `zay_id`='".$query_cleartmp['par_zay_id']."'";
            $result_clear3 = mysql_query($query_clear3);
             if(!$result_clear3) puterror2($TEXT['error-tbl26']);
              if(mysql_num_rows($result_clear3)>0)
              {
               $query_cleartmp3 = mysql_fetch_array($result_clear3,MYSQL_ASSOC);
                if(file_exists("../".$query_cleartmp3['pic_tr']) && $query_cleartmp3['pic_tr'] !="-")
                 unlink("../".$query_cleartmp3['pic_tr']);
              }
          $query_clear4 = "DELETE FROM `tbl_supp_zayavka` WHERE `zay_id` = '".$query_cleartmp['par_zay_id']."' LIMIT 1";
            $result_clear4 = mysql_query($query_clear4);
              if(!$result_clear4) puterror2($TEXT['error-tbl26']);
           }
           $query_clear4 = "DELETE FROM `tbl_ob_zay` WHERE `root_zay_id`='".$_GET['zay_id']."'";
            if(!mysql_query($query_clear4)) puterror2($TEXT['error-tbl36']);
          }
       }else puterror2($TEXT['error-tbl26']);
     }
    }
    //*************************************
    $ip = $_SERVER['REMOTE_ADDR'];
    $doing = $TEXT['error-supp24'];
    $what = $TEXT['support112']." ".$_GET['zay_id']." ".$TEXT['support52'];
    $who = $manager2;
    //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
            echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=manager.php' />
                   </head></html>";
           }else puterror2($TEXT['error-log']);
?>
</body>
</html>