<!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 percent = new DecimalFormat("#,##0.00");

	CheckNull chk = new CheckNull();
	CscCalendar st = new CscCalendar();
	CscCalendar en = new CscCalendar();
	String view = chk.chkNullString(request.getParameter("__view"));
	String year = chk.chkNullString(request.getParameter("__year"),String.valueOf(st.getThaiYYYY()));
	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 = "PRI002_EXP.jsp?__year="+__year.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="PRI002.jsp">


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

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

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

<input type="hidden" name="__cmd" value="">
<input type="hidden" name="__screen" value="PRI002">
<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">PRI002 , รายงานอัตรากำลัง</td>
	</tr>
	<tr>
		<td  class="blankspace"></td>
	</tr>
	<tr>
		<td  class="" align="center">กรุณาระบุปี พ.ศ. <input name="__year" class="numeric" onkeypress="checkWordNumber(this.value,event);"  type="text" size="10" maxLength="4" alt="" value="<%=year%>"/></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>
	<%if(view.equals("1") && !year.equals("")){

		LinkedHashMap hm_pos = new LinkedHashMap();
		DbInquiry pos = new InitialInquiry(uprofile).getDbInquiry("Mposition");
		try{
			pos.setColumn("*");
			pos.setFilter("1=1");
			pos.setOrderBy("positionid");
			pos.refresh();
			while(pos.next()){
				hm_pos.put(pos.getString("positionid"),pos.getString("tdesc"));
			}
		}catch(Exception e){}

		LinkedHashMap hm_bu5 = new LinkedHashMap();
		DbInquiry bu5 = new InitialInquiry(uprofile).getDbInquiry("Mbu5");
		try{
			bu5.setColumn("*");
			bu5.setFilter("1=1");
			bu5.setOrderBy("bu5id");
			bu5.refresh();
			while(bu5.next()){
				hm_bu5.put(bu5.getString("bu5id"),bu5.getString("tdesc"));
			}
		}catch(Exception e){}

		Iterator it_pos = null;
		Iterator it_bu5 = null;
		Iterator it_total = null;
		String posid = "";
		String bu5id = "";
		String sum_posid = "";

		int amount = 0;
		int sum_amount = 0;
		int total_pos = 0;
		int sum_total = 0;
	
	%>

	<%
	do{
	%>
	<tr>
		<td  class="blankspace"></td>
	</tr>
	<tr> 
		<td class="" align="center"><H1>อัตรากำลังคนเดือน <%=st.getThaiMonth()%> พ.ศ. <%=st.getThaiYYYY()%><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>

							<%
								it_pos = hm_pos.keySet().iterator();
								while(it_pos.hasNext()){
									posid = (String)it_pos.next();
							%>
								<td nowrap class="" width="" align="center"><b><%=hm_pos.get(posid)%></b></td>
							<%
								}
							%>
								<td nowrap class="" width="" align="center"><b>Grand Total</b></td>
							</tr>
							<%
								it_bu5 = hm_bu5.keySet().iterator();
								while(it_bu5.hasNext()){
									bu5id = (String)it_bu5.next();							
							%>
							<tr>
								<td nowrap class="" width="" align=""><%=hm_bu5.get(bu5id)%></td>
								<%
									it_pos = hm_pos.keySet().iterator();
									total_pos = 0;
									while(it_pos.hasNext()){
										posid = (String)it_pos.next();

										amount = getMempl_movement(bu5id,posid,year,st.getMM(),uprofile);
										total_pos += amount;
								%>
								<td nowrap class="" width="" align="center"><%=(amount!=0)?num.format(amount):""%></td>
								<%}%>
								<td nowrap class="" width="" align="center"><b><%=num.format(total_pos)%></b></td>
							</tr>
							<%
								}
							%>
							<tr>
								<td nowrap class="" width="" align=""><b>Grand Total</b></td>
								<%
									it_total = hm_pos.keySet().iterator();
									sum_total = 0;
									while(it_total.hasNext()){
										sum_posid = (String)it_total.next();
										sum_amount = getSum_Mempl_movement(sum_posid,year,st.getMM(),uprofile);
										sum_total += sum_amount;
								%>
								<td nowrap class="" width="" align="center"><b><%=num.format(sum_amount)%></b></td>
								<%}%>
								<td nowrap class="" width="" align="center"><b><%=num.format(sum_total)%></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>