<?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['purchase_date_b']) || true === isset($_GET['purchase_date_e'])){
    $VarWhere .= SiteLibs::ConvertTextDateToSql('purchase_datetill', $_GET['purchase_date_b'], $_GET['purchase_date_e']);
  }
  if (true === isset($_GET['purchasestatus_id']) || true === is_numeric($_GET['purchasestatus_id'])){
    $VarWhere .= "AND p.purchasestatus_id = '". $_GET['purchasestatus_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();
  $ObjPurchaseMod = new PurchaseMod($MS_SP);
  $count = $ObjPurchaseMod->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $total_pages = $ObjJqGridMod->GetTotalPages($count);
  $page = $ObjJqGridMod->GetPage();
  $ArrPurchase = $ObjPurchaseMod->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);
  //SiteLibs::GetVarDump($ArrPurchase);
  $VarTotal = 0;
  foreach ($ArrPurchase AS $key => $row){
    if (true === is_numeric($row['purchase_price'])){
      $VarTotal += $row['purchase_price'];
    }
    $json->rows[$key]['id'] = $row['purchase_id'];
    $json->rows[$key]['cell'][] = $row['purchase_name'].'';
    $json->rows[$key]['cell'][] = $row['structureorganization_title'].'';
    $json->rows[$key]['cell'][] = '<span style="background-color: '. $row['purchasestatus_color'] .';">'. $row['purchasestatus_name'] .'</span>';
    $json->rows[$key]['cell'][] = $row['purchase_price'];
    $json->rows[$key]['cell'][] = $row['purchase_datetill'].'';
  }
//  if(true === isset($key) && true === is_numeric($key)){
//    $ArrFields[$key+1]['id'] = 'total';
//    $ArrFields[$key+1]['cell'] = array('', '', '', $MS_SP->Sp_LangMessage['script']['ScrTotal'] .' '. number_format($VarTotal, 0, '.', ' '), '');
//  }
  $json->page = $page;
  $json->total = $total_pages;
  $json->records = $count;
  $json->userdata['purchasestatus_color'] = $MS_SP->Sp_LangMessage['script']['ScrTotal'];
  $json->userdata['purchase_price'] = $VarTotal;
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>