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

$logger = new logger(basename(__FILE__));
$logger->info("=== af215x_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;
  global $serverUtil;

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

  $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"]   = "";
  $whereParams["ret"]["out"]   = "";
  $whereParams["obats"]["out"]  = "";
  $sql = file_get_contents("./sql/call_Fm_AF215X_Set.sql");
  $ret_plsql = $dbutil->call($sql,$whereParams);

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

  $bat = $dbutil->nextVal("innerbat");
  $wsid = "AP".$_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $pid = $serverUtil->getProductId();
  $dats = null;
  $datt = null;
  $pnm = $serverUtil->getProductName();
  if($header["sflg"] == '0') {
    $inf1 = "F9 確定";
  } else if($header["sflg1"] == '1') { //呼ばれないはずだけど一応移植
    $inf1 = "F10 確定&FAX&ﾌﾟﾘﾝﾀ";
  } else if($header["sflg1"] == '2') {
    $inf1 = "F11 確定%ﾌﾟﾘﾝﾀ";
  } else {
    $inf1 = "想定外";
  }
  $inf2 = "入力者:".$_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"]." 発注番号:".$ret_plsql["vseq"];;
  $valutil->insertAfs73($bat,$wsid,$pid,$dats,$datt,$pnm,$inf1,$inf2);
  //$dbutil->commit(); //不要？

  if($header["sflg1"] == '1') {
    //F10 確定&FAX&ﾌﾟﾘﾝﾀ から呼ばれた場合、plsql:Snd_Hattyu を呼ぶ想定をされていたらしいけど、
    //イシザワF10押せないので記述しない。
  }

  
  if($header["sflg"] == '1' || $header["sflg"] == '2') {
    //liv用(iReport用)に追加。発注データをaw851にinsertするplsqlのコール
    $pwseq = $dbutil->nextVal("innerwseq");
    $whereParams = array();
    $whereParams["wseq"]["in"]   = $pwseq;
    $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
    $whereParams["chgcd"]["in"]  = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
    $whereParams["dat1"]["in"]   = "";
    $whereParams["mcd"]["in"]    = $header["mcd"];
    $whereParams["vseqs"]["in"]  = "";
    $whereParams["vseqe"]["in"]  = "";
    $whereParams["obats"]["in"]  = $ret_plsql["obats"];
    $whereParams["ret"]["out"]   = "";
    $sql = file_get_contents("./sql/call_Fm_AF412X_Get.sql");
    $pret_plsql = $dbutil->call($sql,$whereParams);

    //liv用(iReport用)に追加。発注データをaw851にinsertするplsqlの結果を返す
    $retData["pret"]  = $pret_plsql["ret"];
    $retData["pwseq"] = $pwseq;
  }
 

  $retData["vseq"] = $ret_plsql["vseq"];
  $retData["ret"] = $ret_plsql["ret"];
  $retData["obats"] = $ret_plsql["obats"];

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




?>
