<?php
//共通
require_once '../../base/common.php';
$logger = new logger(basename(__FILE__));
$logger->info("=== af224x_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;
  $header = $data["header"];

  $cnt = 0;
  foreach($data["data"] as $detail) {
    if(!$stringUtil->isEmpty($detail["cucd"])
          || !$stringUtil->isEmpty($detail["cuscd"])
          || !$stringUtil->isEmpty($detail["vseq"]) ) { //入力があるときだけ

      if(!$stringUtil->isEmpty($detail["viseq"]) && !$stringUtil->isEmpty($detail["nvseq"]) ) {   //伝票特定＆新伝票番号あり
        $cnt++;

        updateAur32($detail, $dbutil, $logger );
        updateAfr3123($detail, $dbutil, $logger); /* 更新は手打ちの3123のみを対象とする */
        updateAfr31txt($detail, $dbutil, $logger); /* 更新は手打ちの3123のみを対象とする */
      }
    }
  }

  $dbutil->commit();

  $ret_array["ret"] = $result["ret"];
  $response["data"] = $ret_array;

} 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("=== af224x_updata_end ===");


function updateAur32($data, $dbutil, $logger) {
  global $stringUtil;

  $valueData["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $valueData["nvseq"] = $data["nvseq"];
  $valueData["vseq"] = $data["vseq"];
  $valueData["viseq"]   = $data["viseq"];

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

  //まず更新
  $sql = file_get_contents("./sql/updateAur32.sql");
  $result = $dbutil->update($sql, $valueData);
  $logger->info("did_update");

  return $reuslt;
}

function updateAfr3123($data, $dbutil, $logger) {
  global $stringUtil;

  $valueData["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $valueData["nvseq"] = $data["nvseq"];
  $valueData["viseq"]   = $data["viseq"];

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

  //まず更新
  $sql = file_get_contents("./sql/updateAfr3123.sql");
  $result = $dbutil->update($sql, $valueData);
  $logger->info("did_update");

  return $reuslt;
}

function updateAfr31txt($data, $dbutil, $logger) {
  global $stringUtil;

  $valueData["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $valueData["nvseq"] = $data["nvseq"];
  $valueData["viseq"]   = $data["viseq"];

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

  //まず更新
  $sql = file_get_contents("./sql/updateAfr31txt.sql");
  $result = $dbutil->update($sql, $valueData);
  $logger->info("did_update");

  return $reuslt;
}

?>
