<?php
//共通
require_once '../../base/common.php';
require_once './allways.php';

$logger = new logger(basename(__FILE__));
$logger->info("=== af203x_update_start ===");

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

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

$logger->info("data", $data);
$header = $data["header"];
$footer = $data["footer"];

try {
  global $dbutil;
  global $stringUtil;

  $ret = true;


  $valutil = new getValueUtil();

  if($stringUtil->isEmpty($header["wseq"])) {
    $header["wseq"] = $dbutil->nextVal("innerwseq");
  }

  $cnt = 0;
  //updMode 0:登録, 1:削除
//  if($data["updMode"] == '0') {
    foreach($data["data"] as $detail) { // insert/update
      if(!$stringUtil->isEmpty($detail["iseq80"]) || !$stringUtil->isEmpty($detail["ijan"])) {
        $cnt = $cnt + 1;

        if($data["updMode"] == '1') {
          $detail["chkLineDel"] = '1';
        }

        //複写時にも実行するので、allwaysに記述
        insertUpdateAfw80($header, $detail, $footer, $dbutil, $logger, $cnt);
      }
    }
//  }


  //複写されている分のpkbn1、pkbn2の更新
  $whereParams = array();
  $whereParams["wseq"] = $header["wseq"];
  $whereParams["pkbn1"] = $header["pkbn1"];
  $whereParams["pkbn2"] = $header["pkbn2"];
  $sql = file_get_contents("./sql/updateAfw80Pkbn.sql");
  $dbutil->update($sql,$whereParams);
  $logger->info("updAfw80Pkbn>>".$sql,$whereParams);

  $dbutil->commit();

  //plsql コール
  $whereParams=array();
  $whereParams["wseq"]["in"] = $header["wseq"];
  $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $whereParams["chgcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
  $whereParams["pmode"]["in"] = '1';
  $whereParams["vseq"]["out"] = "";
  $whereParams["ret"]["out"] = "";
  $sql = file_get_contents("./sql/call_Fm_AF203X_Set.sql");
  $ret_plsql = $dbutil->call($sql,$whereParams);

  $logger->info("ret_plsql",$ret_plsql);


 $retData["vseq"] = $ret_plsql["vseq"];
 $retData["cslno"] = $ret_plsql["cslno"];
 $retData["vret"] = $ret_plsql["ret"];
 $response["data"]   = $retData;

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




?>
