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

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


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) { 

      $whereParams = array();
      $whereParams["ijan"] = $data["ijan"];
      $whereParams["mcd"]  = $data["mcd"];
      $sql = file_get_contents("./sql/chkAfm02e.sql");
      $retAfm02e = $dbutil->select($sql, $whereParams);

      if($retAfm02e[0]["cnt"] != '0') {

        /* 新規の場合->廃止されたマスタは入力不可とする */
        if($data["modelbl"] == "新規") {
          if(!$stringUtil->isEmpty($result[0]["gdstp"])) {
            $ret_array["ret"] = '-1';
            $ret_array["msg"] = "入力されたJANは未登録です。入力を確認して下さい。";
            goto endGdnmInfo;
          /* バラ発注区分が不可の場合はエラー */
          } else if ($result[0]["undkbn"] == '1') {
            $ret_array["ret"] = '-1';
            $ret_array["msg"] = "発注不可の商品です。";
            goto endGdnmInfo;
          }
        }

        //税率情報
        $ret_tax = $valutil->FmGetTaxrdivTaxrt($data["ijan"],$data["dat"],$_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"]);

        $logger->info("ret_get_tax",$ret_tax);
        $ret_array["taxrt"] = $ret_tax["taxrt"];
        $ret_array["taxdiv"] = $ret_tax["taxdiv"];
        $ret_array["taxinfo"] = $ret_tax["taxinfo"];
        $ret_array["gdcnum"] = $result[0]["gdcnum"];
        $ret_array["gdbnum"] = $result[0]["gdbnum"];
        $ret_array["gdhtun"] = $result[0]["gdhtun"];
        $ret_array["cst"] = $valutil->get_gdcst($data["ijan"],$data["mcd"]);
        $ret_array["undkbn"] = $result[0]["undkbn"];

      } else {
        $ret_array["ret"] = '-1';
        $ret_array["msg"] = "仕入先が違います。入力を確認して下さい。";
        goto endGdnmInfo;
      }
      

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



    endGdnmInfo:

  } 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 calcAmt($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("calcAmt>>",$data);
  $ret_array = array(
    "ret" => "0",
  );

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

  try {
    $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];

    $retScst = $valutil->calc_amt($data["mcd"],$data["mvpno"],$data["cst"],'2',$ktktcd,$data["scutaxc"]);

    $ret_array["scst"] = $retScst;

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

?>
