<?
	class History {
		var $DB;
		var $_LIST_NUM = 20;


		# 생성자
		function History($db) {
			$this->DB = $db;
		}

		#등록#
		function set_history_insert($argu){


			$data = array(
				"h_year"	=> $argu["h_year"],
				"h_month"	=> $argu["h_month"],
				"h_regdate"	=> _NowTime,
				"h_gubun" => $argu["h_gubun"],
				"division" => $argu["division"]
			);
			
			$sth = $this->DB->autoPrepare(TABLE_HISTORY, array_keys($data), DB_AUTOQUERY_INSERT);
			$res = $this->DB->execute($sth, array_values($data));

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
				exit;
			}
			
			$h_no=mysql_insert_id();
			
			$data=array_merge($data,array("h_no"=>$h_no));

			if(count($argu["h_content"]) > 0){
				for($i=0;$i<count($argu["h_content"]);$i++){
					if($argu["h_content"][$i] != ""){
						$data_list = array(
							"h_no" => $data['h_no'],
							"h_content" => $argu['h_content'][$i],
						);

						$res = $this->DB->autoExecute(TABLE_HISTORY_LIST, $data_list, DB_AUTOQUERY_INSERT);
						if (DB::isError($res)) {
							go_url(""," 오류 입니다.");
							exit;
						}
					}

				}
			}

			return $data;

		}

		#수정#
		function set_history_modify($argu){

			$data = array(
				"h_year"	=> $argu["h_year"],
				"h_month"	=> $argu["h_month"]
			);

			$res = $result = $this->DB->autoExecute(TABLE_HISTORY, $data, DB_AUTOQUERY_UPDATE, " h_no = '{$argu['h_no']}' ");

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
				exit;
			}
			
			if(count($argu["h_content"]) > 0){
				for($i=0;$i<count($argu["h_content"]);$i++){
					if($argu["hl_no"][$i] != ""){
						$data_list = array(
							"h_content"    => $argu['h_content'][$i]
						);

						$res = $this->DB->autoExecute(TABLE_HISTORY_LIST, $data_list, DB_AUTOQUERY_UPDATE, " hl_no = '{$argu['hl_no'][$i]}' ");

						if (DB::isError($res)) {
							go_url("", $res->getMessage());
							exit;
						}
					}
					else{
						$data_list = array(
							"h_no" => $argu['h_no'],
							"h_content" => $argu['h_content'][$i]
						);

						$res = $this->DB->autoExecute(TABLE_HISTORY_LIST, $data_list, DB_AUTOQUERY_INSERT);
						if (DB::isError($res)) {
							debug($res);
							exit;
							go_url(""," 오류 입니다.");
							exit;
						}
					}
				}
			}

			return true;
		}


		#삭제#
		function set_history_delete($h_no){
			$query = "DELETE FROM ".TABLE_HISTORY." WHERE h_no = '".$h_no."'";
			
			$res = $this->DB->query($query);
			if (DB::isError($res)) {
			    go_url("", $res->getMessage());
			    exit;
			}
			
			$query = "DELETE FROM ".TABLE_HISTORY_LIST." WHERE h_no = '".$h_no."'";
			$res = $this->DB->query($query);
			if (DB::isError($res)) {
			    go_url("", $res->getMessage());
			    exit;
			}

			return true;
		}

		function set_history_detail_delete($hl_no){
			$query = "DELETE FROM ".TABLE_HISTORY_LIST." WHERE hl_no = '".$hl_no."'";
			$res = $this->DB->query($query);
			if (DB::isError($res)) {
			    go_url("", $res->getMessage());
			    exit;
			}

			return true;
		}


		#리스트삭제#
		function set_history_delete_list($argu){
			if(count($argu["chk"]) > 0){
				for($i = 0; $i < count($argu["chk"]); $i++){
					$h_no = $argu["chk"][$i];
					$_list = $this -> set_history_delete($h_no);
				}
				return true;
			}
			else{
			    go_url("","삭제할데이터가 없습니다.");
				exit;
			}
		}


		#리스트#
		function get_history_list($argu,&$total,$limit = true){
			$addwhere = " where (1=1) and (h_gubun = '".$argu["h_gubun"]."') and (division = '".$argu["division"]."')";

			/// 검색쿼리
			if($argu['h_year']){	$addwhere .= " and h_year like '{$argu['h_year']}%' ";	}
			if($argu['s_string']){	$addwhere .= " and h_content like '%{$argu['s_string']}%' ";	}

			$query = "
				select
					count(*)
				from
					".TABLE_HISTORY."
				".$addwhere;

			$res = $total = $this->DB->getOne($query);
			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			$query = "
				select
					*,
					date_format(STR_TO_DATE(h_month,'%m'),'%M') as h_month_m
				from
					".TABLE_HISTORY."
				".$addwhere."
				order by
					CONVERT(h_year, UNSIGNED) desc, CONVERT(h_month, UNSIGNED) desc, h_no desc
			";
			if($limit)
			$query .= "
				limit ".($argu['p']-1)*$this->_LIST_NUM.",".$this->_LIST_NUM;

			$res = $list =& $this->DB->getAll($query, array(), DB_FETCHMODE_ASSOC);

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $list;

		}

		function get_histroy_detail($h_no){
			$addwhere = "where h_no = '".$h_no."'";
			$query = "
				select
					*
				from
					".TABLE_HISTORY_LIST."
				".$addwhere."
				order by
					hl_no asc
			";

			$res = $list =& $this->DB->getAll($query, array(), DB_FETCHMODE_ASSOC);

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $list;
		}


		#상세보기
		function get_history_view($h_no){
			$query = "SELECT 
						* 
					  FROM ".TABLE_HISTORY." 
					  where h_no = '".$h_no."'";

			$res = $row = $this->DB->getRow($query,DB_FETCHMODE_ASSOC);
			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $row;
			
		}

		
		#메인리스트#
		function get_main_history($h_no){

			$addwhere = "WHERE (1=1) and (h_gubun = '".$argu["h_gubun"]."')";
			$addwhere .= " AND (h_no = '{$h_no}')";

			$query = "SELECT 
						*
					  FROM ".TABLE_HISTORY_LIST." 
					  ".$addwhere."
					  ORDER BY hl_no asc";

	
			$res = $list =& $this->DB->getAll($query, array(), DB_FETCHMODE_ASSOC);

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $list;
		}

		#메인리스트#
		function get_main_year($argu){
			$addwhere = "WHERE (1=1) and (h_gubun = '".$argu["h_gubun"]."') and (division = '".$argu["division"]."')";

			$query = "SELECT 
						DISTINCT h_year,
						h_no
					  FROM ".TABLE_HISTORY." 
					  ".$addwhere."
					  ORDER BY h_year desc";
			
			$res = $list =& $this->DB->getAll($query, array(), DB_FETCHMODE_ASSOC);

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $list;
		}
		
		function get_main_year1($argu){
			$addwhere = "WHERE (1=1) and (h_gubun = '".$argu["h_gubun"]."') and (division = '".$argu["division"]."')";

			$query = "SELECT 
						*
					  FROM ".TABLE_HISTORY." 
					  ".$addwhere."
					  ORDER BY CONVERT(h_year, UNSIGNED) desc, CONVERT(h_month, UNSIGNED) desc, h_no desc";
			
			$res = $list =& $this->DB->getAll($query, array(), DB_FETCHMODE_ASSOC);

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $list;
		}

		function get_main_month($h_year,$h_gubun){


			$query = "SELECT 
						h_no,
						substr(date_format(STR_TO_DATE(h_month,'%m'),'%M'),1,3) as h_month_txt
					  FROM ".TABLE_HISTORY." 
					  WHERE (h_year = '{$h_year}') and (h_gubun = '".$h_gubun."')
					  ORDER BY CONVERT(h_month, UNSIGNED) desc";
			
			$res = $list =& $this->DB->getAll($query, array(), DB_FETCHMODE_ASSOC);

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $list;
		}

	}
?>