package com.kingdee.shr.customer.gtiit.rpt;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.customer.gtiit.util.ExportAllUtil;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;

public class AbsenceSummaryForAllLeaveAListHandler extends ListHandler
{
	private static final Logger log = LoggerFactory.getLogger(AbsenceSummaryForAllLeaveAListHandler.class);

	Context ctx = SHRContext.getInstance().getContext();
	private int totalCount;

	public String getGridColModelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException, BOSException, SQLException
	{
		List colNames = getcolNamesAction();
		List colModel = getcolModelAction();
		LinkedHashMap map = new LinkedHashMap();
		map.put("colNames", colNames);
		map.put("colModel", colModel);
		JSONUtils.writeJson(response, map);
		return null;
	}

	public List<String> getcolNamesAction()
	{
		List list = new ArrayList();
		list.add("Dept");
		list.add("Staff No.");
		list.add("Name");
		list.add("Position");
		list.add("Work Category");
		list.add("Person Type");
		list.add("Full Time or Part Time");
		list.add("Hire Date");
		list.add("Ter Date");
		list.add("Status");
		list.add("Annual Entitlement (Days)");
		list.add("Faculty Sick Entitlement (Days)");
		list.add("Faculty Sick Max Entitlement (Days)");
		list.add("Child care Entitlement (Days)");
		list.add("Elderly care Entitlement (Days)");
		list.add("Annual Leave (Days)");
		list.add("Sick Leave (Days)");
		list.add("Marriage Leave (Days)");
		list.add("Paternity Leave (Days)");
		list.add("Maternity Leave (Days)");
		list.add("Bereavement Leave (Days)");
		list.add("Miscarriage Leave (Days)");
		list.add("Leave Of Family Planning Birth Control (Days)");
		list.add("Work-related Injury Leave (Days)");
		list.add("Medical Treatment Leave (Days)");
		list.add("Unpaid Leave (H)");
		list.add("Compensatory Leave (H)");
		list.add("Child Care Leave (Days)");
		list.add("Elderly Care Leave (Days)");
		list.add("Annual Leave Balance (Days)");
		list.add("sick leave balance");
		list.add("Compensatory Leave Balance(H)");
		list.add("Child Care Leave Balance");
		list.add("Elderly Care Leave Balance");
		return list;
	}

	public void getGridDataAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws BOSException, SQLException, SHRWebException, com.kingdee.eas.base.form.json.JSONException, org.json.JSONException
	{
		log.info(">>> Enter AbsenceSummaryForAllLeaveListHandler...getGridDataAction");
		int rows = Integer.parseInt(request.getParameter("rows"));
		int page = Integer.parseInt(request.getParameter("page"));

		IRowSet rs = getIRowSet(request);
		if (null == rs) {
			return;
		}
		int num = 0;
		List maplist = new ArrayList();
		while (rs.next()) {
			num++;
			if ((num >= rows * (page - 1) + 1) && (num <= rows * page)) {
				Map map = new HashMap();
				map.put("Dept", rs.getString("Dept"));
				map.put("Staff No.", rs.getString("StaffNo"));
				map.put("Name", rs.getString("Name"));
				map.put("Position", rs.getString("Position"));
				map.put("Work Category", rs.getString("WorkCategory"));
				map.put("Person Type", rs.getString("PersonType"));
				map.put("Full Time or Part Time", rs.getString("FullTimeOrPartTime"));
				map.put("Hire date", StringUtils.isEmpty(rs.getString("HireDate")) ? null : getDateString(rs.getString("HireDate")));
				map.put("Ter  Date", StringUtils.isEmpty(rs.getString("TerDate")) ? null : getDateString(rs.getString("TerDate")));
				map.put("Status", rs.getString("Status"));
				map.put("Annual Entitlement", rs.getBigDecimal("AnnualEntitlement") != null ? rs.getBigDecimal("AnnualEntitlement").setScale(2, 4) : Integer.valueOf(0));
				map.put("Faculty Sick Entitlement", rs.getBigDecimal("FacultySickEntitlement") != null ? rs.getBigDecimal("FacultySickEntitlement").setScale(2, 4) : Integer.valueOf(0));
				map.put("Faculty Sick Max Entitlement", rs.getBigDecimal("FacultySickMaxEntitlement") != null ? rs.getBigDecimal("FacultySickMaxEntitlement").setScale(2, 4) : Integer.valueOf(0));
				map.put("Child care Entitlement", rs.getBigDecimal("ChildCareEntitlement") != null ? rs.getBigDecimal("ChildCareEntitlement").setScale(2, 4) : Integer.valueOf(0));
				map.put("Elderly care Entitlement", rs.getBigDecimal("ElderlyCareEntitlement") != null ? rs.getBigDecimal("ElderlyCareEntitlement").setScale(2, 4) : Integer.valueOf(0));
				map.put("annual leave", rs.getBigDecimal("AnnualLeave") != null ? rs.getBigDecimal("AnnualLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("sick leave", rs.getBigDecimal("SickLeave") != null ? rs.getBigDecimal("SickLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("marriage leave", rs.getBigDecimal("MarriageLeave") != null ? rs.getBigDecimal("MarriageLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("paternity leave", rs.getBigDecimal("PaternityLeave") != null ? rs.getBigDecimal("PaternityLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("maternity leave", rs.getBigDecimal("MaternityLeave") != null ? rs.getBigDecimal("MaternityLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("bereavement leave", rs.getBigDecimal("BereavementLeave") != null ? rs.getBigDecimal("BereavementLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("miscarriage leave", rs.getBigDecimal("MiscarriageLeave") != null ? rs.getBigDecimal("MiscarriageLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("leave of family planning birth control", rs.getBigDecimal("LeaveOfFamilyPlanningBirthControl") != null ? rs.getBigDecimal("LeaveOfFamilyPlanningBirthControl").setScale(2, 4) : Integer.valueOf(0));
				map.put("work-related injury leave", rs.getBigDecimal("WorkRelatedInjuryLeave") != null ? rs.getBigDecimal("WorkRelatedInjuryLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("medical treatment leave", rs.getBigDecimal("MedicalTreatmentLeave") != null ? rs.getBigDecimal("MedicalTreatmentLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("unpaid leave(H)", rs.getBigDecimal("UnpaidLeaveH") != null ? rs.getBigDecimal("UnpaidLeaveH").setScale(2, 4) : Integer.valueOf(0));
				map.put("compensatory leave(H)", rs.getBigDecimal("CompensatoryLeaveH") != null ? rs.getBigDecimal("CompensatoryLeaveH").setScale(2, 4) : Integer.valueOf(0));
				map.put("Child Care Leave", rs.getBigDecimal("ChildCareLeave") != null ? rs.getBigDecimal("ChildCareLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("Elderly Care Leave", rs.getBigDecimal("ElderlyCareLeave") != null ? rs.getBigDecimal("ElderlyCareLeave").setScale(2, 4) : Integer.valueOf(0));
				map.put("Annual Leave Balance", rs.getBigDecimal("AnnualLeaveBalance") != null ? rs.getBigDecimal("AnnualLeaveBalance").setScale(2, 4) : Integer.valueOf(0));
				map.put("sick leave balance", rs.getBigDecimal("SickLeaveBalance") != null ? rs.getBigDecimal("SickLeaveBalance").setScale(2, 4) : Integer.valueOf(0));
				map.put("Compensatory Leave Balance(H)", rs.getBigDecimal("CompensatoryLeaveBalanceH") != null ? rs.getBigDecimal("CompensatoryLeaveBalanceH").setScale(2, 4) : Integer.valueOf(0));
				map.put("Child Care Leave Balance", rs.getBigDecimal("ChildCareLeaveBalance") != null ? rs.getBigDecimal("ChildCareLeaveBalance").setScale(2, 4) : Integer.valueOf(0));
				map.put("Elderly Care Leave Balance", rs.getBigDecimal("ElderlyCareLeaveBalance") != null ? rs.getBigDecimal("ElderlyCareLeaveBalance").setScale(2, 4) : Integer.valueOf(0));
				maplist.add(map);
			}
		}
		this.totalCount = num;
		Map gridDataMap = new LinkedHashMap();
		if (this.totalCount % rows == 0)
			gridDataMap.put("total", Integer.valueOf(this.totalCount / rows));
		else {
			gridDataMap.put("total", Integer.valueOf(this.totalCount / rows + 1));
		}
		gridDataMap.put("page", Integer.valueOf(page));
		gridDataMap.put("records", Integer.valueOf(this.totalCount));
		gridDataMap.put("rows", maplist);
		JSONUtils.writeJson(response, gridDataMap);
		log.info(">>> complete AbsenceSummaryForAllLeaveListHandler...getGridDataAction!!!");
	}

	public IRowSet getIRowSet(HttpServletRequest request)
			throws BOSException, org.json.JSONException
	{
		String fastFilterItems = request.getParameter("fastFilterItems");
		JSONObject tofastFilterItems = new JSONObject(fastFilterItems);
		log.info("ASFALH...tofastFilterItems = " + tofastFilterItems);
		JSONObject status = tofastFilterItems.getJSONObject("status");
		log.info("ASFALH...status = " + status);
		String statusValues = status.getString("values");
		log.info("ASFALH...statusValues = " + statusValues);
		JSONObject cutOffDateJSON = tofastFilterItems.getJSONObject("cutOffDate");
		if ("".equals(cutOffDateJSON.get("values"))) {
			return null;
		}
		JSONObject cutOffDateValues = cutOffDateJSON.getJSONObject("values");
		if (ObjectUtils.isEmpty(cutOffDateValues)) {
			return null;
		}

		JSONObject fullOrPart = tofastFilterItems.getJSONObject("fullOrPart");
		String fullOrPartValues = fullOrPart.getString("values");

		String cutOffDate = cutOffDateValues.getString("date");
		SimpleDateFormat dat = new SimpleDateFormat("yyyy-MM-dd");
		String yearFirstDate = null;
		String year = "";
		try {
			Date cfDate = dat.parse(cutOffDate);
			Calendar ins = Calendar.getInstance();
			ins.setTime(cfDate);
			int yearDate = ins.get(1);
			year = String.valueOf(yearDate);
			Date yearFirst = getYearFirst(yearDate);
			yearFirstDate = dat.format(yearFirst);
		}
		catch (ParseException e) {
			return null;
		}

		String where = " where 1=1 ";
		if (ObjectUtils.isNotEmpty(statusValues)) {
			if ("1".equals(statusValues))
			{
				where = where + "  and final.Status='Active' ";
			}
			else {
				where = where + "  and final.Status='Negative' ";
			}
		}

		if (ObjectUtils.isNotEmpty(fullOrPartValues)) {
			if ("1".equals(fullOrPartValues))
			{
				where = where + "   and ftorpt =(select fid from CT_MP_Fullorpart where fnumber = 'FULL') ";
			} else if ("2".equals(fullOrPartValues))
			{
				where = where + "   and ftorpt =(select fid from CT_MP_Fullorpart where fnumber = 'PART')";
			}
		}

		JSONObject queryTime = tofastFilterItems.getJSONObject("queryTime");
		String queryTimeValues = queryTime.getString("values");
		String startDate = null;
		String endDate = null;
		if (ObjectUtils.isNotEmpty(queryTimeValues)) {
			JSONObject periodEmploymentValues = queryTime.getJSONObject("values");

			startDate = periodEmploymentValues.getString("startDate");
			endDate = periodEmploymentValues.getString("endDate");
			log.info("FASRH...getGridDataAction : startDate = " + startDate + "; endDate = " + endDate);
		}

		if ((null == startDate) || (null == endDate)) {
			return null;
		}

		startDate = getDateString(startDate) + " 00:00:00";
		endDate = getDateString(endDate) + " 23:59:59";

		String sql = getSql(startDate, endDate, cutOffDate, yearFirstDate, year, where);
		log.info("AbsenceSummaryForAllLeaveListHandler...sql---{}", sql);
		log.error("AbsenceSummaryForAllLeaveListHandler...sql---error-" + sql);
		return DbUtil.executeQuery(this.ctx, sql);
	}

	private String getSql(String startDate, String endDate, String cutoffDate, String firstYearDate, String year, String where)
	{

		StringBuilder sql = new StringBuilder();
		sql.append(" /*dialect*/ SELECT  ");
		sql.append(" final.Dept Dept, ");
		sql.append(" final.StaffNo StaffNo, ");
		sql.append(" final.Name Name, ");
		sql.append(" final.ftorpt ftorpt, ");
		sql.append(" final.POSITION POSITION, ");
		sql.append(" final.WorkCategory WorkCategory, ");
		sql.append(" final.PersonType PersonType, ");
		sql.append(" final.FullTimeOrPartTime FullTimeOrPartTime, ");
		sql.append(" final.HireDate HireDate, ");
		sql.append(" final.TerDate TerDate, ");
		sql.append(" final.Status Status, ");
		sql.append(" CASE ");
		sql.append(" 		WHEN final.FullTimeOrPartTime= 'Part Time' THEN ");
		sql.append(" 		0 ELSE COALESCE ( final.AnnualEntitlement, 0 )  ");
		sql.append(" 	END AnnualEntitlement, ");
		sql.append(" CASE ");
		sql.append(" 	WHEN ( final.WorkCategory= 'General Administrative Staff' OR final.WorkCategory= 'Professional Supervisory Staff' OR final.WorkCategory= 'Senior Administrative Staff' )  ");
		sql.append(" 	AND final.FacultySickEntitlement= '0' THEN ");
		sql.append(" 	15 ELSE COALESCE ( final.FacultySickEntitlement, 0 )  ");
		sql.append(" 	END FacultySickEntitlement, ");
		sql.append(" CASE ");
		sql.append(" 	WHEN ( final.WorkCategory= 'General Administrative Staff' OR final.WorkCategory= 'Professional Supervisory Staff' OR final.WorkCategory= 'Senior Administrative Staff' )  ");
		sql.append(" 	AND final.FacultySickMaxEntitlement= '0' THEN ");
		sql.append(" 	75 ELSE COALESCE ( final.FacultySickMaxEntitlement, 0 )  ");
		sql.append(" 	END FacultySickMaxEntitlement, ");
		sql.append(" 	COALESCE ( final.ChildCareEntitlement, 0 ) ChildCareEntitlement, ");
		sql.append(" 	COALESCE ( final.ElderlyCareEntitlement, 0 ) ElderlyCareEntitlement, ");
		sql.append(" 	COALESCE ( final.AnnualLeave, 0 ) AnnualLeave, ");
		sql.append(" 	COALESCE ( final.SickLeave, 0 ) SickLeave, ");
		sql.append(" 	COALESCE ( final.MarriageLeave, 0 ) MarriageLeave, ");
		sql.append(" 	COALESCE ( final.PaternityLeave, 0 ) PaternityLeave, ");
		sql.append(" 	COALESCE ( final.MaternityLeave, 0 ) MaternityLeave, ");
		sql.append(" 	COALESCE ( final.BereavementLeave, 0 ) BereavementLeave, ");
		sql.append(" 	COALESCE ( final.MiscarriageLeave, 0 ) MiscarriageLeave, ");
		sql.append(" 	COALESCE ( final.LeaveOfFamilyPlanningBirthControl, 0 ) LeaveOfFamilyPlanningBirthControl, ");
		sql.append(" 	COALESCE ( final.WorkRelatedInjuryLeave, 0 ) WorkRelatedInjuryLeave, ");
		sql.append(" 	COALESCE ( final.MedicalTreatmentLeave, 0 ) MedicalTreatmentLeave, ");
		sql.append(" 	COALESCE ( final.UnpaidLeaveH, 0 ) UnpaidLeaveH, ");
		sql.append(" 	COALESCE ( final.CompensatoryLeaveH, 0 ) CompensatoryLeaveH, ");
		sql.append(" 	COALESCE ( final.ChildCareLeave, 0 ) ChildCareLeave, ");
		sql.append(" 	COALESCE ( final.ElderlyCareLeave, 0 ) ElderlyCareLeave, ");
		sql.append(" CASE ");
		sql.append(" 		WHEN final.FullTimeOrPartTime= 'Part Time' THEN ");
		sql.append(" 		0 ELSE COALESCE ( final.AnnualLeaveBalance, 0 )  ");
		sql.append(" 	END AnnualLeaveBalance, ");
		sql.append(" CASE ");
		sql.append(" 	WHEN final.WorkCategory= 'Academic Staff'  ");
		sql.append(" 	OR final.WorkCategory= 'Others' THEN ");
		sql.append(" 	COALESCE ( final.SickLeaveBalance, 0 )  ");
		sql.append(" 	WHEN ( ");
		sql.append(" 		COALESCE ( final.SickLeaveBalance, 0 ) > ");
		sql.append(" 	CASE ");
		sql.append(" 			WHEN ( final.WorkCategory= 'General Administrative Staff' OR final.WorkCategory= 'Professional Supervisory Staff' OR final.WorkCategory= 'Senior Administrative Staff' )  ");
		sql.append(" 			AND final.FacultySickMaxEntitlement= '0' THEN ");
		sql.append(" 				75 ELSE COALESCE ( final.FacultySickMaxEntitlement, 0 )  ");
		sql.append(" 			END  ");
		sql.append(" 				) THEN ");
		sql.append(" 			CASE ");
		sql.append(" 					WHEN ( final.WorkCategory= 'General Administrative Staff' OR final.WorkCategory= 'Professional Supervisory Staff' OR final.WorkCategory= 'Senior Administrative Staff' )  ");
		sql.append(" 					AND final.FacultySickMaxEntitlement= '0' THEN ");
		sql.append(" 						75 ELSE COALESCE ( final.FacultySickMaxEntitlement, 0 )  ");
		sql.append(" 					END  ");
		sql.append(" 						WHEN ( ");
		sql.append(" 							COALESCE ( final.SickLeaveBalance, 0 ) <= ");
		sql.append(" 						CASE ");
		sql.append(" 								WHEN ( final.WorkCategory= 'General Administrative Staff' OR final.WorkCategory= 'Professional Supervisory Staff' OR final.WorkCategory= 'Senior Administrative Staff' )  ");
		sql.append(" 								AND final.FacultySickMaxEntitlement= '0' THEN ");
		sql.append(" 									75 ELSE COALESCE ( final.FacultySickMaxEntitlement, 0 )  ");
		sql.append(" 								END  ");
		sql.append(" 									) THEN ");
		sql.append(" 									COALESCE ( final.SickLeaveBalance, 0 )  ");
		sql.append(" 								END SickLeaveBalance, ");
		sql.append(" 	COALESCE ( final.CompensatoryLeaveBalanceH, 0 ) CompensatoryLeaveBalanceH, ");
		sql.append(" 	COALESCE ( final.ChildCareLeaveBalance, 0 ) ChildCareLeaveBalance, ");
		sql.append(" 	COALESCE ( final.ElderlyCareLeaveBalance, 0 ) ElderlyCareLeaveBalance  ");
		sql.append(" FROM ( ");
		sql.append(" 	SELECT * FROM  ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tbp.fid fpersonid, ");
		sql.append(" 			bcc.fname_l1 AS Dept, ");
		sql.append(" 			tbp.fnumber AS StaffNo, ");
		sql.append(" 			tbp.fname_l1 AS Name, ");
		sql.append(" 			tbp.cfftorptid AS ftorpt, ");
		sql.append(" 			bd.fname_l1 AS POSITION, ");
		sql.append(" 			be.fname_l1 AS WorkCategory, ");
		sql.append(" 			hepc.fname_l1 AS PersonType, ");
		sql.append(" 			bg.fname_l1 AS FullTimeOrPartTime, ");
		sql.append(" 			hmrc.joinDate AS HireDate, ");
		sql.append(" 			bb.fleftdate AS TerDate, ");
		sql.append(" 		CASE ");
		sql.append(" 				WHEN bh.finservice= '1' THEN CAST ('Active' AS VARCHAR ( 10 ))  ");
		sql.append(" 				WHEN bh.finservice= '0' THEN CAST ('Negative' AS VARCHAR ( 10 ))  ");
		sql.append(" 				WHEN bh.finservice= '2' THEN CAST ('Negative' AS VARCHAR ( 10 ))  ");
		sql.append(" 				WHEN bh.finservice= '3' THEN CAST ('Negative' AS VARCHAR ( 10 ))  ");
		sql.append(" 				WHEN bh.finservice= '4' THEN CAST ('Active' AS VARCHAR ( 10 ))  ");
		sql.append(" 				else CAST ('Negative' AS VARCHAR ( 10 ))  ");
		sql.append(" 		END AS Status  ");
		sql.append(" 		FROM ");
		sql.append(" 			( SELECT * FROM T_BD_PERSON WHERE fhiredate <= '" + cutoffDate + "' ) tbp ");
		sql.append(" 			LEFT JOIN ( ");
		sql.append(" 			SELECT ");
		sql.append(" 				bahis.*  ");
		sql.append(" 			FROM ");
		sql.append(" 				T_HR_EmpOrgRelation bahis  ");
		sql.append(" 			WHERE ");
		sql.append(" 				bahis.fid IN ( ");
		sql.append(" 				SELECT ");
		sql.append(" 					hera.fid  ");
		sql.append(" 				FROM ");
		sql.append(" 					T_HR_EmpOrgRelation hera ");
		sql.append(" 					INNER JOIN ( ");
		sql.append(" 					SELECT MAX( fleffdt ) maxDate, ");
		sql.append(" 						fpersonid fpersonid, ");
		sql.append(" 						fid fid  ");
		sql.append(" 					FROM ");
		sql.append(" 						T_HR_EmpOrgRelation  ");
		sql.append(" 					WHERE ");
		sql.append(" 						FIsLatestInAday = '1'  ");
		sql.append(" 						AND FAssignType = '1'  ");
		sql.append(" 						AND ( feffdt <= '" + cutoffDate + "' AND fleffdt >= '" + cutoffDate + "' )  ");
		sql.append(" 					GROUP BY ");
		sql.append(" 						fpersonid, ");
		sql.append(" 						fid  ");
		sql.append(" 					) herb ON hera.fid= herb.fid  ");
		sql.append(" 				)) temp ON tbp.fid= temp.fpersonid ");
		sql.append(" 			LEFT JOIN T_ORG_Admin bcc ON bcc.fid= temp.fadminorgid ");

		sql.append(" 			left join  T_BD_PERSONHis tbph on tbph.fhistoryrelateid=tbp.fid AND ( tbph.feffdt <= '" + cutoffDate + "' AND tbph.fleffdt >= '" + cutoffDate + "' ) ");
		sql.append(" 			LEFT JOIN T_HR_EmployeeClassify hepc ON hepc.fid= COALESCE(temp.cfpersontypeid,tbph.cfpersontypeid,tbp.cfpersontypeid) ");
		sql.append(" LEFT JOIN T_ORG_Position bd ON temp.fpositionid = bd.fid ");
		sql.append(" LEFT JOIN CT_MP_WorkerCategory be ON be.fid = COALESCE(temp.cfworkercategoryid,tbph.cfworkercategoryid,tbp.cfworkercategoryid)  ");
		sql.append(" LEFT JOIN CT_MP_Fullorpart bg ON bg.fid = COALESCE(temp.CFFtorptID,tbph.CFFtorptID,tbp.CFFtorptID) ");
		sql.append(" LEFT JOIN T_HR_EmpLaborRelation hebr on hebr.fpersonid = temp.fpersonid ");
		sql.append(" LEFT JOIN T_HR_BDEmployeeType bh ON bh.fid= hebr.flaborrelationstateid  ");
//
//		sql.append(" 			LEFT JOIN T_HR_EmployeeClassify hepc ON hepc.fid= temp.cfpersontypeid ");
//		sql.append(" 			LEFT JOIN T_ORG_Position bd ON temp.fpositionid = bd.fid ");
//		sql.append(" 			LEFT JOIN CT_MP_WorkerCategory be ON be.fid = temp.cfworkercategoryid ");
//		sql.append(" 			LEFT JOIN CT_MP_Fullorpart bg ON bg.fid = tbp.CFFtorptID ");

		//sql.append(" 			LEFT JOIN ( SELECT hebr.fpersonid fpersonid, bem.finservice finservice FROM T_HR_EmpLaborRelation hebr LEFT JOIN T_HR_BDEmployeeType bem ON bem.fid= hebr.flaborrelationstateid ) bh ON bh.fpersonid = temp.fpersonid ");
		sql.append(" 			LEFT JOIN ( ");
		sql.append(" 			SELECT ");
		sql.append(" 				hpipa.fleftdate fleftdate, ");
		sql.append(" 				hpipa.fpersonid fpersonid  ");
		sql.append(" 			FROM ");
		sql.append(" 				T_HR_PersonPositionHis hpipa ");
		sql.append(" 				INNER JOIN ( SELECT MAX ( fleffdt ) maxDate, fpersonid fpersonid FROM T_HR_PersonPositionHis GROUP BY fpersonid ) hpipb ON hpipa.fleffdt= hpipb.maxDate  ");
		sql.append(" 				AND hpipa.fpersonid= hpipb.fpersonid  ");
		sql.append(" 			) bb ON temp.fpersonid = bb.fpersonid ");
		sql.append(" 			LEFT JOIN ( ");
		sql.append(" 			SELECT ");
		sql.append(" 				hmra.fpersonid, ");
		sql.append(" 				hmra.fenterdate joinDate  ");
		sql.append(" 			FROM ");
		sql.append(" 				T_HR_EmpLaborRelationHis hmra ");
		sql.append(" 				INNER JOIN ( SELECT MAX ( fleffdt ) maxDate, fpersonid fpersonid FROM T_HR_EmpLaborRelationHis WHERE fenterdate <= '" + cutoffDate + "' GROUP BY fpersonid ) hmrb ON hmra.fleffdt= hmrb.maxDate  ");
		sql.append(" 				AND hmra.fpersonid= hmrb.fpersonid  ");
		sql.append(" 			) hmrc ON hmrc.fpersonid= temp.fpersonid  ");
		sql.append(" 		) tep ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			fproposerid fpersonid, ");
		sql.append(" 			COALESCE ( cfentitlementlimit, 0 ) AnnualEntitlement  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_HolidayLimit  ");
		sql.append(" 		WHERE ");
		sql.append(" 			fcyclebegindate <= '" + cutoffDate + "'  ");
		sql.append(" 			AND fcycleenddate >= '" + cutoffDate + "'  ");
		sql.append(" 			AND fstatus = '1'  ");
		sql.append(" 			AND ( ");
		sql.append(" 				fholidaypolicyid IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000001Y' )  ");
		sql.append(" 			)  ");
		sql.append(" 		) htmab ON htmab.fpersonid= tep.fpersonid ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			hera.fhistoryrelateid fpersonid, ");
		sql.append(" 			hera.cffse FacultySickEntitlement  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_BD_PersonHis hera ");
		sql.append(" 			INNER JOIN ( SELECT MAX ( fleffdt ) maxDate, fhistoryrelateid fpersonid FROM T_BD_PersonHis WHERE feffdt <= '" + cutoffDate + "' AND fleffdt >= '" + cutoffDate + "' GROUP BY fpersonid ) herb ON hera.fhistoryrelateid= herb.fpersonid  ");
		sql.append(" 			AND hera.fleffdt= herb.maxDate  ");
		sql.append(" 		) htma ON htma.fpersonid= tep.fpersonid ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			hera.fhistoryrelateid fpersonid, ");
		sql.append(" 			hera.cffsme FacultySickMaxEntitlement, ");
		sql.append(" 			hera.fleffdt fleffdt  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_BD_PersonHis hera ");
		sql.append(" 			INNER JOIN ( SELECT MAX ( fleffdt ) maxDate, fhistoryrelateid fpersonid FROM T_BD_PersonHis WHERE feffdt <= '" + cutoffDate + "' AND fleffdt >= '" + cutoffDate + "' GROUP BY fpersonid ) herb ON hera.fhistoryrelateid= herb.fpersonid  ");
		sql.append(" 			AND hera.fleffdt= herb.maxDate  ");
		sql.append(" 		) htmb ON htmb.fpersonid= tep.fpersonid ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			fproposerid fpersonid, ");
		sql.append(" 			COALESCE ( SUM ( fstandardlimit ), 0 ) ChildCareEntitlement  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_HolidayLimit  ");
		sql.append(" 		WHERE ");
		sql.append(" 			fcyclebegindate <= '" + cutoffDate + "'  ");
		sql.append(" 			AND fcycleenddate >= '" + cutoffDate + "'  ");
		sql.append(" 			AND fstatus = '1'  ");
		sql.append(" 			AND ( ");
		sql.append(" 				fholidaypolicyid IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000015Y' )  ");
		sql.append(" 			)  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			fproposerid  ");
		sql.append(" 		) htmc ON htmc.fpersonid= tep.fpersonid ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			fproposerid fpersonid, ");
		sql.append(" 			COALESCE ( SUM ( fstandardlimit ), 0 ) ElderlyCareEntitlement  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_HolidayLimit  ");
		sql.append(" 		WHERE ");
		sql.append(" 			EXTRACT ( YEAR FROM fcyclebegindate ) = '" + year + "'  ");
		sql.append(" 			AND fstatus = '1'  ");
		sql.append(" 			AND ( ");
		sql.append(" 				fholidaypolicyid IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000016Y' )  ");
		sql.append(" 			)  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			fproposerid  ");
		sql.append(" 		) htmd ON htmd.fpersonid= tep.fpersonid ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tema.FPERSONID, ");
		sql.append(" 			SUM ( tema.AnnualLeave ) AnnualLeave  ");
		sql.append(" 		FROM ");
		sql.append(" 			( ");
		sql.append(" 			SELECT ");
		sql.append(" 				tem.fpersonId FPERSONID, ");
		sql.append(" 				tem.leavedate leavedate, ");
		sql.append(" 			CASE ");
		sql.append(" 					WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 					WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END AnnualLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT DISTINCT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			b.fleavedate leavedate, ");
		sql.append(" 			b.fleavelength fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000001Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT DISTINCT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			c.cfleavedate leavedate, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			c.cfleavelenght cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000001Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId, ");
		sql.append(" 	tem.leavedate  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htme ON htme.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.SickLeave ) SickLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END SickLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000003Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000003Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmf ON htmf.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.MarriageLeave ) MarriageLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 		END MarriageLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000005Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000005Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmh ON htmh.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.PaternityLeave ) PaternityLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE ");
		sql.append(" 			WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END PaternityLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000007Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000007Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmi ON htmi.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.MaternityLeave ) MaternityLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END MaternityLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000006Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000006Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmj ON htmj.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.BereavementLeave ) BereavementLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END BereavementLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000011Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000011Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmk ON htmk.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.MiscarriageLeave ) MiscarriageLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END MiscarriageLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000013Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000013Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) html ON html.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.LeaveOfFamilyPlanningBirthControl ) LeaveOfFamilyPlanningBirthControl  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END LeaveOfFamilyPlanningBirthControl  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000008Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000008Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmm ON htmm.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.WorkRelatedInjuryLeave ) WorkRelatedInjuryLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END WorkRelatedInjuryLeave  ");
		sql.append(" FROM  ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000009Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000009Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmn ON htmn.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.MedicalTreatmentLeave ) MedicalTreatmentLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE  ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END MedicalTreatmentLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000012Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000012Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmo ON htmo.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.UnpaidLeaveH ) UnpaidLeaveH  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END UnpaidLeaveH  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000002Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000002Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmp ON htmp.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID FPERSONID, ");
		sql.append(" 		SUM ( tema.CompensatoryLeaveH ) CompensatoryLeaveH  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 		CASE  ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN ");
		sql.append(" 				SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN ");
		sql.append(" 				SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END CompensatoryLeaveH  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000004Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000004Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmq ON htmq.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID, ");
		sql.append(" 		SUM ( tema.AnnualLeave ) ChildCareLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 			tem.leavedate leavedate, ");
		sql.append(" 		CASE ");
		sql.append(" 			WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 			WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END AnnualLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT DISTINCT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			b.fleavedate leavedate, ");
		sql.append(" 			b.fleavelength fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000015Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT DISTINCT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			c.cfleavedate leavedate, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			c.cfleavelenght cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000015Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId, ");
		sql.append(" 	tem.leavedate  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmqa ON htmqa.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tema.FPERSONID, ");
		sql.append(" 		SUM ( tema.AnnualLeave ) ElderlyCareLeave  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tem.fpersonId FPERSONID, ");
		sql.append(" 			tem.leavedate leavedate, ");
		sql.append(" 		CASE  ");
		sql.append(" 				WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 				WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 			END AnnualLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT DISTINCT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			b.fleavedate leavedate, ");
		sql.append(" 			b.fleavelength fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000016Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + endDate + "'  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT DISTINCT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			c.cfleavedate leavedate, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			c.cfleavelenght cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000016Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + startDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + endDate + "'  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId, ");
		sql.append(" 	tem.leavedate  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) htmqb ON htmqb.FPERSONID= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tempa.fpersonid fpersonid, ");
		sql.append(" 		COALESCE ( tempa.freallimit, 0 ) - COALESCE ( tempb.AnnualLeave, 0 ) AnnualLeaveBalance  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tbp.fid fpersonid, ");
		sql.append(" 			COALESCE ( temp.freallimit, 0 ) freallimit  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_BD_PERSON tbp ");
		sql.append(" 			LEFT JOIN ( ");
		sql.append(" 			SELECT ");
		sql.append(" 				fproposerid fpersonid, ");
		sql.append(" 				COALESCE ( freallimit, 0 ) freallimit  ");
		sql.append(" 			FROM ");
		sql.append(" 				T_HR_ATS_HolidayLimit  ");
		sql.append(" 			WHERE ");
		sql.append(" 				fcyclebegindate <= '" + cutoffDate + "'  ");
		sql.append(" 				AND fcycleenddate >= '" + cutoffDate + "'  ");
		sql.append(" 				AND fstatus = '1'  ");
		sql.append(" 				AND ( ");
		sql.append(" 					fholidaypolicyid IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000001Y' )  ");
		sql.append(" 				)) temp ON tbp.fid= temp.fpersonid  ");
		sql.append(" 		) tempa ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tema.FPERSONID, ");
		sql.append(" 			SUM ( tema.AnnualLeave ) AnnualLeave  ");
		sql.append(" 		FROM ");
		sql.append(" 			( ");
		sql.append(" 			SELECT ");
		sql.append(" 				tem.fpersonId FPERSONID, ");
		sql.append(" 				tem.leavedate leavedate, ");
		sql.append(" 			CASE ");
		sql.append(" 					WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 					WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 				END AnnualLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT DISTINCT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			b.fleavedate leavedate, ");
		sql.append(" 			b.fleavelength fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000001Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + firstYearDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT DISTINCT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			c.cfleavedate leavedate, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			c.cfleavelenght cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000001Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + firstYearDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId, ");
		sql.append(" 	tem.leavedate  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) tempb ON tempa.fpersonid= tempb.FPERSONID  ");
		sql.append(" 	) htmw ON htmw.fpersonid= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tempa.fpersonid fpersonid, ");
		sql.append(" 		COALESCE ( tempa.FacultySickEntitlement, 0 ) - COALESCE ( tempb.SickLeave, 0 ) SickLeaveBalance  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tbp.fid fpersonid, ");
		sql.append(" 			COALESCE ( sick.limitCount, 0 ) FacultySickEntitlement  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_BD_PERSON tbp ");
		sql.append(" 			LEFT JOIN ( ");
		sql.append(" 			SELECT ");
		sql.append(" 				cfpersonid fpersonid, ");
		sql.append(" 				SUM ( ");
		sql.append(" 				COALESCE ( cfinitiallimit, 0 ) + COALESCE ( cfrealitygrantlimit, 0 )) limitCount  ");
		sql.append(" 			FROM ");
		sql.append(" 				CT_CUS_SickLeaveLimit  ");
		sql.append(" 			WHERE ");
		sql.append(" 				cfmonth <= date_trunc ( 'month', '" + cutoffDate + "' :: DATE ) - INTERVAL '1 day'  ");
		sql.append(" 			GROUP BY ");
		sql.append(" 				cfpersonid  ");
		sql.append(" 			) sick ON sick.fpersonid= tbp.fid  ");
		sql.append(" 		) tempa ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tema.FPERSONID FPERSONID, ");
		sql.append(" 			SUM ( tema.SickLeave ) SickLeave  ");
		sql.append(" 		FROM ");
		sql.append(" 			( ");
		sql.append(" 			SELECT ");
		sql.append(" 				tem.fpersonId FPERSONID, ");
		sql.append(" 			CASE ");
		sql.append(" 					WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 					WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 				END SickLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000003Y' )  ");
		sql.append(" 			AND b.fleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000003Y' )  ");
		sql.append(" 			AND cfleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) tempb ON tempb.FPERSONID= tempa.fpersonid  ");
		sql.append(" 	) htmx ON htmx.fpersonid= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tempa.fpersonid fpersonid, ");
		sql.append(" 		COALESCE ( tempa.CompensatoryLeava ) - COALESCE ( tempb.AnnualLeave, 0 ) CompensatoryLeaveBalanceH  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tbp.fid fpersonid, ");
		sql.append(" 			COALESCE ( child.CompensatoryLeava, 0 ) CompensatoryLeava  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_BD_PERSON tbp ");
		sql.append(" 			LEFT JOIN ( ");
		sql.append(" 			SELECT ");
		sql.append(" 				fproposerid fpersonid, ");
		sql.append(" 				COALESCE ( freallimit, 0 ) CompensatoryLeava  ");
		sql.append(" 			FROM ");
		sql.append(" 				T_HR_ATS_HolidayLimit  ");
		sql.append(" 			WHERE ");
		sql.append(" 				EXTRACT ( YEAR FROM fcyclebegindate ) = '" + year + "'  ");
		sql.append(" 				AND fstatus = '1'  ");
		sql.append(" 				AND ( ");
		sql.append(" 					fholidaypolicyid IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000004Y' )  ");
		sql.append(" 				)) child ON child.fpersonid= tbp.fid  ");
		sql.append(" 		) tempa ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tema.FPERSONID FPERSONID, ");
		sql.append(" 			SUM ( tema.AnnualLeave ) AnnualLeave  ");
		sql.append(" 		FROM ");
		sql.append(" 			( ");
		sql.append(" 			SELECT ");
		sql.append(" 				tem.fpersonId FPERSONID, ");
		sql.append(" 			CASE ");
		sql.append(" 					WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 					WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 				END AnnualLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000004Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + firstYearDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000004Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + firstYearDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) tempb ON tempb.FPERSONID= tempa.fpersonid  ");
		sql.append(" 	) htmy ON htmy.fpersonid= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tempa.fpersonid fpersonid, ");
		sql.append(" 		COALESCE ( tempa.ChildCareLeave ) - COALESCE ( tempb.AnnualLeave, 0 ) ChildCareLeaveBalance  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tbp.fid fpersonid, ");
		sql.append(" 			COALESCE ( child.ChildCareLeave, 0 ) ChildCareLeave  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_BD_PERSON tbp ");
		sql.append(" 			LEFT JOIN ( ");
		sql.append(" 			SELECT ");
		sql.append(" 				fproposerid fpersonid, ");
		sql.append(" 				COALESCE ( SUM ( freallimit ), 0 ) ChildCareLeave  ");
		sql.append(" 			FROM ");
		sql.append(" 				T_HR_ATS_HolidayLimit  ");
		sql.append(" 			WHERE ");
		sql.append(" 				( fcyclebegindate, fcycleenddate ) OVERLAPS ( ");
		sql.append(" 					to_date ( '" + firstYearDate + "', 'yyyy-MM-dd' ), ");
		sql.append(" 				to_date ( '" + cutoffDate + "', 'yyyy-MM-dd' ))  ");
		sql.append(" 				AND fstatus = '1'  ");
		sql.append(" 				AND ( ");
		sql.append(" 					fholidaypolicyid IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000015Y' )  ");
		sql.append(" 				)  ");
		sql.append(" 			GROUP BY ");
		sql.append(" 				fproposerid  ");
		sql.append(" 			) child ON child.fpersonid= tbp.fid  ");
		sql.append(" 		) tempa ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tema.FPERSONID FPERSONID, ");
		sql.append(" 			SUM ( tema.AnnualLeave ) AnnualLeave  ");
		sql.append(" 		FROM ");
		sql.append(" 			( ");
		sql.append(" 			SELECT ");
		sql.append(" 				tem.fpersonId FPERSONID, ");
		sql.append(" 			CASE ");
		sql.append(" 					WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 					WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 				END AnnualLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000015Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + firstYearDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000015Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + firstYearDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) tempb ON tempb.FPERSONID= tempa.fpersonid  ");
		sql.append(" 	) htmz ON htmz.fpersonid= tep.fpersonid ");
		sql.append(" 	LEFT JOIN ( ");
		sql.append(" 	SELECT ");
		sql.append(" 		tempa.fpersonid fpersonid, ");
		sql.append(" 		COALESCE ( tempa.ElderlyCareLeave ) - COALESCE ( tempb.AnnualLeave, 0 ) ElderlyCareLeaveBalance  ");
		sql.append(" 	FROM ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tbp.fid fpersonid, ");
		sql.append(" 			COALESCE ( child.ElderlyCareLeave, 0 ) ElderlyCareLeave  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_BD_PERSON tbp ");
		sql.append(" 			LEFT JOIN ( ");
		sql.append(" 			SELECT ");
		sql.append(" 				fproposerid fpersonid, ");
		sql.append(" 				COALESCE ( SUM ( freallimit ), 0 ) ElderlyCareLeave  ");
		sql.append(" 			FROM ");
		sql.append(" 				T_HR_ATS_HolidayLimit  ");
		sql.append(" 			WHERE ");
		sql.append(" 				( fcyclebegindate, fcycleenddate ) OVERLAPS ( ");
		sql.append(" 					to_date ( '" + firstYearDate + "', 'yyyy-MM-dd' ), ");
		sql.append(" 				to_date ( '" + cutoffDate + "', 'yyyy-MM-dd' ))  ");
		sql.append(" 				AND fstatus = '1'  ");
		sql.append(" 				AND ( ");
		sql.append(" 					fholidaypolicyid IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000016Y' )  ");
		sql.append(" 				)  ");
		sql.append(" 			GROUP BY ");
		sql.append(" 				fproposerid  ");
		sql.append(" 			) child ON child.fpersonid= tbp.fid  ");
		sql.append(" 		) tempa ");
		sql.append(" 		LEFT JOIN ( ");
		sql.append(" 		SELECT ");
		sql.append(" 			tema.FPERSONID FPERSONID, ");
		sql.append(" 			SUM ( tema.AnnualLeave ) AnnualLeave  ");
		sql.append(" 		FROM ");
		sql.append(" 			( ");
		sql.append(" 			SELECT ");
		sql.append(" 				tem.fpersonId FPERSONID, ");
		sql.append(" 			CASE ");
		sql.append(" 					WHEN tem.status IN ( '3', '4' ) THEN SUM ( tem.fleavelength )  ");
		sql.append(" 					WHEN tem.status IN ( '1', '2' ) THEN SUM ( tem.cfleavelenght )  ");
		sql.append(" 				END AnnualLeave  ");
		sql.append(" FROM ");
		sql.append(" 	( ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			b.fproposerid fpersonId, ");
		sql.append(" 			SUM ( b.fleavelength ) fleavelength, ");
		sql.append(" 			0 cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			b.FPOLICYID IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000016Y' )  ");
		sql.append(" 			AND b.fleavedate >= '" + firstYearDate + "'  ");
		sql.append(" 			AND b.fleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			b.fproposerid  ");
		sql.append(" 		) UNION ");
		sql.append(" 		( ");
		sql.append(" 		SELECT ");
		sql.append(" 			a.fbillstate status, ");
		sql.append(" 			c.cfproposer fpersonId, ");
		sql.append(" 			0 fleavelength, ");
		sql.append(" 			SUM ( c.cfleavelenght ) cfleavelenght  ");
		sql.append(" 		FROM ");
		sql.append(" 			T_HR_ATS_LeaveBill a ");
		sql.append(" 			LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  ");
		sql.append(" 		WHERE ");
		sql.append(" 			c.cfpolicy IN ( SELECT ua.fid FROM T_HR_ATS_HolidayPolicy ua LEFT JOIN T_HR_ATS_HolidayType ub ON ub.FID = ua.FHOLIDAYTYPEID WHERE ub.FNUMBER = 'JQLX000016Y' )  ");
		sql.append(" 			AND c.cfleavedate >= '" + firstYearDate + "'  ");
		sql.append(" 			AND cfleavedate <= '" + cutoffDate + "'  ");
		sql.append(" 		GROUP BY ");
		sql.append(" 			a.fbillstate, ");
		sql.append(" 			c.cfproposer  ");
		sql.append(" 		)  ");
		sql.append(" 	) tem  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tem.status, ");
		sql.append(" 	tem.fpersonId  ");
		sql.append(" 	) tema  ");
		sql.append(" GROUP BY ");
		sql.append(" 	tema.FPERSONID  ");
		sql.append(" 	) tempb ON tempb.FPERSONID= tempa.fpersonid  ");
		sql.append(" 	) htmma ON htmma.fpersonid= tep.fpersonid  ");
		sql.append(" 	) final ");
		sql.append(where);
		return sql.toString();
	}

	public String getDateString(String dateStr)
	{
		return dateStr.substring(0, 10).toString();
	}

	public List<Map<String, Object>> getcolModelAction() {
		List modelColList = new ArrayList();
		modelColList.add(buildColModelMap("Dept", "Dept", 120));
		modelColList.add(buildColModelMap("Staff No.", "Staff No.", 120));
		modelColList.add(buildColModelMap("Name", "Name", 120));
		modelColList.add(buildColModelMap("Position", "Position", 120));
		modelColList.add(buildColModelMap("Work Category", "Work Category", 120));
		modelColList.add(buildColModelMap("Person Type", "Person Type", 120));
		modelColList.add(buildColModelMap("Full Time or Part Time", "Full Time or Part Time", 120));
		modelColList.add(buildColModelMap("Hire date", "Hire date", 120));
		modelColList.add(buildColModelMap("Ter  Date", "Ter  Date", 120));
		modelColList.add(buildColModelMap("Status", "Status", 120));
		modelColList.add(buildColModelMap("Annual Entitlement", "Annual Entitlement", 120));
		modelColList.add(buildColModelMap("Faculty Sick Entitlement", "Faculty Sick Entitlement", 120));
		modelColList.add(buildColModelMap("Faculty Sick Max Entitlement", "Faculty Sick Max Entitlement", 120));
		modelColList.add(buildColModelMap("Child care Entitlement", "Child care Entitlement", 120));
		modelColList.add(buildColModelMap("Elderly care Entitlement", "Elderly care Entitlement", 120));
		modelColList.add(buildColModelMap("annual leave", "annual leave", 120));
		modelColList.add(buildColModelMap("sick leave", "sick leave", 120));
		modelColList.add(buildColModelMap("marriage leave", "marriage leave", 120));
		modelColList.add(buildColModelMap("paternity leave", "paternity leave", 120));
		modelColList.add(buildColModelMap("maternity leave", "maternity leave", 120));
		modelColList.add(buildColModelMap("bereavement leave", "bereavement leave", 120));
		modelColList.add(buildColModelMap("miscarriage leave", "miscarriage leave", 120));
		modelColList.add(buildColModelMap("leave of family planning birth control", "leave of family planning birth control", 120));
		modelColList.add(buildColModelMap("work-related injury leave", "work-related injury leave", 120));
		modelColList.add(buildColModelMap("medical treatment leave", "medical treatment leave", 120));
		modelColList.add(buildColModelMap("unpaid leave(H)", "unpaid leave(H)", 120));
		modelColList.add(buildColModelMap("compensatory leave(H)", "compensatory leave(H)", 120));
		modelColList.add(buildColModelMap("Child Care Leave", "Child Care Leave", 120));
		modelColList.add(buildColModelMap("Elderly Care Leave", "Elderly Care Leave", 120));
		modelColList.add(buildColModelMap("Annual Leave Balance", "Annual Leave Balance", 120));
		modelColList.add(buildColModelMap("sick leave balance", "sick leave balance", 120));
		modelColList.add(buildColModelMap("Compensatory Leave Balance(H)", "Compensatory Leave Balance(H)", 120));
		modelColList.add(buildColModelMap("Child Care Leave Balance", "Child Care Leave Balance", 120));
		modelColList.add(buildColModelMap("Elderly Care Leave Balance", "Elderly Care Leave Balance", 120));
		return modelColList;
	}

	private Map<String, Object> buildColModelMap(String index, String label, int width) {
		return buildColModelMap(index, label, width, false, false, false);
	}

	private Map<String, Object> buildColModelMap(String index, String label, int width, boolean rowspan, boolean isKey, boolean isHedden) {
		Map gridIdMap = new LinkedHashMap();
		gridIdMap.put("index", index);
		gridIdMap.put("name", index);
		gridIdMap.put("label", label);
		gridIdMap.put("width", Integer.valueOf(width));
		if (rowspan) {
			gridIdMap.put("rowspan", Boolean.valueOf(rowspan));
		}
		if (isKey) {
			gridIdMap.put("key", Boolean.valueOf(isKey));
		}
		if (isHedden) {
			gridIdMap.put("hidden", Boolean.valueOf(isHedden));
		}
		return gridIdMap;
	}

	public String toExcelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws BOSException, SQLException, SHRWebException, org.json.JSONException
	{
		IRowSet rs = getIRowSet(request);
		if (null == rs) {
			return null;
		}
		LinkedHashMap selectFieldsTypeMap = new LinkedHashMap();
		String stringType = "String_@";
		String stringDateType = "StringDate_@";
		String numberType = "Number_.";
		selectFieldsTypeMap.put("Dept", stringType);
		selectFieldsTypeMap.put("StaffNo", stringType);
		selectFieldsTypeMap.put("Name", stringType);
		selectFieldsTypeMap.put("Position", stringType);
		selectFieldsTypeMap.put("WorkCategory", stringType);
		selectFieldsTypeMap.put("PersonType", stringType);
		selectFieldsTypeMap.put("FullTimeOrPartTime", stringType);
		selectFieldsTypeMap.put("HireDate", stringDateType);
		selectFieldsTypeMap.put("TerDate", stringDateType);
		selectFieldsTypeMap.put("Status", stringType);
		selectFieldsTypeMap.put("AnnualEntitlement", numberType);
		selectFieldsTypeMap.put("FacultySickEntitlement", numberType);
		selectFieldsTypeMap.put("FacultySickMaxEntitlement", numberType);
		selectFieldsTypeMap.put("ChildCareEntitlement", numberType);
		selectFieldsTypeMap.put("ElderlyCareEntitlement", numberType);
		selectFieldsTypeMap.put("AnnualLeave", numberType);
		selectFieldsTypeMap.put("SickLeave", numberType);
		selectFieldsTypeMap.put("MarriageLeave", numberType);
		selectFieldsTypeMap.put("PaternityLeave", numberType);
		selectFieldsTypeMap.put("MaternityLeave", numberType);
		selectFieldsTypeMap.put("BereavementLeave", numberType);
		selectFieldsTypeMap.put("MiscarriageLeave", numberType);
		selectFieldsTypeMap.put("LeaveOfFamilyPlanningBirthControl", numberType);
		selectFieldsTypeMap.put("WorkRelatedInjuryLeave", numberType);
		selectFieldsTypeMap.put("MedicalTreatmentLeave", numberType);
		selectFieldsTypeMap.put("UnpaidLeaveH", numberType);
		selectFieldsTypeMap.put("CompensatoryLeaveH", numberType);
		selectFieldsTypeMap.put("ChildCareLeave", numberType);
		selectFieldsTypeMap.put("ElderlyCareLeave", numberType);
		selectFieldsTypeMap.put("AnnualLeaveBalance", numberType);
		selectFieldsTypeMap.put("SickLeaveBalance", numberType);
		selectFieldsTypeMap.put("CompensatoryLeaveBalanceH", numberType);
		selectFieldsTypeMap.put("ChildCareLeaveBalance", numberType);
		selectFieldsTypeMap.put("ElderlyCareLeaveBalance", numberType);
		List list = getcolNamesAction();
		ExportAllUtil.exportData(response, rs, "AbsenceSummaryForAllLeaveList.xlsx", selectFieldsTypeMap, list);
		return null;
	}

	public Date getYearFirst(int year)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(1, year);
		return calendar.getTime();
	}

	public Date getYearLast(int year)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(1, year);
		calendar.roll(6, -1);
		return calendar.getTime();
	}
}