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

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

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



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

  $valueData['cucd'] = $header['cucd'];
  $valueData['cunm'] = $data['cunm'];
  $valueData['cunms'] = $data['cunms'];
  $valueData['cuknm'] = $data['cuknm'];
  $valueData['cuknms'] = $data['cuknms'];
  $valueData['spedi'] = $data['spedi'];

  $valueData['cumcd4'] = $data['cumcd4'];
  $valueData['cumcd6'] = $data['cumcd6'];
  $valueData['cumcd3'] = $data['cumcd3'];
  $valueData['cuslcd'] = $data['cuslcd'];
  $valueData['cuslcd2'] = $data['cuslcd2'];
  $valueData['cubscd'] = $data['cubscd'];
  $valueData['kechg'] = $data['kechg'];

  $valueData['cuzip'] = $data['cuzip'];
  $valueData['cuad1'] = $data['cuad1'];
  $valueData['cuad2'] = $data['cuad2'];
  $valueData['cukad1'] = $data['cukad1'];
  $valueData['cukad2'] = $data['cukad2'];
  $valueData['cutel'] = $data['cutel'];
  $valueData['cufax'] = $data['cufax'];
  
  $valueData['cumcd2'] = $data['cumcd2'];

  $valueData['cudt1'] = $data['cudt1'];
  $valueData['cukbn'] = $data['cukbn'];
  $valueData['cusei'] = $data['cusei'];
  $valueData['cusnm'] = $data['cusnm'];
  $valueData['cusknm'] = $data['cusknm'];
  $valueData['cuszip'] = $data['cuszip'];
  $valueData['cusad1'] = $data['cusad1'];
  $valueData['cusad2'] = $data['cusad2'];
  
  $valueData['cuspflg'] = $data['cuspflg'];
  $valueData['cuosflg'] = $data['cuosflg'];
  $valueData['ketxt1'] = $data['ketxt1'];
  
  $valueData['cuskad1'] = $data['cuskad1'];
  $valueData['cuskad2'] = $data['cuskad2'];
  $valueData['custel'] = $data['custel'];
  $valueData['cusfax'] = $data['cusfax'];
  $valueData['cusmail'] = $data['cusmail'];
  
  $valueData['cuedi'] = $data['cuedi'];
  $valueData['cudenp1'] = $data['cudenp1'];
  $valueData['cudenp2'] = $data['cudenp2'];
  $valueData['cudpcls'] = $data['cudpcls'];
  $valueData['salpcls'] = $data['salpcls'];
  $valueData['cstpcls'] = $data['cstpcls'];

  $valueData['dmzkflg'] = $data['dmzkflg'];
  $valueData['dtcor'] = $data['dtcor'];
  $valueData['dyflg'] = $data['dyflg'];
  $valueData['dhfflg'] = $data['dhfflg'];
  $valueData['dgsiflg'] = $data['dgsiflg'];
  $valueData['spnoflg'] = $data['spnoflg'];
  $valueData['stcorflg'] = $data['stcorflg'];

  $valueData['cudpcls2'] = $data['cudpcls2'];
  $valueData['salpcls2'] = $data['salpcls2'];
  $valueData['cstpcls2'] = $data['cstpcls2'];
  $valueData['dmzkflg2'] = $data['dmzkflg2'];
  $valueData['dtcor2'] = $data['dtcor2'];
  $valueData['dyflg2'] = $data['dyflg2'];

  $valueData['dhfflg2'] = $data['dhfflg2'];
  $valueData['dgsiflg2'] = $data['dgsiflg2'];
  $valueData['spnoflg2'] = $data['spnoflg2'];
  $valueData['stcorflg2'] = $data['stcorflg2'];

  $valueData['staxcls'] = $data['staxcls'];
  $valueData['cutax'] = $data['cutax'];
  $valueData['cutaxc'] = $data['cutaxc'];
  $valueData['custdc'] = $stringUtil->toNumber($data['custdc']);
  $valueData['cutaxc1'] = $data['cutaxc1'];
  $valueData['custdc1'] = $stringUtil->toNumber($data['custdc1']);
  $valueData['upchke'] = $data['upchke'];
  $valueData['vdlmt'] = $stringUtil->toNumber($data['vdlmt']);
  $valueData['cllmt'] = $stringUtil->toNumber($data['cllmt']);

  $valueData['samtchk'] = $stringUtil->toNumber($data['samtchk']);
  $valueData['samtlmt'] = $stringUtil->toNumber($data['samtlmt']);
  $valueData['cusacls'] = $data['cusacls'];
  $valueData['curtcls'] = $data['curtcls'];
  $valueData['cuhcls'] = $data['cuhcls'];
  $valueData['dcubcd'] = $data['dcubcd'];
  $valueData['dcls'] = $data['dcls'];
  $valueData['cucat'] = $data['cucat'];

  $valueData['pcflg'] = $data['pcflg'];
  $valueData['cupcls'] = $data['cupcls'];
  $valueData['cupdt'] = $stringUtil->toNumber($data['cupdt']);
  $valueData['cupmn'] = $stringUtil->toNumber($data['cupmn']);
  $valueData['cupycls'] = $data['cupycls'];
  
  $valueData['com'] = $data['com'];
  $valueData['jwcom'] = $data['jwcom'];
  $valueData['rper1'] = $stringUtil->toNumber($data['rper1']);
  $valueData['rper2'] = $stringUtil->toNumber($data['rper2']);
  $valueData['rper3'] = $stringUtil->toNumber($data['rper3']);
  $valueData['rper4'] = $stringUtil->toNumber($data['rper4']);
  $valueData['rper5'] = $stringUtil->toNumber($data['rper5']);

  $valueData['jcls'] = $data['jcls'];
  $valueData['cencls'] = $data['cencls'];
  $valueData['cucuc'] = $data['cucuc'];

  $valueData['cubyr1'] = $data['cubyr1'];
  $valueData['cubyr2'] = $data['cubyr2'];
  $valueData['cubyr3'] = $data['cubyr3'];
  $valueData['cubyr4'] = $data['cubyr4'];
  $valueData['cubyr5'] = $data['cubyr5'];
  $valueData['curgin'] = $data['curgin'];

  $valueData['culct'] = $data['culct'];
  $valueData['cucpcls'] = $data['cucpcls'];
  $valueData['cuptax'] = $data['cuptax'];
  $valueData['aslmt'] = $stringUtil->toNumber($data['aslmt']);
  $valueData['asflg'] = $data['asflg'];

  $valueData['ltxt1'] = $data['ltxt1'];
  $valueData['ltxt2'] = $data['ltxt2'];
  $valueData['dtxt1'] = $data['dtxt1'];
  $valueData['dtxt2'] = $data['dtxt2'];
  $valueData['com2'] = $data['com2'];
  $valueData['undkbn'] = $data['undkbn'];
  $valueData['com3'] = $data['com3'];
  $valueData['com4'] = $data['com4'];
  $valueData['cuflcls'] = $data['cuflcls'];
  $valueData['cuflamt'] = $stringUtil->toNumber($data['cuflamt']);

  $valueData['cutaxdiv'] = $data['cutaxdiv'];
  $valueData['cutaxmxtrt'] = $data['cutaxmxtrt'];
  $valueData['cutaxdiff'] = $data['cutaxdiff'];

  $valueData['cuchgcd'] = $data['cuchgcd'];
  $valueData['custp'] = $data['custp'];

  $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;
}

?>
