<?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();  //ヘッダー用
  
  // afm02の情報の取得
  $whereParams = array();
  $whereParams["ijan"]   = $header["ijan"];
  $sql = file_get_contents("./sql/searchAfm02.sql");
  $logger->info("whereParams",$whereParams);
  $result = $dbutil->select($sql, $whereParams);

  if ($result == null) {
    $ret = "0";
    $cnt = 0;
  } else {
    $ret = "0";
    $ret_detail = $result;
    $ret_header = $result[0];

    // afs741(受注統計)の情報の取得
    $whereParams = array();
    $whereParams["ijan"]   = $header["ijan"];
    $whereParams["spktcd"] = $header["spktcd"];
    $sql = file_get_contents("./sql/searchAfs741.sql");
    $logger->info("whereParams",$whereParams);
    $retAfs741 = $dbutil->select($sql, $whereParams);

    //結果を配列に追加
    $ret_header = array_merge($ret_header, $retAfs741[0]);

    //在庫情報とか(afv3105, afv3109, afm02d)の取得
    $whereParams = array();
    $whereParams["ijan"]   = $header["ijan"];
    $whereParams["spktcd"] = $header["spktcd"];
    $sql = file_get_contents("./sql/getPsum.sql");
    $logger->info("whereParams",$whereParams);
    $retPsum = $dbutil->select($sql, $whereParams);

    //在庫情報は、明細の他明細として返す
    $ret_detail = $retPsum;
    $cntpsum = count($ret_detail); //在庫明細行数


  }

  $ret_array["ret"] = $ret;
  $ret_array["ret_array"]  = $ret_detail;
  $ret_array["ret_header"] = $ret_header;
  $ret_array["cnt"] = $cnt;
  $ret_array["cntpsum"] = $cntpsum;
  $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);

?>
