package com.csc.library.databean;

import java.util.Iterator;
import java.util.StringTokenizer;

import com.csc.library.database.DataSet;
import com.csc.library.database.MyHashMap;
import com.csc.library.database.StaticConfig;
import com.csc.library.formula.FormulaString;
import com.csc.library.session.DbInquiry;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialInquiry;
import com.csc.library.utilities.CscCalendar;
import com.csc.library.utilities.MyLog;

/*
 * ���ʴ������š�÷ӧҹ �����š���� �����š�÷ӧҹ 
 * �˹�Ҩ� EMV_VIEW �ͧ NSTDA
 */
public class Simmemployee_view_statisticleave extends Memployee {
	
	private String employeeid = "";
	private double hour_d = 8;
	private MyHashMap hm_Col = new MyHashMap();
	private MyHashMap hm_Month = new MyHashMap();
	private CscCalendar cscLeaveStart = null;
	private CscCalendar cscLeaveEnd = null;	
	private FormulaString fs = new FormulaString();
	private String leave_clear = ""; //�纻������ѹ����ͧ�����������������
	private String leave_not_clear = ""; //�纻������ѹ��������ͧ�����������������
	
	public int assignField(DataSet rs) {
		int temp = super.assignField(rs);		
		if (this.foundData()) {
			if (this.param!=null && this.param.containsKey("__employeeid")) {
				this.employeeid = (String) this.param.get("__employeeid");
			} else this.employeeid = this.getString("employeeid");
			
			CscCalendar cStart = new CscCalendar((String) StaticConfig.getConfigCompany(this.userPro, "ta1001"));
			CscCalendar cEnd = new CscCalendar((String) StaticConfig.getConfigCompany(this.userPro, "ta1002"));			
			//this.cscLeaveStart = new CscCalendar("2007-04-01");
			//this.cscLeaveEnd = new CscCalendar("2007-05-28");
			if (this.param!=null && this.param.containsKey("__year") && !this.param.get("__year").toString().equals("")) {
				int year = Integer.parseInt((String) this.param.get("__year")); 
				this.cscLeaveStart = new CscCalendar(year+"-"+cStart.getMM()+"-"+cStart.getDD());
				if (cStart.getYear()!=cEnd.getYear()) {
					this.cscLeaveEnd = new CscCalendar((year+1)+"-"+cStart.getMM()+"-"+cStart.getDD());
				} else this.cscLeaveEnd = new CscCalendar(year+"-"+cEnd.getMM()+"-"+cEnd.getDD());				
			} else {
				this.cscLeaveStart = cStart;
				this.cscLeaveEnd = cEnd;
			}
			this.hour_d = this.getHour_Day();
			this.initMonth();
			this.selectEvent();
			
			/*String fFilter = "";
			if (!this.leave_not_clear.equals("")) {
				StringTokenizer str = new StringTokenizer(this.leave_not_clear,",");
				while (str.hasMoreTokens()) {
					String s = str.nextToken();
					if (fFilter.equals("")) {
						fFilter = "lv_type='"+s+"'";
					} else fFilter += " or lv_type='"+s+"'";
				}				 
			}*/
			
			this.sumLeave();
			//this.sumLeave("", "1");
			//this.sumLeave(fFilter, "0");
			this.sumWorkPlan("ttime_current1");
			this.sumWorkPlan("ttime_history1");
			this.sumLateAndWork("ttime_current1");
			this.sumLateAndWork("ttime_history1");
			
			MyLog.debug("===>"+this.hm_Col);						
		}		
		return temp;
	}
	
	private double getHour_Day() {
		double value = 8;
		try {			
			DbInquiry inq1 = new InitialInquiry(this.getUProfile()).getDbInquiry();
			inq1.initMyTable("mtime0", "time0id='"+this.getString("time0")+"'", "");
			inq1.setColumn("time0id,companyid,edesc,tdesc,hour_d");
			inq1.refresh();
			if (inq1.next()) {
				value = inq1.getCurrentDbRecord().getDouble("hour_d");  
			}
		} catch (Exception e) {
			MyLog.error(this, e);			
		}
		return value;
	}
	
	private void initMonth() {
		int i=1;
		CscCalendar cBegin = new CscCalendar(this.cscLeaveStart.getYYYYMMDD());
		CscCalendar cEnd = new CscCalendar(this.cscLeaveEnd.getYYYYMMDD());
		while (cBegin.before(cEnd)) {
			this.hm_Month.put(cBegin.getMM(), String.valueOf(i));
			i++;
			cBegin.incMonth(1);
		}
	}
	
	private void selectEvent() {
		try {
			String limit = "";
			FormulaString fs = new FormulaString();
			DbInquiry inq = new InitialInquiry(this.getUProfile()).getDbInquiry();
			inq.setSchemaName(this.getUProfile().getSchemaName());
			inq.initMyTable("meventgrp","daytype!='1' and daytype !='5' and daytype !='6' and daytype !='9' ","");
			inq.setColumn("*");
			inq.referLangOn();
			inq.refresh();			
			while (inq.next()) {
				if (inq.getString("daytype").equalsIgnoreCase("7")) {
					DbInquiry inqEmp = new InitialInquiry(this.getUProfile()).getDbInquiry();
					inqEmp.initMyTable("memployee", "", "");
					inqEmp.setColumn("employeeid,companyid,vacation0,vacation1");
					inqEmp.setFilter("employeeid='" + this.employeeid + "'");
					inqEmp.refresh();
					int i=0;
					if (inqEmp.next()) {		
						try {
							i = fs.DhmToM(inqEmp.getString("vacation0"), this.hour_d) + fs.DhmToM(inqEmp.getString("vacation1"), this.hour_d);
							limit = fs.MTODhm(i, this.hour_d);
						} catch (Exception e) {
							MyLog.error(this, e);
							limit = "00:00:00";
						}
					}
				} else {
					limit = inq.getString("limit")+":00:00";
				}
				
				if (inq.getString("clear_leave").equals("0")) {
					this.leave_not_clear += inq.getString("eventgrpid")+",";
				} else if (inq.getString("clear_leave").equals("1")) {
					this.leave_clear += inq.getString("eventgrpid")+",";
				}
				
				int i=1;
				MyHashMap hmMonth = null;
				if (this.hm_Col.containsKey(String.valueOf(i))) {
					hmMonth = (MyHashMap) this.hm_Col.get(String.valueOf(i));
				} else hmMonth = new MyHashMap();
				CscCalendar cBegin = new CscCalendar(this.cscLeaveStart.getYYYYMMDD());
				CscCalendar cEnd = new CscCalendar(this.cscLeaveEnd.getYYYYMMDD());
				while (cBegin.before(cEnd)) {
					MyHashMap hm = null;
					if (this.hm_Col.containsKey(String.valueOf(i))) {
						hm = (MyHashMap) this.hm_Col.get(String.valueOf(i));
					} else { 
						hm = new MyHashMap();
						hm.put("type", inq.getString("daytype")); 
						hm.put("clear", inq.getString("clear_leave"));
						hm.put("hour_d", this.hour_d); 
						hm.put("late", "00:00:00"); 
						hm.put("absent", "00:00:00"); 
						hm.put("work_plan", "00:00:00");
						hm.put("work_actual", "00:00:00");
						hm.put("month", cEnd.getMM());
						if (this.userPro.get("lang").toString().equalsIgnoreCase("tha")) {
							hm.put("month_name", cBegin.getThaiLongMonth());
						} else {
							hm.put("month_name", cBegin.getEngLongMonth());
						}
					}
					hm.put(inq.getString("eventgrpid"), inq.getString("eventgrpid"));
					hm.put(inq.getString("eventgrpid") + "_desc", inq.getString("edesc")); //���͡����
					hm.put(inq.getString("eventgrpid") + "_limit", limit); //�Է�������	
					hm.put(inq.getString("eventgrpid") + "_used", "00:00:00"); //�ӹǹ������
					hm.put(inq.getString("eventgrpid") + "_remain", limit); //�ӹǹ��������
					
					
					hmMonth.put(inq.getString("eventgrpid"), hm);
					this.hm_Col.put(String.valueOf(i), hm);
					i++;
					cBegin.incMonth(1);
				}
			}
		} catch (Exception e) {
			MyLog.error(this, e);		
		}
	}
	
	/*
	 * �ӡ�����ѹ����ͧ�ӧҹ�������������͹ (���Ἱ)
	 */
	private void sumWorkPlan(String tableName) {
		try {
			int i=1;
			String tmp_csc="";
			String filter = "employeeid='"+this.employeeid+"' and " +
				"(dateid between '"+this.cscLeaveStart.getYYYYMMDD()+"' " +
				"and '"+this.cscLeaveEnd.getYYYYMMDD()+"') " +
				"and (eventgrp<>'O' and eventgrp<>'H' and eventgrp<>'I')";
						
			DbInquiry inq = new InitialInquiry(this.getUProfile()).getDbInquiry();
			inq.initMyTable(tableName, filter, "dateid");
			inq.setColumn("*");
			inq.refresh();
			while (inq.next()) {
				DbRecord rec = inq.getCurrentDbRecord();
				CscCalendar csc = new CscCalendar(rec.getString("dateid"));
				
				if (i==1) {
					tmp_csc = csc.getYYYYMMDD();
				}
				
				if (i>1 && !tmp_csc.equals(csc.getYYYYMMDD())) {
					if (this.hm_Month.containsKey(csc.getMM())) {
						if (rec.getDouble("hour_s")>0) {
							String col = this.hm_Month.getString(csc.getMM());
							MyHashMap hm = (MyHashMap) this.hm_Col.get(col);
							
							String tmp = (String) hm.get("work_plan");
							int minute = fs.DhmToM(tmp, this.hour_d) + fs.Tm_To_Min(rec.getDouble("hour_s"));
							String DHM = fs.MTODhm(minute, this.hour_d);
							hm.put("work_plan", DHM);
						}
					}	
				}
				
				i++;
				tmp_csc = csc.getYYYYMMDD();
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}
	
	/*
	 * �ӡ�����ѹ����ҷӧҹ��� ����ѹ����ҷӧҹ��ԧ
	 */
	private void sumLateAndWork(String tableName) {
		try {
			String filter = "employeeid='"+this.employeeid+"' and " +
				"(dateid between '"+this.cscLeaveStart.getYYYYMMDD()+"' " +
				"and '"+this.cscLeaveEnd.getYYYYMMDD()+"') and (lt>0 or eventgrp='T'or eventgrp='J')";
			
			DbInquiry inq = new InitialInquiry(this.getUProfile()).getDbInquiry();
			inq.initMyTable(tableName, filter, "dateid");
			inq.setColumn("*");
			inq.refresh();
			while (inq.next()) {
				DbRecord rec = inq.getCurrentDbRecord();
				CscCalendar csc = new CscCalendar(rec.getString("dateid"));
				
				if (this.hm_Month.containsKey(csc.getMM())) {
					if (rec.getDouble("lt")>0) {
						String col = this.hm_Month.getString(csc.getMM());
						MyHashMap hm = (MyHashMap) this.hm_Col.get(col);
						
						String tmp = (String) hm.get("late");
						int minute = fs.DhmToM(tmp, this.hour_d) + fs.Tm_To_Min(rec.getDouble("lt"));
						String DHM = fs.MTODhm(minute, this.hour_d);
						hm.put("late", DHM);
					}
					
					if (rec.getString("eventgrp").equals("T") && rec.getDouble("hour_d")>0) {
						String col = this.hm_Month.getString(csc.getMM());
						MyHashMap hm = (MyHashMap) this.hm_Col.get(col);
						
						String tmp = (String) hm.get("work_actual");
						int minute = fs.DhmToM(tmp, this.hour_d) + fs.Tm_To_Min(rec.getDouble("hour_d"));
						String DHM = fs.MTODhm(minute, this.hour_d);
						hm.put("work_actual", DHM);
					}
					
					if (rec.getString("eventgrp").equals("J")) {
						String col = this.hm_Month.getString(csc.getMM());
						MyHashMap hm = (MyHashMap) this.hm_Col.get(col);
						
						String tmp = (String) hm.get("absent");
						int minute = fs.DhmToM(tmp, this.hour_d) + fs.Tm_To_Min(rec.getDouble("m_lv"));
						String DHM = fs.MTODhm(minute, this.hour_d);
						hm.put("absent", DHM);
					}
				}				
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}
	
	/*
	 * �ӡ�����ѹ�����
	 */
	private void sumLeave() {
		try {
			String filter = "employeeid='"+this.employeeid+"' and " +
				"(dateid between '"+this.cscLeaveStart.getYYYYMMDD()+"' " +
				"and '"+this.cscLeaveEnd.getYYYYMMDD()+"')";					
			
			DbInquiry inq = new InitialInquiry(this.getUProfile()).getDbInquiry();
			inq.initMyTable("tleave_summary", filter, "dateid");
			inq.setColumn("*");
			inq.refresh();			
			while (inq.next()) {
				DbRecord rec = inq.getCurrentDbRecord();
				CscCalendar csc = new CscCalendar(rec.getString("dateid"));
				
				if (this.hm_Month.containsKey(csc.getMM())) {
					String lv_type = rec.getString("lv_type");
					String col = this.hm_Month.getString(csc.getMM());
					MyHashMap hm = (MyHashMap) this.hm_Col.get(col);
					
					String tmp = (String) hm.get(lv_type+"_used");
					int minute = fs.DhmToM(tmp, this.hour_d) + fs.Tm_To_Min(rec.getDouble("m_lv"));
					String DHM = fs.MTODhm(minute, this.hour_d);
					hm.put(lv_type + "_used", DHM);
					hm.put(lv_type + "_remain", this.getRemainTime(lv_type, hm.getString(lv_type+"_limit"), hm.getString(lv_type+"_used")));										
					this.hm_Col.put(col, hm);
				}
			}
		} catch (Exception e) {
			MyLog.error(this, e);			
		}
	}
	
	private void sumLeave(String fFilter, String clear) {
		try {
			String filter = "employeeid='"+this.employeeid+"' and " +
				"(dateid between '"+this.cscLeaveStart.getYYYYMMDD()+"' " +
				"and '"+this.cscLeaveEnd.getYYYYMMDD()+"')";					
			
			if (!fFilter.equals("")) {
				filter = "employeeid='"+this.employeeid+"' and ("+fFilter+")";
			}
			
			DbInquiry inq = new InitialInquiry(this.getUProfile()).getDbInquiry();
			inq.initMyTable("tleave_summary", filter, "dateid");
			inq.setColumn("*");
			inq.refresh();
			while (inq.next()) {
				DbRecord rec = inq.getCurrentDbRecord();
				CscCalendar csc = new CscCalendar(rec.getString("dateid"));
				
				if (this.hm_Month.containsKey(csc.getMM())) {
					String lv_type = rec.getString("lv_type");
					String col = this.hm_Month.getString(csc.getMM());
					MyHashMap hm = (MyHashMap) this.hm_Col.get(col);
					
					if (hm.getString("clear").equals(clear)) {
						String tmp = (String) hm.get(lv_type+"_used");
						int minute = fs.DhmToM(tmp, this.hour_d) + fs.Tm_To_Min(rec.getDouble("m_lv"));
						String DHM = fs.MTODhm(minute, this.hour_d);
						hm.put(lv_type + "_used", DHM);
						hm.put(lv_type + "_remain", this.getRemainTime(lv_type, hm.getString(lv_type+"_limit"), hm.getString(lv_type+"_used")));										
						this.hm_Col.put(col, hm);
					}
				}
			}
		} catch (Exception e) {
			MyLog.error(this, e);			
		}
	}
	
	private String getRemainTime(String type, String limit, String used) {
		FormulaString fs = new FormulaString();
		int result = fs.DhmToM(limit, this.hour_d) - fs.DhmToM(used, this.hour_d);
		String value = fs.MTODhm(result, this.hour_d);
		return value;
	}
	
	public String getString(String fieldName) {
		if (fieldName.indexOf("$") > -1) {
			return this.getStringChild(fieldName);
		} else {
			return super.getString(fieldName);
		}
	}

	private String getStringChild(String fieldName) {
		if (fieldName.indexOf("$") > -1) {
			String tName = fieldName.substring(0, fieldName.indexOf("$"));
			String fName = fieldName.substring(fieldName.indexOf("$") + 1, fieldName.length());
			if (this.hm_Col.get(tName) != null) {
				MyHashMap hm = (MyHashMap) this.hm_Col.get(tName); 
				return String.valueOf(hm.get(fName));				
			}
		}
		return super.getString(fieldName);
	}
}