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

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

try {
  global $dbutil;
  global $stringUtil;

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

  $logger->info($data);

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

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

  $ret_array = array();    //return用
  $ret_detail = array();   //データ用
  $ret_detail2 = array();  //編集中店舗用用
  $ret_header = array();   //ヘッダー用


  //afm04の情報の取得
  //(税率混在制御区分の取得 + 新規時廃止は不可)
  $whereParams = array();
  $whereParams["mcd"] = $header["mcd"];
  $sql = file_get_contents("./sql/getMnmInfo.sql");
  $ret04 = $dbutil->select($sql, $whereParams);


  // pl/sql のコール
  $whereParams=array();
  $whereParams["wseq"]["in"]   = $wseq;
  $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $whereParams["vseq"]["in"]   = $header["vseq"];
  $whereParams["mcd"]["in"]    = $header["mcd"];
  $whereParams["cucd"]["in"]   = $header["cucd"];
  $whereParams["cuscd"]["in"]  = $header["cuscd"];
  $whereParams["ret"]["out"]   = "";
  $sql = file_get_contents("./sql/call_Fm_AF215X_Get.sql");
$logger->info($sql,$whereParams);
  $ret_plsql = $dbutil->call($sql,$whereParams);
$logger->info("ret_plsql",$ret_plsql);

  $wret = $ret_plsql["ret"];



  //ワークからデータ(編集中店舗)を取ってくる
  //新規の場合のafw81も作成しとってきて表示するのでここ
  $whereParams=array();
  $whereParams["wseq"] = $wseq;
  $whereParams["cucd"] = $header["cucd"];
  $sql = file_get_contents("./sql/searchAfw81.sql");
  $result2 = $dbutil->select($sql, $whereParams);
  $ret_detail2 = $result2;

  if((int)$wret > 0){     //1:正常(修正)

    /*afw80を取ってくる*/
    $ret_detail = getMeisai($dbutil,$header);
    $ret_header = $ret_detail[0]; //ヘッダーに埋める用

    //afm03の情報の取得
    $whereParams = array();
    $whereParams["cucd"]  = $ret_header["cucd"];
    $whereParams["cuscd"] = $ret_header["cuscd"];
    $sql = file_get_contents("./sql/getCunmInfo.sql");
    $logger->info($sql, $whereParams);
    $ret03 = $dbutil->select($sql, $whereParams);


    if($ret_detail == null) {
      $ret == "0";
      $cnt = 0;
    } else {
      $ret = "0";
      $ret_header["cunm"]     = $ret03[0]["cunm"];
      $ret_header["spnm"]     = $ret03[0]["spnm"];
      $ret_header["mnm"]      = $ret04[0]["mnm"];

      $ret_header["cutax"]    = $ret03[0]["cutax"];
      $ret_header["lblcutax"] = $ret03[0]["lblcutax"];
      $ret_header["cutaxdiv"] = $ret03[0]["cutaxdiv"];
      $ret_header["mtaxdiv"]  = $ret04[0]["mtaxdiv"];
      $ret_header["maxvlin"]  = count($ret_detail); //修正時の最大行数は、元伝票数(新規行挿入不可)
    }
    
  } else {
     if($wret == '0') { //0:正常(新規)
      $ret = "0";
      $cnt = 0;

      //afm03の情報の取得
      $whereParams = array();
      $whereParams["cucd"]  = $header["cucd"];
      $whereParams["cuscd"] = $header["cuscd"];
      $sql = file_get_contents("./sql/getCunmInfo.sql");
      $logger->info($sql, $whereParams);
      $ret03 = $dbutil->select($sql, $whereParams);

      //新規の場合、ヘッダ得意先店舗が廃止になっていないかチェック
      if(!$stringUtil->isEmpty($ret03[0]["stp"]) || $ret04[0]["stp"]) {
        $ret = '-9';
        $msg = '廃止されているため、登録できません';
        goto endSearch;
      }

      //初期値を埋める
      $ret_header["cucd"]     = $header["cucd"];
      $ret_header["cuscd"]    = $header["cuscd"];
      $ret_header["cunm"]     = $ret03[0]["cunm"];
      $ret_header["spnm"]     = $ret03[0]["spnm"];
      $ret_header["mnm"]      = $ret04[0]["mnm"];

      $ret_header["cutax"]    = $ret03[0]["cutax"];
      $ret_header["lblcutax"] = $ret03[0]["lblcutax"];
      $ret_header["cutaxdiv"] = $ret03[0]["cutaxdiv"];
      $ret_header["mtaxdiv"]  = $ret04[0]["mtaxdiv"];
      $ret_header["maxvlin"]  = "6"; //新規時の最大伝票行数は6行

      $ret_header["gdflg"] = '1';
      $ret_header["dat1"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"];

      goto endSearch;
    } else if($wret == '-1') { //エラー
      $ret = '-9';
      $msg = 'データ呼出に失敗しました';
      goto endSearch;
    } else if($wret == '-2') { //該当データなし
      $ret = '-9';
      $msg = '該当データがありません';
      goto endSearch;
    } else if($wret == '-1') { //同一仕入先・発注番号データ複数あり
      $ret = '-9';
      $msg = '同一発注番号のデータが複数あります。';
      goto endSearch;
    }

  }

  endSearch:

  $ret_array["ret"] = $ret;
  $ret_array["wret"] = $wret;
  $ret_array["msg"] = $msg;
  $ret_array["wseq"] = $wseq;
  $ret_array["ret_array"] = $ret_detail;
  $ret_array["ret_array2"] = $ret_detail2;
  $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);

?>
