<?php
/*
 * @Overview 생산 실적 등록
 * @History 2024-04-17 / 박민희  
*/
$cate_id = "REG_ORDER_RESULT";
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 "getRegOrder":
      $sql = "USP_P_WO_OPER_RSLT_S10";

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

      $params = array(
        "date_st"      => $_REQUEST["date_st"] ?: "",
        "date_ed"      => $_REQUEST["date_ed"] ?: "",
        "WO_NO"        => $_REQUEST["WO_NO_P"] ?: "",
        "ITEM_CD"      => $_REQUEST["ITEM_CD_P"] ?: "",
        "OP_CD"        => $_REQUEST["OP_CD_P"] ?: "",
        "FACLT_CD "    => $_REQUEST["FACLT_CD_P"] ?: "",
        "RS_CODE"      => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
        "RS_MSG"       => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
      );
      $rst = $baseObj->DB->exec($sql, $params);

      $row = array();


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

    case "putOrder":
      if (_notAuth("W"))
        break; /************ 권한체크*/
      $_REQUEST["reqdata"] = json_decode($_REQUEST['reqdata'], true);
      $rst = true;
      foreach ($_REQUEST['reqdata'] as $data) {
        $_REQUEST['RS_CODE'] = "";
        $_REQUEST['RS_MSG'] = "";
        $params = array(
          "RSLT_NO"          => $data['RSLT_NO']?:"",
          "WO_NO"            => $data['WO_NO']?:"",
          "WO_SEQ"           => $data['WO_SEQ']?:"",
          "OP_CD"            => $data['OP_CD']?:"",
          "FACLT_CD"         => $data['FACLT_CD'] ?: NULL,
          "WRK_QTY"          => $data['WRK_QTY'] ?: 0,
          "PRD_QTY"          => $data['PRD_QTY'] ?: 0,
          "WRK_DT"           => $data['WRK_DT'] ?: '',
          "WRK_TM"           => $data['WRK_TM'] ?: '',
          "WRK_EMP_NO"       => $data['EMP_NO'] ?: '',
          "MAKE_USER_ID"     => $data['MAKE_USER_ID'] ?: '',
          "MAKE_TM"          => $data['MAKE_TM'] ?: '',
          "EDIT_USER_ID"     => $data['EDIT_USER_ID'] ?: '',
          "EDIT_TM"          => $data['EDIT_TM'] ?: '',
          "RSLT_GRP"         => $data['RSLT_GRP'] ?: '',
          "REMARK"           => $data['REMARK'] ?: '',
        );


        switch ($data['_STATUS_']) {
          case 'I':
            $sql = "USP_P_WO_OPER_RSLT_I10";
            break;
          case 'U':
            $sql = "USP_P_WO_OPER_RSLT_U10";
            break;
          case 'D':
            $sql = "USP_P_WO_OPER_RSLT_D10";
            $params = array(
              "RSLT_NO" => $data['RSLT_NO']
            );
            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;
  }
}    


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