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

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

//返り値
//ret : 0:正常終了, -1:重複
$ret_array = array(
  "ret" => "0",
  "vseq" => "",
);


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

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

$header = $data["header"];

try {
  global $dbutil;
  global $stringUtil;
  $valutil = new getValueUtil();

  $ret = true;

  //afw851にinsertし、plsqlでafr3123を作成する(plsqlある場合外出ししたほうがcommitタイミング上都合がいいため)
  //livlaboの場合はplsqlの中で、afr31.cls(25)と、afr3111も作成する。あとafr3123も出庫済みに更新する

  $header["wseq"] = $dbutil->nextVal("innerwseq");
  $wlcnt = 0; //afw851のvlin要
  $cnt = 0;   //isnert有無判別用
  foreach($data["data"] as $detail) {
    if(!$stringUtil->isEmpty($detail["iseq"]) || !$stringUtil->isEmpty($detail["jan"])) {
      if($data["updMode"] == '1' || $detail["chkLineDel"] == '1') {
        if(!$stringUtil->isEmpty($detail["iseq"])) {
          $cnt = $cnt + 1;
          insertAfw851($header, $detail, $dbutil, $logger, null, '1'); //delete
        }
      } else {
        $wlcnt = $wlcnt + 1;
        $cnt = $cnt + 1;
        insertAfw851($header, $detail, $dbutil, $logger, $wlcnt, '0'); //afr3123のdelete
      }
    }
  }

  if($cnt > 0) {
    $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["vseq"]["in"]    = $header["vseq"];
    $whereParams["cuiseq"]["in"]  = $header["cuiseq"];
    $whereParams["pmode"]["in"]   = $header["issue_mode"]; //登録時実績まで作るか。axm994.cls(AF251X).r02 0:作らない、1:作る
    //$whereParams["pmode"]["in"]   = "0";
    $whereParams["rvseq"]["out"]  = "";
    $whereParams["dcnt"]["out"]   = "";
    $whereParams["ret"]["out"]    = "";
    $sql = file_get_contents("./sql/call_Fm_AF251X_Set.sql");
    $ret_plsql = $dbutil->call($sql,$whereParams);
    $logger->info("call_Fm_AF251X_Set",$ret_plsql);
  }
    

  //$ret_array["vseq"] = $header["vseq"];
  //$ret_array["dcnt"] = $wdcnt;
  $ret_array["ret"]  = $ret_plsql["ret"];
  $ret_array["vseq"] = $ret_plsql["rvseq"];
  $ret_array["dcnt"] = $ret_plsql["dcnt"];

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


function insertAfw851($header, $data, $dbutil, $logger, $vlin, $dflg) {
  global $stringUtil;


  $valueData["wseq"]    = $header["wseq"];
  $valueData["vlin"]    = $vlin;

  $valueData["ktktcd"]  = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $valueData["spktcd1"] = $header["spktcd1"];
  $valueData["ijan"]    = $data["ijan"];
  $valueData["cuiseq"]  = $header["cuiseq"];
  $valueData["com"]     = $header["com"];
  $valueData["spktcd2"] = $header["spktcd2"];
  $valueData["chgcd"]   = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
  $valueData["climit"]  = $header["climit"];

  $valueData["dflg"]    = $dflg; //1:削除, 0:ins/upd
  $valueData["vseq"]    = $header["vseq"];
  $valueData["jan"]     = $data["jan"];
  $valueData["iseq"]    = $data["iseq"];
  $valueData["mvpno"]   = $stringUtil->toNumber($data["mvpno"]);


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

  $sql = file_get_contents("./sql/insertAfw851.sql");
  $result = $dbutil->insert($sql, $valueData);

  return $reuslt;
}


?>
