<?php
/*
 * @Overview 불량실적 등록
 * @History 2024-04-16 / 최승연 / 최초작성
 */

$cate_id = "REG_DEFECT_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 'getDefectList':


      $squl = "USP_P_WO_DEF_RSLT_S10";
      // $squl = "USP_P_DEF_S10";

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

      $params = array(
        "P_START_DATE" => $_REQUEST["date_st"] ?: "",
        "P_END_DATE"   => $_REQUEST["date_ed"] ?: "",
        "P_FACLT_CD"   => $_REQUEST["FACLT_CD"] ?: "",
        // "P_FACLT_NM"   => $_REQUEST["FACLT_NM"] ?: ""
        "RS_CODE"      => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
        "RS_MSG"       => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
      );
      $rst = $baseObj->DB->exec($squl, $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['mesg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
        $retval['mesg'] = $_REQUEST['RS_MSG'];
      }



      break;

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

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

      $rst = true;

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

        $_REQUEST['RS_CODE'] = "";
        $_REQUEST['RS_MSG'] = "";
        $params = array(
          // 아이템 관련 저장 안되고 있음 확인할것
          "DEF_NO"        => $data['DEF_NO']     ?:NULL,
          "WO_NO"        => $data['WO_NO']       ?:"",
          "WO_SEQ"       => $data['WO_SEQ']      ?:0,
          "DEF_OP_CD"    => $data['DEF_OP_CD']   ?: "",
          'DEF_FACLT_CD' => $data['DEF_FACLT_CD']?:"",
          "DEF_DT"       => $data['DEF_DT']      ?:"",
          "DEF_TM"       => $data['DEF_TM']      ?: "",
          "DEF_CD"       => $data['DEF_CD']      ?: "",
          "DEF_QTY"      => $data['DEF_QTY']     ?: 0,
          "DEF_UNIT"     => $data['DEF_UNIT']    ?: "",
          "DEF_CAUSE"    => $data['DEF_CAUSE']   ?: "",
          "DEF_ACTION"   => $data['DEF_ACTION']  ?: "",
          "ACT_EMP_NO"   => $data['ACT_EMP_NO']  ?: "",
          "ACT_DT"       => $data['ACT_DT']  ?: null,
          "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_P_WO_DEF_RSLT_D10";

          $params = array(
            "DEF_NO" => $data['DEF_NO'],

            "RS_CODE"      => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
            "RS_MSG"       => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
          );

        } else {
          switch ($data['_STATUS_']) {
            case 'I':
              $sql = "USP_P_WO_DEF_RSLT_I10";
              break;

            case 'U':
              $sql = "USP_P_WO_DEF_RSLT_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);
  // echo $retval;
}
