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

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

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

    $valutil = new getValueUtil();

    try {

      $ijan = $data["ijan"];
      $mcd =  $data["mcd"];
      $ret02 = $valutil->allMastData("0","afm02",$ijan,"9");
      $w_gdcls7 = $ret02["gdcls7"];
      $ret_array["fcst"] = $valutil->get_gdcst($ijan, $mcd);
      if ($w_gdcls7 != "1") {
        $ret02e = $valutil->allMastData("0","afm02e",$ijan,$mcd);
        if ($ret02e["ret"] == "-1") { //データなし
          $ret_array["ret"] = '-1';
          $ret_array["msg"] = "仕入先が違います。\n入力を確認して下さい。";
        }
      }
      $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",
    "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 {
    $result = $valutil->chk_adat("3", $ktktcd, "", "", $header["adat"]);
    if ($result != "0") {
      $ret1 = "-1";
      $msg = "仕入日が不正です。";
    }
$logger->info("chk_adat:".$result);

    $ret04 = $valutil->allMastData("0","afm04",$header["mcd"]);
    if ($stringUtil->isEmpty($ret04["mtaxdiv"])) {
      $mtaxdiv = "0";
    } else {
      $mtaxdiv = $ret04["mtaxdiv"];
    }
    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["mtaxdiv"] == "0") {
            $ret2 = "-2";
            $detail["ret"] = "err";
            $detail["janmsg"] = "異なる税区分、税率の商品が混在しています";
          }
        }

        $w_tgcnt = $w_tgcnt + 1;
      }
      array_push($ret_meisai, $detail);
    }

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



?>
