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

  $logger->info("insupd_start");

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

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

      } elseif ($detail["cugdcd"] != $detail["orgCugdcd"]
      || $detail["medcd"] != $detail["orgMedcd"]
      || $detail["cugdde"] != $detail["orgCugdde"]
      || $detail["ijan"] != $detail["orgIjan"]
      || $detail["cugdstp"] != $detail["orgCugdstp"]
      ) { //削除対象以外で変更があるものだけ更新対象

        $detail["cucd"] = $header["cucd"];

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


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

  $logger->info("header",$header);
  
  $valueData["cucd"]  = $header["cucd"];
  $valueData["cugdcd"]  = $data["cugdcd"];
  $valueData["medcd"]  = $data["medcd"];
  $valueData["cugdde"]  = $data["cugdde"];
  $valueData["cvsno"]  = $data["cvsno"];
  $valueData["ijan"]    = $data["ijan"];

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

  $valueUpdate = $valueData;

  $valueUpdate["orgcugdcd"] = $data["orgCugdcd"];
  $valueUpdate["orgmedcd"] = $data["orgMedcd"];
  $valueUpdate["orgcugdde"] = $data["orgCugdde"];

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

  //まず更新
  $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($sql, $valueData);
    $logger->info("did_insert");
  }

  return $reuslt;
}

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

  $valueData["cucd"]  = $header["cucd"];
  $valueData["orgcugdcd"] = $data["orgCugdcd"];
  $valueData["orgmedcd"] = $data["orgMedcd"];
  $valueData["orgcugdde"] = $data["orgCugdde"];
  
  $logger->info("deleteData", $valueData);
  $sql = file_get_contents("./sql/delete.sql");
  $result = $dbutil->delete($sql, $valueData);

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

  return $result;
}

?>
