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 AbsenceSummaryForAllLeaveListHandler extends ListHandler
{
    private static final Logger log = LoggerFactory.getLogger(AbsenceSummaryForAllLeaveListHandler.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 = "";
        if (ObjectUtils.isNotEmpty(statusValues)) {
            if ("1".equals(statusValues))
            {
                where = "  where final.Status='Active' ";
            }
            else {
                where = "  where 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)
    {
        return "/*dialect*/select final.Dept Dept, final.StaffNo StaffNo, final.Name Name,final.ftorpt ftorpt,  " +
                "final.POSITION POSITION, final.WorkCategory WorkCategory, final.PersonType PersonType, " +
                "final.FullTimeOrPartTime FullTimeOrPartTime, final.HireDate HireDate, final.TerDate TerDate, " +
                "final.Status Status, case when final.FullTimeOrPartTime='Part Time' then 0 else COALESCE(final.AnnualEntitlement,0) end  AnnualEntitlement, " +
                "case when (final.WorkCategory='General Administrative Staff' or final.WorkCategory='Professional Supervisory Staff' or final.WorkCategory='Senior Administrative Staff') and final.FacultySickEntitlement='0' then 15 else COALESCE(final.FacultySickEntitlement,0) end FacultySickEntitlement, " +
                "case when (final.WorkCategory='General Administrative Staff' or final.WorkCategory='Professional Supervisory Staff' or final.WorkCategory='Senior Administrative Staff') and final.FacultySickMaxEntitlement='0' then 75 else COALESCE(final.FacultySickMaxEntitlement,0) end FacultySickMaxEntitlement, " +
                "COALESCE(final.ChildCareEntitlement,0) ChildCareEntitlement, COALESCE(final.ElderlyCareEntitlement,0) ElderlyCareEntitlement, COALESCE(final.AnnualLeave,0)  AnnualLeave, " +
                "COALESCE(final.SickLeave,0) SickLeave, COALESCE(final.MarriageLeave,0) MarriageLeave, " +
                "COALESCE(final.PaternityLeave,0) PaternityLeave, COALESCE(final.MaternityLeave,0) MaternityLeave, " +
                "COALESCE(final.BereavementLeave,0) BereavementLeave, COALESCE(final.MiscarriageLeave,0) MiscarriageLeave, " +
                "COALESCE(final.LeaveOfFamilyPlanningBirthControl,0) LeaveOfFamilyPlanningBirthControl, " +
                "COALESCE(final.WorkRelatedInjuryLeave,0) WorkRelatedInjuryLeave, COALESCE(final.MedicalTreatmentLeave,0) MedicalTreatmentLeave, " +
                "COALESCE(final.UnpaidLeaveH,0) UnpaidLeaveH, COALESCE(final.CompensatoryLeaveH,0) CompensatoryLeaveH, " +
                "COALESCE(final.ChildCareLeave,0) ChildCareLeave, COALESCE(final.ElderlyCareLeave,0) ElderlyCareLeave, " +
                "case when final.FullTimeOrPartTime='Part Time' then 0 else COALESCE(final.AnnualLeaveBalance,0) end   AnnualLeaveBalance,  " +
                "CASE   WHEN final.WorkCategory= 'Academic Staff'   OR final.WorkCategory= 'Others' THEN  COALESCE ( final.SickLeaveBalance, 0 ) when   (  COALESCE ( final.SickLeaveBalance, 0 ) >case when (final.WorkCategory='General Administrative Staff' or final.WorkCategory='Professional Supervisory Staff' or final.WorkCategory='Senior Administrative Staff') and final.FacultySickMaxEntitlement='0' then 75 else COALESCE(final.FacultySickMaxEntitlement,0) end) then case when (final.WorkCategory='General Administrative Staff' or final.WorkCategory='Professional Supervisory Staff' or final.WorkCategory='Senior Administrative Staff') and final.FacultySickMaxEntitlement='0' then 75 else COALESCE(final.FacultySickMaxEntitlement,0) end     when ( COALESCE ( final.SickLeaveBalance, 0 ) <=case when (final.WorkCategory='General Administrative Staff' or final.WorkCategory='Professional Supervisory Staff' or final.WorkCategory='Senior Administrative Staff') and final.FacultySickMaxEntitlement='0' then 75 else COALESCE(final.FacultySickMaxEntitlement,0) end) then COALESCE ( final.SickLeaveBalance, 0 ) end SickLeaveBalance,  COALESCE(final.CompensatoryLeaveBalanceH,0) CompensatoryLeaveBalanceH, COALESCE(final.ChildCareLeaveBalance,0) ChildCareLeaveBalance, COALESCE(final.ElderlyCareLeaveBalance,0) ElderlyCareLeaveBalance from ( select * from  (select  tbp.fid fpersonid, bcc.fname_l1 AS Dept,  tbp.fnumber AS StaffNo,  tbp.fname_l1 AS Name, tbp.cfftorptid AS ftorpt,  bd.fname_l1 AS POSITION, " +
                "be.fname_l1 AS WorkCategory,  hepc.fname_l1 AS PersonType,  bg.fname_l1 AS FullTimeOrPartTime,  hmrc.joinDate AS HireDate,  bb.fleftdate AS TerDate,    " +
                "case  when bh.finservice='1' then CAST ('Active' AS VARCHAR ( 10 ))  when bh.finservice='0' then CAST ('Negative' AS VARCHAR ( 10 ))   when bh.finservice='2' then CAST ('Negative' AS VARCHAR ( 10 ))   when bh.finservice='3' then CAST ('Negative' AS VARCHAR ( 10 ))    when bh.finservice='4' then CAST ('Active' AS VARCHAR ( 10 ))  END AS Status  " +
                "from  (select * from T_BD_PERSON where fhiredate<='" + cutoffDate + "') tbp left join (select bahis.* from T_HR_EmpOrgRelation bahis where bahis.fid in  " +
                "(select hera.fid from  T_HR_EmpOrgRelation hera inner join (select MAX(fleffdt) maxDate,fpersonid fpersonid,fid fid from T_HR_EmpOrgRelation where FIsLatestInAday = '1' and  FAssignType = '1' " +
                "and (feffdt <='" + cutoffDate + "' and fleffdt>='" + cutoffDate + "')  group by fpersonid,fid) herb on hera.fid=herb.fid)) temp on tbp.fid=temp.fpersonid " +
                "LEFT JOIN T_ORG_Admin bcc ON bcc.fid= temp.fadminorgid " +
                "left join T_HR_EmployeeClassify hepc on hepc.fid=temp.cfpersontypeid " +
                " LEFT JOIN T_ORG_Position bd ON temp.fpositionid = bd.fid " +
                " LEFT JOIN CT_MP_WorkerCategory be ON be.fid = temp.cfworkercategoryid " +
                " LEFT JOIN CT_MP_Fullorpart bg ON bg.fid = tbp.CFFtorptID " +
                " 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 " +
                " LEFT JOIN (select hpipa.fleftdate fleftdate, hpipa.fpersonid fpersonid from T_HR_PersonPositionHis hpipa inner join (select Max(fleffdt) maxDate,fpersonid fpersonid from  T_HR_PersonPositionHis group by fpersonid) hpipb on hpipa.fleffdt=hpipb.maxDate and hpipa.fpersonid=hpipb.fpersonid " +
                " ) bb ON temp.fpersonid = bb.fpersonid " +
                " left join (select hmra.fpersonid,hmra.fenterdate joinDate  from T_HR_EmpLaborRelationHis hmra inner join  (select Max(fleffdt) maxDate,fpersonid fpersonid from T_HR_EmpLaborRelationHis where fenterdate<='" + cutoffDate + "' group by fpersonid ) hmrb on hmra.fleffdt=hmrb.maxDate and hmra.fpersonid=hmrb.fpersonid) hmrc on hmrc.fpersonid=temp.fpersonid) tep " + "  " + " left join ( " + " SELECT " + " fproposerid fpersonid, " + " COALESCE(cfentitlementlimit,0) AnnualEntitlement  " + " FROM " + " T_HR_ATS_HolidayLimit  " + " WHERE " + " fcyclebegindate<='" + cutoffDate + "' and fcycleenddate>='" + cutoffDate + "' " + " AND fstatus = '1'  " + " AND ( " + "  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' )  " + "  ) " + ") htmab on htmab.fpersonid=tep.fpersonid  " + " " + "left join (select hera.fhistoryrelateid fpersonid,hera.cffse FacultySickEntitlement from  T_BD_PersonHis hera 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 and hera.fleffdt=herb.maxDate) htma on htma.fpersonid=tep.fpersonid " + "  " + " left join(select hera.fhistoryrelateid fpersonid,hera.cffsme FacultySickMaxEntitlement,hera.fleffdt fleffdt  from  T_BD_PersonHis hera 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 and hera.fleffdt=herb.maxDate) htmb on htmb.fpersonid=tep.fpersonid " + "  " + " left join(SELECT " + " fproposerid fpersonid, " + " COALESCE(sum(fstandardlimit),0) ChildCareEntitlement  " + " FROM " + " T_HR_ATS_HolidayLimit  " + " WHERE " + " fcyclebegindate<='" + cutoffDate + "' and fcycleenddate>='" + cutoffDate + "' " + " AND fstatus = '1'  " + " AND ( " + "  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' )  " + "  ) group by fproposerid) htmc on htmc.fpersonid=tep.fpersonid " + "   " + "  left join(SELECT " + " fproposerid fpersonid, " + " COALESCE(sum(fstandardlimit),0) ElderlyCareEntitlement  " + " FROM " + " T_HR_ATS_HolidayLimit  " + " WHERE " + " EXTRACT(YEAR FROM fcyclebegindate)= '" + year + "'  " + " AND fstatus = '1'  " + " AND ( " + "  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' )  " + "  ) group by fproposerid) htmd on htmd.fpersonid=tep.fpersonid " + "   " + "  left join ( SELECT " + " tema.FPERSONID, " + " SUM ( tema.AnnualLeave ) AnnualLeave  " + "FROM " + " ( " + " SELECT " + " tem.fpersonId FPERSONID, " + " tem.leavedate leavedate, " + " CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END AnnualLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " distinct a.fbillstate status, " + " b.fproposerid fpersonId, " + " b.fleavedate leavedate, " + "  b.fleavelength  fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " ) UNION " + " ( " + " SELECT " + " distinct " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " c.cfleavedate leavedate, " + " 0 fleavelength, " + " c.cfleavelenght  cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId, " + "tem.leavedate  " + " ) tema  " + "GROUP BY " + " tema.FPERSONID ) htme on htme.FPERSONID=tep.fpersonid " + "  " + " left join ( select tema.FPERSONID  FPERSONID,sum(tema.SickLeave) SickLeave from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END SickLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID ) htmf on htmf.FPERSONID=tep.fpersonid " + "  " + " left join( select tema.FPERSONID FPERSONID , sum(tema.MarriageLeave) MarriageLeave from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END MarriageLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID ) htmh on htmh.FPERSONID=tep.fpersonid " + " left join( select tema.FPERSONID FPERSONID,sum(tema.PaternityLeave) PaternityLeave from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END PaternityLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) htmi on htmi.FPERSONID=tep.fpersonid " + " left join( select tema.FPERSONID FPERSONID,sum(tema.MaternityLeave) MaternityLeave from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END MaternityLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) htmj on htmj.FPERSONID=tep.fpersonid " + "  " + " left join( select tema.FPERSONID FPERSONID,sum(tema.BereavementLeave) BereavementLeave from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END BereavementLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) htmk on htmk.FPERSONID=tep.fpersonid " + "  " + " left join( select tema.FPERSONID FPERSONID,sum(tema.MiscarriageLeave) MiscarriageLeave from ( SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END MiscarriageLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) html on html.FPERSONID=tep.fpersonid " + "  " + " left join ( select tema.FPERSONID FPERSONID, sum(tema.LeaveOfFamilyPlanningBirthControl) LeaveOfFamilyPlanningBirthControl from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END LeaveOfFamilyPlanningBirthControl  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) htmm on htmm.FPERSONID=tep.fpersonid " + "  " + " left join ( select tema.FPERSONID FPERSONID, sum(tema.WorkRelatedInjuryLeave) WorkRelatedInjuryLeave from( SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END WorkRelatedInjuryLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) htmn on htmn.FPERSONID=tep.fpersonid " + " left join( select tema.FPERSONID FPERSONID, sum(tema.MedicalTreatmentLeave) MedicalTreatmentLeave from(SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END MedicalTreatmentLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) htmo on htmo.FPERSONID=tep.fpersonid " + "  " + " left join( select tema.FPERSONID FPERSONID,sum(tema.UnpaidLeaveH) UnpaidLeaveH from ( SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END UnpaidLeaveH  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) htmp on htmp.FPERSONID=tep.fpersonid " + "  " + " left join ( select tema.FPERSONID FPERSONID, sum(tema.CompensatoryLeaveH) CompensatoryLeaveH from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END CompensatoryLeaveH  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) htmq on htmq.FPERSONID=tep.fpersonid " + "  " + " left join (SELECT " + " tema.FPERSONID, " + " SUM ( tema.AnnualLeave ) ChildCareLeave  " + "FROM " + " ( " + " SELECT " + " tem.fpersonId FPERSONID, " + " tem.leavedate leavedate, " + " CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END AnnualLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " distinct a.fbillstate status, " + " b.fproposerid fpersonId, " + " b.fleavedate leavedate, " + "  b.fleavelength  fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " ) UNION " + " ( " + " SELECT " + " distinct " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " c.cfleavedate leavedate, " + " 0 fleavelength, " + " c.cfleavelenght  cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId, " + "tem.leavedate  " + " ) tema  " + "GROUP BY " + " tema.FPERSONID) htmqa on htmqa.FPERSONID=tep.fpersonid " + " left join (SELECT " + " tema.FPERSONID, " + " SUM ( tema.AnnualLeave ) ElderlyCareLeave  " + "FROM " + " ( " + " SELECT " + " tem.fpersonId FPERSONID, " + " tem.leavedate leavedate, " + " CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END AnnualLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " distinct a.fbillstate status, " + " b.fproposerid fpersonId, " + " b.fleavedate leavedate, " + "  b.fleavelength  fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + startDate + "'  " + " AND b.fleavedate <= '" + endDate + "'  " + " ) UNION " + " ( " + " SELECT " + " distinct " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " c.cfleavedate leavedate, " + " 0 fleavelength, " + " c.cfleavelenght  cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + startDate + "'  " + " AND cfleavedate <= '" + endDate + "'  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId, " + "tem.leavedate  " + " ) tema  " + "GROUP BY " + " tema.FPERSONID) htmqb on htmqb.FPERSONID=tep.fpersonid " + "  " + " left join(select tempa.fpersonid fpersonid,COALESCE(tempa.freallimit,0)-COALESCE(tempb.AnnualLeave,0) AnnualLeaveBalance from (select tbp.fid fpersonid,COALESCE(temp.freallimit,0) freallimit  from T_BD_PERSON tbp left join ( " + "SELECT " + " fproposerid fpersonid, " + " COALESCE(freallimit,0) freallimit  " + " FROM " + " T_HR_ATS_HolidayLimit  " + " WHERE " + " fcyclebegindate<='" + cutoffDate + "' and fcycleenddate>='" + cutoffDate + "' " + " AND fstatus = '1'  " + " AND ( " + "  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' )  " + "  )) temp on tbp.fid=temp.fpersonid) tempa left join ( SELECT " + " tema.FPERSONID, " + " SUM ( tema.AnnualLeave ) AnnualLeave  " + "FROM " + " ( " + " SELECT " + " tem.fpersonId FPERSONID, " + " tem.leavedate leavedate, " + " CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END AnnualLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " distinct a.fbillstate status, " + " b.fproposerid fpersonId, " + " b.fleavedate leavedate, " + "  b.fleavelength  fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + firstYearDate + "'  " + " AND b.fleavedate <= '" + cutoffDate + "'  " + " ) UNION " + " ( " + " SELECT " + " distinct " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " c.cfleavedate leavedate, " + " 0 fleavelength, " + " c.cfleavelenght  cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + firstYearDate + "'  " + " AND cfleavedate <= '" + cutoffDate + "'  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId, " + "tem.leavedate  " + " ) tema  " + "GROUP BY " + " tema.FPERSONID ) tempb on tempa.fpersonid=tempb.FPERSONID " + ") htmw on htmw.fpersonid=tep.fpersonid " + " " + "left join(select tempa.fpersonid fpersonid,COALESCE(tempa.FacultySickEntitlement,0)-COALESCE(tempb.SickLeave,0) SickLeaveBalance from (select tbp.fid fpersonid,COALESCE(sick.limitCount,0) FacultySickEntitlement from T_BD_PERSON tbp left join (SELECT " + " cfpersonid fpersonid, " + " SUM ( " + " COALESCE ( cfinitiallimit, 0 ) + COALESCE ( cfrealitygrantlimit, 0 )) limitCount  " + "FROM " + " CT_CUS_SickLeaveLimit  " + "WHERE " + " cfmonth <= date_trunc('month', '" + cutoffDate + "'::DATE) - INTERVAL '1 day' " + "GROUP BY " + " cfpersonid) sick on sick.fpersonid=tbp.fid) tempa  " + " left join(select tema.FPERSONID FPERSONID,sum(tema.SickLeave) SickLeave from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END SickLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate <= '" + cutoffDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND cfleavedate <= '" + cutoffDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID) tempb on tempb.FPERSONID=tempa.fpersonid) htmx on htmx.fpersonid=tep.fpersonid " + "  " + " left join(select tempa.fpersonid fpersonid, COALESCE(tempa.CompensatoryLeava)-COALESCE(tempb.AnnualLeave,0) CompensatoryLeaveBalanceH from (select tbp.fid fpersonid,COALESCE(child.CompensatoryLeava,0) CompensatoryLeava from T_BD_PERSON tbp left join(SELECT " + " fproposerid fpersonid, " + " COALESCE(freallimit,0) CompensatoryLeava  " + " FROM " + " T_HR_ATS_HolidayLimit  " + " WHERE " + " EXTRACT(YEAR FROM fcyclebegindate)= '" + year + "' " + " AND fstatus = '1'  " + " AND ( " + "  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' )  " + "  )) child on child.fpersonid=tbp.fid) tempa left join( select tema.FPERSONID FPERSONID,sum(tema.AnnualLeave)  AnnualLeave from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END AnnualLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + firstYearDate + "'  " + " AND b.fleavedate <= '" + cutoffDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + firstYearDate + "'  " + " AND cfleavedate <= '" + cutoffDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID " + ") tempb on tempb.FPERSONID=tempa.fpersonid) htmy on htmy.fpersonid=tep.fpersonid " + " " + "left join(select tempa.fpersonid fpersonid, COALESCE(tempa.ChildCareLeave)-COALESCE(tempb.AnnualLeave,0) ChildCareLeaveBalance from (select tbp.fid fpersonid,COALESCE(child.ChildCareLeave,0) ChildCareLeave from T_BD_PERSON tbp left join(SELECT " + " fproposerid fpersonid, " + " COALESCE(sum(freallimit),0) ChildCareLeave  " + " FROM " + " T_HR_ATS_HolidayLimit  " + " WHERE " + "  (fcyclebegindate, fcycleenddate) overlaps (to_date('" + firstYearDate + "','yyyy-MM-dd'), to_date('" + cutoffDate + "','yyyy-MM-dd')) " + " AND fstatus = '1'  " + " AND ( " + "  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' )  " + "  ) group by fproposerid) child on child.fpersonid=tbp.fid) tempa left join( select tema.FPERSONID FPERSONID,sum(tema.AnnualLeave) AnnualLeave from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END AnnualLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + firstYearDate + "'  " + " AND b.fleavedate <= '" + cutoffDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + firstYearDate + "'  " + " AND cfleavedate <= '" + cutoffDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID " + ") tempb on tempb.FPERSONID=tempa.fpersonid) htmz on htmz.fpersonid=tep.fpersonid " + " " + "left join(select tempa.fpersonid fpersonid, COALESCE(tempa.ElderlyCareLeave)-COALESCE(tempb.AnnualLeave,0) ElderlyCareLeaveBalance from (select tbp.fid fpersonid,COALESCE(child.ElderlyCareLeave,0) ElderlyCareLeave from T_BD_PERSON tbp left join(SELECT " + " fproposerid fpersonid, " + " COALESCE(sum(freallimit),0) ElderlyCareLeave  " + " FROM " + " T_HR_ATS_HolidayLimit  " + " WHERE " + "  (fcyclebegindate, fcycleenddate) overlaps (to_date('" + firstYearDate + "','yyyy-MM-dd'), to_date('" + cutoffDate + "','yyyy-MM-dd')) " + " AND fstatus = '1'  " + " AND ( " + "  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' )  " + "  ) group by fproposerid) child on child.fpersonid=tbp.fid) tempa left join( select tema.FPERSONID FPERSONID,sum(tema.AnnualLeave) AnnualLeave from (SELECT " + " tem.fpersonId FPERSONID, " + "CASE " + "  " + " WHEN tem.status IN ( '3', '4' ) THEN " + " SUM ( tem.fleavelength )  " + " WHEN tem.status IN ( '1', '2' ) THEN " + " SUM ( tem.cfleavelenght )  " + " END AnnualLeave  " + "FROM " + " ( " + " ( " + " SELECT " + " a.fbillstate status, " + " b.fproposerid fpersonId, " + " SUM ( b.fleavelength ) fleavelength, " + " 0 cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN T_HR_ATS_LeaveBillDetail b ON b.FLEAVEBILLID = a.fid  " + " WHERE " + " 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' )  " + " AND b.fleavedate >= '" + firstYearDate + "'  " + " AND b.fleavedate <= '" + cutoffDate + "'  " + " GROUP BY " + " a.fbillstate, " + " b.fproposerid  " + " ) UNION " + " ( " + " SELECT " + " a.fbillstate status, " + " c.cfproposer fpersonId, " + " 0 fleavelength, " + " SUM ( c.cfleavelenght ) cfleavelenght  " + " FROM " + " T_HR_ATS_LeaveBill a " + " LEFT JOIN CT_ATS_AtsLeaveBillDetailSame c ON c.cfleavebill= a.fid  " + " WHERE " + " 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' )  " + " AND c.cfleavedate >= '" + firstYearDate + "'  " + " AND cfleavedate <= '" + cutoffDate + "'  " + " GROUP BY " + " a.fbillstate, " + " c.cfproposer  " + " )  " + " ) tem  " + "GROUP BY " + " tem.status, " + " tem.fpersonId) tema group by tema.FPERSONID " + ") tempb on tempb.FPERSONID=tempa.fpersonid) htmma on htmma.fpersonid=tep.fpersonid " + " " + ") final " + where;
    }

    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();
    }
}