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

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


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

$retData = array(
  "ret" => '0',
);

try {
  global $dbutil;

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

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

  $dbutil->commit();
  $response["data"]   = $retData;

} catch(userException $ue) {

  $logger->info("error1");
  $logger->info("error1",$ue->getErrorOraCode());
  $logger->info("error1",$ue->getErrorOraMessage());

  $wsqlcode = $ue->getErrorOraCode();
  $wsqlmsg = $ue->getErrorOraMessage();

  if($wsqlcode == '20001') {
    $retData["ret"]   = "-9";
    $retData["code"]  = $ue->getErrorOraCode();
    $retData["msg"]   = "売上が既に存在するため、店舗請求区分は変更できません。";
  
    $response["status"] = "OK";
    $response["data"]   = $retData;
  
  } else {
    $retData["ret"]   = "-9";
    $retData["code"]  = $ue->getErrorCode();
    $retData["msg"]   = $ue->getErrorMessage();
  
    $response["status"] = "NG";
    $response["data"]   = $retData;
  }


  $dbutil->rollback();

} catch(Exception $e) {
  $logger->info("error2");
  $response["status"] = "NG";
  $response["data"]   = $e->getMessage();

  $dbutil->rollback();

} finally {
  $logger->info("response", $response );
  print( json_encode($response) );
}

$logger->info("=== af9032x_updata_end ===");



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

  $valueData['cucd'] = $header['cucd'];
  $valueData['cuscd'] = $header['cuscd'];

  $valueData['spnm'] = $data['spnm'];
  $valueData['spnms'] = $data['spnms'];
  $valueData['spcls'] = $data['spcls'];
  $valueData['spknm'] = $data['spknm'];
  $valueData['shflg'] = $data['shflg'];
  $valueData['shfdt'] = $data['shfdt'];
  $valueData['biko'] = $data['biko'];

  $valueData['spzip'] = $data['spzip'];
  $valueData['spad1'] = $data['spad1'];
  $valueData['spad2'] = $data['spad2'];
  $valueData['spkad1'] = $data['spkad1'];
  $valueData['spkad2'] = $data['spkad2'];
  $valueData['sptel'] = $data['sptel'];
  $valueData['spfax'] = $data['spfax'];
  $valueData['splct'] = $data['splct'];
  
  $valueData['spknms'] = $data['spknms'];
  $valueData['spprio'] = $stringUtil->toNumber($data['spprio']);
  $valueData['spscm'] = $data['spscm'];

  $valueData['cuspcd'] = $data['cuspcd'];
  $valueData['cusycd'] = $data['cusycd'];
  $valueData['cuedi'] = $data['cuedi'];
  
  $valueData['spslcd'] = $data['spslcd'];
  $valueData['spslcd2'] = $data['spslcd2'];
  $valueData['spbscd'] = $data['spbscd'];
  $valueData['spktcd'] = $data['spktcd'];
  $valueData['cuspkf'] = $data['cuspkf'];

  $valueData['cuclcls'] = $data['cuclcls'];
  $valueData['cupcls'] = $data['cupcls'];
  $valueData['cupdt'] = $stringUtil->toNumber($data['cupdt']);
  $valueData['cupmn'] = $stringUtil->toNumber($data['cupmn']);
  $valueData['cupycls'] = $data['cupycls'];

  $valueData['cshcd'] = $data['cshcd'];
  $valueData['com'] = $data['com'];
  $valueData['spbucn'] = $data['spbucn'];
  $valueData['dcubcd'] = $data['dcubcd'];
  $valueData['dcls'] = $data['dcls'];
  $valueData['spcat'] = $data['spcat'];
  $valueData['splnk'] = $data['splnk'];
  $valueData['vdlmt'] = $stringUtil->toNumber($data['vdlmt']);
  $valueData['cllmt'] = $stringUtil->toNumber($data['cllmt']);

  $valueData['plcd'] = $data['plcd'];
  $valueData['plhocd'] = $data['plhocd'];
  $valueData['pladcd'] = $data['pladcd'];

  $valueData['spsnm'] = $data['spsnm'];
  $valueData['spsnmk'] = $data['spsnmk'];
  $valueData['spszip'] = $data['spszip'];
  $valueData['spsad1'] = $data['spsad1'];
  $valueData['spsad2'] = $data['spsad2'];
  $valueData['spskad1'] = $data['spskad1'];
  $valueData['spskad2'] = $data['spskad2'];
  $valueData['spstel'] = $data['spstel'];
  $valueData['spsfax'] = $data['spsfax'];

  $valueData['stp'] = $data['stp'];

  $valueUpdate = $valueData;

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

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

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

  return $reuslt;


return true;
}

?>
