<?php
class Common_vars extends BaseCode {
  function __construct(){
    parent::__construct();
  }

  function getBizAreaList(){ //사업장 리스트
    $sql = "USP_B_BIZ_AREA_S10";
    $params = Array(
      "",
      "USE_FLG" => "Y",
    );
    $rst = $this->DB->exec($sql, $params);

    $bzarea_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        $bzarea_rows[$row['BIZ_AREA_CD']] = $row['BIZ_AREA_NM'];
      }
    }
    return $bzarea_rows?:array();
  }

  function getItemList(){ // 품목 리스트
    $sql = "USP_B_ITEM_S10";
    $rst = $this->DB->exec($sql);

    $bzarea_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        $bzarea_rows[$row['ITEM_CD']] = $row['ITEM_NM'];
      }
    }
    return $bzarea_rows?:array();
  }

  function getOperationList(){ //공정리스트 (tree)
    $sql = "USP_B_OPERATION_S10";
    $params = Array(
      "USE_FLG" => "Y",
    );
    $rst = $this->DB->exec($sql, $params);

    $oper_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        $key = array_search($row['OP_GRP'], array_column($oper_rows, "OP_GRP"));

        if( $key === false ){
          $oper_rows[] = Array(
            "OP_GRP"    => $row['OP_GRP'],
            "OP_GRP_NM" => $row['OP_GRP_NM'],
            "OPTIONS"   => Array($row),
          );
        }else{
          $oper_rows[$key]['OPTIONS'][] = $row;
        }
      }
    }
    return $oper_rows?:array();
  }

  function getOperList(){ //공정리스트
    $sql = "USP_B_OPERATION_S10";
    $params = Array(
      "USE_FLG" => "Y",
    );
    $rst = $this->DB->exec($sql, $params);

    $oper_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        $oper_rows[$row['OP_CD']] = $row;
      }
    }
    return $oper_rows?:array();
  }

  function getFacilityList(){ //설비리스트
    $sql = "USP_B_FACILITY_S10";
    $params = Array(
      "USE_FLG" => "Y",
    );
    $rst = $this->DB->exec($sql, $params);

    $fac_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        $fac_rows[] = $row;
      }
    }
    return $fac_rows?:array();
  }

  function getFacilityList2(){ //설비리스트
    $sql = "USP_B_FACILITY_S10";
    $params = Array(
      "USE_FLG" => "Y",
    );
    $rst = $this->DB->exec($sql, $params);

    $fac_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        $fac_rows[$row['FACLT_CD']] = $row['FACLT_NM'];
      }
    }
    return $fac_rows?:array();
  }

  function getEmployeeList($DEPT_CD="", $JOB_POS=""){ //설비리스트
    $sql = "USP_B_EMPLOYEE_S30";
    $params = Array(
      "",
      "",
      "RET_FLG" => "N",
    );
    $rst = $this->DB->exec($sql, $params);

    $emp_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        if(
          (!$DEPT_CD || $DEPT_CD == $row['DEPT_CD']) &&
          (!$JOB_POS || $JOB_POS == $row['JOB_POS'])
        ){
          $emp_rows[] = $row;
        }
      }
    }
    return $emp_rows?:array();
  }

  function getLocList(){ //로케이션리스트 (tree)
    $sql = "USP_B_LOC_S10";
    $params = Array(
      "WH_CD"   => "",
      "USE_FLG" => "Y",
    );
    $rst = $this->DB->exec($sql, $params);

    $lc_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        $key = array_search($row['WH_CD'], array_column($lc_rows, "WH_CD"));

        if( $key === false ){
          $lc_rows[] = Array(
            "WH_CD"    => $row['WH_CD'],
            "WH_NM" => $row['WH_NM'],
            "OPTIONS"   => Array($row),
          );
        }else{
          $lc_rows[$key]['OPTIONS'][] = $row;
        }
      }
    }
    return $lc_rows?:array();
  }

  function getWkStTime(){
    $sql = "select DBO.UFN_GET_BASECODE('WDAY_BTIME','WDAY_BTIME','DAY_ST') as WkStTime";
    $WkStTime = $this->DB->queryFirstField($sql);
    return $WkStTime;
  }

  function getScheduleList($bizarea, $from="", $to=""){ //근무캘린더
    if( !$from ){
      $from = date("Y-m-01");
    }
    if( !$to ){
      $to = date("Y-m-t");
    }
    $sql = "USP_B_CALENDAR_S20";
    $params = Array(
      "BIZ_AREA_CD" => $bizarea,
      "SDATE"       => $from,
      "FDATE"       => $to,
    );
    $rst = $this->DB->exec($sql, $params);

    $cal_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        $cal_rows[] = Array(
          "DT"        => getDT_date($row['DT']),
          "DAY_TYPE"  => $row['DAY_TYPE'],
          "DAY_SHAPE" => $row['DAY_SHAPE'],
          "DAY_NM"    => $row['DAY_NM'],
          "WEEK_DAY"  => $row['WEEK_DAY'],
        );
      }
    }
    return $cal_rows?:array();
  }

  function getWprktimeList($bizarea){ //조업시간
    $sql = "USP_B_WORKTIME_S10";
    $params = Array(
      "BIZ_AREA_CD" => $bizarea,
      "USE_FLG" => "Y",
    );
    $rst = $this->DB->exec($sql, $params);

    $wtime_rows = array();
    if( $rst ){
      $retval['code'] = true;
      while( $row = $this->DB->fetch_array($rst) ){
        $wtime_rows[] = $row;
      }
    }
//    $min_wt = min( array_column( $wtime_rows, 'ST_TM' ) );
//    $max_wt = max( array_column( $wtime_rows, 'ED_TM' ) );
//    if( 0 && $min_wt && $min_wt > '00:00' ){
//      $wtime_rows[] = Array(
//        "ST_TM" => "00:00",
//        "ED_TM" => $min_wt,
//        //"WORK_TM_NM" => "OFF-HOURS",
//        "WORK_TM_NM" => "OFF",
//        "TM_TYPE" => "OFF",
//        "RUNSTOP_FLG" => "N",
//        "USE_FLG" => "Y",
//      );
//    }
//    if( 0 && $max_wt && $max_wt < "24:00" ){
//      $wtime_rows[] = Array(
//        "ST_TM" => $max_wt,
//        "ED_TM" => "24:00",
//        //"WORK_TM_NM" => "OFF-HOURS",
//        "WORK_TM_NM" => "OFF",
//        "TM_TYPE" => "OFF",
//        "RUNSTOP_FLG" => "N",
//        "USE_FLG" => "Y",
//      );
//    }
    return $wtime_rows?:array();
  }

  function getProcConds( $shape_type="" ){
    $procRow = Array(
      "SHARE" => Array(),
      "TURN"  => Array(),
      "HOLE"  => Array(),
      "RATE"  => Array(),
      "WTAMT" => Array(),
    );

    $params = Array();
    if( $shape_type ){
      $params['SHAPE_TYPE'] = $shape_type;
    }

    $sql = "USP_B_PROC_SPARE_S10";
    $rst = $this->DB->exec($sql, $params);
    if( $rst ){
      while( $row = $this->DB->fetch_array($rst) ){
        if( !isset($procRow['SHARE'][$row['SHAPE_TYPE']]) ) $procRow['SHARE'][$row['SHAPE_TYPE']] = Array();
        $procRow['SHARE'][$row['SHAPE_TYPE']][] = $row;
      }
    }

    $sql = "USP_B_PROC_TURN_S10";
    $rst = $this->DB->exec($sql, $params);
    if( $rst ){
      while( $row = $this->DB->fetch_array($rst) ){
        if( !isset($procRow['TURN'][$row['SHAPE_TYPE']]) ) $procRow['TURN'][$row['SHAPE_TYPE']] = Array();
        $procRow['TURN'][$row['SHAPE_TYPE']][] = $row;
      }
    }

    $sql = "USP_B_PROC_HOLE_S10";
    $rst = $this->DB->exec($sql, $params);
    if( $rst ){
      while( $row = $this->DB->fetch_array($rst) ){
        if( !isset($procRow['HOLE'][$row['SHAPE_TYPE']]) ) $procRow['HOLE'][$row['SHAPE_TYPE']] = Array();
        $procRow['HOLE'][$row['SHAPE_TYPE']][] = $row;
      }
    }

    $sql = "USP_B_PROC_RATE_S10";
    $rst = $this->DB->exec($sql, $params);
    if( $rst ){
      while( $row = $this->DB->fetch_array($rst) ){
        if( !isset($procRow['RATE'][$row['SHAPE_TYPE']]) ) $procRow['RATE'][$row['SHAPE_TYPE']] = Array();
        $procRow['RATE'][$row['SHAPE_TYPE']][] = $row;
      }
    }

    $sql = "USP_B_PROC_WTAMT_S10";
    $rst = $this->DB->exec($sql, $params);
    if( $rst ){
      while( $row = $this->DB->fetch_array($rst) ){
        if( !isset($procRow['WTAMT'][$row['SHAPE_TYPE']]) ) $procRow['WTAMT'][$row['SHAPE_TYPE']] = Array();
        $procRow['WTAMT'][$row['SHAPE_TYPE']][] = $row;
      }
    }

    return $procRow;
  }

  static function dateRformat( $number, $conf=Array() ){
    $number = (int)$number;
    $dan = $conf['dan']?:'s';
    $days = 0;
    $hours = 0;
    $minutes = 0;
    $seconds = 0;
    $rem = 0;
    $retstr = '';

    switch( $dan ){
      case "m": //분단위
        $number *= 60;
      break;
      case "h": //시간단위
        $number *= 60*60;
      break;
    }


    $days    = floor( $number / (3600*24) );
    $rem     = $number % (3600*24);
    $hours   = floor($rem / 3600);
    $rem     = $rem % (3600);
    $minutes = floor($rem / 60);
    $seconds = $rem % (60);

    if( $days )    $retstr .= ($retstr?' ':'') . $days    . ($conf['text']['d']?:'일');
    if( $hours )   $retstr .= ($retstr?' ':'') . $hours   . ($conf['text']['h']?:'시간');
    if( $minutes ) $retstr .= ($retstr?' ':'') . $minutes . ($conf['text']['m']?:'분');
    if( $seconds ) $retstr .= ($retstr?' ':'') . $seconds . ($conf['text']['s']?:'초');

    return $retstr;
  }
}