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

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


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

try {
  global $dbutil;

  foreach($data["data"] as $detail) {
    $logger->info("data", $detail );

    insertUpdateAfm04( $data["header"], $detail, $dbutil, $logger );
  }

  $dbutil->commit();

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



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

  $valueData['mcd'] = $header['mcd'];
  $valueData['mnm'] = $data['mnm'];
  $valueData['mnms'] = $data['mnms'];
  $valueData['mknm'] = $data['mknm'];
  $valueData['mknms'] = $data['mknms'];

  $valueData['mzip'] = $data['mzip'];
  $valueData['madd1'] = $data['madd1'];
  $valueData['madd2'] = $data['madd2'];
  $valueData['mkad1'] = $data['mkad1'];
  $valueData['mkad2'] = $data['mkad2'];
  $valueData['mtel'] = $data['mtel'];
  $valueData['mfax'] = $data['mfax'];
  $valueData['mmail'] = $data['mmail'];

  $valueData['mchgcd'] = $data['mchgcd'];
  $valueData['echgcd'] = $data['echgcd'];
  $valueData['mpnm'] = $data['mpnm'];
  $valueData['mchgnm'] = $data['mchgnm'];
  $valueData['mcom'] = $data['mcom'];

  $valueData['htel'] = $data['htel'];
  $valueData['hfax'] = $data['hfax'];
  $valueData['modfr'] = $data['modfr'];
  $valueData['htcls'] = $stringUtil->toNumber($data['htcls']);
  $valueData['htpnt1'] = $stringUtil->toNumber($data['htpnt1']);
  $valueData['htpnt2'] = $stringUtil->toNumber($data['htpnt2']);

  $valueData['plcd'] = $data['plcd'];
  $valueData['mtori'] = $data['mtori'];
  $valueData['meda'] = $data['meda'];
  $valueData['plhcls'] = $stringUtil->toNumber($data['plhcls']);

  $valueData['paycls'] = $data['paycls'];
  $valueData['mpcls'] = $data['mpcls'];
  $valueData['paydat'] = $stringUtil->toNumber($data['paydat']);
  $valueData['pmn'] = $stringUtil->toNumber($data['pmn']);

  $valueData['cdcls'] = $data['cdcls'];
  $valueData['mres'] = $data['mres'];
  $valueData['mpay'] = $data['mpay'];
  $valueData['msu'] = $data['msu'];

  $valueData['kmcd'] = $data['kmcd'];
  $valueData['rper'] = $stringUtil->toNumber($data['rper']);
 
  $valueData['chgcd'] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
  $valueData['stp'] = $data['stp'];

  $valueUpdate = $valueData;

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

  //まず更新
  $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;


return true;
}

?>
