<?php
$cate_id = "MNG_ORDER_RESULT";

/*
* @Overview 생산 오더 완료승인
* @History 2024-04-18 / 이원기 / 최초작성
* @History 2024-05-02 / 김승혁 / SP 명 변경, 불필요제거
*/
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 'getOrderList':
      //$sql = "USP_P_WO_S30";
      $sql = "USP_P_CLS_WO_S10";

      $_REQUEST['RS_CODE'] = "";
      $_REQUEST['RS_MSG']  = "";
      
      $params = array(
        "order_pop"         => $_REQUEST['order_pop'] ?: "",
        "order_product_pop" => $_REQUEST['order_product_pop'] ?: "",
        "order_bp_pop"      => $_REQUEST['order_bp_pop'] ?: "",
        "date_st"           => $_REQUEST['date_st'] ?: "",
        "date_ed"           => $_REQUEST['date_ed'] ?: "",
        "RS_CODE"           => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
        "RS_MSG"            => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
      );

      $rst = $baseObj->DB->exec($sql, $params);
      $rows = array();
      if ($rst && $_REQUEST['RS_CODE'] !== 'E') {
        $retval['code'] = true;
        while ($row = $baseObj->DB->fetch_array($rst)) {
          $rows[] = $row;
        };
        $retval['data'] = $rows;
        $retval['msg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
        $retval['mesg'] = $_REQUEST['RS_MSG'];
      }
      break;

    case "putOrder":
      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;

          if( $data['WO_NO'] && $data['_STATUS_'] == 'U' ) {
            //$sql = "USP_P_WO_U20";
            $sql = "USP_P_CLS_WO_U10";
            $params = Array();
          }
          $params = array_merge($params, Array(
            "WO_NO"       => $data['WO_NO']?:"",
            "ORD_STATUS"  => $data['ORD_STATUS'] == "Y"?"060":"",
            "EDITOR"      => $GLOBALS['_Umem']['member_id'],
          ));
      }

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


      $retval['data'] = $ok_cd;

      if($_REQUEST['RS_CODE'] == null){
        $retval['code'] = 1;
      }else{
        $retval['code'] = "0";
      }
      break;

    // 공정별 실적 조회
    case "getOpList":
      //$sql = "USP_P_WO_OPER_RSLT_S30";
      $sql = "USP_P_CLS_WO_S20";
      $params = array(
        "WO_NO" => $_REQUEST["WO_NO"] ,
        "ITEM_CD" => $_REQUEST["ITEM_CD"] ?: "",
      );
      $rst = $baseObj->DB->exec($sql, $params);
      $row = 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 'getOrderEndList':
      //$sql = "USP_P_WO_S40";
      $sql  = "USP_P_CLS_WO_S30";
      $params = array(
        "date_st"   => $_REQUEST['date_st'] ?: "",
      );
      $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['msg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
      }
      break;
  }
}

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