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

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

/*
  表示前チェック
  指定されたvseqで複数伝票(複数cuiseqがないかをちぇっく)
*/
function chkSearchBefore($dbutil, $data){
$logger = new logger(basename(__FILE__));
  global $stringUtil;
$logger->info("chkSearchBefore>>",$data);
  $ret_array = array(
    "ret" => "0", //0:OK, -1:マスタと重複
  );
  $ret = '0';

  $valutil = new getValueUtil();

  $header = $data["header"][0];

  $whereParams = array();
  $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $whereParams["vseq"] = $header["vseq"];
  $sql = file_get_contents("./sql/cntAfr3123.sql");

$logger->info("cntAfr3123>>".$sql,$whereParams);
  $ret_cnt = $dbutil->select($sql, $whereParams);
$logger->info("result",$ret_cnt);

  $ret_array["cnt"] = $ret_cnt[0]["cnt"];
  $ret_array["cuiseq"] = $ret_cnt[0]["cuiseq"];

  if((int)$ret_cnt[0]["cnt"] > 1) {
    $ret_array["ret"] = "-1";
  } else if((int)$ret_cnt[0]["cnt"] == 0) {
    $ret_array["ret"] = "-2";
  } 

  return $ret_array;
}


function getGdnmInfo($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("getGdnmInfo>>",$data);
  $ret_array = array(
    "ret" => "0",
  );

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

  try {

    $whereParams = array();
    $whereParams["ijan"] = $data["ijan"];
    $sql = file_get_contents("./sql/getGdnmInfo.sql");
    $logger->info($sql, $whereParams);
    $result = $dbutil->select($sql, $whereParams);
    $logger->info("getGdnmInfo_result",$result);
    if($result != null) { 
      $ret_array["gdcnum"] = $result[0]["gdcnum"];
      $ret_array["gdbnum"] = $result[0]["gdbnum"];

      //有効在庫情報
      $whereParams = array();
      $whereParams["spktcd"] = $data["spktcd1"];
      $whereParams["ijan"] = $data["ijan"];
      $sql = file_get_contents("./sql/getPsum.sql");
      $logger->info($sql, $whereParams);
      $retPsum = $dbutil->select($sql, $whereParams);
      $logger->info("retPsum",$retPsum);
      $ret_array["psum"] = $stringUtil->nvl0($retPsum[0]["psum"]);

    } else {
      $ret_array["ret"] = '-1';
      $ret_array["msg"] = "入力されたJANは未登録です";
    }



  } 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;
}


/*
  登録前チェック
  在庫数を取り直して、入力数が超えていないか
*/
function chkRegBefore($dbutil, $data){
$logger = new logger(basename(__FILE__));
  global $stringUtil;
$logger->info("chkRegBefore>>",$data);
  $ret_array = array(
    "ret" => "0", //0:OK, -1:マスタと重複
  );
  $ret = '0';

  $valutil = new getValueUtil();

  $header = $data["header"][0];
  $dataList = $data["data"];

  foreach($dataList as $key => $val) {
  
    //有効在庫情報
    $whereParams = array();
    $whereParams["spktcd"] = $header["spktcd1"];
    $whereParams["ijan"] = $val["ijan"];
    $sql = file_get_contents("./sql/getPsum.sql");
    $logger->info($sql, $whereParams);
    $retPsum = $dbutil->select($sql, $whereParams);
    $logger->info("retPsum",$retPsum);
    $dataList[$key]["psum"]= $stringUtil->nvl0($retPsum[0]["psum"]);     
  
  }


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

  return $ret_array;
}

?>
