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

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

try {
  global $dbutil;

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

  $logger->info($data);

  $valutil = new getValueUtil();

  // SQL実行
  $header = $data["header"];

  $ret_array  = array();  //return用
  $ret_detail = array();  //データ用
  $ret_header = array();  //ヘッダー用
  
  // sqlの条件を設定する
  $whereParams = array();
  $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $whereParams["vseq"] = $header["vseq"];
  $whereParams["cuiseq"] = $header["cuiseq"];
  $sql = file_get_contents("./sql/search.sql");
  $logger->info("whereParams",$whereParams);
  $result = $dbutil->select($sql, $whereParams);

  if ($result == null) {
    $ret = "0";
    $cnt = 0;

    $whereParams = array();
    $whereParams["dat"]  = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"];
    $sql = file_get_contents("./sql/getNextSdday.sql");
    $retNextSdaay = $dbutil->select($sql, $whereParams);
    $dat = $retNextSdaay[0]["dat"];

    $ret_header["climit"] = $dat;

  } else {
    $ret = "0";
    $ret_detail = $result;
    $ret_header = $result[0];

    $cnt = 0;
    $smvpno = 0;
    foreach($ret_detail as $key => $val) {
      $cnt++;

      //有効在庫情報
      $whereParams = array();
      $whereParams["spktcd"] = $val["spktcd1"];
      $whereParams["ijan"] = $val["ijan"];
      $sql = file_get_contents("./sql/getPsum.sql");
      $logger->info($sql, $whereParams);
      $retPsum = $dbutil->select($sql, $whereParams);
      $logger->info("retPsum",$retPsum);
      $ret_detail[$key]["psum"]= $stringUtil->nvl0($retPsum[0]["psum"]);     

      $smvpno = $smvpno + (int)$val["mvpno"];

    }

    $ret_header["smvpno"] = $smvpno;

  }

  $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("=== getDetail_end ===", $response);

?>
