<?php
//共通
require_once '../../base/common.php';
$logger = new logger(basename(__FILE__));
$logger->info("===af238x_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 = "0";
  $newret = "0";
  $newMode = "NEW";
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];

  //倉庫のsdday取得
  $ret99 = $valutil->allMastData("0","axm99",$header["spktcd"]);
  $w_sdday = $ret99["d_sdday"];
  if ($stringUtil->isEmpty($w_sdday)) {
    $w_sdday = date('Y/m/d');
  }

  $w_viseq = "";

  if (!$stringUtil->isEmpty($header["vseq"])) { //修正:vseq入力あり
    $whereParams = array();
    $whereParams["ktktcd"] = $ktktcd;
    $whereParams["ssbat2"] = $header["ssbat2"];
    $whereParams["vseq"] = $header["vseq"];
    $whereParams["mcd"] = $header["mcd"];
    $whereParams["spktcd"] = $header["spktcd"];
    $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") { //データなし → 新規
      //他倉庫にある？
      $whereParams = array();
      $whereParams["ktktcd"] = $ktktcd;
      $whereParams["ssbat2"] = $header["ssbat2"];
      $whereParams["vseq"] = $header["vseq"];
      $whereParams["spktcd"] = $header["spktcd"];
      $sql = file_get_contents("./sql/chkViseqSpktcd.sql");
  $logger->info($sql, $whereParams);
      $result = $dbutil->selectnocount($sql, $whereParams);
      if ($result == null) {
        $w_spktcd = "";
        $w_ktnm = "";
      } else {
        $w_spktcd = $result[0]["spktcd"];
        $w_ktnm = $result[0]["ktktnm"];
      }
      if (!$stringUtil->isEmpty($w_spktcd) && $header["spktcd"]!=$w_spktcd) {
        $msg = "伝票番号:".$header["vseq"]." は、以下の倉庫にて既に登録があります。\n"."【".$w_spktcd."】".$w_ktnm;
        $newret = "1";
      } else {
        //過去90日使用済み？
        $whereParams = array();
        $whereParams["ktktcd"] = $ktktcd;
        $whereParams["ssbat2"] = $header["ssbat2"];
        $whereParams["vseq"] = $header["vseq"];
        $whereParams["sdday"] = $w_sdday;
        $sql = file_get_contents("./sql/chkVseqPast90.sql");
    $logger->info($sql, $whereParams);
        $result = $dbutil->selectnocount($sql, $whereParams);
        if ($result == null) {
          $w_viseq2 = "";
          $w_cnt = "0";
        } else {
          $w_viseq2 = $result[0]["viseq2"];
          $w_adat =  $result[0]["adat"];
          $w_cnt =  $result[0]["cnt"];
        }
        if (intval($w_cnt) > 0) {
          $msg = "伝票番号:".$header["vseq"]." は、計上日:".$w_adat."にて使用済みです。";
          $newret = "1";
        }
      }
      $newMode = "NEW";
    } else {  //伝票複数

      $ret = "1";
    }
  }

  if ($ret == "0" && $newret == "0") {  //ここまでOK 新規 or 伝票特定
    if ($newMode == "UPD") { //表示
$logger->info("UPD start");
      $whereParams = array();
      $whereParams["ktktcd"] = $ktktcd;
      $whereParams["ssbat2"] = $header["ssbat2"];
      $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["viseq"] = $ret_detail[0]["viseq"];
        $ret_header["adat"] = $ret_detail[0]["adat"];
        $ret_header["old_adat"] = $ret_detail[0]["adat"];
        $ret_header["com"] = $ret_detail[0]["com"];
        $ret_header["old_com"] = $ret_detail[0]["com"];
        $ret_header["dat"] = $ret_detail[0]["dat"];
        $ret_header["dat2"] = $ret_detail[0]["dat2"];
        $ret_header["dat3"] = $ret_detail[0]["dat3"];
        //フッター情報
        $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) {
          //pno取得
          $whereParams = array();
          $whereParams["spktcd"] = $header["spktcd"];
          $whereParams["ijan"] = $detail["ijan"];
          $sql = file_get_contents("./sql/getPno.sql");
        $logger->info($sql, $whereParams);
          $result = $dbutil->selectnocount($sql, $whereParams);
          if ($result != null) {  //念のため
            $ret_detail[$key]["pno"] = $result[0]["pno"];
          } else {
            $ret_detail[$key]["pno"] = "0";
          }

          //消費税情報セット
          $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_fcst"] = $valutil->calc_amt($header["mcd"], $detail["tpno"], $detail["fcst"], "2", $ktktcd, $ret_header["cutaxc"]);
        }

      }
    } else {  //新規
      $ret_header["viseq"] = "";
      $ret_header["maxvlin"] = "20"; //最大行数
    }
  }

  $ret_header["newmode"] = $newMode;

  $ret_array["ret"] = $ret;
  $ret_array["newret"] = $newret;
  $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("=== af238x_search_end ===", $response);

?>
