package com.csc.library.databean;

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

import com.csc.library.utilities.CscCalendar;

/**
 * @author xerox
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class Simmemployee_tadjtime1 extends Memployee {
	MyHashMap hm_data = new MyHashMap();
	int count = 0;
	double hr = 0;
	String employeeid;
	
	public int assignField(DataSet rs) {
		int temp = super.assignField(rs);
		this.hm_data.clear();
		employeeid = this.getString("employeeid");
		if (!this.getString("employeeid").trim().equals("")) {
			//	MyLog.debug(this, "employeeid--->>>>>"+this.employeeid);
			//hr=Double.valueOf(this.getString("hour_d")).doubleValue();
			this.getHour_Day();
			this.setEventgrp();
			this.setDaytype();
			this.setSumData();
			this.setSubData();
			//MyLog.debug(this, "hm_eventgrp-->"+this.hm_data);
		}
		return temp;
	}

	public void setEventgrp() {
		MyLog.debug(this, "<<<<<--------setEventgrp--------->>>>>>");
		try {
			String DataEventgrp = "", lang = "ENG";
			if (this.param.containsKey("LANGUAGE")) {
				String codeL = (String) this.param.get("LANGUAGE");
				if (!codeL.trim().equals("")) {
					lang = codeL;
				}
			}
			//MyLog.debug(this, "param_lang---> "+this.param.get("LANGUAGE"));
			//MyLog.debug(this, "lang---> "+lang);
			DbInquiry in2 = new InitialInquiry(this.getUProfile()).getDbInquiry();
			in2.initMyTable("Meventgrp",	"daytype!='1' and daytype !='5' and daytype !='6' and daytype !='9'", "");
			in2.setColumn("eventgrpid,limit,edesc,tdesc");
			in2.refresh();
			while (in2.next()) {
				if (lang.equals("ENG")) {
					this.hm_data.put(in2.getString("eventgrpid"), in2.getString("edesc"));
				} else {
					this.hm_data.put(in2.getString("eventgrpid"), in2.getString("tdesc"));
				}
				this.hm_data.put("id_" + in2.getString("eventgrpid"), in2.getString("eventgrpid"));
				this.hm_data.put("limit_" + in2.getString("eventgrpid"), this.lToL(in2.getString("limit")));
				DataEventgrp += in2.getString("eventgrpid") + ",";
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}

	public void setDaytype() {
		// MyLog.debug(this, "<-------------------setdayType------------------->");
		try {
			count = 0;
			String codecol = "";
			String data = "0:0:0";
			MyLog.debug(this, "param--->" + this.param);

			codecol = "01";
			MyLog.debug(this, "param--->" + this.param.get("CODEID"));
			if (this.param.containsKey("CODEID")) {
				String code = (String) this.param.get("CODEID");
				if (!code.trim().equals("")) {
					codecol = code;
				}
			}

			DbInquiry in1 = new InitialInquiry(this.getUProfile()).getDbInquiry();
			in1.initMyTable("Mleave_report", "", "");
			in1.setColumn("*");
			in1.setFilter("codeid = '" + codecol + "'");
			in1.refresh();
			in1.next();
			for (int i = 1; i <= 10; i++) {
				if (i == 10) {
					if (!in1.getString("col10").equals("")) {
						count++;
					}
					if (in1.getString("col10").trim().equals("")) {
						continue;
					}
					this.hm_data.put("id_col10", in1.getString("col10"));
					this.hm_data.put("col10", this.hm_data.get(in1.getString("col10")).toString());
					this.hm_data.put("limit_col10", this.hm_data.get("limit_" + in1.getString("col10")).toString());
					this.hm_data.put("lv_col10", data);
					this.hm_data.put("total_col10", data);
					continue;
				}
				if (in1.getString("col0" + i).trim().equals("")) {
					continue;
				}
				if (!in1.getString("col0" + i).equals("")) {
					count++;
				}

				MyLog.debug(this, "Data Inq0" + i + "-->"+ in1.getString("col0" + i));
				this.hm_data.put("id_col0" + i, in1.getString("col0" + i));
				this.hm_data.put("col0" + i, this.hm_data.get(in1.getString("col0" + i)).toString());
				this.hm_data.put("limit_col0" + i, this.hm_data.get("limit_" + in1.getString("col0" + i)).toString());
				this.hm_data.put("lv_col0" + i, data);
				this.hm_data.put("total_col0" + i, data);
			}
		} catch (Exception e) {
			MyLog.error(this, e);

		}
	}

	public void setSumData() {
		//MyLog.debug(this, "<-------------------setSumData------------------->");
		// MyLog.debug(this, "STARTD--->"+this.param.get("STARTD"));
		// MyLog.debug(this, "ENDD--->"+this.param.get("ENDD"));
		try {
			double lt = 0;
			String tlv = "";
			int rec = 0, lv = 0, lv1 = 0, lvSum = 0;
			String startD = "", endD = "", sql = "";
			CscCalendar cs = new CscCalendar();
			FormulaString fs = new FormulaString();
			startD = String.valueOf(cs.getYear()) + "-01-01";
			endD = cs.getYYYYMMDD();

			if (this.param.containsKey("STARTD")) {
				String st = (String) this.param.get("STARTD");
				if (!st.trim().equals("")) {
					CscCalendar csS = new CscCalendar(st);
					startD = csS.getYYYYMMDD();
				}
			}

			if (this.param.containsKey("ENDD")) {
				String en = (String) this.param.get("ENDD");
				if (!en.trim().equals("")) {
					CscCalendar csE = new CscCalendar(en);
					endD = csE.getYYYYMMDD();
				}
			}

			MyLog.debug(this, "date---> " + startD + " <> " + endD);
			CscCalendar cs1 = new CscCalendar(startD);
			CscCalendar cs2 = new CscCalendar(endD);

			sql = "employeeid='" + this.getString("employeeid") + "' "
					+ "and (dateid between '" + startD + "' and '" + endD + "') "
					+ "and (doctype='A' or doctype='S')";
			DbInquiry in1 = new InitialInquiry(this.getUProfile()).getDbInquiry();
			in1.initMyTable("tleave_summary", sql, "employeeid,dateid");
			in1.setColumn("*");
			in1.refresh();
			while (in1.next()) {
				//MyLog.debug(this, "*docdate--->"+in1.getString("docdate"));
				//this.setHourD(in1.getString("docdate"),in1.getString("time0id"));
				//lt += Double.valueOf(in1.getString("lt")).doubleValue();
				for (int i = 1; i <= 10; i++) {
					if (i == 10) {
						if (in1.getString("lv_type").equals(this.hm_data.get("id_col10"))) {
							lv = fs.DhmToM(this.hm_data.get("lv_col10" + i).toString());
							lv += fs.Tm_To_Min(Double.valueOf(in1.getString("m_lv")).doubleValue());
							tlv = fs.MTODhm(lv, hr);
							this.hm_data.put("lv_col10", tlv);
							MyLog.debug(this, "lv_data----> "+ this.hm_data.get("id_col10") + " >"+ this.hm_data.get("lv_col10"));
						}
						continue;
					}
					//  MyLog.debug(this, "id_col"+i+"--->"+this.hm_data.get("id_col0"+i)+"<>"+in1.getString("eventgrp"));
					if (in1.getString("lv_type").equals(this.hm_data.get("id_col0" + i))) {
						//  MyLog.debug(this, "<-------------if-------------->");
						lv = fs.DhmToM(this.hm_data.get("lv_col0" + i).toString());
						lv1 = fs.Tm_To_Min(Double.valueOf(in1.getString("m_lv")).doubleValue());
						lvSum = lv + lv1;
						tlv = fs.MTODhm(lvSum, hr);
						this.hm_data.put("lv_col0" + i, tlv);
						rec += 1;
						//MyLog.debug(this, ">>>>>>>>>>lv_col0"+i+"<<<<<<<<<<<<<  "+tlv+"  hr-> "+hr);
						//MyLog.debug(this, "lv start  --> "+lv+" + "+lv1+" --> "+lvSum);
						//MyLog.debug(this, "lv_data   --> "+this.hm_data.get("lv_col0"+i)+" + "+in1.getString("lv")+" -->"+this.hm_data.get("lv_col0"+i));
					}
				}
				lv = 0;
			}
			startD = cs1.getDD() + "/" + cs1.getMM() + "/"+ String.valueOf(cs1.getYear());
			endD = cs2.getDD() + "/" + cs2.getMM() + "/"+ String.valueOf(cs2.getYear());
			//this.hm_data.put("lt_col",new Double(lt));
			this.hm_data.put("rec_col", new Integer(rec));
			this.hm_data.put("startd", startD);
			this.hm_data.put("endd", endD);
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}

	public void setSubData() {
		//  MyLog.debug(this, "<-------------------subData------------------->");
		String dhm;
		int sub = 0;
		//  MyLog.debug(this, "count----->"+count);
		FormulaString fs = new FormulaString();
		for (int i = 1; i <= count; i++) {
			if (i == 10) {
				//sub=fs.DhmToM(this.hm_data.get("limit_col10").toString(),hr);
				//sub-=fs.DhmToM(this.hm_data.get("lv_col10").toString(),hr);
				//this.hm_data.put("total_col10",fs.MTODhm(sub,hr));
				dhm = fs.subDhm(this.hm_data.get("limit_col10").toString(),this.hm_data.get("lv_col10").toString(), this.hr);
				this.hm_data.put("total_col10", dhm);
				//MyLog.debug(this, "total_col10--->"+this.hm_data.get("total_col10"));
				continue;
			}
			//sub=fs.DhmToM(this.hm_data.get("limit_col0"+i).toString(),hr);
			//sub-=fs.DhmToM(this.hm_data.get("lv_col0"+i).toString(),hr);
			//this.hm_data.put("total_col0"+i,fs.MTODhm(sub,hr));
			dhm = fs.subDhm(this.hm_data.get("limit_col0" + i).toString(),this.hm_data.get("lv_col0" + i).toString(), this.hr);
			this.hm_data.put("total_col0" + i, dhm);
			//MyLog.debug(this, "total_col0"+i+"--->"+this.hm_data.get("total_col0"+i));

		}
	}

	private String lToL(String a) {
		String dt = "";
		FormulaString fs = new FormulaString();
		if (a.indexOf(":") > -1) {
			dt = fs.MTODhm(fs.DhmToM(a, hr), hr);
		} else {
			dt = a + ":00:00";
		}
		return dt;
	}

	private void getHour_Day() {
		try {
			DbInquiry inq1 = new InitialInquiry(this.getUProfile()).getDbInquiry();
			inq1.initMyTable("mtime0", "", "");
			inq1.setColumn("time0id,companyid,edesc,tdesc,hour_d");
			inq1.refresh();

			DbInquiry inq = new InitialInquiry(this.getUProfile()).getDbInquiry();
			inq.initMyTable("memployee", "", "");
			inq.setColumn("employeeid,companyid,time0");
			inq.setFilter("employeeid='" + this.employeeid + "'");
			inq.putChild(inq1);
			inq.refresh();
			inq.next();
			this.hr = Double.valueOf(inq.getString("mtime0", "hour_d")).doubleValue();
			//	MyLog.debug(this, "hour_d------->>>" + this.hr);
		} catch (Exception e) {
			MyLog.error(this, e);

		}
	}

	public String getString(String field) {
		if (this.hm_data.containsKey(field)) {
			return this.hm_data.get(field).toString();
		} else {
			return super.getString(field);
		}
	}

}