<?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['structureorganization_id']) && '' != $_GET['structureorganization_id']){
    $ObjStructureOrganizationMod = new StructureOrganizationMod($MS_SP);
    $ArrStructureOrganization = $ObjStructureOrganizationMod->GetFromDb('SqlAll', 'GetAll');
    $ArrStructureOrganizationTree = SiteLibs::array2tree($ArrStructureOrganization, $_GET['structureorganization_id'], 'child_nodes', 'structureorganization_id', 'structureorganization_parent_id');
    $ArrStructureOrganizationTree = array_keys($ArrStructureOrganizationTree);
    $ArrStructureOrganizationTree[] = $_GET['structureorganization_id'];
    $VarStructureOrganizationWhere = implode(',', $ArrStructureOrganizationTree);
    $VarWhere .= " AND a.structureorganization_id IN (". $VarStructureOrganizationWhere .")";
  }
  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();
  $ObjSvtMod = new SvtMod($MS_SP);
  $count = $ObjSvtMod->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $total_pages = $ObjJqGridMod->GetTotalPages($count);
  $page = $ObjJqGridMod->GetPage();
  $ArrSvt = $ObjSvtMod->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);

  $ArrFields = array();
  foreach ($ArrSvt AS $key => $row){
    $ArrFields[$key]['id'] = 'SvtForStructureList'. $row['svt_id'];
    if(true === isset($MS_SP->Sp_Config['DemandsWithoutAuth']) && 1 == $MS_SP->Sp_Config['DemandsWithoutAuth']){
      $ArrFields[$key]['cell'][] = '<a class="ui-state-default" href="'. $MS_SP->Sp_PathTop .'/Demands.php?UrlId='. $row['svt_id'] .'" title="Оформить" target="_blank">Оформить</a>';
    }
    else{
      $ArrFields[$key]['cell'][] = '-';
    }
    $ArrFields[$key]['cell'][] = $row['svt_name'].'';
    $ArrFields[$key]['cell'][] = $row['svt_buxname'].'';
    $ArrFields[$key]['cell'][] = $row['svttype_name'].'';
    $ArrFields[$key]['cell'][] = $row['svt_invnum'].'';
    $ArrFields[$key]['cell'][] = $row['svt_serialnum'].'';
    $ArrFields[$key]['cell'][] = $row['users_fio'].'';
  }
  $json = array("page" => $page, "total" => $total_pages, "records" => $count,"rows" => $ArrFields);
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>