/*
 * Created on 11 ��.�. 2548
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.csc.library.process;

import com.csc.library.utilities.MyLog;
import java.util.StringTokenizer;
import java.util.Vector;

import com.csc.library.session.DbInquiry;
import com.csc.library.session.DbRecord;
import com.csc.library.session.DbTable;
import com.csc.library.session.InitialInquiry;
import com.csc.library.session.InitialRecord;
import com.csc.library.session.InitialTable;
import com.csc.library.utilities.CscCalendar;

/**
 * @author SassyBoy100
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/*
 * class ���������Ѻź�����š�÷ӧҹ�ͧ��ѡ�ҹ
 * TTIME_CURRENT ��� TTIME_CURRENT1
 */
public class ClearTimeWorking extends ProcessTimeBase {
	
	private DbRecord recTime = null;
	private DbInquiry inqTime = null;
	private DbRecord recTime1 = null;
	private DbInquiry inqTime1 = null;
	private DbRecord recTleave = null;
			
	public Vector getProcessList() {
		Vector v = new Vector();
		try {
			this.setParamData();
			if (!this.getParamValue("filterProcess").equals("")) {
				DbInquiry inq = new InitialInquiry(this.upf).getDbInquiry();
				inq.setParam(this.data);
				inq.setSchemaName(this.upf.getSchemaName());
				inq.initMyTable("Memployee", this.getParamValue("filterProcess"), "employeeid");			
				inq.setColumn("employeeid, runno, emp_position, emp_group, branch, job, bossid, emp_level," +
					"bu1, bu2, bu3, bu4, bu5, costcenter, salatype, currency, salary, emp_every, time0, companyid," + 
					"status, startdate, resigndate, workarea");	
				inq.setFatchAll(true);
				inq.refresh();
				while (inq.next()) {
					v.addElement(inq.getCurrentDbRecord().getString("employeeid"));
				}
			}
		} catch (Exception e) {
			MyLog.error(this, e);			
		}
		return v;
	}
	
	/* Override */
	public void setParamData() {		
		this.hmParam.put("employeeProcess", this.getParamData("employeeProcess"));
		this.hmParam.put("startDate", this.getParamData("startDate"));
		this.hmParam.put("endDate", this.getParamData("endDate"));
		this.hmParam.put("timein", this.getParamData("timein"));
		this.hmParam.put("timeout", this.getParamData("timeout"));
		this.hmParam.put("cmdValue", this.getParamData("cmdValue"));
		
		String startDate = this.getParamValue("startDate");
		if(startDate.equals("")){
			//�ͧ��� 
			this.hmParam.put("filterProcess", this.getProcessCondition());
		}else{
			String whereClause = this.getProcessCondition();
			String processDate = new CscCalendar(startDate).getYYYYMMDD();
	 		whereClause += " AND employeeid in (select emp.employeeid from memployee emp where (emp.status in('A','B','S','V','W','Y') or emp.endworkdate >= '"+processDate+"'))";
			this.hmParam.put("filterProcess", whereClause);
		}//end if 
		//=======================Nuiss Code ==========================
		
		this.hmParam.put("startTime", this.getParamData("startTime"));
		this.hmParam.put("endTime", this.getParamData("endTime"));
	}
	
	private String getCondition(){ 
		String filter="";
		try {
			if (!this.getParamValue("startDate").equals("") && !this.getParamValue("endDate").equals("")) {
				CscCalendar cs1 = new CscCalendar(this.getParamValue("startDate"));				
				CscCalendar cs2 = new CscCalendar(this.getParamValue("endDate"));
				if (filter.length()>0) {
					filter = filter + " and ";
				} 
				filter = filter + " (dateid between '"+cs1.getYYYYMMDD()+"' and '"+cs2.getYYYYMMDD()+"')"; 
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}
		return filter;
	}
	
	public void process() throws Exception {		
		this.setParamData();
		if (this.getParamValue("cmdValue").toString().equalsIgnoreCase("clear")) {	
			this.clearTimeWorking();
		}		
	}	
	
	private void clearTimeWorking(){
		try {
			Vector vt = this.processList;
			String filter = this.getCondition();
			for (int i=0;i<vt.size() && !this.isCancle();i++) {
				String id = (String) vt.elementAt(i);
				//Delete Ttime_current
				if (this.inqTime==null) {
					this.inqTime = new InitialInquiry(this.upf).getDbInquiry();
					this.inqTime.setParam(this.data);
					this.inqTime.setSchemaName(this.upf.getSchemaName());
					this.inqTime.initMyTable("TTIME_CURRENT", "", "");
				}
				this.inqTime.setFilter(filter+" and employeeid='"+id+"'");
				this.inqTime.refresh();
				while (this.inqTime.next()) {
					this.recTime = this.inqTime.getCurrentDbRecord().cloneRecord(true);
					this.recTime.delete();
				}
				
				//Delete Ttime_current1
				if (this.inqTime1==null) {
					this.inqTime1 = new InitialInquiry(this.upf).getDbInquiry();
					this.inqTime1.setParam(this.data);
					this.inqTime1.setSchemaName(this.upf.getSchemaName());
					this.inqTime1.initMyTable("TTIME_CURRENT1", "", "");
				}
				this.inqTime1.setFilter(filter+" and employeeid='"+id+"'");
				this.inqTime1.refresh();
				while (this.inqTime1.next()) {
					this.recTime1 = this.inqTime1.getCurrentDbRecord().cloneRecord(true);
					this.recTime1.delete();
				}
				
				//Delete Tleave_summary
				/* ��Ŵ��ѹ����������͹�Ѻ TTIME_CURRENT �֧��ͧ����¹���Ϳ�Ŵ�� Filter ��͹ */
				String _FILTER = filter.replace("dateid", "lv_bg_date"); 
				
				DbInquiry TLeave = new InitialInquiry(this.upf).getDbInquiry();
				TLeave.setParam(this.data);
				TLeave.setSchemaName(this.upf.getSchemaName());
				TLeave.initMyTable("TLEAVE_SUMMARY", "", "");
				TLeave.setFilter(_FILTER+" and employeeid='"+id+"'");
				TLeave.refresh();
				while (TLeave.next()) {
					recTleave = TLeave.getCurrentDbRecord().cloneRecord(true);
					recTleave.delete();
				}
			}
		} catch (Exception e) {
			MyLog.error(this, e);			
		}
	}
}