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

  $logger->info("insupd_start");

  $detail = $data["data"][0];

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

      if($detail["chkLineDelval"] == '-1'){ /*削除対象*/
        deleteAfm0311($header, $detail, $dbutil, $logger );

      } elseif ($detail["cucd"] != $detail["orgcucd"]
      || $detail["ijan"] != $detail["orgijan"]
      || $stringUtil->toNumber($detail["gdbcst"]) != $detail["orggdbcst"]
      || $stringUtil->toNumber($detail["gdsal"]) != $detail["orggdsal"]
      || $stringUtil->toNumber($detail["gdbcst1"]) != $detail["orggdbcst1"]
      || $stringUtil->toNumber($detail["gdsal1"]) != $detail["orggdsal1"]
      || $stringUtil->toNumber($detail["gdspcst"]) != $detail["orggdspcst"]
      || $detail["gdspst"] != $detail["orggdspst"]
      || $detail["gdspen"] != $detail["orggdspen"]
      || $detail["cugdde"] != $detail["orgcugdde"]
      || $stringUtil->toNumber($detail["gdstun"]) != $detail["orggdstun"]
      || $detail["gdretcls"] != $detail["orggdretcls"] 
      || $detail["cufhcls"] != $detail["orgcufhcls"] ) { //変更があるものだけ

        insertupdateAfm0311( $header, $detail, $dbutil, $logger );

      }
    }
  }

  $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("=== af90311x_updata_end ===");


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

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

  $valueData["cucd"]  = $data["cucd"];
  $valueData["ijan"]  = $data["ijan"];

  $valueData["gdbcst"]    = $stringUtil->toNumber($data["gdbcst"]);
  $valueData["gdbcst1"]   = $stringUtil->toNumber($data["gdbcst1"]);
  $valueData["gdsal"]     = $stringUtil->toNumber($data["gdsal"]);
  $valueData["gdsal1"]    = $stringUtil->toNumber($data["gdsal1"]);
  $valueData["gdspcst"]   = $stringUtil->toNumber($data["gdspcst"]);
  $valueData["gdspst"]    = $data["gdspst"];
  $valueData["gdspen"]    = $data["gdspen"];
  $valueData["cugdde"]    = $data["cugdde"];
  $valueData["gdstun"]    = $stringUtil->toNumber($data["gdstun"]);
  $valueData["gdretcls"]  = $data["gdretcls"];
  $valueData["cufhcls"]   = $stringUtil->toNumber($data["cufhcls"]);;

  $valueData['chgcd'] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];

  $valueUpdate = $valueData;

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

  $valueUpdate["orgcucd"] = $data["orgcucd"];
  $valueUpdate["orgijan"] = $data["orgijan"];

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

  if($result == 0){ //UpdateできなかったらInsert
    $sql = file_get_contents("./sql/insert.sql");
    $result = $dbutil->insert($sql, $valueData);
    $logger->info("did_insert");
    $logger->info($sql, $valueData);
  }

  return $reuslt;
}

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

  $valueData["cucd"]  = $header["cucd"];
  $valueData["orgijan"] = $data["orgijan"];

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

  $logger->info("deleteResult", $result);

  return $result;
}

?>
