<%  /**
     * **********************************************************
     * @@ Program Name : total_reports.jsp
     * @@ Description : /집계자/내문서함/진행함/집계현황
     * @@ Author : 강원중
     * @@ Create Date : 2004.10.11
     * @@ History : 
     * @@***********************************************************
     */
%>
<%@ page contentType="application/x-msexcel;charset=UTF-8"
		 import="java.net.URLEncoder"
		 import="java.net.URLDecoder"
		 import="javax.jdo.*"
		 import="kr.co.kihyun.lang.Encoder"
		 import="kr.co.kihyun.lang.MString" import="kr.co.kihyun.lang.MLong"
		 import="kr.co.kihyun.lang.MInteger"
		 import="kr.co.kihyun.text.excel.Excel"
		 import="kr.co.kihyun.text.html.TagFilter"
		 import="kr.co.kihyun.moumi.doc.MDoc"
		 import="kr.co.kihyun.moumi.report.MReport"
		 import="kr.co.kihyun.beans.user.Part"
		 import="kr.co.kihyun.beans.user.PartList"
		 import="kr.co.kihyun.beans.totsys.doc.table.Table"
		 import="kr.co.kihyun.beans.totsys.doc.table.item.Item"
		 import="kr.co.kihyun.beans.totsys.doc.table.item.ItemList"
		 import="kr.co.kihyun.beans.totsys.report.tots.ReportsViewer"
		 import="kr.co.kihyun.beans.totsys.report.tot.TotReportViewer"
		 import="kr.co.kihyun.beans.totsys.report.ReportViewManager"
		 import="kr.co.kihyun.beans.totsys.report.ReportFormElement"
		 import="kr.co.kihyun.beans.totsys.report.ReportsView"
		 import="kr.co.kihyun.moumi.Moumi"
		 import="kr.co.kihyun.beans.entity.*"
		 import="kr.co.kihyun.beans.entity.util.*"
		 import="kr.co.kihyun.beans.totsys.doc.Doc"%>

<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>

<%		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			/****** Parameter ******/
			Long docID = MLong.parseLong(request.getParameter("docID"), null);
			String partID = request.getParameter("partID");
			String docName = clearXSS(URLDecoder.decode(request.getParameter("docName"), "UTF-8"), "");
			int rowDelType = MInteger.parseInt(request.getParameter("rowDelType"), 0);
			String saveExprType = clearXSS(MString.checkNull(request.getParameter("saveExprType")), "");
			String saveType = clearXSS(request.getParameter("saveType"), "");

			response.setHeader("Accept-Ranges", "bytes");
			response.setHeader("Content-Disposition", "attachment; filename=" + (URLEncoder.encode(docName, "UTF-8")).replaceAll(":", "_").replace("+", "%20") + ".xls;");



			/*********** 집계자정보 ***********/
			String deptID = null;
			String userID = null;

			Doc doc = new Doc();

			deptID = doc.getRegisterDept(docID);
			userID = doc.getRegisterID(docID);


			/*********** Part View vlaues ***********/
			int partCount = 0;
			String[] partIDList = null;
			String[] partNameList = null;
			Long[] downDocIDList = null;
			PartList partList = new PartList();
                        //v2. 13.SQL 삽입 : partList.executeQuery()에서 prepare SQL 문으로 되어 있다.
			partList.executeQuery(docID, "sendedReport");
                        //=================
			partCount = partList.getCount();
			partIDList = partList.getIDList();
			partNameList = partList.getNameList();

			if (!MString.isNull(saveType) && saveType.equals("all")) {
				partID = null;
			}

			/********** doc_id 에 대한 part 리스트 추출 ********/
			PartList repoPartList = new PartList();
                        //v2. 13.SQL 삽입 : partList.executeQuery()에서 prepare SQL 문으로 되어 있다.
			partList.executeQuery(docID, "sendedRepoReport");
                        //=================
			partCount = partList.getCount();
			partIDList = partList.getIDList();
			partNameList = partList.getNameList();

			/********** down_doc_id 리스트 추출 **********/
			ReportsView reportsView = new ReportsView();
                        //v2. 13.SQL 삽입 : reportsView.executeQuery()에서 prepare SQL 문으로 되어 있다.
			reportsView.executeQuery(docID, partID);
                        //=================
			downDocIDList = reportsView.getDownDocIDList();

			/********** down_doc_id 리스트들에 대한 part 리스트 추출 ***********/
			int last = 0;
			int repoPartCount = 0;
			String[] repoPartIDList = null;
			//String[] repoPartNameList = null;


			/********** 집계자료보기 ***********/
			TotReportViewer totViewManager = null;
			int partReportCount = 0;
			int partAllReportCount = 0;
			String totReport = "";


			/*********** HTML Form values ***********/
			int tableCount = (new Table()).getCount(docID);
			int[] lineNum = new int[tableCount];
			String[] delSelect = new String[tableCount];
			StringBuffer queryBuff = new StringBuffer();
			int firstTableNum = -1;
			int firstTableTitleCount = -1;
			if (rowDelType == 1) {
				for (int i = 0; i < tableCount; i++) {
					lineNum[i] = MInteger.parseInt(request.getParameter("table" + i + ""), 0);
					queryBuff.append("&table" + i + "=" + lineNum[i]);
					if (firstTableNum == -1) {
						if (lineNum[i] > -1) {
							firstTableNum = i;
						}
					}
				}
			} else {
				firstTableNum = 0;
			}

			TotDoc totDoc = pm.getObjectById(TotDoc.class, docID);

			//각 제출자의 제출문서에 대한 전체 표를 위한 객체
			ReportViewManager reportViewManager = new ReportViewManager(totDoc);
			ReportFormElement reportFormElement = new ReportFormElement(totDoc);
			String Form = reportFormElement.getForm();
			ItemList itemTypes = reportFormElement.getItemList();
			int[] tmpItemTypeList = itemTypes.getTypeList();

			/*********** Doc & Report Values ***********/
			int reportCount = 0;
			String[] repoPartNameList = null;
			String[] repoDeptNameList = null;
			String[] reportList = null;
			ReportsViewer viewManager = new ReportsViewer(totDoc, MReport.EXCEL);

			/********** doc title count **********/
			Item item = new Item();
			int[] titleCount = item.getTitleRowCountList(docID);
			for (int i = 0; i < titleCount.length; i++) {
				if (i == firstTableNum) {
					firstTableTitleCount = titleCount[i];
				}
			}
			//docName = viewManager.getDocName();
%>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
	  xmlns:x="urn:schemas-microsoft-com:office:excel"
	  xmlns="http://www.w3.org/TR/REC-html40">
	<head>
        <meta http-equiv="Content-Type" content="text/html; charset=MS949">
            <meta name="ProgId" content="Excel.Sheet">
                <meta name="Generator" content="Microsoft Excel 10">
    	</head>
	<body style=''>

	<tr>
		<td>
			<table>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td colspan="4"><%=docName%></td>
				</tr>
			</table>
		</td>
	</tr>

	<!---- 집계자 정보 시작---->
	<jsp:include page="/totsys/common/inc/totper/totper_excel.jsp"
				 flush="true">
		<jsp:param name="docID" value="<%= docID%>" />
		<jsp:param name="deptID" value="<%= deptID%>" />
		<jsp:param name="userID" value="<%= userID%>" />
	</jsp:include>

	<!---- 집계자 정보 끝 ---->
	<tr>
		<td>&nbsp;</td>
	</tr>


	<tr>
		<td>
			<table border="1">
				<tr>
					<td align="center" colspan="2">제출기관</td>
					<td align="center">제출부서</td>
				</tr>
			</table>
		</td>
	</tr>

	<table bordercolor="#000000" cellspacing="2" cellpadding="6"
		   width="100%" align="left" border="1">



		<%
			boolean isAllDelete = true;	//모든표 삭제

			if (partID != null) {
				partCount = 1;								//하나의 기관에 대한 보고자료 집계
				partIDList = new String[1];
				partIDList[0] = partID;
				partNameList = new String[1];
				partNameList[0] = (new Part()).getName(partID);
			}

			for (int i = 0; i < downDocIDList.length; i++) {
				//repoPartList.executeQuery(downDocIDList[i], "sendedRepoReport");
				repoPartList.executeQuery(downDocIDList[i], "sendedReport");
				repoPartIDList = repoPartList.getIDList();
				TotDoc downDoc = pm.getObjectById(TotDoc.class, downDocIDList[i]);
				viewManager = new ReportsViewer(downDoc);
				totViewManager = new TotReportViewer(downDoc, MReport.EXCEL);

				totViewManager.setReportData(partID);
				partReportCount = totViewManager.getReportCount();
				partAllReportCount = totViewManager.getAllReportCount();
				totReport = totViewManager.getReport("save", "onlyData");

				for (int p = 0; p < repoPartIDList.length; p++) {
					/********* report *********/
					viewManager.setReportData(repoPartIDList[p]);
					reportCount = viewManager.getReportsView().getCount();
					repoPartNameList = viewManager.getReportsView().getPartNameList();
					repoDeptNameList = viewManager.getReportsView().getDeptNameList();
					reportList = viewManager.getReportNBSPList();
					for (int d = 0; d < reportCount; d++) {
						reportList[d] = reportList[d].replaceAll("<br>", "");
						//title 행 삭제
						if (rowDelType == 1) {
							int delLine = 0;
							for (int t = 1; t <= tableCount; t++) {
								if (lineNum[t - 1] == -1) { //표삭제
									if (t == tableCount) {
										reportList[d] = MReport.getDeleteTableEnd(reportList[d], (t - delLine));
										reportList[d] = MReport.getDeleteTableEndBr(reportList[d]);
									} else {
										reportList[d] = MReport.getDeleteTable(reportList[d], (t - delLine));
									}
									delLine = delLine + 1;
								} else if (lineNum[t - 1] > 0) { //행삭제
									if (d > 0 || p > 0) {
										for (int k = 0; k < lineNum[t - 1]; k++) {
											if (saveExprType.equals("no")) {
												reportList[d] = MReport.getDeleteTitleAll(reportList[d], t - delLine);				//동작 적용
											} else {
												reportList[d] = MReport.getDeleteTitle2All(reportList[d], t - delLine, k);		//이반 적용
											}
										}
									}
									isAllDelete = false;
								} else {
									isAllDelete = false;
								}
							}
						} else {
							isAllDelete = false;
						}

						if (saveExprType.equals("no")) {
							reportList[d] = Excel.removeFmla(reportList[d]);
						}
		%>
		<tr>
			<td valign="top" bgcolor="#E0E0E0" colspan="2"><font
					color="#333333"><%=repoPartNameList[d]%></font></td>
			<td valign="top" bgcolor="#E0E0E0"><font color="#333333"><%=repoDeptNameList[d]%></font></td>
			<td bgcolor="#FAFAFA" width="100%"><%=Excel.setCellTypes(reportList[d], tmpItemTypeList)%></td>
		</tr>
		<%
								}
		%>
		<%
							}
		%>
		<%
			}
		%>
		<%
			for (int i = 0; i < downDocIDList.length; i++) {
				repoPartList.executeQuery(downDocIDList[i], "sendedReport");
				repoPartIDList = repoPartList.getIDList();
				TotDoc downDoc = pm.getObjectById(TotDoc.class, downDocIDList[i]);
				viewManager = new ReportsViewer(downDoc);
				totViewManager = new TotReportViewer(downDoc, MReport.EXCEL);

				totViewManager.setReportData(partID);
				partReportCount = totViewManager.getReportCount();
				partAllReportCount = totViewManager.getAllReportCount();
				totReport = totViewManager.getReport("save", "onlyData").replaceAll("<br>", "");
		%>

		<!--tr width=12>
					<td colspan="2">
						&nbsp;
					</td>
					<td nowrap align="left" valign="top" bgcolor="#E0E0E0">
						<font color="#333333"><b><%// partNameList[i]%></b></font><br>
						<font color="#4F484F">제&nbsp;&nbsp;출:</font><font color="#ff6600"><%// partReportCount%></font><br>
						<font color="#4F484F">전&nbsp;&nbsp;체:</font><font color="#ff6600"><%// partAllReportCount%></font><br>
					</td>
						<td width="100%"-->
		<%
							//title 행 삭제
									/*
							if(rowDelType == 1){
							int delLine = 0;
							for(int t=1; t<=tableCount; t++){
							if(lineNum[t-1] == -1){ //표삭제
							if(t == tableCount){
							totReport = MReport.getDeleteTableEnd(totReport, (t-delLine));
							totReport = MReport.getDeleteTableEndBr(totReport);
							}else{
							totReport = MReport.getDeleteTable(totReport, (t-delLine));
							}
							delLine = delLine + 1;
							}else if(lineNum[t-1] > 0){ //행삭제
							//if(p > 0){
							for(int k=0; k<lineNum[t-1]; k++){
							if(saveExprType.equals("no")){
							totReport = MReport.getDeleteTitleAll(totReport, t-delLine);                            //동작 적용
							}else{
							totReport = MReport.getDeleteTitle2All(totReport, t-delLine, k);                //이반 적용
							}
							}
							//}
							isAllDelete = false;
							}else{
							isAllDelete = false;
							}
							}
							}else{
							isAllDelete = false;
							}

							if(saveExprType.equals("no")) totReport = TagFilter.removeFmla(totReport);
							 */
		%>
		<%
						// totReport
%>
		<!--/td>
                        	</tr-->

		<%
			}
		%>






		<%
			if (partCount > 0) {
				totViewManager = new TotReportViewer(totDoc, MReport.EXCEL);
				totViewManager.setReportData(null);
				reportCount = totViewManager.getReportCount();
				int allReportCount = totViewManager.getAllReportCount();
				totReport = totViewManager.getReport("save", "onlyData").replaceAll("<br>", "");

				if (saveExprType.equals("no")) {
					totReport = Excel.removeFmla(totReport);
				}
		%>
		<tr width=12>
			<td colspan="2">&nbsp;</td>
			<td nowrap align="left" valign="top" bgcolor="#cccccc"><font
					color="#333333"><b>전체</b></font><br>
				<font color="#4F484F">제&nbsp;&nbsp;출:</font><font color="#ff6600"><%= reportCount%></font><br>
				<font color="#4F484F">전&nbsp;&nbsp;체:</font><font color="#ff6600"><%= allReportCount%></font><br>
			</td>
			<td width="100%"><%= Excel.removeFmla(Excel.setCellTypes(totReport, tmpItemTypeList))%>
			</td>
		</tr>
		<% }%>
	</table>
</body>
</html>
<%
		} catch (Exception ex) {
			ex.printStackTrace();
			out.println(kr.co.kihyun.text.html.ServletUtil.getJavaScript("location='/servlet/kr.co.kihyun.beans.user.HttpSSOLogin?mode=logout';"));
		} finally {
			pm.close();
		}
%>
