public List<AttendanceSummaryReport> GetAttendanceSummaryReport(int iCompany, int iDepartment, DateTime beginDate, DateTime endDate)
        {
            List<AttendanceSummaryReport> attSummarys = new List<AttendanceSummaryReport>();

            List<AttendanceLogReport> attReports = GetAttendanceLogReportList(iCompany, iDepartment, beginDate, endDate);

            if (attReports == null || attReports.Count == 0)
                return null;

            AttendanceSummaryReport attSummary = null;

            foreach (AttendanceLogReport attRp in attReports)
            {
                attSummary = new AttendanceSummaryReport();
                attSummary.EmployeeNumber = attRp.EmployeeNumber;
                attSummary.FullName = attRp.FullName;
                attSummary.TotalHour = attRp.TotalHour;
                attSummary.DateLog = attRp.WorkFrom;
                attSummary.WorkingHour = "Regular Hour : " + attRp.WorkingHour;
                attSummary.ChartData = new double[] { attRp.RegularHour, attRp.WorkingHour, attRp.OvertimeHour1 + attRp.OvertimeHour2 + attRp.OvertimeHour3 + attRp.OvertimeHour4 };
                attSummarys.Add(attSummary);

                if (attRp.OvertimeHour1 > 0)
                {
                    attSummary = new AttendanceSummaryReport();
                    attSummary.WorkingHour = "Overtime Hour 1 : " + attRp.OvertimeHour1;
                    attSummarys.Add(attSummary);

                    if (attRp.OvertimeHour2 > 0)
                    {
                        attSummary = new AttendanceSummaryReport();
                        attSummary.WorkingHour = "Overtime Hour 2 : " + attRp.OvertimeHour2;
                        attSummarys.Add(attSummary);

                        if (attRp.OvertimeHour3 > 0)
                        {
                            attSummary = new AttendanceSummaryReport();
                            attSummary.WorkingHour = "Overtime Hour 3 : " + attRp.OvertimeHour3;
                            attSummarys.Add(attSummary);

                            if (attRp.OvertimeHour4 > 0)
                            {
                                attSummary = new AttendanceSummaryReport();
                                attSummary.WorkingHour = "Overtime Hour 4 : " + attRp.OvertimeHour4;
                                attSummarys.Add(attSummary);
                            }
                        }
                    }
                }
            }

            return attSummarys;
        }
        public List<AttendanceSummaryViewReport> GetAttendanceSummaryReport(int iCompany, int iDepartment, DateTime beginDate, DateTime endDate)
        {
            List<AttendanceSummaryReport> attSummarys = new List<AttendanceSummaryReport>();

            List<AttendanceLogReport> attReports = GetAttendanceLogReportList(iCompany, iDepartment, beginDate, endDate);

            if (attReports == null || attReports.Count == 0)
                return null;

            AttendanceSummaryReport attSummary = null;

            foreach (AttendanceLogReport attRp in attReports)
            {
                //attRp.EmployeeNumber
                attSummary = new AttendanceSummaryReport();
                attSummary.EmployeeNumber = attRp.EmployeeNumber;
                attSummary.FullName = attRp.FullName;
                attSummary.TotalHour = attRp.TotalHour;
                attSummary.DateLog = attRp.WorkFrom;
                attSummary.WorkingHour = "Regular Hour : " + attRp.WorkingHour;
                attSummary.ChartData = new double[] { attRp.RegularHour, attRp.WorkingHour, attRp.OvertimeHour1 + attRp.OvertimeHour2 + attRp.OvertimeHour3 + attRp.OvertimeHour4 };
                attSummarys.Add(attSummary);

                if (attRp.OvertimeHour1 > 0)
                {
                    attSummary = new AttendanceSummaryReport();
                    attSummary.EmployeeNumber = attRp.EmployeeNumber;
                    attSummary.DateLog = attRp.WorkFrom;
                    attSummary.WorkingHour = "Overtime Hour 1 : " + attRp.OvertimeHour1;
                    attSummarys.Add(attSummary);

                    if (attRp.OvertimeHour2 > 0)
                    {
                        attSummary = new AttendanceSummaryReport();
                        attSummary.EmployeeNumber = attRp.EmployeeNumber;
                        attSummary.DateLog = attRp.WorkFrom;
                        attSummary.WorkingHour = "Overtime Hour 2 : " + attRp.OvertimeHour2;
                        attSummarys.Add(attSummary);

                        if (attRp.OvertimeHour3 > 0)
                        {
                            attSummary = new AttendanceSummaryReport();
                            attSummary.EmployeeNumber = attRp.EmployeeNumber;
                            attSummary.DateLog = attRp.WorkFrom;
                            attSummary.WorkingHour = "Overtime Hour 3 : " + attRp.OvertimeHour3;
                            attSummarys.Add(attSummary);

                            if (attRp.OvertimeHour4 > 0)
                            {
                                attSummary = new AttendanceSummaryReport();
                                attSummary.EmployeeNumber = attRp.EmployeeNumber;
                                attSummary.DateLog = attRp.WorkFrom;
                                attSummary.WorkingHour = "Overtime Hour 4 : " + attRp.OvertimeHour4;
                                attSummarys.Add(attSummary);
                            }
                        }
                    }
                }
            }

            if (attSummarys.Count == 0)
                return null;

            int employeeNumber = 0, flexiHours = 0;//, wcalRegHour = 8;
            bool applyFlexiHours = false, isFirst = true;
            WorkingCalendar wCal = null;
            PaymentRate paymentRate = null;
            List<Shift> shiftList = new List<Shift>();
            DayOfWeek weekStartsOn = DayOfWeek.Monday;
            DateTime beginFlexiDate, endFlexiDate;
            List<AttendanceSummaryViewReport> attSummarysRs = new List<AttendanceSummaryViewReport>();
            DateTime duplicatedate = DateTime.MinValue;
            while (attSummarys.Count > 0)
            {
                AttendanceSummaryReport attSumRp = attSummarys[0];
                if (employeeNumber == attSumRp.EmployeeNumber)
                {
                    if (applyFlexiHours)
                    {

                        beginFlexiDate = attSumRp.DateLog;
                        endFlexiDate = beginFlexiDate;

                        if (weekStartsOn.Equals(attSumRp.DateLog.DayOfWeek))
                        {
                            endFlexiDate = beginFlexiDate.AddDays(6);//Week
                        }
                        else
                        {
                            while (true)
                            {
                                if (weekStartsOn.Equals(endFlexiDate.DayOfWeek))
                                    break;
                                endFlexiDate = endFlexiDate.AddDays(1);
                            }
                            endFlexiDate = endFlexiDate.AddDays(-1);
                        }

                        List<AttendanceSummaryReport> attSummarysSub = attSummarys.FindAll(delegate(AttendanceSummaryReport e) { return e.EmployeeNumber == employeeNumber && e.DateLog.CompareTo(beginFlexiDate) != -1 && e.DateLog.CompareTo(endFlexiDate) != 1; });
                        if (attSummarysSub == null || attSummarysSub.Count == 0)
                            continue;

                        List<AttendanceSummaryReport> attSummarys_1 = new List<AttendanceSummaryReport>();

                        foreach (AttendanceSummaryReport attRp in attSummarysSub)
                        {
                            if (attRp.DateLog.Equals(duplicatedate))
                            {
                                attSummarys_1[attSummarys_1.Count - 1].WorkingHour = "Regular Hour : " + attSummarys_1[attSummarys_1.Count - 1].TotalHour;
                            }
                            else
                                attSummarys_1.Add(attRp);

                            duplicatedate = attRp.DateLog;
                        }

                        isFirst = true;
                        double dFlexiHours = Convert.ToDouble(flexiHours);
                        AttendanceSummaryViewReport attRp_1;

                        double totalOvertimeHour = 0;
                        int rateNumber = 1;
                        List<string> overtimeHours = null;
                        foreach (AttendanceSummaryReport attRp in attSummarys_1)
                        {
                            attRp_1 = new AttendanceSummaryViewReport();
                            attRp_1.ChartData = attRp.ChartData;
                            attRp_1.DateLog = attRp.DateLog.ToString("d MMM yyyy");
                            attRp_1.DateLogTime = attRp.DateLog;
                            attRp_1.WorkingHour = attRp.WorkingHour;
                            attRp_1.TotalHour = attRp.TotalHour;
                            attRp_1.EmployeeNumber = 0;
                            attRp_1.FullName = null;

                            if (isFirst)
                            {
                                dFlexiHours -= attRp.TotalHour;
                                attRp_1.EmployeeNumber = attRp.EmployeeNumber;
                                attRp_1.FullName = attRp.FullName;
                                double[] chartData = attRp.ChartData;
                                chartData[1] = attRp.TotalHour;
                                chartData[2] = 0;
                                attRp_1.ChartData = chartData;
                                attSummarysRs.Add(attRp_1);
                                isFirst = false;
                            }
                            else
                            {

                                if (dFlexiHours > 0)
                                {
                                    if (dFlexiHours < attRp.TotalHour)
                                    {
                                        attRp_1.WorkingHour = "Regular Hour : " + dFlexiHours;
                                        double[] chartData = attRp.ChartData;
                                        chartData[1] = dFlexiHours;
                                        chartData[2] = attRp.TotalHour - dFlexiHours;
                                        attRp_1.ChartData = chartData;
                                        attSummarysRs.Add(attRp_1);

                                        overtimeHours = new List<string>();
                                        totalOvertimeHour += attRp.TotalHour - dFlexiHours;
                                        GetOverTimeHour(ref overtimeHours, ref rateNumber, ref totalOvertimeHour, paymentRate);
                                        foreach (string strOvertime in overtimeHours)
                                        {
                                            attRp_1 = new AttendanceSummaryViewReport();
                                            attRp_1.ChartData = null;
                                            attRp_1.DateLog = null;
                                            attRp_1.DateLogTime = attRp.DateLog;
                                            attRp_1.TotalHour = -1;
                                            attRp_1.EmployeeNumber = 0;
                                            attRp_1.FullName = null;
                                            attRp_1.WorkingHour = strOvertime;
                                            attSummarysRs.Add(attRp_1);
                                        }
                                    }
                                    else
                                    {
                                        double[] chartData = attRp.ChartData;
                                        chartData[1] = attRp.TotalHour;
                                        chartData[2] = 0;
                                        attRp_1.ChartData = chartData;
                                        attSummarysRs.Add(attRp_1);
                                    }
                                    dFlexiHours -= attRp.TotalHour;
                                }
                                else
                                {
                                    overtimeHours = new List<string>();
                                    totalOvertimeHour += attRp.TotalHour;
                                    GetOverTimeHour(ref overtimeHours, ref rateNumber, ref totalOvertimeHour, paymentRate);
                                    for (int i = 0; i < overtimeHours.Count; i++)
                                    {
                                        attRp_1 = new AttendanceSummaryViewReport();
                                        attRp_1.WorkingHour = overtimeHours[i];
                                        if (i == 0)
                                        {
                                            attRp_1.DateLog = attRp.DateLog.ToString("d MMM yyyy");
                                            attRp_1.DateLogTime = attRp.DateLog;
                                            attRp_1.TotalHour = attRp.TotalHour;
                                            attRp_1.EmployeeNumber = 0;
                                            attRp_1.FullName = null;
                                            double[] chartData = attRp.ChartData;
                                            chartData[1] = 0;
                                            chartData[2] = attRp.TotalHour;
                                            attRp_1.ChartData = chartData;
                                        }

                                        attSummarysRs.Add(attRp_1);
                                    }
                                }
                            }
                        }

                        attSummarys.RemoveAll(delegate(AttendanceSummaryReport e) { return e.EmployeeNumber == employeeNumber && e.DateLog.CompareTo(beginFlexiDate) != -1 && e.DateLog.CompareTo(endFlexiDate) != 1; });

                    }
                    else
                    {
                        if (shiftList.Count > 1)
                        {
                            AttendanceSummaryViewReport attRp_1 = new AttendanceSummaryViewReport();
                            attRp_1.ChartData = attSumRp.ChartData;
                            attRp_1.DateLogTime = attSumRp.DateLog;
                            attRp_1.WorkingHour = attSumRp.WorkingHour;

                            if (duplicatedate.Equals(attSumRp.DateLog))
                            {
                                attRp_1.TotalHour = -1;
                                attRp_1.EmployeeNumber = 0;
                            }
                            else
                            {
                                attRp_1.DateLog = attSumRp.DateLog.ToString("d MMM yyyy");
                                attRp_1.TotalHour = attSumRp.TotalHour;
                                attRp_1.EmployeeNumber = attSumRp.EmployeeNumber;
                                attRp_1.FullName = attSumRp.FullName;
                            }

                            int shiftNumber = -1;
                            if (!attSumRp.DateLog.Equals(DateTime.MinValue))
                            {
                                shiftNumber = GetShiftNumber(attSumRp.DateLog, wCal, shiftList, false);
                                if (attRp_1.DateLog != null)
                                    attRp_1.DateLog += " [Shift " + shiftNumber + "]";
                            }
                            duplicatedate = attSumRp.DateLog;

                            attSummarysRs.Add(attRp_1);
                            attSummarys.Remove(attSumRp);
                        }
                        else
                        {
                            AttendanceSummaryViewReport attRp_1 = new AttendanceSummaryViewReport();
                            attRp_1.ChartData = attSumRp.ChartData;
                            attRp_1.DateLogTime = attSumRp.DateLog;
                            attRp_1.WorkingHour = attSumRp.WorkingHour;

                            if (duplicatedate.Equals(attSumRp.DateLog))
                            {
                                attRp_1.TotalHour = -1;
                                attRp_1.EmployeeNumber = 0;
                            }
                            else
                            {
                                attRp_1.DateLog = attSumRp.DateLog.ToString("d MMM yyyy");
                                attRp_1.TotalHour = attSumRp.TotalHour;
                                attRp_1.EmployeeNumber = attSumRp.EmployeeNumber;
                                attRp_1.FullName = attSumRp.FullName;
                            }

                            duplicatedate = attSumRp.DateLog;

                            attSummarysRs.Add(attRp_1);
                            attSummarys.Remove(attSumRp);
                        }
                    }
                }
                else
                {
                    employeeNumber = attSumRp.EmployeeNumber;
                    wCal = GetWorkingCalendarByEmployee(employeeNumber);
                    shiftList = GetShiftListByWorkingCalendar(wCal.ID);
                    paymentRate = GetWorkingDayPaymentRateByWorkingCalendar(wCal.ID);
                    applyFlexiHours = wCal.ApplyFlexiHours;
                    flexiHours = wCal.FlexiHours;
                    weekStartsOn = GetDayOfWeek(wCal.WeekStartsOn);
                }
            }

            List<RoostedDayOff> roostedDayOffList = GetRoostedDayOffList();
            Hashtable hasEmplName = new Hashtable();
            if (roostedDayOffList.Count > 0)
            {
                AttendanceSummaryViewReport attReport = null;
                foreach (RoostedDayOff roostedDayOff in roostedDayOffList)
                {
                    double regularHour = 0;
                    string employeeName = null;
                    if (hasEmplName.ContainsKey(roostedDayOff.EmployeeNumber))
                    {
                        object[] objData= (object[])hasEmplName[roostedDayOff.EmployeeNumber];
                        employeeName = (string)objData[0];
                        regularHour =  (double)objData[1];
                    }
                    else
                    {
                        Employee employee = GetEmployeeByEmployeeNumber(roostedDayOff.EmployeeNumber);
                        if (employee == null)
                            continue;

                        employeeName = employee.LastName + ", " + employee.FirstName;

                        AttendanceSummaryViewReport attViewRp = attSummarysRs.Find(delegate(AttendanceSummaryViewReport e) { return e.EmployeeNumber == roostedDayOff.EmployeeNumber; });
                        if (attViewRp != null)
                            regularHour = attViewRp.ChartData[0];
                        else
                        {
                            wCal = GetWorkingCalendarByEmployee(roostedDayOff.EmployeeNumber);
                            paymentRate = GetWorkingDayPaymentRateByWorkingCalendar(wCal.ID);
                            regularHour = paymentRate.NumberOfRegularHours;
                        }
                        hasEmplName.Add(roostedDayOff.EmployeeNumber, new object[] { employeeName, regularHour });
                    }

                    attReport = new AttendanceSummaryViewReport();
                    attReport.EmployeeNumber = roostedDayOff.EmployeeNumber;
                    attReport.FullName = employeeName;
                    attReport.DateLog = roostedDayOff.Date.ToString("d MMM yyyy");
                    attReport.DateLogTime = roostedDayOff.Date;
                    attReport.ChartData = new double[] { regularHour, roostedDayOff.TotalHours, 0 };
                    attReport.TotalHour = roostedDayOff.TotalHours;
                    attReport.WorkingHour = "Roosted day off";

                    int indexRp = attSummarysRs.FindIndex(0, delegate(AttendanceSummaryViewReport e) { return e.EmployeeNumber == attReport.EmployeeNumber && e.DateLogTime.Date.CompareTo(attReport.DateLogTime.Date) == 1; });
                    if (indexRp < 0)
                    {
                        indexRp = attSummarysRs.FindLastIndex(delegate(AttendanceSummaryViewReport e) { return e.EmployeeNumber == attReport.EmployeeNumber; });

                        if (indexRp < 0)
                        {
                            indexRp = attSummarysRs.FindIndex(delegate(AttendanceSummaryViewReport e) { return e.EmployeeNumber > attReport.EmployeeNumber; });

                            if (indexRp < 0)
                            {
                                indexRp = attSummarysRs.Count;
                            }
                        }
                        else
                            indexRp++;
                    }

                    int indexRp_1 = 0;
                    while (true)
                    {
                        if (indexRp > attSummarysRs.Count - 1)
                            break;
                        indexRp_1 = attSummarysRs.FindIndex(indexRp, 1, delegate(AttendanceSummaryViewReport e) { return e.DateLog == null; });
                        if (indexRp_1 < 1)
                            break;
                        indexRp++;
                    }

                    attSummarysRs.Insert(indexRp, attReport);
                }
                hasEmplName.Clear();
                roostedDayOffList.Clear();
            }
            return attSummarysRs;
        }