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

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


 function getCunmInfo($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
$logger->info("doSend>>",$data);
    $ret_array = array(
      "ret" => "0", //0:OK
      "msg" => "", //afw851.wseq
    );
    $ret = '0';
    $msg = "";

    try {

      $sql = file_get_contents("./sql/getCunmInfo.sql");
      $whereParams = array();
      $whereParams["cucd"]   = $data["cucd"];
      $ret03 = $dbutil->select($sql, $whereParams);

      $sql = file_get_contents("./sql/getSpnmInfo.sql");
      $whereParams = array();
      $whereParams["cucd"]   = $data["cucd"];
      $whereParams["cuscd"]  = $data["cuscd"];
      $ret032 = $dbutil->select($sql, $whereParams);


      $ret_array["cudt1"] = $ret03[0]["cudt1"];
      $ret_array["cupdt"] = $ret03[0]["cupdt"];
      $ret_array["cupmn"] = $ret03[0]["cupmn"];

      if($ret032 != null && !$stringUtil->isEmpty($ret032[0]["cupmn"])) {
        $ret_array["cupmn"] = $ret032[0]["cupmn"];
      }

      
    } catch(userException $ue) {
      $ret = '-999';
      $msg = $ue->getErrorMessage();
      $dbutil->rollback();
    } catch(Exception $e) {
      $ret = '-999';
      $msg = $e->getMessage();
      $dbutil->rollback();
    }

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

    return $ret_array;
  }

  function get_sbat($dbutil, $data){
    $logger = new logger(basename(__FILE__));
      global $stringUtil;
  $logger->info("doSend>>",$data);
      $ret_array = array(
        "ret" => "0", //0:OK
        "msg" => "", //afw851.wseq
      );
      $ret = '0';
      $msg = "";
  
      try {
  
        $sql = file_get_contents("./sql/callGet_sbat.sql");
        $whereParams = array();
        $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
        $whereParams["cucd"]["in"]   = $data["cucd"];
        $whereParams["cuscd"]["in"]  = $data["cuscd"];
        $whereParams["sbat"]["out"]  = "";
        $whereParams["cdat"]["out"]  = "";
        $whereParams["sts"]["out"]   = "";
        $whereParams["ret"]["out"]   = "";
        $result = $dbutil->call($sql, $whereParams);
  
      } catch(userException $ue) {
        $ret = '-999';
        $msg = $ue->getErrorMessage();
        $dbutil->rollback();
      } catch(Exception $e) {
        $ret = '-999';
        $msg = $e->getMessage();
        $dbutil->rollback();
      }
  
  
      return $result;
    }
  


/*
  伝票番号入力で表示時、同一伝票番号で複数伝票(複数viseq)がないかチェック
*/
function chkSearchBefore($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("chkSearchBefore>>",$data);
  $ret_array = array(
    "cuiseq" => "",
    "cnt" => "0",
    "ret" => "0",
    "msg" => "",
  );

  $valutil = new getValueUtil();

  try {

    // get_sbatのコール
    $retSbat = get_sbat($dbutil, $data);

    $whereParams = array();
    $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
    $whereParams["cucd"]  = $data["cucd"];
    $whereParams["cuscd"] = $data["cuscd"];
    $whereParams["vseq"]  = $data["vseq"];
    $whereParams["sts"]   = $retSbat["sts"];
    $whereParams["sbat"]  = $retSbat["sbat"];
    $sql = file_get_contents("./sql/cntAur32.sql");
    $logger->info($sql, $whereParams);
    $result = $dbutil->select($sql, $whereParams);
    $logger->info("getCunmInfo_result",$result);
    if($result != null) { 
      $ret_array["viseq"] = $result[0]["viseq"];
      $ret_array["cnt"] = $result[0]["cnt"];
    } else {
      $ret_array["cnt"] = "0";
    }

    $ret_array["mode"] = $mode;

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

  return $ret_array;
}




?>
