<?php
/*
* @Overview 창고 등록
* @History 2024-03-22 / 최승연 / 최초작성
*/

$cate_id = "BASE_WAREHOUSE"; //현재 메뉴 ID 필수!!!

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

if(!_notAuth("R")) {


  switch($_REQUEST['mode']) {

    // 창고 리스트
    case 'getWarehouseList':
      $squl = "USP_B_WAREHOUSE_S10";
      $params = Array(
        "WH_NM"   => $_REQUEST['WH_NM']?:"",
        "WH_TYPE" => $_REQUEST['WH_TYPE']?:"",
        "USE_FLG" => $_REQUEST['USE_FLG']?:"Y"
      );
      $rst = $baseObj->DB->exec($squl,$params);

      $rows = array();
      if( $rst ){
        $retval['code'] = true;
        while( $row = $baseObj->DB->fetch_array($rst) ){
          $rows[] = $row;
        };
        $retval['data'] = $rows;
      }else{
        $retval['code'] = false;
      }

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

      $_REQUEST["reqdata"] = json_decode( $_REQUEST['reqdata'] );

      foreach( $_REQUEST['reqdata'] as $data ){
        $data =(array)$data;

        $_REQUEST['RS_CODE'] = "";
        $_REQUEST['RS_MSG'] = "";
        $params = Array(
          "WH_CD"			   => $data['WH_CD']?:"",
          "WH_NM"			   => $data['WH_NM']?:"",
          'IN_FLG'			 => $data['IN_FLG']?:"N",
          "OT_FLG"			 => $data['OT_FLG']?:"N",
          "WH_TYPE"		   => $data['WH_TYPE']?:"",
          "BIZ_AREA_CD"	 => $data['BIZ_AREA_CD'],
          "BP_CD"		     => $data['BP_CD'],
          "REMK"			   => $data['REMK'],
          "USE_FLG"		   => $data['USE_FLG']?:"Y",

          "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['_STATUS_']=='D'){
          $sql = "USP_B_WAREHOUSE_D10";
  
          $params = Array(
            "WH_CD"    => $data['WH_CD'],
            "RS_CODE"  => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
            "RS_MSG"   => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
          );

        }else{
          if(strlen($data['WH_CD']) > 10){$_REQUEST['RS_MSG'] = cUtil\Mlang::getText("창고코드는 10자 내로 작성해주세요."); $rst=false; break;}
          if(mb_strlen($data['WH_NM']) > 50){$_REQUEST['RS_MSG'] = cUtil\Mlang::getText("창고명은 50자 내로 작성해주세요."); $rst=false; break;}
          if(strlen($data['REMK']) > 100){$_REQUEST['RS_MSG'] = cUtil\Mlang::getText("비고는 100자 내로 작성해주세요."); $rst=false; break;}

          switch($data['_STATUS_']) {
            case 'I':
              $sql = "USP_B_WAREHOUSE_I10";
            break;
  
            case 'U':
              $sql = "USP_B_WAREHOUSE_U10";
            break;
          };
        }
        
        $rst = $baseObj->DB->exec($sql, $params);
      }
      
      if($rst && !$_REQUEST['RS_CODE']){
        $retval['code'] = true;
      }else{
        $retval['code'] = false;
        $retval['mesg']  = $_REQUEST['RS_MSG'];
      }
    break;


    // 작업장 리스트
    case 'getLocation':
      $squl = "USP_B_LOCATION_S10";
      $params = Array(
        "WH_CD" => $_REQUEST['WH_CD']
      );
      $rst = $baseObj->DB->exec($squl, $params);

      $rows = array();
      if( $rst ){
        $retval['code'] = true;
        while( $row = $baseObj->DB->fetch_array($rst) ){
          $rows[] = $row;
        };
        $retval['data'] = $rows;
      }else{
        $retval['code'] = false;
      }

      break;

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

      $_REQUEST["reqdata"] = json_decode( $_REQUEST['reqdata'] );
      foreach( $_REQUEST['reqdata'] as $data ){
        $data =(array)$data;
        $cdata = $data;

        $_REQUEST['RS_CODE'] = "";
        $_REQUEST['RS_MSG'] = "";
        $params = Array(
          "WH_CD"			    => $_REQUEST['WH_CD'],
          "LOC_CD"			  => $data['LOC_CD']?:"",
          "LOCATION_NM"		=> $data['LOCATION_NM']?:"",
          "LOCATION_TYPE"	=> $data['LOCATION_TYPE']?:"",
          "LOAD_TYPE"		  => $data['LOAD_TYPE']?:"",
          "REMK"			    => $data['REMK']?:"",
          "USE_FLG"		    => $data['USE_FLG']?:"Y",

          "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['_STATUS_']=='D'){
          $sql = "USP_B_LOCATION_D10";
  
          $params = Array(
            "WH_CD"		 => $_REQUEST['WH_CD'],
            "LOC_CD"   => $data['LOC_CD'],
          );

        }else{
          if(strlen($data['LOC_CD']) > 10){$_REQUEST['RS_MSG'] = cUtil\Mlang::getText("로케이션코드는 10자 내로 작성해주세요."); $rst=false; break;}
          if(mb_strlen($data['LOCATION_NM']) > 50){$_REQUEST['RS_MSG'] = cUtil\Mlang::getText("로케이션명은 50자 내로 작성해주세요."); $rst=false; break;}
          if(strlen($data['REMK']) > 50){$_REQUEST['RS_MSG'] = cUtil\Mlang::getText("비고는 50자 내로 작성해주세요."); $rst=false; break;}

          switch($data['_STATUS_']) {
            case 'I':
              $sql = "USP_B_LOCATION_I10";
            break;
  
            case 'U':
              $sql = "USP_B_LOCATION_U10";
            break;
          };
        }
        $rst = $baseObj->DB->exec($sql, $params);
        
      }
      
      
      if($rst && !$_REQUEST['RS_CODE']){
        $retval['code'] = true;
      }else{
        $retval['code'] = false;
        $retval['mesg']  = $_REQUEST['RS_MSG'];
      }
      break;

  }
}

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