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

  $ret03 = getCunmInfo($dbutil, $header); //afm03情報の取得(ヘッダーcucd変更時も同じのを呼ぶ)

  // pl/sql のコール
  $whereParams=array();
  $whereParams["wseq"]["in"] = $wseq;
  $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $whereParams["cucd"]["in"] = $header["cucd"];
  $whereParams["cuscd"]["in"] = $header["cuscd"];
  $whereParams["vseq"]["in"] = $header["vseq"];
  $whereParams["cuiseq"]["in"] = $header["cuiseq"];
  $whereParams["ret"]["out"] = "";
  $sql = file_get_contents("./sql/call_Fm_AF203X_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($wret == '-1') { //エラー
    $ret = '-9';
    $msg = 'データ呼出に失敗しました';
    goto endSearch;
  } else if($wret == '0') { //正常
    $ret = "0";
    $cnt = 0;

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

    //初期値を埋める
    //afm03, afm032からの情報。getCunmInfoで取得した内容
    //$ret_header = $ret03;
    $ret_header["cunm"]     = $ret03["cunm"];
    $ret_header["spnm"]     = $ret03["spnm"];

    $ret_header["dcubcd"]   = $ret03["dcubcd"];
    $ret_header["dcls"]     = $ret03["dcls"];
    $ret_header["spktcd"]   = $ret03["spktcd"];
    $ret_header["spktnm"]   = $ret03["spktnm"];
    $ret_header["cudcd"]    = $ret03["cudcd"];
    $ret_header["cudnm"]    = $ret03["cudnm"];
    $ret_header["ltxt1"]    = $ret03["ltxt1"];
    $ret_header["ltxt2"]    = $ret03["ltxt2"];
    $ret_header["dtxt1"]    = $ret03["dtxt1"];
    $ret_header["dtxt2"]    = $ret03["dtxt2"];
    $ret_header["com"]      = $ret03["com"];

    $ret_header["maxvlin"]  = $ret03["maxvlin"];
    //$ret_header["cutaxcls"] = $ret03["cutaxcls"];
    $ret_header["cutax"]    = $ret03["cutax"];
    $ret_header["lblcutax"] = $ret03["lblcutax"];
    $ret_header["asnmsg"]   = $ret03["asnmsg"];

    $ret_header["gdflg"] = '1';
    $ret_header["jdat"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"];
    if($header["deftoday"] == '1') { //当日
      $ret_header["ydat"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"];
    } else {                         //翌日
      $ret_header["ydat"] = getNextSdday($dbutil,$_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"]);
    }
    $ret_header["climit"] = getNextSdday($dbutil, $ret_header["ydat"]);
    $ret_header["jmcat"] = $header["defjmcat"];


    goto endSearch;
  } else if($wret == '2') { //確定済み
    $ret = '-9';
    $msg = '当日確定済みです';
    goto endSearch;
  } else {                   //1:正常(修正), 3:入力チェック済(表示のみ)

    //ワークからデータを取ってくる前に、jmcatのチェック
    $whereParams=array();
    $whereParams["wseq"] = $wseq;
    $whereParams["cucd"] = $header["cucd"];
    $whereParams["cuscd"] = $header["cuscd"];
    $sql = file_get_contents("./sql/getMinJmcat.sql");
    $retJmcat = $dbutil->select($sql, $whereParams);
    $wjmcat = $retJmcat[0]["wjmcat"];
    //OKの組み合わせ以外はエラー
    if(  ($header["defjmcat"]=='1' && $wjmcat=='1')
      || ($header["defjmcat"]=='1' && $wjmcat=='3')
      || ($header["defjmcat"]=='2' && $wjmcat=='2')
    ) {
      //OK!!
    } else {
      $ret = '-9';
      $msg = '入力種別が違います';
      deleteWork($dbutil,$header);
      goto endSearch;
    }

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


    if($ret_detail == null) {
      $ret == "0";
      $cnt = 0;
    } else {
      $ret = "0";
      $ret_header["maxvlin"]  = $ret03["maxvlin"];
      //$ret_header["cutaxcls"] = $ret03["cutaxcls"];
      $ret_header["cutax"]    = $ret03["cutax"];
      $ret_header["lblcutax"] = $ret03["lblcutax"];
      $ret_header["asnmsg"]   = $ret03["asnmsg"];
    }
    
  }

  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);

?>
