package com.csc.library.databean;

import java.text.DecimalFormat;
import java.util.Vector;

import com.csc.library.database.DataSet;
import com.csc.library.database.MyHashMap;
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.CscTime;
import com.csc.library.utilities.MyLog;

/*
 * �����š�÷ӧҹ�ͧ��ѡ�ҹ �ͧ NSTDA
 */
public class Memployee_working_nstda extends Memployee {

	private int shift = 0;
	private CscTime ac_ot = new CscTime(0f);
	private CscTime ot1 = new CscTime(0f);
	private CscTime ot5 = new CscTime(0f);
	private CscTime ot2 = new CscTime(0f);
	private CscTime ot3 = new CscTime(0f);
	private CscTime otFlag = new CscTime(0f);
	private DbInquiry inqTimeCurrent = null;
	private DbInquiry inqTimeHistory = null;
	private String header = "";
	private DecimalFormat df = new DecimalFormat("0.00");
	private String filterFor147 = "";
	
	/*
	public static void main (String[] args){
		 CscTime acotTest= new CscTime(0f);
		 acotTest.add(4.13f);
		 acotTest.add(3.37f);
		 acotTest.add(3.56f);
		 acotTest.add(2.18f);
		 acotTest.add(1.44f);
		 acotTest.add(2.58f);
		 acotTest.add(3.04f);
		 System.out.print(acotTest.getFloat());
	}//end main for Test
	*/
	
	public int assignField(DataSet rs) {				
		int temp = super.assignField(rs);
		
		//for TAR146_NSTDA
		String month = (String) this.param.get("MONTHID");
		String year = (String) this.param.get("YEARID");
		
		//for TAR147_NSTDA
		String payMonth = (String) this.param.get("PAYMONTH");
		String payYear = (String) this.param.get("PAYYEAR");

		String stDayOfMonth= (String) this.param.get("STDOM");
		String stMonth = (String) this.param.get("STMONTH");
		String stYear = (String) this.param.get("STYEAR");
		String endDayOfMonth= (String) this.param.get("ENDDOM");
		String endMonth = (String) this.param.get("ENDMONTH");
		String endYear = (String) this.param.get("ENDYEAR");

	 	CscCalendar c = null;
		CscCalendar cscStart = null;
		CscCalendar cscEnd = null;
		//CscCalendar cscStart = new CscCalendar("01-01-2007");
		//CscCalendar cscEnd = new CscCalendar("31-12-2007");
		
		if(this.param.get("SCREEN").equals("TAR146_NSTDA")){
			//����Ѻ��§ҹ��ҡ� [TAR146_NSTDA]
			c = new CscCalendar("01-"+month+"-"+year);
			cscStart = new CscCalendar("01"+"-"+c.getMM()+"-"+c.getYear());
			cscEnd= new CscCalendar(c.getEndDateOfMonth()+"-"+c.getMM()+"-"+c.getYear());
			
			//�����§ҹ
			if (this.header.equals("")) {
				if (((String) this.userPro.get("lang")).equalsIgnoreCase("ENG")) {
					this.header = cscStart.getEngLongMonth()+" "+cscStart.getEngYYYY();
				} else{
					this.header = cscStart.getThaiLongMonth()+" "+cscStart.getThaiYYYY();
				}//end if 
			}
		}else{
			c = new CscCalendar("01-"+payMonth+"-"+payYear);
			c.decMonth();
			cscStart = new CscCalendar("01"+"-"+c.getMM()+"-"+c.getYear());
			cscEnd= new CscCalendar(c.getEndDateOfMonth()+"-"+c.getMM()+"-"+c.getYear());

			//����Ѻ��§ҹ OT [TAR147_NSTDA]
			CscCalendar stApprove = new CscCalendar(Integer.valueOf(stYear),Integer.valueOf(stMonth),Integer.valueOf(stDayOfMonth));
			CscCalendar endApprove= new CscCalendar(Integer.valueOf(endYear),Integer.valueOf(endMonth),Integer.valueOf(endDayOfMonth));
			
			//�����§ҹ
			if (this.header.equals("")) {
				if (((String) this.userPro.get("lang")).equalsIgnoreCase("ENG")) {
					this.header =cscStart.getDayOfMonth()+" "+cscStart.getEngLongMonth()+" "+cscStart.getEngYYYY()
					+" - "+cscEnd.getDayOfMonth()+" "+cscEnd.getEngLongMonth()+" "+cscEnd.getEngYYYY();
				} else{
					this.header = cscStart.getDayOfMonth()+" "+cscStart.getThaiLongMonth()+" "+cscStart.getThaiYYYY()
					+" �֧ "+cscEnd.getDayOfMonth()+" "+cscEnd.getThaiLongMonth()+" "+cscEnd.getThaiYYYY();
				}//end if 
			}//end if 
			
			//fix ������੾�� OT ��͹��ѵ�����
			//�������ö filter �����ǧ�ѹ���͹��ѵ��� 
			/**
			 * docno in(select approveid from totapprove_nstda1 where create_date >='2009-07-27' and create_date <='2009-07-27')
			 * and doctype ='OTA'  
			 */
			this.filterFor147 =" docno in(select tap1.approveid "+
				" from totapprove_nstda1 tap1"+
				" where tap1.create_date >='"+stApprove.getYYYYMMDD()+"' "+
				" and tap1.create_date <='"+endApprove.getYYYYMMDD()+"')"+
			    " and doctype ='OTA' ";		
		}//end if 
		
		
		
		this.clearData();
		if (!this.getString("employeeid").equalsIgnoreCase("")) {
			this.sumData(cscStart, cscEnd);
		}
		return temp;
	}
	
	public boolean foundData() {
		if (this.shift==0 && this.param.get("SCREEN").equals("TAR146_NSTDA")) { //��§ҹ TAR146_NSTDA ���ӹǹ�� ��Ҩӹǹ�� = 0 ������ ���. ������
			return false;
		} else if (this.ac_ot.getFloat()==0  && this.param.get("SCREEN").equals("TAR147_NSTDA")) { //��§ҹ TAR147_NSTDA ���ӹǹ�ͷ� ��Ҩӹǹ�ͷ� = 0 ������ ���. ������
			return false;
		} else return super.foundData();
	}
	
	/*
	 * �ӡ�� clear ��������ҷ��
	 */
	private void clearData() {
		this.ac_ot = new CscTime(0f);
		this.ot1 = new CscTime(0f);
		this.ot5 = new CscTime(0f);
		this.ot2 = new CscTime(0f);
		this.ot3 = new CscTime(0f);
		this.otFlag = new CscTime(0f);
		this.shift = 0;
	}
	
	private DbInquiry getInqTimeCurrent() {
		try {
			if (this.inqTimeCurrent==null) {				
				this.inqTimeCurrent = new InitialInquiry(this.userPro).getDbInquiry();
				this.inqTimeCurrent.setParam(this.param);
				this.inqTimeCurrent.setSchemaName(this.userPro.getSchemaName());
				this.inqTimeCurrent.initMyTable("Ttime_current1", "", "");
				this.inqTimeCurrent.setColumn("*");				
			}			
		} catch (Exception e) {
			MyLog.error(this, e);
		}
		return this.inqTimeCurrent;
	}
	
	private DbInquiry getInqTimeHistory() {
		try {
			if (this.inqTimeHistory==null) {				
				this.inqTimeHistory = new InitialInquiry(this.userPro).getDbInquiry();
				this.inqTimeHistory.setParam(this.param);
				this.inqTimeHistory.setSchemaName(this.userPro.getSchemaName());
				this.inqTimeHistory.initMyTable("Ttime_history1", "", "");
				this.inqTimeHistory.setColumn("*");				
			}			
		} catch (Exception e) {
			MyLog.error(this, e);
		}
		return this.inqTimeHistory;
	}
	
	private void sumData(CscCalendar c1, CscCalendar c2) {
		this.sumCurrentData(c1, c2);
		this.sumHistoryData(c1, c2);
	}
	
	/*
	 * �ӡ������š�÷ӧҹ �ͧ�����š�÷ӧҹ TTIME_CURRENT
	 */
	private void sumCurrentData(CscCalendar c1, CscCalendar c2) {
		try {
			this.inqTimeCurrent = this.getInqTimeCurrent();
			if(this.param.get("SCREEN").equals("TAR146_NSTDA")){
				this.inqTimeCurrent.setFilter("employeeid='"+this.getString("employeeid")+"' and " +
						"(dateid between '"+c1.getYYYYMMDD()+"' and '"+c2.getYYYYMMDD()+"')");
			}else{
				this.inqTimeCurrent.setFilter("employeeid='"+this.getString("employeeid")+"' and " +
						"(dateid between '"+c1.getYYYYMMDD()+"' and '"+c2.getYYYYMMDD()+"') and " +
						this.filterFor147);
			}//end if 
 
			this.inqTimeCurrent.refresh();
			while (this.inqTimeCurrent.next()) {
				DbRecord rec = this.inqTimeCurrent.getCurrentDbRecord();
				/**========= Nuiss Code =========
				 * Author : Nuiss
				 * Date : 22/03/2553
				 * Logic : ac_ot ��� NSTDA �ѡ����͡����
				 */
				CscTime ac_otTime = new CscTime(rec.getFloat("ac_ot"));
				CscTime ltTime = new CscTime(rec.getFloat("lt"));
				ac_otTime.sub(ltTime);
				this.ac_ot.add(ac_otTime.getFloat());
				/**========== Nuiss Code =======*/
				//�ͧ���
				//this.ac_ot.add(rec.getFloat("ac_ot"));
				this.ot1.add(rec.getFloat("ot1"));
				this.ot5.add(rec.getFloat("ot5"));
				this.ot2.add(rec.getFloat("ot2"));
				this.ot3.add(rec.getFloat("ot3"));
				if (rec.getFloat("unused1")==1) {
					this.shift++;
				}
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}
	
	/*
	 * �ӡ������š�÷ӧҹ �ͧ�����š�÷ӧҹ㹻���ѵ� TTIME_HISTORY
	 */
	private void sumHistoryData(CscCalendar c1, CscCalendar c2) {
		try {
			this.inqTimeHistory = this.getInqTimeHistory();
			if(this.param.get("SCREEN").equals("TAR146_NSTDA")){
				this.inqTimeHistory.setFilter("employeeid='"+this.getString("employeeid")+"' and " +
						"(dateid between '"+c1.getYYYYMMDD()+"' and '"+c2.getYYYYMMDD()+"')");
			}else{
				this.inqTimeHistory.setFilter("employeeid='"+this.getString("employeeid")+"' and " +
						"(dateid between '"+c1.getYYYYMMDD()+"' and '"+c2.getYYYYMMDD()+"') and " +
						this.filterFor147);
			}//end if 
			this.inqTimeHistory.refresh();
			while (this.inqTimeHistory.next()) {
				DbRecord rec = this.inqTimeHistory.getCurrentDbRecord();
				/**========= Nuiss Code =========
				 * Author : Nuiss
				 * Date : 22/03/2553
				 * Logic : ac_ot ��� NSTDA �ѡ����͡����
				 */
				CscTime ac_otTime = new CscTime(rec.getFloat("ac_ot"));
				CscTime ltTime = new CscTime(rec.getFloat("lt"));
				ac_otTime.sub(ltTime);
				this.ac_ot.add(ac_otTime.getFloat());
				/**========== Nuiss Code =======*/
				//�ͧ���
				//this.ac_ot.add(rec.getFloat("ac_ot"));
				this.ot1.add(rec.getFloat("ot1"));
				this.ot5.add(rec.getFloat("ot5"));
				this.ot2.add(rec.getFloat("ot2"));
				this.ot3.add(rec.getFloat("ot3"));
				if (rec.getFloat("unused1")==1) {
					this.shift++;
				}
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}
	
	public String getString(String field) {
     	if (field.equalsIgnoreCase("ac_ot")) {
     		return this.df.format(Double.parseDouble(String.valueOf(this.ac_ot.getFloat())));
     	} else if (field.equalsIgnoreCase("ot1")) {
     		return this.df.format(Double.parseDouble(String.valueOf(this.ot1.getFloat())));
     	} else if (field.equalsIgnoreCase("ot5")) {
     		return this.df.format(Double.parseDouble(String.valueOf(this.ot5.getFloat())));
     	} else if (field.equalsIgnoreCase("ot2")) {
     		return this.df.format(Double.parseDouble(String.valueOf(this.ot2.getFloat())));
     	} else if (field.equalsIgnoreCase("ot3")) {
     		return this.df.format(Double.parseDouble(String.valueOf(this.ot3.getFloat())));
     	} else if (field.equalsIgnoreCase("shift")) {
     		return String.valueOf(this.shift);
     	} else if (field.equalsIgnoreCase("header")) {
     		return String.valueOf(this.header);
     	} else return super.getString(field);
	}
}