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

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

function getPsum($dbutil, $ijan, $spktcd) {
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("getPsum>>".$ijan.",".$spktcd);

  $sql = file_get_contents("./sql/getPsum.sql");  //不良品在庫
  $whereParams = array();
  $whereParams["spktcd"] = $spktcd;
  $whereParams["ijan"] = $ijan;
$logger->info('psum>>'.$sql, $whereParams);
  $result = $dbutil->selectnocount($sql, $whereParams);
$logger->info('retpsum>>',$result);
  if ($result == null) {
    $psum = "0";
  } else {
    $psum = $result[0]["psum"];
  }
  return $psum;
}
/*
  商品情報取得
*/
function getGdnmInfo($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("getGdnmInfo>>",$data);
    $ret_array = array(
      "ret" => "0", //0:OK, -1:商品情報NG, -2:不良品在庫数NG
      "psum" => "",
      "cst" => "",
      //afr3127より
      "iseq" => "",
      "pno" => "",
      "cst3127" => "",
      "com1" => "",
      "cdat" => "",
      "com1" => "",
      "amt" => "0",
      "flg3127" => "0",
    );

    $valutil = new getValueUtil();

    try {
      $ijan = $data["ijan"];

      if ($data["inpflg"] == "JAN") {
        //商品情報
        $ret02 = $valutil->allMastData("0", "afm02", $ijan, "9");
        $ret_array["cst"] = $ret02["gdcst"];
      }

      //不良品在庫数
      $ret_array["psum"] = getPsum($dbutil, $ijan, $data["spktcd"]);
      //afr3127
      if ($stringUtil->isEmpty($data["vseq"])) {
        $sql = file_get_contents("./sql/getAfr3127.sql");
        $whereParams = array();
        $whereParams["ijan"] = $ijan;
        $whereParams["cdat"] = $data["cdat"];
        $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
        $whereParams["spktcd"] = $data["spktcd"];
      $logger->info('afr3127>>'.$sql, $whereParams);
        $result = $dbutil->selectnocount($sql, $whereParams);
        if ($result == null) {
          $ret_array["flg3127"] = "0";
        } else {
          $ret_array["flg3127"] = "1";
          $ret_array["iseq"] = $result[0]["iseq"];
          $ret_array["pno"] = $result[0]["pno"];
          $ret_array["cst3127"] = $result[0]["cst"];
          $ret_array["com1"] = $result[0]["com1"];
          $ret_array["cdat"] = $result[0]["cdat"];
          $ret_array["iseq"] = $result[0]["iseq"];
          $ret_array["amt"] = $valutil->calc_amt("", $result[0]["pno"], $result[0]["cst"], "2", $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"], $data["scutaxc"]);
        }
      }
    } 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;
}



?>
