<?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']) {
  if (true === isset($_POST['page']) && true === is_numeric($_POST['page'])){
    $page = $_POST['page'];
  }
  else{
    $page = 1;
  }
  if (true === isset($_POST['rp']) && true === is_numeric($_POST['rp'])){
    $rp = $_POST['rp'];
  }
  else{
    $rp = 10;
  }
  if (true === isset($_POST['sortname']) && SiteLibs::RuleCheckSqlTextField($_POST['sortname'])){
    $VarOrder = $_POST['sortname'];
  }
  else{
    $VarOrder = 1;
  }
  if (true === isset($_POST['sortorder']) && ('desc' == $_POST['sortorder'] || 'asc' == $_POST['sortorder'])){
    $VarOrder .= ' '. $_POST['sortorder'];
  }
  else{
    $VarOrder .= ' '. 'desc';
  }

  $VarWhere = '';
  if (true === isset($_POST['demands_statusdate_b']) || true === isset($_POST['demands_statusdate_e'])){
    $VarWhere .= SiteLibs::ConvertTextDateToSql('demands_statusdate', $_POST['demands_statusdate_b'], $_POST['demands_statusdate_e']);
  }
  $VarWhere = SiteLibs::StripAndOrFirst($VarWhere);
//echo "VarWhere: ". $VarWhere ."<br />";
  $start = (($page-1) * $rp);

  $limit = "LIMIT $start, $rp";

  $VarLimit = "$start, $rp";

	$ObjDemandsMod = new DemandsMod($MS_SP);
	$total = $ObjDemandsMod->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $ArrDemands = $ObjDemandsMod->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);

  $ArrFields = array();
  foreach ($ArrDemands AS $key => $row){
//    if (true === is_numeric($row['demands_summa'])){
//      $VarTotal += $row['demands_summa'];
//    }
    $ArrFields[$key]['id'] = $row['demands_id'];
    $ArrFields[$key]['cell'][] = $row['demands_id'];
    $ArrFields[$key]['cell'][] = $row['demands_date'];
    $ArrFields[$key]['cell'][] = $row['sprstatus_name'];
    $ArrFields[$key]['cell'][] = $row['svttype_name'];
    $ArrFields[$key]['cell'][] = $row['svt_buxname'];
    $ArrFields[$key]['cell'][] = $row['svt_name'];
    $ArrFields[$key]['cell'][] = $row['svt_invnum'];
    $ArrFields[$key]['cell'][] = $row['svt_serialnum'];
    $ArrFields[$key]['cell'][] = $row['users_fio'];
  }
//  $ArrFields[$key+1]['id'] = 'total';
//  $ArrFields[$key+1]['cell'] = array('', '', '', '', 'Всего: '. number_format($VarTotal, 0, '.', ' '), '', '', '', '', '', '');
  $json = array("page" => $page, "total" => $total, "rows" => $ArrFields);
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>