<?php
//共通
require_once '../../base/common.php';
require_once './allways.php';
$logger = new logger(basename(__FILE__));
$logger->info("===af265x_getDetail_start===",$_POST);

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

$gInit_array = array( //初期値
  "iseq" => "",
);

try {
  global $dbutil;
  global $stringUtil;
  $valutil = new getValueUtil();

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

  // SQL実行
  $header = $data["header"];
$logger->info("data",$data);

  $ret_array = array(); //return用
  $ret_detail = array();  //データ用
  $ret_header = array();  //データ用

  // get_sbatのコール
  $retSbat = get_sbat($dbutil, $header);
  $logger->info("retSbat",$retSbat);


  if($retSbat["ret"] == '0') {

    //header情報の取得
    //締日、支払日は、伝票選択などでchangeイベント発火されていないと取得されないので、ここでも取り直す
    $retCunmInfo = getCunmInfo($dbutil, $header);
    $ret_header = $retCunmInfo;

    //前回残高の取得
    //まずaus75.sbatの取得
    $whereParams = array();
    $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
    $whereParams["cucd"] = $header["cucd"];
    $whereParams["cuscd"] = $header["cuscd"];
    $sql = file_get_contents("./sql/getAus75Sbat.sql");
    $retAus75Sbat = $dbutil->select($sql, $whereParams);
    //次に前回残高を取得
    $whereParams = array();
    $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
    $whereParams["cucd"] = $header["cucd"];
    $whereParams["cuscd"] = $header["cuscd"];
    $whereParams["sbat"]  = $retAus75Sbat[0]["sbat"];
    $sql = file_get_contents("./sql/getBal.sql");
    $retGetBal = $dbutil->select($sql, $whereParams);
    if($retGetBal == null) {
      $ret_header["bal"] = "0";
    } else {
      $ret_header["bal"] = $retGetBal[0]["bal"];
    }


    if(!$stringUtil->isEmpty($header["vseq"])) {


      //vseq入力ありの場合、aur32からデータ表示
      $whereParams=array();
      $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
      $whereParams["cucd"]   = $header["cucd"];
      $whereParams["cuscd"]  = $header["cuscd"];
      $whereParams["vseq"]   = $header["vseq"];
      $whereParams["viseq"]  = $header["viseq"];
      $whereParams["sts"]    = $retSbat["sts"];
      $whereParams["sbat"]   = $retSbat["sbat"];
      $sql = file_get_contents("./sql/search.sql");
    $logger->info('search>>'.$sql, $whereParams);
      $result = $dbutil->select($sql, $whereParams);
    
      if ($result == null) { //該当データなければ入力不可
        $ret = "-9";
        $cnt = 0;
        $ret_detail = $gInit_array;
        $ret_detail["cls"] = $header["cls"];
      } else {
        $ret = "0";

        $ret_detail = $result;
    
        $cnt = 0;
        foreach($ret_detail as $key => $val) {
          $cnt++;
        }
      }
    } else {
      //vseq入力なしの場合、新規
      //上でとったヘッダー部分だけそのまま渡し、明細は無し
      $ret = "0";
      $cnt = 0;
    }
  } else {
    $ret = "-9";
    $cnt = 0;
  }


  $ret_array["ret"] = $ret;
  $ret_array["ret_array"] = $ret_detail;
  $ret_array["ret_header"] = $ret_header;
  $ret_array["cnt"] = $cnt;
  $result = $ret_array;

  $logger->info('getDetail_ret',$result );
  $response["status"] = "OK";
  $response["data"]   = json_encode($result, true);

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

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

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

//  $dbutil->rollback();

} finally {
  print(json_encode($response));
}


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

?>
