<?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['_search']) && 'true' == $_GET['_search']){
    if(true === isset($ObjJqGridMod->filters) && '' != $ObjJqGridMod->filters){
      $VarWhere .= $ObjJqGridMod->generateSearchStringFromFilter();
    }
    if(true === isset($ObjJqGridMod->searchField) && '' != $ObjJqGridMod->searchField){
      $VarWhere .= $ObjJqGridMod->generateSearchStringFromSearchField();
    }
  }
  $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);
  $purchase_price = 0;
  $purchase_value = 0;
  foreach ($ArrPurchase AS $key => $row){
    $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_quantity'].'';
    $json->rows[$key]['cell'][] = $row['purchase_value'].'';
    $json->rows[$key]['cell'][] = $row['purchase_datetill'].'';
    $json->rows[$key]['cell'][] = $row['purchase_note'].'';
    $purchase_price += $row['purchase_price'];
    $purchase_value += $row['purchase_value'];
  }
  $json->userdata['purchase_price'] = $purchase_price;
  $json->userdata['purchase_value'] = $purchase_value;
  $json->page = $page;
  $json->total = $total_pages;
  $json->records = $count;
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>