<!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.*,java.rmi.RemoteException"%>
<%@page import="java.util.*,java.io.*,java.sql.*"%>
<%@ include file="../CHECKPROFILE.jsp" %>
<script src="//code.jquery.com/jquery-1.11.1.js"></script>
<%

	DecimalFormat num = new DecimalFormat("#,###");
	DecimalFormat dmfm = new DecimalFormat("#,##0.00");

	CheckNull chk = new CheckNull();
	
	String view = chk.chkNullString(request.getParameter("__view"));
	String date1 = chk.chkNullString(request.getParameter("__date1"));
	String date2 = chk.chkNullString(request.getParameter("__date2"));
	CscCalendar st = new CscCalendar(date1);
	CscCalendar en = new CscCalendar(date2);
	//int YY = Integer.parseInt(year)-543;
	//st.setYear(YY);
	//st.setMonth(0);
	st.setDate(1);
	//en.setYear(YY);
	en.setDate(1);
	//if(st.getYear() != new CscCalendar().getYear()){
	//	en.setMonth(11);
	//}
	
	//out.print("ST = "+st.getDDMMYYYY());

%>

<%!

	private int getMempl_movement(String bu5, String emp_position, String year, String month, UProfile uprofile)throws RemoteException, SQLException{
		CheckNull chk = new CheckNull();
		int yyyy = Integer.parseInt(year)-543;
		int num = 0;
		DataSet ds = null;
		
		ds = new DataSet(" select SUM(EVENT)num from MEMPL_MOVEMENT where BU5='"+bu5+"' and EMP_POSITION='"+emp_position+"' and EFF_DATE <='"+yyyy+"-"+month+"-31' " , uprofile.getDbName() , "MEMPL_MOVEMENT"); 
		ds.initConnection(uprofile);
		if(ds.next()){
			num = Integer.parseInt(chk.chkNullString(ds.getString("num"),"0"));
		}
		return num;
	}

	private int getSum_Mempl_movement(String emp_position, String year,String month, UProfile uprofile)throws RemoteException, SQLException{
		CheckNull chk = new CheckNull();
		int yyyy = Integer.parseInt(year)-543;
		int num = 0;
		DataSet ds = null;
		
		ds = new DataSet(" select SUM(EVENT)num from MEMPL_MOVEMENT where BU5 in (select BU5ID from MBU5) and EMP_POSITION='"+emp_position+"' and EFF_DATE <='"+yyyy+"-"+month+"-31' " , uprofile.getDbName() , "MEMPL_MOVEMENT"); 
		ds.initConnection(uprofile);
		if(ds.next()){
			num = Integer.parseInt(chk.chkNullString(ds.getString("num"),"0"));
		}
		return num;
	}
%>

<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"  type="text/css">
<link href="../CSS/bootstrap_csc_backend.css" rel="stylesheet"  type="text/css">
<script type="text/javascript" src="../JS/jquery-2.1.3.min.js"></script>
<script type='text/javascript' src="../JS/bootstrap.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">getTitleName();</script>


<script language="JavaScript" type="text/JavaScript">
<!--

//-->
function back(unit){
	with(window.opener.document.cscform){
	        locateId.value =unit;
	}
	window.close();
}

//-->

function GoExport(){
	with(document.cscform){
	//alert(__filter.value);
	var param = "PRI004_EXP.jsp?__date1="+__date1.value+"&__date1="+__date1.value;
	//alert(param);
	window.open(param);

//		__export.value = "1";
//		action = "PRU212_EXP.jsp";
//		target = "";
//		submit();
	}
}

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

</script>
</head>
<%
String helpReturn="";
if(request.getParameter("__helpReturn")!=null){
	helpReturn=request.getParameter("__helpReturn");
}
String fieldSearch="";
if(request.getParameter("__fieldSearch")!=null){
	fieldSearch=request.getParameter("__fieldSearch");
}
%>
<body leftmargin="0" topmargin="0">
<form name="cscform" method="post" action="PRI004.jsp">


<!----------BEGIN CSC INITIAL ZONE ------------------------>

<%//=screen.InitialVariable()%>

<!----------END CSC INITIAL ZONE -------------------------->

<input type="hidden" name="__cmd" value="">
<input type="hidden" name="__screen" value="PRI004">
<input type="hidden" name="__help" value="">
<input type="hidden" name="__goPage" value="">
<input type="hidden" name="__language" value="<%=uprofile.get("lang")%>">
<input type="hidden" name="__orderBy" value="<%=request.getParameter("__orderBy")%>">
<input type="hidden" name="__helpReturn" value="<%=helpReturn%>">
<input type="hidden" name="__fieldSearch" value="<%=fieldSearch%>"> 
<input type="hidden" name="__helpName" value="<%=request.getParameter("__helpName")%>">
<input type="hidden" name="__pageCall" value="<%=request.getParameter("__screen")%>">
<input type="hidden" name="__fixCon" value="<%=request.getParameter("__fixCon")%>">
<input type="hidden" name="__view" value="<%=request.getParameter("__view")%>">
<input type="hidden" name="__export" value=""> 
<input type="hidden" name="__calendar" value=""> 
<input type="hidden" name="__filter" value="<%//=sql%>"> 
<script language="javascript">getInputFormatDate()</script>

<br>
<div align="center">
<table class="large" cellpadding="0"  cellspacing="0"  >
	<tr> 
		<td   class="header">PRI004 , รายงานเงินได้-ค่าเฉลี่ยต้นทุน</td>
	</tr>
	<tr>
		<td  class="blankspace"></td>
	</tr>
	<tr>
		<td  class="" align="center">
			กรุณาระบุช่วงวันที่
			<input name="__date1" onkeyup="formatDate(this)" onkeypress="chkIntegerOfDate(this.value)" onblur="chkInputDate2(this,__date2)" type="text" size="10" maxLength="8" value="<%=chk.chkNullString(request.getParameter("__date1"))%>"/>
			<a onmouseover="MM_swapImage('calendar1','','../IMAGES/BUTTON/SWAP/CALENDAR_SWAP20.gif',1)" onmouseout="MM_swapImgRestore()" href="javascript:;"><img name="calendar1" align="absmiddle" onclick="viewCal('__date1')" alt="Calendar" src="../IMAGES/BUTTON/MAIN/CALENDAR20.gif" border="0"/></a>
			-
			<input name="__date2" onkeyup="formatDate(this)" onkeypress="chkIntegerOfDate(this.value)" onblur="chkInputDate2(__date1,this)" type="text" size="10" maxLength="8" value="<%=chk.chkNullString(request.getParameter("__date2"))%>"/>
			<a onmouseover="MM_swapImage('calendar2','','../IMAGES/BUTTON/SWAP/CALENDAR_SWAP20.gif',1)" onmouseout="MM_swapImgRestore()" href="javascript:;"><img name="calendar2" align="absmiddle" onclick="viewCal('__date2')" alt="Calendar" src="../IMAGES/BUTTON/MAIN/CALENDAR20.gif" border="0"/></a>
		</td>
	</tr>
	<tr>
		<td  class="blankspace"></td>
	</tr>
	<tr>
		<td>
			<table class="maxsize" align="center" cellpadding="0" cellspacing="0" class="large">

				<tr>
					<td  class="buttonline" colspan="7"><div align="center">
						<button type="button" class="btn btn-primary width100" swlang code="SW013178" onClick="GoExport();">EXPORT</button>&nbsp;
						<button type="button" class="btn btn-primary width100" swlang code="SW013170" onClick="GoView();">VIEW</button>
					</div>
					</td>
				</tr>
			</table>
		</td>
    </tr>
	<%
	DataSet ds = null;
	String sql = "";
	if(view.equals("1")){
	do{
	%>
	<tr>
		<td  class="blankspace"></td>
	</tr>
	<tr> 
		<td class="" align="center"><H1>เงินได้และสวัสดิการประจำฝ่าย เดือน <%=st.getThaiShortMonth()%>&nbsp;<%=st.getThaiYY()%><H1></td>
	</tr>
	<tr> 
		<td>
			 <table height="" align="center" cellpadding="0" cellspacing="0" class="maxsize" border="0" width="100%">	
				<tr bgcolor="#3366CC">
					<td> 
						<table class="maxsize" border="0" cellpadding="1" cellspacing="1" cols=11 bgcolor="#CCCCCC">
							<tr> 
								<td nowrap class="" width="" align="center"><b>หน่วยงาน</b></td>
								<td nowrap class="" width="" align="center"><b>จำนวนพนักงาน</b></td>
								<td nowrap class="" width="" align="center"><b>เงินเดือน / ค่าจ้าง</b></td>
								<td nowrap class="" width="" align="center"><b>ค่าล่วงเวลา</b></td>
								<td nowrap class="" width="" align="center"><b>สวัสดิการต่างๆ<br>ไม่รวมค่ารักษาพยาบาล</b></td>
								<td nowrap class="" width="" align="center"><b>เงินสมทบ P/F</b></td>
								<td nowrap class="" width="" align="center"><font color="red"><b>Man Hour Lost</b></font></td>
								<td nowrap class="" width="" align="center"><b>รวม</b></td>
								<td nowrap class="" width="" align="center"><b>ค่าเฉลี่ยต้นทุนพนักงาน / คน</b></td>
							</tr>
						<%
							sql = "SELECT r.BU5,bu5.TDESC ";
							sql += ",COUNT(r.EMPLOYEEID)AMOUNT ";
							sql += ",r.GENMONTH ";
							sql += ",SUM(r.GROUP_1)GROUP_1 ";
							sql += ",SUM(r.GROUP_6)GROUP_6 ";
							sql += ",SUM(r.GROUP_W)GROUP_W ";
							sql += ",SUM(r.GROUP_L)GROUP_L ";
							sql += "FROM VW_INTELLIGENT_REPORT4 r ";
							sql += "LEFT JOIN MBU5 bu5 on r.BU5=bu5.BU5ID ";
							sql += "WHERE ISNULL(r.BU5,'')!='' ";
							sql += "AND r.GENMONTH = '"+st.getYear()+"-"+st.getMM()+"' ";
							sql += "GROUP BY r.BU5,bu5.TDESC,r.GENMONTH ";
							sql += "HAVING SUM(r.GROUP_1) > 0 OR SUM(r.GROUP_6) > 0 OR SUM(r.GROUP_W) > 0 OR SUM(r.GROUP_L) > 0 ";
							sql += "ORDER BY GENMONTH,BU5 ";
							ds = new DataSet(sql , uprofile.getDbName() , "VW_INTELLIGENT_REPORT4"); 
							ds.initConnection(uprofile);
							int sum_amount = 0;
							Double sum_group_1 = 0.0;
							Double sum_group_6 = 0.0;
							Double sum_group_w = 0.0;
							Double sum_group_l = 0.0;
							Double sum_total = 0.0;
							while(ds.next()){
								int amount = Integer.parseInt(chk.chkNullString(ds.getString("amount"),"0"));
								double  group_1 = Double.parseDouble(chk.chkNullString(ds.getString("group_1"),"0.00"));
								double  group_6 = Double.parseDouble(chk.chkNullString(ds.getString("group_6"),"0.00"));
								double  group_w = Double.parseDouble(chk.chkNullString(ds.getString("group_w"),"0.00"));
								double  group_l = Double.parseDouble(chk.chkNullString(ds.getString("group_l"),"0.00"));
								double  total = group_1+group_6+group_w+group_l;


								sum_amount += amount;
								sum_group_1 += group_1;
								sum_group_6 += group_6;
								sum_group_w += group_w;
								sum_group_l += group_l;
								sum_total += total;
						%>
							<tr> 
								<td nowrap class="" width="" align="center"><%=ds.getString("tdesc")%></td>
								<td nowrap class="" width="" align="center"><%=ds.getString("amount")%></td>
								<td nowrap class="" width="" align="center"><%=dmfm.format(group_1)%></td>
								<td nowrap class="" width="" align="center"><%=dmfm.format(group_6)%></td>
								<td nowrap class="" width="" align="center"><%=dmfm.format(group_w)%></td>
								<td nowrap class="" width="" align="center"><%=dmfm.format(group_l)%></td>
								<td nowrap class="" width="" align="center"></td>
								<td nowrap class="" width="" align="center"><%=dmfm.format(total)%></td>
								<td nowrap class="" width="" align="center"><%=dmfm.format(total / amount)%></td>
							</tr>
						<%
							}
							ds.closeConnection();
						%>
							<tr> 
								<td nowrap class="" width="" align="center"><b>Grand Total</b></td>
								<td nowrap class="" width="" align="center"><b><%=num.format(sum_amount)%></b></td>
								<td nowrap class="" width="" align="center"><b><%=dmfm.format(sum_group_1)%></b></td>
								<td nowrap class="" width="" align="center"><b><%=dmfm.format(sum_group_6)%></b></td>
								<td nowrap class="" width="" align="center"><b><%=dmfm.format(sum_group_w)%></b></td>
								<td nowrap class="" width="" align="center"><b><%=dmfm.format(sum_group_l)%></b></td>
								<td nowrap class="" width="" align="center"><b>&nbsp;</b></td>
								<td nowrap class="" width="" align="center"><b><%=dmfm.format(sum_total)%></b></td>
								<td nowrap class="" width="" align="center"><b><%=sum_amount==0?"0.00":dmfm.format(sum_total / sum_amount)%></b></td>
							</tr>
						</table>
					</td>

				</tr>
			</table>

		</td>
	</tr>
	<%
			st.incMonth(1);
			//out.print("ST : "+st.getDDMMYYYY()+"   EN : "+en.getDDMMYYYY());
		}while(!st.afterDate(en));
	}
	%>
	<tr>
		<td  class="blankspace"></td>
	</tr>

    <tr bordercolor="0069B3"> 
      <td  class="bottom"></td>
    </tr>
</table>
</form>
</body>
</html>