<?php
/*
* @Overview 품목 등록
* @History 2024-03-28 / 박민희 / 최초작성
*/

$cate_id = "BASE_ITEM";
include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _SVC_INC_PATH_ ."/auth_check.php";
include_once _HOME_INC_PATH_ ."/ajax_init.php";

$_tmp_dir = _UP_PATH_."/temp"; //임시파일저장경로
$_draw_dir = _UP_PATH_."/draw"; //도면파일저장경로
$_rout_dir = _UP_PATH_."/rout"; //라우트파일저장경로

if(!_notAuth("R")) {
  switch($_REQUEST['mode']) {
    case 'getItemList':
      $sql = "USP_B_ITEM_S10";
      $params = Array(
        "ITEM_GRP"    => $_REQUEST['ITEM_GRP'],
        "ITEM_CD"     => $_REQUEST['ITEM_CD'],
        // "DRAW_NO"     => $_REQUEST['DRAW_NO'],
      );
      $rst = $baseObj->DB->exec($sql,$params);

      $rows = array();
      if( $rst ){
        $retval['code'] = true;
        while( $row = $baseObj->DB->fetch_array($rst) ){
          $rows[] = $row;
        };
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      }else{
        $retval['code'] = false;
      }
      break;
      
    case 'putItem':
      if( _notAuth("W") ) break;

      $_REQUEST["reqdata"] = json_decode($_REQUEST['reqdata'], true);
      $rst = true;
      foreach( $_REQUEST['reqdata'] as $data ){
        $_REQUEST['RS_CODE'] = "";
        $_REQUEST['RS_MSG'] = "";
        $data =(array)$data;
         $params = Array(
             "ITEM_CD"      => $data['ITEM_CD'] ?: "",
             "ITEM_NM"      => $data['ITEM_NM'] ?: "",
             "SPEC"         => $data['SPEC'] ?: "",
             "ITEM_ACCNT"   => $data['ITEM_ACCNT'] ?: "",
             "ITEM_GRP"     => $data['ITEM_GRP'] ?: "",
             "DRAW_NO"      => $data['DRAW_NO'] ?: "",
             "DRAW_VER"     => $data['DRAW_VER'] ?: "",
             "USE_FLG"      => $data['USE_FLG'] ?: "",
             "MAKE_USER_ID" => $GLOBALS['_Umem']['member_id'],
             "EDIT_USER_ID" => $data['EDIT_USER_ID'] ?: "",
             "RS_CODE"      => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
             "RS_MSG"       => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
            );

        switch($data['_STATUS_']) {
            case 'I':
            $sql = "USP_B_ITEM_I10";
            break;
            case 'U':
            $sql = "USP_B_ITEM_U10";
            break;
            case 'D':
            $sql = "USP_B_ITEM_D10";
            $params = Array(
              'ITEM_CD'   => $data['ITEM_CD']
            );
            break;

        };

        $rst = $baseObj->DB->exec($sql, $params);
      }
      if( $_REQUEST['RS_CODE'] === "E" ){
        $retval['mesg']  = $_REQUEST['RS_MSG'];
        $retval['code'] = false;
      }else{
        $retval['code'] = true;
      }
      break;

    case 'copyItem':
      if( _notAuth("W") ) break;

      $_REQUEST['RS_CODE'] = "";
      $_REQUEST['RS_MSG']  = "";

      $rst = true;
      $ok_cd = array();

      $_REQUEST['RS_CODE'] = "";
      $_REQUEST['RS_MSG']  = "";

      $sql = "USP_B_ITEM_I20";
      $params = Array(
        "BASE_CD"    => $_REQUEST['BASE_CD']?:"",
        "ITEM_CD"    => $_REQUEST['ITEM_CD']?:"",
        "ITEM_NM"    => $_REQUEST['ITEM_NM']?:"",

        "MAKE_USER_ID"  => $GLOBALS['_Umem']['member_id'],
        "RS_CODE"       => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
        "RS_MSG"        => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
      );
      
      $rst =  $baseObj->DB->exec($sql, $params);

      $retval['data'] = $ok_cd;

      if( $_REQUEST['RS_CODE'] === "E" ){
        $retval['mesg']  = $_REQUEST['RS_MSG'];
        $retval['code'] = false;
      }else{
        $retval['code'] = true;
      }
      break;

    case 'getRoutingList':
      $sql = "USP_B_ROUTING_S10";
      $params = Array(
        "ITEM_CD"     => $_REQUEST['ITEM_CD']?:"",
      );
      $rst = $baseObj->DB->exec($sql,$params);

      $rows = array();
      if( $rst ){
        while( $row = $baseObj->DB->fetch_array($rst) ){
          $rows[] = $row;
        };

        $retval['code'] = true;
        $retval['data'] = $rows;
//        print_r($retval['data']);
        $retval['mesg'] = '정상 동작 되었습니다.';
      }else{
        $retval['code'] = false;
      }
      break;
//    case 'getRoutingList':
//      $sql = "USP_B_ROUTING_S10";
//      $params = Array(
//        "ITEM_CD"     => $_REQUEST['ITEM_CD'],
//      );
//      $rst = $baseObj->DB->exec($sql,$params);
//
//      $rows = array();
//      if( $rst ){
//        $retval['code'] = true;
//        while( $row = $baseObj->DB->fetch_array($rst) ){
//          $rows[] = $row;
//        };
//        $retval['data'] = $rows;
//        $retval['mesg'] = '정상 동작 되었습니다.';
//      }else{
//        $retval['code'] = false;
//      }
//      break;

    //라우팅 등록/ 수정 / 삭제
    case "putRouting":
      if( _notAuth("W") ) break; /************ 권한체크*/
      $_REQUEST['RS_CODE'] = "";
      $_REQUEST['RS_MSG'] = "";

      $_REQUEST['reqdata'] = json_decode($_REQUEST['reqdata']);
      $rst = true;
//      $ok_cd = array();

      foreach( $_REQUEST['reqdata'] as $data ){
        $_REQUEST['RS_CODE'] = "";
        $_REQUEST['RS_MSG']  = "";
        $data = (array)$data;
        $cdata = $data;
        unset($cdata['_STATUS_']);
        unset($cdata['USE_FLG']);
        if( empty(array_filter($cdata)) ){
          if( $data['_STATUS_'] == 'I' ) continue; //빈열제거
          if( $data['_STATUS_'] == 'U' ) $data['_STATUS_'] = 'D'; //빈열제거
        }
        if(!$data['OP_CD']) { $retval['msg'] = cUtil\Mlang::getText("공정코드가 누락된 행은 반영되지 않습니다."); $rst=false; break; }
        else if(!$data['OP_NM']) { $retval['msg'] = cUtil\Mlang::getText("공정명이 누락된 행은 반영되지 않았습니다."); $rst=false; break; }


        $cdata = $data;


        if( empty(array_filter($cdata)) ){
          if( $data['_STATUS_'] == 'I' ) continue; //빈열제거
          if( $data['_STATUS_'] == 'U' ) $data['_STATUS_'] = 'D'; //빈열제거
        }


        if( $data['OP_CD'] && $data['_STATUS_'] == 'D' ){
          $sql = "USP_B_ROUTING_D10";
          $params = Array(
            "ROUT_SQNO"     => $data['ROUT_SQNO'],
            "ITEM_CD"       => $data['ITEM_CD'],
            "OP_CD"         => $data['OP_CD'],
          );
          $retval['delete'] = true;
//          $retval['code']   = 1;
        }else{
          if( $data['OP_CD'] && $data['_STATUS_'] == 'U' ) {
//            $chkparams[] = $data['BIZ_AREA_CD_OLD']; //CD 중복검사용

            $sql = "USP_B_ROUTING_U10";
            $params = Array();



          }else{
            $sql = "USP_B_ROUTING_I10";
            $params = Array();


          }

          $params = array_merge($params, Array(
            "ITEM_CD"        => $data['ITEM_CD']?:"",
            "ROUT_SQNO"      => $data['ROUT_SQNO']?:"",
            "OP_SEQ"         => $data['OP_SEQ']?:"",
            "OP_CD"          => $data['OP_CD'],
            "STD_SETUP_TM"   => floatval($data['STD_SETUP_TM']) == "" ? null : floatval($data['STD_SETUP_TM']),
            "CYCLE_TM"       => floatval($data['CYCLE_TM'])     == "" ? null : floatval($data['CYCLE_TM']),
            "STD_OP_AMT"     => floatval($data['STD_OP_AMT'])   == "" ? null : floatval($data['STD_OP_AMT']),
            "FRST_OP_FLG"    => $data['FRST_OP_FLG']?:"Y",
            "LAST_OP_FLG"    => $data['LAST_OP_FLG']?:"Y",
            "PRV_OP_CD"      => $data['PRV_OP_CD']       ?:"",
            "NXT_OP_CD"      => $data['NXT_OP_CD']       ?:"",
            "MAKE_USER_ID"   => $GLOBALS['_Umem']['member_id'],
            "RS_CODE"        => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
            "RS_MSG"         => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
          ));
        }
        if($data['ROUT_SQNO']){
          $data['rout_sqno'] =  $baseObj->DB->result($baseObj->DB->exec($sql, $params));
          $data['rout_sqno'] = $data['ROUT_SQNO'];
          $retval['std'] = $_REQUEST['RS_CODE'];
        }else{
          $data['rout_sqno'] =  $baseObj->DB->result($baseObj->DB->exec($sql, $params));
          $retval['std'] = $_REQUEST['RS_CODE'];
        }

        if($data['rout_sqno']){

//          $upfiles = array();
//          if ($data['TMP_NAME'] && file_exists($_tmp_dir . "/{$data['TMP_NAME']}")) {
//            $upfiles = \Frody\Util\MultiFileUpload::swfFormFileInfo($data['TMP_NAME'], $_tmp_dir);
//          }
//
//          if ($upfiles) {
//
//            $rout_params = array(
//
//              "ITEM_CD" => $data['ITEM_CD'],        // 품목코드
//              "ROUT_SQNO" => $data['ROUT_SQNO']?:$data['rout_sqno'],    // 라우팅일련번호
//              "FILE_NM"   => $upfiles[0]['name'],   // 파일 이름
//              "MAKE_USER_ID" => $GLOBALS['_Umem']['member_id'], // 계정 ID
//            );
//
//            $rout_sql = "USP_B_ROUTING_FILE_I10";
//
//            $rst = $baseObj->DB->exec($rout_sql, $rout_params);
//          }
//          if ($rst) {
//            \Frody\Util\MultiFileUpload::swfMoveUploadedFile($upfiles[0]['tmp_name'], "{$_rout_dir}/{$data['ITEM_CD']}_{$data['rout_sqno']}.dat");
//          }
        }

      }

      if($rst){
        $retval['msg'] = $_REQUEST['RS_MSG'];
        $retval['code'] = 1;
      }else{
        $retval['code'] = "0";
      }
      break;
    case "getRoutFile":

      $sql = "USP_B_ROUTING_FILE_S10";

      $params = Array(
        "ITEM_CD"       => $_REQUEST['ITEM_CD'],
        "ROUT_SQNO"     => intval($_REQUEST['ROUT_SQNO']),
      );
      $rst = $baseObj->DB->exec($sql, $params);
      if( $rst ){
        $retval['code'] = true;
        while( $row = $baseObj->DB->fetch_array($rst) ){
          $rows[] = $row;
        };
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      }else{
        $retval['code'] = false;
      }
      break;

    case "putRoutFile":



      $_REQUEST["reqdata"] = json_decode($_REQUEST['reqdata'], true);
      $_REQUEST['RS_CODE'] = "";
      $_REQUEST['RS_MSG'] = "";
      $rst = false;
      foreach( $_REQUEST['reqdata'] as $data ){
        $data = (array)$data;

        if( $data['_STATUS_'] == 'D' ){

          $sql = "USP_B_ROUTING_FILE_D10";
          $params = Array(
            "ITEM_CD"       => $data['ITEM_CD'],
            "FILE_ID"       => $data['FILE_ID'],
            "ROUT_SQNO"     => $data['ROUT_SEQNO'],

          );
//          $retval['delete'] = true;
//          $retval['code']   = 1;
          $rst = $baseObj->DB->exec($sql, $params);
          if($rst){
            @unlink("{$_rout_dir}/{$data['ITEM_CD']}_{$data['ROUT_SEQNO']}_{$data['FILE_ID']}.dat");
          }

          rename("{$_rout_dir}/TESTITEM_17105.dat", "{$_rout_dir}/TESTITEM_17105_55.dat");
        }else{
//          print_r($data);

            $upfiles = array();
            if ($data['TMP_NAME'] && file_exists($_tmp_dir . "/{$data['TMP_NAME']}")) {
              $upfiles = \Frody\Util\MultiFileUpload::swfFormFileInfo($data['TMP_NAME'], $_tmp_dir);
            }

            if ($upfiles) {

              $rout_params = array(

                "ITEM_CD" => $data['ITEM_CD'],        // 품목코드
                "ROUT_SQNO" => $data['ROUT_SEQNO']?:$data['rout_sqno'],    // 라우팅일련번호
                "FILE_NM"   => $upfiles[0]['name'],   // 파일 이름
                "MAKE_USER_ID" => $GLOBALS['_Umem']['member_id'], // 계정 ID
              );

              $rout_sql = "USP_B_ROUTING_FILE_I10";

              $data['FILE_ID'] = $baseObj->DB->result($baseObj->DB->exec($rout_sql, $rout_params),0,0);
//              if ($rst) {
//                \Frody\Util\MultiFileUpload::swfMoveUploadedFile($upfiles[0]['tmp_name'], "{$_rout_dir}/{$data['ITEM_CD']}_{$data['rout_sqno']}.dat");
//              }else{
//
//              }
            }


        }

      }
      if($data['FILE_ID']){
//        print_r($rst);
        $retval['code'] = 1;
        \Frody\Util\MultiFileUpload::swfMoveUploadedFile($upfiles[0]['tmp_name'], "{$_rout_dir}/{$data['ITEM_CD']}_{$data['ROUT_SEQNO']}_{$data['FILE_ID']}.dat");
      }else{
        $retval['code'] = 0;
      }
      break;
    case 'getInspSpecList':
      $sql = "USP_B_INSP_SPEC_S10";
      $params = Array(

        "ITEM_CD"        => $_REQUEST['ITEM_CD'],
        "ROUT_SQNO"     => $_REQUEST['ROUT_SQNO']?:"",
      );
      $rst = $baseObj->DB->exec($sql,$params);

      $rows = array();
      if( $rst ){
        $retval['code'] = true;
        while( $row = $baseObj->DB->fetch_array($rst) ){
          $rows[] = $row;
        };
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      }else{
        $retval['code'] = false;
      }
      break;


    case "putInspSpec":
      if( _notAuth("W") ) break; /************ 권한체크*/
      $_REQUEST['RS_CODE'] = "";
      $_REQUEST['RS_MSG']  = "";

      $_REQUEST['reqdata'] = json_decode($_REQUEST['reqdata']);
      $rst = true;
//      $ok_cd = array();

      foreach( $_REQUEST['reqdata'] as $data ){
        $_REQUEST['RS_CODE'] = "";
        $_REQUEST['RS_MSG']  = "";
        $data = (array)$data;
        $cdata = $data;
        unset($cdata['_STATUS_']);
        unset($cdata['USE_FLG']);
        if( empty(array_filter($cdata)) ){
          if( $data['_STATUS_'] == 'I' ) continue; //빈열제거
          if( $data['_STATUS_'] == 'U' ) $data['_STATUS_'] = 'D'; //빈열제거
        }
        // if(!$data['INSP_ITEM']) { $_REQUEST['RS_MSG'] = cUtil\Mlang::getText("검사항목이 누락된 행은 반영되지 않았습니다."); $rst=false; break; }
        else if(!$data['OP_CD']) { $_REQUEST['RS_MSG'] = cUtil\Mlang::getText("공정이 선택되지 않은 행은 반영되지 않았습니다."); $rst=false; break; }
//        else if(!$data['SPEC_VAL']) { $_REQUEST['RS_MSG'] = cUtil\Mlang::getText("기준 값이 누락된 행은 반영되지 않았습니다."); $rst=false; break;}

        $cdata = $data;
//        unset($cdata['_STATUS_']);
//        unset($cdata['USE_FLG']);

        if( empty(array_filter($cdata)) ){
          if( $data['_STATUS_'] == 'I' ) continue; //빈열제거
          if( $data['_STATUS_'] == 'U' ) $data['_STATUS_'] = 'D'; //빈열제거
        }

//        if(!$data['BIZ_AREA_CD']) { $retval['mesg'] = cUtil\Mlang::getText("사업장코드가 누락된 행은 반영되지 않았습니다."); $rst=false; break; }
//        else if(!$data['BIZ_AREA_NM']) { $retval['mesg'] = cUtil\Mlang::getText("사업장명이 누락된 행은 반영되지 않았습니다."); $rst=false; break; }

//        $chksql = "USP_B_BIZ_AREA_S20"; //CD 중복검사용
//        $chkparams = Array( //CD 중복검사용
//          $data['BIZ_AREA_CD']
//        );

        if( $data['OP_CD'] && $data['_STATUS_'] == 'D' ){
          $sql = "USP_B_INSP_SPEC_D10";
          $params = Array(
            "INSP_ITEM"      => $data['INSP_ITEM'],
            "ROUT_SQNO"      => $data['ROUT_SQNO'],
            "ITEM_CD"        => $data['SPEC_ITEM_CD'],
            "OP_CD"        => $data['OP_CD'],
            "RS_CODE"        => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
            "RS_MSG"         => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
          );
        }else{
          if( $data['OP_CD'] && $data['_STATUS_'] == 'U' ) {
//            $chkparams[] = $data['BIZ_AREA_CD_OLD']; //CD 중복검사용
            $sql = "USP_B_INSP_SPEC_U10";
            $params = Array();
          }else{
            $sql = "USP_B_INSP_SPEC_I10";
            $params = Array();
          }

          $params = array_merge($params, Array(
            "SPEC_ITEM_CD"   => $data['SPEC_ITEM_CD']?:"",
            "ROUT_SQNO"      => $data['ROUT_SQNO']?:"",
            "OP_CD"          => $data['OP_CD'],
            "INSP_ITEM"      => $data['INSP_ITEM'],
            "INSP_NAME"      => $data['INSP_NAME'],
            "INSP_METH"      => $data['INSP_METH'],
            "INSP_TOOL"      => $data['INSP_TOOL'],
            "OKNG_FLG"       => $data['OKNG_FLG'],
            "SPEC_VAL"       => floatval($data['SPEC_VAL'])==""?null:floatval($data['SPEC_VAL']),
            "TOL_LOWER"      => floatval($data['TOL_LOWER'])==""?null:floatval($data['TOL_LOWER']),
            "TOL_UPPER"      => floatval($data['TOL_UPPER'])==""?null:floatval($data['TOL_UPPER']),
            "REMARK"         => $data['REMARK']?:"",
            "MAKE_USER_ID"   => $GLOBALS['_Umem']['member_id'],
            "RS_CODE"        => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
            "RS_MSG"         => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
          ));
        }
        
//        print_r($params);
        $rst =  $baseObj->DB->exec($sql, $params);
//        if( $rst2 ) $ok_cd[] = $data['BIZ_AREA_CD'];
//        $rst = $rst && $rst2;
      }

//      $retval['data'] = $ok_cd;

      // if($rst){
      //   $retval['msg'] = $_REQUEST['RS_MSG'];
      //   $retval['code'] = $_REQUEST['RS_CODE'];
      // }else{
      //   $retval['code'] = "0";
      // }
      if($rst && !$_REQUEST['RS_CODE']){
        $retval['code'] = true;

      }else{
        $retval['code'] = false;
        $retval['mesg']  = $_REQUEST['RS_MSG'];
      }
      break;


    case 'getDrawList':
      $sql = "USP_B_ITEM_DRAW_S10";
      $params = Array(
        "ITEM_CD"     => $_REQUEST['ITEM_CD'],
        "USE_FLAG"     => $_REQUEST['USE_FLAG']??"",
      );
      $rst = $baseObj->DB->exec($sql,$params);

      $rows = array();
      if( $rst ){
        while( $row = $baseObj->DB->fetch_array($rst) ){
          $rows[] = $row;
        };

        $retval['code'] = true;
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      }else{
        $retval['code'] = false;
      }
      break;
    case "putDraw":
      $date = date('Y-m-d');
      $_REQUEST['RS_CODE']="";
      $_REQUEST['RS_MSG']="";
      $_REQUEST['reqdata'] = json_decode($_REQUEST['reqdata'], 1);
      $rst = 1;
      foreach( $_REQUEST['reqdata'] as $data ){
//        if( !$data['DRAW_NO'] ){
//          $rst = false;
//          $retval['code'] = $rst;
//          $retval['mesg'] = cUtil\Mlang::getText("도면번호를 입력하지 않았습니다."); break;
//        }
      }
      $upfiles = array();
      if( $data['TMP_NAME'] && file_exists($_tmp_dir."/{$data['TMP_NAME']}") ){
        $upfiles = \Frody\Util\MultiFileUpload::swfFormFileInfo($data['TMP_NAME'], $_tmp_dir);
//        print_r($upfiles);
      }
      if( $rst ){
        foreach( $_REQUEST['reqdata'] as $data ){
          $_REQUEST['RS_CODE']="";
          $_REQUEST['RS_MSG']="";

          $sid = $data['SID']?:uniqid();

          $params = Array(
            "gubun"       => "draw",
            "nid"         => $data['DRAW_NO'],      // 도면 번호
            "sid"         => $sid,
            "file_name"   => $upfiles[0]['name'],
            "file_size"   => $upfiles[0]['size'],
            "file_type"   => $upfiles[0]['type'],
            "file_width"  => $upfiles[0]['width'],
            "file_height" => $upfiles[0]['height'],
            "file_exp"    => $data['file_exp']?:"",
            "reg_date"   => $data['reg_date']?:$date
          );

          switch( $data['_STATUS_'] ){
            case "I":
              if( $upfiles ){

                $sql = "USP_ZB_ATTACH_I10";

//                $rst = $baseObj->DB->result($baseObj->DB->exec($sql, $params),0,0);
                $data['FILE_ID'] = $baseObj->DB->result($baseObj->DB->exec($sql, $params),0,0);

                if( $data['FILE_ID']){
                  \Frody\Util\MultiFileUpload::swfMoveUploadedFile($upfiles[0]['tmp_name'], "{$_draw_dir}/{$data['DRAW_NO']}_{$sid}_{$data['FILE_ID']}.dat");
                }

                $draw_sql = "USP_B_ITEM_DRAW_I10";

                $draw_params = Array(
                  "ITEM_CD"        => $data['ITEM_CD']?:"",
                  "DRAW_NO"        => $data['FILE_NM']?:"",
                  "DRAW_VER"       => $data['DRAW_VER']?:"",
                  "FILE_ID"        => $data['FILE_ID']?:"",
                  "FILE_NM"        => $data['FILE_NM']?:"",
                  "MAKE_USER_ID"   => $GLOBALS['_Umem']['member_id']?:"",
                  "RS_CODE"        => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
                  "RS_MSG"         => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
                );

                $rst =  $baseObj->DB->exec($draw_sql, $draw_params);

              }
              break;

            case "U":
              $draw_sql = "USP_B_ITEM_DRAW_U10";

              $draw_params = Array(
                "USE_FLAG"        => $data['USE_FLAG']?:"",
                "ITEM_CD"        => $data['ITEM_CD']?:"",
                "DRAW_NO"        => $data['DRAW_NO']?:"",
                "DRAW_VER"       => $data['DRAW_VER']?:"",
                "FILE_ID"        => $data['FILE_ID']?:"",
                "FILE_NM"        => $data['FILE_NM']?:"",
                "MAKE_USER_ID"   => $GLOBALS['_Umem']['member_id']?:"",
                "RS_CODE"        => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
                "RS_MSG"         => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
              );

              $rst =  $baseObj->DB->exec($draw_sql, $draw_params);
              break;

            case "D":
              $draw_sql = "USP_B_ITEM_DRAW_D10";

              $draw_params = Array(
                "ITEM_CD"        => $data['ITEM_CD']?:"",
                "FILE_ID"        => $data['FILE_ID']?:"",
              );
              $rst =  $baseObj->DB->exec($draw_sql, $draw_params);

              if($rst){
                $sql = "USP_ZB_ATTACH_D10";
                $params = Array(
                   "file_id" => $data['FILE_ID']?:"",
                   "gubun"   => "draw",
                   "nid"     => $data['DRAW_NO']?:"",
                   "sid"     => $data['SID']?:"",
                );

                $rst = $rst && $baseObj->DB->exec($sql, $params);

                if( $rst ){
                  @unlink("{$_draw_dir}/{$data['DRAW_NO']}_{$data['SID']}_{$data['FILE_ID']}.dat");
                }
              }else{
              }

              break;
          }
          if($rst){
            $retval['code'] = 1;
          }else{
            $retval['code'] = 0;
          }
        }
      }
      break;

    case "putTempFile":
      if( isset($_FILES["upfile"]) ){
        $fdata['file_name']   = $_FILES['upfile']['name']?:"";
        $fdata['file_size']   = $_FILES['upfile']['size']?:0;
        $fdata['file_type']   = $_FILES['upfile']['type']?:"";
        $fdata['file_width']  = $_FILES['upfile']['width']?:"";
        $fdata['file_height'] = $_FILES['upfile']['height']?:"";

        $fdata['tmp_name'] = \Frody\Util\MultiFileUpload::swfFormSaveFile($_FILES["upfile"], $_tmp_dir, $_REQUEST['temp_fid']);

        $retval['code'] = 1;
        $retval['data'] = $fdata;

        \Frody\Util\MultiFileUpload::swfCleanDir($_tmp_dir);
      }
      break;
    case "putRoutTempFile":
      if( isset($_FILES["upfile"]) ){
//        print_r($_FILES["upfile"]);
        $fdata['file_name']   = $_FILES['upfile']['name']?:"";
        $fdata['file_size']   = $_FILES['upfile']['size']?:0;
        $fdata['file_type']   = $_FILES['upfile']['type']?:"";
        $fdata['file_width']  = $_FILES['upfile']['width']?:"";
        $fdata['file_height'] = $_FILES['upfile']['height']?:"";

        $fdata['tmp_name'] = \Frody\Util\MultiFileUpload::swfFormSaveFile($_FILES["upfile"], $_tmp_dir, $_REQUEST['temp_fid']);

        $retval['code'] = 1;
        $retval['data'] = $fdata;

        \Frody\Util\MultiFileUpload::swfCleanDir($_tmp_dir);
      }else{
        $retval['code'] = 0;
      }
      break;
    case "delRoutFile":
      $rst = true;
      $sql = "USP_B_ROUTING_FILE_D10";
      $params = Array(
        "ITEM_CD" => $_REQUEST['ITEM_CD']?:"",
//        "gubun"   => "draw",
        "ROUT_SQNO"     => $_REQUEST['ROUT_SQNO']?:"",
      );
//      print_r($params);
      $rst = $rst && $baseObj->DB->exec($sql, $params);

      if( $rst ){
        @unlink("{$_rout_dir}/{$_REQUEST['ITEM_CD']}_{$_REQUEST['ROUT_SQNO']}.dat");
        $retval['code'] = 1;
      }
//      $retval['data'] = $fdata;
      break;
    case "uploadData":
      $_REQUEST['RS_MSG']  = "";
      $_REQUEST['RS_CODE'] = "";
      $sql = "USP_B_ITEM_I30";
      $_REQUEST["reqdata"] = json_decode($_REQUEST['reqdata'], true);
//      print_r($_REQUEST["reqdata"]);
      $errorRow = array();
      foreach($_REQUEST["reqdata"] as $data){
        $params = Array(
          "ITEM_CD"      => $data['ITEM_CD']          ?:"",
          "ITEM_NM"      => $data['ITEM_NM']          ?:"",
          "ITEM_GRP"     => $data['ITEM_GRP']         ?:"",
          "OP_SEQ"       => $data['OP_SEQ']           ?:"",
          "OP_CD"        => $data['OP_CD']            ?:"",
          "STD_SETUP_TM" => $data['STD_SETUP_TM']     ?:0,
          "CYCLE_TM"     => $data['CYCLE_TM']         ?:0,
          "STD_OP_AMT"   => $data['STD_OP_AMT']       ?:0,
          "INSP_TOOL"    => $data['INSP_TOOL']        ?:"",
          "INSP_NAME"    => $data['INSP_NAME']        ?:"",
          "INSP_METH"    => $data['INSP_METH']        ?:"",
          "INSP_DESC"    => $data['INSP_DESC']        ?:"",
          "SPEC_VAL"     => $data['SPEC_VAL']         ?:0,
          "OKNG_FLG"     => $data['OKNG_FLG']         ?:"",
          "TOL_UPPER"    => $data['TOL_UPPER']        ?:0,
          "TOL_LOWER"    => $data['TOL_LOWER']        ?:0,
          "MAKE_USER_ID" => $GLOBALS['_Umem']['member_id']?:"",
          "RS_CODE"      => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
          "RS_MSG"       => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
        );
        $rst = $baseObj->DB->exec($sql, $params);
        array_push($errorRow, $params);

      }
//      print_r($errorRow);
      if($_REQUEST['RS_CODE'] !== 'E'){
        $retval['code'] = 1;
      }else{
        $retval['code'] = 0;
        $retval['mesg'] = $errorRow;
      }

      break;

  }
}

if (!$_REQUEST['no_echo']) {
  echo json_encode($retval);
  // echo $retval;
}