<?php /*SVN-1324(5.0.09)*/
include_once("../../includes/var.php");
include_once("../../libs/utils.php");
include_once("../../libs/functions.php");

header("Content-Type: text/html;charset=".CS_ENCODING);

$sid = (isset($_GET["sid"])) ? $_GET["sid"] : "";
$ERI = (isset($_GET["ERI"])) ? $_GET["ERI"] : "";
$act = (isset($_GET["act"])) ? $_GET["act"] : "";
$view = (isset($_GET["view"])) ? $_GET["view"] : "";
$mode = (isset($_GET["mode"])) ? $_GET["mode"] : "";
$fow = (isset($_GET["fow"])) ? $_GET["fow"] : "";
$exec = (isset($_POST["exec"])) ? $_POST["exec"] : "";
$qwe = (isset($_POST["qwe"])) ? $_POST["qwe"] : "";
$sor = (isset($_POST["sor"])) ? $_POST["sor"] : "";

$parary = createsidobject($sid);
if (empty($parary)) {
	$output["result"] = "false";
	$output["message"] = "System session error!";
	echo json_encode(array(0 => $output));
	return;
}
$dbo = $parary["dbo"];$ln = $parary["ln"];$user = $parary["user"];
include_once("../../classes/class.Locale_DB.php");
$lo = new Locale($ln, $dbo, 1, $mode);

$data = array();$output = array();

switch ($act) {
	case "check": // 主副檔-欄位檢查 
		$verlen = (isset($_GET["verlen"])) ? $_GET["verlen"] : 0;
		if ($verlen == mb_strlen($_POST["htmljson"],"utf-8")) {
			$data_ary = json_decode($_POST["htmljson"],true);
			$output["result"] = "true";
			$output["message"] = "";
			$output["idstrs"] = "";
		} else {
			$output["result"] = "false";
			$output["message"] = $lo->text("ERROR_POST_JSON_NOT_LEN");
			echo json_encode(array(0 => $output));
			return;
		}
		$zmode = (isset($_GET["zmode"])) ? $_GET["zmode"] : "";
		$zeri = (isset($_GET["zeri"])) ? $_GET["zeri"] : "";
		if (empty($zmode) || empty($zeri)) {
			$uieri = "";
		} else {
			$dset = get_cw_sdc($sid, $zmode, $zeri, TYPE_FETCH_ARRAY);
			$uieri = (isset($dset[0]["UIERI"])) ? $dset[0]["UIERI"] : "";
		}
		$data_ary["uieri"] = $uieri;
		$output = cs5e_otf_fdcheck_two($dbo, $ln, $lo, $user, $mode, $data_ary, $output);
//------------------BEGIN------------------
		/*if ($mode == "CSORDCORD") {
			// 主檔-檢查單號是否重複
			if (isset($data_ary["CSORDCORD01_CODE"])) {
				$sql = " select (select Count(ERI) ".
					   "		 from CSORDCORD1 ".
					   "		 where ERI <> '".$ERI."' and CODE = '".$data_ary["CSORDCORD01_CODE"]."') AS [COUNT]; ";
				$res = $dbo->execute($sql);
				$error = trim($dbo->getMessage());
				if ($error) {
					$output["result"] = "false";
					$output["message"] .= $error;
				} else {
					$hadCHK = fetch_query_result($res, TYPE_FETCH_ARRAY);
				}
				if (!empty($hadCHK)) {
					if ($hadCHK[0]["COUNT"] > 0) {
						$output["result"] = "false";
						$output["message"] .= $lo->text("CODE_NOT_PLEASE_RESET")."\n";
						if(!empty($output["idstrs"])) $output["idstrs"] .= ",";
						$output["idstrs"] .= "CODE";
					}
				}
			}
		}*/
//------------------END------------------
		echo json_encode(array(0 => $output));
		return;
		break;

	case "mapcheck": //FLOW-欄位檢查 
		$verlen = (isset($_GET["verlen"])) ? $_GET["verlen"] : 0;
		if ($verlen == mb_strlen($_POST["htmljson"],"utf-8")) {
			$data_ary = json_decode($_POST["htmljson"],true);
			$output["result"] = "true";
			$output["message"] = "";
			$output["idstrs"] = "";
		} else {
			$output["result"] = "false";
			$output["message"] = $lo->text("ERROR_POST_JSON_NOT_LEN");
			echo json_encode(array(0 => $output));
			return;
		}
		$zmode = (isset($_GET["zmode"])) ? $_GET["zmode"] : "";
		$zeri = (isset($_GET["zeri"])) ? $_GET["zeri"] : "";
		if (empty($zmode) || empty($zeri)) {
			$uieri = "";
		} else {
			$dset = get_cw_sdc($sid, $zmode, $zeri, TYPE_FETCH_ARRAY);
			$uieri = (isset($dset[0]["UIERI"])) ? $dset[0]["UIERI"] : "";
		}
		$data_ary["uieri"] = $uieri;
		// 0:無,1:同意,2:否決,3:處理中,4:退回
		if (isset($data_ary["HIDEFLOW_ERI"])) {
			if (isset($data_ary["flowmap_BTYPE"])) {
				if ($data_ary["flowmap_BTYPE"] == 1 && $data_ary["flowmap_TYPE"] == 1) {
					$data_ary["feoaf"] = 1;
				} else if ($data_ary["flowmap_BTYPE"] == 1 && $data_ary["flowmap_TYPE"] == 0) {
					$data_ary["feoaf"] = 2;
				} else if ($data_ary["flowmap_BTYPE"] == 2) {
					$data_ary["feoaf"] = 3;
				} else $data_ary["feoaf"] = 0;
			} else if (isset($data_ary["flowmap_CTYPE"])) {
				if ($data_ary["flowmap_CTYPE"] == 1) {
					$data_ary["feoaf"] = 1;
				} else if ($data_ary["flowmap_CTYPE"] == 4) {
					$data_ary["feoaf"] = 3;
				} else if ($data_ary["flowmap_CTYPE"] == 2) {
					$data_ary["feoaf"] = 4;
				} else $data_ary["feoaf"] = 0;
			} else $data_ary["feoaf"] = 0;
		} else $data_ary["feoaf"] = 0;
		$output = cs5e_otf_fdcheck_two($dbo, $ln, $lo, $user, $mode, $data_ary, $output);
//------------------BEGIN------------------
		/*if (isset($data_ary["WBILPGEN02_DERI"])) {
			$D_MSG = array("","");
			if (is_array($data_ary["WBILPGEN02_DERI"])) {
				$dnum = count($data_ary["WBILPGEN02_DERI"]);
				for ($i = 0; $i < $dnum; $i++) {
					$deri = (isset($data_ary["WBILPGEN02_DERI"][$i])) ? $data_ary["WBILPGEN02_DERI"][$i] : "";
					$taxcate = (isset($data_ary["WBILPGEN02_TAXCATE"][$i])) ? $data_ary["WBILPGEN02_TAXCATE"][$i] : 0;
					$invcate = (isset($data_ary["WBILPGEN02_INVCATE"][$i])) ? $data_ary["WBILPGEN02_INVCATE"][$i] : 0;
					$invno = (isset($data_ary["WBILPGEN02_INVNO"][$i])) ? $data_ary["WBILPGEN02_INVNO"][$i] : "";
					$invdate = (isset($data_ary["WBILPGEN02_INVDATE"][$i])) ? $data_ary["WBILPGEN02_INVDATE"][$i] : "";
					if (!empty($invno) && !empty($invdate)) {
						// 檢查發票編號字軌是否合法 / 檢查發票編號是否重覆
						if($taxcate == 0 && $invcate <> 3 && $invcate <> 4) {
							$sql = "select (select COUNT(ERI) ".
								   "        from INVTRACK ".
								   "        where BGNYYMM <= '".substr($invdate, 0, 7)."' ".
								   "          and ENDYYMM >= '".substr($invdate, 0, 7)."' ".
								   "          and INVTRACK = '".substr($invno, 0, 2)."') ".
								   "       as [COUNT1], ".
								   "       (select COUNT(ERI) ".
								   "        from (select ERI, MASTERI, INVNO, 0 as TCHECK, 0 as FLOWTYPE from BILPINV ".
								   "              union ".
								   "              select D.ERI, D.MASTERI, D.INVNO, M.TCHECK, D.FLOWTYPE from WBILEEPAYGEND as D ".
								   "              left join WBILEEPAYGENM as M on M.ERI = D.MASTERI) AS M ".
								   "        where M.MASTERI <> '".$ERI."' and M.INVNO = '".$invno."' ".
								   "          and M.TCHECK <> 5 and isnull(M.FLOWTYPE,0) = 0) ".
								   "       as [COUNT2]; ";
							$res = $dbo->execute($sql);
							$error = trim($dbo->getMessage());
							if ($error) {
								$output["result"] = "false";
								$output["message"] .= $error;
							} else {
								$hadCHK = fetch_query_result($res, TYPE_FETCH_ARRAY);
							}
							if ($hadCHK[0]["COUNT1"] < 1) {
								$D_MSG[0] = $lo->text("INVTRACK_INVNO_ERROR")."\n"; // 發票編號字軌不合法!請重新輸入!
								if(!empty($output["idstrs"])) $output["idstrs"] .= ",";
								$output["idstrs"] .= "INVNO_".$deri;
							}
							if ( $hadCHK[0]["COUNT2"] > 0 ) {
								$D_MSG[1] = $lo->text("BILPINV_INVNO_NOT_REPEAT")."\n"; // 發票編號重覆!請重新輸入!
								if(!empty($output["idstrs"])) $output["idstrs"] .= ",";
								$output["idstrs"] .= "INVNO_".$deri;
							}
						}
					}
				}
			} else {
				$deri = (isset($data_ary["WBILPGEN02_DERI"])) ? $data_ary["WBILPGEN02_DERI"] : "";
				$taxcate = (isset($data_ary["WBILPGEN02_TAXCATE"])) ? $data_ary["WBILPGEN02_TAXCATE"] : 0;
				$invcate = (isset($data_ary["WBILPGEN02_INVCATE"])) ? $data_ary["WBILPGEN02_INVCATE"] : 0;
				$invno = (isset($data_ary["WBILPGEN02_INVNO"])) ? $data_ary["WBILPGEN02_INVNO"] : "";
				$invdate = (isset($data_ary["WBILPGEN02_INVDATE"])) ? $data_ary["WBILPGEN02_INVDATE"] : "";
				if (!empty($invno) && !empty($invdate)) {
					// 檢查發票編號字軌是否合法 / 檢查發票編號是否重覆
					if($taxcate == 0 && $invcate <> 3 && $invcate <> 4) {
						$sql = "select (select COUNT(ERI) ".
							   "        from INVTRACK ".
							   "        where BGNYYMM <= '".substr($invdate, 0, 7)."' ".
							   "          and ENDYYMM >= '".substr($invdate, 0, 7)."' ".
							   "          and INVTRACK = '".substr($invno, 0, 2)."') ".
							   "       as [COUNT1], ".
							   "       (select COUNT(ERI) ".
							   "        from (select ERI, MASTERI, INVNO, 0 as TCHECK, 0 as FLOWTYPE from BILPINV ".
							   "              union ".
							   "              select D.ERI, D.MASTERI, D.INVNO, M.TCHECK, D.FLOWTYPE from WBILEEPAYGEND as D ".
							   "              left join WBILEEPAYGENM as M on M.ERI = D.MASTERI) AS M ".
							   "        where M.MASTERI <> '".$ERI."' and M.INVNO = '".$invno."' ".
							   "          and M.TCHECK <> 5 and isnull(M.FLOWTYPE,0) = 0) ".
							   "       as [COUNT2]; ";
						$res = $dbo->execute($sql);
						$error = trim($dbo->getMessage());
						if ($error) {
							$output["result"] = "false";
							$output["message"] .= $error;
						} else {
							$hadCHK = fetch_query_result($res, TYPE_FETCH_ARRAY);
						}
						if ($hadCHK[0]["COUNT1"] < 1) {
							$D_MSG[0] = $lo->text("INVTRACK_INVNO_ERROR")."\n"; // 發票編號字軌不合法!請重新輸入!
							if(!empty($output["idstrs"])) $output["idstrs"] .= ",";
							$output["idstrs"] .= "INVNO_".$deri;
						}
						if ( $hadCHK[0]["COUNT2"] > 0 ) {
							$D_MSG[1] = $lo->text("BILPINV_INVNO_NOT_REPEAT")."\n"; // 發票編號重覆!請重新輸入!
							if(!empty($output["idstrs"])) $output["idstrs"] .= ",";
							$output["idstrs"] .= "INVNO_".$deri;
						}
					}
				}
			}
			$output["message"] .= implode("", $D_MSG);
			if (!empty($output["message"])) $output["result"] = "false";
		}*/
//------------------END------------------
		echo json_encode(array(0 => $output));
		return;
		break;

	case "uptime":
		if (empty($ERI)) {
			$output["result"] = "false";
			$output["message"] = "No data!";
			echo json_encode(array(0 => $output));
			return;
		}
		if ($user["ERI"] != "ADMIN") {
			$sql = "SET NOCOUNT ON;".
				   "IF EXISTS(SELECT * ".
							 "FROM WEBMSGVIEWM ".
							 "WHERE ERI = '".$ERI."' AND OPENTIME IS NULL) BEGIN ".
						"UPDATE WEBMSGVIEWM ".
						"SET OPENTIME = GETDATE() ".
						"WHERE ERI = '".$ERI."' AND OPENTIME IS NULL;".
						"UPDATE WEBMSGD ".
						"SET OPENTIME = GETDATE() ".
						"WHERE WPERI = '".$user["ERI"]."' AND OPENTIME IS NULL ".
						  "AND ERI IN(SELECT ISNULL(SRCERI,'') ".
									 "FROM WEBMSGVIEWM ".
									 "WHERE ERI = '".$ERI."');".
				   "END;".
				   "SELECT (SELECT COUNT(*) ".
						   "FROM WEBMSGVIEWM ".
						   "WHERE MSGTYPE = 0 AND WPERI = '".$user["ERI"]."' ".
							 "AND BDATE <= CONVERT(NVARCHAR(10), GETDATE(), 111) ".
							 "AND EDATE >= CONVERT(NVARCHAR(10), GETDATE(), 111) ".
							 "AND READTIME IS NULL) AS [COUNT1];".
				   "SET NOCOUNT OFF;";
			$res = $dbo->execute($sql);
			$error = trim($dbo->getMessage());
			if ($error) {
				$output["result"] = "false";
				$output["message"] = $error;
				echo json_encode(array(0 => $output));
				return;
			}
			$check = fetch_query_result($res, TYPE_FETCH_ARRAY);
			if (empty($check[0]["COUNT1"])) {
				$output["loader"] = "true";
			}
		}
		$output["result"] = "true";
		$output["message"] = "";
		echo json_encode(array(0 => $output));
		return;
		break;
	default:
		// Default action, no break;
		return;
		break;
}
?>