<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="com.csc.library.entry.*,com.csc.library.system.*,com.csc.library.session.*,com.csc.library.utilities.*,com.csc.library.components.*"%>
<%@page import="java.text.DecimalFormat,com.csc.library.database.*,com.csc.library.timeattendance.*,com.csc.library.databean.*"%>
<%@page import="java.util.*,java.io.*,java.sql.*"%>
<jsp:useBean id="TAR11128" class="com.csc.library.system.Task" scope="page"/>
<%
	TAR11128.setChannel(request, response);
	HelpEntry screen = (HelpEntry) TAR11128.process("HelpEntry", "MEMPLOYEEHELP");

	CheckNull chk = new CheckNull();
	CscCalendar now = new CscCalendar();

	String cmd = chk.chkNullString(request.getParameter("__cmd"));
	String branch = chk.chkNullString(request.getParameter("__branch"));
	String branch_desc = chk.chkNullString(request.getParameter("MBRANCH@TDESC"));
	String workarea = chk.chkNullString(request.getParameter("__workarea"));
	String workarea_desc = chk.chkNullString(request.getParameter("MWORKAREA@TDESC"));
	String status = chk.chkNullString(request.getParameter("__status"));
	String date = chk.chkNullString(request.getParameter("__date"),now.getDDMMYYYY());
	String groupby = chk.chkNullString(request.getParameter("__groupby"),"bu1");
	String view = chk.chkNullString(request.getParameter("__view"));

	if(branch.equals("")){
		branch_desc = "";
	}

	CscCalendar cal_date = new CscCalendar(date);
	String select_date = cal_date.getYYYYMMDD();
	String filter = "1=1";
	String filteremp = " and employeeid in (select employeeid from ttimetemp where swipetype = '0' and swipedate = '"+select_date+"')";

	if( !select_date.equals("") ){
		filter += " and resigndate >= '"+select_date+"'";
	}
	if( !branch.equals("") ){
		filter += " and branch = '"+branch+"'";
	}
	if( !workarea.equals("") ){
		filter += " and workarea = '"+workarea+"'";
	}
	if( !status.equals("") ){
		filter += " and status = '"+status+"'";
	}

	DbInquiry inq = new InitialInquiry(screen.getUProfile()).getDbInquiry("memployment_type");
	inq.setColumn("*");
	inq.setOrderBy("codeid");
	inq.refresh();
	inq.referLangOn();

	int col_num = inq.recCount();
	String coltype = "";
	HashMap<String,String> hashmap_type = new HashMap<String,String>();
	Map<String, String> linkedHashMap_coltype = new LinkedHashMap<String, String>();

	while(inq.next()){
		hashmap_type.put(inq.getString("codeid"), inq.getString("tdesc"));
	}

%>

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../CSS/TISCO.css" rel="stylesheet" type="text/css">
<link href="../CSS/bootstrap.css" rel="stylesheet">
<script src="../JS/jquery-1.11.3.min.js"></script>
<script src="../JS/bootstrap.min.js"></script>
<script language="JavaScript" src="../JS/SCREEN.js"></script>
<SCRIPT TYPE='text/JavaScript' SRC='../JS/DATEINPUT.js'></SCRIPT>
<SCRIPT TYPE='text/JavaScript' SRC='../JS/VIEWCALENDAR.js'></SCRIPT>
<script language="javascript">setLang('<%=screen.getUProfile().get("lang")%>');</script>
<script language="JavaScript" type="text/JavaScript">

function goHelp(helpName,inputName){
	var helpReturn;
	if(inputName=="__branch" ){
		helpReturn="__branch:branchid,MBRANCH@TDESC";
	} else if(inputName=="__workarea" ){
		helpReturn="__workarea:workareaid,MWORKAREA@TDESC";
	} else if(inputName=="__status" ){
		helpReturn="__status:statuscode,MEMPL_STAUS@TDESC";
	}
	linkHelp_Return(helpName, helpReturn);
}

function GoExport(){
	with(document.cscform){
		var col_num = <%=col_num%>;
		var param = "TAR11128_EXP.jsp?__branch="+__branch.value+"&__date="+__date.value+"&__groupby="+__groupby.value;
		for(var i=1; i<=col_num; i++){
			var colname = "__col_"+i;
			param += "&"+colname+"="+$("select[name='"+colname+"']").val();
		}
		window.open(param);
	}
}

function GoView(){
	with(document.cscform){
		__view.value = "1"
		submit();
	}
}

$(function() {
	$('#export').click(function() {
		GoExport();
	});

	$('#preview').click(function() {
		GoView();
	});

	$('#print').click(function() {
		GoPrint();
	});

	$('.column-box option[value=""]').each(function() {
	    var msg = getLang() === "tha"?"ไม่แสดงข้อมูล":"Don't show data";
	    $(this).text(msg);
	});

});



</script>
<style>
	.table-condensed > tbody > tr > td {
		padding: 4px;
		border: 0px solid #ddd;
	}
	img[height="20"] {
		padding: 0px;
	}
	.table-bordered th {
		color: #ffffff;
		text-align: center;
		vertical-align: middle;
		background-color: #336699;
	}
	.eiei {
		overflow: scroll;
		height: 100%;
	}
</style>
</head>
<body leftmargin="0" topmargin="0">
	<form name="cscform" method="post">
		<input type="hidden" name="__language" value="<%=screen.getUProfile().get("lang")%>%>">
		<input type="hidden" name="__fixCon" value="">
		<input type="hidden" name="__cmd" value="">
		<input type="hidden" name="__view" value="">
		<input type="hidden" name="__screen" value="TAR11127">
		<input type="hidden" name="__goPage" value ="">
		<input type="hidden" name="__employeeid" value="">
		<input type="hidden" name="__companyid" value="<%=screen.getUProfile().get("companyid")%>">
		<input type="hidden" name="__orderBy" value="">
		<input type="hidden" name="__empview" value="">
		<input type="hidden" name="__calendar" value="">
		<input type="hidden" name="__filter" value="">
		<script language="javascript">getInputFormatDate();</script>
		<br>
		<table class="table table-condensed maxsize">
			<tbody>
				<tr>
					<td colspan="2" class="header">รายงานตรวจสอบการมาทำงาน&nbsp;&nbsp;</td>
				</tr>
				<tr>
					<td colspan="2">&nbsp;</td>
				</tr>
				<tr style="text-align: center;">
					<td width="40%" style="text-align: right;"><%=screen.getUProfile().get("lang").equalsIgnoreCase("tha")?"วันที่":"Date"%>&nbsp;</td>
					<td style="text-align: left;">
						<input type="text" id="__date" name="__date" value="<%=date%>" size="10" maxlength="10" onkeypress="chkIntegerOfDate(this.value,event);" onkeyup="sysIsEdit();formatDate(this);" onblur="chkInputDate(this);" onfocus="this.select();">
						<a href="javascript:;" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image19','','../IMAGES/BUTTON/SWAP/CALENDAR_SWAP20.gif',1)" onClick="viewCal('__date')"><img src="../IMAGES/BUTTON/MAIN/CALENDAR20.gif" alt="Calendar" name="Image19" width="20" height="20" border="0" style="vertical-align: middle"></a>
					</td>
				</tr>
				<tr style="text-align: center;">
					<td width="40%" style="text-align: right;"><%=screen.getUProfile().get("lang").equalsIgnoreCase("tha")?"บริษัท":"Company"%>&nbsp;</td>
					<td style="text-align: left;">
						<input type="text" id="__branch" name="__branch" value="<%=branch%>" size="10">
						<a href ="javascript:goHelp('MBRANCHHELP','__branch');" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('branch_image','','../IMAGES/BUTTON/SWAP/SEARCH_SWAP20.gif',1)"><img src="../IMAGES/BUTTON/MAIN/SEARCH20.gif" ALT="SEARCH" name="branch_image" width="20" height="20" border="0" style="vertical-align: middle"></a>
						<input type="text" id="__branch_desc" name="MBRANCH@TDESC" size="40" readonly value="<%=branch_desc%>">
					</td>
				</tr>
				<tr style="text-align: center;">
					<td width="40%" style="text-align: right;"><%=screen.getLabel("workarea")%>&nbsp;</td>
					<td style="text-align: left;">
						<input type="text" id="__workarea" name="__workarea" value="<%=workarea%>" size="10">
						<a href ="javascript:goHelp('MWORKAREAHELP','__workarea');" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('workarea_image','','../IMAGES/BUTTON/SWAP/SEARCH_SWAP20.gif',1)"><img src="../IMAGES/BUTTON/MAIN/SEARCH20.gif" ALT="SEARCH" name="workarea_image" width="20" height="20" border="0" style="vertical-align: middle"></a>
						<input type="text" id="__workarea_desc" name="MWORKAREA@TDESC" size="40" readonly value="<%=workarea_desc%>">
					</td>
				</tr>
				<tr style="text-align: center;">
					<td width="40%" style="text-align: right;"><%=screen.getLabel("status")%>&nbsp;</td>
					<td style="text-align: left;">
						<%
						ListBox listboxStatus = new ListBox();
						listboxStatus.setUProfile(screen.getUProfile());
						listboxStatus.setName("status"); //ชื่อ ListBox
						listboxStatus.setTable("MEMPL_STATUSHELP","STATUSCODE","EDESC","","SORT",screen.getUProfile().get("lang")); //tag Systemcode ที่เรียกใช้
						listboxStatus.setChecked(status);
						listboxStatus.setDisable("false");
						listboxStatus.setBlank(true);
						out.println(listboxStatus.process());
						%>
					</td>
				</tr>
				<tr style="text-align: center;">
					<td width="40%" style="text-align: right;"><%=screen.getUProfile().get("lang").equalsIgnoreCase("tha")?"กลุ่ม":"Group"%>&nbsp;</td>
					<td style="text-align: left;">
						<select name="__groupby">
							<option value="bu1" <%if(groupby.equals("bu1")){%>selected<%}%>><%=screen.getLabel("bu1")%></option>
							<option value="bu2" <%if(groupby.equals("bu2")){%>selected<%}%>><%=screen.getLabel("bu2")%></option>
							<option value="bu3" <%if(groupby.equals("bu3")){%>selected<%}%>><%=screen.getLabel("bu3")%></option>
							<option value="bu4" <%if(groupby.equals("bu4")){%>selected<%}%>><%=screen.getLabel("bu4")%></option>
							<option value="bu5" <%if(groupby.equals("bu5")){%>selected<%}%>><%=screen.getLabel("bu5")%></option>
						</select>
					</td>
				</tr>
				<%
				ListBox listbox = new ListBox();
				listbox.setUProfile(screen.getUProfile());
				listbox.setTable("memployment_typehelp","codeid","tdesc","","codeid",screen.getUProfile().get("lang"));
				listbox.setBlank(true);
				listbox.setDisable("false");
				listbox.setCSS("column-box");
				for(int i=1; i<=col_num; i++){
					listbox.setChecked(chk.chkNullString(request.getParameter("__col_"+i)));
					listbox.setName("col_"+i); //ชื่อ ListBox
					if(!chk.chkNullString(request.getParameter("__col_"+i)).equals("")){
						linkedHashMap_coltype.put(String.valueOf(i),chk.chkNullString(request.getParameter("__col_"+i)));
						coltype = chkdata(coltype) + chk.chkNullString(request.getParameter("__col_"+i));
					}
				%>
				<tr style="text-align: center;">
					<td width="40%" style="text-align: right;"><%=screen.getUProfile().get("lang").equalsIgnoreCase("tha")?"คอลัมน์ "+i:"Column "+i%>&nbsp;</td>
					<td style="text-align: left;">
						<%=listbox.process() %>
					</td>
				</tr>
				<%
				}
				int count_column = linkedHashMap_coltype.size();
				%>
				<tr>
					<td colspan="2">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="2" style="text-align: center;">
						<div id="preview" class="btn btn-primary btn-sm" style="padding-top: 2px;">Search</div>&nbsp;&nbsp;
						<div id="export" class="btn btn-primary btn-sm" style="padding-top: 2px;">Export</div>
					</td>
				</tr>

				<tr>
					<td colspan="2">&nbsp;</td></tr>
				<tr>
					<td colspan="2" class="bottom" style="padding-top: 0px; padding-bottom: 0px;">&nbsp;</td>
				</tr>
			</tbody>
		</table>
		<%if (view.equals("1")){ %>
		<img src="../IMAGES/loading2.gif" id="loading" style="margin-left: 40%">
			<div align="left"><b><u>Daily Attendance&nbsp;&nbsp;<%=select_date%></u><b></div>
				<table class="table table-bordered maxsize">
					<thead>
						<tr>
							<th style="vertical-align: middle;" rowspan="2"><%=screen.getLabel(groupby)%></th>
							<th style="vertical-align: middle;" colspan="<%=count_column+1%>"><%=screen.getUProfile().get("lang").equalsIgnoreCase("tha")?"อัตราพนักงานที่มีอยู่จริง":"Working Plan"%></th>
							<th style="vertical-align: middle;" colspan="<%=count_column+1%>"><%=screen.getUProfile().get("lang").equalsIgnoreCase("tha")?"อัตราพนักงานที่มาทำงานจริง":"Actual Working"%></th>
							<th style="vertical-align: middle;" colspan="<%=count_column+1%>"><%=screen.getUProfile().get("lang").equalsIgnoreCase("tha")?"ผลต่าง":"Result Difference"%></th>
						</tr>
						<tr>
							<%
							for(int hcol=1; hcol<=3; hcol++){
								for (String key : linkedHashMap_coltype.keySet()) {
							%>
							<th style="vertical-align: middle;"><%=hashmap_type.get(linkedHashMap_coltype.get(key)) %></th>
							<%
								}
							%>
							<th style="vertical-align: middle;"><%=screen.getUProfile().get("lang").equalsIgnoreCase("tha")?"รวม":"Total"%></th>
							<%
							}
							%>
						</tr>
					</thead>
					<tbody>
					<%
					String tablegroup = "m"+groupby;
					String fieldid = groupby+"id";
					int[] temp_all = new int[count_column+1];
					int[] temp_true = new int[count_column+1];
					int[] temp_dif = new int[count_column+1];
					DbInquiry inqgroup = new InitialInquiry(screen.getUProfile()).getDbInquiry(tablegroup);
					inqgroup.setColumn("*");
					inqgroup.setFilter(fieldid+" in (select "+groupby+" from memployee where "+filter+" and emp_type in ('"+coltype+"'))");
					inqgroup.setOrderBy(fieldid);
					inqgroup.refresh();
					inqgroup.referLangOn();
					while(inqgroup.next()){
					%>
						<tr>
							<td align="left"><%=inqgroup.getString(fieldid)+" : "+inqgroup.getString("tdesc")%></td>
							<%
							int[] emp_all = new int[count_column+1];
							int[] emp_true = new int[count_column+1];

							for(int dcol=1; dcol<=3; dcol++){
								String mainsql = "select count(*) as cou from memployee where "+filter+" and "+groupby+" = '"+inqgroup.getString(fieldid)+"'";
								int total = 0;
								int colcount = 1;

								for (String key : linkedHashMap_coltype.keySet()) {
									String dcol_str = linkedHashMap_coltype.get(key);
									int data = 0;

									if(dcol == 1){
										String round_sql = " and emp_type = '"+dcol_str+"'";
										data = getCountEmp(mainsql+round_sql,screen.getUProfile());
										emp_all[colcount] = data;
										temp_all[colcount] += data;
									} else if(dcol == 2){
										String round_sql = " and emp_type = '"+dcol_str+"'"+filteremp;
										data = getCountEmp(mainsql+round_sql,screen.getUProfile());
										emp_true[colcount] = data;
										temp_true[colcount] += data;
									} else if(dcol == 3){
										data = emp_true[colcount] - emp_all[colcount];
										temp_dif[colcount] += data;
									}

									total += data;
									colcount++;
							%>
							<td align="center"><%=data%></td>
							<%
								}
							%>
							<td align="center"><%=total%></td>
							<%
							}
							%>
						</tr>
					<%
					}
					%>
						<tr>
							<td align="right"><%=screen.getUProfile().get("lang").equalsIgnoreCase("tha")?"รวม":"Total"%></td>
							<%
							for(int bcol=1; bcol<=3; bcol++){

								int total = 0;
								int tcolcount = 1;

								for (String key : linkedHashMap_coltype.keySet()) {

									int data = 0;

									if(bcol == 1){
										data = temp_all[tcolcount];
									} else if(bcol == 2){
										data = temp_true[tcolcount];
									} else if(bcol == 3){
										data = temp_dif[tcolcount];
									}

									total += data;
									tcolcount++;
							%>
							<td align="center"><%=data%></td>
							<%
								}
							%>
							<td align="center"><%=total%></td>
							<%
							}
							%>
						</tr>
					</tbody>
				</table>
			<p>
		<%}%>
		<!----------BEGIN CSC ENDING ZONE ------------------------>
		<!----------END CSC ENDING ZONE -------------------------->
		<script>$('#loading').hide();</script>
	</form>
</body>

</html>
<%!
private int getCountEmp(String sql, UProfile up){
	int count = 0;
	try{
		DataSet ds = new DataSet(sql,up.get("dbname"),"MEMPLOYEE");
		ds.initConnection(up);
		ds.next();
		count = new CheckNull().chkNullStringToInt(ds.getString("cou"));
		ds.closeConnection();
	} catch(Exception e) {
		e.printStackTrace();
	}
	return count;
}
private String chkdata(String str){
	if(!str.equals("")){
		str += "','";
	}
	return str;
}
%>