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

/**
 * af215x共通カスタム関数
 * by SE 2464
 */



/*
  afw80を取得する
*/
function getMeisai($dbutil, $data) {
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("getMeisai>>",$data);
  $ret_array = array(
    "ret" => "0",
  );

  $valutil = new getValueUtil();

  try {

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

    //ワークからデータを取ってくる
    $whereParams=array();
    $whereParams["wseq"] = $data["wseq"];
    $whereParams["cucd"] = $data["cucd"];
    $whereParams["cuscd"] = $data["cuscd"];
    $sql = file_get_contents("./sql/search.sql");
    $result = $dbutil->select($sql, $whereParams);

    if ($result == null) {
      $ret = "0";
      $cnt = 0;
    } else {
      $ret = "0";
      $ret_detail = $result;

      foreach($ret_detail as $key => $val) {
        $cnt++;


        //税率情報
        $ret_tax = $valutil->FmGetTaxrdivTaxrt($val["ijan"],$val["climit"],$_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"]);
  $logger->info("ret_get_tax",$ret_tax);
        $ret_detail[$key]["taxrt"] = $ret_tax["taxrt"];
        $ret_detail[$key]["taxdiv"] = $ret_tax["taxdiv"];
        $ret_detail[$key]["taxinfo"] = $ret_tax["taxinfo"];

      }
    }


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


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

  $valutil = new getValueUtil();

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

    $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_array["ijan"]     = $result[0]["ijan"];
        $ret_array["jan"]      = $result[0]["jan"];
        $ret_array["gdjnm"]    = $result[0]["gdjnm"];
        $ret_array["gdjstnd"]  = $result[0]["gdjstnd"];
        $ret_array["gdcls9"]   = $result[0]["gdcls9"];
        $ret_array["gdcls9nm"] = $result[0]["gdcls9nm"];
        $ret_array["gdcls7"]   = $result[0]["gdcls7"];
        $ret_array["gdhtun"]   = $result[0]["gdhtun"];
        $ret_array["gdcnum"]   = $result[0]["gdcnum"];
        $ret_array["gdbnum"]   = $result[0]["gdbnum"];
        $ret_array["undkbn"]   = $result[0]["undkbn"];

        //単価をgetvalueから
        $ret_array["cst"]   = $valutil->get_gdbcstEx($data["ijan"],$data["cucd"],$data["climit"],$data["cutaxcls"],$data["cutax"]);
        $ret_array["gdcst"] = $valutil->get_gdcst($data["ijan"]);

        //税率情報
        $ret_tax = $valutil->FmGetTaxrdivTaxrt($data["ijan"],$data["climit"],$_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"];


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

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

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

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

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

    //単価取得(F7)の時
    $whereParams = array();
    $whereParams["ijan"] = $data["ijan"];
    $sql = file_get_contents("./sql/getAfm02Gdbcst.sql");
    $retGdbcst = $dbutil->select($sql, $whereParams);
    $ret_array["afm02gdbcst"] = $retGdbcst[0]["gdbcst"];

    $retAfm02Sgdbcst = $valutil->calc_amt($data["cucd"],$data["pno"],$ret_array["afm02gdbcst"]);

    $ret_array["afm02sgdbcst"] = $retAfm02Sgdbcst; //単価取得(F7)の時

    $logger->info("ret_array_cst",$ret_array);

  } 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["cucd"],$data["pno"],$data["cst"]);
    //$retSgdcst = $valutil->calc_amt($data["cucd"],$data["pno"],$data["gdcst"]);

    $whereParams = array();
    $whereParams["pno"] = $data["pno"];
    $whereParams["gdcst"] = $data["gdcst"];
    $sql = file_get_contents("./sql/getSgdcst.sql");
    $retSgdcst = $dbutil->select($sql, $whereParams);

    $ret_array["scst"] = $retScst;
    $ret_array["sgdcst"] = $retSgdcst[0]["sgdcst"];

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


/*
  納品日をchk_adatでチェック
  仕入伝票番号が既に登録ないかをチェック
*/
function chkRegBefore($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("chkRegBefore>>",$data);
  $ret_array = array(
    "climitMsg" => "",
    "pgvseqMsg" => "",
    "ret" => "0",
    "msg" => "",
  );

  $valutil = new getValueUtil();

  try {

    if(!$stringUtil->isEmpty($data["climit"])) {
      $whereParams = array();
      $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
      $whereParams["cucd"]  = $data["cucd"];
      $whereParams["cuscd"] = $data["cuscd"];
      $whereParams["climit"] = $data["climit"];
      $sql = file_get_contents("./sql/chk_adat.sql");
      $logger->info($sql, $whereParams);
      $retAdat = $dbutil->select($sql, $whereParams);
      if($retAdat[0]["ret"] != '0') {
        $ret_array["ret"] = '-1';
        $ret_array["climitMsg"] = "納品日が不正です";      
      }
    }

    if(!$stringUtil->isEmpty($data["pgvseq"])
    && $stringUtil->nvl($data["pgvseq"],'NULL') != $stringUtil->nvl($data["old_pgvseq"],'NULL')) {
      $whereParams = array();
      $whereParams["pgvseq"] = $data["pgvseq"];
      $sql = file_get_contents("./sql/chkAur32.sql");
      $logger->info($sql, $whereParams);
      $retAur32 = $dbutil->select($sql, $whereParams);
      if($retAur32[0]["cnt"] != '0') {
        $ret_array["ret"] = '-1';
        $ret_array["pgvseqMsg"] = "既に登録のある仕入伝票番号です。";
      }
    }


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


/* Afw80の削除 */
function deleteWork($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("calcAmt>>",$data);
  $ret_array = array(
    "ret" => "0",
    "msg" => "",
  );

  $valutil = new getValueUtil();

  try {
    $whereParams = array();
    $whereParams["wseq"] = $data["wseq"];
    $sql = file_get_contents("./sql/deleteWork.sql");
    $result = $dbutil->delete($sql, $whereParams);
    $sql = file_get_contents("./sql/deleteWorkAfw81.sql");
    $result = $dbutil->delete($sql, $whereParams);

    $dbutil->commit();

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


/*登録用のafw80の作成
  複写時にも呼ばれるのでここで書く*/
function insertUpdateAfw80($header, $data, $footer, $dbutil, $logger, $vlin) {
  global $stringUtil;

  $valutil = new getValueUtil();

  $wiseq80 = $dbutil->nextVal("innerseq80");

  $valueData = array();

  $valueData["wseq"]    = $header["wseq"];
  $valueData["iseq80"]  = $data["iseq80"];

  $valueData["gdflg"]   = $header["gdflg"];
  $valueData["dat1"]    = $header["dat1"];
  $valueData["climit"]  = $header["climit"];
  $valueData["pgvseq"]  = $header["pgvseq"];
  $valueData["dcubcd"]  = $header["dcubcd"];
  $valueData["cuslip2"] = $header["cuslip2"];
  $valueData["bbno"]    = $header["bbno"];

  $valueData["com1"]    = $header["com1"];
  $valueData["dtxt1"]   = $header["dtxt1"];
  $valueData["dtxt2"]   = $header["dtxt2"];
  $valueData["ltxt1"]   = $header["ltxt1"];
  $valueData["ltxt2"]   = $header["ltxt2"];

  $valueData["vlin"]      = $vlin;
  $valueData["jan"]       = $data["jan"];
  $valueData["ijan"]      = $data["ijan"];
  $valueData["gdjnm"]     = $data["gdjnm"];
  $valueData["gdjstnd"]   = $data["gdjstnd"];

  $valueData["gdcnum"]  = $stringUtil->toNumber($data["gdcnum"]);
  $valueData["gdbnum"]  = $stringUtil->toNumber($data["gdbnum"]);
  $valueData["gdhtun"]  = $stringUtil->toNumber($data["gdhtun"]);
  $valueData["gdcls9"]  = $data["gdcls9"];

  $valueData["mvpno"]   = $stringUtil->toNumber($data["mvpno"]);
  $valueData["cst"]     = $stringUtil->toNumber($data["cst"]);
  $valueData["gdcst"]   = $stringUtil->toNumber($data["gdcst"]);
  $valueData["com"]     = $data["com"];

  $valueUpdate = $valueData;

  $valueUpdate["dflg"] = $data["chkLineDel"];

  $valueData["iseq80"] = $wiseq80;
  $valueData["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $valueData["cucd"]   = $header["cucd"];
  $valueData["cuscd"]  = $header["cuscd"];
  $valueData["mcd"]    = $header["mcd"];
  $valueData["vseq"]   = $header["vseq"];


  if(!$stringUtil->isEmpty($data["iseq80"])) {
    $sql = file_get_contents("./sql/update.sql");
    $logger->info($sql,$valueData);
    $result = $dbutil->update($sql, $valueUpdate);
  } else if ($data["chkLineDel"] != '1') {
    $sql = file_get_contents("./sql/insert.sql");
    $logger->info($sql,$valueData);
    $result = $dbutil->insert($sql, $valueData);
  }

  return $reuslt;
}


/*
  複写の店舗選択チェック。
  vseqの入力がある場合でのみ、既登録のチェック
*/
function chkCopyAfw81($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("chkCopyAfw81>>",$data);
  $ret_array = array(
    "cnt" => "0",
    "ret" => "0",
  );

  $valutil = new getValueUtil();

  $header = $data["header"];

  $ret_detail = array();

  try {

    foreach($data["data"] as $key => $detail) { // insert/update

      //afr3123にあるvseqでないかチェックする
      $whereParams=array();
      $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
      $whereParams["cucd"]   = $detail["cucd"];
      $whereParams["cuscd"]  = $detail["cuscd"];
      $whereParams["vseq"]   = $detail["vseq"];
      $sql = file_get_contents("./sql/chkCopyAfw81.sql");
      $result = $dbutil->select($sql, $whereParams);

      if($result[0]["wcnt"] != '0') {
        $ret_array["ret"] = '-1';
      }
      $data["data"][$key]["cnt"] = $result[0]["wcnt"];
    }

    $ret_array["data"] = $data["data"];

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


/*
  複写の店舗insert。
*/
function insCopyAfw81($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("insCopyAfw81>>",$data);
  $ret_array = array(
    "cnt" => "0",
    "ret" => "0",
  );

  $valutil = new getValueUtil();

  $header = $data["header"];

  $ret_detail = array();

  try {

    //afw81にinsertする (一回deteleしてから)
    $whereParams=array();
    $whereParams["wseq"]   = $header["wseq"];
    $whereParams["cuscd"]  = $header["cuscd"];
    $sql = file_get_contents("./sql/delCopyAfw81.sql");
    $result = $dbutil->delete($sql, $whereParams);

    foreach($data["data"] as $key => $detail) { // insert/update

      $whereParams=array();
      $whereParams["wseq"]   = $header["wseq"];
      $whereParams["cucd"]   = $detail["cucd"];
      $whereParams["cuscd"]  = $detail["cuscd"];
      $whereParams["vseq"]   = $detail["vseq"];
      $sql = file_get_contents("./sql/insCopyAfw81.sql");
      $result = $dbutil->insert($sql, $whereParams);

    }
    $dbutil->commit();

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

  $valutil = new getValueUtil();

  $header = $data["header"];

  $ret_detail2 = array();

  try {

    $whereParams=array();
    $whereParams["wseq"]["in"] = $data["wseq"];
    $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
    $whereParams["cucd"]["in"] = $data["cucd"];
    $whereParams["cuscd"]["in"] = $data["cuscd"];
    $whereParams["ret"]["out"] = "";
    $sql = file_get_contents("./sql/call_Fm_AF215X_Copy.sql");
  $logger->info($sql,$whereParams);
    $ret_plsql = $dbutil->call($sql,$whereParams);
  $logger->info("ret_plsql",$ret_plsql);
  
    //ワークからデータ(編集中店舗)を取ってくる
    $whereParams=array();
    $whereParams["wseq"] = $data["wseq"];
    $whereParams["cucd"] = $data["cucd"];
    $sql = file_get_contents("./sql/searchAfw81.sql");
    $result2 = $dbutil->select($sql, $whereParams);
    $ret_detail2 = $result2;

    $ret_array["ret_array2"] = $ret_detail2;


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

/*⓵メイン画面で複写押下時のafw80登録
　⓶メイン画面の編集中店舗「切替」押下時のafw80登録
　画面内容でafw80をinsertしてから、選択したafw80を取ってくる
  Formsでは⓵でafw80のセレクトはしていないが、php画面だとafw80.iseq80を登録しながら埋めれないので、
  セレクトして、afw80.iseq80を取得する
  */
function insAfw80($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("insAfw80_".$data["type"].">>",$data);
  $ret_array = array(
    "cnt" => "0",
    "ret" => "0",
  );

  $valutil = new getValueUtil();

  $header = $data["header"];

  if($stringUtil->isEmpty($header["wseq"])) {
    $header["wseq"] = $dbutil->nextVal("innerwseq");
  }

  $cnt = 0;
  foreach($data["data"] as $detail) { // insert/update
    if(!$stringUtil->isEmpty($detail["iseq80"]) || !$stringUtil->isEmpty($detail["ijan"])) {
      $cnt = $cnt + 1;
      
      if($data["updMode"] == '1') {
        $detail["chkLineDel"] = '1';
      }

      //登録時にも実行するので、allwaysに記述
      insertUpdateAfw80($header, $detail, $footer, $dbutil, $logger, $cnt); 
    }
  }

  $dbutil->commit();

  /*afw80を取ってくる。複写押下時はafw80.iseq80取得のため、店舗切替時は切替後のafw80を取ってくるため*/
  if($data["type"] == "changeCuscd") {
    $header["cuscd"] = $header["cuscd2"]; //店舗切替時は、ヘッダー店舗を選択した店舗で上書き
  }
  $ret_detail = getMeisai($dbutil,$header);
  $ret_header = $ret_detail[0]; //ヘッダーに埋める用
  if($ret_detail == null) {
    $ret == "0";
    $cnt = 0;
  } else {
    //絶対ある
    $ret = "-9";
  }


  $ret_array["ret_array"] = $ret_detail;
  $ret_array["ret_header"] = $ret_header;


  try {

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


?>
