<?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'] && 
  true === isset($_SESSION['SessionAuth']['id']) && 0 < $_SESSION['SessionAuth']['id'] &&
  ((true === isset($_GET['svt_id']) && true === is_numeric($_GET['svt_id'])) || 
    (true === isset($_GET['po_id']) && true === is_numeric($_GET['po_id'])))) {
  $ObjJqGridMod = new JqGridMod();
  $ObjJqGridMod->SetArrayIn($_GET);
  $VarWhere = '';
  if(true === isset($_GET['svt_id']) && true === is_numeric($_GET['svt_id'])){
    $VarWhere .= ' AND sp.svt_id = "'. $_GET['svt_id'] .'"';
  }
  if(true === isset($_GET['po_id']) && true === is_numeric($_GET['po_id'])){
    $VarWhere .= ' AND sp.po_id = "'. $_GET['po_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();
  $ObjSvtPoMod = new SvtPoMod($MS_SP);
  $count = $ObjSvtPoMod->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $total_pages = $ObjJqGridMod->GetTotalPages($count);
  $page = $ObjJqGridMod->GetPage();
  $ArrSvtPo = $ObjSvtPoMod->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);

  $ArrFields = array();
  if(true === isset($_GET['svt_id']) && true === is_numeric($_GET['svt_id'])){
    foreach ($ArrSvtPo AS $key => $row){
      $ArrFields[$key]['id'] = $row['svtpo_id'];
      $ArrFields[$key]['cell'][] = $row['po_name'];
      $ArrFields[$key]['cell'][] = $row['po_note'].'';
    }
  }
  if(true === isset($_GET['po_id']) && true === is_numeric($_GET['po_id'])){
    foreach ($ArrSvtPo AS $key => $row){
//      $VarName = '';
//      if(true === isset($row['svt_name']) && '' != $row['svt_name']){
//        $VarName = '<a target="_blank" class="ui-state-default" href="/'. $MS_SP->Sp_PathTop .'/SvtInformation.php?svt_id='. $row['svt_id'] .'">'. $row['svt_name'] .'</a>';
//      }
      $ArrFields[$key]['id'] = $row['svtpo_id'];
      $ArrFields[$key]['cell'][] = $row['svt_name'].'';
      $ArrFields[$key]['cell'][] = '<a target="_blank" class="ui-state-default" href="/'. $MS_SP->Sp_PathTop .'/SvtInformation.php?svt_id='. $row['svt_id'] .'">'. $row['svt_buxname'] .'</a>'.'';
      $ArrFields[$key]['cell'][] = $row['svt_invnum'].'';
      $ArrFields[$key]['cell'][] = $row['svt_serialnum'].'';
      $ArrFields[$key]['cell'][] = $row['svt_date'].'';
      $ArrFields[$key]['cell'][] = $row['svt_garant_date'].'';
    }
  }
  $json = array("page" => $page, "total" => $total_pages, "records" => $count,"rows" => $ArrFields);
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>