Example #1
0
        public IEnumerable <EmployeeDTO> GetAll(SearchCriteria <EmployeeDTO> criteria, out int totCount)
        {
            totCount = 0;
            IEnumerable <EmployeeDTO> catEmployee;

            try
            {
                if (criteria != null)
                {
                    IRepositoryQuery <EmployeeDTO> pdto = Get();

                    foreach (var cri in criteria.FiList)
                    {
                        pdto.FilterList(cri);
                    }

                    #region By Duration

                    if (criteria.BeginingDate != null)
                    {
                        var beginDate = new DateTime(criteria.BeginingDate.Value.Year, criteria.BeginingDate.Value.Month,
                                                     criteria.BeginingDate.Value.Day, 0, 0, 0);
                        switch (criteria.ReportType)
                        {
                        case ReportTypes.TicketList:
                        case ReportTypes.TicketAmountList:
                        case ReportTypes.LabourMonthly:
                            pdto.FilterList(p => p.FlightProcess.SubmitDate >= beginDate);
                            break;

                        case ReportTypes.LabourReturned:
                        case ReportTypes.LabourLost:
                            pdto.FilterList(p => p.AfterFlightStatusDate >= beginDate);
                            break;

                        case ReportTypes.LabourContractEnd:
                            pdto.FilterList(p => p.LabourProcess.ContratEndDate >= beginDate);
                            break;

                        case ReportTypes.LabourDiscontinued:
                            pdto.FilterList(p => p.DiscontinuedDate >= beginDate);
                            break;

                        case ReportTypes.EmbassyMonthly:
                            pdto.FilterList(p => p.EmbassyProcess.SubmitDate >= beginDate);
                            break;
                        }
                    }

                    if (criteria.EndingDate != null)
                    {
                        var endDate = new DateTime(criteria.EndingDate.Value.Year, criteria.EndingDate.Value.Month,
                                                   criteria.EndingDate.Value.Day, 23, 59, 59);
                        switch (criteria.ReportType)
                        {
                        case ReportTypes.TicketList:
                        case ReportTypes.TicketAmountList:
                        case ReportTypes.LabourMonthly:
                            pdto.FilterList(p => p.FlightProcess.SubmitDate <= endDate);
                            break;

                        case ReportTypes.LabourReturned:
                        case ReportTypes.LabourLost:
                            pdto.FilterList(p => p.AfterFlightStatusDate <= endDate);
                            break;

                        case ReportTypes.LabourContractEnd:
                            pdto.FilterList(p => p.LabourProcess.ContratEndDate <= endDate);
                            break;

                        case ReportTypes.LabourDiscontinued:
                            pdto.FilterList(p => p.DiscontinuedDate <= endDate);
                            break;

                        case ReportTypes.EmbassyMonthly:
                            pdto.FilterList(p => p.EmbassyProcess.SubmitDate <= endDate);
                            break;
                        }
                    }

                    #endregion

                    #region Filter By User

                    if (Singleton.Edition == PinnaFaceEdition.WebEdition && criteria.CurrentUserId != 1)
                    {
                        var currentUser = new UserService(true)
                                          .GetAll(new UserSearchCriteria <UserDTO>())
                                          .FirstOrDefault(u => u.UserId == criteria.CurrentUserId);

                        if (currentUser != null && currentUser.AgenciesWithAgents != null &&
                            currentUser.AgenciesWithAgents.Count > 0)
                        {
                            IList <Expression <Func <EmployeeDTO, bool> > > filtersExpressions =
                                new List <Expression <Func <EmployeeDTO, bool> > >();

                            var agencyAgents = currentUser.AgenciesWithAgents;
                            foreach (var agencyAgentsDto in agencyAgents)
                            {
                                var dto = agencyAgentsDto;

                                filtersExpressions.Add(e =>
                                                       (e.AgencyId == dto.AgencyAgent.AgencyId &&
                                                        e.AgentId == dto.AgencyAgent.AgentId) ||
                                                       (e.AgencyId == dto.AgencyAgent.AgencyId &&
                                                        e.AgentId == null));
                                //(e.AgencyId == null && ALWAYS TRUE
                                //e.AgentId == dto.AgencyAgent.AgentId) ||
                            }

                            var filtersExp = filtersExpressions.FirstOrDefault();
                            foreach (var filtersExpression in filtersExpressions.Skip(1))
                            {
                                filtersExp = filtersExp.Or(filtersExpression);
                            }
                            pdto.FilterList(filtersExp);
                        }
                        else// To Filter out users with no AgenciesWithAgents
                        {
                            pdto.FilterList(e => e.MoreNotes == "No Notes");
                        }
                    }

                    #endregion

                    IList <EmployeeDTO> pdtoEmployee;
                    if (criteria.Page != 0 && criteria.PageSize != 0)
                    {
                        int totalCount;
                        pdtoEmployee = pdto.GetPage(criteria.Page, criteria.PageSize, out totalCount).ToList();
                        totCount     = totalCount;
                    }
                    else
                    {
                        pdtoEmployee = pdto.GetList().ToList();
                    }

                    catEmployee = pdtoEmployee.ToList();
                }
                else
                {
                    catEmployee = Get().Get().ToList();
                }
            }
            finally
            {
                Dispose(_disposeWhenDone);
            }

            return(catEmployee);
        }