<?php
//共通
require_once '../../base/common.php';

/**
 * af224x共通カスタム関数
 */

/*
  aur32情報
*/
function getAur32Info($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("getAur32Info>>",$data);
  $ret_array = array(
    "ret" => "0",
    "msg" => "",
  );

  $valutil = new getValueUtil();
  global $stringUtil;

  $ret = "0";
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  try {
    //sbat
    $result = $valutil->getSbat("1","0",$ktktcd,$data["cucd"],$data["cuscd"]);
    if ($result["ret"] != "0") {
      $ret = "-1";
      $msg = "伝票情報取得に失敗しました(店舗請求区分取得)";
    }
    if ($ret == "0") {
      if ($stringUtil->isEmpty($data["viseq"])) { //viseq未
        $whereParams = array();
        $whereParams["ktktcd"] = $ktktcd;
        $whereParams["tcls"] = $data["tcls"];
        $whereParams["cucd"] = $data["cucd"];
        $whereParams["cuscd"] = $data["cuscd"];
        $whereParams["vseq"] = $data["vseq"];
        $whereParams["sts"] = $result["sts"];
        $whereParams["sbat2"] = $data["sbat2"]; //axm99.sbat2
        $whereParams["sbat"] = $result["sbat"];
        $sql = file_get_contents("./sql/getViseq.sql");
$logger->info($sql, $whereParams);
        $result = $dbutil->selectnocount($sql, $whereParams);
        if ($result[0]["viseq"] == null) {
          $ret = "-1";
          $msg = "該当する伝票がありません。";
        } else {
          $viseq = $result[0]["viseq"];
        }
      } else {  //viseqあり 検索画面で指定
        $viseq = $data["viseq"];
      }

      if ($ret=="0") {  //viseqあり
        $whereParams = array();
        $whereParams["ktktcd"] = $ktktcd;
        $whereParams["tcls"] = $data["tcls"];
        $whereParams["viseq"] = $viseq;
        $sql = file_get_contents("./sql/getAur32Info.sql");
        $result = $dbutil->selectnocount($sql, $whereParams);
$logger->info($sql, $whereParams);
$logger->info($result);
        if ($result[0]["viseq"] == null) {  //ありえない
          $ret = "-1";
          $msg = "該当する伝票がありません。";
        } else {
          $amt = $result[0]["amt"];
          $adat = $result[0]["adat"];
          $sbat = $result[0]["sbat"];
        }
      }
    }

    $ret_array["ret"] = $ret;
    $ret_array["msg"] = $msg;

    $ret_array["viseq"] = $viseq;
    $ret_array["amt"] = $amt;
    $ret_array["adat"] = $adat;
    $ret_array["sbat"] = $sbat;


  } catch(userException $ue) {
    $retData["code"]  = $ue->getErrorCode();
    $retData["msg"]   = $ue->getErrorMessage();
    $response["status"] = "NG";
    $ret_array["ret"] = '-9';
  } catch(Exception $e) {
    $response["status"] = "NG";
    $response["data"]   = $e->getMessage();
    $ret_array["ret"] = '-9';
  }

  return $ret_array;
}

/*
  aur32伝票番号ちぇっく
*/
function chkAur32Info($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("chkAur32Info>>",$data);
  $ret_array = array(
    "ret" => "0",
    "msg" => "",
  );

  $valutil = new getValueUtil();

  $ret = "0";
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  try {
    $whereParams = array();
    $whereParams["ktktcd"] = $ktktcd;
    $whereParams["tcls"] = $data["tcls"];
    $whereParams["cucd"] = $data["cucd"];
    $whereParams["cuscd"] = $data["cuscd"];
    $whereParams["nvseq"] = $data["nvseq"];
    $whereParams["sbat2"] = $data["sbat2"]; //axm99.sbat2
    $whereParams["sbat"] = $data["sbat"];
    $sql = file_get_contents("./sql/cntAur32.sql");
$logger->info($sql, $whereParams);
    $result = $dbutil->selectnocount($sql, $whereParams);
$logger->info($result);
    if ($result[0]["cnt"] != "0") {
      $ret = "-1";
      $msg = "伝票番号が重複しています。";
    }

    $ret_array["ret"] = $ret;
    $ret_array["msg"] = $msg;

  } catch(userException $ue) {
    $retData["code"]  = $ue->getErrorCode();
    $retData["msg"]   = $ue->getErrorMessage();
    $response["status"] = "NG";
    $ret_array["ret"] = '-9';
  } catch(Exception $e) {
    $response["status"] = "NG";
    $response["data"]   = $e->getMessage();
    $ret_array["ret"] = '-9';
  }

  return $ret_array;
}
?>
