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

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

$gInit_array = array( //初期値
  "iseq" => "",
);

//ret = 0 OK, 1=仕入伝票複数, 2=受注伝票複数, -1=仕入伝票無し(ありえない), -2=受注伝票無し, -9=エラー
try {
  global $dbutil;
  global $stringUtil;
  $valutil = new getValueUtil();

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

  // SQL実行
  $header = $data["header"];
$logger->info("header>>",$header);
  $ret_array = array(); //return用
  $ret_detail = array();  //データ用
  $ret_header = array();  //ヘッダー・フッター情報
  $ret_header["kflg_kbn"] = "OFF";

  $ret = "0";
  $newMode = "NEW";
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];

  $w_cuiseq = "";
  if (!$stringUtil->isEmpty($header["cvseq"])) {  //発注伝票番号入力
    $whereParams = array();
    $whereParams["ktktcd"] = $ktktcd;
    $whereParams["mcd"] = $header["mcd"];
    $whereParams["cvseq"] = $header["cvseq"];
    $sql = file_get_contents("./sql/getCuiseq.sql");
$logger->info($sql, $whereParams);
    $result = $dbutil->selectnocount($sql, $whereParams);
$logger->info("getviseq>>",$result);
    if ($result[0]["cnt"] == "1") {
      $w_cuiseq = $result[0]["cuiseq"];
      $newMode = "UNDER"; //動きとしては更新
    } else if ($result[0]["cnt"] == "0") { //データなし → エラー
      $ret = "-1";
      $msg = "発注データがありません。";
    } else {  //伝票複数:想定していない
      $ret = "2";
    }
  }

  if ($ret == "0") {  //発注伝票有 or 指定無し
    if (!$stringUtil->isEmpty($header["vseq"])) { //修正:vseq入力あり
      $whereParams = array();
      $whereParams["ktktcd"] = $ktktcd;
      $whereParams["mcd"] = $header["mcd"];
      $whereParams["vseq"] = $header["vseq"];
      $whereParams["viseq"] = $header["viseq"]; //修正:viseq特定の場合
      $sql = file_get_contents("./sql/getViseq.sql");
$logger->info($sql, $whereParams);
      $result = $dbutil->selectnocount($sql, $whereParams);
$logger->info("getviseq>>",$result);
      if ($result[0]["cnt"] == "1") {
        $w_viseq = $result[0]["viseq"];
        $newMode = "UPD";
      } else if ($result[0]["cnt"] == "0") { //データなし → 新規
        $newMode = "NEW";
      } else {  //伝票複数
        $ret = "1";
      }
    }
  }

  if ($ret == "0") {  //ここまでOK 新規 or 伝票特定
    //scutaxc取得
    $ret_afm04 = $valutil->allMastData("", "afm04", $header["mcd"]);
    if ($stringUtil->isEmpty($ret_afm03["scutaxc"])) {
      $ret_header["cutaxc"] = "0";
    } else {
      $ret_header["cutaxc"] = $ret_afm03["scutaxc"];
    }

    if ($newMode == "UPD" || $newMode == "UNDER") { //表示
      if ($newMode == "UNDER") { //受注下敷き
$logger->info("3111Start");
        $whereParams = array();
        $whereParams["sdday"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"];
        $whereParams["ktktcd"] = $ktktcd;
        $whereParams["cvseq"] = $header["cvseq"];
        $whereParams["mcd"] = $header["mcd"];
//        $whereParams["cuiseq"] = $w_cuiseq; //cvseqは重複しない前提
        $sql = file_get_contents("./sql/search3111.sql");
      } else {
$logger->info("UPD start");
        $whereParams = array();
        $whereParams["ktktcd"] = $ktktcd;
        $whereParams["vseq"] = $header["vseq"];
        $whereParams["mcd"] = $header["mcd"];
        $whereParams["viseq"] = $w_viseq;
        $sql = file_get_contents("./sql/search.sql");
      }
$logger->info('search>>'.$sql, $whereParams);
      $result = $dbutil->select($sql, $whereParams);
$logger->info($result);
      if ($result == null) {  //ありえない
        $ret = "-1";
      } else {
        $ret_detail = $result;
        //発注下敷き
        $ret_header["cvseq"] = $ret_detail[0]["cvseq"];
        if (!$stringUtil->isEmpty($header["cvseq"])) {  //発注伝票番号入力
          $ret_header["vseq"] = ""; //伝票番号無効
          $ret_header["cuiseq3111"] = $ret_detail[0]["cuiseq3111"];
        }

        //ヘッダー情報
        $ret_header["viseq"] = $ret_detail[0]["viseq"];
        $ret_header["hvseq"] = $ret_detail[0]["hvseq"];
        $ret_header["adat"] = $ret_detail[0]["adat"];
        $ret_header["old_adat"] = $ret_detail[0]["adat"];
        $ret_header["spktcd"] = $ret_detail[0]["spktcd"];
        $ret_header["tcls"] = $ret_detail[0]["tcls"];
        //フッター情報
        $ret_header["chgcdnm"] = $ret_detail[0]["chgcdnm"];
        $ret_header["idat"] = $ret_detail[0]["idat"];
        $ret_header["upchgnm"] = $ret_detail[0]["upchgnm"];
        $ret_header["udat"] = $ret_detail[0]["udat"];

        foreach($ret_detail as $key => $detail) {
          //消費税情報セット
          $ret_taxrt = $valutil->FmGetTaxrdivTaxrt($detail["ijan"], $ret_header["adat"], $ktktcd);
          $ret_detail[$key]["gdtax"] = $ret_taxrt["gdtax"];
          $ret_detail[$key]["gdtaxrt"] = $ret_taxrt["gdtaxrt"];
          $ret_detail[$key]["taxrt"] = $ret_taxrt["taxrt"];
          $ret_detail[$key]["taxinfo"] = $ret_taxrt["taxinfo"];
          //納入金額cal_amt
          $ret_detail[$key]["s_cst"] = $valutil->calc_amt($header["mcd"], $detail["pno"], $detail["cst"], "2", $ktktcd, $ret_header["cutaxc"]);
          $ret_detail[$key]["s_fcst"] = $valutil->calc_amt($header["mcd"], $detail["tpno"], $detail["fcst"], "2", $ktktcd, $ret_header["cutaxc"]);
        }

        if ($newMode == "UNDER") { //受注下敷き
        } else {  //仕入修正
          //伝票確認済み？
          if ($header["r30"] == "1" && $ret_detail[0]["kflg"] == "1") {
            $ret_header["kflg_kbn"] == "ON";
          }
        }
      }
    } else {  //新規
      $ret_header["adat"] = "";
    }
  }
  $ret_header["maxvlin"] = "99"; //最大行数

  if ($newMode == "NEW") {  //発注無し仕入
    $ret_header["tcls"] = "11";
    $ret_header["spktcd"] = "";
  }
  $ret_header["newmode"] = $newMode;

  $ret_array["ret"] = $ret;
  $ret_array["msg"] = $msg;
  $ret_array["ret_array"] = $ret_detail;
  $ret_array["ret_header"] = $ret_header;
  $ret_array["cnt"] = $cnt;
  $result = $ret_array;

  $logger->info('search_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("=== af233x_search_end ===", $response);

?>
