<?php  
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
include($_SERVER['DOCUMENT_ROOT'] .'/script/HeaderInc.php');
if('XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']) {
  $ObjJqGridMod = new JqGridMod();
  $ObjJqGridMod->SetArrayIn($_GET);
  $VarWhere = '';
  if (true === isset($_GET['demands_statusdate_b']) || true === isset($_GET['demands_statusdate_e'])){
    $VarWhere .= SiteLibs::ConvertTextDateToSql('a.demands_statusdate', $_GET['demands_statusdate_b'], $_GET['demands_statusdate_e']);
  }
  if (true === isset($_GET['demands_masterid']) && true === is_numeric($_GET['demands_masterid'])){
    $VarWhere .= " AND a.demands_masterid = '". $_GET['demands_masterid'] ."'";
  }  
  if (true === isset($_GET['sprstatus_id']) && true === is_numeric($_GET['sprstatus_id'])){
    $VarWhere .= " AND a.sprstatus_id = '". $_GET['sprstatus_id'] ."'";
  }  
  if (true === isset($_GET['svt_id']) && true === is_numeric($_GET['svt_id'])){
    $VarWhere .= " AND c.svt_id = '". $_GET['svt_id'] ."'";
  }
  if(true === isset($_GET['_search']) && 'true' == $_GET['_search']){
    if(true === isset($ObjJqGridMod->filters) && '' != $ObjJqGridMod->filters){
      $VarWhere .= ' AND '. $ObjJqGridMod->generateSearchStringFromFilter();
    }
    if(true === isset($ObjJqGridMod->searchField) && '' != $ObjJqGridMod->searchField){
      $VarWhere .= ' AND '. $ObjJqGridMod->generateSearchStringFromSearchField();
    }
  }
  $VarWhere = SiteLibs::StripAndOrFirst($VarWhere);
  $VarOrder = $ObjJqGridMod->GetOrder();
  $VarLimit = $ObjJqGridMod->GetLimit();
  $ObjDemandsMod = new DemandsMod($MS_SP);
  $count = $ObjDemandsMod->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $total_pages = $ObjJqGridMod->GetTotalPages($count);
  $page = $ObjJqGridMod->GetPage();
  $ArrDemands = $ObjDemandsMod->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);
  //SiteLibs::GetVarDump($ArrDemands);
  $ArrFields = array();
  foreach ($ArrDemands AS $key => $row){
    $ArrFields[$key]['id'] = $row['demands_id'];
    $ArrFields[$key]['cell'][] = '<a class="ui-state-default" href="/'. $MS_SP->Sp_PathTop .'/OpDemandsDetail.php?demands_id='. $row['demands_id'] .'" target="_blank" title="Подробнее о заявке">'. $row['demands_id'] .'</a>';
    $ArrFields[$key]['cell'][] = $row['demands_date'];
    $ArrFields[$key]['cell'][] = '<span style="background-color: '. $row['sprstatus_color'] .';">'. $row['sprstatus_name'] .'</span>';
    $ArrFields[$key]['cell'][] = $row['svttype_name'];
    if(true === isset($row['svt_name'])){
      $ArrFields[$key]['cell'][] = '<a class="ui-state-default" href="'. $MS_SP->Sp_PathTop .'/SvtInformation.php?svt_id='. $row['svt_id'] .'" target="_blank" title="Подробнее о '. $row['svt_name'] .'">'. $row['svt_name'] .'</a>';
    }
    else{
      $ArrFields[$key]['cell'][] = '';
    }
    $ArrFields[$key]['cell'][] = '<a class="ui-state-default" href="'. $MS_SP->Sp_PathTop .'/SvtInformation.php?svt_id='. $row['svt_id'] .'" target="_blank" title="Подробнее о '. $row['svt_buxname'] .'">'. $row['svt_buxname'] .'</a>';
    $ArrFields[$key]['cell'][] = $row['master_fio'].'';
    $ArrFields[$key]['cell'][] = $row['svt_invnum'];
    $ArrFields[$key]['cell'][] = $row['svt_serialnum'];
    $ArrFields[$key]['cell'][] = $row['structureorganization_title'];
    $ArrFields[$key]['cell'][] = $row['users_fio'];
    $ArrFields[$key]['cell'][] = $row['users_tel'];
    $ArrFields[$key]['cell'][] = $row['users_telint'];
    $ArrFields[$key]['cell'][] = $row['demands_intdocnum'];
    $ArrFields[$key]['cell'][] = $row['demands_intdocdate'];
  }
  $json = array("page" => $page, "total" => $total_pages, "records" => $count,"rows" => $ArrFields);
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>