<?php
//共通
require_once '../../base/common.php';
$logger = new logger(basename(__FILE__));
$logger->info("=== af24ax_update_start ===");

$response["status"] = "OK";

//返り値
$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);

$logger->info("data", $data);

try {
  global $dbutil;
  global $stringUtil;
  $valutil = new getValueUtil();
  $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];

  $ret = "0";
  $header = $data["header"];
  $footer = $data["footer"];

  if ($stringUtil->isEmpty($header["vseq"])) {
    $w_sts = "0";
  } else {
    $w_sts = "1";
  }
	$w_vlin = 0;

  foreach($data["data"] as $key => $detail) {
$logger->info("detail>>",$detail);
    if (!$stringUtil->isEmpty($detail["chkLineDel"])) {   /* 行削除された分を削除 */
      if (!$stringUtil->isEmpty($detail["iseq"])) {
        deleteAfr3127($detail["iseq"], $dbutil, $logger);
      }
    } else {
      /* ISEQが埋まっているレコード or JANの入力があるレコードを対象とする */
      if (!$stringUtil->isEmpty($detail["iseq"])
            || !$stringUtil->isEmpty($detail["ijan"]) ) {

        if (!$stringUtil->isEmpty($detail["iseq"])) { /* ISEQが埋まっているレコードを更新、以外は新規 */
          if ($stringUtil->isEmpty($detail["ijan"])) {  //ijan=nullなら削除
            deleteAfr3127($detail["iseq"], $dbutil, $logger);
          } else {
            $w_vlin = $w_vlin +1;
            if ($stringUtil->isEmpty($header["vseq"])) {
              $ww_vlin = "";
            } else {
              $ww_vlin = $w_vlin;
            }
            $oparam = array();
            $oparam["sts"] = $w_sts;
            updateInsertAfr3127($header, $detail, $oparam,$dbutil, $logger, $ww_vlin);
          }
				} else {  //新規行
          $w_vlin = $w_vlin +1;
          if ($stringUtil->isEmpty($header["vseq"])) {
            $ww_vlin = "";
          } else {
            $ww_vlin = $w_vlin;
          }
          $oparam = array();
          $oparam["sts"] = $w_sts;
          updateInsertAfr3127($header, $detail, $oparam,$dbutil, $logger, $ww_vlin);
        }
      }
    }
  }

  $dbutil->commit();

  $retData["ret"] = $ret;
  $response["data"]   = $retData;

} catch(userException $ue) {
  $retData["code"]  = $ue->getErrorCode();
  $retData["msg"]   = $ue->getErrorMessage();

  $response["status"] = "NG";
  $response["data"]   = $retData;

  $dbutil->rollback();

} catch(Exception $e) {
  $response["status"] = "NG";
  $response["data"]   = $e->getMessage();

  $dbutil->rollback();

} finally {
  $logger->info("response", $response );
  print( json_encode($response) );
}

$logger->info("=== af24ax_updata_end ===");

function deleteAfr3127($iseq, $dbutil, $logger) {
  $valueData = array();
  $valueData["iseq"] = $iseq;

  $sql = file_get_contents("./sql/delete.sql");
  $result = $dbutil->delete($sql, $valueData);
}

function updateInsertAfr3127($header, $data, $oparam, $dbutil, $logger, $vlin) {
  global $stringUtil;
  $valutil = new getValueUtil();

  $mainParams = array();

  $mainParams["ijan"] = $data["ijan"];
  $mainParams["pno"] = $stringUtil->toNumber($data["pno"]);
  $mainParams["cst"] = $stringUtil->toNumber($data["cst"]);
  $mainParams["cdat"] = $data["cdat"];
  $mainParams["com"] = $header["com"];
  $mainParams["com1"] = $data["com1"];
  $mainParams["vlin"] = $vlin;

  if (!$stringUtil->isEmpty($header["iseq"])) { //更新
    $updParams = array();
    $updParams = $mainParams;
    $updParams["iseq"] = $data["iseq"];
    $sql = file_get_contents("./sql/update.sql");
    $logger->info($sql, $updParams);
    $result = $dbutil->update($sql, $updParams);
  } else {  //新規
    $result = "0";
  }
  if ($result == "0") {
    $updParams = array();
    $updParams = $mainParams;
    $updParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
    $updParams["spktcd"] = $header["spktcd"];
    $updParams["sts"] = $oparam["sts"];
    $updParams["chgcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
    $updParams["vseq"] = $header["vseq"];
    $sql = file_get_contents("./sql/insert.sql");
    $logger->info($sql, $updParams);
    $result = $dbutil->insert($sql, $updParams);
  }
  return $reuslt;
}


?>
