<?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 = '';
//  $VarWhere = " AND a.sprstatus_id IN (3)";
  if (true === isset($_GET['repair_numdocdate_b']) || true === isset($_GET['repair_numdocdate_e'])){
    $VarWhere .= SiteLibs::ConvertTextDateToSql('repair_numdocdate', $_GET['repair_numdocdate_b'], $_GET['repair_numdocdate_e']);
  }
 
  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();
  $ObjRepairMod = new RepairMod($MS_SP);
  $count = $ObjRepairMod->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $total_pages = $ObjJqGridMod->GetTotalPages($count);
  $page = $ObjJqGridMod->GetPage();
  $ArrRepair = $ObjRepairMod->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);
  $VarTotal = 0;
  foreach ($ArrRepair AS $key => $row){
    if (true === is_numeric($row['repair_summa'])){
      $VarTotal += $row['repair_summa'];
    }
//    else{
//      $row['repair_summa'] = 0;
//    }
    $json->rows[$key]['id'] = $row['repair_id'];
    $json->rows[$key]['cell'][] = $row['repair_id'];
    $json->rows[$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'] .'" target="_blank">'. $row['demands_id'] .'</a>';
    $json->rows[$key]['cell'][] = '<span style="background-color: '. $row['repairstatus_color'] .';">'. $row['repairstatus_name'] .'</span>';
    $json->rows[$key]['cell'][] = $row['organiz_name'];
    $json->rows[$key]['cell'][] = $row['repair_summa'];
//    $json->rows[$key]['cell'][] = number_format($row['repair_summa'], 0, '.', ' ');
    $json->rows[$key]['cell'][] = $row['repair_content'];
    $json->rows[$key]['cell'][] = $row['repair_numdoc'];
    $json->rows[$key]['cell'][] = $row['repair_numdocdate'];
    $json->rows[$key]['cell'][] = $row['users_fio'];
    $json->rows[$key]['cell'][] = $row['repair_date_b'];
    $json->rows[$key]['cell'][] = $row['repair_date_e'];
  }
//  $ArrFields[$key+1]['id'] = 'total';
  //$ArrFields[$key+1]['cell'] = array('', '', '', '', $MS_SP->Sp_LangMessage['script']['ScrTotal'] .' '. number_format($VarTotal, 0, '.', ' '), '', '', '', '', '', '');
  //$json = array("page" => $page, "total" => $total_pages, "records" => $count,"rows" => $ArrFields);
  $json->page = $page;
  $json->total = $total_pages;
  $json->records = $count;
  $json->userdata['organiz_name'] = $MS_SP->Sp_LangMessage['script']['ScrTotal'];
  $json->userdata['repair_summa'] = $VarTotal;
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>