<?php
//共通
require_once '../../base/common.php';
$logger = new logger(basename(__FILE__));
$logger->info("===af241x_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(
    "sbat" => "",
    "cdat" => "",
    "sts" => "",
    "kflg_kbn" => "OFF",
  );  //ヘッダー・フッター情報

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

  //sbat取得
  $result = $valutil->getSbat("1","0",$ktktcd,$header["cucd"],$header["cuscd"]);
$logger->info("sbat:",$result);
  $ret = $result["ret"];
  $sbat = $result["sbat"];
  $cdat = $result["cdat"];
  $sts = $result["sts"];

  $ret_header["sbat"] = $sbat;
  $ret_header["cdat"] = $cdat;
  $ret_header["sts"] = $sts;

  if ($ret == "0") {  //sbat OK
    $w_cuiseq = "";
    $w_viseq = "";
    if (!$stringUtil->isEmpty($header["cuiseq"]) && !$stringUtil->isEmpty($header["viseq"])) { //cuiseq,viseqが入ってるとき いつ通る？？？
      $w_cuiseq = $header["cuiseq"];
      $w_viseq = $header["viseq"];
    } else {
      if (!$stringUtil->isEmpty($header["vseq"])) { //修正:vseq入力あり
        //品日が過去1ヶ月分のみ修正で呼出可とする
        $whereParams = array();
        $whereParams["ktktcd"] = $ktktcd;
        $whereParams["cucd"] = $header["cucd"];
        $whereParams["cuscd"] = $header["cuscd"];
        $whereParams["vseq"] = $header["vseq"];
        $whereParams["sts"] = $sts;
        $whereParams["sbat"] = $sbat;
        $whereParams["cuiseq"] = $header["cuiseq"]; //修正:cuiseq特定の場合
        $whereParams["viseq"] = $header["viseq"]; //修正:viseq特定の場合
        $sql = file_get_contents("./sql/getCuiseq.sql");
  $logger->info($sql, $whereParams);
        $result = $dbutil->selectnocount($sql, $whereParams);
  $logger->info("getviseq>>",$result);
        if ($result == null) {
          $newMode = "NEW";
        } else {
          if ($result[0]["cnt"] == "1") {
            if ($result[0]["ktcls"] != "02") {
              $w_cuiseq = $result[0]["cuiseq"];
            } else {
              $w_viseq = $result[0]["cuiseq"];
            }
            $newMode = "UPD";
            $ret_header["cuiseq"] = $w_cuiseq;
            $ret_header["viseq"] = $w_viseq;
          } else if ($result[0]["cnt"] == "0") { //データなし → 新規
            $newMode = "NEW";
          } else {  //伝票複数
            $ret = "1";
          }
        }
      }
    }
  } else {
    $ret = "-9";
    $msg = "データ取得に失敗しました。";
  }

$logger->info("newmode:".$newMode);
  if ($ret == "0") {  //ここまでOK 新規 or 伝票特定
    //cutax,cutaxc等取得
    $whereParams = array();
    $whereParams["cucd"] = $header["cucd"];
    $sql = file_get_contents("./sql/getCucdInfo.sql");
    $result = $dbutil->selectNoCount($sql, $whereParams);
    if ($result == null) {  //ありえない
      $ret_header["cuhcls"] = "0";
      $ret_header["maxvlin"] = "99";
      $ret_header["cutax"] = "0";
      $ret_header["cutaxc"] = "0";
    } else {
      if ($stringUtil->isEmpty($result[0]["maxvlin"])) {
        $ret_header["cuhcls"] = "0";
      } else {
        $ret_header["cuhcls"] = $result[0]["cuhcls"];
      }
      if ($stringUtil->isEmpty($result[0]["maxvlin"])) {
        $ret_header["maxvlin"] = "99";
      } else {
        $ret_header["maxvlin"] = $result[0]["maxvlin"];
      }
      if ($stringUtil->isEmpty($result[0]["cutax"])) {
        $ret_header["cutax"] = "0";
      } else {
        $ret_header["cutax"] = $result[0]["cutax"];
      }
      if ($stringUtil->isEmpty($result[0]["cutaxc"])) {
        $ret_header["cutaxc"] = "0";
      } else {
        $ret_header["cutaxc"] = $result[0]["cutaxc"];
      }
    }

    if ($newMode == "UPD") { //表示
$logger->info("UPD start");
      $whereParams = array();
      $whereParams["ktktcd"] = $ktktcd;
      $whereParams["cucd"] = $header["cucd"];
      $whereParams["cuscd"] = $header["cuscd"];
      $whereParams["vseq"] = $header["vseq"];
      $whereParams["sts"] = $sts;
      $whereParams["sbat"] = $sbat;
      $whereParams["cuiseq"] = $w_cuiseq;
      $whereParams["viseq"] = $w_viseq;
      $whereParams["cutaxcls"] = $header["cutaxcls"];
      $whereParams["cutax"] = $ret_header["cutax"];
      $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["cunm"] = $ret_detail[0]["cunm"];
        $ret_header["spnm"] = $ret_detail[0]["spnm"];

        $ret_header["cuiseq"] = $ret_detail[0]["cuiseq"];
        $ret_header["viseq"] = $ret_detail[0]["viseq"];
        $ret_header["ktcls"] = $ret_detail[0]["ktcls"];

        $ret_header["dat2"] = $ret_detail[0]["dat2"];
        $ret_header["adat"] = $ret_detail[0]["adat"];
        $ret_header["spktcd"] = $ret_detail[0]["spktcd"];
        $ret_header["spktnm"] = $ret_detail[0]["spktnm"];
        $ret_header["dcubcd"] = $ret_detail[0]["dcubcd"];
        $ret_header["cuslip"] = $ret_detail[0]["cuslip"];
        $ret_header["bbno"] = $ret_detail[0]["bbno"];

        $ret_header["old_dat2"] = $ret_detail[0]["dat2"];
        $ret_header["old_adat"] = $ret_detail[0]["adat"];
        $ret_header["old_dcubcd"] = $ret_detail[0]["dcubcd"];
        $ret_header["old_cuslip"] = $ret_detail[0]["cuslip"];
        $ret_header["old_bbno"] = $ret_detail[0]["bbno"];

        $ret_header["ltxt1"] = $ret_detail[0]["ltxt1"];
        $ret_header["old_ltxt1"] = $ret_detail[0]["ltxt1"];
        $ret_header["ltxt2"] = $ret_detail[0]["ltxt2"];
        $ret_header["old_ltxt2"] = $ret_detail[0]["ltxt2"];
        $ret_header["ltxt3"] = $ret_detail[0]["ltxt3"];
        $ret_header["old_ltxt3"] = $ret_detail[0]["ltxt3"];
        //フッター情報
        $ret_header["txt1"] = $ret_detail[0]["txt1"];
        $ret_header["old_txt1"] = $ret_detail[0]["txt1"];

        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["cucd"], $detail["tpno"], $detail["dsp_ncst"], "1", $ktktcd, $ret_header["cutaxc"]);

        }
        //伝票確認済み？
        if ($header["r30"] == "1" && $ret_detail[0]["kflg"] == "1") {
          $ret_header["kflg_kbn"] == "ON";
        }
      }
    } else {  //新規
    }
  }

  if ($stringUtil->isEmpty($ret_header["spktcd"])) {
    $ret_afm05 = $valutil->allMastData("", "afm05", $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"]);
    $ret_header["spktcd"] = $ret_afm05["ktktcd"];
    $ret_header["spktnm"] = $ret_afm05["ktktnm"];
    $ret_header["ktcls"] = $ret_afm05["ktcls"];
  }

$logger->info("search_mode:".$newMode);
  $ret_header["newmode"] = $newMode;
  $ret_header["sbat"] = $sbat;
  $ret_header["cdat"] = $cdat;
  $ret_header["sts"] = $sts;

  $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("=== af241x_search_end ===", $response);

?>
