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

/**
 * af241x共通カスタム関数
 * by M.Sasaki
 */

/*
  商品情報取得
*/
function getGdnmInfo($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("getGdnmInfo>>",$data);
    $ret_array = array(
      "ret" => "0",
      "ncst" => "",
      "sal" => "",
      "gdcst" => "",
      "taxinfo" => "",
      "gdtax" => "",
      "gdtaxrt" => "",
      "taxrt" => "",
      "msg" => "",
    );

    $valutil = new getValueUtil();

    try {

        $ijan = $data["ijan"];

        $ret_array["ncst"] = $valutil->get_gdbcst($data["ijan"], $data["cucd"], $data["adat"]);
$logger->info("ncst>>",$ret_array["ncst"]);
        $ret_array["sal"] = $valutil->get_gdsal($data["ijan"], $data["cucd"]);
				$ret_array["fcst"] = $valutil->get_gdcst($data["ijan"]);
        $ret_taxrt = $valutil->FmGetTaxrdivTaxrt($data["ijan"], $data["adat"], $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"]);
  $logger->info("geinfo>>",$ret_taxrt);
        $ret_array["gdtax"] = $ret_taxrt["gdtax"];
        $ret_array["gdtaxrt"] = $ret_taxrt["gdtaxrt"];
        $ret_array["taxrt"] = $ret_taxrt["taxrt"];
        $ret_array["taxinfo"] = $ret_taxrt["taxinfo"];

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

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

  $valutil = new getValueUtil();

  $header = $data["header"];
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $w_tgcnt = 0;
  try {
    //返品日
    $dat2 = new DateTime($header["dat2"]);
    $sdday = new DateTime($_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"]);
    if ($dat2 < $sdday) {
      $ret3 = "-1";
      $msg = "返品日が不正です。";
    }

    //計上日
    $result = $valutil->chk_adat("1", $ktktcd, $header["cucd"], $header["cuscd"], $header["adat"]);
    if ($result != "0") {
      $ret1 = "-1";
      $msg = "計上日が不正です。";
    }
$logger->info("chk_adat:".$result);

    //在庫チェク用
    $ret05 = $valutil->allMastData("0","afm05",$header["spktcd"]);
    $w_thflg = $ret05["thflg"];
    //ヘッダー部変更有無
    if ( $header["old_dat2"] != $header["dat2"]
        || $header["old_adat"] != $header["adat"]
        || $header["old_dcubcd"] != $header["dcubcd"]
        || $header["old_cuslip"] != $header["cuslip"]
        || $header["old_bbno"] != $header["bbno"]
        || $header["old_ltxt1"] != $header["ltxt1"]
        || $header["old_ltxt2"] != $header["ltxt2"]
        || $header["old_ltxt3"] != $header["ltxt3"]
        || $footer["old_txt1"] != $footer["txt1"] ) {
      $hchange_flg = "1";
    } else {
      $hchange_flg = "0";
    }

    foreach($data["data"] as $key => $detail) {
$logger->info("loop:".$detail["ijan"]);
      if ((!$stringUtil->isEmpty($detail["iseq30"])
            || !$stringUtil->isEmpty($detail["ijan"]))
          && $stringUtil->isEmpty($detail["chkLineDel"]) ) {

        $detail["ret"] = "ok";
        $detail["janmsg"] = "";

        if (!$stringUtil->isEmpty($detail["ijan"])) { //ijanあり
          //税率再取得
          $ret_taxrt = $valutil->FmGetTaxrdivTaxrt($detail["ijan"], $ret_header["adat"], $ktktcd);
          $detail["gdtax"] = $ret_taxrt["gdtax"];
          $detail["gdtaxrt"] = $ret_taxrt["gdtaxrt"];
          $detail["taxrt"] = $ret_taxrt["taxrt"];
          $detail["taxinfo"] = $ret_taxrt["taxinfo"];
        } else {
          $detail["gdtax"] = "";
          $detail["gdtaxrt"] = "";
          $detail["taxrt"] = "";
          $detail["taxinfo"] = "";
        }

        //1行目の税区分を取得
        if ($w_tgcnt == 0) {
          $w_taxkey = $detail["gdtax"].$detail["gdtaxrt"];
        }
$logger->info("taxkey=".$w_taxkey);
        // 2行目以降で税区分重複をチェック
        if ($w_tgcnt > 0) {
          $w_buff = $detail["gdtax"].$detail["gdtaxrt"];
          if ($w_taxkey != $w_buff && $header["cutaxdiv"] == "0") {
            $ret2 = "-2";
            $detail["ret"] = "err";
            $detail["janmsg"] = "異なる税区分、税率の商品が混在しています";
          }
        }

        if ($w_thflg == "0") {
            if (!$stringUtil->isEmpty($detail["iseq30"])
                && ($hchange_flg == "1"
                    || $detail["old_jan"] != $detail["jan"]
                    || floatval($stringUtil->toNumber($detail["old_tpno"])) != floatval($stringUtil->toNumber($detail["tpno"]))
                    || floatval($stringUtil->toNumber($detail["old_ncst"])) != floatval($stringUtil->toNumber($detail["ncst"]))
                    || $detail["old_vlin"] != $w_tgcnt
                  )
            ) { //既存行で変更有
              $sql = file_get_contents("./sql/getAfv3109.sql");
              $whereParams = array();
              $whereParams["spktcd"] = $header["spktcd"];
              $whereParams["ijan"] = $detail["ijan"];
              $whereParams["old_tpno"] = $stringUtil->toNumber($detail["old_tpno"]);
              $whereParams["tpno"] = $stringUtil->toNumber($detail["tpno"]);
$logger->info($sql, $whereParams);
              $result = $dbutil->selectnocount($sql, $whereParams);
              if (floatval($result[0]["psum"]) < 0) {
                $detail["tpnomsg"] = "変更後の在庫数がマイナスです";
                $ret2 = "-2";
                $detail["ret"] = "err";
              }
            }
        }
        $w_tgcnt = $w_tgcnt + 1;
      }
      array_push($ret_meisai, $detail);
    }

    if ($ret1 != "0" || $ret2 != "0" || $ret3 != "0") { //エラー有
      $ret = "-1";
    }
    $ret_array["ret"] = $ret;
    $ret_array["ret1"] = $ret1;
    $ret_array["ret2"] = $ret2;
    $ret_array["ret3"] = $ret3;
    $ret_array["msg"] = $msg;
    $ret_array["ret_meisai"] = $ret_meisai;
  } 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;
}



?>
