<?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();  //ヘッダー用

  $msg = "";
  

  //端数処理区分,税率混在区分の取得
  //ついでに、新規時の日付初期値(afm04のﾘｰﾄﾞﾀｲﾑを使用)もセレクトする
  $whereParams = array();
  $whereParams["mcd"] = $header["mcd"];
  $whereParams["sdday"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"];
  $sql = file_get_contents("./sql/getMnmInfo.sql");
  $ret04 = $dbutil->select($sql, $whereParams);


  if(!$stringUtil->isEmpty($header["vseq"])) {
    // sqlの条件を設定する
    $whereParams = array();
    $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
    $whereParams["vseq"] = $header["vseq"];
    $whereParams["mcd"]  = $header["mcd"];
    $sql = file_get_contents("./sql/search.sql");
    $logger->info("whereParams",$whereParams);
    $result = $dbutil->select($sql, $whereParams);

    if ($result == null) {
      $ret = '-1';
      $msg = '該当データがありません';
    } else {
      $ret = "0";
      $ret_detail = $result;
      $ret_header = $result[0];
      $ret_header["scutaxc"] = $ret04[0]["scutaxc"]; //端数処理区分
      $ret_header["mtaxdiv"] = $ret04[0]["mtaxdiv"]; //税率混在区分

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

        //税率情報
        $ret_tax = $valutil->FmGetTaxrdivTaxrt($val["ijan"],$val["dat"],$_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"]);
  $logger->info("ret_get_tax",$ret_tax);
        $ret_detail[$key]["taxrt"] = $ret_tax["taxrt"];
        $ret_detail[$key]["taxdiv"] = $ret_tax["taxdiv"];
        $ret_detail[$key]["taxinfo"] = $ret_tax["taxinfo"];

        //合計計算
        $ret_detail[$key]["scst"] = $valutil->calc_amt($val["mcd"],$val["mvpno"],$val["cst"],'2',$_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"],$ret_header["scutaxc"]);
      }
    }
  } else {

    //新規時のチェックと初期ぎ
    $ret = "0";
    $cnt = 0;

    //新規時廃止仕入先は不可
    if(!$stringUtil->isEmpty($ret04[0]["stp"])) {
      $ret = '-9';
      $msg = '廃止されているため、登録できません';
    } else {
      //新規時初期値
      $ret_header["dat1"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"];
      $ret_header["dat"] = $ret04[0]["dat"];
      $ret_header["scutaxc"] = $ret04[0]["scutaxc"]; //端数処理区分
      $ret_header["mtaxdiv"] = $ret04[0]["mtaxdiv"]; //税率混在区分
    }

  }

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

?>
