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

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

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

      } elseif ($detail["cucd"] != $detail["orgcucd"]
        || $detail["cshcd"] != $detail["orgcshcd"]
        || $detail["cshnm"] != $detail["orgcshnm"]
        || $detail["cshpcd"] != $detail["orgcshpcd"]
        || $detail["cszip"] != $detail["orgcszip"]
        || $detail["csad1"] != $detail["orgcsad1"]
        || $detail["csad2"] != $detail["orgcsad2"]
        || $detail["cstel"] != $detail["orgcstel"]
        || $detail["csfax"] != $detail["orgcsfax"]
      ) { //変更があるものだけ
        $logger->info("detail",$detail);
        $logger->info("header",$header);
        insertupdateAfm036( $header, $detail, $dbutil, $logger);
        $cnt = $cnt + 1;
      }
    }
  }

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


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

  $valueData["cucd"]   = $data["cucd"];
  $valueData["cshcd"]  = $data["cshcd"];
  $valueData["cshnm"]  = $data["cshnm"];
  $valueData["cshpcd"] = $data["cshpcd"];
  $valueData["cszip"]  = $data["cszip"];
  $valueData["csad1"]  = $data["csad1"];
  $valueData["csad2"]  = $data["csad2"];
  $valueData["cstel"]  = $data["cstel"];
  $valueData["csfax"]  = $data["csfax"];
  
  $valueUpdate = $valueData;

  $valueUpdate["orgcucd"] = $data["orgcucd"];
  $valueUpdate["orgcshcd"] = $data["orgcshcd"];
  $valueUpdate["orgcshpcd"] = $data["orgcshpcd"];

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


  //まず更新
  $sql = file_get_contents("./sql/update.sql");
  $result = $dbutil->update($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");
  }

  return $reuslt;
}

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

  $valueData["orgcucd"]  = $data["orgcucd"];
  $valueData["orgcshcd"] = $data["orgcshcd"];
  $valueData["orgcshpcd"] = $data["orgcshpcd"];

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

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

  return $result;
}

?>
