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

$response["status"] = "OK";
$ret_array = array(
  "ret" => '0',
);
$ret = '0';


$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);
$logger->info("data", $data );

$header = $data["header"];

try {
  global $dbutil;

  // tdat <= sdday の場合、reqから本体に登録するplsqlをコールする
  $wtdat = strtotime($data["data"][0]["tdat"]);
  $wsdday = strtotime($_SESSION[$GLOBALS['SYSTEM_NAME_EN']]['SDDAY']);


  $cnt = 0;
  foreach($data["data"] as $detail) {
    $cnt = $cnt + 1;
    if($cnt == 1) { //afm02について
      insertUpdateAfm02( $header, $detail, $dbutil, $logger );

      //新規の場合を考慮して、ijanをjanで取り直す
      //afm02e, afm02dの更新に使う
      $whereParams=array();
      $whereParams["jan"] = $header["jan"];
      $sql = file_get_contents("./sql/getIjan.sql");
      $result = $dbutil->select($sql, $whereParams);
      $header["ijan"] = $result[0]["ijan"];

      //afm02eは、まずすべての行に対して、stp=sysdate, rkbn='1'をセットする
      //(その後画面の値で正しく更新する)
      $valueData = array();
      $valueData['ijan'] = $header['ijan'];
      $sql = file_get_contents("./sql/setStpAfm02eAll.sql");
      $result = $dbutil->update($sql, $valueData);

      //afm02dは、まずすべての行を削除する
      //(その後画面の値でinsertする)
      $valueData = array();
      $valueData['ijan'] = $header['ijan'];
      $sql = file_get_contents("./sql/deleteAfm02dAll.sql");
      $result = $dbutil->delete($sql, $valueData);


    } else  { //多明細のところ

      if($detail["tblnm"] == "afm02e") {
        if(!$stringUtil->isEmpty($detail["m02emcd"])) {
          insertUpdateAfm02e($header, $detail, $dbutil, $logger);
        }
      } else if($detail["tblnm"] == "afm02d") {
        if($detail["m02dchkLineDel"] != '1') {
          if(!$stringUtil->isEmpty($detail["m02dktktcd"])) {
            insertAfm02d($header, $detail, $dbutil, $logger);
          }
        }
      }
    }

  }
  $dbutil->commit();


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



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

  $valueData = array();
	$valueData["jan"]	 	   = $header["jan"];	
	$valueData["vjan"]	 	 = $data["vjan"];	
	$valueData["gdjnm"]	 	 = $data["gdjnm"];	
	$valueData["gdjstnd"]	 = $data["gdjstnd"];	
	$valueData["gdnms"]	 	 = $data["gdnms"];	
	$valueData["gdnm"]	 	 = $data["gdnm"];	
	$valueData["gdstnd"]	 = $data["gdstnd"];	
	$valueData["itfcd"]	 	 = $data["itfcd"];	
	//$valueData["gdclsd"]	 = $data["gdclsd"];	//ishizawaで画面非表示なのでメンテしない
	$valueData["brand"]	 	 = $data["brand"];	
  
	$valueData["gdacst"]	 = $stringUtil->toNumber($data["gdacst"]);	
	$valueData["gdsal"]	 	 = $stringUtil->toNumber($data["gdsal"]);	
	$valueData["gdbcst"]	 = $stringUtil->toNumber($data["gdbcst"]);	
	$valueData["gdcst"]	 	 = $stringUtil->toNumber($data["gdcst"]);	
	$valueData["gdtsal"]	 = $stringUtil->toNumber($data["gdtsal"]);	

	$valueData["gdcls1"]	 = $data["gdcls1"];	
	$valueData["gdcls2"]	 = $data["gdcls2"];	
	$valueData["gdcls3"]	 = $data["gdcls3"];	
	$valueData["gdpkf"]	 	 = $data["gdpkf"];	

	$valueData["gdcls6"]	 = $data["gdcls6"];	
	$valueData["taxrtd"]	 = $data["taxrtd"];	
	$valueData["gdclsg"]	 = $data["gdclsg"];	
	$valueData["gdtax"]	 	 = $data["gdtax"];	
	$valueData["gdtaxn"]	 = $data["gdtaxn"];	

	$valueData["gdcls7"]	 = $data["gdcls7"];	
	$valueData["gdcls8"]	 = $data["gdcls8"];	
	$valueData["gdcls9"]	 = $data["gdcls9"];	
	$valueData["gdstun"]	 = $stringUtil->toNumber($data["gdstun"]);	

	$valueData["gdunm"]	 	 = $data["gdunm"];	
	$valueData["gdcnum"]	 = $stringUtil->toNumber($data["gdcnum"]);	
	$valueData["gdcwid"]	 = $stringUtil->toNumber($data["gdcwid"]);	
	$valueData["gdcdep"]	 = $stringUtil->toNumber($data["gdcdep"]);	
	$valueData["gdchig"]	 = $stringUtil->toNumber($data["gdchig"]);	
	$valueData["gdcwgh"]	 = $stringUtil->toNumber($data["gdcwgh"]);	
	$valueData["gdbnum"]	 = $stringUtil->toNumber($data["gdbnum"]);	
	$valueData["gdbwid"]	 = $stringUtil->toNumber($data["gdbwid"]);	
	$valueData["gdbdep"]	 = $stringUtil->toNumber($data["gdbdep"]);	
	$valueData["gdbhig"]	 = $stringUtil->toNumber($data["gdbhig"]);	
	$valueData["gdbwgh"]	 = $stringUtil->toNumber($data["gdbwgh"]);	
	$valueData["gdpwid"]	 = $stringUtil->toNumber($data["gdpwid"]);	
	$valueData["gdpdep"]	 = $stringUtil->toNumber($data["gdpdep"]);	
	$valueData["gdphig"]	 = $stringUtil->toNumber($data["gdphig"]);	
	$valueData["gdpwgh"]	 = $stringUtil->toNumber($data["gdpwgh"]);	

	$valueData["gdcnum2"]	 = $stringUtil->toNumber($data["gdcnum2"]);	
	$valueData["gdcwid2"]	 = $stringUtil->toNumber($data["gdcwid2"]);	
	$valueData["gdcdep2"]	 = $stringUtil->toNumber($data["gdcdep2"]);	
	$valueData["gdchig2"]	 = $stringUtil->toNumber($data["gdchig2"]);	
	$valueData["gdcwgh2"]	 = $stringUtil->toNumber($data["gdcwgh2"]);	
	$valueData["gdbnum2"]	 = $stringUtil->toNumber($data["gdbnum2"]);	
	$valueData["gdbwid2"]	 = $stringUtil->toNumber($data["gdbwid2"]);	
	$valueData["gdbdep2"]	 = $stringUtil->toNumber($data["gdbdep2"]);	
	$valueData["gdbhig2"]	 = $stringUtil->toNumber($data["gdbhig2"]);	
	$valueData["gdbwgh2"]	 = $stringUtil->toNumber($data["gdbwgh2"]);	
	$valueData["gdpwid2"]	 = $stringUtil->toNumber($data["gdpwid2"]);	
	$valueData["gdpdep2"]	 = $stringUtil->toNumber($data["gdpdep2"]);	
	$valueData["gdphig2"]	 = $stringUtil->toNumber($data["gdphig2"]);	
	$valueData["gdpwgh2"]	 = $stringUtil->toNumber($data["gdpwgh2"]);	

	$valueData["gdhtun"]	 = $stringUtil->toNumber($data["gdhtun"]);	
	$valueData["htlmt1"]	 = $stringUtil->toNumber($data["htlmt1"]);	
	$valueData["htlmt2"]	 = $stringUtil->toNumber($data["htlmt2"]);	
	$valueData["rldat"]		 = $data["rldat"];	
	$valueData["mknm"]		 = $data["mknm"];	
	$valueData["slimit"]	 = $stringUtil->toNumber($data["slimit"]);	
	$valueData["glimit"]	 = $stringUtil->toNumber($data["glimit"]);	
	$valueData["gdtcls"]	 = $data["gdtcls"];	
	$valueData["gdretcls"] = $data["gdretcls"];	
	$valueData["gdtpc"]	 	 = $data["gdtpc"];	
	$valueData["gdhdfc"]	 = $data["gdhdfc"];	
	$valueData["gdpopcmt"] = $data["gdpopcmt"];	
	$valueData["gdcom"]	 	 = $data["gdcom"];	
	$valueData["dscom"]	 	 = $data["dscom"];	
	$valueData["dsdat"]	 	 = $data["dsdat"];	
  $valueData['chgcd']    = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
	$valueData["mcd"]	 	   = $data["m02mcd"];	//裏側で持っておいた、代表仕入れ先
	$valueData["meka"]	 	 = $data["meka"];	
	$valueData["gdstp"]	 	 = $data["gdstp"];	


  $valueUpdate = $valueData;
  $valueUpdate['ijan'] = $header['ijan']; /*update時条件*/

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

  //まず更新
  $sql = file_get_contents("./sql/update.sql");
  $result = $dbutil->update($sql, $valueUpdate);
  $logger->info("did_update");

  if($result == 0){ //UpdateできなかったらInsert
    $sql = file_get_contents("./sql/insert.sql");
    $result = $dbutil->insert($sql, $valueData);
    $logger->info("did_insert");
  }

  return $reuslt;
}

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

  //既存データは、stp=sysdate,rkbn='1'を最初にセットしてあるので、
  //画面データで正しくupdateする。出来なければinsert

  $valueData = array();
  $valueData['ijan'] = $header['ijan']; /*update時条件*/
  $valueData['mcd']  = $data['m02emcd'];

  $valueData['gdcst']    = $stringUtil->toNumber($data['m02egdcst']);
  $valueData['gdcst1']   = $stringUtil->toNumber($data['m02egdcst1']);
  $valueData['gdhcst']   = $stringUtil->toNumber($data['m02egdhcst']);
  if($data['m02echkRkbn'] == "ON") {
    $valueData['rkbn']   = '0';
  } else {
    $valueData['rkbn']   = '1';
  }
  $valueData['gdretcls'] = $data['m02egdretcls'];
  $valueData['chgcd'] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];

  $valueUpdate = $valueData;
	$valueUpdate["gdstp"]	 	 = $data["m02egdstp"];	

  $logger->info("valueUpdate_afm02e",$valueUpdate);

  //まず更新
  $sql = file_get_contents("./sql/update02e.sql");
  $result = $dbutil->update($sql, $valueUpdate);
  $logger->info("did_update");
  if($result == 0) { //UpdateできなかったらInsert
    $sql = file_get_contents("./sql/insert02e.sql");
    $result = $dbutil->insert($sql, $valueData);
    $logger->info("did_insert");
  }

  return $reuslt;
}

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

  //既存データは、最初に全て物理deleteしてあるので、
  //画面データでinsertする。

  $valueData = array();
  $valueData['ijan']   = $header['ijan']; /*update時条件*/
  $valueData['ktktcd'] = $data['m02dktktcd'];

  $valueData['htcls']  = $data['m02dhtcls'];
  $valueData['htpcls'] = $data['m02dhtpcls'];

  $valueData['htpnt1']  = $stringUtil->toNumber($data['m02dhtpnt1']);
  $valueData['htpnt2']  = $stringUtil->toNumber($data['m02dhtpnt2']);
  $valueData['fhtpnt1'] = $stringUtil->toNumber($data['m02dfhtpnt1']);
  $valueData['fhtpnt2'] = $stringUtil->toNumber($data['m02dfhtpnt2']);

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

  $sql = file_get_contents("./sql/insert02d.sql");
  $result = $dbutil->insert($sql, $valueData);
  $logger->info("did_insert");

  return $reuslt;
}


?>
