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

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

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

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

  $logger->info($data);

  // SQL実行
  $header = $data["header"];
  $fooger = $data["header"];

  $ret_array = array();    //return用
  $ret_detail = array();   //データ用
  $ret_detail2 = array();  //編集中店舗用用

  $ret = "0";

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

  $cnt = 0;
  //updMode 0:登録, 1:削除
  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); 
    }
  }


  /*
  // pl/sql のコール
  $whereParams=array();
  $whereParams["wseq"]["in"] = $wseq;
  $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $whereParams["cucd"]["in"] = $header["cucd"];
  $whereParams["cuscd"]["in"] = $header["cuscd"];
  $whereParams["vseq"]["in"] = $header["vseq"];
  $whereParams["cuiseq"]["in"] = $header["cuiseq"];
  $whereParams["ret"]["out"] = "";
  $sql = file_get_contents("./sql/call_Fm_AF203X_Get.sql");
$logger->info($sql,$whereParams);
  $ret_plsql = $dbutil->call($sql,$whereParams);
$logger->info("ret_plsql",$ret_plsql);
*/

  $wret = $ret_plsql["ret"];

  //ワークからデータ(編集中店舗)を取ってくる
  //新規の場合のafw81も作成しとってきて表示するのでここ
  $whereParams=array();
  $whereParams["wseq"] = $header["wseq"];
  $whereParams["cucd"] = $header["cucd"];
  $sql = file_get_contents("./sql/searchAfw81.sql");
  $result2 = $dbutil->select($sql, $whereParams);
  $ret_detail2 = $result2;



  $ret_array["ret"] = $ret;
  $ret_array["wret"] = $wret;
  $ret_array["msg"] = $msg;
  $ret_array["ret_array"] = $ret_detail;
  $ret_array["ret_array2"] = $ret_detail2;
  $ret_array["ret_header"] = $ret_header;
  $ret_array["cnt"] = $cnt;
  $result = $ret_array;

  $logger->info('getDetail_ret',$result );
  $response["status"] = "OK";
  $response["data"]   = json_encode($result, true);

} 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 {
  print(json_encode($response));
}


$logger->info("=== af203x_doCopy_end ===", $response);

?>
