<?php
//共通
require_once '../../base/common.php';
require_once '../../util/combo.php';
$logger = new logger(basename(__FILE__));
$logger->info("===af227x_search_start===");

//$response["status"] = "NG";

$header = Array();
foreach($_POST as $key => $val) {
  $header[$key] = $val;
}
$logger->info("header",$header);

$valutil = new getValueUtil();

//POPUP
$assign_var["hasPopMcd"] = "1";
$assign_var["hasPopJan"] = "1";
$assign_var["hasPopKtktcd"] = "1";
$assign_var["hasPopScls90"] = "1";

//list
$listutil = new getListUtil();
$assign_var["listGdcls9"] = $listutil->getListAfm90('6');
$assign_var["listCls2"] = $cbo_cls2_284x;
$assign_var["listCls3"] = $cbo_cls3_284x;
$assign_var["listOrd"] = $cbo_ord_284x;

$assign_var["hasError"] = false;

try {
  global $dbutil;
  global $stringUtil;

  $assign_var["isNoData"] = false;
  $assign_var["noeventClassHeader"] = "";
  $assign_var["noeventClassBody"]   = "";

  $requestBody = file_get_contents('php://input');
  $data = json_decode($requestBody, true);
$logger->info($data);


  $wseq = $dbutil->nextVal("innerwseq");
  $header["wseq"] = $wseq;


  $wcls2 = "";
  $wcls3 = "";
  $wcls1 = $header["rdo_cls1"];
  if($wcls1 == '1') {
    $wcls2 = $header["cls2"];
  }
  if($wcls1 == '2') {
    $wcls3 = $header["cls3"];
  }


  //表示件数の取得
  $retLcnt = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["PAGE_LINE"];
  if($retLcnt == null) {
    $lcnt = "100";
  } else {
    $lcnt = $retLcnt;
  }

  $pgcnt = $header["pagecnt"];  //今回のページNo  1

  $ret_array = array(); //return用
  $ret_pgctrl = array(); //ページコントロール用
  $ret_detail = array();  //データ用


  //まずデータ作成用のplsqlのコール
  $whereParams = array();
  $whereParams["wseq"]["in"] = $wseq;
  $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $whereParams["spktcd"]["in"] = $header["spktcd"];
  $whereParams["mcd"]["in"]  = $header["mcd"];
  $whereParams["jans"]["in"] = $header["jans"];
  $whereParams["jane"]["in"] = $header["jane"];
  $whereParams["cls1"]["in"] = $wcls1;
  $whereParams["cls2"]["in"] = $wcls2;
  $whereParams["cls3"]["in"] = $wcls3;
  $whereParams["kbn"]["in"]  = $header["kbn"];
  $whereParams["ret"]["out"] = "";
  $sql = file_get_contents("./sql/callFm_AF284X_Get.sql");
  $ret_plsql = $dbutil->call($sql,$whereParams);
  $logger->info("ret_plsql",$ret_plsql);

  if($ret_plsql["ret"] == "-1") {
    $ret = "1";
    $cnt = 0;
  } else if($ret_plsql["ret"] == "-9") {
    $assign_var["hasError"] = true;
    $assign_var["errorMsg"] = "エラーが発生しました";
  } else {

    $whereParams=array();
    $whereParams["wseq"] = $wseq;
    $whereParams["gdcls9"] = $header["gdcls9"];
    $whereParams["brand"]  = $header["brand"];


    //order by 句を、文字列置換で行う
    $ord_str = "";
    $sql = file_get_contents("./sql/search.sql");
    if($wcls1 == '0') {
      $ord_str = " order by b.jan";
    } else if($wcls1 == '1') {
      $ord_str = " order by a.dat1,b.jan";
    } else if($wcls1 == '2') {
      if($header["ord"] == '0') {
        $ord_str = " order by a.vlin,b.jan";
      } else {
        $ord_str = " order by a.vlin desc, b.jan desc";
      }
    }
    $sql = str_replace('$$ORD_STR$$',$ord_str,$sql);

    //↓ここから固定　--------------------
    $whereParams["lcnt"] = $lcnt; //表示行数

  $logger->info($sql, $whereParams);
    $result = $dbutil->select($sql, $whereParams);
    if ($result == null) {
      $ret = "1";
      $cnt = 0;
    } else {
      $ret = "0";
      $ret_detail = array();

      $lastpage = $result[count($result) - 1]["pgno"];    //最終ページ数
      $ret_detail = $result;

      //伝票金額合計算出
      $tamt = 0;
      $tamt2 = 0;
      $vcnt = 0;
      $header["tpno"] = $result[0]["tpno"];
      $header["tamt"] = $result[0]["tamt"];
      $header["titem"] = $result[0]["titem"];
    }
  }



  $dataList = $ret_detail;  //明細データ

  //ページコントロール用
  $header["lastpage"] = $lastpage;  //最終ページ

  $header["ret"] = $ret; //戻値
  $header["pgcnt"] = $ret_pgctrl;  //頁関連

} catch(userException $ue) {
  $retData["code"]  = $ue->getErrorCode();
  $retData["msg"]   = $ue->getErrorMessage();

  $assign_var["hasError"] = true;
  $assign_var["errorMsg"] = $ue->getErrorMessage();

  $response["status"] = "NG";
  $response["data"]   = $retData;

  //$dbutil->rollback();
} catch(Exception $e) {
  $response["status"] = "NG";
  $response["data"]   = $e->getMessage();

  $assign_var["hasError"] = true;
  $assign_var["errorMsg"] = $e->getErrorMessage();

//  $dbutil->rollback();
}

$assign_var["header"] = $header;
//$assign_var["dataList"] = $dataList;
$assign_var["dataList"] = json_encode($dataList);
$assign_var["footer"] = $footer;

$logger->info('assign_var',$assign_var);

//Smartyへアサイン
$smarty->assign($assign_var);
$smarty->display("index");

$logger->info("===af227x_search_end===", $response);

?>
