<!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.*,java.text.DecimalFormat,com.csc.library.database.*,com.csc.library.timeattendance.*,com.csc.library.databean.*"%>
<%@page import="java.util.*,java.io.*,java.sql.*"%>
<%@ include file="../CHECKPROFILE.jsp" %>
<%
	response.setHeader("Cache-Control","must-revalidate");
	response.setContentType("application/vnd.ms-excel; UTF-8");
	response.setHeader("Content-Disposition", "inline; filename=" + "export.xls");
	CheckNull chk = new CheckNull();
	CscCalendar c = new CscCalendar();
		
	DecimalFormat dmfm1 = new DecimalFormat("#,##0.00");
	DecimalFormat dmfm2 = new DecimalFormat("#,###");

	CscCalendar now = new CscCalendar();
	String cmd = chk.chkNullString(request.getParameter("__cmd"));
	String time0 = chk.chkNullString(request.getParameter("time0"));
	String start_date = chk.chkNullString(request.getParameter("startdate"),now.getDDMMYYYY());
	String end_date = chk.chkNullString(request.getParameter("enddate"),now.getDDMMYYYY());
	CscCalendar start_date2 = new CscCalendar(start_date);
	CscCalendar end_date2 = new CscCalendar(end_date);
	String f_start_date = start_date2.getYYYYMMDD();
	String f_end_date =  end_date2.getYYYYMMDD();
	String bu1id =chk.chkNullString(request.getParameter("__bu1"));
	String bu2id =chk.chkNullString(request.getParameter("__bu2"));
	String bu3id =chk.chkNullString(request.getParameter("__bu3"));
	String bu4id =chk.chkNullString(request.getParameter("__bu4"));
	String bu5id =chk.chkNullString(request.getParameter("__bu5"));
	String shift =chk.chkNullString(request.getParameter("__shift"));
	String type1 =chk.chkNullString(request.getParameter("type1"));
	String type2 =chk.chkNullString(request.getParameter("type2"));
	String type3 =chk.chkNullString(request.getParameter("type3"));
	String employeeid =chk.chkNullString(request.getParameter("__employeeid"));
	String emptype =chk.chkNullString(request.getParameter("__emp_type"));
	String branch =chk.chkNullString(request.getParameter("__branch"));
	String salatype =chk.chkNullString(request.getParameter("__salatype"));
	String filter = "1=1";
	String view = chk.chkNullString(request.getParameter("__view"));
	if( !bu1id.equals("") ){
		 filter += " and me.BU1 = '"+ bu1id+ "'";
	}
	if( !bu2id.equals("") ){
		 filter += " and me.BU2 = '"+ bu2id+ "'";
	}
	if( !bu3id.equals("") ){
		 filter += " and me.BU3 = '"+ bu3id+ "'";
	}
	if( !bu4id.equals("") ){
		 filter += " and me.BU4 = '"+ bu4id+ "'";
	}
	if( !bu5id.equals("") ){
		 filter += " and me.BU5 = '"+ bu5id+ "'";
	}
	if( !shift.equals("") ){
		 filter += " and t1.TIME0ID = '"+ shift+ "'";
	}
	if( !employeeid.equals("") ){
		 filter += " and t1.employeeid = '"+ employeeid+ "'";
	}
	if( !emptype.equals("") ){
		 filter += " and me.emp_type = '"+ emptype+ "'";
	}
	if( !branch.equals("") ){
		 filter += " and t1.branch = '"+ branch+ "'";
	}
	if( !salatype.equals("") ){
		 filter += " and t1.salatype = '"+ salatype+ "'";
	}
	if( type1.equals("1")&&type2.equals("")&&type3.equals("") ){
		 filter += " and t1.EVENTGRP='T'";
	}else if( type1.equals("")&&type2.equals("1")&&type3.equals("") ){
		 filter += " and t1.EVENTGRP='J'";
	}else if( type1.equals("")&&type2.equals("")&&type3.equals("1") ){
		 filter += " and t1.EVENTGRP in ('ANL','LML','MLL','SCL')";
	}else if( type1.equals("1")&&type2.equals("1")&&type3.equals("") ){
		 filter += " and t1.EVENTGRP in ('T','J')";
	}else if( type1.equals("1")&&type2.equals("")&&type3.equals("1") ){
		 filter += " and t1.EVENTGRP in ('T','ANL','LML','MLL','SCL')";
	}else if( type1.equals("")&&type2.equals("1")&&type3.equals("1") ){
		 filter += " and t1.EVENTGRP in ('J','ANL','LML','MLL','SCL')";
	}else if( type1.equals("1")&&type2.equals("1")&&type3.equals("1") ){
		 filter += " and t1.EVENTGRP in ('T','J','ANL','LML','MLL','SCL')";
	}
	filter += " and t1.DATEID between '" + f_start_date + "' and '" +f_end_date+"'";
%>

<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('<%=uprofile.get("lang")%>');</script>
<script language="JavaScript" type="text/JavaScript">

function goHelp(helpName,inputName){
	var helpReturn;
	 if(inputName=="__bu1" ){
		 	
		helpReturn="__bu1:bu1id,__bu1_desc:tdesc";
		
	}else if(inputName=="__bu2" ){
	
        helpReturn="__bu2:bu2id,__bu2_desc:tdesc";
	}else if(inputName=="__bu3" ){
		helpReturn="__bu3:bu3id,__bu3_desc:tdesc";
	}else if(inputName=="__bu4" ){
		helpReturn="__bu4:bu4id,__bu4_desc:tdesc";
	}else if(inputName=="__bu5" ){
		helpReturn="__bu5:bu5id,__bu5_desc:tdesc";
	}else if(inputName=="__employeeid" ){
		helpReturn="__employeeid:employeeid,__employeeid_desc:fullname";
	}else if(inputName=="__shift" ){
		helpReturn="__shift:time0id,__shift_desc:tdesc";
	}
	linkHelp_Return(helpName, helpReturn);
}

function GoExport(){
	with(document.cscform){
	//alert(__filter.value);
	var param = "PSRN001_EXP.jsp?allbranch="+allbranch.value+"&branch="+branch.value+"&startdate="+startdate.value+"&enddate="+enddate.value;
	window.open(param);

	}
}

function GoPrint(){
	with(document.cscform){
	var param = "PSRN001_PDF.jsp?allbranch="+allbranch.value+"&branch="+branch.value+"&startdate="+startdate.value+"&enddate="+enddate.value;
	window.open(param);
		submit();
	}
}
function GoView(){
	with(document.cscform){
		__view.value = "1"
		submit();
	}
}

function checkBox(){
	with(document.cscform){
		if(allbranch.checked==true){
			allbranch.value=" and BRANCH in (select branchid from MBRANCH) ";
			//alert(allbranch.value);
		}else{
			allbranch.value="";
			//alert(allbranch.value);
		}
	}
}

$(function() {
	$('#export').click(function() {
		GoExport();
	});
	
	$('#preview').click(function() {
		 GoView();
	});
	
	$('#print').click(function() {
		GoPrint();
	});
	
	});



</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="<%=uprofile.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="<%=uprofile.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="">
		<input type="hidden" name="__shift" value="<%=chk.chkNullString(request.getParameter("__shift"))%>">
		<input type="hidden" name="__bu1" value="<%=chk.chkNullString(request.getParameter("__bu1"))%>">
		<input type="hidden" name="__bu2" value="<%=chk.chkNullString(request.getParameter("__bu2"))%>">
		<input type="hidden" name="__bu3" value="<%=chk.chkNullString(request.getParameter("__bu3"))%>">
		<input type="hidden" name="__bu4" value="<%=chk.chkNullString(request.getParameter("__bu4"))%>">
		<input type="hidden" name="__bu5" value="<%=chk.chkNullString(request.getParameter("__bu5"))%>">
		<input type="hidden" name="startdate" value="<%=chk.chkNullString(request.getParameter("__startdate"))%>">
		<input type="hidden" name="enddate" value="<%=chk.chkNullString(request.getParameter("enddate"))%>">
		<input type="hidden" name="type1" value="<%=chk.chkNullString(request.getParameter("type1"))%>">
		<input type="hidden" name="type2" value="<%=chk.chkNullString(request.getParameter("type2"))%>">
		<input type="hidden" name="type3" value="<%=chk.chkNullString(request.getParameter("type3"))%>">
		<script language="javascript">getInputFormatDate();</script>

		
		<%
		String temp ="";
		String sql = "select  me.BU3 as bu3id, b3.EDESC as bu3 from TTIME_CURRENT1 t1 left join MEMPLOYEE me on t1.EMPLOYEEID=me.EMPLOYEEID left join MPREFIX mpr on me.EMP_PREFIX=mpr.PREFIXID left join MBU3 b3 on me.BU3 = b3.BU3ID left join MBU4 b4 on me.BU4 = b4.BU4ID left join MEVENTGRP1 mev on t1.EVENTGRP = mev.EVENTGRPID where "+ filter +" group by   me.BU3,b3.EDESC";
		//out.print(filter);
		try{
			DataSet ds = new DataSet(sql,uprofile.get("dbname"),"TTIME_CURRENT1");
			ds.initConnection(uprofile);
			while (ds.next()) {
				String bu3dedc = chk.chkNullString(ds.getString("bu3"));
				String bu3 = ds.getString("bu3id");
		%>
				<br>
				<div align="left"><b>แผนก&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%=bu3dedc%><b></div>
				<table border="1" >
					<thead>
						<tr>
							<th style="vertical-align: middle;"><div style="width: 50px;"></div></th>
							<th style="vertical-align: middle;"><div style="width: 100px;">รหัสพนักงาน</div></th>
							<th style="vertical-align: middle;"><div style="width: 400px;">ชื่อ - นามสกุล</div></th>
							<th style="vertical-align: middle;"><div style="width: 600px;">ชื่อหน่วยงาน</div></th>
							<th style="vertical-align: middle;"><div style="width: 100px;">วันที่</div></th>
							<th style="vertical-align: middle;"><div style="width: 100px;">กะการทำงาน/<div></th>
							<th style="vertical-align: middle;"><div style="width: 100px;">เวลารูดเข้า</div></th>
							<th style="vertical-align: middle;"><div style="width: 100px;">สถานะ</div></th>
							<th style="vertical-align: middle;"><div style="width: 100px;">ลา</div></th>
							<th style="vertical-align: middle;"><div style="width: 200px;">หมายเหตุ</div></th>
						</tr>
					</thead>
					<tbody>
		<%
			int no = 1;
			int countEmp = 0;
			int countWork = 0;
			int countLeave = 0;
			String tempEmp ="";
			String tempWork ="";
			String tempLeave ="";
			String sql2 = "select me.employeeid,mpr.TDESC+me.FNAME+' '+me.LNAME as name,b3.EDESC as bu3,b4.EDESC as bu4,t1.DATEID,t1.TIME0ID,t1.M_TM_BG,t1.M_TM_EN,case t1.LV_TY when 'J' then 'ขาดงาน' when 'T' then  'ทำงาน' else '' end as STATUS,case t1.EVENTGRP when 'J' then '' when 'H' then '' when 'T' then '' else mev.TDESC end as EVENTGRP,t1.REMARK,me.time0 as 'time0' from TTIME_CURRENT1 t1 left join MEMPLOYEE me on t1.EMPLOYEEID=me.EMPLOYEEID left join MPREFIX mpr on me.EMP_PREFIX=mpr.PREFIXID left join MBU3 b3 on me.BU3 = b3.BU3ID left join MBU4 b4 on me.BU4 = b4.BU4ID left join MEVENTGRP1 mev on t1.EVENTGRP = mev.EVENTGRPID where me.bu3='"+bu3+"' and "+ filter +" order by me.bu3,me.bu4,t1.employeeid,t1.dateid,me.time0";
			//out.print(sql2);
				DataSet ds2 = new DataSet(sql2,uprofile.get("dbname"),"Memployee");
				ds2.initConnection(uprofile);
				while (ds2.next()) {
					Shift shift1 = new Shift();
					if(!tempEmp.equals(chk.chkNullString(ds2.getString("name")))){
						tempEmp = chk.chkNullString(ds2.getString("name"));
						countEmp++;
					}
					if(chk.chkNullString(ds2.getString("status")).equals("ทำงาน")&&!tempWork.equals(chk.chkNullString(ds2.getString("name")))){
						tempWork = chk.chkNullString(ds2.getString("name"));
						countWork++;
					}else if(chk.chkNullString(ds2.getString("status")).equals("ขาดงาน")&&!tempLeave.equals(chk.chkNullString(ds2.getString("name")))){
						tempLeave = chk.chkNullString(ds2.getString("name"));
						countLeave++;
					}
					CscCalendar date1 = new CscCalendar(chk.chkNullString(ds2.getString("dateid"))); 
					String date2 = date1.getDD()+"/"+date1.getMM()+"/"+date1.getYear();
					shift1 = getShift(date1,uprofile,chk.chkNullString(ds2.getString("time0")),chk.chkNullString(ds2.getString("employeeid")));
					String getRemark = getChShift(chk.chkNullString(ds2.getString("dateid")),uprofile,chk.chkNullString(ds2.getString("employeeid")));
					String remark = "";
					if(!getRemark.equals("")){
						remark="กะเดิม "+getRemark;
					}
					
		%>
						<tr>
							<td align="center"><%=no%></td>
							<td align="left"><%=chk.chkNullString(ds2.getString("employeeid"))%></td>
							<td align="left"><%=chk.chkNullString(ds2.getString("name"))%></td>
							<td align="center"><%=chk.chkNullString(ds2.getString("bu4"))%></td>
							<td align="center"><%=date2%></td>
							<td align="center"><%=shift1==null?"":shift1.getTimeCode()%></td>
							<td align="center"><%=chk.chkNullString(ds2.getString("m_tm_bg"))%></td>
							<td align="center"><%=chk.chkNullString(ds2.getString("status"))%></td>
							<td align="center"><%=chk.chkNullString(ds2.getString("eventgrp"))%></td>
							<td align="center"><%=remark%></td>
						</tr>
		<%
				no++;
				}
		%>
						<tr>
							<td align="center"></td>
							<td align="center"></td>
							<td align="left">Sub Total</td>
							<td align="left">หนักงานทั้งหมด <%=countEmp%> คน</td>
							<td align="center"></td>
							<td align="left">มาทำงาน <%=countWork%> คน</td>
							<td align="center"></td>
							<td align="center"></td>
							<td align="center" colspan="2">ขาด <%=countLeave%> คน</td>
						</tr>
					</tbody>
				</table>
		<%
				
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		%>
	</form>
</body>

</html>
<%!
private Shift getShift(CscCalendar shdate,UProfile up,String time0id,String empid){
	try {
			DbRecord empRec = null;
			DbInquiry inq_emp = new InitialInquiry(up).getDbInquiry("MEMPLOYEE");
			inq_emp.setColumn("*");
			inq_emp.setFilter("employeeid='"+empid+"'");
			inq_emp.refresh();
			if (inq_emp.recCount() > 0) {
				while(inq_emp.next()){
					empRec = inq_emp.getCurrentDbRecord().cloneRecord(true);
				}
			}
			Mtime0 mt = (Mtime0) new InitialRecord(up).getDbRecord("Mtime0");
			mt.set("time0id", time0id);
			mt.set("companyid", (String) up.get("companyid"));
			mt.setColumn("*");
			mt.setEmpRec(empRec);
			if(mt.search() == 1){
				WorkingShift wst = mt.genWorkingShift(new CscCalendar(shdate.getYYYYMMDD()), new CscCalendar(shdate.getYYYYMMDD()), "true");
				wst.setEmprec(empRec);
				wst.setUProfile(up);
				wst.adjust();   
				wst.createLink();
				
				DayShift ds = (DayShift) wst.get(shdate.getYYYYMMDD());
				Iterator it = ds.keySet().iterator();
				
				if(it.hasNext()){
					Shift sh = (Shift) ds.get((String) it.next());
					if(sh != null){
						return sh ;
					}
				}
				
			}
		}catch(Exception e){
			MyLog.debug("!! Can not find shift on " + shdate);
		}
		return null ;
}
private String getChShift(String date,UProfile up,String empid){
	String shift = "";
	CheckNull chk = new CheckNull();
	CscCalendar date2 = new CscCalendar(date);
	try {
		int count = 0;
		DbInquiry inq = new InitialInquiry(up).getDbInquiry("TCHANGESHIFT_NSTDA");
		inq.setColumn("*");
		inq.setFilter("employeeid='"+empid+"'  ");
		inq.refresh();
		if (inq.recCount() > 0) {
			while(inq.next()){
				CscCalendar st = new CscCalendar(chk.chkNullString(inq.getString("start_date")));
				CscCalendar en = new CscCalendar(chk.chkNullString(inq.getString("end_date")));
				if(date2.afterEqualsDate(st)&&date2.beforeEqualsDate(en)){
					shift = chk.chkNullString(inq.getString("old_shift"));
				}
			}
		}
	}catch(Exception e){}
	return shift ;
}

%>