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

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

//返り値
//ret : 0:正常終了, -1:重複
$ret_array = array(
  "wret" => "0",
  "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;

  //伝票番号の入力がない場合は自動採番
  if($data["updMode"] != '1' && $stringUtil->isEmpty($header["vseq"])) {
    $header["vseq"] = $valutil->get_seq("1");
  }

  //発注方式取得
  $whereParams = array();
  $whereParams["mcd"] = $header["mcd"];
  $sql = file_get_contents("./sql/getModfr.sql");
  $ret04 = $dbutil->select($sql, $whereParams);

  if($stringUtil->isEmpty($header["cuiseq"])) { //新規の場合は内部伝票番号を採番
    $header["cuiseq"] = $dbutil->nextVal("innerviseq");
  }

  //通常登録と即時確定
  if($header["sflg"] == '0') {
    //通常登録
    $header["sts"] = 'N';
    $header["obats"] = '';
    $header["ktchg"] = '';
  } else if($header["sflg"] == '1') {
    //即時確定
    $header["sts"] = ' '; //'X'でなく、' '!!!  ishizawa(forms)->liv(php)で
    $header["obats"] =  $dbutil->nextVal("innerobat");
    $header["ktchg"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
  } if($header["sflg"] == '2') {
    //即時確定(呼ばれないはずだけど一応)
    $header["sts"] = ' '; //'X'でなく、' '!!! 　ishizawa(forms)->liv(php)で
    $header["obats"] =  $dbutil->nextVal("innerobat");
    $header["ktchg"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
  }


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

  $wlcnt = 0;
  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"])) {
          deleteAfr3111($header, $detail, $dbutil, $logger); //afr3111のdelete
        }
      } else {
        //更新
        $wlcnt = $wlcnt + 1;

        //sum(afr3111.nrld)の取得
        $whereParams = array();
        $whereParams["spktcd"] = $header["spktcd"];
        $whereParams["ijan"] = $detail["ijan"];
        $sql = file_get_contents("./sql/getNrld.sql");
        $retGetModfr = $dbutil->select($sql, $whereParams);
        $detail["nrld"] = $retGetModfr[0]["nrld"];


        if($stringUtil->isEmpty($detail["iseq"])) {
          insertAfr3111($header, $detail, $dbutil, $logger, $wlcnt); //afr3111と、afr31txtをinsert
        } else {
          updateAfr3111($header, $detail, $dbutil, $logger, $wlcnt); //afr3111と、afr31txtをupdate
        }
      }

    }
  }

  //確定の場合、発注データ送信plsqlのコール
  //ishizawa(forms)->liv(php)でよばなくなった
//  if($header["sflg"] == '1') {
//    $sql = file_get_contents("./sql/callSndHattyu.sql");
//    $whereParams = array();
//    $whereParams["obats"]["in"] = $header["obats"];
//    $whereParams["vseq"]["in"]  = $header["vseq"];
//    $whereParams["dat1"]["in"]  = $header["dat1"];
//    $whereParams["chgcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
//    $whereParams["wret"]["out"] = "";
//    $whereParams["wmsg"]["out"] = "";
//    $retplsql = $dbutil->call($sql,$whereParams);
//$logger->info("retplsql",$retplsql);
//
//    $ret_array["wret"] = $retplsql["wret"];    
//    if((int)$retplsql["wret"] < 0) {
//      $ret_array["msg"] = $retplsql["wmsg"];
//    }
//  }

  $dbutil->commit();


  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"]   = $header["dat1"];
    $whereParams["mcd"]["in"]    = $header["mcd"];
    $whereParams["vseqs"]["in"]  = $header["vseq"];
    $whereParams["vseqe"]["in"]  = $header["vseq"];
    $whereParams["obats"]["in"]  = "";
    $whereParams["ret"]["out"]   = "";
    $sql = file_get_contents("./sql/call_Fm_AF412X_Get.sql");
    $pret_plsql = $dbutil->call($sql,$whereParams);

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


  $ret_array["vseq"] = $header["vseq"];
  $ret_array["dcnt"] = $wdcnt;
  $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("=== af212x_updata_end ===");

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

  $valueData["iseq"] = $data["iseq"];
  $logger->info("deleteData", $valueData);
  
  $sql = file_get_contents("./sql/delete.sql");
  $result = $dbutil->delete($sql, $valueData);

  $logger->info("deleteResult", $result);

  return $result;
}

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

  $valueData = array();
  $valueData["spktcd"]  = $header["spktcd"];
  $valueData["dat"]     = $header["dat"];
  $valueData["dat1"]    = $header["dat1"];
  $valueData["com1"]    = $header["com1"];
  
  $valueData["vlin"]    = $vlin;
  $valueData["jan"]     = $data["jan"];
  $valueData["ijan"]    = $data["ijan"];
  $valueData["mvpno"]   = $stringUtil->toNumber($data["mvpno"]);
  $valueData["cst"]     = $stringUtil->toNumber($data["cst"]);
  $valueData["com"]     = $data["com"];

  $valueData["modfr"]   = $header["modfr"];
  $valueData['chgcd']   = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
  $valueData["sts"]     = $header["sts"]; 
  $valueData["obats"]   = $header["obats"];
  $valueData["nrld"]    = $data["nrld"];
  $valueData["ktchg"]   = $header["ktchg"];

  $valueData["iseq"] = $data["iseq"];
  $logger->info("valueData_update",$valueData);

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

  return $reuslt;
}

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

  $valueData = array();
  $valueData['ktktcd'] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $valueData["spktcd"] = $header["spktcd"];

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

  $valueData["jan"] = $data["jan"];
  $valueData["mcd"] = $header["mcd"];

  $valueData["mvpno"] = $stringUtil->toNumber($data["mvpno"]);
  $valueData["cst"] = $stringUtil->toNumber($data["cst"]);
  $valueData["modfr"] = $header["modfr"];
  $valueData['chgcd'] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
  $valueData["dat"] = $header["dat"];

  $valueData["sts"] = $header["sts"];
  $valueData["obats"] = $header["obats"];
  $valueData["dat1"] = $header["dat1"];
  $valueData["com1"] = $header["com1"];
  $valueData["com"] = $data["com"];

  $valueData["cuiseq"] = $header["cuiseq"];
  $valueData["nrld"] = $data["nrld"];
  $valueData["ktchg"] = $header["ktchg"];

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

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

  return $reuslt;
}

?>
