<?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(empty($_POST['zay_id'])) puterror2($TEXT['error-supp6']);
  if(strlen($_POST['zay_id'])>11) puterror2($TEXT['error-supp7']);
  if(strlen($_POST['reshenie'])>20000) puterror2($TEXT['error-supp15']);
  if(strlen($_POST['old_sost_id'])>11) puterror2($TEXT['error-supp21']);
  if(strlen($_POST['sost_id'])>11) puterror2($TEXT['error-supp21']);
  if(strlen($_POST['time_isp'])>10) puterror2($TEXT['error-supp23']);
  $_POST['zay_id'] = rep($_POST['zay_id']);
  $_POST['reshenie'] = rep($_POST['reshenie']);
  $_POST['old_sost_id'] = rep($_POST['old_sost_id']);
  $_POST['sost_id'] = rep($_POST['sost_id']);
  $_POST['time_isp'] = rep($_POST['time_isp']);

  if(!preg_match("/[0-9]+/",$_POST['time_isp'])) puterror2($TEXT['error-supp31']);

  $new_sost = $_POST['sost_id'];

  if(!empty($_POST['time_isp'])) $new_time = $_POST['time_isp'];
    else $new_time = 0;
 $new_time = intval($new_time);

  $query4_2 = "SELECT `position`, `sost_id` FROM `tbl_supp_sostoyanie` ORDER BY `position` DESC LIMIT 1";
    $sost2_tmp = mysql_query($query4_2);
     if(!$sost2_tmp) puterror2($TEXT['error-tbl23']);
      $sost2 = mysql_fetch_array($sost2_tmp,MYSQL_ASSOC);
      $sost_id2 = $sost2['sost_id'];

            $query_iskh = "SELECT 
                tbl_supp_zayavka.*,
                tbl_step.logika,
                tbl_step.tema_id
                FROM `tbl_supp_zayavka`
                JOIN tbl_step ON tbl_supp_zayavka.mar_id=tbl_step.mar_id AND tbl_supp_zayavka.step=tbl_step.position 
                WHERE tbl_supp_zayavka.zay_id='".$_POST['zay_id']."'";
              $zay_iskh_tmp = mysql_query($query_iskh);
               if(!$zay_iskh_tmp) puterror2(mysql_error());
                $zay_iskh = mysql_fetch_array($zay_iskh_tmp,MYSQL_ASSOC);
                $max_step_ins =  $zay_iskh['max_step'];
                $step_iskh =  $zay_iskh['step'];
                $step_ins =  $zay_iskh['step'] + 1;
                $logi_ka = $zay_iskh['logika'];

         if($sost_id2 !== $new_sost) $data_close = "0000-00-00";
           else 
            {
            //Обрабатываем маршрут
            $data_close = "NOW()";
            $time_isp = 0;
            $otv = 1;
            $ob_type_ins = 5;

           if($max_step_ins >= $step_ins)
            {
            $query_st = "SELECT `position`, `sost_id` FROM `tbl_supp_sostoyanie` ORDER BY `position` LIMIT 1";
             $sost_tmp = mysql_query($query_st);
              if(!$sost_tmp) puterror2($TEXT['error-tbl23']);
               $sost = mysql_fetch_array($sost_tmp,MYSQL_ASSOC);
            // Откл.
            if($logi_ka == "1")
            {
             $tmp_reshenie = $_POST['reshenie'];
             $query_mar = "SELECT 
                 tbl_step.mar_id,
                 tbl_step.login_id,
                 tbl_step.logika,
                 tbl_step.tema_id,
                 tbl_supp_tema.shablon
                 FROM tbl_step
                 LEFT JOIN tbl_supp_tema ON tbl_step.tema_id=tbl_supp_tema.tema_id
                 WHERE tbl_step.mar_id = ".$zay_iskh['mar_id']." AND tbl_step.position = ".$step_ins." ";
             $mar_tmp = mysql_query($query_mar);
              if(!$mar_tmp) puterror2(mysql_error());
               while($mar = mysql_fetch_array($mar_tmp,MYSQL_ASSOC))
               {
                 if($mar['shablon'] !== "") $tmp_reshenie .= "\r\n=====\r\n".$mar['shablon']."\r\n===============\r\n";
                  else $tmp_reshenie = $_POST['reshenie'];

                 $query_new = "INSERT INTO `tbl_supp_zayavka` (`zay_id` , `data_post` , `data_k_isp` , `data_close` , `man_id` , `mast_id` , `usr_id` , `sost_id` , `sroch_id` , `tema_id` , `problema` , `pic_tr` , `reshenie` , `kod` , `time_isp` , `org_id` , `ob_type`, `otv`, `mar_id`, `step`, `max_step`, `parent` )
                  VALUES (NULL , NOW() , NOW() , '0000-00-00' , '".$zay_iskh['man_id']."' , ".$mar['login_id']." , '".$zay_iskh['usr_id']."' , '".$sost['sost_id']."' , '".$zay_iskh['sroch_id']."' , '".$mar['tema_id']."' , '".$zay_iskh['problema']."' , '".$zay_iskh['pic_tr']."' , '".$tmp_reshenie."' , '".$zay_iskh['kod']."' , '$time_isp' , '".$zay_iskh['org_id']."' , '$ob_type_ins' , '$otv', '".$zay_iskh['mar_id']."', '$step_ins', '$max_step_ins', '".$zay_iskh['parent']."' )";
                  $new_step = mysql_query($query_new);
                  if(!$new_step) puterror2($TEXT['error-tbl26']);
                  $tmp_reshenie = $_POST['reshenie'];
                }
            }
            // Конец Откл.

            // И
           if($logi_ka == "2")
           {
            $query_count_logi = "SELECT COUNT(zay_id) AS count_zay FROM `tbl_supp_zayavka` WHERE `mar_id`='".$zay_iskh['mar_id']."' AND `parent`='".$zay_iskh['parent']."' AND `zay_id`!='".$_POST['zay_id']."' AND `step` = ".$step_iskh." AND `sost_id` != '".$sost_id2."' ";
            $zay_count_logi = mysql_query($query_count_logi);
            if(!$zay_count_logi) puterror2($TEXT['error-tbl26']);
             $zay_tmpcount = mysql_fetch_array($zay_count_logi,MYSQL_ASSOC);
              if($zay_tmpcount['count_zay'] < 1)
              {
               $query_logi2 = "SELECT `zay_id`, `reshenie` FROM `tbl_supp_zayavka` WHERE `mar_id`='".$zay_iskh['mar_id']."' AND `parent`='".$zay_iskh['parent']."' AND `zay_id`!='".$_POST['zay_id']."' AND `step` = ".$step_iskh." AND `sost_id` = '".$sost_id2."' ORDER BY `zay_id` ";
                $zay_iskh_logi2 = mysql_query($query_logi2);
                 if(!$zay_iskh_logi2) puterror2($TEXT['error-tbl26']);
                  if(mysql_num_rows($zay_iskh_logi2) > 0)
                   {
                    $tmp_reshenie = "\r\n=====".$TEXT['support12']."&#8470; ".$_POST['zay_id']."=======\r\n".$_POST['reshenie']."\r\n";
                    while($zay_tmpcount2 = mysql_fetch_array($zay_iskh_logi2,MYSQL_ASSOC))
                    {
                     $tmp_reshenie .= "\r\n=====".$TEXT['support12']."&#8470; ".$zay_tmpcount2['zay_id']."=======\r\n".$zay_tmpcount2['reshenie']."\r\n";
                    }

                    $tmp_reshenie2 = $tmp_reshenie;
                    $query_mar = "SELECT
                       tbl_step.mar_id,
                       tbl_step.login_id,
                       tbl_step.logika,
                       tbl_step.tema_id,
                       tbl_supp_tema.shablon
                       FROM tbl_step
                       LEFT JOIN tbl_supp_tema ON tbl_step.tema_id=tbl_supp_tema.tema_id
                       WHERE tbl_step.mar_id = ".$zay_iskh['mar_id']." AND tbl_step.position = ".$step_ins." ";
                    $mar_tmp = mysql_query($query_mar);
                     if(!$mar_tmp) puterror2(mysql_error());
                      while($mar = mysql_fetch_array($mar_tmp,MYSQL_ASSOC))
                       {
                       if($mar['shablon'] !== "") $tmp_reshenie2 .= "\r\n=====\r\n".$mar['shablon']."\r\n===============\r\n";
                        else $tmp_reshenie2 = $tmp_reshenie;
                       $query_new = "INSERT INTO `tbl_supp_zayavka` (`zay_id` , `data_post` , `data_k_isp` , `data_close` , `man_id` , `mast_id` , `usr_id` , `sost_id` , `sroch_id` , `tema_id` , `problema` , `pic_tr` , `reshenie` , `kod` , `time_isp` , `org_id` , `ob_type`, `otv`, `mar_id`, `step`, `max_step`, `parent` )
                        VALUES (NULL , NOW() , NOW() , '0000-00-00' , '".$zay_iskh['man_id']."' , ".$mar['login_id']." , '".$zay_iskh['usr_id']."' , '".$sost['sost_id']."' , '".$zay_iskh['sroch_id']."' , '".$mar['tema_id']."' , '".$zay_iskh['problema']."' , '".$zay_iskh['pic_tr']."' , '".$tmp_reshenie2."' , '".$zay_iskh['kod']."' , '$time_isp' , '".$zay_iskh['org_id']."' , '$ob_type_ins' , '$otv', '".$zay_iskh['mar_id']."', '$step_ins', '$max_step_ins', '".$zay_iskh['parent']."' )";
                        $new_step = mysql_query($query_new);
                        if(!$new_step) puterror2($TEXT['error-tbl26']);
                        $tmp_reshenie2 = $tmp_reshenie;
                        }
                   }
             }
           }
           // Конец И

          // ИЛИ
          if($logi_ka == "3")
           {
            $tmp_reshenie = $_POST['reshenie'];
            $query_mar = "SELECT
                tbl_step.mar_id,
                tbl_step.login_id,
                tbl_step.logika,
                tbl_step.tema_id,
                tbl_supp_tema.shablon
                FROM tbl_step
                LEFT JOIN tbl_supp_tema ON tbl_step.tema_id=tbl_supp_tema.tema_id
                WHERE tbl_step.mar_id = ".$zay_iskh['mar_id']." AND tbl_step.position = ".$step_ins." ";
             $mar_tmp = mysql_query($query_mar);
              if(!$mar_tmp) puterror2(mysql_error());
               while($mar = mysql_fetch_array($mar_tmp,MYSQL_ASSOC))
               {
                if($mar['shablon'] !== "") $tmp_reshenie .= "\r\n=====\r\n".$mar['shablon']."\r\n===============\r\n";
                  else $tmp_reshenie = $_POST['reshenie'];

                 $query_new = "INSERT INTO `tbl_supp_zayavka` (`zay_id` , `data_post` , `data_k_isp` , `data_close` , `man_id` , `mast_id` , `usr_id` , `sost_id` , `sroch_id` , `tema_id` , `problema` , `pic_tr` , `reshenie` , `kod` , `time_isp` , `org_id` , `ob_type`, `otv`, `mar_id`, `step`, `max_step`, `parent` )
                  VALUES (NULL , NOW() , NOW() , '0000-00-00' , '".$zay_iskh['man_id']."' , ".$mar['login_id']." , '".$zay_iskh['usr_id']."' , '".$sost['sost_id']."' , '".$zay_iskh['sroch_id']."' , '".$mar['tema_id']."' , '".$zay_iskh['problema']."' , '".$zay_iskh['pic_tr']."' , '".$tmp_reshenie."' , '".$zay_iskh['kod']."' , '$time_isp' , '".$zay_iskh['org_id']."' , '$ob_type_ins' , '$otv', '".$zay_iskh['mar_id']."', '$step_ins', '$max_step_ins', '".$zay_iskh['parent']."' )";
                  $new_step = mysql_query($query_new);
                  if(!$new_step) puterror2($TEXT['error-tbl26']);
                  $tmp_reshenie = $_POST['reshenie'];
                }

            $query_logi2 = "SELECT `zay_id` FROM `tbl_supp_zayavka` WHERE `mar_id`='".$zay_iskh['mar_id']."' AND `parent`='".$zay_iskh['parent']."' AND `zay_id`!='".$_POST['zay_id']."' AND `step` = ".$step_iskh." ";
             $zay_iskh_logi2 = mysql_query($query_logi2);
              if(!$zay_iskh_logi2) puterror2($TEXT['error-tbl26']);
               while($zay_logi2 = mysql_fetch_array($zay_iskh_logi2,MYSQL_ASSOC))
               {
               $query_logi_rem2 = "DELETE FROM `tbl_supp_zayavka` WHERE `zay_id`='".$zay_logi2['zay_id']."' ";
                $zay_iskh_logi_rem2 = mysql_query($query_logi_rem2);
                 if(!$zay_iskh_logi_rem2) puterror2($TEXT['error-tbl26']);
              }
         }
         // Конец ИЛИ

            $query_add_step = "UPDATE `tbl_supp_zayavka` SET `reshenie` = '".$_POST['reshenie']."', `step`='$step_ins' WHERE `zay_id`='".$zay_iskh['parent']."'";
              $zay_add_step_tmp = mysql_query($query_add_step);
               if(!$zay_add_step_tmp) puterror2($TEXT['error-tbl26']);
          }
          else
          {
            $query_iskh2 = "SELECT `time_isp` FROM `tbl_supp_zayavka` WHERE `parent`='".$zay_iskh['parent']."' AND `zay_id`!='".$_POST['zay_id']."' ";
              $zay_iskh_tmp2 = mysql_query($query_iskh2);
               if(!$zay_iskh_tmp2) puterror2($TEXT['error-tbl26']);
                while($zay_iskh2 = mysql_fetch_array($zay_iskh_tmp2,MYSQL_ASSOC))
                {
                  $zay_time += $zay_iskh2['time_isp'];
                }
            $zay_time_all = $zay_time + $new_time;
          // Откл.
          if($logi_ka == "1")
            {
            $query_close = "UPDATE `tbl_supp_zayavka` SET `time_isp` = '$zay_time_all', `data_close` = NOW(), `sost_id` = '$new_sost', `reshenie` = '".$_POST['reshenie']."', `step`='$max_step_ins' WHERE `zay_id`='".$zay_iskh['parent']."'";
              $zay_close_tmp = mysql_query($query_close);
               if(!$zay_close_tmp) puterror2($TEXT['error-tbl26']);
            }
          // Конец Откл.

          // И
          if($logi_ka == "2")
          {
          $query_count_logi = "SELECT COUNT(zay_id) AS count_zay FROM `tbl_supp_zayavka` WHERE `mar_id`='".$zay_iskh['mar_id']."' AND `parent`='".$zay_iskh['parent']."' AND `zay_id`!='".$_POST['zay_id']."' AND `step` = ".$step_iskh." AND `sost_id` != '".$sost_id2."' ";
          $zay_count_logi = mysql_query($query_count_logi);
            if(!$zay_count_logi) puterror2($TEXT['error-tbl26']);
            $zay_tmpcount = mysql_fetch_array($zay_count_logi,MYSQL_ASSOC);
              if($zay_tmpcount['count_zay'] == 0)
              {
               $query_logi2 = "SELECT `zay_id`, `reshenie` FROM `tbl_supp_zayavka` WHERE `mar_id`='".$zay_iskh['mar_id']."' AND `parent`='".$zay_iskh['parent']."' AND `step` = ".$step_iskh." AND `sost_id` = '".$sost_id2."' ORDER BY `zay_id` ";
                $zay_iskh_logi2 = mysql_query($query_logi2);
                 if(!$zay_iskh_logi2) puterror2($TEXT['error-tbl26']);
                  if(mysql_num_rows($zay_iskh_logi2) > 0)
                   {
                    $tmp_reshenie2 = "\r\n=====".$TEXT['support12']."&#8470; ".$_POST['zay_id']."=======\r\n".$_POST['reshenie']."\r\n===============";
                    while($zay_tmpcount2 = mysql_fetch_array($zay_iskh_logi2,MYSQL_ASSOC))
                     {
                     $tmp_reshenie2 .= "\r\n=====".$TEXT['support12']."&#8470; ".$zay_tmpcount2['zay_id']."=======\r\n".$zay_tmpcount2['reshenie']."\r\n=========\r\n";
                     }
                   }
              }

            $query_close = "UPDATE `tbl_supp_zayavka` SET `time_isp` = '$zay_time_all', `data_close` = NOW(), `sost_id` = '$new_sost', `reshenie` = '".$tmp_reshenie2."', `step`='$max_step_ins' WHERE `zay_id`='".$zay_iskh['parent']."'";
              $zay_close_tmp = mysql_query($query_close);
               if(!$zay_close_tmp) puterror2($TEXT['error-tbl26']);
           }
          // Конец И
          // ИЛИ
          if($logi_ka == "3")
           {
            $query_logi2 = "SELECT `zay_id` FROM `tbl_supp_zayavka` WHERE `mar_id`='".$zay_iskh['mar_id']."' AND `parent`='".$zay_iskh['parent']."' AND `zay_id`!='".$_POST['zay_id']."' AND `step` = ".$step_iskh." ";
              $zay_iskh_logi2 = mysql_query($query_logi2);
               if(!$zay_iskh_logi2) puterror2($TEXT['error-tbl26']);
                while($zay_logi2 = mysql_fetch_array($zay_iskh_logi2,MYSQL_ASSOC))
                {
                $query_logi_rem2 = "DELETE FROM `tbl_supp_zayavka` WHERE `zay_id`='".$zay_logi2['zay_id']."' ";
                $zay_iskh_logi_rem2 = mysql_query($query_logi_rem2);
                 if(!$zay_iskh_logi_rem2) puterror2($TEXT['error-tbl26']);
                }

            $query_close = "UPDATE `tbl_supp_zayavka` SET `time_isp` = '$zay_time_all', `data_close` = NOW(), `sost_id` = '$new_sost', `reshenie` = '".$_POST['reshenie']."', `step`='$max_step_ins' WHERE `zay_id`='".$zay_iskh['parent']."'";
              $zay_close_tmp = mysql_query($query_close);
               if(!$zay_close_tmp) puterror2($TEXT['error-tbl26']);
            }
           // Конец ИЛИ

          }
        }

    $query_st = "SELECT `sostoyanie` FROM `tbl_supp_sostoyanie` WHERE `sost_id`='$new_sost' ";
       $sost_tmp = mysql_query($query_st);
         if(!$sost_tmp) puterror2($TEXT['error-tbl23']);
           $sost = mysql_fetch_array($sost_tmp,MYSQL_ASSOC);

  $query = "UPDATE `tbl_supp_zayavka` SET `time_isp` = '$new_time', `data_close` = $data_close, `sost_id` = '$new_sost', `reshenie` = '".$_POST['reshenie']."' WHERE `zay_id`='".$_POST['zay_id']."'";
  if(mysql_query($query))
  {
    //*************************************
    if($_POST['old_sost_id'] !== $_POST['sost_id']) $log_sost = $TEXT['support40']." - ".$sost['sostoyanie'].", ";
      else $log_sost = "";

    if(!empty($_POST['time_isp'])) $log_time = $TEXT['support47']." ".$_POST['time_isp']." ".$TEXT['support48'].", ";
      else $log_time = "";

    $ip = $_SERVER['REMOTE_ADDR'];
    $doing = $TEXT['error-supp32'];
    $what = $log_sost." ".$log_time." ".$TEXT['support1']." ".$_POST['zay_id'];
    $who = $result2['fullname'];
    //*************************************
    $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=master.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-tbl26']);
?>
</body>
</html>