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

/**
 * af902x共通カスタム関数
 * @author M.Sasaki
 */

//新規データ初期値
function getDefaultData() {
  $logger = new logger(basename(__FILE__));
  $logger->info("===getDefaultData===");

  $ret_array = Array(
    'ijan' => '',
    'jan' => '',
    'gdsal' => '0',
    'gdacst' => '0',
    'gdbcst' => '0',
    'gdpkf'  => '0',
    'gdcls7' => '0',
    'gdcls8' => '0',
    'gdcls9' => '0',
    'gdcnum' => '1',
    'gdcwid' => '0',
    'gdcdep' => '0',
    'gdchig' => '0',
    'gdcwgh' => '0',
    'gdbnum' => '1',
    'gdbwid' => '0',
    'gdbdep' => '0',
    'gdbhig' => '0',
    'gdbwgh' => '0',
    'gdpwid' => '1',
    'gdpdep' => '1',
    'gdphig' => '1',
    'gdpwgh' => '0',
    'gdpcap' => '1',
    'gdcnum2' => '1',
    'gdcwid2' => '0',
    'gdcdep2' => '0',
    'gdchig2' => '0',
    'gdbnum2' => '1',
    'gdbwid2' => '0',
    'gdbdep2' => '0',
    'gdbhig2' => '0',
    'gdpwid2' => '1',
    'gdpdep2' => '1',
    'gdphig2' => '1',
    'gdpcap2' => '1',
  );

  return $ret_array;
}

//getMakeCd
//JAN C/D 取得
function getMakeCd($dbutil, $data) {
  global $stringUtil;
$logger = new logger(basename(__FILE__));
$logger->info("getMakeCd",$data);
$whereParams = array();
  $whereParams['jan'] = $data['jan'];
  $sql = file_get_contents("./sql/makeCD.sql");
  $logger->info("makeCd>>".$sql, $whereParams);
  $result = $dbutil->selectNoCount($sql, $whereParams);
  $wjan = $result[0]['chkjan'];

  return $wjan;
}

//chkCd
//JAN C/Dチェック
function chkJan($dbutil, $data) {
  global $stringUtil;
$logger = new logger(basename(__FILE__));
$logger->info("chkJan",$data);
  $ret = '0';
  $jan = $data['jan'];
  if (!$stringUtil->isEmpty($jan)) {
    $wjan = getMakeCd($dbutil, $data);
    if ($jan != $wjan) {
      $ret = "-1";
    }
  }
  $ret_array = array(
    "ret" => $ret,
    "wjan" => $wjan,
  );
  return $ret_array;
}

//dspTaxRt
//税率取得
function dspTaxRt($dbutil, $taxrtd, $gdtax, $kbn, $ktktcd) {
  global $stringUtil;
$logger = new logger(basename(__FILE__));
$logger->info('taxrtd='.$taxrtd.',gdtax='.$gdtax.',kbn='.$kbn.',ktktcd='.$ktktcd);

  $ret_array = Array(
    'gdcls' => '',
    'gdclsnm' => '',
  );

  if ($stringUtil->isEmpty($gdtax)) { //新のみ通る
    $ret_array['gdcls'] = '';
    $ret_array['gdclsnm'] = '';
  } else {
    $whereParams = array();
    $whereParams['taxrtd']['in'] = $taxrtd;
    $whereParams['gdtax']['in'] = $gdtax;
    $whereParams['pkbn']['in'] = $kbn;
    $whereParams['ktktcd']['in'] = $ktktcd;
    $whereParams['taxrt']['out'] = '';
    //$whereParams['taxrtcd']['out'] = '';
    $whereParams['taxrtnm']['out'] = '';
    $sql = file_get_contents("./sql/callFmGetTaxrtKbn.sql");
    $result = $dbutil->call($sql, $whereParams);
    $ret_array['gdcls'] = $result['taxrt'];
    $ret_array['gdclscd'] = $result['taxrtcd'];
    $ret_array['gdclsnm'] = $result['taxrtnm'];
  }
  $logger->info("ret_dspTaxRt", $ret_array);

  return $ret_array;
}
//getTaxRt
//税率表示
function getTaxRt($dbutil, $data) {
  global $stringUtil;
$logger = new logger(basename(__FILE__));
$logger->info('getTaxRt',$data);
  //戻り値
  $ret_array = Array(
    'gdcls6' => '',
    'gdcls6nm' => '',
    'gdclsg' => '',
    'gdclsgnm' => '',
  );

  //変数セット
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["ktktcd"];
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $sdday = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]['SDDAY'];
  $hikazeicls = '9';

  if ($data['pmode'] == '1') {  //現
    $gdtax = $data['gdtax'];
    //消費税率取得
    $result = dspTaxRt($dbutil, $data['taxrtd'], $gdtax, '0', $ktktcd);
    $ret_array['gdcls6'] = $result['gdcls'];
    $ret_array['gdcls6cd'] = $result['gdclscd'];
    $ret_array['gdcls6nm'] = $result['gdclsnm'];
  } else {  //新
    $gdtaxn = $data['gdtaxn'];
    //消費税率取得
    $result = dspTaxRt($dbutil, $data['taxrtd'], $gdtaxn, '1', $ktktcd);
    $ret_array['gdclsg'] = $result['gdcls'];
    $ret_array['gdclsgcd'] = $result['gdclscd'];
    $ret_array['gdclsgnm'] = $result['gdclsnm'];
  }


  $retdata['ret_array'] = $ret_array; //項目値

  $logger->info("ret_getTaxRt", $retdata);

  return $retdata;
}

//dspTaxCombo
//消費税関連
function dspTaxCombo($dbutil, $data) {
  global $stringUtil;
$logger = new logger(basename(__FILE__));
$logger->info('dspTaxCombo',$data);
  //戻り値
  $ret_array = Array(
    'gdtax' => '',
    'gdcls6' => '',
    'gdcls6nm' => '',
    'gdtaxn' => '',
    'gdclsg' => '',
    'gdclsgnm' => '',
  );

  //変数セット
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["ktktcd"];
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $sdday = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]['SDDAY'];
  $hikazeicls = '9';

  //課税種類取得
  $whereParams = array();
  $whereParams['hikazeicls'] = $hikazeicls;
  $whereParams['taxrtd'] = $data['taxrtd'];
  $whereParams['ktktcd'] = $ktktcd;
  $sql = file_get_contents("./sql/getTaxdt.sql");
  $result = $dbutil->selectNoCount($sql, $whereParams);
$logger->info('taxdt', $result);
  $taxdat = $result[0]['taxdat'];
  $b_keigen = $result[0]['b_keigen'];
  $b_kazei = $result[0]['b_kazei'];
  $a_keigen = $result[0]['a_keigen'];
  $a_kazei = $result[0]['a_kazei'];
$logger->info("taxdat>>".$taxdat);
  //日付をyyyymmddに変換
  $whereParams = array();
  $whereParams['taxrtd'] = $data['taxrtd'];
  $whereParams['sdday'] = $sdday;
  $whereParams['taxdat'] = $taxdat;
  $sql = file_get_contents("./sql/chgDateYYYYMMDD.sql");
$logger->info('chgDate',$whereParams);
  $result = $dbutil->selectNoCount($sql, $whereParams);
  $wtaxrtd = $result[0]['taxrtd'];
  $wsdday = $result[0]['sdday'];
  $wtaxdat = $result[0]['taxdat'];
  //消費税率現は指定日とマスタ適用日によって違うため
  if ($stringUtil->isEmpty($data['taxrtd']) && $wsdday >= $wtaxdat) { //指定日がNullでsddayが適用日以後なら後を取得
    $wkeigen = $a_keigen;
    $wkazei = $a_kazei;
  } else {  //それ以外は前を取得
    $wkeigen = $b_keigen;
    $wkazei = $b_kazei;
  }
$logger->info("wkeigen>>".$wkeigen.", wkazei>>".$wkazei);
  // 消費税区分(共通) ---------------------------------------
	$sqlstr1 = "select nm nm, scls1 cd from afm90 where cls='Z' ";
	$sqlstr3 = "order by snum1 ";
  // 消費税区分(現) ↓-------------------------------------------------
  $sqlstr = '';
	$sqlstr2 = "and scls1 in (";
  if ($wkazei != $hikazeicls) { //課税
		$sqlstr2 .= "'0', ";
  }
  if ($wkeigen != $hikazeicls) {  //軽減税率
    $sqlstr2 .= "'2', ";
  }
  $sqlstr2 .= "'1') ";  //非課税固定
  $sqlstr = $sqlstr1.$sqlstr2.$sqlstr3;

  $wgdtax = $data['gdtax'];
  //cbobox用データ作成
  $result = $dbutil->select($sqlstr);
  $listGdtax = $result;
  //gdtxlcsがリストにあるか
  $flg = false;
  foreach ($listGdtax as $val) {
    if ($val['cd'] == $data['gdtax']) {
      $flg = true;
    }
  }
  if (!$flg) {  //コンボボックスにない
    $gdtax = '0';
  } else {
    $gdtax = $data['gdtax'];
  }

  //消費税率取得
  $result = dspTaxRt($dbutil, $data['taxrtd'], $gdtax, '0', $ktktcd);
  $ret_array['gdcls6'] = $result['gdcls'];
  $ret_array['gdcls6cd'] = $result['gdclscd'];
  $ret_array['gdcls6nm'] = $result['gdclsnm'];
  // 消費税区分(現) ↑-------------------------------------------------

  // 消費税区分(新) ↓-------------------------------------------------
  $sqlstr = '';
  $sqlstr2 = "and scls1 in (";
  if ($a_kazei != $hikazeicls) {  //課税
    $sqlstr2 .= "'0', ";
  }
  if ($a_keigen != $hikazeicls) { //軽減
    $sqlstr2 .= "'2', ";
  }
  $sqlstr2 .= "'1') ";  //非課税固定
  $sqlstr = $sqlstr1.$sqlstr2.$sqlstr3;
  $wgdtax = $data['gdtaxn'];
  //cbobox用データ作成
  $result = $dbutil->select($sqlstr);
  $listGdtaxn = $result;
  //gdtxlcsnがリストにあるか
  $flg = false;
  foreach ($listGdtaxn as $val) {
    if ($val['cd'] == $data['gdtaxn']) {
      $flg = true;
    }
  }
  if (!$flg) {  //コンボボックスにない
    $gdtaxn = '0';
  } else {
    $gdtaxn = $data['gdtaxn'];
  }
  //消費税率取得
  $result = dspTaxRt($dbutil, $data['taxrtd'], $gdtaxn, '1', $ktktcd);
  $ret_array['gdclsg'] = $result['gdcls'];
  $ret_array['gdclsgcd'] = $result['gdclscd'];
  $ret_array['gdclsgnm'] = $result['gdclsnm'];
  // 消費税区分(新) ↑-------------------------------------------------
$logger->info('dsptax>>',$ret_array);
  $ret_array['gdtax'] = $gdtax;
  $ret_array['gdtaxn'] = $gdtaxn;

  $retdata['ret_array'] = $ret_array; //項目値
  //コンボボックスデータ
  $retdata['listGdtax'] = $listGdtax; //旧税区分
  $retdata['listGdtaxn'] = $listGdtaxn;  //新税区分
  $logger->info("ret_dspTaxCombo", $retdata);
  return $retdata;
}


//chkAfm0312
//出荷制限区分変更時、出荷制限マスタをチェックし、登録があれば変更不可とする
function chkAfm0312($dbutil, $data) {
  global $stringUtil;
$logger = new logger(basename(__FILE__));
$logger->info("chkAfm0312",$data);
  $ret_array = array(
    "ret"   => "0",
    "msg"   => "",
    "gdpkf" => "",
  );

  $ret   = "0";
  $msg   = "";
  $gdpkf = "";

  $whereParams = array();
  $whereParams['ijan'] = $data["ijan"];
  $sql = file_get_contents("./sql/chkAfm0312.sql");
  $result = $dbutil->select($sql, $whereParams);
  $logger->info("retCntAfm032",$result);
  if($result == null) {
    $wcnt1 = '0';
    $wcnt2 = '0';
  } else {
    $wcnt1 = $result[0]["wcnt1"];
    $wcnt2 = $result[0]["wcnt2"];
  }

  if($data["gdpkf"] == '0') { /* 1->0 */
    if($stringUtil->nvl0($wcnt1) > 0) {
      $ret   = "-1";
      $gdpkf = "1";
      $msg   = "出荷許可設定がなされている為、\n出荷制限へ区分変更が出来ません。";
    }
  } else {                    /* 0->1 */
    if($stringUtil->nvl0($wcnt2) > 0) {
      $ret   = "-2";
      $gdpkf = "0";
      $msg   = "出荷制限設定がなされている為、\n出荷許可へ区分変更が出来ません。";
    }
  }


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

  return $ret_array;
}


/*
  登録前チェック
*/
function chkRegBefore($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("chkRegBeforeInfo>>",$data);
  $ret_array = array(
    "ret" => "0",
    "msg" => "",
  );
  $ret = "0";
  $ret1 = "0";  //計上日
  $ret2 = "0";  //明細
  $ret_meisai = array();  //明細エラーあり

  $valutil = new getValueUtil();

  $header = $data["header"];
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $w_tgcnt = 0;
  try {

    $whereParams = array();
    $whereParams["vjan"] = $data["vjan"];
    $whereParams["ijan"] = $data["ijan"];
    $sql = file_get_contents("./sql/chkVjan.sql");
    $result = $dbutil->select($sql, $whereParams);
    $logger->info("retChkVjan",$result);
  
    if($result[0]["cnt"] != '0') {
      $ret = '-1';
    }

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




?>
