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

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

/*
　得意先コードからcusacls取得
*/
function getCunmInfo($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("getCunmInfo>>",$data);
  $ret_array = array(
    "ret" => "0",
    "cusacls" => "",
    "msg" => "",
  );

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

  $ret = "0";

  try {
    $result = $valutil->allMastData("","afm03",$data["cucd"]);
    $ret_array["ret"] = $result["ret"];
    $ret_array["cusacls"] = $result["cusacls"];

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

      if ($data["tcls"] == "01") {  //売上
        $ret_array["ncst"] = $valutil->get_gdbcst($data["ijan"], $data["cucd"], $data["adat"]);
        $ret_array["sal"] = $valutil->get_gdsal($data["ijan"], $data["cucd"]);
				$ret_array["gdcst"] = $valutil->get_gdcst($data["ijan"]);
      } else if ($data["tcls"] == "03") { //値引
        $ret_array["ncst"] = -1 * $valutil->get_gdbcst($data["ijan"], $data["cucd"], $data["adat"]);
        $ret_array["sal"] = "0";
				$ret_array["gdcst"] = "0";
      } else {
        $ret_array["ret"] = '-9';
        $ret_array["msg"] = "起動引数が不正です。";
      }
      $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("1", $ktktcd, $header["cucd"], $header["cuscd"], $header["adat"]);
    if ($result != "0") {
      $ret1 = "-1";
      $msg = "計上日が不正です。";
    }
$logger->info("chk_adat:".$result);

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

$logger->info("tcls=".$header["tcls"]);
        $detail["ret"] = "ok";
        $detail["janmsg"] = "";
        $detail["tpnomsg"] = "";

        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 ($header["tcls"] == "01") {  //売上
          /* prop=2の場合は在庫にも影響する為、マイナスにならないかチェックが必要 */
          if ($detail["prop"] == "2") {
            $w_tpno = $stringUtil->nvl0($stringUtil->toNumber($detail["tpno"]));
            $w_oldtpno = $stringUtil->nvl0($stringUtil->toNumber($detail["old_tpno"]));
            if (!$stringUtil->isEmpty($detail["spktcd"]) && ($w_tpno - $w_oldtpno) > 0 ) {
              $sql = file_get_contents("./sql/getPsum.sql");
              $whereParams = array();
              $whereParams["spktcd"] = $detail["spktcd"];
              $whereParams["ijan"] = $detail["ijan"];
              $result = $dbutil->selectnocount($sql, $whereParams);
              if (($w_tpno - $w_oldtpno) > $result[0]["psum"]) {
                $ret2 = "-2";
                $msg = "在庫が無い為、数量を増やす事は出来ません";
                $detail["ret"] = "err";
                $detail["tpnomsg"] = $msg;
                array_push($ret_meisai, $detail);
              }
            }
          }
        } else if ($header["tcls"] == "03") { //値引き

        }

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



?>
